def test_yolov2_post_process(): tf.InteractiveSession() image_size = [96, 64] batch_size = 2 classes = range(5) anchors = [(0.1, 0.2), (1.2, 1.1)] data_format = "NHWC" score_threshold = 0.25 nms_iou_threshold = 0.5 model = YoloV2( image_size=image_size, batch_size=batch_size, classes=classes, anchors=anchors, data_format=data_format, score_threshold=score_threshold, nms_iou_threshold=nms_iou_threshold, ) post_process = Sequence([ FormatYoloV2( image_size=image_size, classes=classes, anchors=anchors, data_format=data_format, ), ExcludeLowScoreBox(threshold=score_threshold), NMS( iou_threshold=nms_iou_threshold, classes=classes, ), ]) shape = (batch_size, len(anchors) * (len(classes) + 5), image_size[0] // 32, image_size[1] // 32) np_output = np.random.uniform(-2., 2., size=shape).astype(np.float32) output = tf.constant(np_output) ys = model.post_process(output) expected_ys = post_process(outputs=np_output)["outputs"] for y, expected_y in zip(ys, expected_ys): assert np.allclose(y.eval(), expected_y), (y.eval(), expected_y)
def test_format_yolov2_shape(): image_size = [128, 96] batch_size = 2 classes = range(8) anchors = [(0.1, 0.2), (1.2, 1.1)] post_process = FormatYoloV2( image_size=image_size, classes=classes, anchors=anchors, data_format="NCHW", ) shape = (batch_size, len(anchors) * (len(classes) + 5), image_size[0]//32, image_size[1]//32) output = np.random.uniform(-2., 2., size=shape).astype(np.float32) y = post_process(output)["outputs"] expected_shape = (batch_size, len(anchors) * len(classes) * image_size[0]//32 * image_size[1]//32, 6) assert expected_shape == y.shape
# pretrain IS_PRETRAIN = False PRETRAIN_VARS = [] PRETRAIN_DIR = "" PRETRAIN_FILE = "" PRE_PROCESSOR = Sequence([ResizeWithGtBoxes(size=IMAGE_SIZE), DivideBy255()]) anchors = [(1.3221, 1.73145), (3.19275, 4.00944), (5.05587, 8.09892), (9.47112, 4.84053), (11.2364, 10.0071)] score_threshold = 0.05 nms_iou_threshold = 0.5 nms_max_output_size = 100 POST_PROCESSOR = Sequence([ FormatYoloV2( image_size=IMAGE_SIZE, classes=CLASSES, anchors=anchors, data_format=DATA_FORMAT, ), ExcludeLowScoreBox(threshold=score_threshold), NMS( iou_threshold=nms_iou_threshold, max_output_size=nms_max_output_size, classes=CLASSES, ), ]) NETWORK = EasyDict() NETWORK.OPTIMIZER_CLASS = tf.train.MomentumOptimizer NETWORK.OPTIMIZER_KWARGS = {"momentum": 0.9} NETWORK.LEARNING_RATE_FUNC = tf.train.piecewise_constant # In the origianl yolov2 Paper, with a starting learning rate of 10−3, dividing it by 10 at 60 and 90 epochs.