-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpredict.py
51 lines (39 loc) · 1.48 KB
/
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
# -*- coding: utf-8 -*-
import pandas as pd
from train import load,load2d
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Activation, Dropout, MaxPooling2D, Flatten, GlobalMaxPooling2D, BatchNormalization
from keras.optimizers import SGD, Adam
import matplotlib.pyplot as plt
def plot_sample(x, y, axis):
img = x.reshape(96, 96)
axis.imshow(img, cmap='gray')
axis.scatter(y[0::2]*48+48, y[1::2]*48+48, marker='x', s=10)
model = Sequential()
model.add(BatchNormalization(input_shape=(96,96,1)))
model.add(Conv2D(32, kernel_size=(3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128,kernel_size=(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=(3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(GlobalMaxPooling2D())
model.add(Dense(512)) #512
model.add(Activation('relu'))
model.add(Dense(30))
model.load_weights(filepath='weights.75-0.74206.hdf5')
#predict test images
X_test, _ = load2d(test=True)
y_test = model.predict(X_test)
fig = plt.figure(figsize=(6,6))
for i in range(25):
axis = fig.add_subplot(5,5,i+1,xticks=[],yticks=[])
plot_sample(X_test[i], y_test[i], axis)
plt.show()
fig.savefig('predicted.png')