Beispiel #1
0
def main(argv):
    dataset_path = os.path.expanduser(argv.dataset_path)
    dataset_path = os.path.abspath(dataset_path)
    model_file = os.path.expanduser(argv.model)
    model_file = os.path.abspath(model_file)
    classifier_filename_exp = os.path.expanduser(argv.output_classifier)
    classifier_filename_exp = os.path.abspath(classifier_filename_exp)

    assert os.path.exists(dataset_path) is True, 'please correct path...'
    np.random.seed(seed=777)

    # TF
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                            log_device_placement=False))
    pnet, rnet, onet = facenet.align.detect_face.create_mtcnn(sess, None)

    fn.load_model(model_file)

    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]

    dataset = fn.get_dataset(dataset_path)
    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 = fn.get_image_paths_and_labels(dataset)
    print('Number of classes: %d' % len(dataset))
    print('Number of images: %d' % len(paths))

    nrof_images = len(paths)
    nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / 90))
    emb_array = np.zeros((nrof_images, embedding_size))

    for i in range(nrof_batches_per_epoch):
        start_index = i * 90
        end_index = min((i + 1) * 90, nrof_images)
        paths_batch = paths[start_index:end_index]
        images = fn.load_data(paths_batch, False, False, 160)
        feed_dict = {
            images_placeholder: images,
            phase_train_placeholder: False
        }
        emb_array[start_index:end_index, :] = sess.run(embeddings,
                                                       feed_dict=feed_dict)

    model = SVC(kernel='linear', probability=True)
    model.fit(emb_array, labels)
    class_names = [cls.name.replace('_', ' ') for cls in dataset]

    with open(classifier_filename_exp, 'wb') as outfile:
        pickle.dump((model, class_names), outfile)
    print('Saved classifier model to file "%s"' % classifier_filename_exp)
    exit(0)
def main():
  with tf.Graph().as_default() as _:
    with tf.Session() as sess:
      if get_num_of_files_in_dir(data_dir) == 0:
        print('No train data!')
        return
      
      dataset = facenet.get_dataset(data_dir)

      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)
      
      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(model_path)

      # Get input and output tensors
      images_placeholder = sess.graph.get_tensor_by_name("input:0")
      embeddings = sess.graph.get_tensor_by_name("embeddings:0")
      phase_train_placeholder = sess.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')
      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):
        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)
      
      classifier_filename_exp = os.path.expanduser(classifier_filename)

      x, y = SMOTETomek(random_state=4).fit_sample(emb_array, labels)

      print('Training classifier')
      model = SVC(kernel='linear', probability=True)
      model.fit(x, y)
  
      # 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)
Beispiel #3
0
def main(args, self):

    self.update_state(state='RUNNING')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25, allow_growth = True)
        with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:

            np.random.seed(seed=args.seed)

            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)

            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]

            # 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)

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

            if (args.mode=='TRAIN'):
                # Train classifier
                print('Training classifier')
                model = SVC(kernel='linear', probability=True)
                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))
                print('Accuracy: %.3f' % accuracy)

            sess.close()
Beispiel #4
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(
            per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        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, 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, _ = 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)
Beispiel #5
0
def classifier(data_dir, model_path, classifier_path):
    with tf.Graph().as_default():
        with tf.Session() as sess:
            if (get_num_of_files_in_dir(data_dir) == 0):
                print('There is no train data')
                return 0

            dataset = facenet.get_dataset(data_dir)
            paths, labels = facenet.get_image_paths_and_labels(dataset)
            # Load pretrained model's train parameter
            facenet.load_model(model_path)
            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]

            # Preprocess images
            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):
                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)

            # Load classifier model, class-label dict
            with open(classifier_path, 'rb') as infile:
                (model, class_names) = pickle.load(infile)
            print(class_names)
            # print('Loaded classifier model from file "%s"' % classifier_path)

            predictions = model.predict_proba(emb_array)
            # print(len(paths))
            # print(prediction_time_elapsed/len(paths))

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

            label_to_class_map = create_label_to_class_map(class_names)
            print(label_to_class_map)

            data_dir_result_label_map = create_data_dir_result_label_map(
                paths, best_class_indices, best_class_probabilities)

            data_dir_result_class_map = {}
            for dir_name in data_dir_result_label_map:
                label = data_dir_result_label_map[dir_name]
                data_dir_result_class_map[dir_name] = label_to_class_map[label]
                print('dir_name : ' + dir_name + '\t recognition : ' +
                      label_to_class_map[label])

            rename_all_files_by_details(best_class_indices,
                                        best_class_probabilities, paths,
                                        label_to_class_map)
            return data_dir_result_class_map
def classify_process(sess_name, sess, graph, model, class_names):
    start = time.time()
    sess_dir = 'tmp/' + sess_name
    align_result = align_dataset_mtcnn(sess_name)
    if align_result == None:
        print('Face not detected!')
        shutil.rmtree(sess_dir)
        return
    print(align_result)
    tracking(align_result[0], align_result[1], sess_name)
    try:
        with graph.as_default():
            dataset = facenet.get_dataset(sess_dir + '_t')
            paths, labels = facenet.get_image_paths_and_labels(dataset)

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

            # Preprocess images
            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):
                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)

            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]

            label_to_class_map = create_label_to_class_map(class_names)
            print(label_to_class_map)

            data_dir_result_label_map = create_data_dir_result_label_map(
                paths, best_class_indices, best_class_probabilities)

            data_dir_result_class_map = {}
            for dir_name in data_dir_result_label_map:
                label = data_dir_result_label_map[dir_name]
                data_dir_result_class_map[dir_name] = label_to_class_map[label]

            print(data_dir_result_class_map)
            end = time.time()
            print('Total time elapsed:', (end - start))

            processed_names = []
            for name in data_dir_result_class_map.values():
                if name == 'unknown' or name in processed_names:
                    continue
                processed_names.append(name)
                try:
                    update_attendance(name)
                except Exception as e:
                    print(e)
                sio.emit('attend', name)

            for i in range(len(paths)):
                filename = paths[i].split('/')[-1]
                path = paths[i][:-len(filename)]
                acc = round(best_class_probabilities[i] * 50, 4)
                acc = pow(acc, 3)
                acc = math.sqrt(acc)
                acc = round(acc)
                if acc >= 200:
                    for imgname in dataset:
                        t = str(time.time())
                        shutil.move(
                            path + '/' + imgname, 'dataset/{}/{}.jpg'.format(
                                label_to_class_map[best_class_indices[i]], t))

    except Exception as e:
        print(e)