Skip to content

Commit 98e72cc

Browse files
YunYang1994YunYang1994
authored andcommitted
I hate tensorflow
1 parent ef94861 commit 98e72cc

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

quick_train.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,54 @@
1818
BATCH_SIZE = 1
1919
EPOCHS = 20
2020
LR = 0.001
21+
SHUFFLE_SIZE = 1
22+
weights_path = "/home/yang/test/yolov3.weights"
2123

2224
sess = tf.Session()
2325
classes = utils.read_coco_names('./data/coco.names')
2426
num_classes = len(classes)
2527
file_pattern = "./data/train_data/quick_train_data/tfrecords/quick_train_data*.tfrecords"
2628
anchors = utils.get_anchors('./data/yolo_anchors.txt')
2729

28-
30+
is_training = tf.placeholder(dtype=tf.bool, name="phase_train")
2931
dataset = tf.data.TFRecordDataset(filenames = tf.gfile.Glob(file_pattern))
3032
dataset = dataset.map(utils.parser(anchors, num_classes).parser_example, num_parallel_calls = 10)
31-
dataset = dataset.repeat().batch(BATCH_SIZE).prefetch(BATCH_SIZE)
33+
dataset = dataset.repeat().shuffle(SHUFFLE_SIZE).batch(BATCH_SIZE).prefetch(BATCH_SIZE)
3234
iterator = dataset.make_one_shot_iterator()
3335
example = iterator.get_next()
3436
images, *y_true = example
37+
model = yolov3.yolov3(num_classes)
38+
with tf.variable_scope('yolov3'):
39+
y_pred = model.forward(images, is_training=is_training)
40+
loss = model.compute_loss(y_pred, y_true)
41+
y_pred = model.predict(y_pred)
3542

36-
# model = yolov3.yolov3(num_classes)
37-
# with tf.variable_scope('yolov3'):
38-
# y_pred = model.forward(images, is_training=True)
39-
# result = model.compute_loss(y_pred, y_true)
4043

44+
# # train
4145
# optimizer = tf.train.AdamOptimizer(LR)
42-
# train_op = optimizer.minimize(result[3])
46+
# train_op = optimizer.minimize(loss[0])
4347
# sess.run(tf.global_variables_initializer())
44-
4548
# for epoch in range(EPOCHS):
46-
# run_items = sess.run([train_op] + result)
47-
# print("=> EPOCH:%4d\t| prec_50:%.4f\trec_50:%.4f\tavg_iou:%.4f\t | total_loss:%7.4f\tloss_coord:%7.4f"
48-
# "\tloss_sizes:%7.4f\tloss_confs:%7.4f\tloss_class:%7.4f" %(epoch, run_items[1], run_items[2],
49-
# run_items[3], run_items[4], run_items[5], run_items[6], run_items[7], run_items[8]))
49+
# run_items = sess.run([train_op, y_pred, y_true] + loss, feed_dict={is_training:True})
50+
# rec, prec, mAP = utils.evaluate(run_items[1], run_items[2], num_classes)
5051

51-
#************************ test with yolov3.weights ****************************#
52+
# print("=> EPOCH: %2d\ttotal_loss:%7.4f\tloss_coord:%7.4f\tloss_sizes:%7.4f\tloss_confs:%7.4f\tloss_class:%7.4f"
53+
# "\trec:%.2f\tprec:%.2f\tmAP:%.2f"
54+
# %(epoch, run_items[3], run_items[4], run_items[5], run_items[6], run_items[7], rec, prec, mAP))
5255

53-
model = yolov3.yolov3(num_classes)
54-
with tf.variable_scope('yolov3'):
55-
y_pred = model.forward(images, is_training=False)
56-
load_ops = utils.load_weights(tf.global_variables(scope='yolov3'), "./checkpoint/yolov3.weights")
57-
sess.run(load_ops)
58-
result = model.compute_loss(y_pred, y_true)
56+
57+
58+
# test
59+
load_ops = utils.load_weights(tf.global_variables(scope='yolov3'), weights_path)
60+
sess.run(load_ops)
5961

6062
for epoch in range(EPOCHS):
61-
run_items = sess.run(result)
62-
print("=> EPOCH:%4d\t| prec_50:%.4f\trec_50:%.4f\tavg_iou:%.4f\t | total_loss:%7.4f\tloss_coord:%7.4f"
63-
"\tloss_sizes:%7.4f\tloss_confs:%7.4f\tloss_class:%7.4f" %(epoch, run_items[0], run_items[1],
64-
run_items[2], run_items[3], run_items[4], run_items[5], run_items[6], run_items[7]))
63+
run_items = sess.run([y_pred, y_true] + loss, feed_dict={is_training:True})
64+
rec, prec, mAP = utils.evaluate(run_items[0], run_items[1], num_classes, score_thresh=0.3, iou_thresh=0.5)
65+
66+
print("=> EPOCH: %2d\ttotal_loss:%7.4f\tloss_coord:%7.4f\tloss_sizes:%7.4f\tloss_confs:%7.4f\tloss_class:%7.4f"
67+
"\trec:%.2f\tprec:%.2f\tmAP:%.2f"
68+
%(epoch, run_items[2], run_items[3], run_items[4], run_items[5], run_items[6], rec, prec, mAP))
69+
6570

6671

0 commit comments

Comments
 (0)