def main(_):
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)
    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         num_classes=cfg['num_classes'],
                         head_type=cfg['head_type'],
                         embd_shape=cfg['embd_shape'],
                         w_decay=cfg['w_decay'],
                         training=True)
    model.summary()

    learning_rate = tf.constant(cfg['base_lr'])
    optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate,
                                        momentum=0.9,
                                        nesterov=True)
    loss_fn = SoftmaxLoss()
    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
    else:
        print("[*] training from scratch.")

    model.compile(optimizer=optimizer, loss=loss_fn)

    data_path = 'data'
    lfw, lfw_issame = get_val_pair(data_path, 'lfw_align_112/lfw')
    lfw = np.transpose(lfw, [0, 2, 3, 1]) * 0.5 + 0.5

    image_1 = lfw[0::2]
    image_2 = lfw[1::2]

    augment = ImgAugTransform()
    if FLAGS.mode == 'eager_tf':
        top_left_all = [0.008807]
        for epochs in range(cfg['epochs']):
            logging.info("Shuffle ms1m dataset.")
            dataset_len = cfg['num_samples']
            steps_per_epoch = dataset_len // cfg['batch_size']
            train_dataset = dataset.load_tfrecord_dataset(
                cfg['train_dataset'],
                cfg['batch_size'],
                cfg['binary_img'],
                is_ccrop=cfg['is_ccrop'])

            for batch, (x, y) in enumerate(train_dataset):
                x0_new = np.array(x[0], dtype=np.uint8)
                x1_new = np.array(x[1], dtype=np.float32)
                for i in np.arange(len(x0_new)):
                    x0_new[i] = augment(x0_new[i])
                temp = np.array(x0_new, dtype=np.float32) / 255.0

                loss = model.train_on_batch(*((temp, x1_new), x1_new))

                if batch % 50 == 0:
                    verb_str = "Epoch {}/{}: {}/{}, loss={:.6f}, lr={:.6f}"
                    print(
                        verb_str.format(
                            epochs, cfg['epochs'], batch, steps_per_epoch,
                            loss, cfg['base_lr'] / (1.0 + cfg['w_decay'] *
                                                    (epochs * 45489 + batch))))

                    if batch % cfg['save_steps'] == 0:
                        resnet_model = tf.keras.Model(
                            inputs=model.get_layer('resnet50').input,
                            outputs=model.get_layer('resnet50').output)

                        output_model = tf.keras.Model(
                            inputs=model.get_layer('OutputLayer').input,
                            outputs=model.get_layer('OutputLayer').output)

                        dist_all = []
                        top_left_batch = []
                        for idx in range(0, len(lfw_issame),
                                         cfg['batch_size']):
                            tem = resnet_model.predict(
                                image_1[idx:idx + cfg['batch_size']])
                            embeds_1 = output_model.predict(tem)
                            norm_embeds_1 = preprocessing.normalize(embeds_1,
                                                                    norm='l2',
                                                                    axis=1)

                            tem = resnet_model.predict(
                                image_2[idx:idx + cfg['batch_size']])
                            embeds_2 = output_model.predict(tem)
                            norm_embeds_2 = preprocessing.normalize(embeds_2,
                                                                    norm='l2',
                                                                    axis=1)

                            diff = np.subtract(norm_embeds_1, norm_embeds_2)
                            dist = np.sqrt(np.sum(np.square(diff), 1)) / 2
                            dist_all.extend(dist)

                        thresholds = np.arange(0, 1, 0.01)
                        for thr in thresholds:
                            tpr, fpr, _ = calculate_accuracy(
                                thr, np.array(dist_all), lfw_issame)
                            top_left = np.sqrt((1 - tpr)**2 + fpr**2)
                            top_left_batch.append(top_left)
                        print(
                            "The current top left: {:.6f}     Threshold: {:.2f}"
                            .format(np.min(top_left_batch),
                                    0.01 * np.argmin(top_left_batch)))

                        if not len(top_left_all):
                            print(
                                "The best top left: {:.6f}     Threshold: {:.2f}"
                                .format(np.min(top_left_batch),
                                        0.01 * np.argmin(top_left_batch)))
                        else:
                            print("The best top left: {:.6f}".format(
                                top_left_all[-1]))

                        if not len(top_left_all):
                            top_left_all.append(np.min(top_left_batch))
                            print('[*] save ckpt file!')
                            model.save_weights(
                                'checkpoints/{}/e_{}_b_{}.ckpt'.format(
                                    cfg['sub_name'], epochs,
                                    batch % steps_per_epoch))

                        elif top_left_all[-1] > np.min(top_left_batch):
                            top_left_all.append(np.min(top_left_batch))
                            print('[*] save ckpt file!')
                            model.save_weights(
                                'checkpoints/{}/e_{}_b_{}.ckpt'.format(
                                    cfg['sub_name'], epochs,
                                    batch % steps_per_epoch))

                    model.save_weights('checkpoints/train_{}/{}.ckpt'.format(
                        cfg['sub_name'], cfg['sub_name']))
Ejemplo n.º 2
0
def main(_argv):
    # init
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg_path = "./configs/retinaface_mbv2.yaml"
    cfg = load_yaml(cfg_path)

    # define network
    model = RetinaFaceModel(cfg,
                            training=False,
                            iou_th=FLAGS.iou_th,
                            score_th=FLAGS.score_th)

    # load checkpoint
    checkpoint_dir = './checkpoints/' + cfg['sub_name']
    checkpoint = tf.train.Checkpoint(model=model)
    if tf.train.latest_checkpoint(checkpoint_dir):
        checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
        print("[*] load ckpt from {}.".format(
            tf.train.latest_checkpoint(checkpoint_dir)))
    else:
        print("[*] Cannot find ckpt from {}.".format(checkpoint_dir))
        exit()

    if not FLAGS.webcam:
        if not os.path.exists(FLAGS.img_path):
            print(f"cannot find image path from {FLAGS.img_path}")
            exit()

        print("[*] Processing on single image {}".format(FLAGS.img_path))

        img_raw = cv2.imread(FLAGS.img_path)
        img_height_raw, img_width_raw, _ = img_raw.shape
        img = np.float32(img_raw.copy())

        if FLAGS.down_scale_factor < 1.0:
            img = cv2.resize(img, (0, 0),
                             fx=FLAGS.down_scale_factor,
                             fy=FLAGS.down_scale_factor,
                             interpolation=cv2.INTER_LINEAR)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # pad input image to avoid unmatched shape problem
        img, pad_params = pad_input_image(img, max_steps=max(cfg['steps']))

        # run model
        outputs = model(img[np.newaxis, ...]).numpy()

        # recover padding effect
        outputs = recover_pad_output(outputs, pad_params)

        # draw and save results
        save_img_path = os.path.join('out_' + os.path.basename(FLAGS.img_path))
        for prior_index in range(len(outputs)):
            draw_bbox_landm(img_raw, outputs[prior_index], img_height_raw,
                            img_width_raw)
            cv2.imwrite(save_img_path, img_raw)
        print(f"[*] save result at {save_img_path}")

    else:
        cam = cv2.VideoCapture(0)

        start_time = time.time()
        while True:
            _, frame = cam.read()
            if frame is None:
                print("no cam input")

            frame_height, frame_width, _ = frame.shape
            img = np.float32(frame.copy())
            if FLAGS.down_scale_factor < 1.0:
                img = cv2.resize(img, (0, 0),
                                 fx=FLAGS.down_scale_factor,
                                 fy=FLAGS.down_scale_factor,
                                 interpolation=cv2.INTER_LINEAR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            # pad input image to avoid unmatched shape problem
            img, pad_params = pad_input_image(img, max_steps=max(cfg['steps']))

            # run model
            outputs = model(img[np.newaxis, ...]).numpy()

            # recover padding effect
            outputs = recover_pad_output(outputs, pad_params)

            # draw results
            for prior_index in range(len(outputs)):
                draw_bbox_landm(frame, outputs[prior_index], frame_height,
                                frame_width)

            # calculate fps
            fps_str = "FPS: %.2f" % (1 / (time.time() - start_time))
            start_time = time.time()
            cv2.putText(frame, fps_str, (25, 25), cv2.FONT_HERSHEY_DUPLEX,
                        0.75, (0, 255, 0), 2)

            # show frame
            cv2.imshow('frame', frame)
            if cv2.waitKey(1) == ord('q'):
                exit()
Ejemplo n.º 3
0
def main(_argv):
    pipe = rs.pipeline()

    # Build config object and request pose data
    cfg = rs.config()
    cfg.enable_stream(rs.stream.color)

    # Start streaming with requested config
    pipe.start(cfg)

    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         training=False)

    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
    else:
        print("[*] Cannot find ckpt from {}.".format(ckpt_path))
        exit()

    if FLAGS.update:
        print('Face bank updating...')
        targets, names = prepare_facebank(cfg, model)
        print('Face bank updated')
    else:
        targets, names = load_facebank(cfg)
        print('Face bank loaded')

    if FLAGS.save:
        video_writer = cv2.VideoWriter('./recording.avi',
                                       cv2.VideoWriter_fourcc(*'XVID'), 10,
                                       (640, 480))
        # frame rate 6 due to my laptop is quite slow...

    while True:

        frame = pipe.wait_for_frames()

        img = frame
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        bboxes, landmarks, faces = align_multi(
            cfg, img, min_confidence=FLAGS.min_confidence, limits=3)
        bboxes = bboxes.astype(int)
        embs = []
        for face in faces:
            if len(face.shape) == 3:
                face = np.expand_dims(face, 0)
            face = face.astype(np.float32) / 255.
            embs.append(l2_norm(model(face)).numpy())

        list_min_idx = []
        list_score = []
        for emb in embs:
            dist = [euclidean(emb, target) for target in targets]
            min_idx = np.argmin(dist)
            list_min_idx.append(min_idx)
            list_score.append(dist[int(min_idx)])
        list_min_idx = np.array(list_min_idx)
        list_score = np.array(list_score)
        list_min_idx[list_score > FLAGS.threshold] = -1
        for idx, box in enumerate(bboxes):
            frame = utils.draw_box_name(box, landmarks[idx],
                                        names[list_min_idx[idx] + 1], frame)
        frame = cv2.resize(frame, (640, 480))
        cv2.imshow('face Capture', frame)

        key = cv2.waitKey(1) & 0xFF
        if FLAGS.save:
            video_writer.write(frame)

        if key == ord('q'):
            break

    if FLAGS.save:
        video_writer.release()
    pipe.stop()
