Beispiel #1
0
            'phase_train:0')
        embedding_size = embeddings.get_shape()[1]

        # Run a forward pass to calculate the image
        # vector embeddings.
        print('Calculating image features...')
        batch_size = 500  # 1000
        image_size = 160
        nrof_images = len(paths)
        nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / batch_size))
        embedding_arr = np.zeros((nrof_images, embedding_size))
        for i in range(nrof_batches_per_epoch):
            start_index = i * batch_size
            end_index = min((i + 1) * batch_size, nrof_images)
            paths_batch = paths[start_index:end_index]
            images = facenet.load_data(paths_batch, False, False, image_size)
            feed_dict = {
                images_placeholder: images,
                phase_train_placeholder: False
            }
            embedding_arr[start_index:end_index, :] = sess.run(
                embeddings, feed_dict=feed_dict)

        # Setup the classifier for classifing new images
        # based on their embedding vector.
        classifier_filename = './my_classifier/SVC.pkl'
        classifier_filename_exp = os.path.expanduser(classifier_filename)

        # Train the classifier
        print('Training classifier...')
        model = SVC(kernel='linear', probability=True)
def main():
    args = parse_args()
    align_command = 'python align_dataset_mtcnn.py ' + args.input_dir + args.align_dir + ' --image_size 182' + ' --margin 44'
    os.system(align_command)
    print("-------- Alignment Completed ----------")
    with tf.Graph().as_default():

        with tf.Session() as sess:
            np.random.seed(666)
            datadir = args.align_dir
            embeddingdir = "data/new_person_embedding/"
            modeldir = args.model_path

            dataset = facenet.get_dataset(datadir)
            paths, labels = facenet.get_image_paths_and_labels(dataset)
            print(labels)

            # # Create a list of class names
            #class_names = [cls.name.replace('_', ' ') for cls in dataset]
            #label_name = [class_names[i] for i in labels]

            print('Number of classes: {}'.format(len(dataset)))
            print('Number of images: {}'.format(len(paths)))
            print('Loading feature extraction model')

            facenet.load_model(modeldir)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]
            print(embedding_size)

            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            batch_size = 200
            image_size = 160
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(
                math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches_per_epoch):
                print('{}/{}'.format(i, nrof_batches_per_epoch))
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            # store embedding and labels
            np.savetxt(embeddingdir + 'embedding.csv',
                       emb_array,
                       delimiter=",")
            with open(embeddingdir + 'label.csv', 'w') as f:
                writer = csv.writer(f)
                writer.writerows(zip(labels, paths))
            # merge 2 embedding files
            merge_embedding_files("data/embedding/", embeddingdir,
                                  "embedding.csv")
            merge_label_files("data/embedding/", embeddingdir, "label.csv")

            # re-train the classifier
            start = time.time()
            fname = "data/embedding/label.csv"
            labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
            labels = map(itemgetter(1),
                         map(os.path.split, map(os.path.dirname,
                                                labels)))  # Get the directory.
            print(labels)
            print('list labels')
            labels = list(labels)
            print(labels)
            fname = "data/embedding/embedding.csv"
            embeddings = pd.read_csv(fname, header=None).as_matrix()
            le = LabelEncoder().fit(labels)
            class_names = list(le.classes_)
            class_names = [i.replace("_", " ") for i in class_names]
            labelsNum = le.transform(labels)
            #print(labelsNum)
            print(class_names)
            print(labelsNum)
            classifier_filename_exp = os.path.expanduser(
                args.classifier_filename)
            print('Start training classifier')
            if (args.classifier == 'SVM'):
                model = SVC(kernel='linear', probability=True)
            elif (args.classifier == 'KNN'):
                model = KNeighborsClassifier(n_neighbors=1)
            elif (args.classifier == 'LinearSVC'):
                model = LinearSVC(random_state=0, tol=1e-5)
            else:
                model = RandomForestClassifier(n_estimators=2000,
                                               max_depth=100,
                                               max_features='sqrt',
                                               n_jobs=-1)
            model.fit(embeddings, labelsNum)
            print(
                "Re-train the classifier took {} seconds.".format(time.time() -
                                                                  start))
            print('End training classifier')
            print(le)
            # saving classifier model
            with open(classifier_filename_exp, 'wb') as outfile:
                pickle.dump((model, class_names), outfile)
            print('Saved classifier model to file "%s"' %
                  classifier_filename_exp)
            print('Goodluck')
