This is a Convolutional Neural Network Model using TensorFlow and Python.
In this blog, I will explain how this simple model was built using the TensorFlow library, and then check how you can test it with your webcam or by just uploading an image.
The dataset used in this project is in the following Kaggle repository, and is conformed between Train, Validation and Test Dataset. The train set has 800 images, 400 pizza images and 400 not pizza images, since the model is going to classify if a given image is a pizza or not, this is a binary classification problem, so we are going to use the sigmoid activation function in the output neuron.
Modules Needed
TensorFlow
TensorFlow JS
import tensorflow as tf
import tensorflowjs as tfjs
Then using the TensorFlow Keras API, we are going to load the Train and Validation images dataset with the tool ImageDataGenerator, with a batch size of 8 and the pic size of every image is going to be 256x256 pixels.
BATCH_SIZE = 8
PIC_SIZE = 256
train_data = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255, rotation_range=20, width_shift_range = 0.1, height_shift_range = 0.1, shear_range= 15, zoom_range= [0.5, 1.5])
validation_data = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
train = train_data.flow_from_directory("./data/train", target_size=(PIC_SIZE,PIC_SIZE), batch_size=BATCH_SIZE, class_mode="binary", color_mode='rgb')
validation = validation_data.flow_from_directory("./data/validation", target_size=(PIC_SIZE,PIC_SIZE), batch_size=BATCH_SIZE, class_mode="binary", color_mode='rgb')
print(train.class_indices)
Model Structure
The model has the following sequential structure, using the convolutional neural network and output on a single neuron with the sigmoid activation function:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(PIC_SIZE, PIC_SIZE, 3), kernel_initializer = "he_normal", strides=1, padding='same'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding="same"))
model.add(tf.keras.layers.Conv2D(64, (3,3), activation='relu', kernel_regularizer = tf.keras.regularizers.l2(0.01), kernel_initializer = "he_normal", strides=1,
padding='same'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding="same"))
model.add(tf.keras.layers.Conv2D(128, (3,3), activation='relu', kernel_regularizer = tf.keras.regularizers.l2(0.01), kernel_initializer = "he_normal", strides=1,
padding='same'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding="same"))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512, activation='relu', kernel_regularizer = tf.keras.regularizers.l2(0.01), kernel_initializer = "he_normal"))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(256, activation='relu', kernel_regularizer = tf.keras.regularizers.l2(0.01), kernel_initializer = "he_normal"))
model.add(tf.keras.layers.Dropout(0.4))
model.add(tf.keras.layers.Dense(64, activation='relu', kernel_regularizer = tf.keras.regularizers.l2(0.01), kernel_initializer = "he_normal"))
model.add(tf.keras.layers.Dropout(0.1))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
Compiling and Training the Model
Then the model was compiled using the Binary Crossentropy Loss, Nadam as an optimizer with a Learning Rate of 0.001, and training during 35 epochs.
model.compile(
optimizer=tf.keras.optimizers.experimental.Nadam(learning_rate=0.001),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=["acc"]
)
history = model.fit(train,
epochs=35,
validation_data=validation)
Model Accuracy
Using the Test dataset, we are going to check the Accuracy. The model has between 70% - 80% accuracy.
test_data = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
test = train_data.flow_from_directory("./data/test", target_size=(PIC_SIZE,PIC_SIZE), batch_size=BATCH_SIZE, class_mode="binary", color_mode='rgb')
model.evaluate(test)
Exporting the Model in a JS Format
With TensorFlowJS, the model can be exported in a JavaScript format, so it can be proved using HTML-CSS-JS.
tfjs.converters.save_keras_model(model, "./model_js")
Testing the Model
Then the model can be proven with the webcam or by uploading an image.