Ejemplo n.º 4
0
def main(_):
    # init
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    # define network
    model = RRDB_Model(cfg['input_size'], cfg['ch_size'], cfg['network_G'])
    model.summary(line_length=80)

    # load dataset
    train_dataset = load_dataset(cfg, 'train_dataset', shuffle=True)

    # define optimizer
    learning_rate = MultiStepLR(cfg['lr'], cfg['lr_steps'], cfg['lr_rate'])
    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate,
                                         beta_1=cfg['adam_beta1_G'],
                                         beta_2=cfg['adam_beta2_G'])

    # define losses function
    pixel_loss_fn = PixelLoss(criterion=cfg['pixel_criterion'])

    # load checkpoint
    checkpoint_dir = './checkpoints/' + cfg['sub_name']
    checkpoint = tf.train.Checkpoint(step=tf.Variable(0, name='step'),
                                     optimizer=optimizer,
                                     model=model)
    manager = tf.train.CheckpointManager(checkpoint=checkpoint,
                                         directory=checkpoint_dir,
                                         max_to_keep=3)
    if manager.latest_checkpoint:
        checkpoint.restore(manager.latest_checkpoint)
        print('[*] load ckpt from {} at step {}.'.format(
            manager.latest_checkpoint, checkpoint.step.numpy()))
    else:
        print("[*] training from scratch.")

    # define training step function
    @tf.function
    def train_step(lr, hr):
        with tf.GradientTape() as tape:
            sr = model(lr, training=True)

            losses = {}
            losses['reg'] = tf.reduce_sum(model.losses)
            losses['pixel'] = cfg['w_pixel'] * pixel_loss_fn(hr, sr)
            total_loss = tf.add_n([l for l in losses.values()])

        grads = tape.gradient(total_loss, model.trainable_variables)
        optimizer.apply_gradients(zip(grads, model.trainable_variables))

        return total_loss, losses

    # training loop
    summary_writer = tf.summary.create_file_writer('./logs/' + cfg['sub_name'])
    prog_bar = ProgressBar(cfg['niter'], checkpoint.step.numpy())
    remain_steps = max(cfg['niter'] - checkpoint.step.numpy(), 0)

    for lr, hr in train_dataset.take(remain_steps):
        checkpoint.step.assign_add(1)
        steps = checkpoint.step.numpy()

        total_loss, losses = train_step(lr, hr)

        prog_bar.update("loss={:.4f}, lr={:.1e}".format(
            total_loss.numpy(),
            optimizer.lr(steps).numpy()))

        if steps % 10 == 0:
            with summary_writer.as_default():
                tf.summary.scalar('loss/total_loss', total_loss, step=steps)
                for k, l in losses.items():
                    tf.summary.scalar('loss/{}'.format(k), l, step=steps)
                tf.summary.scalar('learning_rate',
                                  optimizer.lr(steps),
                                  step=steps)

        if steps % cfg['save_steps'] == 0:
            manager.save()
            print("\n[*] save ckpt file at {}".format(
                manager.latest_checkpoint))

    print("\n[*] training done!")
Ejemplo n.º 5
0
def main(_):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)

    cfg = load_yaml(FLAGS.cfg_path)

    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         num_classes=cfg['num_classes'],
                         head_type=cfg['head_type'],
                         embd_shape=cfg['embd_shape'],
                         training=True)
    model.summary(line_length=80)

    if cfg['train_dataset']:
        logging.info("load ms1m dataset.")
        dataset_len = cfg['num_samples']
        steps_per_epoch = dataset_len // cfg['batch_size']
        train_dataset = dataset.load_tfrecord_dataset(
            cfg['train_dataset'], cfg['batch_size'], cfg['binary_img'],
            is_ccrop=cfg['is_ccrop'])
    else:
        logging.info("load fake dataset.")
        dataset_len = 1
        steps_per_epoch = 1
        train_dataset = dataset.load_fake_dataset(cfg['input_size'])

    learning_rate = tf.constant(cfg['base_lr'])
    optimizer = tf.keras.optimizers.SGD(
        learning_rate=learning_rate, momentum=0.9, nesterov=True)
    loss_fn = SoftmaxLoss()

    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print('[*] load ckpt from {}'.format(ckpt_path))
        epochs, steps = get_ckpt_inf(ckpt_path)
        model.load_weights(ckpt_path)
    else:
        print('[*] training from scratch.')
        epochs = 1
        steps = 0

    if FLAGS.mode == 'eager_tf':
        # Eager mode is great for debugging
        # Non eager graph mode is recommended for real training
        summary_writer = tf.summary.create_file_writer(
            './logs/' + cfg['sub_name'])

        while epochs < cfg['epochs'] + 1:
            for inputs, labels in train_dataset:
                with tf.GradientTape() as tape:
                    logist = model(inputs, training=True)
                    reg_loss = tf.reduce_sum(model.losses) * cfg['w_decay']
                    pred_loss = loss_fn(labels, logist)
                    total_loss = pred_loss + reg_loss

                grads = tape.gradient(total_loss, model.trainable_variables)
                optimizer.apply_gradients(
                    zip(grads, model.trainable_variables))

                if steps % 5 == 0 and steps > 0:
                    verb_str = "Epoch {}/{}: {}/{}, loss={:.2f}, lr={:.4f}"
                    print(verb_str.format(epochs, cfg['epochs'],
                                          steps % steps_per_epoch,
                                          steps_per_epoch,
                                          total_loss.numpy(),
                                          learning_rate.numpy()))

                    with summary_writer.as_default():
                        tf.summary.scalar(
                            'loss/total loss', total_loss, step=steps)
                        tf.summary.scalar(
                            'loss/pred loss', pred_loss, step=steps)
                        tf.summary.scalar(
                            'loss/reg loss', reg_loss, step=steps)
                        tf.summary.scalar(
                            'learning rate', optimizer.lr, step=steps)

                if steps % 1000 == 0 and steps > 0:
                    print('[*] save ckpt file!')
                    ckpt_name = 'checkpoints/{}/e_{}_s_{}.ckpt'
                    model.save_weights(
                        ckpt_name.format(cfg['sub_name'], epochs, steps))

                steps += 1
            epochs += 1
    else:
        model.compile(optimizer=optimizer, loss=loss_fn,
                      run_eagerly=(FLAGS.mode == 'eager_fit'))

        mc_callback = ModelCheckpoint(
                'checkpoints/' + cfg['sub_name'] + '/e_{epoch}_s_{batch}.ckpt',
                save_freq=1000 * cfg['batch_size'] + 1, verbose=1,
                save_weights_only=True)
        tb_callback = TensorBoard(log_dir='logs/',
                                  update_freq=cfg['batch_size'] * 5,
                                  profile_batch=0)
        tb_callback._total_batches_seen = steps
        tb_callback._samples_seen = steps * cfg['batch_size']
        callbacks = [mc_callback, tb_callback]

        history = model.fit(train_dataset,
                            epochs=cfg['epochs'],
                            steps_per_epoch=steps_per_epoch,
                            callbacks=callbacks,
                            initial_epoch=epochs - 1)
