def get_wts(caffe_model_config_json, wt_layer):
    feat_extractor = CaffeFeatureExtractor(caffe_model_config_json)

    wts = feat_extractor.net.params[wt_layer][0].data
    print('wts.shape:', wts.shape)

    return wts
Example #2
0
def main(config_json, save_dir, image_list_file, image_dir):
    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    fp = open(image_list_file, 'r')

    # init a feat_extractor
    print '\n===> init a feat_extractor'
    feat_extractor = CaffeFeatureExtractor(config_json)

    batch_size = feat_extractor.get_batch_size()

    print 'feat_extractor can process %d images in a batch' % batch_size

    img_list = []
    cnt = 0
    batch_cnt = 0

    for line in fp:
        if line.startswith('#'):
            continue

        items = line.split()
        img_list.append(items[0].strip())
        cnt += 1

        if cnt == batch_size:
            batch_cnt += 1
            print '\n===> Processing batch #%d with %d images' % (batch_cnt,
                                                                  cnt)

            process_image_list(feat_extractor, img_list, image_dir, save_dir)
            cnt = 0
            img_list = []

    if cnt > 0:
        batch_cnt += 1
        print '\n===> Processing batch #%d with %d images' % (batch_cnt, cnt)
        process_image_list(feat_extractor, img_list, image_dir, save_dir)

    fp.close()
def get_all_features(image_dir, sub_dirs):
	# Initialize Extraction Net (for pre-processing):
	extractionNet = CaffeFeatureExtractor(
		model_path = "/media/jedrzej/Seagate/Python/siamese-network/model/googlenet_deploy.prototxt",
		pretrained_path = "/media/jedrzej/Seagate/Python/siamese-network/model/bvlc_googlenet.caffemodel",
		blob = "pool5/7x7_s1",
		crop_size = 224,
		mean_values = [104.0, 117.0, 123.0]
		)
	all_info = []
	for sub_dir_i, sub_dir in enumerate(sub_dirs):
		print sub_dir_i
		allVal = []
		extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']	# allowed image extensions
		file_list = get_images(image_dir, sub_dir, extensions)

		n = len(file_list)
		for i in xrange(n):
			feat = np.asarray(extractionNet.extract_feature(caffe.io.load_image(file_list[i])).reshape(1024))
			allVal.append([file_list[i], feat.copy()])
		all_info.append(allVal)
	pickle.dump( all_info, open( "./train_features_70.p", "wb" ) )
	print "File saved"
	return all_info
Example #4
0
def run_caffe_feature_extractor_on_dataset(datasetPath, pathToSave,
                                           imgExtension):

    #Create caffe feature extractor object
    extractionNet = CaffeFeatureExtractor(
        model_path=DATA_ROOT +
        "ILSVRC2012/googlenet/googlenet_deploy.prototxt",
        pretrained_path=DATA_ROOT +
        "ILSVRC2012/googlenet/bvlc_googlenet.caffemodel",
        blob="pool5/7x7_s1",
        crop_size=224,
        mean_values=[104.0, 117.0, 123.0])
    model_path = DATA_ROOT + "ILSVRC2012/googlenet/googlenet_deploy.prototxt"
    pretrained_path = DATA_ROOT + "ILSVRC2012/googlenet/bvlc_googlenet.caffemodel"
    blob = "pool5/7x7_s1"
    crop_size = 224
    mean_values = [104.0, 117.0, 123.0]

    caffe.set_mode_gpu()
    caffe.set_device(1)

    extractionNet = caffe.Net(model_path, pretrained_path, caffe.TEST)

    mean = np.zeros((3, crop_size, crop_size))
    mean[0] = mean_values[0]
    mean[1] = mean_values[1]
    mean[2] = mean_values[2]
    transformer = caffe.io.Transformer(
        {"data": extractionNet.blobs["data"].data.shape})  # for cropping
    transformer.set_transpose("data", (2, 0, 1))  # (H,W,C) => (C,H,W)
    transformer.set_mean("data", mean)  # subtract by mean
    transformer.set_raw_scale("data", 255)  # [0.0, 1.0] => [0.0, 255.0].
    transformer.set_channel_swap("data", (2, 1, 0))  # RGB => BGR
    print "Transformer ready"

    if not os.path.exists(pathToSave):
        os.makedirs(pathToSave)

