|
a |
|
b/src/train.py |
|
|
1 |
from datetime import datetime |
|
|
2 |
from model import MyDeepModel, create_submission |
|
|
3 |
from data_loader import read_testset, read_trainset, DataGenerator |
|
|
4 |
|
|
|
5 |
import keras as K |
|
|
6 |
|
|
|
7 |
from sklearn.model_selection import ShuffleSplit |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
# from K_applications.resnet import ResNet50 |
|
|
11 |
from keras.applications.inception_v3 import InceptionV3 |
|
|
12 |
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input |
|
|
13 |
from keras.applications.densenet import DenseNet121 |
|
|
14 |
from keras.applications.mobilenet_v2 import MobileNetV2 |
|
|
15 |
|
|
|
16 |
test_images_dir = '../../data/stage_1_test_images/' |
|
|
17 |
train_images_dir = '../../data/stage_1_train_images/' |
|
|
18 |
trainset_filename = "../../data/stage_1_train.csv" |
|
|
19 |
testset_filename = "../../stage_1_sample_submission.csv" |
|
|
20 |
num_epochs = 10 |
|
|
21 |
img_shape = (256,256,3) |
|
|
22 |
batch_size=32 |
|
|
23 |
TRAINING =True # If False, then just load model and predict |
|
|
24 |
|
|
|
25 |
engine=InceptionV3 |
|
|
26 |
model_filename="InceptionV3_{}.hdf5".format(datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) |
|
|
27 |
#model_filename="wrapper_2019_11_02_22_06_45.hdf5" |
|
|
28 |
|
|
|
29 |
# obtain model |
|
|
30 |
model = MyDeepModel(engine=engine, input_dims=img_shape, batch_size=batch_size, |
|
|
31 |
learning_rate=5e-4, |
|
|
32 |
num_epochs=num_epochs, decay_rate=0.8, |
|
|
33 |
decay_steps=1, |
|
|
34 |
weights="imagenet", verbose=1, |
|
|
35 |
train_image_dir=train_images_dir, |
|
|
36 |
model_filename=model_filename) |
|
|
37 |
|
|
|
38 |
model.load("epoch2.hdf5") |
|
|
39 |
|
|
|
40 |
#model.load(model_filename) # Use previous checkpoint |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
if (TRAINING == True): |
|
|
44 |
|
|
|
45 |
df = read_trainset(trainset_filename) |
|
|
46 |
ss = ShuffleSplit(n_splits=10, test_size=0.1, random_state=816).split(df.index) |
|
|
47 |
# lets go for the first fold only |
|
|
48 |
train_idx, valid_idx = next(ss) |
|
|
49 |
|
|
|
50 |
# Train the model |
|
|
51 |
model.fit_model(df.iloc[train_idx], df.iloc[valid_idx]) |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
test_df = read_testset(testset_filename) |
|
|
55 |
test_generator = DataGenerator(test_df.index, None, 1, img_shape, test_images_dir) |
|
|
56 |
best_model = K.models.load_model(model.model_filename, compile=False) |
|
|
57 |
|
|
|
58 |
prediction_df = create_submission(best_model, test_generator, test_df) |