def __init__(self, imgfn_iter): self.imgfn_iter = imgfn_iter try: self.imgfns = lz.msgpack_load(src_folder + '/all_imgs.pk') except: logging.info( "After crop_face_oppo.py runned, *_OPPOFaces/all_imgs.pk will be generetd, which logs img list. But, all_imgs.pk cannot be loaded, we are regenerating all img list now ...") self.imgfns = list(self.imgfn_iter) self.length = len(self.imgfns)
def __init__( self, path_ms1m=lz.share_path2 + 'faces_ms1m_112x112/', ): self.path_ms1m = path_ms1m self.root_path = Path(path_ms1m) path_imgrec = path_ms1m + '/train.rec' path_imgidx = path_imgrec[0:-4] + ".idx" assert os.path.exists(path_imgidx) self.imgrec = recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r') self.imgidx, self.ids, self.id2range = lz.msgpack_load(path_ms1m + '/info.pk') self.num_classes = len(self.ids) self.cur = 0 self.lock = mp.Lock()
def load_ms1m_info(): self = edict() path_ms1m = lz.share_path2 + 'faces_ms1m_112x112/' path_imgrec = lz.share_path2 + 'faces_ms1m_112x112/train.rec' path_imgidx = path_imgrec[0:-4] + ".idx" self.imgrec = recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r') imgidx, ids, id2range = lz.msgpack_load(path_ms1m + '/info.pk') print(len(imgidx), len(ids), len(id2range)) # while True: # time.sleep(10) # for indt in range(1): # id1 = ids[0] # imgid = id2range[id1][0] # s = self.imgrec.read_idx(imgid) # header, img = recordio.unpack(s) # print(header.label, id1) imgidx, ids = np.array(imgidx), np.array(ids) print(stat_np(imgidx)) print(stat_np(ids))
import lz from scipy.spatial.distance import cdist from sklearn.preprocessing import normalize import lmdb inputp = '/data/share/iccv19.lwface/iQIYI-VID-FACE/' env = lmdb.open( inputp + '/imgs_lmdb', readonly=True, ) lines = open(os.path.join(inputp, 'filelist.txt'), 'r').readlines() lines = [line.strip().split(' ')[0] for line in lines] df = pd.read_csv(inputp + '/filelist.txt', header=None, sep=' ') allids = lz.msgpack_load(inputp + '../allids.pk') nimgs = lz.msgpack_load(inputp + '../nimgs.pk') allimgs = lz.msgpack_load(inputp + '../allimgs.pk') vdonm2imgs = lz.msgpack_load(inputp + '../vdonm2imgs.pk') lines = np.asarray(lines) allids = np.asarray(allids) nimgs = np.asarray(nimgs) allimgs = np.asarray(allimgs) import six from PIL import Image def get_iccv_vdo(item): item = item.replace(inputp, '').strip('/') item = '/' + item
from lz import * import lz from reid.lib.cython_eval import eval_market1501_wrap # num_q = 1980 # num_g = 9330 # # distmat = np.random.rand(num_q, num_g).astype(np.float32) * 20 # q_pids = np.random.randint(0, min(num_q, num_g), size=num_q, dtype=np.int64) # g_pids = np.random.randint(0, min(num_q, num_g), size=num_g, dtype=np.int64) # q_camids = np.random.randint(0, 5, size=num_q, dtype=np.int64) # g_camids = np.random.randint(0, 5, size=num_g, dtype=np.int64) distmat, q_pids, g_pids, q_camids, g_camids = lz.msgpack_load(work_path + 'tmp.mp') # distmat, q_pids, g_pids, q_camids, g_camids = list(map(np.array, [distmat, q_pids, g_pids, q_camids, g_camids])) num_q = q_pids.shape[0] num_g = g_pids.shape[0] # for mat in [distmat, q_pids, g_pids, q_camids, g_camids]: # print(mat.dtype) tic = time.time() mAP, cmc = eval_market1501_wrap(distmat, q_pids, g_pids, q_camids, g_camids, 10) toc = time.time() print('\nconsume time {} \n mAP is {} \n cmc is {}\n'.format( toc - tic, mAP, cmc)) def eval_market1501(distmat, q_pids, g_pids, q_camids, g_camids, max_rank): """Evaluation with market1501 metric
def test_ijbc3(conf, learner): if not use_mxnet: learner.model.eval() ds = DatasetIJBC2(flip=False) loader = torch.utils.data.DataLoader( ds, batch_size=bs, num_workers=conf.num_workers, shuffle=False, pin_memory=False, ) len(loader) * bs for ind, data in enumerate(loader): (img, faceness_score, items, names) = data if ind % 9 == 0: logging.info(f'ok {ind} {len(loader)}') if not use_mxnet: with torch.no_grad(): img_feat = learner.model(img) img_featf = learner.model(img.flip((3, ))) fea = (img_feat + img_featf) / 2. fea = fea.cpu().numpy() else: img = img.numpy() img_feat = learner.gets(img) img_featf = learner.gets(img[:, :, :, ::-1].copy()) fea = (img_feat + img_featf) / 2. # fea = fea * faceness_score.numpy().reshape(-1, 1) # todo need? img_feats[ind * bs:min((ind + 1) * bs, ind * bs + fea.shape[0]), :] = fea print('last fea shape', fea.shape, np.linalg.norm(fea, axis=-1)[-10:], img_feats.shape) if dump_mid_res: import h5py f = h5py.File('/tmp/feas.ijbb.h5', 'w') f['feas'] = img_feats f.flush() f.close() templates, medias = df_tm.values[:, 1], df_tm.values[:, 2] p1, p2, label = df_pair.values[:, 0], df_pair.values[:, 1], df_pair.values[:, 2] unique_templates = np.unique(templates) # cache_tfn = lz.work_path + 'ijbc.tfeas.256.pk' cache_tfn = None if cache_tfn and osp.exists(cache_tfn): template_norm_feats = lz.msgpack_load(cache_tfn).copy() clst = msgpack_load(lz.work_path + 'ijbc.tclst.pk').copy() # clst = msgpack_load(work_path + 'ijbc.tgt.pk').copy() lbs = np.unique(clst) for lb in lbs: if lb == -1: continue mask = clst == lb if mask.sum() <= 4: continue nowf = template_norm_feats[mask, :] dist = cdist(nowf, nowf) dist[np.arange(dist.shape[0]), np.arange(dist.shape[0])] = dist.max() * 10 wei = lz.softmax_th(-dist, dim=1, temperature=1) refinef = np.matmul(wei, nowf) refinef = refinef * 0.3 + nowf * 0.7 refinef = normalize(refinef, axis=1) template_norm_feats[mask, :] = refinef if lb % 999 == 1: print('now refine ', lb, len(lbs), np.linalg.norm(nowf, axis=1), np.linalg.norm(refinef, axis=1)) else: template_feats = np.zeros((len(unique_templates), img_feats.shape[1])) for count_template, uqt in enumerate(unique_templates): (ind_t, ) = np.where(templates == uqt) face_norm_feats = img_feats[ind_t] face_medias = medias[ind_t] unique_medias, unique_media_counts = np.unique(face_medias, return_counts=True) media_norm_feats = [] for u, ct in zip(unique_medias, unique_media_counts): (ind_m, ) = np.where(face_medias == u) if ct == 1: media_norm_feats += [face_norm_feats[ind_m]] else: # image features from the same video will be aggregated into one feature media_norm_feats += [ np.mean(face_norm_feats[ind_m], 0, keepdims=True) ] media_norm_feats = np.array(media_norm_feats) # media_norm_feats = media_norm_feats / np.sqrt(np.sum(media_norm_feats ** 2, -1, keepdims=True)) template_feats[count_template] = np.sum(media_norm_feats, 0) if count_template % 2000 == 0: print('Finish Calculating {} template features.'.format( count_template)) template_norm_feats = template_feats / np.sqrt( np.sum(template_feats**2, -1, keepdims=True)) if cache_tfn: lz.msgpack_dump(template_norm_feats, cache_tfn) template2id = np.zeros((max(unique_templates) + 1, 1), dtype=int) for count_template, uqt in enumerate(unique_templates): template2id[uqt] = count_template score = np.zeros((len(p1), )) # save cosine distance between pairs total_pairs = np.array(range(len(p1))) batchsize = 100000 # small batchsize instead of all pairs in one batch due to the memory limiation sublists = [ total_pairs[i:i + batchsize] for i in range(0, len(p1), batchsize) ] total_sublists = len(sublists) for c, s in enumerate(sublists): feat1 = template_norm_feats[template2id[p1[s]]].reshape(-1, DIM) feat2 = template_norm_feats[template2id[p2[s]]].reshape(-1, DIM) # similarity_score = np.sum(feat1 * feat2, -1) similarity_score = -(np.linalg.norm(feat1, axis=-1) + np.linalg.norm( feat2, axis=-1) - 2 * np.sum(feat1 * feat2, -1)) score[s] = similarity_score.flatten() if c % 10 == 0: print('Finish {}/{} pairs.'.format(c, total_sublists)) if dump_mid_res: msgpack_dump([label, score], f'/tmp/score.ijbb.pk') print('score range', score.max(), score.min()) # _ = plt.hist(score) fpr, tpr, _ = roc_curve(label, score) # plt.figure() # plt.semilogx(fpr, tpr, '.-') # plt.show() fpr = np.flipud(fpr) tpr = np.flipud(tpr) # select largest tpr at same fpr res = [] x_labels = [ 10**-6, 10**-4, 10**-3, ] for fpr_iter in np.arange(len(x_labels)): _, min_index = min( list(zip(abs(fpr - x_labels[fpr_iter]), range(len(fpr))))) print(x_labels[fpr_iter], tpr[min_index]) res.append((x_labels[fpr_iter], tpr[min_index])) roc_auc = auc(fpr, tpr) print('roc aux', roc_auc) logging.info(f'perf {res}') if not use_mxnet: learner.model.train() return res
def __init__(self, batch_size, data_shape, path_imgrec=None, shuffle=False, aug_list=None, mean=None, rand_mirror=False, cutoff=0, color_jittering=0, images_filter=0, data_name='data', label_name='softmax_label', # metric_learning=True, metric_learning=False, # todo metric learning batch formation method do not affects the scale of loss ? **kwargs): super(FaceImageIter, self).__init__() self.metric_learning = metric_learning assert path_imgrec if path_imgrec: logging.info('loading recordio %s...', path_imgrec) path_imgidx = path_imgrec[0:-4] + ".idx" self.imgrec = recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r') # pylint: disable=redefined-variable-type try: self.imgidx, self.seq_identity, self.id2range = lz.msgpack_load( os.path.dirname(path_imgidx) + '/info.mxnet.pk', use_list=True) except: s = self.imgrec.read_idx(0) header, _ = recordio.unpack(s) if header.flag > 0: print('header0 label', header.label) self.header0 = (int(header.label[0]), int(header.label[1])) # assert(header.flag==1) # self.imgidx = list(range(1, int(header.label[0]))) self.imgidx = [] self.id2range = {} self.seq_identity = list(range(int(header.label[0]), int(header.label[1]))) for identity in self.seq_identity: s = self.imgrec.read_idx(identity) header, _ = recordio.unpack(s) a, b = int(header.label[0]), int(header.label[1]) count = b - a if count < images_filter: continue self.id2range[identity] = (a, b) self.imgidx += range(a, b) else: self.imgidx = list(self.imgrec.keys) lz.msgpack_dump([self.imgidx, self.seq_identity, self.id2range], os.path.dirname(path_imgidx) + '/info.mxnet.pk') print('id2range', len(self.id2range)) if shuffle: self.seq = self.imgidx self.oseq = self.imgidx print(len(self.seq)) else: self.seq = None self.mean = mean self.nd_mean = None if self.mean: self.mean = np.array(self.mean, dtype=np.float32).reshape(1, 1, 3) self.nd_mean = mx.nd.array(self.mean).reshape((1, 1, 3)) self.check_data_shape(data_shape) self.provide_data = [(data_name, (batch_size,) + data_shape)] self.batch_size = batch_size self.data_shape = data_shape self.shuffle = shuffle self.image_size = '%d,%d' % (data_shape[1], data_shape[2]) self.rand_mirror = rand_mirror print('rand_mirror', rand_mirror) self.cutoff = cutoff self.color_jittering = color_jittering self.CJA = mx.image.ColorJitterAug(0.125, 0.125, 0.125) self.provide_label = [(label_name, (batch_size,))] logging.info(f'one iter: provide {self.provide_data}, {self.provide_label}') # print(self.provide_label[0][1]) self.cur = 0 self.nbatch = 0 self.is_init = False self.num_instances = 4 self.num_pids_per_batch = self.batch_size // self.num_instances self.inds_queue = []
def main_allimg(args): global image_shape global net global ctx_num, env, glargs print(args) glargs = args env = lmdb.open( args.input + '/imgs_lmdb', readonly=True, # max_readers=1, lock=False, # readahead=False, meminit=False ) ctx = [] cvd = os.environ['CUDA_VISIBLE_DEVICES'].strip() if len(cvd) > 0: for i in xrange(len(cvd.split(','))): ctx.append(mx.gpu(i)) if len(ctx) == 0: ctx = [mx.cpu()] print('use cpu') else: print('gpu num:', len(ctx)) ctx_num = len(ctx) image_shape = [int(x) for x in args.image_size.split(',')] if use_mxnet: vec = args.model.split(',') assert len(vec) > 1 prefix = vec[0] epoch = int(vec[1]) print('loading', prefix, epoch) net = edict() net.ctx = ctx net.sym, net.arg_params, net.aux_params = mx.model.load_checkpoint( prefix, epoch) # net.arg_params, net.aux_params = ch_dev(net.arg_params, net.aux_params, net.ctx) all_layers = net.sym.get_internals() net.sym = all_layers['fc1_output'] net.model = mx.mod.Module(symbol=net.sym, context=net.ctx, label_names=None) net.model.bind(data_shapes=[('data', (args.batch_size, 3, image_shape[1], image_shape[2]))]) net.model.set_params(net.arg_params, net.aux_params) else: # sys.path.insert(0, lz.home_path + 'prj/InsightFace_Pytorch/') from config import conf lz.init_dev(use_devs) conf.need_log = False conf.fp16 = True # maybe faster ? conf.ipabn = False conf.cvt_ipabn = False conf.use_chkpnt = False conf.net_mode = 'ir_se' conf.net_depth = 100 conf.input_size = 128 conf.embedding_size = 512 from Learner import FaceInfer net = FaceInfer( conf, gpuid=range(len(use_devs)), ) net.load_state( resume_path=args.model, latest=True, ) net.model.eval() features_all = None filelist = os.path.join(args.input, 'filelist.txt') lines = open(filelist, 'r').readlines() buffer_images = [] buffer_embedding = np.zeros((0, emb_size), dtype=np.float16) row_idx = 0 import h5py f = h5py.File(args.output, 'w') chunksize = 80 * 10**3 dst = f.create_dataset("feas", (chunksize, 512), maxshape=(None, emb_size), dtype='f2') ind_dst = 0 vdonm2imgs = lz.msgpack_load(args.input + '/../vdonm2imgs.pk') for line in lines: if row_idx % 1000 == 0: logging.info( f"processing {(row_idx, len(lines), row_idx / len(lines),)}") row_idx += 1 # if row_idx<203000:continue # print('stat', i, len(buffer_images), buffer_embedding.shape, aggr_nums, row_idx) videoname = line.strip().split()[0] # images2 = glob.glob("%s/%s/*.jpg" % (args.input, videoname)) # images2 = np.sort(images2).tolist() images = vdonm2imgs[videoname] assert len(images) > 0 for image_path in images: buffer_images.append(image_path) while len(buffer_images) >= args.batch_size: embedding = get_feature(buffer_images[0:args.batch_size]) buffer_images = buffer_images[args.batch_size:] if ind_dst + args.batch_size > dst.shape[0]: dst.resize((dst.shape[0] + chunksize, emb_size), ) dst[ind_dst:ind_dst + args.batch_size, :] = embedding.astype('float16') ind_dst += args.batch_size # buffer_embedding = np.concatenate((buffer_embedding, embedding), axis=0).astype('float16') if len(buffer_images) != 0: embedding = get_feature(buffer_images) if ind_dst + args.batch_size > dst.shape[0]: dst.resize((dst.shape[0] + chunksize, emb_size), ) dst[ind_dst:ind_dst + args.batch_size, :] = embedding.astype('float16') # lz.save_mat(args.output, buffer_embedding) f.flush() f.close()