You are on page 1of 2

In 

[1]: import glob, os

import numpy as np

from keras.preprocessing.image import load_img

from keras.preprocessing.image import img_to_array

from keras.applications.resnet50 import preprocess_input

from keras.applications.imagenet_utils import preprocess_input,decode_predictions

import matplotlib.pyplot as plt

from tensorflow.keras.applications import resnet50

resnet_model = resnet50.ResNet50(weights='imagenet')

Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/r


esnet/resnet50_weights_tf_dim_ordering_tf_kernels.h5

102973440/102967424 [==============================] - 153s 1us/step

In [2]: def run_prediction(filename):

input_image = load_img(filename, target_size=(224, 224))

plt.imshow(input_image)

plt.show()

# convert the image to a numpy array

numpy_image = img_to_array(input_image)

image_batch = np.expand_dims(numpy_image,axis=0)

image_procesed= preprocess_input(image_batch)

prediction=resnet_model.predict(image_procesed)

output=filename, decode_predictions(prediction, top=10)

print(output)

with open('Results/predictions/resnet50/results.txt', 'a') as f:

f.writelines(str(output))

In [5]: filename = 'C:/Users/walee/Desktop/Adil Khan/Big Data/DSC 650/Assignments/Assignment


run_prediction(filename)

('C:/Users/walee/Desktop/Adil Khan/Big Data/DSC 650/Assignments/Assignment 06/image


s/cat1.PNG', [[('n02124075', 'Egyptian_cat', 0.40473214), ('n02123045', 'tabby', 0.3
7820032), ('n02123159', 'tiger_cat', 0.18440679), ('n02127052', 'lynx', 0.02030049),
('n02120505', 'grey_fox', 0.0031651843), ('n02356798', 'fox_squirrel', 0.000663975
2), ('n02129604', 'tiger', 0.0004948665), ('n02128757', 'snow_leopard', 0.0004507356
2), ('n03958227', 'plastic_bag', 0.00036802635), ('n02128385', 'leopard', 0.00035691
64)]])

In [6]: filename2 = 'C:/Users/walee/Desktop/Adil Khan/Big Data/DSC 650/Assignments/Assignmen


run_prediction(filename2)

('C:/Users/walee/Desktop/Adil Khan/Big Data/DSC 650/Assignments/Assignment 06/image


s/cat2.PNG', [[('n02123045', 'tabby', 0.6036403), ('n02124075', 'Egyptian_cat', 0.30
550975), ('n02123159', 'tiger_cat', 0.023157293), ('n02127052', 'lynx', 0.00862331
1), ('n04493381', 'tub', 0.005825389), ('n02909870', 'bucket', 0.004084795), ('n0348
2405', 'hamper', 0.003631829), ('n02123394', 'Persian_cat', 0.0030924305), ('n079308
64', 'cup', 0.0030116315), ('n04209239', 'shower_curtain', 0.0025875547)]])

In [8]: filename3 = 'C:/Users/walee/Desktop/Adil Khan/Big Data/DSC 650/Assignments/Assignmen


run_prediction(filename3)

('C:/Users/walee/Desktop/Adil Khan/Big Data/DSC 650/Assignments/Assignment 06/image


s/human.PNG', [[('n03617480', 'kimono', 0.2810746), ('n02963159', 'cardigan', 0.0987
3904), ('n04136333', 'sarong', 0.09751529), ('n03866082', 'overskirt', 0.08254426),
('n03877472', 'pajama', 0.065047406), ('n03450230', 'gown', 0.051486332), ('n0431117
4', 'steel_drum', 0.026140286), ('n04325704', 'stole', 0.014662189), ('n03534580',
'hoopskirt', 0.014379749), ('n02948072', 'candle', 0.013780539)]])

In [ ]:

You might also like