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) 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.') 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))
def main(args): ijbc_meta = np.load(args.meta_path) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu logger = tf.get_logger() logger.disabled = True logger.setLevel(logging.FATAL) set_memory_growth() #cfg = load_yaml('configs/arc_res50.yaml') cfg = load_yaml(args.config_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() img_names = [ os.path.join(args.input_path, img_name.split('/')[-1]) for img_name in ijbc_meta['img_names'] ] embedding_size = cfg['embd_shape'] batch_size = cfg['batch_size'] img_size = cfg['input_size'] def read_img(filename): raw = tf.io.read_file(filename) img = tf.image.decode_jpeg(raw, channels=3) img = tf.cast(img, tf.float32) img = img / 255 return img dataset = tf.data.Dataset.from_tensor_slices(img_names) dataset = dataset.map(read_img, num_parallel_calls=tf.data.experimental.AUTOTUNE) dataset = dataset.batch(batch_size) dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) embeddings = model.predict(dataset, batch_size=batch_size, verbose=1) print('embeddings', embeddings.shape) np.save(args.output_path, embeddings)
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: str1 = 'this' str2 = 'this1' #print(str1 in str2) file_dir = "newTWICE_id" output = glob.glob(file_dir + "/*.npy") for i in range(100): sampleList = random.sample(output, 2) embedding1 = np.load(sampleList[0]) embedding2 = np.load(sampleList[1]) dist = distance(embedding1, embedding2, 1) str1 = sampleList[0].split("\\")[1].split(".")[0] str2 = sampleList[1].split("\\")[1].split(".")[0] print(str1) if dist < 0.4: print(str1 + " and " + str2 + "is same") print(dist) # tp = tp + 1 else: print(str1 + " and " + str2 + "is diff") print(dist)
def main(): 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) model.summary() 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.") temp1 = np.ones((62,112,3)) temp2 = np.zeros((50,112,3)) masked_img = np.concatenate([temp1, temp2], axis =0) path_img1 = '/home/anhdq23/Desktop/nguyen/data/AR/test2/M-002-12.bmp' path_img2 = '/home/anhdq23/Desktop/nguyen/data/AR/test2/M-003-01.bmp' img1 = Image.open(path_img1) img1 = img1.resize((112, 112)) img1 = np.array(img1)/255.0 img2 = Image.open(path_img2) img2 = img2.resize((112, 112)) img2 = np.array(img2)/255.0 mask_img2 = np.multiply(img2, masked_img) fc1 = model.predict(mask_img2.reshape((1,112,112,3))) norm_fc1 = preprocessing.normalize(fc1.reshape((1,512)), norm='l2', axis=1) fc2 = model.predict(img2.reshape((1,112,112,3))) 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) for i in np.arange(20): print(np.sqrt(np.sum(np.square(diff[0][i*25:i*25+25]), 0))/2) fig = plt.figure() ax = fig.add_subplot(2,1,1) ax.plot(np.arange(512), norm_fc1[0]) # ax = fig.add_subplot(2,1,2) ax.plot(np.arange(512), norm_fc2[0]) ax = fig.add_subplot(2,1,2) ax.plot(np.arange(512), diff[0]) plt.show()
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() 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))
def main(_argv): model = ArcFaceModel(size=FLAGS.input_size, training=False, use_pretrain=False) model.load_weights(FLAGS.weights).expect_partial() model.summary() # Saved path will be 'output_dir/model_name/version' saved_path = os.path.join(FLAGS.output_dir, 'arcface', str(FLAGS.version)) tf.saved_model.save(model, saved_path) logging.info("model saved to: {}".format(saved_path)) model = tf.saved_model.load(saved_path) infer = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY] logging.info(infer.structured_outputs) if not FLAGS.image: return img = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img = tf.image.resize(img, (FLAGS.input_size, FLAGS.input_size)) img = img / 255. img = tf.expand_dims(img, 0) t1 = time.time() outputs = infer(img) embeddings = outputs['OutputLayer'] t2 = time.time() logging.info('time: {}'.format(t2 - t1))
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 = 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(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)) 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) 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={:.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 % 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: model.compile(optimizer=optimizer, loss=loss_fn, run_eagerly=(FLAGS.mode == 'eager_fit')) 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/', 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) print("[*] training done!")
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) 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/train_' + 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) # 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) # archead = tf.keras.Model(inputs=model.get_layer('ArcHead').input, # outputs=model.get_layer('ArcHead').output) temp1 = np.ones((62, 112, 3)) temp2 = np.zeros((50, 112, 3)) masked_img = np.concatenate([temp1, temp2], axis=0).reshape(1, 112, 112, 3) temp1 = np.ones((1, 18, 28, 256)) temp2 = np.zeros((1, 10, 28, 256)) masked = np.concatenate([temp1, temp2], axis=1) # inputs = Input((112, 112, 3)) # labels = Input([]) # s = resnet_head(inputs) # s = tf.keras.layers.Multiply()([s, tf.constant(masked, dtype=tf.float32)]) # s = resnet_tail(s) # s = output_model(s) # s = archead([s, labels]) # new_model = Model((inputs, labels), s) # new_model.summary() # new_model.compile(optimizer=optimizer, loss=loss_fn) path_to_data = '/home/anhdq23/Desktop/nguyen/data/AR/test2' anchor_names = get_gallery_pr2(path_to_data) # From 1 to 100 name_dicts = get_probe_pr2( path_to_data) # Dictionary: {anchor_name:[name_image, ...]} augment = ImgAugTransform() import faiss if FLAGS.mode == 'eager_tf': top_left_all = [0.012] best_acc = 0.8 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']) sub_train_dataset = dataset.load_tfrecord_dataset( cfg['train_dataset'], cfg['batch_size'], cfg['binary_img'], is_ccrop=cfg['is_ccrop']) for batch, ((x, y), (sub_x, sub_y)) in enumerate( zip(train_dataset, sub_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 temp = np.multiply(temp, masked_img) sub_x0_new = np.array(sub_x[0], dtype=np.uint8) sub_x1_new = np.array(sub_x[1], dtype=np.float32) for i in np.arange(len(sub_x0_new)): sub_x0_new[i] = augment(sub_x0_new[i]) sub_temp = np.array(sub_x0_new, dtype=np.float32) / 255.0 model.train_on_batch(*((sub_temp, sub_x1_new), sub_x1_new)) 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) database_image_names = [] database_feature_list = [] for anchor_name in anchor_names: img1 = Image.open( os.path.join(path_to_data, anchor_name)) img1 = img1.resize((112, 112)) img1 = np.array(img1) / 255.0 img1 = np.multiply(img1, masked_img) fc1 = resnet_model.predict( img1.reshape((1, 112, 112, 3))) 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) index_flat = faiss.IndexFlatL2(512) 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()): 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_model.predict( img2.reshape((1, 112, 112, 3))) 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 if name[0:5] == database_image_names[I[0] [0]][0:5]: count += 1 acc = count / 600.0 if acc > best_acc: best_acc = acc print('[*] save ckpt file!') model.save_weights( 'checkpoints/{}/e_{}_b_{}.ckpt'.format( cfg['sub_name'], epochs, batch % steps_per_epoch)) print("The current acc: {:.6f} ".format(acc)) print("The best acc: {:.6f} ".format(best_acc)) model.save_weights('checkpoints/train_{}/{}.ckpt'.format( cfg['sub_name'], cfg['sub_name']))
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("Check Start!!!") file_dir = "C:/Users/smgg/Desktop/dataset/superjunior/all3.jpeg" npy_dir = "/SCLab/newTWICE_id/*,npy" img_list = glob.glob(file_dir) npy_list = glob.glob(npy_dir) for img_name in img_list: img = cv2.cvtColor(cv2.imread(img_name), cv2.COLOR_BGR2RGB) detector = MTCNN() data_list = detector.detect_faces(img) for data in data_list: xmin, ymin, width, height = data['box'] xmax = xmin + width ymax = ymin + height face_image = img[ymin:ymax, xmin: xmax, :] face_image = cv2.cvtColor(face_image, cv2.COLOR_RGB2BGR) # cv2.imshow('', face_image) # cv2.waitKey(0) img_resize = cv2.resize(face_image, (cfg['input_size'], cfg['input_size'])) img_resize = img_resize.astype(np.float32) / 255. if len(img_resize.shape) == 3: img_resize = np.expand_dims(img_resize, 0) embeds = l2_norm(model(img_resize)) i = 0 cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 1) for npy_name in npy_list: name_embeds = np.load(npy_name) # print(1) if distance(embeds, name_embeds,1) < 0.37: i = i + 1 name = npy_name.split('/')[5].split('\\')[1].split('.npy')[0] cv2.putText(img, name, (xmin, ymin - 15*(i)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1,cv2.LINE_AA) # else: # cv2.putText(img, "Unknown", (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA) # cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imshow('', img) cv2.waitKey(0)
logger = tf.get_logger() logger.disabled = True logger.setLevel(logging.FATAL) set_memory_growth() cfg = load_yaml(FLAGS.cfg_path) cfg['num_classes'] = 179721 cfg['num_samples'] = 8621403 cfg['train_dataset'] = 'data/popet/dtat/ms1m_asian.tfrecord' cfg['sub_name'] = '200324_model' print(cfg) 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(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
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)) # file_dir = "C:/Users/chaehyun/PycharmProjects/ArcFace37_TF2x/data/AFDB_masked_face_dataset/*" file_dir = "C:/Users/smgg/Desktop/dataset/superjunior/*.jpg" # detector = MTCNN() i = 0 img_list = glob.glob(file_dir) for i_list in img_list: img = cv2.cvtColor(cv2.imread(i_list), cv2.COLOR_BGR2RGB) detector = MTCNN() data_list = detector.detect_faces(img) for data in data_list: xmin, ymin, width, height = data['box'] xmax = xmin + width ymax = ymin + height face_image = img[ymin:ymax, xmin:xmax, :] face_image = cv2.cvtColor(face_image, cv2.COLOR_RGB2BGR) cv2.imshow('', face_image) print(i_list) img_resize = cv2.resize(face_image, (cfg['input_size'], cfg['input_size'])) cv2.imwrite("./{}.jpg".format(i), img_resize) cv2.imshow('', img_resize) cv2.waitKey(0) i = i + 1 cv2.putText(img, "0", (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA) cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 1) img_resize = img_resize.astype(np.float32) / 255. if len(img_resize.shape) == 3: img_resize = np.expand_dims(img_resize, 0) embeds = l2_norm(model(img_resize)) #print('./newTWICE_id/{}.npy'.format(i_list.split('/')[6].split('\\')[1].split('.jpg')[0])) np.save( './newTWICE_id/{}.npy'.format( i_list.split('/')[5].split('\\')[1].split('.jpg')[0]), 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))
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') # model.summary() cfg = load_yaml('./configs/arc_res50_mix.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) model.summary() ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name']) print(ckpt_path) if ckpt_path is not None: print("[*] load ckpt from {}".format(ckpt_path)) model.load_weights(ckpt_path) else: print("[*] training from scratch.") # # serialize model to JSON # model_json = model.to_json() # with open("/home/anhdq23/Desktop/nguyen/image-segmentation-keras/weights/arc_res50_new.json", "w") as json_file: # json.dump(model_json, json_file) # model_mask.save_weights("/home/anhdq23/Desktop/nguyen/VT_simulation/weights/arc_res50_mask.h5") data_path = '/home/anhdq23/Desktop/nguyen/arcface-tf2/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] dist_all = [] for idx in range(len(lfw_issame)): print(idx) fc1 = model.predict(image_1[idx].reshape((1,112,112,3))) norm_fc1 = preprocessing.normalize(fc1.reshape((1,cfg['embd_shape'])), norm='l2', axis=1) fc2 = model.predict(image_2[idx].reshape((1,112,112,3))) norm_fc2 = preprocessing.normalize(fc2.reshape((1,cfg['embd_shape'])), norm='l2', axis=1) # dist = tf.keras.losses.cosine_similarity(fc1.reshape((1,512)), fc2.reshape((1,512))) diff = np.subtract(norm_fc1, norm_fc2) dist = np.sqrt(np.sum(np.square(diff), 1))/2 dist_all.extend(dist) 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), lfw_issame) 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(lfw_issame, 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()
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()
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') # model.summary() cfg = load_yaml('./configs/arc_res50_new.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) model.summary() ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name']) print(ckpt_path) if ckpt_path is not None: print("[*] load ckpt from {}".format(ckpt_path)) model.load_weights(ckpt_path) else: print("[*] training from scratch.") model_mask = 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/' + 'arc_res50_mask') print(ckpt_path) if ckpt_path is not None: print("[*] load ckpt from {}".format(ckpt_path)) model_mask.load_weights(ckpt_path) else: print("[*] training from scratch.") import sys sys.path.append('/home/anhdq23/Desktop/nguyen/VT_simulation/') from detector import get_detector predictor = get_detector() ICPR_dict = dict() path_ICPR = '/home/anhdq23/Desktop/nguyen/data/ICPR_cropped_face' for name_fold in os.listdir(path_ICPR): print(name_fold) path_fold = os.path.join(path_ICPR, name_fold) if name_fold not in ICPR_dict.keys(): ICPR_dict[name_fold] = [] for name_image in os.listdir(path_fold): path_image = os.path.join(path_fold, name_image) if '60' not in name_image[-10:-4] and '90' not in name_image[-10:-4]\ and '75' not in name_image[-10:]: image = Image.open(path_image) image = expand2square(image, (255, 255, 255)) image = image.resize((112, 112)) image = np.array(image)/255.0 _, labels, _ = predictor.predict(image, 1500/2, 0.6) if labels.numpy()[0] == 1: fc1 = model_mask.predict(image.reshape((1,112,112,3))) norm_fc1 = preprocessing.normalize(fc1.reshape((1,cfg['embd_shape'])), norm='l2', axis=1) else: fc1 = model.predict(image.reshape((1,112,112,3))) norm_fc1 = preprocessing.normalize(fc1.reshape((1,cfg['embd_shape'])), norm='l2', axis=1) ICPR_dict[name_fold].append(norm_fc1) path_ICPR = '/home/anhdq23/Desktop/nguyen/data/ICPR_cropped_face' anchor_list = [] name_list = [] for name_fold in os.listdir(path_ICPR): print(name_fold) path_fold = os.path.join(path_ICPR, name_fold) for name_image in os.listdir(path_fold): path_image = os.path.join(path_fold, name_image) if '+0+0' in name_image[-10:] or '+0-15' in name_image[-10:] or\ '+0+15' in name_image[-10:] or '+15+0' in name_image[-10:] or\ '-15+0' in name_image[-10:]: print(name_image) image = Image.open(path_image) image = expand2square(image, (255, 255, 255)) image = image.resize((112, 112)) image = np.array(image)/255.0 _, labels, _ = predictor.predict(image, 1500/2, 0.6) if labels.numpy()[0] == 1: fc1 = model_mask.predict(image.reshape((1,112,112,3))) norm_fc1 = preprocessing.normalize(fc1.reshape((1,cfg['embd_shape'])), norm='l2', axis=1) else: fc1 = model.predict(image.reshape((1,112,112,3))) norm_fc1 = preprocessing.normalize(fc1.reshape((1,cfg['embd_shape'])), norm='l2', axis=1) anchor_list.append(norm_fc1) name_list.append(name_fold) # Init faiss import faiss count_true = 0 count_all = 0 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(anchor_list).reshape((-1,512))) for key in list(ICPR_dict.keys()): for feature in ICPR_dict[key]: D, I = gpu_index_flat.search(feature, k=1) # actual search print(key, name_list[I[0][0]]) if key == name_list[I[0][0]]: count_true +=1 count_all +=1 print(count_true, count_all)
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.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.video is None: cap = cv2.VideoCapture(0) else: cap = cv2.VideoCapture(str(FLAGS.video)) 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 cap.isOpened(): is_success, frame = cap.read() if is_success: 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 cap.release() if FLAGS.save: video_writer.release() cv2.destroyAllWindows()
import base64 import numpy as np import tensorflow as tf from sklearn import neighbors from helper import confirm_checkin from modules.models import ArcFaceModel from classifier import knn_init, add_embeds from modules.utils import load_yaml, l2_norm from flask import Flask, render_template, request, jsonify os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Load ResNet50 model print('[*] Model is importing') model = ArcFaceModel(size=112, backbone_type='ResNet50', training=False) model.load_weights(tf.train.latest_checkpoint('./checkpoints/arc_res50')) # Create daemon & init KNN clf = knn_init() app = Flask(__name__) app.secret_key = "@#!ILMHSOMUCH*@@!@" def get_embeds(base64_image): global model image = base64.b64decode(base64_image) numpy_image = np.frombuffer(image, dtype=np.uint8) face = cv2.imdecode(numpy_image, 1) face = cv2.resize(face, (112, 112)) face = face.astype(np.float32) / 255.
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']))
config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.9 tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config)) # 대충 모델관련된것들 가져오는중 from modules.evaluations import get_val_data, perform_val from modules.models import ArcFaceModel from modules.utils import set_memory_growth, load_yaml, l2_norm cfg_yaml_path = "./configs/arc_res50.yaml" cfg = load_yaml(cfg_yaml_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() def get_vect_face_img(align_face_img): # 벡터화된 이미지 값을 직접 넣도록 했습니다. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['CUDA_VISIBLE_DEVICES'] = '0' # 기본값 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_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()