def epoch_completed_callback(engine): epoch = engine.state.epoch if lr_scheduler is not None: lr_scheduler.step() if epoch % eval_interval == 0: logger.info("Model saved at {}/{}_model_{}.pth".format( save_dir, prefix, epoch)) if evaluator and epoch % eval_interval == 0: torch.cuda.empty_cache() # extract query feature evaluator.run(query_loader) q_feats = torch.cat(evaluator.state.feat_list, dim=0) q_ids = torch.cat(evaluator.state.id_list, dim=0).numpy() q_cams = torch.cat(evaluator.state.cam_list, dim=0).numpy() # extract gallery feature evaluator.run(gallery_loader) g_feats = torch.cat(evaluator.state.feat_list, dim=0) g_ids = torch.cat(evaluator.state.id_list, dim=0).numpy() g_cams = torch.cat(evaluator.state.cam_list, dim=0).numpy() g_img_paths = np.concatenate(evaluator.state.img_path_list, axis=0) perm = sio.loadmat( os.path.join(dataset_cfg.sysu.data_root, 'exp', 'rand_perm_cam.mat'))['rand_perm_cam'] mAP, r1, r5, _, _ = eval_sysu(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, perm) if writer is not None: writer.add_scalar('eval/mAP', mAP, epoch) writer.add_scalar('eval/r1', r1, epoch) writer.add_scalar('eval/r5', r5, epoch) evaluator.state.feat_list.clear() evaluator.state.id_list.clear() evaluator.state.cam_list.clear() evaluator.state.img_path_list.clear() del q_feats, q_ids, q_cams, g_feats, g_ids, g_cams torch.cuda.empty_cache()
logging.basicConfig(level=logging.INFO, format="%(message)s") # extract feature cmd = "python{} extract.py {} {} ".format(sys.version[0], args.gpu, args.model_path) if args.dataset is not None: cmd += "--dataset {}".format(args.dataset) subprocess.check_call(cmd.strip().split(" ")) # evaluation q_mat_path = 'features/%s/query-%s.mat' % (dataset, prefix) g_mat_path = 'features/%s/gallery-%s.mat' % (dataset, prefix) assert os.path.exists(q_mat_path) assert os.path.exists(g_mat_path) mat = sio.loadmat(q_mat_path) q_feats = mat["feat"] q_ids = mat["ids"].squeeze() q_cam_ids = mat["cam_ids"].squeeze() mat = sio.loadmat(g_mat_path) g_feats = mat["feat"] g_ids = mat["ids"].squeeze() g_cam_ids = mat["cam_ids"].squeeze() g_img_paths = mat['img_path'].squeeze() perm = sio.loadmat(os.path.join(dataset_cfg.get(dataset).data_root, 'exp', 'rand_perm_cam.mat'))['rand_perm_cam'] eval_sysu(q_feats, q_ids, q_cam_ids, g_feats, g_ids, g_cam_ids, g_img_paths, perm)
def train_completed(engine): torch.cuda.empty_cache() # extract query feature evaluator.run(query_loader) q_feats = torch.cat(evaluator.state.feat_list, dim=0) q_ids = torch.cat(evaluator.state.id_list, dim=0).numpy() q_cams = torch.cat(evaluator.state.cam_list, dim=0).numpy() q_img_paths = np.concatenate(evaluator.state.img_path_list, axis=0) # extract gallery feature evaluator.run(gallery_loader) g_feats = torch.cat(evaluator.state.feat_list, dim=0) g_ids = torch.cat(evaluator.state.id_list, dim=0).numpy() g_cams = torch.cat(evaluator.state.cam_list, dim=0).numpy() g_img_paths = np.concatenate(evaluator.state.img_path_list, axis=0) # print("best rank1={:.2f}%".format(engine.state.best_rank1)) if dataset == 'sysu': perm = sio.loadmat( os.path.join(dataset_cfg.sysu.data_root, 'exp', 'rand_perm_cam.mat'))['rand_perm_cam'] eval_sysu(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, perm, mode='all', num_shots=1, rerank=engine.rerank) eval_sysu(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, perm, mode='all', num_shots=10, rerank=engine.rerank) eval_sysu(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, perm, mode='indoor', num_shots=1, rerank=engine.rerank) eval_sysu(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, perm, mode='indoor', num_shots=10, rerank=engine.rerank) elif dataset == 'regdb': print('infrared to visible') eval_regdb(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, rerank=engine.rerank) print('visible to infrared') eval_regdb(g_feats, g_ids, g_cams, q_feats, q_ids, q_cams, q_img_paths, rerank=engine.rerank) elif dataset == 'market': eval_regdb(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, rerank=engine.rerank) evaluator.state.feat_list.clear() evaluator.state.id_list.clear() evaluator.state.cam_list.clear() evaluator.state.img_path_list.clear() del q_feats, q_ids, q_cams, g_feats, g_ids, g_cams torch.cuda.empty_cache()
def epoch_completed_callback(engine): epoch = engine.state.epoch if lr_scheduler is not None: lr_scheduler.step() if epoch % eval_interval == 0: logger.info("Model saved at {}/{}_model_{}.pth".format( save_dir, prefix, epoch)) if evaluator and epoch % eval_interval == 0 and epoch > start_eval: torch.cuda.empty_cache() # extract query feature evaluator.run(query_loader) q_feats = torch.cat(evaluator.state.feat_list, dim=0) q_ids = torch.cat(evaluator.state.id_list, dim=0).numpy() q_cams = torch.cat(evaluator.state.cam_list, dim=0).numpy() q_img_paths = np.concatenate(evaluator.state.img_path_list, axis=0) # extract gallery feature evaluator.run(gallery_loader) g_feats = torch.cat(evaluator.state.feat_list, dim=0) g_ids = torch.cat(evaluator.state.id_list, dim=0).numpy() g_cams = torch.cat(evaluator.state.cam_list, dim=0).numpy() g_img_paths = np.concatenate(evaluator.state.img_path_list, axis=0) if dataset == 'sysu': perm = sio.loadmat( os.path.join(dataset_cfg.sysu.data_root, 'exp', 'rand_perm_cam.mat'))['rand_perm_cam'] mAP, r1, r5, _, _ = eval_sysu(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, perm, mode='all', num_shots=1, rerank=engine.rerank) elif dataset == 'regdb': print('infrared to visible') mAP, r1, r5, _, _ = eval_regdb(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, rerank=engine.rerank) print('visible to infrared') mAP, r1_, r5, _, _ = eval_regdb(g_feats, g_ids, g_cams, q_feats, q_ids, q_cams, q_img_paths, rerank=engine.rerank) r1 = (r1 + r1_) / 2 elif dataset == 'market': mAP, r1, r5, _, _ = eval_regdb(q_feats, q_ids, q_cams, g_feats, g_ids, g_cams, g_img_paths, rerank=engine.rerank) if r1 > engine.state.best_rank1: engine.state.best_rank1 = r1 torch.save(model.state_dict(), "{}/model_best.pth".format(save_dir)) if writer is not None: writer.add_scalar('eval/mAP', mAP, epoch) writer.add_scalar('eval/r1', r1, epoch) writer.add_scalar('eval/r5', r5, epoch) evaluator.state.feat_list.clear() evaluator.state.id_list.clear() evaluator.state.cam_list.clear() evaluator.state.img_path_list.clear() del q_feats, q_ids, q_cams, g_feats, g_ids, g_cams torch.cuda.empty_cache()