Alberto Perro
Alberto Perro

Reputation: 55

Keras Dense Output is not from 0 to 1

Sorry for the dumb question. I have tried to run the Image Classification Tutorial from Tensorflow (https://www.tensorflow.org/tutorials/images/classification) and after I trained the network, I have tried to make predictions.

Following the guides online, I have found that I had to modify the last layer Dense(1) to Dense(1, activation= 'sigmoid') but the results where not constrained from 0 to 1 but they were of several thousands, positive and negative.

Where am I wrong?

Thanks.

Edit: this is the model I am trying to train

model = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1, activation = 'sigmoid')
])

And this is the result of a prediction:

img = Image.open("D:\\Downloads\\mask-datasets-v1\\Mask_Datasets\\Train\\Mask\\1.PNG").convert('RGB').resize(size=(IMG_HEIGHT,IMG_WIDTH))
img_array = (np.expand_dims(img,0))
img_array.shape
model.predict(img_array)
array([[-4602.1157]], dtype=float32)

Upvotes: 1

Views: 1174

Answers (1)

sempersmile
sempersmile

Reputation: 481

When running the code with example images I get correct results - just as I would expect from having a sigmoid activation in the output layer.

Did you forget to compile your model? https://keras.io/models/model/#compile

So your model is probably still predicting on a deprecated version of your model that does not have the sigmoid activation in its output layer yet.

Upvotes: 2

Related Questions