Ejemplo n.º 6
0
def main():
    # with open('/home/anhdq23/Desktop/nguyen/VT_simulation/weights/arcface_ret50.json', 'r') as f:
    #     model_json = json.load(f)
    # model = model_from_json(model_json)
    # model.load_weights('/home/anhdq23/Desktop/nguyen/VT_simulation/weights/arcface_ret50.h5')

    cfg = load_yaml('./configs/arc_res50.yaml')
    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         num_classes=cfg['num_classes'],
                         head_type=cfg['head_type'],
                         embd_shape=cfg['embd_shape'],
                         w_decay=cfg['w_decay'],
                         training=False)
    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
    else:
        print("[*] training from scratch.")

    resnet_model = tf.keras.Model(inputs=model.get_layer('resnet50').input,
                                    outputs=model.get_layer('resnet50').output)
    resnet_head = tf.keras.Model(inputs=resnet_model.input,
                                    outputs=resnet_model.get_layer('conv2_block1_add').output)
    resnet_tail = split(resnet_model, 18, 1000) # conv2_block1_out
    output_model = tf.keras.Model(inputs=model.get_layer('OutputLayer').input,
                                    outputs=model.get_layer('OutputLayer').output)

    temp1 = np.ones((1,17,28,256))
    temp2 = np.zeros((1,11,28,256))
    masked = np.concatenate([temp1, temp2], axis =1)

    path_to_data = '/home/anhdq23/Desktop/nguyen/data/AR/test2'
    anchor_names = get_probe_pr2(path_to_data) # From 1 to 100
    name_dicts = get_probe_pr2(path_to_data) # Dictionary: {anchor_name:[name_image, ...]}

    dist_all = []
    labels_ARface = []

    # Calculate label for protocol2
    for subject in np.arange(100):
        tmp = np.zeros((600,))
        tmp[subject*6: subject*6+6] += 1
        labels_ARface.extend(list(tmp))
    labels_ARface = np.array(labels_ARface)

    # Extract featre with padding
    for anchor_name in anchor_names:
        start = time.time()
        for key in list(name_dicts.keys()):
            print(key)
            for name in name_dicts[key]:
                img1 = Image.open(os.path.join(path_to_data, anchor_name))
                img1 = expand2square(img1, (255, 255, 255))
                img1 = img1.resize((112, 112))
                img1 = np.array(img1)/255.0

                img2 = Image.open(os.path.join(path_to_data, name)).resize((112, 112))
                img2 = expand2square(img2, (255, 255, 255))
                img2 = img2.resize((112, 112))
                img2 = np.array(img2)/255.0

                fc1 = resnet_head.predict(img1.reshape((1,112,112,3)))
                fc1 = np.multiply(fc1, masked)
                fc1 = resnet_tail.predict(fc1)
                fc1 = output_model.predict(fc1)
                norm_fc1 = preprocessing.normalize(fc1.reshape((1,512)), norm='l2', axis=1)

                fc2 = resnet_head.predict(img2.reshape((1,112,112,3)))
                fc2 = np.multiply(fc2, masked)
                fc2 = resnet_tail.predict(fc2)
                fc2 = output_model.predict(fc2)
                norm_fc2 = preprocessing.normalize(fc2.reshape((1,512)), norm='l2', axis=1)

                diff = np.subtract(norm_fc1, norm_fc2)
                dist = np.sqrt(np.sum(np.square(diff), 1))/2
                print(dist, anchor_name, name)

                dist_all.extend(dist)
        end = time.time()
        print(end - start)
    
   
    plt.plot(dist_all)
    plt.show()
    thresholds = np.arange(0, 1, 0.01)
    tpr_all = []
    fpr_all = []
    for thr in thresholds:
        tpr, fpr, acc, f1 = calculate_accuracy(thr, np.array(dist_all), labels_ARface)
        top_left = np.sqrt((1-tpr)**2 + fpr**2)
        print('thr %.4f' % thr , 'tpr %.4f' % tpr, 'fpr %.4f' % fpr, \
        'top left %.4f' % top_left, 'acc %.4f' % acc, 'f1_score %.4f'%f1)
        # top_left_batch.append(top_left)
        tpr_all.append(tpr)
        fpr_all.append(fpr)

    for threshold in thresholds:
        predict_issame = np.less(np.array(dist_all), threshold)
        conf_matrix = confusion_matrix(labels_ARface, predict_issame)
        print(conf_matrix)

    plt.figure()
    lw = 2
    plt.plot(fpr_all, tpr_all, color='darkorange',
            lw=lw, label='ROC curve')
    plt.xlim([0.0, 1.])
    plt.ylim([0.0, 1.])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic')
    plt.legend(loc="lower right")
    plt.show()
Ejemplo n.º 7
0
def main(_):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)
    model = Modelembedding(
        size=cfg['input_size'],
        embd_shape=cfg['embd_shape'],
        backbone_type=cfg['backbone_type'],
        training=
        True,  # here equal false, just get the model without acrHead, to load the model trained by arcface
        cfg=cfg)

    cifar = Cifar(cfg['batch_size'])
    train_dataset = cifar.build_training_data()
    val_dataset = cifar.build_validation_data()
    dataset_len = cfg['num_samples']
    steps_per_epoch = dataset_len // cfg['batch_size']

    learning_rate = tf.constant(cfg['base_lr'])
    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
    # optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)
    # optimiser = tf.train.MomentumOptimizer(learning_rate,momentum=0.9, )
    for x in model.trainable_weights:
        print("trainable:", x.name)
    print('\n')
    model.summary(line_length=80)

    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
        epochs, steps = get_ckpt_inf(ckpt_path, steps_per_epoch)
    else:
        print("[*] training from scratch.")
        epochs, steps = 1, 1

    if FLAGS.mode == 'eager_tf':
        # Eager mode is great for debugging
        # Non eager graph mode is recommended for real training
        summary_writer = tf.summary.create_file_writer('./logs/' +
                                                       cfg['sub_name'])

        train_dataset = iter(train_dataset)
        marginloss = MarginLossLayer2(cfg=cfg)

        while epochs <= cfg['epochs']:
            if steps % 5 == 0:
                start = time.time()

            inputs, labels = next(
                train_dataset)  #print(inputs[0][1][:])  labels[2][:]

            with tf.GradientTape() as tape:
                logist = model((inputs, labels), training=True)
                reg_loss = tf.reduce_sum(model.losses)
                pred_loss = marginloss(logist, labels)
                # logist = tf.cast(logist,tf.double)

                total_loss = reg_loss + pred_loss
            grads = tape.gradient(total_loss, model.trainable_variables)
            optimizer.apply_gradients(zip(grads, model.trainable_variables))

            if steps % 5 == 0:
                end = time.time()
                verb_str = "Epoch {}/{}: {}/{}, loss={:.2f}, lr={:.4f}, time per step={:.2f}s, remaining time 4 this epoch={:.2f}min"
                print(
                    verb_str.format(epochs, cfg['epochs'],
                                    steps % steps_per_epoch, steps_per_epoch,
                                    total_loss.numpy(), learning_rate.numpy(),
                                    end - start, (steps_per_epoch -
                                                  (steps % steps_per_epoch)) *
                                    (end - start) / 60.0))

                with summary_writer.as_default():
                    tf.summary.scalar('loss/total loss',
                                      total_loss,
                                      step=steps)
                    tf.summary.scalar('loss/pred loss', pred_loss, step=steps)
                    tf.summary.scalar('loss/reg loss', reg_loss, step=steps)
                    tf.summary.scalar('learning rate',
                                      optimizer.lr,
                                      step=steps)

            if steps % cfg['save_steps'] == 0:
                print('[*] save ckpt file!')
                model.save_weights('checkpoints/{}/e_{}_b_{}.ckpt'.format(
                    cfg['sub_name'], epochs, steps % steps_per_epoch))

            steps += 1
            epochs = steps // steps_per_epoch + 1
    else:
        print("[*] only support eager_tf!")
        marginloss = MarginLossLayer2(cfg=cfg)
        model.compile(optimizer=optimizer, loss=marginloss)
        mc_callback = ModelCheckpoint(
            'checkpoints/' + cfg['sub_name'] + '/e_{epoch}_b_{batch}.ckpt',
            save_freq=cfg['save_steps'] * cfg['batch_size'],
            verbose=1,
            save_weights_only=True)
        tb_callback = TensorBoard(log_dir='logs/' + cfg['sub_name'],
                                  update_freq=cfg['batch_size'] * 5,
                                  profile_batch=0)
        tb_callback._total_batches_seen = steps
        tb_callback._samples_seen = steps * cfg['batch_size']
        callbacks = [mc_callback, tb_callback]

        def batch_generator(train_dataset):
            train_dataset = iter(train_dataset)
            while True:
                inputs, labels = next(
                    train_dataset)  #print(inputs[0][1][:])  labels[2][:]
                yield [inputs, labels]

        model.fit_generator(batch_generator(train_dataset),
                            epochs=cfg['epochs'],
                            steps_per_epoch=steps_per_epoch,
                            callbacks=callbacks,
                            initial_epoch=epochs - 1)

    print("[*] training done!")
