Hi. I have trained a MobileNets model and in the same code used the model.evaluate() on a set of test data to determine its performance. This test is indicating nearly 97% accuracy. Here is the code that performs this.
import os
import tensorflow.keras as keras
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import load_model
from tensorflow.keras.callbacks import ModelCheckpointimage_size_y = 1056 # The height of one input image
image_size_x = 1920 # The width of one input imageChoose a width multiplier which changes the number of filters per layer
depth_mul = 1.0/8.0
Set input shape for color images
shape = (image_size_y, image_size_x, 3)
Import the MobileNet model and set input dimensions and hyperparameters.
model = MobileNet(input_shape=shape, alpha=depth_mul, weights=None, classes=2)
Setting up the data directory paths
BaseDir = os.path.join(‘path’,‘to’,‘directory’,‘containing’,‘data’)
train_dir = os.path.join(BaseDir,‘train’)
val_dir = os.path.join(BaseDir,‘val’)
test_dir = os.path.join(BaseDir,‘test’)train_positive_dir = os.path.join(train_dir,‘positive’)
train_negative_dir = os.path.join(train_dir,‘negative’)val_positive_dir = os.path.join(val_dir,‘positive’)
val_negative_dir = os.path.join(val_dir,‘negative’)test_positive_dir = os.path.join(test_dir,‘positive’)
test_negative_dir = os.path.join(test_dir,‘negative’)Define desired Batch Size
batchsize = 32
Only use data augmentation that generate images that could reasonably occur in real-world situation (just scale brightness a bit)
train_datagen = ImageDataGenerator(
rescale= 1./255,
brightness_range=[0.9,1.1]
)
valid_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)Create Data Generators for each group of data
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(image_size_x,image_size_y),
batch_size=batchsize,
class_mode=‘categorical’
)validation_generator = valid_datagen.flow_from_directory(
val_dir,
target_size=(image_size_x,image_size_y),
batch_size=batchsize,
class_mode=‘categorical’
)test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(image_size_x,image_size_y),
batch_size=batchsize,
class_mode=‘categorical’
)Compile the model for training
model.compile(
loss=‘categorical_crossentropy’,
optimizer=‘rmsprop’,
metrics = [‘accuracy’]
)Save the model at every epoch, overwriting each time, so the final version after the last epoch will remain and can be tested
finalNetwork = os.path.join(‘path’,‘to’,‘MobileNetsModel.h5’)
mcf = ModelCheckpoint(finalNetwork)Train the network
history = model.fit(
train_generator,
steps_per_epoch = 40646 // batchsize,
epochs = 20,
validation_data = validation_generator,
validation_steps = 5080 // batchsize,
callbacks = [mcf]
)Evaluation on test data of the model after the final epoch of training
saved_model = load_model(finalNetwork)
_,test_acc = saved_model.evaluate(test_generator,verbose = 0)
print(“Final Model Accuracy = %.1f%%” % (100.0 *test_acc))keras.backend.clear_session()
And then I created another piece of code to actually use the trained model, but it doesn’t seem to be working. I’m getting nearly 50% true positives and 50% false positives, so only 50% accuracy. Here is that code. Am I performing the inferences wrong in this code? Am I not saving or loading my model properly? Please help!
import os
from matplotlib import image
import tensorflow as tf
from tensorflow.keras.models import load_modelLoad a model that was trained and saved
model = load_model(os.path.join(‘path’,‘to’,‘MobileNetsModel.h5’))
Set the directory containing the test images
datadir = os.path.join(‘directory’,‘containing’,‘jpgs’)
Get the filenames of all the test images
imgNames = os.listdir(datadir)
Make inferences using the provided model
for imgName in imgNames:
# Get the image img = image.imread(os.path.join(datadir,imgName)) # Make an inference input = tf.convert_to_tensor(img) input = tf.image.resize(input,(1056,1920)) input = input[None,:,:,:] input = input/255.0 output = model(input) prob_pos = output.numpy()[0,0]*100 prob_neg = output.numpy()[0,1]*100 # Categorize inferences and output to console if prob_pos >= prob_neg: print(imgName,' is positive') else: print(imgName,' is negative')