img_ids=img_ids, ishape=ishape) for sample_order in range(num_of_examples): # generate x x, img_id = next(gen) # get labels bbox2d, _ = gety(coco=coco, img_id=img_id, classes=classes, frame_mode=frame_mode, mapping=mapping) _, ax = plt.subplots(figsize=(15, 7.35)) # Generates anchors anchor4d = genanchors( isize=ishape[:2], ssize=feature_map_size, # this is hardcode value, depends on pooling layers of base net asizes=asizes) anchor_4dtensor = tf.constant(value=anchor4d, dtype='float32') H = anchor4d.shape[0] W = anchor4d.shape[1] K = anchor4d.shape[2] # Generates y print('{}: START'.format(datetime.now().time()), end='\n') clzbbe_3dtensor = genpy(anchor_4dtensor=anchor_4dtensor, bbox2d=bbox2d[:, :4], iou_thresholds=iou_thresholds[:2], num_of_samples=num_of_samples) print('{}: END'.format(datetime.now().time()), end='\n') clz_3dtensor = clzbbe_3dtensor[:, :, :2*K] bbe_3dtensor = clzbbe_3dtensor[:, :, 2*K:]
from utils import genanchors print('tensorflow version: {}'.format(tf.__version__)) output_path = 'output' ishape = [512, 512, 3] ssize = [128, 128] asizes = [[32, 32]] total_classes = 1 resnet_settings = [[8, 8, 32], [24, [2, 2]]] nsm_iou_threshold = 0.2 nsm_score_threshold = 0.8 nsm_max_output_size = 100 abox_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssize, asizes=asizes), dtype='float32') # (h*w*k, 4) model = build_test_model(ishape=ishape, resnet_settings=resnet_settings, k=len(asizes), total_classes=total_classes, abox_2dtensor=abox_2dtensor, nsm_iou_threshold=nsm_iou_threshold, nsm_score_threshold=nsm_score_threshold, nsm_max_output_size=nsm_max_output_size) # model.summary() model.load_weights('{}/weights_best_precision.h5'.format(output_path), by_name=True) model.save('{}/model'.format(output_path))
rpn_head_dim = 64 nsm_iou_threshold = 0.2 nsm_score_threshold = 0.1 fc_denses = [1024, 1024] block_settings = [[16, 16, 64], [3, [2, 2]], [4, [2, 2]], [6, [2, 2]], [3, [1, 1]]] output_path = 'output' ann_file = '../datasets/coco/annotations/instances_face.json' img_dir = '../datasets/coco/images/face' coco = COCO(ann_file) ################################## # DETECTION TRAIN & VALIDATION ################################## abox4d = genanchors(isize=ishape[:2], ssize=ssize, asizes=asizes) anchor_4dtensor = tf.constant(value=abox4d, dtype='float32') rpn_model, detection_model = build_train_maskrcnn_non_fpn( ishape=ishape, anchor_4dtensor=anchor_4dtensor, classes=classes, max_num_of_rois=max_num_of_rois, nsm_iou_threshold=nsm_iou_threshold, nsm_score_threshold=nsm_score_threshold, unified_roi_size=unified_roi_size, rpn_head_dim=rpn_head_dim, fc_denses=fc_denses, block_settings=block_settings, base_block_trainable=True)
k0 = 5 top_down_pyramid_size = 512 rpn_head_dim = 512 fc_denses = [1024, 1024] block_settings = [[64, 64, 256], [3, [2, 2]], [4, [2, 2]], [6, [2, 2]], [3, [2, 2]]] output_path = 'output' ann_file = '../datasets/coco/annotations/instances_face.json' img_dir = '../datasets/coco/images/face' coco = COCO(ann_file) ################################## # DETECTION TRAIN & VALIDATION ################################## lvl1_aboxes = genanchors(isize=ishape[:2], ssize=(256, 512), asizes=asizes[0]) lvl2_aboxes = genanchors(isize=ishape[:2], ssize=(128, 256), asizes=asizes[1]) lvl3_aboxes = genanchors(isize=ishape[:2], ssize=(64, 128), asizes=asizes[2]) lvl4_aboxes = genanchors(isize=ishape[:2], ssize=(32, 64), asizes=asizes[3]) anchor_4dtensors = [ tf.constant(value=lvl1_aboxes, dtype='float32'), tf.constant(value=lvl2_aboxes, dtype='float32'), tf.constant(value=lvl3_aboxes, dtype='float32'), tf.constant(value=lvl4_aboxes, dtype='float32') ] rpn_model, detection_model = build_train_maskrcnn_fpn( ishape=ishape, anchor_4dtensors=anchor_4dtensors, classes=classes, max_num_of_rois=max_num_of_rois,
rpn_head_dim = params['rpn_head_dim'] fc_denses = params['fc_denses'] block_settings = params['resnet'] iou_thresholds = params['iou_thresholds'] nsm_iou_threshold = params['nsm_iou_threshold'] nsm_score_threshold = params['nsm_score_threshold'] classes = params['classes'] mapping = params['mapping'] frame_mode = params['frame_mode'] ann_file = params['dataset_anno_file_path'] img_dir = params['dataset_image_dir_path'] output_path = params['output_path'] coco = COCO(ann_file) lvl1_aboxes = genanchors(isize=ishape[:2], ssize=ssizes[0], asizes=asizes[0]) lvl2_aboxes = genanchors(isize=ishape[:2], ssize=ssizes[1], asizes=asizes[1]) lvl3_aboxes = genanchors(isize=ishape[:2], ssize=ssizes[2], asizes=asizes[2]) lvl4_aboxes = genanchors(isize=ishape[:2], ssize=ssizes[3], asizes=asizes[3]) anchor_4dtensors = [ tf.constant(value=lvl1_aboxes, dtype='float32'), tf.constant(value=lvl2_aboxes, dtype='float32'), tf.constant(value=lvl3_aboxes, dtype='float32'), tf.constant(value=lvl4_aboxes, dtype='float32')] rpn_model, _ = build_inference_maskrcnn_fpn( ishape=ishape, anchor_4dtensors=anchor_4dtensors, classes=classes, max_num_of_rois=max_num_of_rois, nsm_iou_threshold=nsm_iou_threshold,
# image data (h, w, channels) pix = io.imread('{}/{}'.format(img_dir, img['file_name'])) # padding input img x = np.zeros(ishape, dtype='int32') if len(pix.shape) == 2: x[:pix.shape[0], :pix.shape[1], 0] = pix x[:pix.shape[0], :pix.shape[1], 1] = pix x[:pix.shape[0], :pix.shape[1], 2] = pix else: x[:pix.shape[0], :pix.shape[1], :] = pix # generate anchor boxes aboxes = genanchors( isize=ishape[:2], ssize=feature_map_size, asizes=asizes) fig, ax = plt.subplots(figsize=(15, 7.35)) ax.imshow(x/255) for i in range(aboxes.shape[0]): for j in range(aboxes.shape[1]): for k in range(aboxes.shape[2]): if all_anchor_points: abox = aboxes[i, j, k] frame = box2frame(box=abox, apoint=[0, 0]) ax.add_patch(Rectangle( (frame[0], frame[1]), frame[2], frame[3],
[32, 32], [16, 16], ] asizes = [ [[32, 32]], [[64, 64]], [[128, 128]], ] total_classes = 1 resnet_settings = [[16, 16, 64], [4, [2, 2]], [8, [2, 2]], [16, [2, 2]]] top_down_pyramid_size = 64 nsm_iou_threshold = 0.2 nsm_score_threshold = 0.8 nsm_max_output_size = 10 a1box2d = genanchors(isize=ishape[:2], ssize=ssizes[0], asizes=asizes[0]) # (h1 * w1 * k1, 4) a2box2d = genanchors(isize=ishape[:2], ssize=ssizes[1], asizes=asizes[1]) # (h2 * w2 * k2, 4) a3box2d = genanchors(isize=ishape[:2], ssize=ssizes[2], asizes=asizes[2]) # (h3 * w3 * k3, 4) abox2d = np.concatenate([a1box2d, a2box2d, a3box2d], axis=0) # (h1*w1*k1 + h2*w2*k2 + h3*w3*k3, 4) abox_2dtensor = tf.constant( value=abox2d, dtype='float32') # (h1*w1*k1 + h2*w2*k2 + h3*w3*k3, 4) model = build_infer_model(ishape=ishape, resnet_settings=resnet_settings, top_down_pyramid_size=top_down_pyramid_size, k=[len(asizes[0]), len(asizes[1]), len(asizes[2])],
[32, 32], [16, 16], ] asizes = [ [[32, 32]], [[64, 64]], [[128, 128]], [[256, 256]], ] total_classes = 1 iou_thresholds = [[0.5, 0.6], [0.45, 0.55], [0.4, 0.5], [0.35, 0.45]] anchor_samplings = [256, 128, 64, 32] total_examples = 100 a1box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[0], asizes=asizes[0]), dtype='float32') # (h1*w1*k1, 4) a2box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[1], asizes=asizes[1]), dtype='float32') # (h2*w2*k2, 4) a3box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[2], asizes=asizes[2]), dtype='float32') # (h3*w3*k3, 4) a4box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[3], asizes=asizes[3]), dtype='float32') # (h4*w4*k4, 4) abox_2dtensors = [
[32, 32], ] asizes = [ [[64, 64]], [[128, 128]], [[256, 256]], [[512, 512]], ] total_classes = 1 resnet_settings = [[8, 8, 32], [8, [2, 2]], [8, [2, 2]], [8, [2, 2]], [4, [2, 2]]] top_down_pyramid_size = 64 nsm_iou_threshold = 0.2 nsm_score_threshold = 0.8 nsm_max_output_size = 100 a1box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[0], asizes=asizes[0]), dtype='float32') # (h1*w1*k1, 4) a2box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[1], asizes=asizes[1]), dtype='float32') # (h2*w2*k2, 4) a3box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[2], asizes=asizes[2]), dtype='float32') # (h3*w3*k3, 4) a4box_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssizes[3], asizes=asizes[3]), dtype='float32') # (h4*w4*k4, 4) abox_2dtensors = [a1box_2dtensor, a2box_2dtensor, a3box_2dtensor, a4box_2dtensor] abox_2dtensor = tf.concat(values=abox_2dtensors, axis=0) model = build_infer_model( ishape=ishape, resnet_settings=resnet_settings, top_down_pyramid_size=top_down_pyramid_size, k=[len(asizes[0]), len(asizes[1]), len(asizes[2]), len(asizes[3])], total_classes=total_classes, abox_2dtensor=abox_2dtensor, nsm_iou_threshold=nsm_iou_threshold, nsm_score_threshold=nsm_score_threshold,
output_path = 'output' test_anno_file_path = '../datasets/widerface/test.txt' test_image_dir = '../datasets/widerface/test' ishape = [64, 64, 3] ssize = [16, 16] asizes = [[32, 32]] total_classes = 1 resnet_settings = [[8, 8, 32], [32, [2, 2]]] nsm_iou_threshold = 0.2 nsm_score_threshold = 0.8 nsm_max_output_size = 100 total_test_examples = 100 abox_2dtensor = tf.constant(value=genanchors(isize=ishape[:2], ssize=ssize, asizes=asizes), dtype='float32') # (h*w*k, 4) model = build_test_model( ishape=ishape, resnet_settings=resnet_settings, k=len(asizes), total_classes=total_classes, abox_2dtensor=abox_2dtensor, nsm_iou_threshold=nsm_iou_threshold, nsm_score_threshold=nsm_score_threshold, nsm_max_output_size=nsm_max_output_size) # model.summary() model.load_weights('{}/weights_best_precision.h5'.format(output_path), by_name=True) test_dataset = load_dataset(anno_file_path=test_anno_file_path) gen = genbbox(