Beispiel #3
0
def main():
    args = parse_args()
    with tf.Graph().as_default():

        with tf.Session() as sess:
            np.random.seed(666)
            datadir = args.align_dir
            modeldir = args.model_path
            dataset = facenet.get_dataset(datadir)
            #f_map = "data/embedding/maps.csv"
            paths, labels = facenet.get_image_paths_and_labels(dataset)
            label1 = []
            for i in paths:
                person_name = i.split('/')[-2]
                label1.append(person_name)
            #map_test = pd.read_csv(f_map, header=None)
            #labelnum = []
            #for i in label1:
            #ID = int(map_test.loc[map_test[1]==i, 0].values)
            #labelnum.append(ID)
            label1 = [i.replace("_", " ") for i in label1]
            print('Number of classes: {}'.format(len(dataset)))
            print('Number of images: {}'.format(len(paths)))
            print('Loading feature extraction model')

            facenet.load_model(modeldir)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            batch_size = 200
            image_size = 160
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(
                math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches_per_epoch):
                print('{}/{}'.format(i, nrof_batches_per_epoch))
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)
            print('Testing classifier')
            classifier_filename_exp = os.path.expanduser(
                args.classifier_filename)
            with open(classifier_filename_exp, 'rb') as infile:
                (model, class_names) = pickle.load(infile)
            labelnum = []
            for i in label1:
                num = class_names.index(i)
                labelnum.append(num)
            print('Loaded classifier model from file "%s"' %
                  classifier_filename_exp)
            print(class_names)
            predictions = model.predict(emb_array)
            print(predictions)
            best_class_indices = predictions
            #best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
            #for i in range(len(best_class_indices)):
            #print('%4d  %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i]))
            print(best_class_indices)
            print('labelnum')
            print(labelnum)
            report = precision_recall_fscore_support(labelnum,
                                                     best_class_indices,
                                                     average='weighted')
            print(report[2])
Beispiel #4
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_idx
    network = importlib.import_module(args.model_def)
    ckpt_model_path = args.ckpt_model_path
    save_pb_path = args.save_pb_path

    # eval from ckpt(fake_ckpt)
    create_inference_graph(network)
    g = tf.get_default_graph()
    tf.contrib.quantize.create_eval_graph(input_graph=g)
    all_vars = tf.global_variables()
    print("eval global vars: \n +++++++++++++++++++++\n")
    for var in all_vars:
        print(var.name)
    print("++++++++++++++++++++++++++")
    saver = tf.train.Saver(tf.global_variables())
    sess = tf.Session()
    with sess.as_default():
        saver.restore(sess, ckpt_model_path)
        input_saver_def = saver.as_saver_def()
        frozen_graph_def = freeze_graph.freeze_graph_with_def_protos(
            input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=input_saver_def,
            input_checkpoint=ckpt_model_path, output_node_names='embeddings', restore_op_name='',
            filename_tensor_name='', clear_devices=True, output_graph='', initializer_nodes='')

        binary_graph = save_pb_path

        with tf.gfile.GFile(binary_graph, 'wb') as f:
            f.write(frozen_graph_def.SerializeToString())

        # evaluate test pairs acc
        paths, actual_issame = get_test_path.get_paths(os.path.expanduser(args.my_data_dir))
        path_all = {"paths": paths, "actual_issame": actual_issame, "type": "MY_DATA"}

        images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        image_size = args.image_size
        embedding_size = embeddings.get_shape()[1]
        paths = path_all["paths"]
        actual_issame = path_all["actual_issame"]
        batch_size = args.batch_size
        nrof_images = len(paths)
        nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size))
        emb_array = np.zeros((nrof_images, embedding_size))
        # ipdb.set_trace()
        for i in range(nrof_batches):
            start_index = i * batch_size
            end_index = min((i + 1) * batch_size, nrof_images)
            print("get embedding,idx start %d,idx end %d" %
                  (start_index, end_index))
            paths_batch = paths[start_index:end_index]
            images = facenet.load_data(
                paths_batch, False, False, image_size)
            feed_dict = {images_placeholder: images}
            emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict)
        # ipdb.set_trace()
        tpr, fpr, accuracy, val, val_std, far, acc_dot, recall, fpr_dot, percision_dot, dot_product_all, fp_idxs, fn_idxs, recall_th, percision_th, acc_th = lfw.evaluate(emb_array, actual_issame)
        """
        embeddings1 = emb_array[0::2]
        embeddings2 = emb_array[1::2]
        dot_product_all = np.sum(embeddings1 * embeddings2, 1)
        f = open("/data1/lishanlu/raw_data/dot_score.txt","w")
        f.write(str(dot_product_all))
        f.close()
        """
        print('Accuracy: %1.3f+-%1.3f' %
              (np.mean(accuracy), np.std(accuracy)))
        print('tpr: %1.3f+-%1.3f' % (np.mean(tpr), np.std(tpr)))
        print('fpr: %1.3f+-%1.3f' % (np.mean(fpr), np.std(fpr)))
        print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' %
              (val, val_std, far))

        print("\nacc_dot:%1.3f,recall:%1.3f,fpr_dot:%1.3f,percision_dot:%1.3f" %
              (acc_dot, recall, fpr_dot, percision_dot))
