-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_code_resnet50_v1.py
76 lines (59 loc) · 2.11 KB
/
testing_code_resnet50_v1.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from keras.applications.resnet50 import ResNet50, preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Flatten, Dropout
from keras.models import Sequential, Model
from keras.optimizers import SGD, Adam
from keras.callbacks import TensorBoard
import keras
import matplotlib.pyplot as plt
import sys
from keras.preprocessing import image
import numpy as np
import glob
import os
HEIGHT = 300
WIDTH = 300
BATCH_SIZE = 8
class_list = ["booth", "highway", "open_non_highway", "overpass", "settlement", "traffic_road", "tunnel", "tunnel_exit"]
FC_LAYERS = [256, 128]
dropout = 0.5
weight_file = sys.argv[1]
def build_finetune_model(base_model, dropout, fc_layers, num_classes):
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = Flatten()(x)
for fc in fc_layers:
x = Dense(fc, activation='relu')(x)
x = Dropout(dropout)(x)
preditions = Dense(num_classes, activation='softmax')(x)
finetune_model = Model(inputs = base_model.input, outputs = preditions)
return finetune_model
base_model = ResNet50(weights = 'imagenet',
include_top = False,
input_shape = (HEIGHT, WIDTH, 3))
finetune_model = build_finetune_model(base_model,
dropout = dropout,
fc_layers = FC_LAYERS,
num_classes = len(class_list))
pwd = os.getcwd()
# Please use "pwd+weight_file" if the weight file is saved in the root directory of the project
finetune_model.load_weights(pwd+"/Weights/"+weight_file)
predict_image_path = pwd+"/dataset/raw_image/"
os.getcwd()
os.chdir(predict_image_path)
images = []
images = glob.glob('*')
for imagepath in images:
test_image = image.load_img(path = predict_image_path+'/'+imagepath, target_size = (300, 300))
plt.imshow(test_image)
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
#print(test_image.shape())
result = finetune_model.predict(test_image)
print(result)
result = result.argmax(axis=1)[0]
label = class_list[result]
plt.title(label)
plt.show()
print("Image : {} || Index : {} || Label : {}".format(imagepath, result, label))