forked from andrecreppe/gesture-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
131 lines (106 loc) · 4.05 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
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
print('[train.py] Initiating script - Training model')
import os
import sys
import tensorflow as tf
from object_detection.utils import config_util
from object_detection.protos import pipeline_pb2
from google.protobuf import text_format
WORKSPACE_PATH = 'Tensorflow/workspace'
SCRIPTS_PATH = 'Tensorflow/scripts'
APIMODEL_PATH = 'Tensorflow/models'
ANNOTATION_PATH = WORKSPACE_PATH + '/annotations'
IMAGE_PATH = WORKSPACE_PATH + '/images'
MODEL_PATH = WORKSPACE_PATH + '/models'
PRETRAINED_MODEL_PATH = WORKSPACE_PATH + '/pre-trained-models'
CUSTOM_MODEL_NAME = 'my_ssd_mobnet'
CONFIG_PATH = MODEL_PATH + '/' + CUSTOM_MODEL_NAME + '/pipeline.config'
labelQtd = 5
labels = [
{'name': 'Hello', 'id': 1},
{'name': 'Yes', 'id': 2},
{'name': 'No', 'id': 3},
{'name': 'Thanks', 'id': 4},
{'name': 'I Love You', 'id': 5}
]
trainSteps = 20000
#
print('[train.py] Creating Label Map')
with open(ANNOTATION_PATH + '\label_map.pbtxt', 'w') as f:
for label in labels:
f.write('item { \n')
f.write('\tname:\'{}\'\n'.format(label['name']))
f.write('\tid:{}\n'.format(label['id']))
f.write('}\n')
#
print('[train.py] Creating tfrecords')
trainRecordCommand = '{} -x {} -l {} -o {}'.format(
SCRIPTS_PATH + '/generate_tfrecord.py',
IMAGE_PATH + '/train',
ANNOTATION_PATH + '/label_map.pbtxt',
ANNOTATION_PATH + '/train.record'
)
testRecordCommand = '{} -x {} -l {} -o {}'.format(
SCRIPTS_PATH + '/generate_tfrecord.py',
IMAGE_PATH + '/test',
ANNOTATION_PATH + '/label_map.pbtxt',
ANNOTATION_PATH + '/test.record'
)
os.system('python {}'.format(trainRecordCommand))
os.system('python {}'.format(testRecordCommand))
#
print('[train.py] Updating Config For Transfer Learning')
print("[train.py] Make sure to have copied the 'pipeline.config' as stated in the README")
config = config_util.get_configs_from_pipeline_file(CONFIG_PATH)
pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()
with tf.io.gfile.GFile(CONFIG_PATH, 'r') as f:
proto_str = f.read()
text_format.Merge(proto_str, pipeline_config)
pipeline_config.model.ssd.num_classes = labelQtd
pipeline_config.train_config.batch_size = 4
pipeline_config.train_config.fine_tune_checkpoint = PRETRAINED_MODEL_PATH+'/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint/ckpt-0'
pipeline_config.train_config.fine_tune_checkpoint_type = 'detection'
pipeline_config.train_input_reader.label_map_path = ANNOTATION_PATH + '/label_map.pbtxt'
pipeline_config.train_input_reader.tf_record_input_reader.input_path[:] = [ANNOTATION_PATH + '/train.record']
pipeline_config.eval_input_reader[0].label_map_path = ANNOTATION_PATH + '/label_map.pbtxt'
pipeline_config.eval_input_reader[0].tf_record_input_reader.input_path[:] = [ANNOTATION_PATH + '/test.record']
config_text = text_format.MessageToString(pipeline_config)
with tf.io.gfile.GFile(CONFIG_PATH, 'wb') as f:
f.write(config_text)
#
def query_yes_no(question, default = 'yes'):
valid = {'yes': True, 'y': True, 'ye': True,
'no': False, 'n': False}
if default is None:
prompt = ' [y/n] '
elif default == 'yes':
prompt = ' [Y/n] '
elif default == 'no':
prompt = ' [y/N] '
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
print('[train.py] Maps and records ready!')
runnow = query_yes_no('[train.py] Do you want to execute the training now?')
runTrainingCommand = 'python {}/research/object_detection/model_main_tf2.py --model_dir={}/{} --pipeline_config_path={}/{}/pipeline.config --num_train_steps={}'.format(
APIMODEL_PATH,
MODEL_PATH,
CUSTOM_MODEL_NAME,
MODEL_PATH,
CUSTOM_MODEL_NAME,
trainSteps
)
if runnow:
os.system(runTrainingCommand)
print('[train.py] Finished training model')
else:
print('[train.py] Execute this command later in the project folder:')
print('> ' + runTrainingCommand)