Sunday, 18 April 2021

Non-Identical results from String Identifier and Actual Class names for activations, loss functions, and metrics

I have the following keras model that is working fine:

model = tf.keras.Sequential(
    [
     #first convolution
     tf.keras.layers.Conv2D(16, (3,3), activation="relu", 
                         input_shape=(IMAGE_SIZE,IMAGE_SIZE,3)),
     tf.keras.layers.MaxPooling2D(2,2),
     #second convolution
     tf.keras.layers.Conv2D(32, (3,3), activation="relu"),
     tf.keras.layers.MaxPooling2D(2,2),
     #third convolution
     tf.keras.layers.Conv2D(64, (3,3), activation="relu"),
     tf.keras.layers.MaxPooling2D(2,2),
     #flatten the results to feed into a DNN
     tf.keras.layers.Flatten(),
     tf.keras.layers.Dense(512, activation="relu"),
     #only 1 neuron, as its a binary classification problem
     tf.keras.layers.Dense(1, activation="sigmoid")
    ]
)
model.compile(optimizer = tf.keras.optimizers.RMSprop(lr=0.001),
              loss="binary_crossentropy", 
              metrics=["acc"])
history = model.fit_generator(train_generator, epochs=15,
             steps_per_epoch=100, validation_data = validation_generator,
             validation_steps=50, verbose=1)

However, when attempting to replace the magic strings, and use actual class names for activations, loss functions, and metrics, I have the following model, which compiles fine, but accuracy is always 0. This model is behaving differently than the one above, with everything else remaining the same. Here is the new model:

model = tf.keras.Sequential(
    [
     #first convolution
     tf.keras.layers.Conv2D(16, (3,3), activation=tf.keras.activations.relu,
                input_shape=(IMAGE_SIZE,IMAGE_SIZE,3)),
     tf.keras.layers.MaxPooling2D(2,2),
     #second convolution
     tf.keras.layers.Conv2D(32, (3,3), activation=tf.keras.activations.relu),
     tf.keras.layers.MaxPooling2D(2,2),
     #third convolution
     tf.keras.layers.Conv2D(64, (3,3), activation=tf.keras.activations.relu),
     tf.keras.layers.MaxPooling2D(2,2),
     #flatten the results to feed into a DNN
     tf.keras.layers.Flatten(),
     tf.keras.layers.Dense(512, activation=tf.keras.activations.relu),
     #only 1 neuron, as its a binary classification problem
     tf.keras.layers.Dense(1, activation=tf.keras.activations.sigmoid)
    ]
)
model.compile(optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001),
              loss=tf.keras.losses.BinaryCrossentropy(), 
              metrics=[tf.keras.metrics.Accuracy()])
history = model.fit_generator(train_generator, epochs=15,
                steps_per_epoch=100, validation_data = validation_generator,
                validation_steps=50, verbose=1)

I am guessing I have made a mistake in replacing the magic strings with class names, but I can't spot the mistake. Any recommendations?



from Non-Identical results from String Identifier and Actual Class names for activations, loss functions, and metrics

No comments:

Post a Comment