pip install tensorflow #install tensor flow
!pip install keras #install keras
import numpy as np #import keras and numpy
import keras
from keras import backend as K
from keras.layers.core import Dense, Activation #import the activation functions
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
import keras.utils as image
from keras.models import Model
from keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix #import confusion matrix
import itertools
import matplotlib.pyplot as plt
mobile = keras.applications.mobilenet.MobileNet() #call the mobile net architecture from the keras
def prepare_image(file): #prepocessing the image
img = image.load_img(file, target_size=(224, 224)) #load the file resized to 224,224 as mobilenet standard
img_array = image.img_to_array(img)
img_array_expanded_dims = np.expand_dims(img_array, axis=0)
return keras.applications.mobilenet.preprocess_input(img_array_expanded_dims) #return the preprocessed image,scale the image to pixel values
from IPython.display import Image
Image(filename='cat.1.jpg', width=300,height=200) #display the image to be classified
preprocessed_image = prepare_image('cat.1.jpg') #pass the image to the prepare image
predictions = mobile.predict(preprocessed_image) #predicts the image
results = imagenet_utils.decode_predictions(predictions) #gives the top 5 predicted results
results
from IPython.display import Image
Image(filename='cat.15.jpg', width=300,height=200)
preprocessed_image = prepare_image('cat.15.jpg')
predictions = mobile.predict(preprocessed_image)
results = imagenet_utils.decode_predictions(predictions)
results