Ejemplo n.º 8
0
def test_model_selector_creation(config: dict, problem_type: str):
    path = config.get('MODEL_SELECTOR_CONFIG_DIR')
    path = os.path.join(path, f'{problem_type}.yml')
    model_selector_config = load_yaml(path=path)

    assert ModelSelector(config=model_selector_config)
Ejemplo n.º 9
0
        if hvd.rank() == 0:
            manager.save()
            print("\n[*] training done! save ckpt file at {}".format(
                manager.latest_checkpoint))
    else:
        manager.save()
        print("\n[*] training done! save ckpt file at {}".format(
            manager.latest_checkpoint))


def get_args():
  parser = argparse.ArgumentParser(
      description='RetinaFace train')
  parser.add_argument('--dataset_root', required=True, help='Dataset path', type=str)
  parser.add_argument('--output_path', required=True, help='Output path', type=str)
  parser.add_argument('--cfg_path', required=True, help='Config file path', type=str)
  args = parser.parse_args()

  return args


if __name__ == '__main__':
  args = get_args()
  cfg = load_yaml(args.cfg_path)
  cfg['dataset_root'] = args.dataset_root
  cfg['output_path'] = args.output_path

  train_retinaface(cfg)


Ejemplo n.º 10
0
def main(_):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    if FLAGS.v_tf == '2':
        set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         num_classes=cfg['num_classes'],
                         head_type=cfg['head_type'],
                         embd_shape=cfg['embd_shape'],
                         w_decay=cfg['w_decay'],
                         training=True,
                         version_tensorflow=FLAGS.v_tf)
    model.summary(line_length=80)

    if cfg['train_dataset']:
        logging.info("load ms1m dataset.")
        dataset_len = cfg['num_samples']
        steps_per_epoch = dataset_len // cfg['batch_size']
        train_dataset = dataset.load_tfrecord_dataset(cfg['train_dataset'],
                                                      cfg['batch_size'],
                                                      cfg['binary_img'],
                                                      is_ccrop=cfg['is_ccrop'])
    else:
        logging.info("load fake dataset.")
        steps_per_epoch = 1
        train_dataset = dataset.load_fake_dataset(cfg['input_size'])

    ckpt_path = tf.train.latest_checkpoint(cfg['checkpoints_dir'] +
                                           cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
        epochs, steps = get_ckpt_inf(ckpt_path, steps_per_epoch)
    else:
        print("[*] training from scratch.")
        epochs, steps = 1, 1

    if FLAGS.mode == 'eager_tf':

        learning_rate = 0.1

        optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate,
                                            decay=1e-4,
                                            momentum=0.9,
                                            nesterov=True)
        loss_fn = SoftmaxLoss()

        # Eager mode is great for debugging
        # Non eager graph mode is recommended for real training

        summary_writer = tf.summary.create_file_writer(cfg['logs_dir'] +
                                                       cfg['sub_name'])

        train_dataset = iter(train_dataset)

        while epochs <= cfg['epochs']:
            inputs, labels = next(train_dataset)

            with tf.GradientTape() as tape:
                logist = model(inputs, training=True)
                reg_loss = tf.reduce_sum(model.losses)
                pred_loss = loss_fn(labels, logist)
                total_loss = pred_loss + reg_loss

            grads = tape.gradient(total_loss, model.trainable_variables)
            optimizer.apply_gradients(zip(grads, model.trainable_variables))

            if steps % 5 == 0:
                verb_str = "Epoch {}/{}: {}/{}, loss={:.2f}, lr={:.2f}"
                print(
                    verb_str.format(epochs, cfg['epochs'],
                                    steps % steps_per_epoch, steps_per_epoch,
                                    total_loss.numpy(), optimizer.lr.numpy()))

                with summary_writer.as_default():
                    tf.summary.scalar('loss/total loss',
                                      total_loss,
                                      step=steps)
                    tf.summary.scalar('loss/pred loss', pred_loss, step=steps)
                    tf.summary.scalar('loss/reg loss', reg_loss, step=steps)
                    tf.summary.scalar('learning rate',
                                      optimizer.lr,
                                      step=steps)

            if steps % cfg['save_steps'] == 0:
                print('[*] save ckpt file!')
                model.save_weights('{}{}/e_{}_b_{}.ckpt'.format(
                    cfg['checkpoints_dir'], cfg['sub_name'], epochs,
                    steps % steps_per_epoch))

            steps += 1
            epochs = steps // steps_per_epoch + 1

    else:

        learning_rate = 0.1

        optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate,
                                            decay=1e-4,
                                            momentum=0.9,
                                            nesterov=True)
        loss_fn = SoftmaxLoss()

        model.compile(optimizer=optimizer, loss=loss_fn)

        mc_callback = ModelCheckpoint(
            cfg['checkpoints_dir'] + cfg['sub_name'] +
            '/e_{epoch}_b_{batch}.ckpt',
            save_freq=cfg['save_steps'] * cfg['batch_size'],
            verbose=1,
            save_weights_only=True)
        tb_callback = TensorBoard(log_dir=cfg['logs_dir'] + cfg['sub_name'],
                                  update_freq=cfg['batch_size'] * 5,
                                  profile_batch=0)
        tb_callback._total_batches_seen = steps
        tb_callback._samples_seen = steps * cfg['batch_size']
        #lrate = LearningRateScheduler(step_decay)
        #callbacks = [mc_callback, tb_callback, lrate]

        callbacks = [mc_callback, tb_callback]

        model.fit(train_dataset,
                  epochs=cfg['epochs'],
                  steps_per_epoch=steps_per_epoch,
                  callbacks=callbacks,
                  initial_epoch=epochs - 1)

    print("[*] training done!")