# Extracting features from datasePath and saving them as HDF5 files in pathToSave
    folderList = glob(datasetPath + '*/')
    for folder_i, folder in enumerate(folderList):
        start_time = time()
        category_name = folder.split("/")[-2]
        print category_name
        if (os.path.isfile(pathToSave + category_name + ".hdf5") == False):
            img_path_list = glob(folder + '*' + imgExtension)
            del folder
            images = []
            for img_path_i, img_path in enumerate(img_path_list):
                try:
                    img = caffe.io.load_image(img_path)  # read the image
                    img = caffe.io.resize_image(
                        img, (224, 224))  # resize to correct dimensions
                    images.append(transformer.preprocess("data", img))

                    #img_mirror = img[:, ::-1, :]  			# flip for mirrors
                    #images.append(transformer.preprocess("data", img_mirror))

                    del img  #, img_mirror
                except:
                    print "Probable error"
            del img_path_list
            images = np.asarray(images)

            n = 10  # number of splits so to not get 'out of memory'
            feats = np.zeros((1, 1024))
            ims = np.array_split(images, n)
            for i in xrange(n):
                extractionNet.blobs['data'].reshape(ims[i].shape[0],
                                                    ims[i].shape[1],
                                                    ims[i].shape[2],
                                                    ims[i].shape[3])
                out = extractionNet.forward_all(**{
                    extractionNet.inputs[0]: ims[i],
                    "blobs": [blob]
                })[blob].copy()
                feats = np.concatenate(
                    (feats, out.reshape(out.shape[0], out.shape[1])), axis=0)
                del out
            del images, ims
            feats = feats[1:, ...]

            f = h5py.File(pathToSave + category_name + '.hdf5', 'w')
            f.create_dataset('data', (feats.shape[0], feats.shape[1]),
                             dtype='double')
            f['data'][...] = feats
            f.close()
            del feats, f
        end_time = time()
        print "Processed", folder_i + 1, "folder in", str(
            round(end_time - start_time, 2)), "seconds"
Example #5
0
def run_caffe_feature_extractor_on_list_of_images(imageList):
    #Create caffe feature extractor object
    extractionNet = CaffeFeatureExtractor(
        model_path=DATA_ROOT +
        "ILSVRC2012/googlenet/googlenet_deploy.prototxt",
        pretrained_path=DATA_ROOT +
        "ILSVRC2012/googlenet/bvlc_googlenet.caffemodel",
        blob="pool5/7x7_s1",
        crop_size=224,
        mean_values=[104.0, 117.0, 123.0])
    model_path = DATA_ROOT + "ILSVRC2012/googlenet/googlenet_deploy.prototxt"
    pretrained_path = DATA_ROOT + "ILSVRC2012/googlenet/bvlc_googlenet.caffemodel"
    blob = "pool5/7x7_s1"
    crop_size = 224
    mean_values = [104.0, 117.0, 123.0]

    caffe.set_mode_gpu()
    caffe.set_device(1)

    extractionNet = caffe.Net(model_path, pretrained_path, caffe.TEST)

    mean = np.zeros((3, crop_size, crop_size))
    mean[0] = mean_values[0]
    mean[1] = mean_values[1]
    mean[2] = mean_values[2]
    transformer = caffe.io.Transformer(
        {"data": extractionNet.blobs["data"].data.shape})  # for cropping
    transformer.set_transpose("data", (2, 0, 1))  # (H,W,C) => (C,H,W)
    transformer.set_mean("data", mean)  # subtract by mean
    transformer.set_raw_scale("data", 255)  # [0.0, 1.0] => [0.0, 255.0].
    transformer.set_channel_swap("data", (2, 1, 0))  # RGB => BGR
    print "Transformer ready"
    images = []
    for img_path in imageList:
        try:
            start_time = time()
            img = caffe.io.load_image(img_path)  # read the image
            img = caffe.io.resize_image(
                img, (224, 224))  # resize to correct dimensions
            images.append(transformer.preprocess("data", img))
            del img
        except:
            print img_path
    print "Images transformed"
    images = np.asarray(images)
    n = 50  # number of splits so to not get 'out of memory'
    feats = np.zeros((1, 1024))
    ims = np.array_split(images, n)
    for i in xrange(n):
        extractionNet.blobs['data'].reshape(ims[i].shape[0], ims[i].shape[1],
                                            ims[i].shape[2], ims[i].shape[3])
        out = extractionNet.forward_all(**{
            extractionNet.inputs[0]: ims[i],
            "blobs": [blob]
        })[blob].copy()
        feats = np.concatenate(
            (feats, out.reshape(out.shape[0], out.shape[1])), axis=0)
        del out
    del images, ims
    feats = feats[1:, ...]

    return feats
