Exemple #1
0
import utils.facenet as facenet
import utils.detect_face as detect_face
import random
from time import sleep

# Set allow_pickle=True
np_load_old = np.load
np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)

output_dir_path = './aligned_img_db/'
output_dir = os.path.expanduser(output_dir_path)
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

datadir = './img_db/'
dataset = facenet.get_dataset(datadir)

print('Creating networks and loading parameters')
with tf.Graph().as_default():
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                            log_device_placement=False))
    with sess.as_default():
        pnet, rnet, onet = detect_face.create_mtcnn(sess, './npy')

# Config variables
minsize = 20  # minimum size of face
threshold = [0.6, 0.7, 0.7]  # three steps's threshold
factor = 0.709  # scale factor
margin = 44
image_size = 182
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')
Exemple #3
0
def main(args):
    sleep(random.random())
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(args.input_dir)
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(allow_growth=True)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
    
    minsize = 20 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    
    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir, filename+'.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim<2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:,:,0:3]
    
                        bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces>0:
                            det = bounding_boxes[:,0:4]
                            det_arr = []
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces>1:
                                if args.detect_multiple_faces:
                                    for i in range(nrof_faces):
                                        det_arr.append(np.squeeze(det[i]))
                                else:
                                    bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                                    offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                                    index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                                    det_arr.append(det[index,:])
                            else:
                                det_arr.append(np.squeeze(det))

                            for i, det in enumerate(det_arr):
                                det = np.squeeze(det)
                                bb = np.zeros(4, dtype=np.int32)
                                bb[0] = np.maximum(det[0]-args.margin/2, 0)
                                bb[1] = np.maximum(det[1]-args.margin/2, 0)
                                bb[2] = np.minimum(det[2]+args.margin/2, img_size[1])
                                bb[3] = np.minimum(det[3]+args.margin/2, img_size[0])
                                cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                                scaled = misc.imresize(cropped, (args.image_size, args.image_size), interp='bilinear')
                                nrof_successfully_aligned += 1
                                filename_base, file_extension = os.path.splitext(output_filename)
                                if args.detect_multiple_faces:
                                    output_filename_n = "{}_{}{}".format(filename_base, i, file_extension)
                                else:
                                    output_filename_n = "{}{}".format(filename_base, file_extension)
                                misc.imsave(output_filename_n, scaled)
                                text_file.write('%s %d %d %d %d\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3]))
                        else:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
Exemple #4
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])
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_idx
    network = importlib.import_module(args.model_def)
    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    model_good_dir = os.path.join(os.path.expanduser(args.models_good_dir), subdir)
    if not os.path.isdir(log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    if not os.path.isdir(model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)
    if not os.path.isdir(model_good_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_good_dir)
    # Write arguments to a text file
    facenet.write_arguments_to_file(
        args, os.path.join(log_dir, 'arguments.txt'))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    train_set = facenet.get_dataset(args.data_dir)

    if args.increased_data_dir:
        increased_set = facenet.get_dataset(args.increased_data_dir)
        train_set += increased_set
    if args.filter_filename:
        nrof_classes_before_filter = len(train_set)
        train_set = filter_dataset(train_set, os.path.expanduser(args.filter_filename),
                                   args.filter_percentile, args.filter_min_nrof_images_per_class)
        print(str(len(train_set) - nrof_classes_before_filter)+" classes filtered")

    nrof_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)

    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # Read the file containing the pairs used for testing
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
        # Get the paths for the corresponding images
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

    train_graph = tf.Graph()
    with train_graph.as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)

        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The dataset should not be empty'

        # Create a queue that produces indices into the image_list and label_list
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]

        index_queue = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32)

        index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue')

        learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate')

        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')

        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')

        image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths')

        labels_placeholder = tf.placeholder(tf.int64, shape=(None, 1), name='labels')

        input_queue = data_flow_ops.FIFOQueue(capacity=150000,
                                              dtypes=[tf.string, tf.int64],
                                              shapes=[(1,), (1,)],
                                              shared_name=None, name=None)
        enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder], name='enqueue_op')

        nrof_preprocess_threads = 4
        images_and_labels = []
        for _ in range(nrof_preprocess_threads):
            filenames, label = input_queue.dequeue()
            images = []
            for filename in tf.unstack(filenames):
                file_contents = tf.read_file(filename)
                image = tf.image.decode_image(file_contents, channels=3)
                if args.random_rotate:
                    image = tf.py_func(facenet.random_rotate_image, [image], tf.uint8)
                if args.random_crop:
                    image = tf.random_crop(image, [args.image_size, args.image_size, 3])
                else:
                    image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size)
                if args.random_flip:
                    image = tf.image.random_flip_left_right(image)
                if args.random_brightness:
                    image = tf.image.random_brightness(image, max_delta=0.5)
                if args.random_contrast:
                    image = tf.image.random_contrast(image, 0.8, 1.2)
                if args.random_saturation:
                    image = tf.image.random_saturation(image, 0.3, 0.5)
                image.set_shape((args.image_size, args.image_size, 3))
                images.append(tf.image.per_image_standardization(image))
            images_and_labels.append([images, label])

        image_batch, label_batch = tf.train.batch_join(
            images_and_labels, batch_size=batch_size_placeholder,
            shapes=[(args.image_size, args.image_size, 3), ()], enqueue_many=True,
            capacity=4 * nrof_preprocess_threads * args.batch_size,
            allow_smaller_final_batch=True)
        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')
        image_max = tf.reduce_max(image_batch, name='image_max')  #统计最大值
        image_min = tf.reduce_min(image_batch, name='image_min')  #统计最小值

        print('Total number of classes: %d' % nrof_classes)
        print('Total number of examples: %d' % len(image_list))

        print('Building training graph')

        # Build the inference graph
        # for inception_v3
        # Note that is_training be set to True or False, not be placeholder.
        prelogits, _ = network.inference(image_batch,
                                         num_classes=None,
                                         is_training=True,
                                         dropout_keep_prob=args.keep_probability,
                                         bottleneck_layer_size=args.embedding_size)
        logits = slim.fully_connected(prelogits, len(train_set), activation_fn=None,
                                      weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
                                      weights_regularizer=slim.l2_regularizer(args.weight_decay),
                                      scope='Logits', reuse=False)

        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')

        # Add center loss
        if args.center_loss_factor > 0.0:
            prelogits_center_loss, _ = facenet.center_loss(
                prelogits, label_batch, args.center_loss_alfa, nrof_classes)
            tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                                 prelogits_center_loss * args.center_loss_factor)

        learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step,
                                                   args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch, logits=logits, name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        # Calculate the total losses
        regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        # print("regularization_losses:"+str(regularization_losses))
        total_loss = tf.add_n([cross_entropy_mean] +
                              regularization_losses, name='total_loss')

        # for fake quantized train, add quantized nodes automatically.
        train_graph = tf.get_default_graph()
        tf.contrib.quantize.create_training_graph(input_graph=train_graph, quant_delay=0)

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            train_op = facenet.train(total_loss, global_step, args.optimizer,
                                     learning_rate, args.moving_average_decay, tf.global_variables(),
                                     args.log_histograms, args.train_mode)

        # print all vars
        g_vars = tf.global_variables()
        print("++++++++++++++++++++")
        for g in g_vars:
            print(g.name)
        print("++++++++++++++++++++")

        if pretrained_model:
            # 指定加载某些变量的权重
            all_vars = tf.trainable_variables()
            #all_vars = tf.global_variables()
            # 跳过加载某些层的权重
            var_to_skip = [v for v in all_vars if v.name.startswith('Logits') or v.name.startswith('centers')]
            print("got pretrained_mode, var_to_skip:\n" + " \n".join([x.name for x in var_to_skip]))
            var_to_restore = [v for v in all_vars if not (v.name.startswith('Logits') or v.name.startswith('centers'))]
            """
            g_list = tf.global_variables()
            #bn_moving_vars = [g for g in g_list if "moving_mean" in g.name]
            #bn_moving_vars += [g for g in g_list if "moving_variance" in g.name]
            #act_quant_vars = [g for g in g_list if "act_quant" in g.name]
            #var_to_restore += act_quant_vars
            """
            saver = tf.train.Saver(all_vars, max_to_keep=20)
        else:
            var_list = tf.trainable_variables()
            #var_list = tf.global_variables()
            """
            g_list = tf.global_variables()
            bn_moving_vars = [g for g in g_list if "moving_mean" in g.name]
            bn_moving_vars += [g for g in g_list if "moving_variance" in g.name]
            print("++++++++++++++++++++")
            for g in bn_moving_vars:
                print(g.name)
            print("++++++++++++++++++++")
            var_list += bn_moving_vars
            """
            saver = tf.train.Saver(var_list, max_to_keep=20)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()
        train_global_init = tf.global_variables_initializer()
        train_local_init = tf.local_variables_initializer()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(train_global_init)
        sess.run(train_local_init)
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():
            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)
                """
                if args.lfw_dir:
                    print('evaluate on lfw with pretrained model: %s' %
                          pretrained_model)
                    evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder,
                             batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame,
                             args.lfw_batch_size, args.lfw_nrof_folds, log_dir, 0, summary_writer)
                """

            #tf.train.write_graph(sess.graph_def, model_dir, 'inception_v3_fake_quantized_train.pbtxt')
            saver1 = tf.train.Saver(tf.global_variables(), max_to_keep=20)
            # Training and validation loop
            print('Running training')
            epoch = 0
            while epoch < args.max_nrof_epochs:
                step = sess.run(global_step, feed_dict=None)
                print("step:", step)
                epoch = step // args.epoch_size
                #import ipdb; ipdb.set_trace()
                # Train for one epoch
                train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder,
                      learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, global_step,
                      total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file, image_max, image_min)
                # Save variables and the metagraph if it doesn't exist already
                ckpt_model = save_variables_and_metagraph(sess, saver1, summary_writer, model_dir, subdir, step)
                print("ckpt_model name:", ckpt_model)
                """
                if args.lfw_dir:
                    acc_dot = evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder,
                                       batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame,
                                       args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer)
                    if acc_dot > 0.97:
                        print("get a good model,save it!!!,step:%d,acc_dot:%.3f" %(step, acc_dot))
                        ckpt_model = save_variables_and_metagraph(sess, saver1, summary_writer, model_good_dir, subdir, step)
                """

    return model_dir
Exemple #6
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)