forked from PatrickLib/captcha_recognize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
captcha_train.py
90 lines (72 loc) · 2.44 KB
/
captcha_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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
from datetime import datetime
import argparse
import sys
import tensorflow as tf
import captcha_model as captcha
FLAGS = None
def run_train():
"""Train CAPTCHA for a number of steps."""
with tf.Graph().as_default():
images, labels = captcha.inputs(train=True, batch_size=FLAGS.batch_size)
logits = captcha.inference(images, keep_prob=0.5)
loss = captcha.loss(logits, labels)
train_op = captcha.training(loss)
saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables())
init_op = tf.group(tf.compat.v1.global_variables_initializer(),
tf.compat.v1.local_variables_initializer())
sess = tf.compat.v1.Session()
sess.run(init_op)
coord = tf.compat.v1.train.Coordinator()
threads = tf.compat.v1.train.start_queue_runners(sess=sess, coord=coord)
try:
step = 0
while not coord.should_stop():
start_time = time.time()
_, loss_value = sess.run([train_op, loss])
duration = time.time() - start_time
if step % 10 == 0:
print('>> Step %d run_train: loss = %.2f (%.3f sec)' % (step, loss_value,
duration))
if step % 100 == 0:
print('>> %s Saving in %s' % (datetime.now(), FLAGS.checkpoint))
saver.save(sess, FLAGS.checkpoint, global_step=step)
step += 1
except Exception as e:
print('>> %s Saving in %s' % (datetime.now(), FLAGS.checkpoint))
saver.save(sess, FLAGS.checkpoint, global_step=step)
coord.request_stop(e)
finally:
coord.request_stop()
coord.join(threads)
sess.close()
def main(_):
if tf.io.gfile.exists(FLAGS.train_dir):
tf.io.gfile.rmtree(FLAGS.train_dir)
tf.io.gfile.makedirs(FLAGS.train_dir)
run_train()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--batch_size',
type=int,
default=128,
help='Batch size.'
)
parser.add_argument(
'--train_dir',
type=str,
default='./captcha_train',
help='Directory where to write event logs.'
)
parser.add_argument(
'--checkpoint',
type=str,
default='./captcha_train/captcha',
help='Directory where to write checkpoint.'
)
FLAGS, unparsed = parser.parse_known_args()
tf.compat.v1.app.run(main=main, argv=[sys.argv[0]] + unparsed)