Ejemplo n.º 11
0
def main(_):
    # init
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    # define network
    sna = SearchNetArch(cfg)
    sna.model.summary(line_length=80)
    print("param size = {:f}MB".format(count_parameters_in_MB(sna.model)))

    # load dataset
    t_split = f"train[0%:{int(cfg['train_portion'] * 100)}%]"
    v_split = f"train[{int(cfg['train_portion'] * 100)}%:100%]"
    train_dataset = load_cifar10_dataset(
        cfg['batch_size'],
        split=t_split,
        shuffle=True,
        drop_remainder=True,
        using_normalize=cfg['using_normalize'],
        using_crop=cfg['using_crop'],
        using_flip=cfg['using_flip'],
        using_cutout=cfg['using_cutout'],
        cutout_length=cfg['cutout_length'])
    val_dataset = load_cifar10_dataset(cfg['batch_size'],
                                       split=v_split,
                                       shuffle=True,
                                       drop_remainder=True,
                                       using_normalize=cfg['using_normalize'],
                                       using_crop=cfg['using_crop'],
                                       using_flip=cfg['using_flip'],
                                       using_cutout=cfg['using_cutout'],
                                       cutout_length=cfg['cutout_length'])

    # define optimizer
    steps_per_epoch = int(cfg['dataset_len'] * cfg['train_portion'] //
                          cfg['batch_size'])
    learning_rate = CosineAnnealingLR(initial_learning_rate=cfg['init_lr'],
                                      t_period=cfg['epoch'] * steps_per_epoch,
                                      lr_min=cfg['lr_min'])
    optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate,
                                        momentum=cfg['momentum'])
    optimizer_arch = tf.keras.optimizers.Adam(
        learning_rate=cfg['arch_learning_rate'], beta_1=0.5, beta_2=0.999)

    # define losses function
    criterion = CrossEntropyLoss()

    # load checkpoint
    checkpoint_dir = './checkpoints/' + cfg['sub_name']
    checkpoint = tf.train.Checkpoint(step=tf.Variable(0, name='step'),
                                     optimizer=optimizer,
                                     optimizer_arch=optimizer_arch,
                                     model=sna.model,
                                     alphas_normal=sna.alphas_normal,
                                     alphas_reduce=sna.alphas_reduce,
                                     betas_normal=sna.betas_normal,
                                     betas_reduce=sna.betas_reduce)
    manager = tf.train.CheckpointManager(checkpoint=checkpoint,
                                         directory=checkpoint_dir,
                                         max_to_keep=3)
    if manager.latest_checkpoint:
        checkpoint.restore(manager.latest_checkpoint)
        print('[*] load ckpt from {} at step {}.'.format(
            manager.latest_checkpoint, checkpoint.step.numpy()))
    else:
        print("[*] training from scratch.")
    print(f"[*] searching model after {cfg['start_search_epoch']} epochs.")

    # define training step function for model
    @tf.function
    def train_step(inputs, labels):
        with tf.GradientTape() as tape:
            logits = sna.model((inputs, *sna.arch_parameters), training=True)

            losses = {}
            losses['reg'] = tf.reduce_sum(sna.model.losses)
            losses['ce'] = criterion(labels, logits)
            total_loss = tf.add_n([l for l in losses.values()])

        grads = tape.gradient(total_loss, sna.model.trainable_variables)
        grads = [(tf.clip_by_norm(grad, cfg['grad_clip'])) for grad in grads]
        optimizer.apply_gradients(zip(grads, sna.model.trainable_variables))

        return logits, total_loss, losses

    # define training step function for arch_parameters
    @tf.function
    def train_step_arch(inputs, labels):
        with tf.GradientTape() as tape:
            logits = sna.model((inputs, *sna.arch_parameters), training=True)

            losses = {}
            losses['reg'] = cfg['arch_weight_decay'] * tf.add_n(
                [tf.reduce_sum(p**2) for p in sna.arch_parameters])
            losses['ce'] = criterion(labels, logits)
            total_loss = tf.add_n([l for l in losses.values()])

        grads = tape.gradient(total_loss, sna.arch_parameters)
        optimizer_arch.apply_gradients(zip(grads, sna.arch_parameters))

        return losses

    # training loop
    summary_writer = tf.summary.create_file_writer('./logs/' + cfg['sub_name'])
    total_steps = steps_per_epoch * cfg['epoch']
    remain_steps = max(total_steps - checkpoint.step.numpy(), 0)
    prog_bar = ProgressBar(steps_per_epoch,
                           checkpoint.step.numpy() % steps_per_epoch)

    train_acc = AvgrageMeter()
    for inputs, labels in train_dataset.take(remain_steps):
        checkpoint.step.assign_add(1)
        steps = checkpoint.step.numpy()
        epochs = ((steps - 1) // steps_per_epoch) + 1

        if epochs > cfg['start_search_epoch']:
            inputs_val, labels_val = next(iter(val_dataset))
            arch_losses = train_step_arch(inputs_val, labels_val)

        logits, total_loss, losses = train_step(inputs, labels)
        train_acc.update(
            accuracy(logits.numpy(), labels.numpy())[0], cfg['batch_size'])

        prog_bar.update(
            "epoch={:d}/{:d}, loss={:.4f}, acc={:.2f}, lr={:.2e}".format(
                epochs, cfg['epoch'], total_loss.numpy(), train_acc.avg,
                optimizer.lr(steps).numpy()))

        if steps % 10 == 0:
            with summary_writer.as_default():
                tf.summary.scalar('acc/train', train_acc.avg, step=steps)

                tf.summary.scalar('loss/total_loss', total_loss, step=steps)
                for k, l in losses.items():
                    tf.summary.scalar('loss/{}'.format(k), l, step=steps)
                tf.summary.scalar('learning_rate',
                                  optimizer.lr(steps),
                                  step=steps)

                if epochs > cfg['start_search_epoch']:
                    for k, l in arch_losses.items():
                        tf.summary.scalar('arch_losses/{}'.format(k),
                                          l,
                                          step=steps)
                    tf.summary.scalar('arch_learning_rate',
                                      cfg['arch_learning_rate'],
                                      step=steps)

        if steps % cfg['save_steps'] == 0:
            manager.save()
            print("\n[*] save ckpt file at {}".format(
                manager.latest_checkpoint))

        if steps % steps_per_epoch == 0:
            train_acc.reset()
            if epochs > cfg['start_search_epoch']:
                genotype = sna.get_genotype()
                print(f"\nsearch arch: {genotype}")
                f = open(
                    os.path.join('./logs', cfg['sub_name'],
                                 'search_arch_genotype.py'), 'a')
                f.write(f"\n{cfg['sub_name']}_{epochs} = {genotype}\n")
                f.close()

    manager.save()
    print("\n[*] training done! save ckpt file at {}".format(
        manager.latest_checkpoint))
Ejemplo n.º 12
0
from modules.trainer import CustomTrainer
from modules.utils import load_yaml, save_yaml, get_logger, make_directory

iou_thres = 0.75
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print("The following GPU devices are available: %s" % device)

# DEBUG
DEBUG = False

# CONFIG
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_PROJECT_DIR = os.path.dirname(PROJECT_DIR)
DATA_DIR = os.path.join(PROJECT_DIR, 'data')
TRAIN_CONFIG_PATH = os.path.join(PROJECT_DIR, 'config/train_config.yaml')
config = load_yaml(TRAIN_CONFIG_PATH)

# SEED
RANDOM_SEED = config['SEED']['random_seed']

# DATALOADER
NUM_WORKERS = config['DATALOADER']['num_workers']
CHECKPOINT_PATH = ''
PIN_MEMORY = config['DATALOADER']['pin_memory']

# TRAIN
EPOCHS = config['TRAIN']['num_epochs']
TRAIN_BATCH_SIZE = config['TRAIN']['batch_size']
MODEL = config['TRAIN']['model']
LEARNING_RATE = config['TRAIN']['learning_rate']
EARLY_STOPPING_PATIENCE = config['TRAIN']['early_stopping_patience']
Ejemplo n.º 13
0
def main(_argv):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         training=False)

    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
    else:
        print("[*] Cannot find ckpt from {}.".format(ckpt_path))
        exit()

    if FLAGS.img_path:
        print("[*] Encode {} to ./output_embeds.npy".format(FLAGS.img_path))
        img = cv2.imread(FLAGS.img_path)
        img = cv2.resize(img, (cfg['input_size'], cfg['input_size']))
        img = img.astype(np.float32) / 255.
        if len(img.shape) == 3:
            img = np.expand_dims(img, 0)
        embeds = l2_norm(model(img))
        np.save('./output_embeds.npy', embeds)
    else:
        print("[*] Loading LFW, AgeDB30 and CFP-FP...")
        lfw, agedb_30, cfp_fp, lfw_issame, agedb_30_issame, cfp_fp_issame = \
            get_val_data(cfg['test_dataset'])

        print("[*] Perform Evaluation on LFW...")
        acc_lfw, best_th = perform_val(cfg['embd_shape'],
                                       cfg['batch_size'],
                                       model,
                                       lfw,
                                       lfw_issame,
                                       is_ccrop=cfg['is_ccrop'])
        print("    acc {:.4f}, th: {:.2f}".format(acc_lfw, best_th))

        print("[*] Perform Evaluation on AgeDB30...")
        acc_agedb30, best_th = perform_val(cfg['embd_shape'],
                                           cfg['batch_size'],
                                           model,
                                           agedb_30,
                                           agedb_30_issame,
                                           is_ccrop=cfg['is_ccrop'])
        print("    acc {:.4f}, th: {:.2f}".format(acc_agedb30, best_th))

        print("[*] Perform Evaluation on CFP-FP...")
        acc_cfp_fp, best_th = perform_val(cfg['embd_shape'],
                                          cfg['batch_size'],
                                          model,
                                          cfp_fp,
                                          cfp_fp_issame,
                                          is_ccrop=cfg['is_ccrop'])
        print("    acc {:.4f}, th: {:.2f}".format(acc_cfp_fp, best_th))
