-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparas127.py
227 lines (166 loc) · 7.33 KB
/
paras127.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from keras.models import Sequential, Model,load_model
import math, os, json, sys, re
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from __future__ import division,print_function
import math, os, json, sys, re
# import cPickle as pickle
from glob import glob
# from fast.ai.imports import *
import PIL
from PIL import Image
import numpy as np
from numpy.random import random, permutation, randn, normal, uniform, choice
from numpy import newaxis
import scipy
from scipy import misc, ndimage
from scipy.ndimage.interpolation import zoom
# from scipy.ndimage import imread
#from sklearn.metrics import confusion_matrix
# import bcolz
from sklearn.preprocessing import OneHotEncoder
from sklearn.manifold import TSNE
#from IPython.lib.display import FileLink
# import theano
# from theano import shared, tensor as T
# from theano.tensor.nnet import conv2d, nnet
# from theano.tensor.signal import pool
import keras
from keras import backend as K
from keras.utils.data_utils import get_file
from keras.utils import np_utils
from keras.utils.np_utils import to_categorical
from keras.models import Sequential, Model,load_model
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.layers.convolutional import *
from keras.preprocessing import image, sequence
from keras.optimizers import SGD, RMSprop
import keras.callbacks as kcallbacks
from keras.applications import VGG16
import numpy as np
from scipy import ndimage
categories=['carry','clapHands','pickUp','pull','push','sitDown','standUp','throw','walk','waveHands']
def dt(folder1,cat):
dataset=[]
j = 0
y=[]
for category in categories:
for file in os.listdir(folder1+'/'+str(category)):
#print(file)
img = load_img(folder1+'/'+str(category)+'/'+file)
img.thumbnail((224, 224))
img = img.resize((224,224), Image.ANTIALIAS)
# Convert to Numpy Array
x = img_to_array(img)
x = x.reshape((224,224,3))
#print(x.shape)
#im = Image.open(folder+'/'+str(category)+'/'+file)
#im=cv2.imread(file)
#cv2.resize(im, (224,224), interpolation=cv2.INTER_CUBIC)
#im.resize((224, 224))
#print('hello1')
#im.load()
#print('hello2')
#data = np.asarray(im,dtype="float32")
#print(data.shape)
#print("hello1")
#data= np.array(im)
#print(data.shape)
#x = data.reshape((3,224,224))
dataset.append(x)
#print(categories.index(category))
y.append(categories.index(category))
dataset=np.array(dataset)
values = np.array(y)
#print("All images to array!")
return dataset,values
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
model_path1 = '/home/ws2/Documents/finaldata1/seg1/train/head_spinal/models/best_weights3_resnet.hdf5'
model1 = load_model(model_path1)
dataset1,values=dt('/home/ws2/Documents/finaldata1/seg1/valid/head_spinal',categories)
model1_pred = model1.predict(dataset1,batch_size=4)
classes = np.argmax(model1_pred,axis=1)
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
loss, acc = model1.evaluate(dataset1, onehot_encoded, verbose=0)
print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))
#print(accuracy_score(values,classes))
# print(model_v3.evaluate(X_HPM_test,Y_HPM_test,batch_size=5))
model_path2 = '/home/ws2/Documents/finaldata1/seg1/train/left_hand/models/best_weights3_resnet.hdf5'
model2 = load_model(model_path2)
dataset2,values=dt('/home/ws2/Documents/finaldata1/seg1/valid/left_hand',categories)
model2_pred = model1.predict(dataset2,batch_size=4)
classes = np.argmax(model2_pred,axis=1)
loss, acc = model2.evaluate(dataset2, onehot_encoded, verbose=0)
print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))
#print(accuracy_score(values,classes))
# print(model_v3.evaluate(X_HPM_test,Y_HPM_test,batch_size=5))
model_path3 = '/home/ws2/Documents/finaldata1/seg1/train/left_leg/models/best_weights3_resnet.hdf5'
model3 = load_model(model_path3)
dataset3,values=dt('/home/ws2/Documents/finaldata1/seg1/valid/left_leg',categories)
model3_pred = model3.predict(dataset3,batch_size=4)
classes = np.argmax(model3_pred,axis=1)
loss, acc = model3.evaluate(dataset3, onehot_encoded, verbose=0)
print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))
#print(accuracy_score(values,classes))
# print(model_v3.evaluate(X_HPM_test,Y_HPM_test,batch_size=5))
model_path4 = '/home/ws2/Documents/finaldata1/seg1/train/right_hand/models/best_weights3_resnet.hdf5'
model4 = load_model(model_path4)
dataset4,values=dt('/home/ws2/Documents/finaldata1/seg1/valid/right_hand',categories)
model4_pred = model4.predict(dataset4,batch_size=4)
classes = np.argmax(model4_pred,axis=1)
loss, acc = model4.evaluate(dataset4, onehot_encoded, verbose=0)
print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))
#print(accuracy_score(values,classes))
# print(model_v3.evaluate(X_HPM_test,Y_HPM_test,batch_size=5))
model_path5 = '/home/ws2/Documents/finaldata1/seg1/train/right_leg/models/best_weights3_resnet.hdf5'
model5 = load_model(model_path5)
dataset5,values=dt('/home/ws2/Documents/finaldata1/seg1/valid/right_leg',categories)
model5_pred = model5.predict(dataset5,batch_size=4)
classes = np.argmax(model5_pred,axis=1)
loss, acc = model5.evaluate(dataset5, onehot_encoded, verbose=0)
print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))
from sklearn.metrics import accuracy_score
import pandas as pd
# Y_test = label_binarize(Y_test, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])=onehot_encoded
## ROC curve for max late fusion
# In[2]:
print('weighted average')
np.random.seed(123)
df = pd.DataFrame(columns=('w1', 'w2', 'w3','w4','w5', 'accuracy'))
#prediction= (model1_pred+model2_pred+model3_pred+model4_pred)/4.0
#classes_pro = prediction_weight_avg.argmax(axis=-1)
#print(accuracy_score(values,classes_pro))
#print(prediction[0])
i = 0
for w1 in range(1,6):
for w2 in range(1,6):
for w3 in range(1,6):
for w4 in range(1,6):
for w5 in range(1,6):
#w=(w1+w2+w3+w4+w5)/5.0
w=(w1+w2+w3+w4+w5)
prediction_weight_avg = (w1*model1_pred+w2*model2_pred+w3*model3_pred+w4*model4_pred+w5*model5_pred)/w
# print(prediction_weight_avg[0])
# print("prediction_weight_avg")
# print(prediction_weight_avg)
classes_pro = np.argmax(prediction_weight_avg,axis=-1)
ac1=accuracy_score(values,classes_pro)
df.loc[i] = [w1, w2, w3,w4,w5, ac1]
if(ac1==1.000000):
np.save('/home/ws2/Documents/resnetmodelsarray/prediction_weight_avg_seg1.npy', prediction_weight_avg)
print(w1,w2,w3,w4,w5)
break
#print(ac1)
i += 1
print(ac1)
#df.sort(columns=['accuracy'], ascending=False)
df.sort_values(by='accuracy',ascending=False)
# In[ ]: