-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
39 lines (33 loc) · 1.13 KB
/
test.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
import os
import numpy as np
from skimage.io import imread
from skimage.transform import resize
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
from model import ResNet152
os.environ['CUDA_VISIBLE_DEVICES'] = "-1"
def preprocess(x):
x = resize(x, (224,224), mode='constant') * 255
x = preprocess_input(x)
if x.ndim == 3:
x = np.expand_dims(x, 0)
return x
if __name__ == '__main__':
print('loading ResNet-152 model...', end='')
model = ResNet152()
print('done!')
print('Predicting image with "cat" ...')
cat = imread('./imgs/cat.jpg')
x = preprocess(cat)
y = model.predict(x)
pred_title = decode_predictions(y, top=1)[0][0][1]
print('Model prediction: {}\n'.format(pred_title))
assert pred_title == 'tiger_cat'
print('Predicting image with "dog" ...')
dog = imread('./imgs/dog.jpg')
x = preprocess(dog)
y = model.predict(x)
pred_title = decode_predictions(y, top=1)[0][0][1]
print('Model prediction: {}\n'.format(pred_title))
assert pred_title == 'Eskimo_dog'
print ('Success!')