김성주

fixed errors for evaluation

......@@ -60,7 +60,7 @@ args = parser.parse_args()
args.anchors = parse_anchors(args.anchor_path)
args.classes = read_class_names(args.class_name_path)
args.class_num = len(args.classes)
args.img_cnt = len(open(args.eval_file, 'r').readlines())
args.img_cnt = TFRecordIterator(args.eval_file, 'GZIP').count()
# setting placeholders
is_training = tf.placeholder(dtype=tf.bool, name="phase_train")
......
......@@ -183,4 +183,48 @@ with tf.Session() as sess:
if args.save_optimizer and mAP > best_mAP:
best_mAP = mAP
saver_best.save(sess, args.save_dir + 'best_model_Epoch_{}_step_{}_mAP_{:.4f}_loss_{:.4f}_lr_{:.7g}'.format(
epoch, int(__global_step), best_mAP, val_loss_total.average, __lr))
\ No newline at end of file
epoch, int(__global_step), best_mAP, val_loss_total.average, __lr))
saver_to_restore.save(sess, restore_path)
## all epoches end
sess.run(val_init_op)
val_loss_total, val_loss_xy, val_loss_wh, val_loss_conf, val_loss_class = AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter()
val_preds = []
for j in trange(val_img_cnt):
__image_ids, __y_pred, __loss = sess.run([image_ids, y_pred, loss],
feed_dict={is_training: False})
pred_content = get_preds_gpu(sess, gpu_nms_op, pred_boxes_flag, pred_scores_flag, __image_ids, __y_pred)
val_preds.extend(pred_content)
val_loss_total.update(__loss[0])
val_loss_xy.update(__loss[1])
val_loss_wh.update(__loss[2])
val_loss_conf.update(__loss[3])
val_loss_class.update(__loss[4])
# calc mAP
rec_total, prec_total, ap_total = AverageMeter(), AverageMeter(), AverageMeter()
gt_dict = parse_gt_rec(val_file, 'GZIP', img_size, letterbox_resize)
info = '======> Epoch: {}, global_step: {}, lr: {:.6g} <======\n'.format(epoch, __global_step, __lr)
for ii in range(class_num):
npos, nd, rec, prec, ap = voc_eval(gt_dict, val_preds, ii, iou_thres=eval_threshold, use_07_metric=use_voc_07_metric)
info += 'EVAL: Class {}: Recall: {:.4f}, Precision: {:.4f}, AP: {:.4f}\n'.format(ii, rec, prec, ap)
rec_total.update(rec, npos)
prec_total.update(prec, nd)
ap_total.update(ap, 1)
mAP = ap_total.average
info += 'EVAL: Recall: {:.4f}, Precison: {:.4f}, mAP: {:.4f}\n'.format(rec_total.average, prec_total.average, mAP)
info += 'EVAL: loss: total: {:.2f}, xy: {:.2f}, wh: {:.2f}, conf: {:.2f}, class: {:.2f}\n'.format(
val_loss_total.average, val_loss_xy.average, val_loss_wh.average, val_loss_conf.average, val_loss_class.average)
print(info)
if save_optimizer and mAP > best_mAP:
best_mAP = mAP
saver_best.save(sess, save_dir + 'best_model_Epoch_{}_step_{}_mAP_{:.4f}_loss_{:.4f}_lr_{:.7g}'.format(
epoch, int(__global_step), best_mAP, val_loss_total.average, __lr))
saver_to_restore.save(sess, restore_path)
\ No newline at end of file
......
......@@ -18,7 +18,7 @@
"metadata": {
"id": "p0y3wIkfSuIT",
"colab_type": "code",
"outputId": "eeedd664-406a-43ff-aa5e-bd48963494c4",
"outputId": "e96d25c9-630a-4c10-8fe2-42a0d7771c12",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
......@@ -1171,7 +1171,7 @@
"metadata": {
"id": "X4uQxNl0FRli",
"colab_type": "code",
"outputId": "c2b22c73-6195-4b80-d1b4-5ada76ef3da8",
"outputId": "538b5816-5f2e-4cb1-bb96-6d41636da6a0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 161
......@@ -1540,7 +1540,7 @@
"metadata": {
"id": "Nlddq-K7AJin",
"colab_type": "code",
"outputId": "c5baed55-0d4e-4c65-fa7d-340b27baf8f9",
"outputId": "11d978a6-d7d6-4c1e-c251-27b8cfae7593",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 89
......@@ -1557,7 +1557,7 @@
"data_path = '/content/gdrive/My Drive/yolo/data/'\n",
"train_file = data_path + 'train.tfrecord' # The path of the training txt file.\n",
"val_file = data_path + 'val.tfrecord' # The path of the validation txt file.\n",
"restore_path = data_path + 'darknet_weights/yolov3.ckpt' # The path of the weights to restore.\n",
"restore_path = data_path + 'yolov3.ckpt' # The path of the weights to restore.\n",
"save_dir = '/content/gdrive/My Drive/yolo/checkpoint/' # The directory of the weights to save.\n",
"\n",
"### we are not using tensorboard logs in this code\n",
......@@ -1587,8 +1587,8 @@
"### Learning rate and optimizer\n",
"optimizer_name = 'momentum' # Chosen from [sgd, momentum, adam, rmsprop]\n",
"save_optimizer = True # Whether to save the optimizer parameters into the checkpoint file.\n",
"learning_rate_init = 1e-4\n",
"lr_type = 'piecewise' # Chosen from [fixed, exponential, cosine_decay, cosine_decay_restart, piecewise]\n",
"learning_rate_init = 1e-3\n",
"lr_type = 'fixed' # Chosen from [fixed, exponential, cosine_decay, cosine_decay_restart, piecewise]\n",
"lr_decay_epoch = 5 # Epochs after which learning rate decays. Int or float. Used when chosen `exponential` and `cosine_decay_restart` lr_type.\n",
"lr_decay_factor = 0.96 # The learning rate decay factor. Used when chosen `exponential` lr_type.\n",
"lr_lower_bound = 1e-6 # The minimum learning rate.\n",
......@@ -1659,7 +1659,11 @@
"metadata": {
"id": "NagT2oNZFf0q",
"colab_type": "code",
"colab": {}
"colab": {
"base_uri": "https://localhost:8080/",
"height": 809
},
"outputId": "60b0c3d0-cbc7-43d5-beef-80116b691fc7"
},
"source": [
"## train\n",
......@@ -1835,10 +1839,112 @@
" if save_optimizer and mAP > best_mAP:\n",
" best_mAP = mAP\n",
" saver_best.save(sess, save_dir + 'best_model_Epoch_{}_step_{}_mAP_{:.4f}_loss_{:.4f}_lr_{:.7g}'.format(\n",
" epoch, int(__global_step), best_mAP, val_loss_total.average, __lr))"
" epoch, int(__global_step), best_mAP, val_loss_total.average, __lr))\n",
" saver_to_restore.save(sess, restore_path)\n",
" \n",
"\n",
" ## all epoches end\n",
" sess.run(val_init_op)\n",
"\n",
" val_loss_total, val_loss_xy, val_loss_wh, val_loss_conf, val_loss_class = AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter()\n",
"\n",
" val_preds = []\n",
"\n",
" for j in trange(val_img_cnt):\n",
" __image_ids, __y_pred, __loss = sess.run([image_ids, y_pred, loss],\n",
" feed_dict={is_training: False})\n",
" pred_content = get_preds_gpu(sess, gpu_nms_op, pred_boxes_flag, pred_scores_flag, __image_ids, __y_pred)\n",
" val_preds.extend(pred_content)\n",
" val_loss_total.update(__loss[0])\n",
" val_loss_xy.update(__loss[1])\n",
" val_loss_wh.update(__loss[2])\n",
" val_loss_conf.update(__loss[3])\n",
" val_loss_class.update(__loss[4])\n",
"\n",
" # calc mAP\n",
" rec_total, prec_total, ap_total = AverageMeter(), AverageMeter(), AverageMeter()\n",
" gt_dict = parse_gt_rec(val_file, 'GZIP', img_size, letterbox_resize)\n",
"\n",
" info = '======> Epoch: {}, global_step: {}, lr: {:.6g} <======\\n'.format(epoch, __global_step, __lr)\n",
"\n",
" for ii in range(class_num):\n",
" npos, nd, rec, prec, ap = voc_eval(gt_dict, val_preds, ii, iou_thres=eval_threshold, use_07_metric=use_voc_07_metric)\n",
" info += 'EVAL: Class {}: Recall: {:.4f}, Precision: {:.4f}, AP: {:.4f}\\n'.format(ii, rec, prec, ap)\n",
" rec_total.update(rec, npos)\n",
" prec_total.update(prec, nd)\n",
" ap_total.update(ap, 1)\n",
"\n",
" mAP = ap_total.average\n",
" info += 'EVAL: Recall: {:.4f}, Precison: {:.4f}, mAP: {:.4f}\\n'.format(rec_total.average, prec_total.average, mAP)\n",
" info += 'EVAL: loss: total: {:.2f}, xy: {:.2f}, wh: {:.2f}, conf: {:.2f}, class: {:.2f}\\n'.format(\n",
" val_loss_total.average, val_loss_xy.average, val_loss_wh.average, val_loss_conf.average, val_loss_class.average)\n",
" print(info)\n",
"\n",
" if save_optimizer and mAP > best_mAP:\n",
" best_mAP = mAP\n",
" saver_best.save(sess, save_dir + 'best_model_Epoch_{}_step_{}_mAP_{:.4f}_loss_{:.4f}_lr_{:.7g}'.format(\n",
" epoch, int(__global_step), best_mAP, val_loss_total.average, __lr))\n",
" saver_to_restore.save(sess, restore_path)"
],
"execution_count": 0,
"outputs": []
"outputs": [
{
"output_type": "stream",
"text": [
"WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/array_ops.py:1475: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
"WARNING:tensorflow:Entity <function <lambda> at 0x7f41ba49e378> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Str'\n",
"WARNING: Entity <function <lambda> at 0x7f41ba49e378> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Str'\n",
"WARNING:tensorflow:From <ipython-input-10-64b9cefa41e4>:21: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"tf.py_func is deprecated in TF V2. Instead, there are two\n",
" options available in V2.\n",
" - tf.py_function takes a python function which manipulates tf eager\n",
" tensors instead of numpy arrays. It's easy to convert a tf eager tensor to\n",
" an ndarray (just call tensor.numpy()) but having access to eager tensors\n",
" means `tf.py_function`s can use accelerators such as GPUs as well as\n",
" being differentiable using a gradient tape.\n",
" - tf.numpy_function maintains the semantics of the deprecated tf.py_func\n",
" (it is not differentiable, and manipulates numpy arrays). It drops the\n",
" stateful argument making all functions stateful.\n",
" \n",
"WARNING:tensorflow:Entity <function <lambda> at 0x7f41ba4b4ae8> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Str'\n",
"WARNING: Entity <function <lambda> at 0x7f41ba4b4ae8> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Str'\n",
"WARNING:tensorflow:From <ipython-input-10-64b9cefa41e4>:36: DatasetV1.output_types (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.data.get_output_types(dataset)`.\n",
"WARNING:tensorflow:From <ipython-input-10-64b9cefa41e4>:36: DatasetV1.output_shapes (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.data.get_output_shapes(dataset)`.\n",
"WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/data/ops/iterator_ops.py:347: Iterator.output_types (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.data.get_output_types(iterator)`.\n",
"WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/data/ops/iterator_ops.py:348: Iterator.output_shapes (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.data.get_output_shapes(iterator)`.\n",
"WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/data/ops/iterator_ops.py:350: Iterator.output_classes (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.data.get_output_classes(iterator)`.\n",
"Img size: Tensor(\"yolov3/strided_slice:0\", shape=(2,), dtype=int32)\n",
"WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/contrib/layers/python/layers/layers.py:1057: Layer.apply (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `layer.__call__` method instead.\n",
"Saving optimizer parameters: ON\n",
"\n",
"Start training...: Total epoches = 10 \n",
"\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
" 3%|▎ | 3/95 [00:54<31:17, 20.41s/it]"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
......@@ -1850,58 +1956,32 @@
"source": [
"## evaluation (test)\n",
"\n",
"import argparse\n",
"class ArgumentObject(object):\n",
" pass\n",
"\n",
"if not training:\n",
"\n",
" ### ArgumentParser\n",
" parser = argparse.ArgumentParser(description=\"YOLO-V3 eval procedure.\")\n",
"\n",
" # paths\n",
" parser.add_argument(\"--eval_file\", type=str, default=\"/content/gdrive/My Drive/yolo/data/test.tfrecord\",\n",
" help=\"The path of the validation or test txt file.\")\n",
"\n",
" parser.add_argument(\"--restore_path\", type=str, default=\"/content/gdrive/My Drive/yolo/data/darknet_weights/yolov3.ckpt\",\n",
" help=\"The path of the weights to restore.\")\n",
"\n",
" parser.add_argument(\"--anchor_path\", type=str, default=\"./content/gdrive/My Drive/yolo/data/yolo_anchors.txt\",\n",
" help=\"The path of the anchor txt file.\")\n",
"\n",
" parser.add_argument(\"--class_name_path\", type=str, default=\"/content/gdrive/My Drive/yolo/data/classes.txt\",\n",
" help=\"The path of the class names.\")\n",
"\n",
" # some numbers\n",
" parser.add_argument(\"--img_size\", nargs='*', type=int, default=[416, 416],\n",
" help=\"Resize the input image to `img_size`, size format: [width, height]\")\n",
"\n",
" parser.add_argument(\"--letterbox_resize\", type=lambda x: (str(x).lower() == 'true'), default=False,\n",
" help=\"Whether to use the letterbox resize, i.e., keep the original image aspect ratio.\")\n",
"\n",
" parser.add_argument(\"--num_threads\", type=int, default=10,\n",
" help=\"Number of threads for image processing used in tf.data pipeline.\")\n",
"\n",
" parser.add_argument(\"--prefetech_buffer\", type=int, default=5,\n",
" help=\"Prefetech_buffer used in tf.data pipeline.\")\n",
"\n",
" parser.add_argument(\"--nms_threshold\", type=float, default=0.45,\n",
" help=\"IOU threshold in nms operation.\")\n",
"\n",
" parser.add_argument(\"--score_threshold\", type=float, default=0.01,\n",
" help=\"Threshold of the probability of the classes in nms operation.\")\n",
"\n",
" parser.add_argument(\"--nms_topk\", type=int, default=400,\n",
" help=\"Keep at most nms_topk outputs after nms.\")\n",
"\n",
" parser.add_argument(\"--use_voc_07_metric\", type=lambda x: (str(x).lower() == 'true'), default=False,\n",
" help=\"Whether to use the voc 2007 mAP metrics.\")\n",
"\n",
" args = parser.parse_args()\n",
" args = ArgumentObject()\n",
" args.eval_file = \"/content/gdrive/My Drive/yolo/data/test.tfrecord\"\n",
" args.restore_path = \"/content/gdrive/My Drive/yolo/data/yolov3.ckpt\"\n",
" args.anchor_path = \"/content/gdrive/My Drive/yolo/data/yolo_anchors.txt\"\n",
" args.class_name_path = \"/content/gdrive/My Drive/yolo/data/classes.txt\"\n",
" args.img_size = [416, 416]\n",
" args.letterbox_resize = False\n",
" args.num_threads = 10\n",
" args.prefetech_buffer = 5\n",
" args.nms_threshold = 0.45\n",
" args.score_threshold = 0.01\n",
" args.nms_topk = 400\n",
" args.use_voc_07_metric = False\n",
"\n",
" # args params\n",
" args.anchors = parse_anchors(args.anchor_path)\n",
" args.classes = read_class_names(args.class_name_path)\n",
" args.class_num = len(args.classes)\n",
" args.img_cnt = len(open(args.eval_file, 'r').readlines())\n",
"\n",
"\n",
" args.img_cnt = TFRecordIterator(args.eval_file, 'GZIP').count()\n",
"\n",
" # setting placeholders\n",
" is_training = tf.placeholder(dtype=tf.bool, name=\"phase_train\")\n",
......