Пример #1
0
from __future__ import division, print_function

import os
import sys
import tensorflow as tf
import numpy as np

from YOLOv3.model import yolov3
from YOLOv3.utils.misc_utils import parse_anchors, load_weights

num_class = 80
img_size = 416
weight_path = './data/darknet_weights/yolov3.weights'
save_path = './data/darknet_weights/yolov3.ckpt'
anchors = parse_anchors('./data/yolo_anchors.txt')

model = yolov3(80, anchors)
with tf.Session() as sess:
    inputs = tf.placeholder(tf.float32, [1, img_size, img_size, 3])

    with tf.variable_scope('yolov3'):
        feature_map = model.forward(inputs)

    saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

    load_ops = load_weights(tf.global_variables(scope='yolov3'), weight_path)
    sess.run(load_ops)
    saver.save(sess, save_path=save_path)
    print('TensorFlow model checkpoint has been saved to {}'.format(save_path))
# The path of the anchor txt file.
args.anchor_path = base_path + "marker_anchors.txt"
# Resize the input image with `new_size`, size format: [width, height]
args.new_size = [416, 416]
# Whether to use the letterbox resize.
args.letterbox_resize = True
# The path of the class names.
args.class_name_path = base_path + "data.names"
# The path of the weights to restore.
args.restore_path = "./data/darknet_weights/yolov3.ckpt"
# The probability threshold that the proposed bounding box needs to meet
args.score_thresh = 0.3
# The IoU threshold for non-maximum suppression (NMS) of similar bounding boxes; lower value cuts more
args.nms_thresh = 0.45

args.anchors = parse_anchors(args.anchor_path)
args.classes = read_class_names(args.class_name_path)
args.num_class = len(args.classes)

color_table = get_color_table(args.num_class)

if not os.path.exists(base_path + "test_eval/"):
    os.mkdir(base_path + "test_eval/")

# img_ori = cv2.imread(args.input_image)
# if args.letterbox_resize:
#     img, resize_ratio, dw, dh = letterbox_resize(img_ori, args.new_size[0], args.new_size[1])
# else:
#     height_ori, width_ori = img_ori.shape[:2]
#     img = cv2.resize(img_ori, tuple(args.new_size))
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
Пример #3
0
use_label_smooth = True  # Whether to use class label smoothing strategy.
use_focal_loss = True  # Whether to apply focal loss on the conf loss.
use_mix_up = True  # Whether to use mix up data augmentation strategy.
use_warm_up = True  # whether to use warm up strategy to prevent from gradient exploding.
warm_up_epoch = 3  # Warm up training epochs. Set to a larger value if gradient explodes.

### some constants in validation
# nms
nms_threshold = 0.45  # IoU threshold in nms operation
score_threshold = 0.01  # threshold of the probability of the classes in nms operation, i.e. score = pred_confs * pred_probs. set lower for higher recall.
nms_topk = 150  # keep at most nms_topk outputs after nms
# mAP eval
eval_threshold = 0.5  # the IoU threshold applied in mAP evaluation
use_voc_07_metric = False  # whether to use voc 2007 evaluation metric, i.e. the 11-point metric

### parse some params
if os.path.exists(anchor_path) and os.stat(anchor_path).st_size > 0:
    anchors = parse_anchors(anchor_path)
else:
    print("Anchors file not found or empty!")
classes = read_class_names(class_name_path)
class_num = len(classes)
train_img_cnt = len(open(train_file, 'r').readlines())
val_img_cnt = len(open(val_file, 'r').readlines())
train_batch_num = int(math.ceil(float(train_img_cnt) / batch_size))

lr_decay_freq = int(train_batch_num * lr_decay_epoch)
pw_boundaries = [
    float(i) * train_batch_num + global_step for i in pw_boundaries
]