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
#call the mobile net architecture from the keras
mobile = keras.applications.mobilenet.MobileNet()
#prepocessing the image
def prepare_image(file):
#load the file resized to 224,224 as mobilenet standard
img = image.load_img(file, target_size=(224, 224))
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
#display the image to be classified
Image(filename='cat.1181.jpg', width=300,height=200)
#pass the image to the prepare image
preprocessed_image = prepare_image('cat.1181.jpg')
#predicts the image
predictions = mobile.predict(preprocessed_image)
#gives the top 5 predicted results
results = imagenet_utils.decode_predictions(predictions)
results
from IPython.display import Image
Image(filename='dog.37.jpg', width=300,height=200)
preprocessed_image = prepare_image('dog.37.jpg')
predictions = mobile.predict(preprocessed_image)
results = imagenet_utils.decode_predictions(predictions)
results