Ejemplo n.º 14
0
def main(_argv):

    # FUNCTIONS FOR CROPPING
    #####################################################################################################
    def bounding_box(img, ann, img_height, img_width):
        x1, y1, x2, y2 = int(ann[0] * img_width), int(ann[1] * img_height), \
                         int(ann[2] * img_width), int(ann[3] * img_height)
        return x1, y1, x2, y2

    def calc_points(x, y, side):
        return int(x - side / 2), int(x +
                                      side / 2), int(y -
                                                     side / 2), int(y +
                                                                    side / 2)

    def adjust_points(x_center, y_center, original_longest, scaling_factor,
                      min_scaling_factor):
        factors = np.arange(scaling_factor, min_scaling_factor - 0.04, -0.05)
        for factor in factors:
            # calculate nex points
            x1, x2, y1, y2 = calc_points(x_center, y_center,
                                         int(original_longest * factor))

            for i in range(FLAGS.max_iter):
                if x1 < 0:
                    x2 -= x1
                    x1 = 0
                if y1 < 0:
                    y2 -= y1
                    y1 = 0
                if x2 > img_raw.shape[1]:
                    x1 -= x2
                    x2 = img_raw.shape[1]
                if y2 > img_raw.shape[0]:
                    y1 -= y2
                    y2 = img_raw.shape[0]

                if x1 >= 0 and y1 >= 0 and x2 <= img_raw.shape[
                        1] and y2 <= img_raw.shape[0]:
                    return x1, x2, y1, y2, True

        print("Not cropping", img_path,
              "due to a problem with a cropping square box")
        return x1, x2, y1, y2, False

    def get_dim(lst):
        return [(lst[3] - lst[1]) * (lst[2] - lst[0])]

    def get_max(outputs, lst):
        area = [i[0] for i in lst]
        prob = [i[1] for i in lst]
        max_area_index = set([i for i, j in enumerate(area) if j == max(area)])
        max_prob_index = set([i for i, j in enumerate(prob) if j == max(prob)])
        indecies = list(max_area_index.intersection(max_prob_index))
        if len(indecies) >= 1: return [outputs[indecies[0]]]
        elif len(indecies
                 ) == 0:  # if there is a mismatch, return the largest element
            if len(list(max_area_index)) >= 1:
                return [outputs[list(max_area_index)[0]]]
            else:  # precautionary because there should always be at least one face
                print("Not cropping", img_path,
                      "due to a problem with returning the largest element")
                return []

    #####################################################################################################

    # MODEL
    #####################################################################################################
    # initialisation
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu
    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()
    cfg = load_yaml(FLAGS.cfg_path)

    # define network
    model = RetinaFaceModel(cfg,
                            training=False,
                            iou_th=FLAGS.iou_th,
                            score_th=FLAGS.score_th)

    # load checkpoint
    checkpoint_dir = './checkpoints/' + cfg['sub_name']
    checkpoint = tf.train.Checkpoint(model=model)
    if tf.train.latest_checkpoint(checkpoint_dir):
        checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
        print("[*] load ckpt from {}.".format(
            tf.train.latest_checkpoint(checkpoint_dir)))
    else:
        print("[*] Cannot find ckpt from {}.".format(checkpoint_dir))
        exit()
    #####################################################################################################

    # CROPPING
    #####################################################################################################
    # check if the path exits
    if not os.path.exists(FLAGS.path):
        print(f"cannot find the specified path from {FLAGS.path}")
        exit()

    # make a corresponding directory
    try:
        os.mkdir(FLAGS.path.replace("images", "cropped_images"))
    except FileExistsError:
        print(FLAGS.path.replace("images", "cropped_images"), "already exists")

    # eget subdirectories within the specified folder
    subdirectories = [FLAGS.path+'/'+i for i in os.listdir(FLAGS.path) \
                      if os.path.isdir(FLAGS.path+'/'+i)]

    # loop through each folder
    for subdir in sorted(subdirectories):

        # create corresponding folders for cropped data and get all images in a given folder
        if 'original' in subdir: x = 3
        else: x = 7

        try:
            os.mkdir(subdir.replace("images", "cropped_images"))
            images_lst = glob.glob(subdir + "/*.png")
            cropped_images_lst = []
            print(subdir[len(subdir) - x:len(subdir)])

        except FileExistsError:
            # count number of existing images in this subdirectory, if same as original, skip
            images_lst = glob.glob(subdir + "/*.png")
            cropped_images_lst = glob.glob(
                subdir.replace("images", "cropped_images") + "/*.png")
            cropped_images_lst = [
                e[len(e) - 8:len(e)] for e in cropped_images_lst
            ]

            if len(images_lst) == len(cropped_images_lst):
                print(subdir[len(subdir) - x:len(subdir)],
                      "has already been generated")
                continue
            else:
                print(subdir[len(subdir) - x:len(subdir)])

        # loop through each image in a given folder
        for img_path in sorted(images_lst):

            if img_path[len(img_path) - 8:len(img_path)] in cropped_images_lst:
                continue

            img_raw = cv2.imread(img_path)
            img_height_raw, img_width_raw, _ = img_raw.shape
            img = np.float32(img_raw.copy())
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            # pad input image (unmatched shape problem), run model, recover padding effect
            img, pad_params = pad_input_image(img, max_steps=max(cfg['steps']))
            outputs = model(img[np.newaxis, ...]).numpy()
            outputs = recover_pad_output(outputs, pad_params)

            # get rid of elements which are faces with less that threshold probability
            outputs = [i for i in outputs if i[15] >= FLAGS.threshold_prob]

            # flag any images which have no recognised faces in them
            if len(outputs) == 0:
                print("no faces detected for", img_path)

                # if more than one face detected, select the largest and most definite
            elif len(outputs) > 1:
                f = [list(bounding_box(img_raw, i[0:4], img_height_raw, img_width_raw)) + [i[15]] \
                           for i in outputs]
                f = [get_dim(i[0:4]) + [i[4]] for i in f]
                outputs = get_max(outputs, f)

            # keeping as a loop in case we decide to use multiple faces per frame in the future
            # get cropping coordinates and save results
            for prior_index in range(len(outputs)):
                # get the bounding box coordinates
                bb_x1, bb_y1, bb_x2, bb_y2 = bounding_box(
                    img_raw, outputs[prior_index], img_height_raw,
                    img_width_raw)
                # scale up the magnitude of the longest side
                original_longest = int(max(bb_x2 - bb_x1, bb_y2 - bb_y1))
                longest = int(original_longest * FLAGS.scaling_factor)
                x_center = int((bb_x1 + bb_x2) / 2)
                y_center = int((bb_y1 + bb_y2) / 2)

                x1, x2, y1, y2, save_image = adjust_points(
                    x_center, y_center, original_longest, FLAGS.scaling_factor,
                    FLAGS.min_scaling_factor)

                if save_image:
                    try:
                        save_img_path = os.path.join(subdir.replace("images", "cropped_images") \
                            + "/" + img_path.replace(subdir + '/', ''))
                        cv2.imwrite(save_img_path, img_raw[y1:y2, x1:x2])

                    except:
                        print(img_path, "is not cropped for unknown reasons")
Ejemplo n.º 15
0
def convert_to_tflite(saved_model_dir, output_path, ckpt_path, config):
    if not Path(saved_model_dir).joinpath("saved_model.pb").exists() and ckpt_path is not None:
        export_to_saved_model(ckpt_path, saved_model_dir, config, image_size=320)

    converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
    converter.allow_custom_ops = True
    tflite_model = converter.convert()
    with open(output_path, "wb") as fh:
        fh.write(tflite_model)

    return tflite_model


if __name__ == "__main__":
    config = load_yaml("configs/retinaface_mbv2.yaml")
    tflite_model = convert_to_tflite(
        "saved_models/retinaface_mobile-v2_end2end_fixed-shape",
        "retinaface_mobile-v2.tflite",
        "checkpoints/retinaface_mbv2/ckpt-81",
        config,
    )

    import cv2
    import numpy as np

    # interpreter = tf.lite.Interpreter(model_path="retinaface_mobile-v2.tflite")
    interpreter = tf.lite.Interpreter(model_content=tflite_model)
    interpreter.allocate_tensors()

    input_details = interpreter.get_input_details()
