Keras with MobilenetV2 for Deep Learning
|
Introduction to Keras with MobilenetV2 for Deep Learning
This wiki is intended to give a quick and easy guide to create models using MobileNetV2 with Keras in Ubuntu 16.04 for PC. For better understanding an example using Transfer learning will be given .
Requirements
TensorFlow
Since Keras will be using TensorFlow backend.
pip3 install tensorflow
TensorFlow reserves all GPU memory available even though it doesn't need it. To change this and configure it to reserve only the necessary memory you should write the following before creating the model: |
import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config)
Keras
Since Keras has already in its applications a pre-trained model of MobileNetV2 we need to install it by:
pip3 install Keras --user
The function to create the model has the following default parameters:
keras.applications.mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0, depth_multiplier=1, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000)
which you can modify for your convenience.
Numpy, Scipy and Sklearn
pip3 install numpy pip3 install scipy pip3 install sklearn
Example transfer learning to the MobileNetV2
This example will apply transfer learning to the MobileNetV2 model in order to make it recognize new classes.
Configuring the session to avoid reserving all GPU memory
config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config)
Creating the base model and add some extra layers to adjust to our model
base_model=MobileNetV2(weights='imagenet',include_top=False) #imports the MobileNetV2 model and discards the last 1000 neuron layer. x=base_model.output x=GlobalAveragePooling2D()(x) x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results. x=Dense(1024,activation='relu')(x) #dense layer 2 x=Dense(512,activation='relu')(x) #dense layer 3 preds=Dense($N,activation='softmax')(x) #final layer with softmax activation for N classes model=Model(inputs=base_model.input,outputs=preds) #specify the inputs and outputs
Setting how many layers will be trained
for layer in model.layers[:20]: layer.trainable=False for layer in model.layers[20:]: layer.trainable=True
Creating training and validation set
Note that in the $TRAINING_FOLDER should be the training data organized in folders with its corresponding class name just as shown in Figure 1.
#Training set train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) train_generator=train_datagen.flow_from_directory('$TRAINING_FOLDER/', target_size=(224,224), color_mode='rgb', batch_size=32, class_mode='categorical', shuffle=True)
This piece of code could be used also to create the validation and test sets.
Training the model
model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])
step_size_train=train_generator.n//train_generator.batch_size step_size_val=val_generator.n//val_generator.batch_size model.fit_generator(generator=train_generator, steps_per_epoch=step_size_train, epochs=100, validation_data=val_generator, validation_steps=step_size_val)
Saving the model
model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights("weights.h5") # serialize weights to HDF5
Testing the model
To test the model a confusion matrix and a classification report is used. The metrics used for the report are recall, precision, and fscore.
Y_pred = model.predict_generator(test_generator, test_generator.n//test_generator.batch_size+1) y_pred = np.argmax(Y_pred, axis=1)
confusion_matrix(test_generator.classes, y_pred) target_names = ['00-Class', '01-Class', 'N-Class'] #classes names classification_report(test_generator.classes, y_pred, target_names=target_names)
An output like the following could be shown:
Confusion Matrix [[609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 343 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0] [ 0 0 536 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0] [ 0 0 0 527 4 0 3 6 0 0 0 2 0 0 0 0 0 3] [ 0 0 0 0 135 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 1 0 0 0 94 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 1 0 0 199 0 0 0 0 0 0 0 0 0 0 37] [ 0 0 0 0 0 0 0 435 6 0 0 0 0 0 0 0 0 28] [ 0 0 0 0 0 0 0 2 523 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 503 2 0 0 0 0 0 0 8] [ 0 0 0 0 0 0 0 0 0 0 522 0 0 0 0 0 0 1] [ 2 0 0 0 0 0 0 0 0 0 0 469 0 0 0 0 0 0] [ 0 0 0 4 0 0 0 2 0 0 0 2 369 0 2 0 0 4] [ 0 0 0 0 0 0 0 0 0 0 0 4 0 537 0 0 0 0] [ 0 10 20 0 0 0 0 0 0 0 0 0 0 0 514 6 0 33] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 55 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 219 0] [ 0 174 0 4 0 0 0 17 1 0 0 0 0 6 17 0 0 47]] Classification Report precision recall f1-score support
00-Gesture 1.00 1.00 1.00 609 01-Gesture 0.65 0.99 0.78 347 02-Gesture 0.96 1.00 0.98 537 03-Gesture 0.98 0.97 0.98 545 04-Gesture 0.97 1.00 0.99 135 05-Gesture 1.00 0.99 0.99 95 06-Gesture 0.99 0.84 0.91 237 07-Gesture 0.94 0.93 0.93 469 08-Gesture 0.99 1.00 0.99 525 09-Gesture 1.00 0.98 0.99 513 10-Gesture 1.00 1.00 1.00 523 11-Gesture 0.98 1.00 0.99 471 12-Gesture 1.00 0.96 0.98 383 13-Gesture 0.99 0.99 0.99 541 14-Gesture 0.96 0.88 0.92 583 15-Gesture 0.90 1.00 0.95 55 16-Gesture 1.00 1.00 1.00 219 17-Gesture 0.29 0.18 0.22 266 micro avg 0.94 0.94 0.94 7053 macro avg 0.92 0.93 0.92 7053 weighted avg 0.94 0.94 0.94 7053
Complete script
MobileNetV2_Intranet_Script.py
See also
1. https://www.tensorflow.org/guide/using_gpu
2. https://keras.io/applications/#mobilenetv2
3. https://github.com/ferhat00/Deep-Learning/tree/master/Transfer%20Learning%20CNN
For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.
Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.