-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfredtest.py
122 lines (91 loc) · 2.95 KB
/
fredtest.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import os,cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras import backend as K
K.set_image_dim_ordering('tf')
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop
from sklearn.metrics import classification_report,confusion_matrix
import itertools
PATH = os.getcwd()
# Define data path
data_path = PATH + '/dataset'
data_dir_list = os.listdir(data_path)
img_rows=128
img_cols=128
num_channel=1
num_epoch=20
# Define the number of classes
num_classes = 4
img_data_list=[]
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
input_img_resize=cv2.resize(input_img,(128,128))
img_data_list.append(input_img_resize)
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data /= 255
print (img_data.shape)
if num_channel==1:
if K.image_dim_ordering()=='th':
img_data= np.expand_dims(img_data, axis=1)
print (img_data.shape)
else:
img_data= np.expand_dims(img_data, axis=4)
print (img_data.shape)
else:
if K.image_dim_ordering()=='th':
img_data=np.rollaxis(img_data,3,1)
print (img_data.shape)
names = ['anger','disgust','happy','neutral','surprise']
#%%
# Defining the model
input_shape=img_data[0].shape
from keras.models import load_model
model=load_model('RUN9.hdf5')
model.summary()
model.get_config()
# Evaluating the model
# Testing a new image
test_image = cv2.imread('fredtest_data/h1.jpg')
test_image=cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
test_image=cv2.resize(test_image,(128,128))
test_image = np.array(test_image)
test_image = test_image.astype('float32')
test_image /= 255
print (test_image.shape)
if num_channel==1:
if K.image_dim_ordering()=='th':
test_image= np.expand_dims(test_image, axis=0)
test_image= np.expand_dims(test_image, axis=0)
print (test_image.shape)
else:
test_image= np.expand_dims(test_image, axis=3)
test_image= np.expand_dims(test_image, axis=0)
print (test_image.shape)
else:
if K.image_dim_ordering()=='th':
test_image=np.rollaxis(test_image,2,0)
test_image= np.expand_dims(test_image, axis=0)
print (test_image.shape)
else:
test_image= np.expand_dims(test_image, axis=0)
print (test_image.shape)
# Predicting the test image
print(model.predict(test_image))
print(model.predict_classes(test_image))
plt.imshow(test_image.squeeze())