Example #6
0
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--image',
                        type=str,
                        default="",
                        help="Path to the image")
    parser.add_argument('--model_dir',
                        type=str,
                        default="./model/",
                        help="Directory where the caffe model files are")
    parser.add_argument(
        '--inception_model_dir',
        type=str,
        default="./model_inception/",
        help=
        "Directory where the caffe model files are for the Inception V1 network"
    )

    FLAGS = parser.parse_args()
    extractionNet = CaffeFeatureExtractor(
        model_path=FLAGS.inception_model_dir + "googlenet_deploy.prototxt",
        pretrained_path=FLAGS.inception_model_dir +
        "bvlc_googlenet.caffemodel",
        blob="pool5/7x7_s1",
        crop_size=224,
        mean_values=[104.0, 117.0, 123.0])
    clearNet = caffe.Net(FLAGS.model_dir + "deploy.prototxt",
                         FLAGS.model_dir + "dmt_iter_150000.caffemodel",
                         caffe.TEST)
    CLEAR_boundary = run_net(clearNet, FLAGS.image, extractionNet)
def main(argv):
    args = parse_arguments(argv)
    print '===> args:\n', args

    config = load_config(args.config)
    print '===> config:\n', config

    extractor_config = config['face_feature']
    mtcnn_model_path = str(config['mtcnn_model_dir'])

    do_detect = not args.no_detect
    do_align = not args.no_align

    save_dir = args.save_dir
    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    save_img = args.save_image
    show_img = args.show_image

    detector = None
    aligner = None

    if do_detect:
        detector = MtcnnDetector(mtcnn_model_path)

    if do_align:
        if not do_detect:
            aligner = FaceAligner(mtcnn_model_path)
        else:
            aligner = FaceAligner(None)
    else:
        aligner = None

    feature_extractor = CaffeFeatureExtractor(extractor_config)
    feat_layer = feature_extractor.get_feature_layers()[0]

    fp = open(args.img_list_file, 'r')
    fp_rlt = open(osp.join(save_dir, 'face_feature.json'), 'w')
    fp_rlt.write('[\n')
    write_comma_flag = False

    # result_list = []
    img_cnt = 0
    faces_cnt = 0
    ttl_det_time = 0.0
    ttl_feat_time = 0.0

    for line in fp:
        img_path = line.strip()
        print("\n===>" + img_path)
        if img_path == '':
            print 'empty line, not a file name, skip to next'
            continue

        if img_path[0] == '#':
            print 'skip line starts with #, skip to next'
            continue

        # result_list.append(rlt)
        if write_comma_flag:
            fp_rlt.write(',\n')
        else:
            write_comma_flag = True

        rlt = {}
        rlt["filename"] = img_path
        rlt["faces"] = []
        rlt['face_count'] = 0

        try:
            if args.image_root_dir:
                img = cv2.imread(osp.join(args.image_root_dir, img_path))
            else:
                img = cv2.imread(img_path)
            print '\n---> img.shape: ', img.shape
        except:
            print('failed to load image: ' + img_path)
            #rlt["message"] = "failed to load"
            json_str = json.dumps(rlt, indent=2)
            fp_rlt.write(json_str)
            fp_rlt.flush()
            continue

        if img is None:
            print('failed to load image: ' + img_path)

            rlt["message"] = "failed to load"
            # result_list.append(rlt)
            json_str = json.dumps(rlt, indent=2)
            fp_rlt.write(json_str)
            fp_rlt.flush()
            continue

        img_cnt += 1
        if do_detect:
            t1 = time.clock()

            bboxes, points = detector.detect_face(img)

            t2 = time.clock()
            ttl_det_time += t2 - t1
            print("detect_face() costs %f seconds" % (t2 - t1))

        else:
            print '---> Will not do detection because of option "--no_detect"'
            shp = img.shape
            rect = [0, 0, shp[1] - 1, shp[0] - 1, 1.0]
            bboxes = [rect]
            points = [None]

        n_faces = 0
        if bboxes is not None:
            n_faces = len(bboxes)

        if n_faces > 0:
            for (box, pts) in zip(bboxes, points):
                #                box = box.tolist()
                #                pts = pts.tolist()
                tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts}
                rlt['faces'].append(tmp)

            rlt['face_count'] = n_faces

