-
Notifications
You must be signed in to change notification settings - Fork 9
/
train.py
90 lines (63 loc) · 2.35 KB
/
train.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
from prepare_data import *
from sklearn.model_selection import train_test_split as tts
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.utils import np_utils
from nets.MLP import mlp
from nets.conv import conv
from random import randint
# define some constants
N_FRUITS = 4
FRUITS = {0: "Apple", 1: "Banana", 2: "Grape", 3: "Pineapple"}
# number of samples to take in each class
N = 1000
# some other constants
N_EPOCHS = 20
# data files in the same order as defined in FRUITS
files = ["apple.npy", "banana.npy", "grapes.npy", "pineapple.npy"]
# images need to be 28x28 for training with a ConvNet
fruits = load("data/", files, reshaped=True)
# images need to be flattened for training with an MLP
# fruits = load("data/", files, reshaped=False)
# limit no of samples in each class to N
fruits = set_limit(fruits, N)
# normalize the values
fruits = map(normalize, fruits)
# define the labels
labels = make_labels(N_FRUITS, N)
# prepare the data
x_train, x_test, y_train, y_test = tts(fruits, labels, test_size=0.05)
# one hot encoding
Y_train = np_utils.to_categorical(y_train, N_FRUITS)
Y_test = np_utils.to_categorical(y_test, N_FRUITS)
# use our custom designed ConvNet model
model = conv(classes=N_FRUITS, input_shape=(28, 28, 1))
# use our custom designed MLP model
# model = mlp(classes=N_FRUITS)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
raw_input("Type 'train' to start training: ")
print "Training commenced"
model.fit(np.array(x_train), np.array(Y_train), batch_size=32, epochs=N_EPOCHS, verbose=1)
print "Training complete"
print "Evaluating model"
preds = model.predict(np.array(x_test))
score = 0
for i in range(len(preds)):
if np.argmax(preds[i]) == y_test[i]:
score += 1
print "Accuracy: ", ((score + 0.0) / len(preds)) * 100
name = raw_input(">Enter name to save trained model: ")
model.save(name + ".h5")
print "Model saved"
def visualize_and_predict():
"selects a random test case and shows the object, the prediction and the expected result"
n = randint(0, len(x_test))
visualize(denormalize(np.reshape(x_test[n], (28, 28))))
pred = FRUITS[np.argmax(model.predict(np.array([x_test[n]])))]
actual = FRUITS[y_test[n]]
print "Actual:", actual
print "Predicted:", pred
print "Testing mode"
visualize_and_predict()