-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_keras.py
135 lines (118 loc) · 5.89 KB
/
predict_keras.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from keras.models import Model, Sequential, load_model, model_from_json
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, UpSampling2D, Conv2DTranspose
import cv2
import os
import numpy as np
import glob
def create_model():
image_size = (224,224,3)
model = Sequential()
model.add(Conv2D(64, (3, 3),strides=(1, 1), padding='same', input_shape=image_size, activation='relu'))
model.add(Conv2D(64, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Conv2D(128, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(128, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Conv2D(256, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(256, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(256, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Conv2D(512, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(512, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(512, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Conv2D(512, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(512, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(512, (3, 3),strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
#model.add(Flatten())
#model.add(Dense(4096))
#model.add(Dense(4096))
model.add(Conv2D(4096, (7, 7),strides=(1, 1), padding='valid', activation='relu'))
model.add(Conv2D(4096, (1, 1),strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2DTranspose(512, (7,7), padding='valid', activation='relu'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(512, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(512, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(512, (3,3), padding='same', activation='relu'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(512, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(512, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(256, (3,3), padding='same', activation='relu'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(256, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(256, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(128, (3,3), padding='same', activation='relu'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(128, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(64, (3,3), padding='same', activation='relu'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(64, (3,3), padding='same', activation='relu'))
model.add(Conv2DTranspose(64, (3,3), padding='same', activation='relu'))
model.add(Conv2D(5, (1, 1),strides=(1, 1), padding='same', activation='softmax'))
model.summary()
return model
def load_trained_model_with_FullModel(Model_json_path="./model/model.json", Weights_h5_path="./model/model.h5", test_x_path="./dataset/dataset224x224/test_x/test.png"):
# test_x shape
test_x = np.zeros((1,224,224,3))
# load json and create model
json_file = open('./model/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights("./model/model.h5")
print("Loaded model from disk")
return model
def one_hot_to_BGR(one_hot):
"""
label = {
"car": [142, 0, 0],
"road": [128, 64, 128],
"sky": [180, 130, 70],
"parking": [160, 170, 250],
"else": []
}
BGR
"""
# transfer to one_hot
max_value = max(one_hot)
for i in range(0,len(one_hot)):
if(one_hot[i] == max_value):
one_hot[i] = 1
else:
one_hot[i] = 0
if((one_hot == [1,0,0,0,0]).all()):
return [142,0,0]
elif((one_hot == [0,1,0,0,0]).all()):
return [128,64,128]
elif((one_hot == [0,0,1,0,0]).all()):
return [180,130,0]
elif((one_hot == [0,0,0,1,0]).all()):
return [160,170,250]
else :
return [128,128,128]
def predict(model, test_x_path="./dataset/dataset224x224/test_x/", predict_save_path="./dataset/dataset224x224/test_y/"):
test_x_list = glob.glob(test_x_path+"*.png")
test_x = np.zeros((len(test_x_list), 224, 224, 3))
for i in range(0, len(test_x_list)):
test_x[i] = cv2.imread(test_x_list[i])
predict = model.predict(test_x, verbose=1)
test_y = np.zeros((len(test_x_list), 224, 224, 3))
for i in range(0, test_y.shape[0]):
for w in range(0, test_y.shape[1]):
for h in range(0, test_y.shape[2]):
test_y[i][w][h] = one_hot_to_BGR(predict[i][w][h])
for i in range(0, test_y.shape[0]):
file_name = "predict_{}.png".format(i)
cv2.imwrite(predict_save_path+file_name, test_y[i])
def load_trained_model_with_weight(weights_path="./model/weights.hdf5", test_x_path="./dataset/dataset224x224/test_x/test.png"):
model = create_model()
model.load_weights(weights_path)
test_x = cv2.imread(test_x_path)
predict = model.predict(test_x, verbose=1)
if __name__ == "__main__":
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"]="1"
model = load_trained_model_with_FullModel()
predict(model)