-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsnippet_train_model.py
139 lines (114 loc) · 4.91 KB
/
snippet_train_model.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pickle
from importlib import reload
import tensorflow as tf
import models.custom.detector as g
from libs.dataset_utils import prepare_data_from_tfrecord
from configs.project_config import project_path
"""
Constant
"""
NUM_CLASS = 200
INPUT_SHAPE = (64, 64, 3)
MODEL_BASE_INPUT_SHAPE = (224, 224, 3)
tfrecord_train_dir = "{}/data/tiny-imagenet-200/tfrecord/train".format(project_path)
tfrecord_valid_dir = "{}/data/tiny-imagenet-200/tfrecord/valid".format(project_path)
tfrecord_test_dir = "{}/data/tiny-imagenet-200/tfrecord/test".format(project_path)
meta_path = "{}/data/tiny-imagenet-200/meta.pickle".format(project_path)
pretrained_ckpt_path = "{}/checkpoints/inception_v3/inception_v3.ckpt".format(project_path)
vanila_model_save_path = "{}/checkpoints/vanila_inception_v3/vanila_inception_v3".format(project_path)
has_model_save_path = "{}/checkpoints/has_inception_v3/has_inception_v3".format(project_path)
# ==============================================================================
model_base_name = "InceptionV3"
model = g.Detector(output_dim=NUM_CLASS,
input_shape=INPUT_SHAPE,
model_base_input_shape=MODEL_BASE_INPUT_SHAPE,
model_base_name=model_base_name,
model_name="hide_and_seek")
# ==============================================================================
with model.g.as_default():
"""
Read Data
"""
d = prepare_data_from_tfrecord(
tfrecord_train_dir=tfrecord_train_dir,
tfrecord_valid_dir=tfrecord_valid_dir,
tfrecord_test_dir=tfrecord_test_dir,
batch_size=32)
(X, Y,
init_dataset_train,
init_dataset_train_has,
init_dataset_valid) = (d['X'], d['Y'],
d['init_dataset_train'],
d['init_dataset_train_has'],
d['init_dataset_valid'])
with open(meta_path, "rb") as f:
meta = pickle.load(f)
model.meta.update(meta)
"""
Initialize with pretrained weights
"""
variables_to_restore = tf.contrib.framework.get_variables_to_restore(
include=[model_base_name])
init_pretrain_fn = tf.contrib.framework.assign_from_checkpoint_fn(
pretrained_ckpt_path, variables_to_restore)
init_pretrain_fn(model.sess)
# ==============================================================================
"""
Vanila
"""
model.train_with_dataset_api(X=X,
Y=Y,
init_dataset_train=init_dataset_train,
init_dataset_valid=init_dataset_valid,
n_epoch=10,
learning_rate=0.001,
reg_lambda=0.,
dropout_keep_prob=8.,
patience=10,
verbose_interval=1,
mode=g.MODE_TRAIN_ONLY_CLF,
save_dir_path=None)
model.train_with_dataset_api(X=X,
Y=Y,
init_dataset_train=init_dataset_train,
init_dataset_valid=init_dataset_valid,
n_epoch=3,
learning_rate=0.001,
reg_lambda=0.,
dropout_keep_prob=8.,
patience=10,
verbose_interval=1,
mode=g.MODE_TRAIN_GLOBAL,
save_dir_path=None)
model.save(vanila_model_save_path)
# ==============================================================================
"""
HaS
"""
model.train_with_dataset_api(X=X,
Y=Y,
init_dataset_train=init_dataset_train_has,
init_dataset_valid=init_dataset_valid,
n_epoch=10,
learning_rate=0.001,
reg_lambda=0.,
dropout_keep_prob=8.,
patience=10,
verbose_interval=1,
mode=g.MODE_TRAIN_ONLY_CLF,
flag_has=True,
save_dir_path=None)
model.train_with_dataset_api(X=X,
Y=Y,
init_dataset_train=init_dataset_train_has,
init_dataset_valid=init_dataset_valid,
n_epoch=3,
learning_rate=0.001,
reg_lambda=0.,
dropout_keep_prob=8.,
patience=10,
verbose_interval=1,
mode=g.MODE_TRAIN_GLOBAL,
flag_has=True,
save_dir_path=None)
model.save(has_model_save_path)