#        print('output bboxes: ' + str(bboxes))
#        print('output points: ' + str(points))
# toc()

        if do_detect:
            print(
                "\n===> Detect %d images, costs %f seconds, avg time: %f seconds"
                % (img_cnt, ttl_det_time, ttl_det_time / img_cnt))

        print "---> %d faces detected" % n_faces

        if not n_faces:
            continue

        t1 = time.clock()
        if do_align:
            if points is None or points[0] is None:
                face_chips = aligner.get_face_chips(img, bboxes, None)
            else:
                face_chips = aligner.get_face_chips(img, bboxes, points)


#            face_chips = aligner.get_face_chips(img, bboxes, None)

#            face_chips = [im.astype(np.float) for im in face_chips_ubyte]
        else:
            print '---> Will not do alignment because of option "--no_align"'
            face_chips = [img.astype(np.float)]

        features = feature_extractor.extract_features_batch(
            face_chips)[feat_layer]
        t2 = time.clock()
        ttl_feat_time += t2 - t1
        print("Cropping and extracting features for %d faces cost %f seconds" %
              (n_faces, t2 - t1))
        faces_cnt += n_faces

        print(
            "\n===> Extracting features for %d faces, costs %f seconds, avg time: %f seconds"
            % (faces_cnt, ttl_feat_time, ttl_feat_time / faces_cnt))

        for i, box in enumerate(bboxes):
            # feat_file = '%s_%d_rect[%d_%d_%d_%d].npy' % (
            #     osp.basename(img_path), i, box[0], box[1], box[2], box[3])
            # feat_file = osp.join(save_dir, feat_file)
            # np.save(feat_file, features[i])

            base_name = osp.basename(img_path)

            face_fn_prefix = '%s_face_%d' % (osp.splitext(base_name)[0], i)

            feat_file = face_fn_prefix + '.npy'
            np.save(osp.join(save_dir, feat_file), features[i])

            face_chip_fn = face_fn_prefix + '.jpg'
            cv2.imwrite(osp.join(save_dir, face_chip_fn), face_chips[i])

            rlt['faces'][i]['feat'] = feat_file
            rlt['faces'][i]['face_chip'] = face_chip_fn

        rlt['message'] = 'success'
        #        result_list.append(rlt)
        json_str = json.dumps(rlt, indent=2)
        fp_rlt.write(json_str)
        fp_rlt.flush()

        if save_img or show_img:
            draw_faces(img, bboxes, points)

        if save_img:
            save_name = osp.join(save_dir, osp.basename(img_path))
            cv2.imwrite(save_name, img)

        if show_img:
            cv2.imshow('img', img)

            ch = cv2.waitKey(0) & 0xFF
            if ch == 27:
                break

    #json.dump(result_list, fp_rlt, indent=4)
    fp_rlt.write('\n]\n')
    fp_rlt.close()
    fp.close()

    if show_img:
        cv2.destroyAllWindows()