Ejemplo n.º 16
0
def main(_):
    # init
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    # define network
    if cfg['network_G']['name']=='RRDB':    # ESRGAN 4x
        model = RRDB_Model(None, cfg['ch_size'], cfg['network_G'])
    elif cfg['network_G']['name']=='RRDB_CIPLAB':
        model = RRDB_Model_16x(None, cfg['ch_size'], cfg['network_G'])
    elif cfg['network_G']['name']=='RFB_ESRGAN':
        model = RFB_Model_16x(None, cfg['ch_size'], cfg['network_G'])
    model.summary(line_length=80)

    # load dataset
    train_dataset = load_dataset(cfg, 'train_dataset', shuffle=True)
    set5_dataset = load_val_dataset(cfg, 'set5')
    set14_dataset = load_val_dataset(cfg, 'set14')
    if 'DIV8K' in cfg['test_dataset']:
        DIV8K_val = load_val_dataset(cfg, 'DIV8K', crop_centor=cfg['test_dataset']['DIV8K_crop_centor'])
    # define optimizer
    learning_rate = MultiStepLR(cfg['lr'], cfg['lr_steps'], cfg['lr_rate'])
    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate,
                                         beta_1=cfg['adam_beta1_G'],
                                         beta_2=cfg['adam_beta2_G'])

    # define losses function
    if cfg['cycle_mse']:
        pixel_loss_fn = PixelLossDown(criterion=cfg['pixel_criterion'], scale=cfg['scale'])
    else:
        pixel_loss_fn = PixelLoss(criterion=cfg['pixel_criterion'])
    # load checkpoint
    checkpoint_dir = cfg['log_dir'] + '/checkpoints'
    checkpoint = tf.train.Checkpoint(step=tf.Variable(0, name='step'),
                                     optimizer=optimizer,
                                     model=model)
    manager = tf.train.CheckpointManager(checkpoint=checkpoint,
                                         directory=checkpoint_dir,
                                         max_to_keep=3)
    if manager.latest_checkpoint:
        checkpoint.restore(manager.latest_checkpoint)
        print('[*] load ckpt from {} at step {}.'.format(
            manager.latest_checkpoint, checkpoint.step.numpy()))
    else:
        print("[*] training from scratch.")

    # define training step function
    @tf.function
    def train_step(lr, hr):
        with tf.GradientTape() as tape:
            sr = model(lr, training=True)

            losses = {}
            losses['reg'] = tf.reduce_sum(model.losses)
            losses['pixel'] = cfg['w_pixel'] * pixel_loss_fn(hr, sr)
            total_loss = tf.add_n([l for l in losses.values()])

        grads = tape.gradient(total_loss, model.trainable_variables)
        optimizer.apply_gradients(zip(grads, model.trainable_variables))

        return total_loss, losses

    # training loop
    summary_writer = tf.summary.create_file_writer(cfg['log_dir']+'/logs')
    prog_bar = ProgressBar(cfg['niter'], checkpoint.step.numpy())
    remain_steps = max(cfg['niter'] - checkpoint.step.numpy(), 0)

    for _ in range(remain_steps):
        lr, hr = train_dataset()

        checkpoint.step.assign_add(1)
        steps = checkpoint.step.numpy()

        total_loss, losses = train_step(lr, hr)

        prog_bar.update("loss={:.4f}, lr={:.1e}".format(
            total_loss.numpy(), optimizer.lr(steps).numpy()))

        if steps % 10 == 0:
            with summary_writer.as_default():
                tf.summary.scalar(
                    'loss/total_loss', total_loss, step=steps)
                for k, l in losses.items():
                    tf.summary.scalar('loss/{}'.format(k), l, step=steps)
                tf.summary.scalar(
                    'learning_rate', optimizer.lr(steps), step=steps)

        if steps % cfg['save_steps'] == 0:
            manager.save()
            print("\n[*] save ckpt file at {}".format(
                manager.latest_checkpoint))

            # log results on test data
            set5_logs = evaluate_dataset(set5_dataset, model, cfg)
            set14_logs = evaluate_dataset(set14_dataset, model, cfg)
            if 'DIV8K' in cfg['test_dataset']:
                DIV8K_logs = evaluate_dataset(DIV8K_val, model, cfg)

            with summary_writer.as_default():
                if cfg['logging']['psnr']:
                    tf.summary.scalar('set5/psnr', set5_logs['psnr'], step=steps)
                    tf.summary.scalar('set14/psnr', set14_logs['psnr'], step=steps)
                    if 'DIV8K' in cfg['test_dataset']:
                        tf.summary.scalar('DIV8K/psnr', DIV8K_logs['psnr'], step=steps)

                if cfg['logging']['ssim']:
                    tf.summary.scalar('set5/ssim', set5_logs['ssim'], step=steps)
                    tf.summary.scalar('set14/ssim', set14_logs['ssim'], step=steps)
                    if 'DIV8K' in cfg['test_dataset']:
                        tf.summary.scalar('DIV8K/psnr', DIV8K_logs['psnr'], step=steps)

                if cfg['logging']['lpips']:
                    tf.summary.scalar('set5/lpips', set5_logs['lpips'], step=steps)
                    tf.summary.scalar('set14/lpips', set14_logs['lpips'], step=steps)
                    if 'DIV8K' in cfg['test_dataset']:
                        tf.summary.scalar('DIV8K/lpips', DIV8K_logs['lpips'], step=steps)

                if cfg['logging']['plot_samples']:
                    tf.summary.image("set5/samples", [set5_logs['samples']], step=steps)
                    tf.summary.image("set14/samples", [set14_logs['samples']], step=steps)
                    if 'DIV8K' in cfg['test_dataset']:
                        tf.summary.image("DIV8K/samples", [DIV8K_logs['samples']], step=steps)

    print("\n[*] training done!")
Ejemplo n.º 17
0
import time
import sys

from modules.models import RetinaFaceModel
from modules.utils import (set_memory_growth, load_yaml, draw_bbox_landm,
                           pad_input_image, recover_pad_output)

set_memory_growth()
# tf.debugging.set_log_device_placement(True)

cfg_path = './configs/retinaface_mbv2.yaml'
gpu = '0'
iou_th = 0.4
score_th = 0.5

cfg = load_yaml(cfg_path)

model = RetinaFaceModel(cfg, training=False, iou_th=iou_th, score_th=score_th)

checkpoint_dir = './checkpoints/' + cfg['sub_name']
checkpoint = tf.train.Checkpoint(model=model)
if tf.train.latest_checkpoint(checkpoint_dir):
    checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
    print("[*] load ckpt from {}.".format(
        tf.train.latest_checkpoint(checkpoint_dir)))
else:
    print("[*] Cannot find ckpt from {}.".format(checkpoint_dir))
    exit()

# 부모모듈 경로 가져오기
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
Ejemplo n.º 18
0
def model_selector_regression(config: dict):
    path = config.get('MODEL_SELECTOR_CONFIG_DIR')
    path = os.path.join(path, 'regression.yml')
    model_selector_config = load_yaml(path=path)

    return ModelSelector(config=model_selector_config)
Ejemplo n.º 19
0
    def __len__(self):
        return len(self.datas_list)

    def __getitem__(self, index):
        data = np.load(self.datas_list[index], allow_pickle=True).item()
        if self.is_aug:
            data = self.data_aug(data)

        data['Image'] = (data['Image'] / 255.).astype(np.float32)
        data['Posmap'] = (data['Posmap'] / 255.).astype(np.float32)

        return data


if __name__ == '__main__':
    cfg = load_yaml('./configs/prnet.yaml')
    train_dataset = load_dataset(cfg, shuffle=False)
    remain_steps = 100000
    print(len(train_dataset.dataset))
    model = PRN(cfg, is_dlib=True)

    for sample in take(remain_steps, train_dataset):
        for idx in range(cfg['batch_size']):
            img, pos = sample['Image'][idx], sample['Posmap'][idx]
            vertices = model.get_vertices(pos)
            cv2.imshow('img', img)
            cv2.imshow('pos', pos)
            cv2.imshow('Dense alignment', plot_vertices(img, vertices * 255.))
            if cv2.waitKey(0) == 113:
                exit()