Beispiel #5
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            np.random.seed(seed=args.seed)
            embeddingdir = "data/embedding/"

            if args.use_split_dataset:
                dataset_tmp = facenet.get_dataset(args.data_dir)
                train_set, test_set = split_dataset(
                    dataset_tmp, args.min_nrof_images_per_class,
                    args.nrof_train_images_per_class)
                if (args.mode == 'TRAIN'):
                    dataset = train_set
                elif (args.mode == 'CLASSIFY'):
                    dataset = test_set
            else:
                dataset = facenet.get_dataset(args.data_dir)

            # Check that there are at least one training image per class
            for cls in dataset:
                assert (
                    len(cls.image_paths) > 0,
                    'There must be at least one image for each class in the dataset'
                )

            paths, labels = facenet.get_image_paths_and_labels(dataset)
            # Create a new label list containing names instead of numbers
            class_names = [cls.name.replace('_', ' ') for cls in dataset]
            label_name = [class_names[i] for i in labels]

            print('Number of classes: %d' % len(dataset))
            print('Number of images: %d' % len(paths))

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]
            print(embedding_size)

            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(
                math.ceil(1.0 * nrof_images / args.batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches_per_epoch):
                start_index = i * args.batch_size
                end_index = min((i + 1) * args.batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           args.image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)
            # Store embedding and labels
            np.savetxt(embeddingdir + 'embedding.csv',
                       emb_array,
                       delimiter=",")
            with open(embeddingdir + 'label.csv', 'w') as f:
                writer = csv.writer(f)
                writer.writerows(zip(labels, paths))

            classifier_filename_exp = os.path.expanduser(
                args.classifier_filename)

            if (args.mode == 'TRAIN'):
                # Train classifier
                print('Training classifier')
                if (args.classifier == 'SVM'):
                    model = SVC(kernel='linear', probability=True)
                elif (args.classifier == 'KNN'):
                    model = KNeighborsClassifier(n_neighbors=1)
                elif (args.classifier == 'Softmax'):
                    model = LogisticRegression(random_state=0,
                                               solver='lbfgs',
                                               multi_class='multinomial')
                else:
                    model = RandomForestClassifier(n_estimators=1000,
                                                   max_leaf_nodes=100,
                                                   n_jobs=-1)
                model.fit(emb_array, labels)
                # Create a list of class names
                class_names = [cls.name.replace('_', ' ') for cls in dataset]

                # Saving classifier model
                with open(classifier_filename_exp, 'wb') as outfile:
                    pickle.dump((model, class_names), outfile)
                print('Saved classifier model to file "%s"' %
                      classifier_filename_exp)

            elif (args.mode == 'CLASSIFY'):
                # Classify images
                print('Testing classifier')
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                print('Loaded classifier model from file "%s"' %
                      classifier_filename_exp)

                predictions = model.predict_proba(emb_array)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[
                    np.arange(len(best_class_indices)), best_class_indices]

                for i in range(len(best_class_indices)):
                    print('%4d  %s: %.3f' %
                          (i, class_names[best_class_indices[i]],
                           best_class_probabilities[i]))

                accuracy = np.mean(np.equal(best_class_indices, labels))
                report = precision_recall_fscore_support(labels,
                                                         best_class_indices,
                                                         average='weighted')
                print(report[2])
                print('Accuracy: %.3f' % accuracy)