Object recognition models

Storyboard

>Model

ID:(1783, 0)



Setup for MobilNet

Description

>Top


import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import matplotlib.pyplot as plt
%matplotlib inline

ID:(13775, 0)



Load MobilNet

Description

>Top


mobile = tf.keras.applications.mobilenet.MobileNet()

ID:(13776, 0)



Routine to prepare image

Description

>Top


def prepare_image(file):
    img_path = 'data/MobileNet-samples/'
    img = image.load_img(img_path + file, target_size=(224,224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array,axis=0)
    return tf.keras.applications.mobilenet.preprocess_input(img_array_expanded_dims)

ID:(13777, 0)



Show picture

Description

>Top


from IPython.display import Image
Image(filename='data/MobileNet-samples/1.PNG',width=300,height=200)

ID:(13778, 0)



Analyze image

Description

>Top


preprocessed_image = prepare_image('1.PNG')
predictions = mobile.predict(preprocessed_image)
results=imagenet_utils.decode_predictions(predictions)
results

ID:(13779, 0)



Indicate most probable object

Description

>Top


assert results[0][0][1] == 'American_chameleon'

ID:(13780, 0)