Issue
Referring this tutorial https://www.tensorflow.org/tutorials/images/transfer_learning , I created and trained a resnet model
preprocess_input = tf.keras.applications.resnet50.preprocess_input
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.resnet50.ResNet50(
include_top=False, weights='imagenet',input_shape=IMG_SHAPE, classes=2)
prediction_layer = tf.keras.layers.Dense(1)
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
inputs = tf.keras.Input(shape=IMG_SHAPE)
x = data_augmentation(inputs)
x = preprocess_input(x)
x = base_model(x)
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)
So when I infer does the model perform data augmentation? I want the model to do data augmentation during training and not during inference
Also I get different result when i infer the images as batch and infer one image at a time. When i infer batch of image I always get accuracy 1 (this is an overfitted model), when I infer images one by one I get 2 - 4 errors (this number isn't constant, every time i get different accuracy)
This is my inference code
image_batch, label_batch = test_dataset.as_numpy_iterator().next()
class_list =['close','open']
model = tf.keras.models.load_model("shutter_model")
predictions = model.predict_on_batch(image_batch).flatten()
# Apply a sigmoid since our model returns logits
predictions = tf.nn.sigmoid(predictions)
predictions = tf.where(predictions < 0.5, 0, 1)
error = 0
for i in range(len(predictions)):
if predictions[i]!= label_batch[i]:
error+=1
print("number of errors when batch of images fed into the model: ",error)
print('=='*10)
error = 0
for i in range(len(image_batch)):
img = tf.expand_dims(image_batch[i], axis=0)
predictions = model(img)
predictions = tf.nn.sigmoid(predictions)
class_n = 1 if predictions[0][0] >0.5 else 0
if label_batch[i]!= class_n:
error+=1
print("number of errors when images fed into the model one by one: ",error)
OUTPUT
number of errors when batch of images fed into the model: 0
====================
number of errors when images fed into the model one by one: 3
My purpose was to train (either scratch or from pretrained weights) a 2 class model using Resnet50 architecture
Solution
use model.predict(img)
when inference is done on one image instead of model(img)
Answered By - Athul Suresh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.