Ejemplo n.º 20
0
def main(_argv):
    # init
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    # define network
    model = RetinaFaceModel(cfg,
                            training=False,
                            iou_th=FLAGS.iou_th,
                            score_th=FLAGS.score_th)

    # load checkpoint
    checkpoint_dir = './checkpoints/' + cfg['sub_name']
    checkpoint = tf.train.Checkpoint(model=model)
    if tf.train.latest_checkpoint(checkpoint_dir):
        checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
        print("[*] load ckpt from {}.".format(
            tf.train.latest_checkpoint(checkpoint_dir)))
    else:
        print("[*] Cannot find ckpt from {}.".format(checkpoint_dir))
        exit()

    if not FLAGS.webcam:
        if not os.path.exists(FLAGS.img_path):
            print(f"cannot find image path from {FLAGS.img_path}")
            exit()

        print("[*] Processing on single image {}".format(FLAGS.img_path))

        img_raw = cv2.imread(FLAGS.img_path)
        img_height_raw, img_width_raw, _ = img_raw.shape
        img = np.float32(img_raw.copy())

        if FLAGS.down_scale_factor < 1.0:
            img = cv2.resize(img, (0, 0),
                             fx=FLAGS.down_scale_factor,
                             fy=FLAGS.down_scale_factor,
                             interpolation=cv2.INTER_LINEAR)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # pad input image to avoid unmatched shape problem
        img, pad_params = pad_input_image(img, max_steps=max(cfg['steps']))

        # run model
        outputs = model(img[np.newaxis, ...]).numpy()

        # recover padding effect
        outputs = recover_pad_output(outputs, pad_params)

        # draw and save results
        save_img_path = os.path.join('out_' + os.path.basename(FLAGS.img_path))
        for prior_index in range(len(outputs)):
            draw_bbox_landm(img_raw, outputs[prior_index], img_height_raw,
                            img_width_raw)
            cv2.imwrite(save_img_path, img_raw)
        print(f"[*] save result at {save_img_path}")

    else:

        cam = cv2.VideoCapture(FLAGS.vid_path)
        fps = int(cam.get(cv2.CAP_PROP_FPS))

        ### Saving Video to file
        frame_width = int(cam.get(3))
        frame_height = int(cam.get(4))

        # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
        # import os
        if not os.path.exists('./output'):
            print('Creating folder: output/ for saving video.')
            os.makedirs('./output')
        out = cv2.VideoWriter('./output/output.avi',
                              cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
                              (frame_width, frame_height))

        start_time = time.time()
        counter = 0
        frameCount = 0
        while True:
            _, frame = cam.read()
            if frame is None:
                print("no cam input")
            frameCount = frameCount + 1

            frame_height, frame_width, _ = frame.shape
            img = np.float32(frame.copy())
            if FLAGS.down_scale_factor < 1.0:
                img = cv2.resize(img, (0, 0),
                                 fx=FLAGS.down_scale_factor,
                                 fy=FLAGS.down_scale_factor,
                                 interpolation=cv2.INTER_LINEAR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            # pad input image to avoid unmatched shape problem
            img, pad_params = pad_input_image(img, max_steps=max(cfg['steps']))

            # run model
            outputs = model(img[np.newaxis, ...]).numpy()

            # recover padding effect
            outputs = recover_pad_output(outputs, pad_params)

            # draw results
            for prior_index in range(len(outputs)):
                croppedFace = draw_bbox_landm(frame, outputs[prior_index],
                                              frame_height, frame_width)
                if frameCount >= fps * FLAGS.dfps:
                    fileName = "%d.png" % counter
                    cv2.imwrite(FLAGS.dst_path + fileName, croppedFace)
                    print('Saved:', fileName)
                    counter = counter + 1
                    frameCount = 0

            # calculate fps
            fps_str = "FPS: %.2f" % (1 / (time.time() - start_time))
            start_time = time.time()
            cv2.putText(frame, fps_str, (25, 25), cv2.FONT_HERSHEY_DUPLEX,
                        0.75, (0, 255, 0), 2)

            # show frame
            if FLAGS.preview:
                cv2.imshow('frame', frame)
            out.write(frame)
            if cv2.waitKey(1) == ord('q'):
                exit()
def main():
    # with open('/home/anhdq23/Desktop/nguyen/VT_simulation/weights/arcface_ret50.json', 'r') as f:
    #     model_json = json.load(f)
    # model = model_from_json(model_json)
    # model.load_weights('/home/anhdq23/Desktop/nguyen/VT_simulation/weights/arcface_ret50.h5')

    cfg = load_yaml('./configs/arc_res50_mask.yaml')
    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         num_classes=cfg['num_classes'],
                         head_type=cfg['head_type'],
                         embd_shape=cfg['embd_shape'],
                         w_decay=cfg['w_decay'],
                         training=False)
    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
    else:
        print("[*] training from scratch.")

    resnet_model = tf.keras.Model(inputs=model.get_layer('resnet50').input,
                                  outputs=model.get_layer('resnet50').output)
    resnet_head = tf.keras.Model(
        inputs=resnet_model.input,
        outputs=resnet_model.get_layer('conv2_block1_add').output)
    resnet_tail = split(resnet_model, 18, 1000)  # conv2_block1_out
    output_model = tf.keras.Model(
        inputs=model.get_layer('OutputLayer').input,
        outputs=model.get_layer('OutputLayer').output)

    temp1 = np.ones((62, 112, 3))
    temp2 = np.zeros((50, 112, 3))
    masked_img = np.concatenate([temp1, temp2], axis=0)

    path_to_data = '/home/anhdq23/Desktop/nguyen/data/AR/test2'
    anchor_names = list(get_gallery_pr2(path_to_data))  # From 1 to 100
    name_dicts = get_probe_pr2(
        path_to_data)  # Dictionary: {anchor_name:[name_image, ...]}

    dist_all = []
    labels_ARface = []
    for subject in np.arange(100):
        tmp = np.zeros((600, ))
        tmp[subject * 6:subject * 6 + 6] += 1
        labels_ARface.extend(list(tmp))
    labels_ARface = np.array(labels_ARface)

    database_image_names = []
    database_feature_list = []

    for anchor_name in anchor_names:
        # Woman
        if anchor_name == 'W-042-01.bmp':
            anchor_name = 'W-042-14.bmp'
        if anchor_name == 'W-028-01.bmp':
            anchor_name = 'W-028-14.bmp'
        if anchor_name == 'W-025-01.bmp':
            anchor_name = 'W-025-14.bmp'
        if anchor_name == 'W-016-01.bmp':  #for W7
            anchor_name = 'W-016-16.bmp'
        if anchor_name == 'W-003-01.bmp':
            anchor_name = 'W-003-14.bmp'
        if anchor_name == 'W-050-01.bmp':
            anchor_name = 'W-050-03.bmp'
        if anchor_name == 'W-012-01.bmp':
            anchor_name = 'W-012-03.bmp'

        # Man
        if anchor_name == 'M-009-01.bmp':
            anchor_name = 'M-009-14.bmp'
        if anchor_name == 'M-018-01.bmp':
            anchor_name = 'M-018-14.bmp'
        if anchor_name == 'M-028-01.bmp':
            anchor_name = 'M-028-02.bmp'
        if anchor_name == 'M-043-01.bmp':
            anchor_name = 'M-043-15.bmp'
        if anchor_name == 'M-048-01.bmp':
            anchor_name = 'M-048-14.bmp'

        img1 = Image.open(os.path.join(path_to_data, anchor_name))
        # img1 = expand2square(img1, (255, 255, 255))
        img1 = img1.resize((112, 112))
        img1 = np.array(img1) / 255.0
        img1 = np.multiply(img1, masked_img)

        fc1 = resnet_head.predict(img1.reshape((1, 112, 112, 3)))
        fc1 = resnet_tail.predict(fc1)
        fc1 = output_model.predict(fc1)
        norm_fc1 = preprocessing.normalize(fc1.reshape((1, 512)),
                                           norm='l2',
                                           axis=1)
        database_image_names.append(anchor_name)
        database_feature_list.append(norm_fc1)

    print(
        np.array(database_feature_list).shape, database_feature_list[0].shape)
    # Init faiss
    res = faiss.StandardGpuResources()  # use a single GPU
    index_flat = faiss.IndexFlatL2(512)
    # gpu_index_flat = faiss.index_cpu_to_gpu(res, 0, index_flat)
    gpu_index_flat = index_flat
    gpu_index_flat.add(np.array(database_feature_list).reshape(
        (-1, 512)))  # add vectors to the index

    count = 0
    for key in list(name_dicts.keys()):
        print(key)
        for name in name_dicts[key]:
            img2 = Image.open(os.path.join(path_to_data, name)).resize(
                (112, 112))
            img2 = img2.resize((112, 112))
            img2 = np.array(img2) / 255.0
            img2 = np.multiply(img2, masked_img)

            fc2 = resnet_head.predict(img2.reshape((1, 112, 112, 3)))
            fc2 = resnet_tail.predict(fc2)
            fc2 = output_model.predict(fc2)
            norm_fc2 = preprocessing.normalize(fc2.reshape((1, 512)),
                                               norm='l2',
                                               axis=1)

            D, I = gpu_index_flat.search(norm_fc2, k=1)  # actual search
            print(name, database_image_names[I[0][0]])
            if name[0:5] == database_image_names[I[0][0]][0:5]:
                count += 1
    print(count)

    plt.plot(dist_all)
    plt.show()
    thresholds = np.arange(0, 1, 0.01)
    tpr_all = []
    fpr_all = []
    for thr in thresholds:
        tpr, fpr, acc, f1 = calculate_accuracy(thr, np.array(dist_all),
                                               labels_ARface)
        top_left = np.sqrt((1 - tpr)**2 + fpr**2)
        print('thr %.4f' % thr , 'tpr %.4f' % tpr, 'fpr %.4f' % fpr, \
        'top left %.4f' % top_left, 'acc %.4f' % acc, 'f1_score %.4f'%f1)
        # top_left_batch.append(top_left)
        tpr_all.append(tpr)
        fpr_all.append(fpr)

    for threshold in thresholds:
        predict_issame = np.less(np.array(dist_all), threshold)
        conf_matrix = confusion_matrix(labels_ARface, predict_issame)
        print(conf_matrix)

    plt.figure()
    lw = 2
    plt.plot(fpr_all, tpr_all, color='darkorange', lw=lw, label='ROC curve')
    plt.xlim([0.0, 1.])
    plt.ylim([0.0, 1.])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic')
    plt.legend(loc="lower right")
    plt.show()