def extract_probs_and_refine_labels(config_json,
                                    prob_thresh,
                                    first_new_id,
                                    max_orig_label,
                                    image_list_file,
                                    image_dir,
                                    save_dir=None,
                                    num_images=-1,
                                    mirror_input=False):

    if not save_dir:
        save_dir = 'rlt_probs_and_refined_labels'

    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    new_id_map = np.ones(max_orig_label + 1, dtype=np.int32) * (-1)

    calc_orig_label_prob = (first_new_id == 0)

    output_fn_prefix = 'prob_thresh_%g' % prob_thresh

    # test extract_features_for_image_list()
    output_fn1 = output_fn_prefix + '-nonoverlap-img_list.txt'
    output_fp1 = open(osp.join(save_dir, output_fn1), 'w')
    output_fn2 = output_fn_prefix + '-overlap-img_list.txt'
    output_fp2 = open(osp.join(save_dir, output_fn2), 'w')

    output_fp1.write(
        'image_name        new_label  max_label  prob[max_label]  orig_label  prob[orig_label]\n'
    )
    output_fp2.write(
        'image_name        new_label  max_label  prob[max_label]  orig_label  prob[orig_label]\n'
    )

    fp = open(image_list_file, 'r')

    # init a feat_extractor
    print '\n===> init a feat_extractor'

    feat_extractor = CaffeFeatureExtractor(config_json)
    batch_size = feat_extractor.get_batch_size()
    # overwrite 'feature_layer' in config by the last layer's name
    # prob_layer = feat_extractor.get_final_layer_name()
    # print '===> prob layer name: ', prob_layer
    # feat_extractor.set_feature_layers(prob_layer)

    print 'feat_extractor can process %5d images in a batch' % batch_size

    img_list = []
    label_list = []

    ttl_img_cnt = 0
    batch_img_cnt = 0
    batch_cnt = 0

    last_new_id = first_new_id - 1

    for line in fp:
        if line.startswith('#'):
            continue

        spl = line.split()
        img_list.append(spl[0].strip())

        if (len(spl) > 1):
            label_list.append(int(spl[1]))

        batch_img_cnt += 1
        ttl_img_cnt += 1

        if batch_img_cnt == batch_size or (num_images > 0
                                           and ttl_img_cnt == num_images):
            batch_cnt += 1
            print '\n===> Processing batch #%5d with %5d images' % (
                batch_cnt, batch_img_cnt)

            last_new_id = process_image_list(feat_extractor, prob_thresh,
                                             last_new_id, new_id_map,
                                             calc_orig_label_prob, save_dir,
                                             output_fp1, output_fp2, img_list,
                                             label_list, image_dir,
                                             mirror_input)
            batch_img_cnt = 0
            img_list = []
            label_list = []

            output_fp1.flush()
            output_fp2.flush()

        if (num_images > 0 and ttl_img_cnt == num_images):
            break

    if batch_img_cnt > 0:
        batch_cnt += 1
        print '\n===> Processing batch #%5d with %5d images' % (batch_cnt,
                                                                batch_img_cnt)
        last_new_id = process_image_list(feat_extractor, prob_thresh,
                                         last_new_id, new_id_map,
                                         calc_orig_label_prob, save_dir,
                                         output_fp1, output_fp2, img_list,
                                         label_list, image_dir, mirror_input)

    fp.close()

    output_fp1.close()
    output_fp2.close()

    new_id_map_fn = output_fn_prefix + '-nonoverlap-new_id_map.npy'
    new_id_map_fn = osp.join(save_dir, new_id_map_fn)
    np.save(new_id_map_fn, new_id_map)
def extract_features(config_json,
                     save_dir,
                     image_list_file,
                     image_dir,
                     check_src_exist=True,
                     skip_dst_exist=True,
                     gpu_id=None):

    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    if gpu_id is not None:
        gpu_id = int(gpu_id)
        fp = open(config_json)
        config_json = json.load(fp)
        fp.close()

        print '===> overwirte gpu_id from {} in config file into {}'.format(
            config_json['gpu_id'], gpu_id)
        config_json['gpu_id'] = gpu_id

    fp = open(image_list_file, 'r')

    # init a feat_extractor
    print '\n===> init a feat_extractor'
    feat_extractor = CaffeFeatureExtractor(config_json)

    batch_size = feat_extractor.get_batch_size()
    print 'feat_extractor can process %d images in a batch' % batch_size

    feat_layer_names = feat_extractor.get_feature_layers()
    print 'feat_extractor will extract features from layers:', feat_layer_names

    img_list = []
    cnt = 0
    batch_cnt = 0

    fn_skip = osp.join(save_dir, 'skipped_image_list.txt')
    fp_skip = open(fn_skip, 'w')

    for line in fp:
        if line.startswith('#'):
            continue

        # items = line.split()
        # img_fn = items[0].strip()
        img_fn = line.strip()

        if check_src_exist and not exist_src_img(image_dir, img_fn):
            print '---> Skip {}, source image not found'.format(img_fn)
            fp_skip.write('{}, no source\n'.format(img_fn))
            continue

        if skip_dst_exist and exist_dst_feats(save_dir, img_fn,
                                              feat_layer_names):
            print '---> Skip {}, dst features already exist'.format(img_fn)
            fp_skip.write('{}, exist dst\n'.format(img_fn))
            continue

        img_list.append(img_fn)
        cnt += 1

        if cnt == batch_size:
            batch_cnt += 1
            print '\n===> Processing batch #%d with %d images' % (batch_cnt,
                                                                  cnt)

            process_image_list(feat_extractor, img_list, image_dir, save_dir)
            cnt = 0
            img_list = []

            fp_skip.flush()

    if cnt > 0:
        batch_cnt += 1
        print '\n===> Processing batch #%d with %d images' % (batch_cnt, cnt)
        process_image_list(feat_extractor, img_list, image_dir, save_dir)

    fp.close()
    fp_skip.close()
