-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_predict.py
72 lines (53 loc) · 1.94 KB
/
model_predict.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
import numpy as np
import cv2
import scipy.misc
from keras.models import model_from_json
from skimage import feature
############### set parameters #################
wx = 13
wy = 2
mode = 1 # 1 for CNN/ 0 for HOG
############### load model ######################
if mode:
json_file = open('model_cnn.json', 'r')
else:
json_file = open('model_hog.json', 'r')
model_json = json_file.read()
json_file.close()
model = model_from_json(model_json)
# load weights into new model
if mode:
model.load_weights("mdl_wts_cnn.hdf5")
else:
model.load_weights("mdl_wts_hog.hdf5")
model.compile(loss='binary_crossentropy', optimizer='adam')
print("Loaded model from disk")
############### test on unseen data ######################
filename = 'testimg.jpg'
tst_img = cv2.imread(filename)
tst_img = tst_img.astype(np.float32)/255
#tst_img = cv2.resize(tst_img,(200,200),cv2.INTER_CUBIC)
pimg = cv2.copyMakeBorder(tst_img,wx,wx,wx,wx,cv2.BORDER_REFLECT101)
h, w, d = tst_img.shape
img_out = np.zeros((h,w), dtype=tst_img.dtype)
img_out = cv2.copyMakeBorder(img_out,wy,wy,wy,wy,cv2.BORDER_REFLECT101)
for i in range(wx,w+wx):
for j in range(wx,h+wx):
patch = pimg[j-wx:j+wx+1,i-wx:i+wx+1,:]
if mode:
Xtst = Xtst[np.newaxis,:,:,:]
else:
Xtst = feature.hog(patch[:,:,0], orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), transform_sqrt=True, block_norm="L1")
Xtst = Xtst[np.newaxis,:]
preds = model.predict(Xtst)
label = np.reshape(preds,(2*wy+1,2*wy+1),order='F')
img_out[j-wx:j-wx+2*wy+1,i-wx:i-wx+2*wy+1] = img_out[j-wx:j-wx+2*wy+1,i-wx:i-wx+2*wy+1] + label
# normalize image to get probabilty map [0,1]
img_out = img_out/img_out.max()
# thresholding binarization
img_out[img_out>=0.5] = 1
img_out[img_out<0.5] = 0
img_out = 255*img_out
# save predicted image
scipy.misc.imsave('outfile1.jpg', img_out)