-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
100 lines (83 loc) · 2.81 KB
/
training.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
# imports
import numpy as np
import os
import pandas as pd
from keras import models
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from model import ModelCustom
from sklearn.model_selection import train_test_split
# consts
SIZE = 28
TEST_FRAC = 0.2
RANDOM_SEED = 42
# paths
base_path = os.getcwd()
data_path = os.path.join(base_path, 'data')
train_path = os.path.join(data_path, 'train.csv')
# test_path = os.path.join(data_path, 'test.csv')
# get data
train_data = pd.read_csv(train_path)
# test_data = pd.read_csv(test_path)
# prep data
data_x = np.uint8(train_data.drop(['label'], axis=1)).reshape(train_data.shape[0], SIZE, SIZE, 1)
# convert to float
data_x = data_x / 255.0
# prep labels
data_y = train_data['label']
data_y = to_categorical(data_y, num_classes=data_y[0].shape)
# clean up
del train_data
# del test_data
train_x, val_x, train_y, val_y = train_test_split(data_x,
data_y,
test_size=TEST_FRAC,
random_state=RANDOM_SEED
)
# get data shape
src_size = (train_x.shape[1], train_x.shape[2], 1)
dst_size = (train_y.shape[1])
# instantiate a model
model = ModelCustom(src_size=src_size,
dst_size=dst_size
)
# build a model
model.build(optimizer='Adam',
lr=1e-3
)
# model summary
model.selfie()
epochs = 50
batch_size = 32
# data augmentation
datagen = ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
zoom_range=0.2,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=False,
vertical_flip=False
)
datagen.fit(train_x)
# train the model
model.train_model_holdout(train_x=train_x,
train_y=train_y,
epochs=epochs,
batch_size=batch_size,
validation_data=(val_x, val_y),
data_size=-1
)
# train set evaluation
print('evaluating train set')
results = model.model.evaluate(train_x, train_y)
print('train mse, train mae:', results)
# test set evaluation (never used in training)
print('evaluating test set')
results = model.model.evaluate(val_x, val_y)
print('test mse, test mae:', results)
# save the model
model.save()