def extract_and_save_probs_stats(config_json,
                                 max_orig_label,
                                 image_list_file,
                                 image_dir,
                                 save_dir=None,
                                 num_images=-1,
                                 is_train_set=False,
                                 mirror_input=False):

    if not save_dir:
        save_dir = 'rlt_probs_stats'

    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    num_ids = max_orig_label + 1

    fp = open(image_list_file, 'r')

    # init a feat_extractor
    print('\n===> init a feat_extractor')

    feat_extractor = CaffeFeatureExtractor(config_json)
    batch_size = feat_extractor.get_batch_size()
    # overwrite 'feature_layer' in config by the last layer's name
    # prob_layer = feat_extractor.get_final_layer_name()
    # print '===> prob layer name: ', prob_layer
    # feat_extractor.set_feature_layers(prob_layer)

    print('feat_extractor can process %5d images in a batch' % batch_size)

    img_list = []
    label_list = []

    ttl_img_cnt = 0
    batch_img_cnt = 0
    batch_cnt = 0

    cnt_per_id_vec = np.zeros(num_ids, dtype=np.int32)
    feats_stats_dict = {}

    for line in fp:
        if line.startswith('#'):
            continue

        spl = line.split()
        img_list.append(spl[0].strip())

        if (len(spl) > 1):
            label_list.append(int(spl[1]))

        batch_img_cnt += 1
        ttl_img_cnt += 1

        if batch_img_cnt == batch_size or (num_images > 0
                                           and ttl_img_cnt == num_images):
            batch_cnt += 1
            print('\n===> Processing batch #%5d with %5d images' %
                  (batch_cnt, batch_img_cnt))

            feats_stats_dict = process_image_batch(feat_extractor,
                                                   cnt_per_id_vec,
                                                   feats_stats_dict, img_list,
                                                   label_list, image_dir,
                                                   mirror_input)
            batch_img_cnt = 0
            img_list = []
            label_list = []

        if (num_images > 0 and ttl_img_cnt == num_images):
            break

    if batch_img_cnt > 0:
        batch_cnt += 1
        print('\n===> Processing batch #%5d with %5d images' %
              (batch_cnt, batch_img_cnt))
        feats_stats_dict = process_image_batch(feat_extractor, cnt_per_id_vec,
                                               feats_stats_dict, img_list,
                                               label_list, image_dir,
                                               mirror_input)

    fp.close()

    cnt_per_id_fn = osp.join(save_dir, 'stats-cnt_per_id.npy')
    np.save(cnt_per_id_fn, cnt_per_id_vec)

    for layer in feats_stats_dict.keys():
        for i in range(num_ids):
            if cnt_per_id_vec[i] > 0:
                # sum -> avg
                feats_stats_dict[layer][0, i] /= cnt_per_id_vec[i]
                # sqsum -> std
                feats_stats_dict[layer][1, i] /= cnt_per_id_vec[i]
                feats_stats_dict[layer][1, i] -= np.square(
                    feats_stats_dict[layer][0, i])

    print('\n===> Saving features stats (avg, std) for each id under ' +
          save_dir)
    save_feats_stats_dict(feats_stats_dict, save_dir)

    print('\n===> Saving save_max_label_info_stats under ' + save_dir)
    save_max_label_info_stats(cnt_per_id_vec, feats_stats_dict, save_dir,
                              is_train_set)
