Using Object Detection for Complex Image Classification Scenarios Part 3:

While there are many classification architectures we’ll use MobileNet since it runs fast on a CPU and provides strong results.from keras.layers import Dense,GlobalAveragePooling2Dfrom keras.applications import MobileNetfrom keras.preprocessing import imagefrom keras.applications.mobilenet import preprocess_inputfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.models import Modelfrom keras.optimizers import Adambase_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.x=base_model.outputx=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 2x=Dense(512,activation='relu')(x) #dense layer 3preds=Dense(2,activation='softmax')(x) #final layer with softmax activationmodel=Model(inputs=base_model.input,outputs=preds)for layer in model.layers[:20]: layer.trainable=Falsefor layer in model.layers[20:]: layer.trainable=TrueDownloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.6/mobilenet_1_0_224_tf_no_top.h517227776/17225924 [==============================] – 13s 1us/stepLets take a look at this MobileNet modelmodel.summary()_________________________________________________________________Layer (type) Output Shape Param # =================================================================input_1 (InputLayer) (None, None, None, 3) 0 _________________________________________________________________conv1_pad (ZeroPadding2D) (None, None, None, 3) 0 _________________________________________________________________conv1 (Conv2D) (None, None, None, 32) 864 _________________________________________________________________conv1_bn (BatchNormalization (None, None, None, 32) 128 _________________________________________________________________conv1_relu (ReLU) (None, None, None, 32) 0 _________________________________________________________________conv_dw_1 (DepthwiseConv2D) (None, None, None, 32) 288 _________________________________________________________________conv_dw_1_bn (BatchNormaliza (None, None, None, 32) 128 _________________________________________________________________conv_dw_1_relu (ReLU) (None, None, None, 32) 0 _________________________________________________________________conv_pw_1 (Conv2D) (None, None, None, 64) 2048 _________________________________________________________________conv_pw_1_bn (BatchNormaliza (None, None, None, 64) 256 _________________________________________________________________conv_pw_1_relu (ReLU) (None, None, None, 64) 0 _________________________________________________________________conv_pad_2 (ZeroPadding2D) (None, None, None, 64) 0 _________________________________________________________________conv_dw_2 (DepthwiseConv2D) (None, None, None, 64) 576 _________________________________________________________________conv_dw_2_bn (BatchNormaliza (None, None, None, 64) 256 _________________________________________________________________conv_dw_2_relu (ReLU) (None, None, None, 64) 0 _________________________________________________________________conv_pw_2 (Conv2D) (None, None, None, 128) 8192 _________________________________________________________________conv_pw_2_bn (BatchNormaliza (None, None, None, 128) 512 _________________________________________________________________conv_pw_2_relu (ReLU) (None, None, None, 128) 0 _________________________________________________________________conv_dw_3 (DepthwiseConv2D) (None, None, None, 128) 1152 _________________________________________________________________conv_dw_3_bn (BatchNormaliza (None, None, None, 128) 512 _________________________________________________________________conv_dw_3_relu (ReLU) (None, None, None, 128) 0 _________________________________________________________________conv_pw_3 (Conv2D) (None, None, None, 128) 16384 _________________________________________________________________conv_pw_3_bn (BatchNormaliza (None, None, None, 128) 512 _________________________________________________________________conv_pw_3_relu (ReLU) (None, None, None, 128) 0 _________________________________________________________________conv_pad_4 (ZeroPadding2D) (None, None, None, 128) 0 _________________________________________________________________conv_dw_4 (DepthwiseConv2D) (None, None, None, 128) 1152 _________________________________________________________________conv_dw_4_bn (BatchNormaliza (None, None, None, 128) 512 _________________________________________________________________conv_dw_4_relu (ReLU) (None, None, None, 128) 0 _________________________________________________________________conv_pw_4 (Conv2D) (None, None, None, 256) 32768 _________________________________________________________________conv_pw_4_bn (BatchNormaliza (None, None, None, 256) 1024 _________________________________________________________________conv_pw_4_relu (ReLU) (None, None, None, 256) 0 _________________________________________________________________conv_dw_5 (DepthwiseConv2D) (None, None, None, 256) 2304 _________________________________________________________________conv_dw_5_bn (BatchNormaliza (None, None, None, 256) 1024 _________________________________________________________________conv_dw_5_relu (ReLU) (None, None, None, 256) 0 _________________________________________________________________conv_pw_5 (Conv2D) (None, None, None, 256) 65536 _________________________________________________________________conv_pw_5_bn (BatchNormaliza (None, None, None, 256) 1024 _________________________________________________________________conv_pw_5_relu (ReLU) (None, None, None, 256) 0 _________________________________________________________________conv_pad_6 (ZeroPadding2D) (None, None, None, 256) 0 _________________________________________________________________conv_dw_6 (DepthwiseConv2D) (None, None, None, 256) 2304 _________________________________________________________________conv_dw_6_bn (BatchNormaliza (None, None, None, 256) 1024 _________________________________________________________________conv_dw_6_relu (ReLU) (None, None, None, 256) 0 _________________________________________________________________conv_pw_6 (Conv2D) (None, None, None, 512) 131072 _________________________________________________________________conv_pw_6_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_pw_6_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_dw_7 (DepthwiseConv2D) (None, None, None, 512) 4608 _________________________________________________________________conv_dw_7_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_dw_7_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pw_7 (Conv2D) (None, None, None, 512) 262144 _________________________________________________________________conv_pw_7_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_pw_7_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_dw_8 (DepthwiseConv2D) (None, None, None, 512) 4608 _________________________________________________________________conv_dw_8_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_dw_8_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pw_8 (Conv2D) (None, None, None, 512) 262144 _________________________________________________________________conv_pw_8_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_pw_8_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_dw_9 (DepthwiseConv2D) (None, None, None, 512) 4608 _________________________________________________________________conv_dw_9_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_dw_9_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pw_9 (Conv2D) (None, None, None, 512) 262144 _________________________________________________________________conv_pw_9_bn (BatchNormaliza (None, None, None, 512) 2048 _________________________________________________________________conv_pw_9_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_dw_10 (DepthwiseConv2D) (None, None, None, 512) 4608 _________________________________________________________________conv_dw_10_bn (BatchNormaliz (None, None, None, 512) 2048 _________________________________________________________________conv_dw_10_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pw_10 (Conv2D) (None, None, None, 512) 262144 _________________________________________________________________conv_pw_10_bn (BatchNormaliz (None, None, None, 512) 2048 _________________________________________________________________conv_pw_10_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_dw_11 (DepthwiseConv2D) (None, None, None, 512) 4608 _________________________________________________________________conv_dw_11_bn (BatchNormaliz (None, None, None, 512) 2048 _________________________________________________________________conv_dw_11_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pw_11 (Conv2D) (None, None, None, 512) 262144 _________________________________________________________________conv_pw_11_bn (BatchNormaliz (None, None, None, 512) 2048 _________________________________________________________________conv_pw_11_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pad_12 (ZeroPadding2D) (None, None, None, 512) 0 _________________________________________________________________conv_dw_12 (DepthwiseConv2D) (None, None, None, 512) 4608 _________________________________________________________________conv_dw_12_bn (BatchNormaliz (None, None, None, 512) 2048 _________________________________________________________________conv_dw_12_relu (ReLU) (None, None, None, 512) 0 _________________________________________________________________conv_pw_12 (Conv2D) (None, None, None, 1024) 524288 _________________________________________________________________conv_pw_12_bn (BatchNormaliz (None, None, None, 1024) 4096 _________________________________________________________________conv_pw_12_relu (ReLU) (None, None, None, 1024) 0 _________________________________________________________________conv_dw_13 (DepthwiseConv2D) (None, None, None, 1024) 9216 _________________________________________________________________conv_dw_13_bn (BatchNormaliz (None, None, None, 1024) 4096 _________________________________________________________________conv_dw_13_relu (ReLU) (None, None, None, 1024) 0 _________________________________________________________________conv_pw_13 (Conv2D) (None, None, None, 1024) 1048576 _________________________________________________________________conv_pw_13_bn (BatchNormaliz (None, None, None, 1024) 4096 _________________________________________________________________conv_pw_13_relu (ReLU) (None, None, None, 1024) 0 _________________________________________________________________global_average_pooling2d_1 ( (None, 1024) 0 _________________________________________________________________dense_7 (Dense) (None, 1024) 1049600 _________________________________________________________________dense_8 (Dense) (None, 1024) 1049600 _________________________________________________________________dense_9 (Dense) (None, 512) 524800 _________________________________________________________________dense_10 (Dense) (None, 2) 1026 =================================================================Total params: 5,853,890Trainable params: 5,817,986Non-trainable params: 35,904_________________________________________________________________Lets Process our DataThe code below shows how to train a custom MobileNet model on our custom policy using Keras.Train Data/ Test Datatrain_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependenciestrain_generator=train_datagen.flow_from_directory('/data/dataset/Beverages/Train/',target_size=(224,224),color_mode='rgb',batch_size=32,class_mode='categorical',shuffle=True)test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)test_generator = test_datagen.flow_from_directory( directory=r"/data/dataset/Beverages/Test/", target_size=(224, 224), color_mode="rgb", batch_size=1, class_mode='categorical', shuffle=False, seed=42)Found 180 images belonging to 2 classes.Found 60 images belonging to 2 classes.View Sample of Images Datai = 0for data in test_generator: if i > 3: break else: i+=1 img, cls = data print(np.argmax(cls)) plt.imshow(img[0]) plt.show()Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).0Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).0Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).0Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).0Train Modelmodel.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])# Adam optimizer# loss function will be categorical cross entropy# evaluation metric will be accuracystep_size_train=train_generator.n//train_generator.batch_sizemodel.fit_generator(generator=train_generator, steps_per_epoch=step_size_train, epochs=5)Epoch 1/55/5 [==============================] – 96s 19s/step – loss: 0.8017 – acc: 0.7313Epoch 2/55/5 [==============================] – 77s 15s/step – loss: 0.0101 – acc: 1.0000Epoch 3/55/5 [==============================] – 79s 16s/step – loss: 0.0289 – acc: 0.9937Epoch 4/55/5 [==============================] – 111s 22s/step – loss: 0.0023 – acc: 1.0000Epoch 5/55/5 [==============================] – 87s 17s/step – loss: 0.0025 – acc: 1.0000Benchmark ModelAs we can see below the MobileNet is a really strong model for learning and representing our toy policy.from utils import classification_reporty_true = np.concatenate([np.argmax(test_generator[i][1], axis=1) for i in range(test_generator.n)])y_pred = np.argmax(model.predict_generator(test_generator, steps=test_generator.n), axis=1)classification_report(y_true, y_pred)precision recall f1-score support 0 1.00 1.00 1.00 30 1 1.00 1.00 1.00 30 micro avg 1.00 1.00 1.00 60 macro avg 1.00 1.00 1.00 60weighted avg 1.00 1.00 1.00 60Confusion matrix, without normalization[[30 0] [ 0 30]]Normalized confusion matrix[[1..0.] [0..1.]]However if our policy was more complex it might have been challenging for us to model this way..In the next post we will dive into how to use object detection for complex image classification scenarios.ResourcesThe Pythic Coder’s Recommended Content for Getting Started with Machine Learning on AzureTldr; Since the post on DevOps resources was well received, and it can be difficult tracking down documentation, I was…medium.comaribornstein — Overview@pythiccoder..aribornstein has 68 repositories available..Follow their code on GitHub.github.comCognitive Services | Microsoft AzureMicrosoft Azure Stack is an extension of Azure-bringing the agility and innovation of cloud computing to your…azure.microsoft.comA Big of Tricks for Image ClassificationCome get your deep learning goodiestowardsdatascience.comThe Ultimate NanoBook to understand Deep Learning based Image ClassifierIn today’s world, we are making extensive use of images..Isn’t it?.Have you ever wonder how Facebook automatically…towardsdatascience.comNext PostIn the next post we will dive into how to use object detection for complex image classification scenarios..Future posts will cover.Training and Computer Vision Models on the Cloud using Azure ML ServiceTrain a Computer Vision Model on a Remote Cluster with Azure Machine LearningIf you have any questions, comments, or topics you would like me to discuss feel free to follow me on Twitter if there is a milestone you feel I missed please let me know.About the AuthorAaron (Ari) Bornstein is an avid AI enthusiast with a passion for history, engaging with new technologies and computational medicine..As an Open Source Engineer at Microsoft’s Cloud Developer Advocacy team, he collaborates with Israeli Hi-Tech Community, to solve real world problems with game changing technologies that are then documented, open sourced, and shared with the rest of the world.. More details

Leave a Reply