/
resnet-50.py
48 lines (35 loc) · 1.45 KB
/
resnet-50.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
# /home/victoria/projects/computer_vision/keras_js_canvas/resnet-50.py
# ============================================================================
def preprocess_input(x):
x /= 255.
x -= 0.5
x *= 2.
return x
# ============================================================================
print("\n\tHello!\n")
# ----------------------------------------------------------------------------
# https://keras.io/applications/
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
#def preprocess_input(x):
#x /= 255.
#x -= 0.5
#x *= 2.
#return x
model = ResNet50(weights='imagenet')
#img_path = 'images/elephant.jpg'
img_path = 'images/yellowstone-wolf.jpg'
#img_path = 'images//Yellowstone-wolf-17120b.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('\n\tPredicted:', decode_predictions(preds, top=3)[0], '\n')
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
# ----------------------------------------------------------------------------