Example #11
0
def main(argv):
    args = parse_arguments(argv)
    print '===> args:\n', args

    config = load_config(args.config)
    print '===> config:\n', config

    max_faces = config['max_faces']
    extractor_config = config['face_feature']
    mtcnn_model_path = str(config['mtcnn_model_dir'])

    do_detect = not args.no_detect
    do_align = not args.no_align

    save_dir = args.save_dir
    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    pair_save_dir = osp.join(save_dir, 'img_pairs')
    if not osp.exists(pair_save_dir):
        os.mkdir(pair_save_dir)

    save_img = args.save_image
    show_img = args.show_image

    detector = None
    aligner = None

    if do_detect:
        detector = MtcnnDetector(mtcnn_model_path)

    if do_align:
        if not do_detect:
            aligner = FaceAligner(mtcnn_model_path)
        else:
            aligner = FaceAligner(None)
    else:
        aligner = None

    feature_extractor = CaffeFeatureExtractor(extractor_config)

    ctx_static = {}
    #ctx_static['args'] = args
    ctx_static['detector'] = detector
    ctx_static['aligner'] = aligner
    ctx_static['feature_extractor'] = feature_extractor
    ctx_static['do_detect'] = do_detect
    ctx_static['do_align'] = do_align
    ctx_static['save_img'] = save_img
    ctx_static['show_img'] = show_img
    ctx_static['save_dir'] = save_dir
    ctx_static['max_faces'] = max_faces

    #    result_list = []
    img_cnt = 0
    faces_cnt = 0
    ttl_det_time = 0.0
    ttl_feat_time = 0.0

    ctx_active = {}
    #ctx_active['result_list'] = result_list
    ctx_active['img_cnt'] = img_cnt
    ctx_active['faces_cnt'] = faces_cnt
    ctx_active['ttl_det_time'] = ttl_det_time
    ctx_active['ttl_feat_time'] = ttl_feat_time

    fp = open(args.img_list_file, 'r')
    fp_rlt = open(osp.join(save_dir, 'face_feature.json'), 'w')
    fp_rlt.write('[\n')
    write_comma_flag = False

    while True:
        line = fp.readline().strip()
        print '---> line: ', line
        if not line:
            break

        img_path = get_image_path(line, args.image_root_dir)
        print '---> img_path: ', img_path

        (rlt, features, face_chips) = detect_faces_and_extract_features(
            img_path, ctx_static, ctx_active)
        #        print 'features: ', features
        #        print 'id(features): ', id(features)

        # result_list.append(rlt)
        if write_comma_flag:
            fp_rlt.write(',\n')
        else:
            write_comma_flag = True

        json_str = json.dumps(rlt, indent=2)
        fp_rlt.write(json_str)
        fp_rlt.flush()

        line = fp.readline().strip()
        print '---> line: ', line
        if not line:
            break

        img_path2 = get_image_path(line, args.image_root_dir)
        print '---> img_path2: ', img_path2

        (rlt2, features2, face_chips2) = detect_faces_and_extract_features(
            img_path2, ctx_static, ctx_active)
        #        print 'features2: ', features2
        #        print 'features: ', features
        #
        #        print 'id(features): ', id(features)
        #        print 'id(features2): ', id(features2)
        #
        #        print 'features.data: ', id(features.data)
        #        print 'features2.data: ', id(features2.data)

        # result_list.append(rlt2)
        json_str = json.dumps(rlt2, indent=2)
        fp_rlt.write(',\n' + json_str)
        fp_rlt.flush()

        if rlt['face_count'] and rlt2['face_count']:
            #            sim = calc_similarity(features[0], features2[0])
            #            img_pair = np.hstack((face_chips[0], face_chips2[0]))
            #            img_pair_fn = '%s_%d_vs_%s_%d_%5.4f.jpg' % (osp.basename(img_path), 0, osp.basename(img_path2), 0, sim)
            #            img_pair_fn = osp.join(pair_save_dir, img_pair_fn)
            #            cv2.imwrite(img_pair_fn, img_pair)
            #
            #            print '---> similarity: ', sim

            for j in range(rlt['face_count']):
                for i in range(rlt2['face_count']):
                    sim = calc_similarity(features[j], features2[i])
                    print 'features[%d]: ' % j, features[j]
                    print 'features2[%d]: ' % i, features2[i]

                    img_pair = np.hstack((face_chips[j], face_chips2[i]))

                    img_pair_fn = '%s_%d_vs_%s_%d_%5.4f.jpg' % (osp.basename(
                        img_path), j, osp.basename(img_path2), i, sim)
                    img_pair_fn = osp.join(pair_save_dir, img_pair_fn)

                    sim_txt = '%5.4f' % sim
                    cv2_put_text_to_image(img_pair, sim_txt, 40, 5, 30,
                                          (0, 0, 255))
                    cv2.imwrite(img_pair_fn, img_pair)

                    print '---> similarity: ', sim

    # json.dump(result_list, fp_rlt, indent=2)
    fp_rlt.write('\n]\n')

    fp_rlt.close()
    fp.close()

    if show_img:
        cv2.destroyAllWindows()