def extract_feature_from_file(pic_path): # 包含人脸检测和人脸识别 # align_face(pic_path) images = facenet.load_data([pic_path], False, False, image_size) if images.shape[-1] != 3: return None feed_dict = {images_placeholder: images} face_feature = sess.run(embeddings, feed_dict=feed_dict) return face_feature
def read_images(do_prewhiten=True): """Reads in the facial images of the 12 characters.""" char_img_map = OrderedDict() for character in CHARACTER_ORDER: path = DATASET / character image_paths = sorted(path.glob('*.jpg')) images = facenet.load_data(image_paths, False, False, IMAGE_SIZE, do_prewhiten) char_img_map[character] = images return char_img_map
def get_embedding(self, img_path): start_time = time.time() images = facenet.load_data([img_path], False, False, self._aligned_img_size) feed_dict = { self._images_placeholder: images, self._phase_train_placeholder: False } embeddings_arr = self._sess.run(self._embeddings, feed_dict=feed_dict) logging.warning('get_embedding time: {}s'.format(time.time() - start_time)) return embeddings_arr[0]
def get_embeddings(aligned_imgs_path, trained_model_path, tmp_dir, is_debug): assert (os.path.isdir(tmp_dir)) embeddings_filename = EMBEDDINGS_FILE if is_debug: embeddings_filename += DEBUG_EXT embeddings_path = os.path.join(tmp_dir, embeddings_filename) if os.path.isfile(embeddings_path): with open(embeddings_path, 'rb') as f: return pickle.load(f) aligned_imgs_paths = get_img_paths(aligned_imgs_path) if is_debug: aligned_imgs_paths = aligned_imgs_paths[:DEBUG_COUNT] img_idxs = [get_filename_wo_ext(x) for x in aligned_imgs_paths] with tf.Graph().as_default(): with tf.Session() as sess: facenet.load_model(trained_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] nrof_images = len(aligned_imgs_paths) nrof_batches = int(math.ceil(1.0 * nrof_images / BATCH_SIZE)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches): start_index = i * BATCH_SIZE end_index = min((i + 1) * BATCH_SIZE, nrof_images) paths_batch = aligned_imgs_paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, IMG_SIZE) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) with open(embeddings_path, 'wb') as f: pickle.dump((emb_array, img_idxs), f) logging.info('saved embeddings: {}'.format(embeddings_path)) for i in range(len(emb_array)): logging.debug('embedding: {}: {}'.format( i, aligned_imgs_paths[i])) return emb_array, img_idxs
def faces_to_vectors(inpath, modelpath, outpath, imgsize, batchsize=100): ''' Given a folder and a model, loads images and performs forward pass to get a vector for each face results go to a JSON, with filenames mapped to their facevectors :param inpath: Where are your images? Must be cropped to faces (use MTCNN!) :param modelpath: Where is the tensorflow model we'll use to create the embedding? :param outpath: Full path to output file (better give it a JSON extension) :return: Number of faces converted to vectors ''' results = dict() with tf.Graph().as_default(): with tf.Session() as sess: load_model(modelpath) mdl = None image_paths = get_image_paths(inpath) # 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") # Let's do them in batches, don't want to run out of memory for i in range(0, len(image_paths), batchsize): images = load_data(image_paths=image_paths[i:i + batchsize], do_random_crop=False, do_random_flip=False, image_size=imgsize, do_prewhiten=True) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array = sess.run(embeddings, feed_dict=feed_dict) for j in range(0, len(emb_array)): relpath = os.path.relpath(image_paths[i + j], inpath) results[relpath] = emb_array[j].tolist() # All done, save for later! json.dump(results, open(outpath, "w")) return len(results.keys())
def get_index(aligned_user_img_path, trained_model_path): embeddings_arr = None with tf.Graph().as_default(): with tf.Session() as sess: facenet.load_model(trained_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] images = facenet.load_data([aligned_user_img_path], False, False, IMG_SIZE) feed_dict = { images_placeholder: images, phase_train_placeholder: False } embeddings_arr = sess.run(embeddings, feed_dict=feed_dict) return embeddings_arr[0]
def fit(self): self.model_status = 'TRAIN' self.data_dir = My_align_dataset_mtcnn(0, 160, 32, True, 0.25).output_dir with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=self.seed) if self.use_split_dataset: dataset_tmp = facenet.get_dataset(self.data_dir) train_set, test_set = self.split_dataset( dataset_tmp, self.min_nrof_images_per_class, self.nrof_train_images_per_class) if (self.model_status == 'TRAIN'): dataset = train_set elif (self.model_status == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(self.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' ) self.paths, self.labels = facenet.get_image_paths_and_labels( dataset) print("paths", self.paths) print("labels", self.labels) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(self.paths)) # Load the model print('Loading feature extraction model', self.model) load_model_YesOrNo = facenet.load_model(self.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(self.paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / self.batch_size)) print(nrof_images, self.batch_size, nrof_batches_per_epoch, embedding_size) self.emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * self.batch_size end_index = min((i + 1) * self.batch_size, nrof_images) paths_batch = self.paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, self.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } self.emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) print("shape", self.emb_array.shape) self.classifier_filename_exp = os.path.expanduser( self.classifier_filename) # Train classifier print('Training classifier') self.classify_model = SVC(kernel='linear', probability=True) self.classify_model.fit(self.emb_array, self.labels) # Create a list of class names self.class_names = [ cls.name.replace('_', ' ') for cls in dataset ]
def calculate_embeddings(self, faces, data_from_pipeline=True, batch_size=100, image_size=160): 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] emb_array = None # print('Calculating features for images') # bar = Bar('Processing', max = len(input_data)) i = 0 if data_from_pipeline: i += 1 if len(faces): face_images = [] emb_array = np.zeros((len(faces), embedding_size)) for face in faces: img = face img = cv2.resize(img, dsize=(image_size, image_size), interpolation=cv2.INTER_CUBIC) img = facenet.prewhiten(img) img = facenet.crop(img, False, image_size) img = facenet.flip(img, False) face_images.append(img) feed_dict = { images_placeholder: np.array(face_images), phase_train_placeholder: False } emb_array = self._sess.run(embeddings, feed_dict=feed_dict) # bar.next() else: nrof_images = len(faces) 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 = faces[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, :] = self._sess.run( embeddings, feed_dict=feed_dict) # bar.goto(bar.index + end_index - start_index) # bar.finish() return emb_array
def main(args): with tf.Graph().as_default(): with tf.Session() 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 = GaussianNB() #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)
def classify(use_split_dataset, mode, data_dir, min_nrof_images_per_class, nrof_train_images_per_class, model, classifier_filename, batch_size, image_size): seed = 666 with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=seed) if use_split_dataset: dataset_tmp = facenet.get_dataset(data_dir) train_set, test_set = split_dataset( dataset_tmp, min_nrof_images_per_class, nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(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(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 / 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) res = [] # 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])) res.append((i, class_names[best_class_indices[i]], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy) return res
def train(use_split_dataset, mode, data_dir, min_nrof_images_per_class, nrof_train_images_per_class, model, classifier_filename, batch_size, image_size): seed = 666 with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=seed) if use_split_dataset: dataset_tmp = facenet.get_dataset(data_dir) train_set, test_set = split_dataset( dataset_tmp, min_nrof_images_per_class, nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(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(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 / 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) 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)
with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=666) os.system("python facenet/src/align/align_dataset_mtcnn.py " + sys.argv[1] + " aligned --image_size 160 --margin 32 --random_order") dataset = facenet.get_dataset("aligned") paths, labels = facenet.get_image_paths_and_labels(dataset) facenet.load_model('20180402-114759.pb') 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] images = facenet.load_data(paths, False, False, 150) emb_array_1 = np.zeros((len(labels), embedding_size)) j = min(len(images) - 1, 500) i = 0 while j < len(images): feed_dict = { images_placeholder: images[i:j], phase_train_placeholder: False } emb_array_1[i:j, :] = sess.run(embeddings, feed_dict=feed_dict) i = j j += 500 model = KMeans(n_clusters=max(labels) + 1, random_state=40, max_iter=1000) model.fit(emb_array_1, labels)
def embeding(model_dir='20170512-110547', data_dir='database_aligned', is_aligned=True, image_size=160, margin=44, gpu_memory_fraction=1.0, image_batch=1000, embeddings_name='embeddings.npy', labels_name='labels.npy', labels_strings_name='label_strings.npy', return_image_list=False): train_set = facenet.get_dataset(data_dir) image_list, label_list = facenet.get_image_paths_and_labels(train_set) # fetch the classes (labels as strings) exactly as it's done in get_dataset path_exp = os.path.expanduser(data_dir) classes = [ path for path in os.listdir(path_exp) if os.path.isdir(os.path.join(path_exp, path)) ] # get the label strings label_strings = [ name for name in classes if os.path.isdir(os.path.join(path_exp, name)) ] with tf.Graph().as_default(): with tf.Session() as sess: # Load model facenet.load_model(model_dir) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") # noqa: E501 embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") # noqa: E501 phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") # Run forward pass to calculate embeddings nrof_images = len(image_list) print('Number of images: ', nrof_images) batch_size = image_batch if nrof_images % batch_size == 0: nrof_batches = nrof_images // batch_size else: nrof_batches = (nrof_images // batch_size) + 1 print('Number of batches: ', nrof_batches) embedding_size = embeddings.get_shape()[1] emb_array = np.zeros((nrof_images, embedding_size)) start_time = time.time() for i in range(nrof_batches): if i == nrof_batches - 1: n = nrof_images else: n = i * batch_size + batch_size # Get images for the batch if is_aligned is True: images = facenet.load_data(image_list[i * batch_size:n], False, False, image_size) else: images = load_and_align_data(image_list[i * batch_size:n], image_size, margin, gpu_memory_fraction) feed_dict = { images_placeholder: images, phase_train_placeholder: False } # Use the facenet model to calculate embeddings embed = sess.run(embeddings, feed_dict=feed_dict) emb_array[i * batch_size:n, :] = embed print('Completed batch', i + 1, 'of', nrof_batches) run_time = time.time() - start_time print('Run time: ', run_time) # export embeddings and labels label_list = np.array(label_list) np.save(embeddings_name, emb_array) if emb_array.size > 0: labels_final = (label_list) - np.min(label_list) np.save(labels_name, labels_final) label_strings = np.array(label_strings) np.save(labels_strings_name, label_strings[labels_final]) np.save('image_list.npy', image_list) if return_image_list: np.save('validation_image_list.npy', image_list) return image_list, emb_array
def like_or_dislike_users(self, users): # automatically like or dislike users based on your previously trained # model on your historical preference. # facenet settings from export_embeddings.... data_dir = 'temp_images_aligned' embeddings_name = 'temp_embeddings.npy' # labels_name = 'temp_labels.npy' # labels_strings_name = 'temp_label_strings.npy' is_aligned = True image_size = 160 margin = 44 gpu_memory_fraction = 1.0 image_batch = 1000 prev_user = None for user in users: clean_temp_images() urls = user.get_photos(width='640') image_list = download_url_photos(urls, user.id, is_temp=True) # align the database tindetheus_align.main(input_dir='temp_images', output_dir='temp_images_aligned') # export the embeddings from the aligned database train_set = facenet.get_dataset(data_dir) image_list_temp, label_list = facenet.get_image_paths_and_labels( train_set) # noqa: E501 # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") # noqa: E501 embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") # noqa: E501 phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") # noqa: E501 # Run forward pass to calculate embeddings nrof_images = len(image_list_temp) print('Number of images: ', nrof_images) batch_size = image_batch if nrof_images % batch_size == 0: nrof_batches = nrof_images // batch_size else: nrof_batches = (nrof_images // batch_size) + 1 print('Number of batches: ', nrof_batches) embedding_size = embeddings.get_shape()[1] emb_array = np.zeros((nrof_images, embedding_size)) start_time = time.time() for i in range(nrof_batches): if i == nrof_batches - 1: n = nrof_images else: n = i * batch_size + batch_size # Get images for the batch if is_aligned is True: images = facenet.load_data( image_list_temp[i * batch_size:n], # noqa: E501 False, False, image_size) else: images = load_and_align_data( image_list_temp[i * batch_size:n], # noqa: E501 image_size, margin, gpu_memory_fraction) feed_dict = { images_placeholder: images, phase_train_placeholder: False } # Use the facenet model to calculate embeddings embed = self.sess.run(embeddings, feed_dict=feed_dict) emb_array[i * batch_size:n, :] = embed print('Completed batch', i + 1, 'of', nrof_batches) run_time = time.time() - start_time print('Run time: ', run_time) # export embeddings and labels label_list = np.array(label_list) np.save(embeddings_name, emb_array) if emb_array.size > 0: # calculate the n average embedding per profiles X = calc_avg_emb_temp(emb_array) # evaluate on the model yhat = self.model.predict(X) if yhat[0] == 1: didILike = 'Like' # check to see if this is the same user as before if prev_user == user.id: clean_temp_images_aligned() print('\n\n You have already liked this user!!! \n \n') print('This typically means you have used all of your' ' free likes. Exiting program!!! \n\n') self.likes_left = -1 return else: prev_user = user.id else: didILike = 'Dislike' else: # there were no faces in this profile didILike = 'Dislike' print('**************************************************') print(user.name, user.age, didILike) print('**************************************************') dbase_names = move_images_temp(image_list, user.id) if didILike == 'Like': print(user.like()) self.likes_left -= 1 else: print(user.dislike()) userList = [ user.id, user.name, user.age, user.bio, user.distance_km, user.jobs, user.schools, user.get_photos(width='640'), dbase_names, didILike ] self.al_database.append(userList) np.save('al_database.npy', self.al_database) clean_temp_images_aligned()