def compute_embedding(image): image = load_and_align_image(image, IMAGE_SIZE, MARGIN, GPU_MEMORY_FRACTION) image = image[None, ...] with tf.Graph().as_default(): with tf.Session() as sess: # Load the 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") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: image, phase_train_placeholder: False } embedding = sess.run(embeddings, feed_dict=feed_dict) return embedding
def main(): with tf.Graph().as_default(): args={ 'lfw_dir':'/Users/unclewang/Downloads/lfw_mtcnnpy_60', 'lfw_batch_size':128, 'model':'/Users/unclewang/PycharmProjects/facenet/models/facenet/20170512-110547/20170512-110547.pb', 'image_size':160, 'lfw_pairs':'/Users/unclewang/PycharmProjects/facenet/data/pairs.txt', 'lfw_file_ext':'png', 'lfw_nrof_folds':10 } with tf.Session() as sess: # 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 paths, actual_issame = lfw.get_paths(os.path.expanduser(args['lfw_dir']), pairs, args['lfw_file_ext']) # Load the 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") #image_size = images_placeholder.get_shape()[1] # For some reason this doesn't work for frozen graphs image_size = args['image_size'] embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') batch_size = args['lfw_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)) for i in range(nrof_batches): 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) tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(emb_array, actual_issame, nrof_folds=args['lfw_nrof_folds']) print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy))) print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) auc = metrics.auc(fpr, tpr) print('Area Under Curve (AUC): %1.3f' % auc) eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.) print('Equal Error Rate (EER): %1.3f' % eer)
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: # Get the paths for the images paths = [] for imagePath in os.listdir(args.face_dir): imagePath = os.path.join(args.face_dir, imagePath) if os.path.isfile(imagePath): print(imagePath) paths.append(imagePath) # Load the model print('Model directory: %s' % args.model_dir) meta_file, ckpt_file = facenet.get_model_filenames( os.path.expanduser(args.model_dir)) print('Metagraph file: %s' % meta_file) print('Checkpoint file: %s' % ckpt_file) facenet.load_model(args.model_dir, meta_file, ckpt_file) # 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") image_size = images_placeholder.get_shape()[1] embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Runnning forward pass on images') 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)) for i in range(nrof_batches): 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, True, True, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) # Save embeddings to disk for i in range(nrof_images): np.save(paths[i], emb_array[i, :], allow_pickle=False)
def __init__(self, model_path): # モデルを読み込んでグラフに展開 facenet.load_model(model_path) # リサイズする縦・横の値 self.input_image_size = 150 self.sess = tf.Session() self.images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") self.embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") self.phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") self.embedding_size = self.embeddings.get_shape()[1]
def exporter(): with tf.Graph().as_default(): with tf.Session() as sess: # input_data = tf.placeholder(dtype=tf.float32, shape=[None, 224,224,3], name='input') # # output, _ = network.inference(input_data, keep_probability=1, phase_train=False, bottleneck_layer_size=512) # # embeddings = tf.identity(output, 'embeddings') # # output_node_names = ['input','embeddings'] # # loader = tf.train.Saver() # # # sess.run(tf.global_variables_initializer()) # # loader.restore(sess, ckpt_file) facenet.load_model(model_dir) # Get input and output tensors input_data = 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") tensor_info_inputs = { 'inputs': tf.saved_model.utils.build_tensor_info(input_data), 'phase_train': tf.saved_model.utils.build_tensor_info(phase_train_placeholder) } tensor_info_outputs = { 'embedding': tf.saved_model.utils.build_tensor_info(embeddings) } recognition_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs=tensor_info_inputs, outputs=tensor_info_outputs, method_name=tf.saved_model.signature_constants. PREDICT_METHOD_NAME)) builder = tf.saved_model.builder.SavedModelBuilder( './model/saved_model/1') builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: recognition_signature, }, ) builder.save()
def initFacenet(self): with tf.Graph().as_default(): self.sess = tf.Session() facenet.load_model('models/20180408-102900', session=self.sess) self.images_placeholder = tf.get_default_graph( ).get_tensor_by_name("input:0") self.embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") self.phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") self.classifier_filename_exp = os.path.expanduser( '2018zhongzhuanv2.pkl')
def __init__(self, model): """ Args: model: 模型的路径 """ with tf.Graph().as_default(): self._sess = tf.Session() facenet.load_model(model, self._sess) # Get input and output tensors self._images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") self._embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") self._phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
def main(args): pnet, rnet, onet = create_network_face_detection(args.gpu_memory_fraction) image_path_tmp = os.listdir(args.image_path[0]) print(image_path_tmp) image_paths = [args.image_path[0] + '/' + f for f in image_path_tmp] print(image_paths) # image_files = load_and_align_data(image_paths, args.image_size, args.margin, args.gpu_memory_fraction) all_image_path_num = len(image_path_tmp) chosen_img_index = get_index_every_nth(image_paths, args.batch_size) print(chosen_img_index) print('===============++++++++++++++++++++================') with tf.Graph().as_default(): with tf.Session() as sess: # Load the model facenet.load_model(args.model_path) # 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") emb = np.zeros((all_image_path_num, 128)) # Run forward pass to calculate embeddings for k in np.arange(len(chosen_img_index) - 1): # print('=====================k is {}=============='.format(k)) image_sub_files = image_paths[chosen_img_index[k] + 1:chosen_img_index[k + 1] + 1] images = align_data(image_sub_files, args.image_size, args.margin, pnet, rnet, onet) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb[chosen_img_index[k] + 1:chosen_img_index[k + 1] + 1, :] = sess.run(embeddings, feed_dict=feed_dict) # print('num of images is {} and num of embedding is {}'.format(np.shape(image_sub_files), np.shape(embeddings))) dist = calculate_distance_matrix(emb) folder_name = args.out_put_emb[0] print(dist) print('=========================') print(folder_name) print('======== distance average =============') print(np.sum(dist) / np.size(dist)) with open(folder_name + '/' + 'emb.txt', 'wb') as f: np.savetxt(f, emb, fmt='%1.6f') with open(folder_name + '/' + 'average_emb.txt', 'wb') as f: np.savetxt(f, np.mean(emb, axis=0), fmt='%1.6f') with open(folder_name + '/' + 'distance.txt', 'wb') as f: np.savetxt(f, dist, fmt='%.4f')
def main(args): """ """ with tf.Graph().as_default(): with tf.Session() as sess: # create output directory if it doesn't exist output_dir = os.path.expanduser(args.output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) # load the model print("Loading trained model...\n") facenet.load_model(args.trained_model_dir) # grab all image paths and labels print("Finding image paths and targets...\n") data = load_files(args.data_dir, load_content=False, shuffle=False) labels_array = data['target'] paths = data['filenames'] # 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") image_size = images_placeholder.get_shape()[1] embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Generating embeddings from images...\n') start_time = time.time() batch_size = args.batch_size nrof_images = len(paths) nrof_batches = int(np.ceil(1.0*nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in xrange(nrof_batches): 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, do_random_crop=False, do_random_flip=False, image_size=image_size, do_prewhiten=True) feed_dict = { images_placeholder:images, phase_train_placeholder:False} emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) time_avg_forward_pass = (time.time() - start_time) / float(nrof_images) print("Forward pass took avg of %.3f[seconds/image] for %d images\n" % (time_avg_forward_pass, nrof_images)) print("Finally saving embeddings and gallery to: %s" % (output_dir)) # save the gallery and embeddings (signatures) as numpy arrays to disk np.save(os.path.join(output_dir, "gallery.npy"), labels_array) np.save(os.path.join(output_dir, "signatures.npy"), emb_array)
def main(args): images = load_and_align_data(args.image_files, args.image_size, args.margin, args.gpu_memory_fraction) # plt.figure() # plt.imshow(images[1,:]) # plt.show() # print('askhnauisd') with tf.Graph().as_default(): with tf.Session() as sess: # Load the 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") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) nrof_images = len(args.image_files) print('Images:') for i in range(nrof_images): print('%1d: %s' % (i, args.image_files[i])) print('') # Print distance matrix print('Distance matrix') print(' ', end='') for i in range(nrof_images): print(' %1d ' % i, end='') print('') for i in range(nrof_images): print('%1d ' % i, end='') for j in range(nrof_images): dist = np.sqrt( np.sum(np.square(np.subtract(emb[i, :], emb[j, :])))) print(' %1.4f ' % dist, end='') print('')
def __init__(self): self.g = tf.Graph() with self.g.as_default(): self.sess = tf.Session() # Load the model print('Loading feature extraction model') facenet.load_model(self.pre_trained_model) # Get input and output tensors self.images_placeholder = tf.get_default_graph( ).get_tensor_by_name("input:0") self.embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") self.phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0")
def main(args): print("path: ") print(os.path.abspath('..')) images, cout_per_image, nrof_samples = load_and_align_data( args.image_files, args.image_size, args.margin, args.gpu_memory_fraction) with tf.Graph().as_default(): with tf.Session() as sess: # Load the 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") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser( args.classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"\n' % classifier_filename_exp) predictions = model.predict_proba(emb) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] k = 0 #print predictions for i in range(nrof_samples): print("\npeople in image %s :" % (args.image_files[i])) for j in range(cout_per_image[i]): print('%s: %.3f' % (class_names[best_class_indices[k]], best_class_probabilities[k])) k += 1 return class_names[best_class_indices[0]], best_class_probabilities[0]
def main(args): train_set = facenet.get_dataset(args.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(args.data_dir) classes = [path for path in os.listdir(path_exp) \ if os.path.isdir(os.path.join(path_exp, path))] classes.sort() # get the label strings label_strings = [name for name in classes if \ os.path.isdir(os.path.join(path_exp, name))] images = load_and_align_data(image_list, args.image_size, args.margin, args.gpu_memory_fraction) with tf.Graph().as_default(): with tf.Session() as sess: # Load the 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") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) nrof_images = len(image_list) # export emedings and labels label_list = np.array(label_list) np.save(args.embeddings_name, emb) label_strings = np.array(label_strings) np.save(args.labels_strings_name, label_strings[label_list])
def get_embeddings(args): dataset = gen_dataset(args) with tf.Graph().as_default(): with tf.Session() as sess: # Load the 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") # Run forward pass to calculate embeddings feed_dict = {images_placeholder: dataset, phase_train_placeholder: False} emb = sess.run(embeddings, feed_dict=feed_dict) print(emb) nrof_images = len(args.image_files)
def test(): image_embeddings, image_fnames = [], [] with tf.Session() as sess: # input_data = tf.placeholder(dtype=tf.float32, shape=[None, 160, 160, 3], name='input') # phase_train_placeholder = tf.placeholder(dtype=tf.bool, name='phase_train') # embeddings, _ = network.inference(input_data, keep_probability=1, phase_train=phase_train_placeholder, bottleneck_layer_size=512) # output_node_names = ['input', 'embeddings'] # loader = tf.train.Saver() # sess.run(tf.global_variables_initializer()) # loader.restore(sess, ckpt_file) # # Load the model facenet.load_model(model_dir) # Get input and output tensors input_data = 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") batch_data, batch_fname = get_batch_data_test() # for batch_data,batch_fname in tqdm(get_batch_data()): batch_embedding = sess.run(embeddings, feed_dict={ input_data: batch_data, phase_train_placeholder: False }) image_embeddings.append(batch_embedding) image_fnames.extend(batch_fname) print(euclidean_distances(batch_embedding, batch_embedding)) print(image_fnames) print(image_embeddings) with open('./data/lfw_test_image_embeddigns_inception_v1.pkl', 'wb') as f: pickle.dump(image_embeddings, f) pickle.dump(image_fnames, f)
def load_model_facenet(self): """Loads the facenet model""" print("Classifier: loading model facenet ...") self.model_facenet_g = tf.Graph() self.model_facenet_g.as_default().__enter__() self.model_facenet_session_tf = tf.Session() self.model_facenet_session_tf.__enter__() # Load the model print('Model directory: %s' % configuration.FACEEMBEDDING_MODEL_DIR) meta_file, ckpt_file = facenet.get_model_filenames( os.path.expanduser(configuration.FACEEMBEDDING_MODEL_DIR)) print('Metagraph file: %s' % meta_file) print('Checkpoint file: %s' % ckpt_file) facenet.load_model(configuration.FACEEMBEDDING_MODEL_DIR, meta_file, ckpt_file) # Get input and output tensors self.model_facenet_images_placeholder = tf.get_default_graph( ).get_tensor_by_name("input:0") self.model_facenet_embeddings = tf.get_default_graph( ).get_tensor_by_name("embeddings:0") self.model_facenet_phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") self.model_facenet_image_size = self.model_facenet_images_placeholder.get_shape( )[1] self.model_facenet_embedding_size = self.model_facenet_embeddings.get_shape( )[1] print("Classifier: loading model facenet ... OK")
def reco_face(args): save_path = "{}{}{}{}{}".format(args.save_path, args.model_dir[2:4], '_', args.dist, '/') if not os.path.exists(save_path): os.makedirs(save_path) with tf.device('/gpu:0'): with tf.Graph().as_default(): config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: file_paths = args.img_path imgs_list = os.listdir(file_paths) all_img = len(os.listdir(file_paths)) people_sum = 0 img_list = [] emb_list = [] facenet.load_model(args.model_dir) # facenet.load_model('20190218-164145/20190218-164145.pb') image_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] start_time = time.time() for img in imgs_list: x = len(os.listdir(save_path)) file = "{}{}".format(file_paths, img) image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = facenet.prewhiten(image) image_reshaped = image.reshape(-1, 160, 160, 3) emb_temp = np.zeros((1, embedding_size)) emb_temp[0, :] = sess.run(embeddings, feed_dict={ image_placeholder: image_reshaped, phase_train_placeholder: False })[0] if x == 0: people_sum += 1 output_peoplename = "{}{}".format(save_path, img) misc.imsave(output_peoplename, image) print("save new face") img_list.append(image_reshaped) emb_list.append(emb_temp[0, :]) else: is_exist = False for k in range(x): dist = np.dot(emb_temp[0, :], emb_list[k]) / ( np.linalg.norm(emb_temp[0, :]) * np.linalg.norm(emb_list[k])) print(' %1.4f ' % dist, end='') if (dist > args.dist): print("\n already existed as", k + 1) is_exist = True break if not is_exist: people_sum += 1 output_peoplename = "{}{}".format(save_path, img) misc.imsave(output_peoplename, image) print("save new face") emb_list.append(emb_temp[0, :]) img_list.append(image_reshaped) duration = time.time() - start_time print("detect time:", duration) return people_sum
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 = 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 main(args): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(config=config) as sess: # 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 paths, actual_issame = lfw.get_paths( os.path.expanduser(args.lfw_dir), pairs) image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths') labels_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='labels') batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') control_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='control') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') nrof_preprocess_threads = 4 image_size = (args.image_size, args.image_size) eval_input_queue = data_flow_ops.FIFOQueue( capacity=2000000, dtypes=[tf.string, tf.int32, tf.int32], shapes=[(1, ), (1, ), (1, )], shared_name=None, name=None) eval_enqueue_op = eval_input_queue.enqueue_many( [ image_paths_placeholder, labels_placeholder, control_placeholder ], name='eval_enqueue_op') image_batch, label_batch = facenet.create_input_pipeline( eval_input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder) # Load the model input_map = { 'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder } facenet.load_model(args.model, input_map=input_map) # Get output tensor embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") # coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) evaluate(sess, eval_enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, embeddings, label_batch, paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, args.distance_metric, args.subtract_mean, args.use_flipped_images, args.use_fixed_image_standardization)
height = camera.get(cv2.CAP_PROP_FRAME_HEIGHT) # float if width <= default_width: return 1 return default_width / width camera = cv2.VideoCapture(input_video) scale = cal_scale(camera, default_width) with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): 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] classifier_filename_exp = os.path.expanduser(classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) while True: last_ret, frame = camera.read() frame = cv2.resize(frame, (0, 0), fx=scale, fy=scale) if img_size is None:
def __init__(self): self.sess = tf.Session() with self.sess.as_default(): facenet.load_model(facenet_model_checkpoint)
def main_app(user_name, user_id): cam = cv2.VideoCapture(0, cv2.CAP_DSHOW) #id = input('Nhập mã nhân viên:') #name = input('Nhập tên nhân viên;') #print("Bắt đầu chụp ảnh nhân viên, nhấn q để thoát!") insertOrUpdate(user_id, user_name) sampleNum = 0 MAX_IMG = 10 MINSIZE = 20 THRESHOLD = [0.6, 0.7, 0.7] FACTOR = 0.709 FACENET_MODEL_PATH = 'src/Models/20180402-114759.pb' recog_graph = tf.Graph() recog_sess = tf.Session(graph=recog_graph) with recog_graph.as_default(): with recog_sess.as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) # Load the model print('Loading feature extraction model') facenet.load_model(FACENET_MODEL_PATH) pnet, rnet, onet = detect_face.create_mtcnn( recog_sess, "src/align") img_num = 0 while(img_num <= MAX_IMG): ret, img = cam.read() # Lật ảnh cho đỡ bị ngược img = cv2.flip(img, 1) # Kẻ khung giữa màn hình để người dùng đưa mặt vào khu vực này centerH = img.shape[0] // 2 centerW = img.shape[1] // 2 sizeboxW = 300 sizeboxH = 400 cv2.rectangle(img, (centerW - sizeboxW // 2, centerH - sizeboxH // 2), (centerW + sizeboxW // 2, centerH + sizeboxH // 2), (255, 255, 255), 5) # Đưa ảnh về ảnh xám gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Nhận diện khuôn mặt # faces = detector.detectMultiScale(gray, 1.3, 5) bounding_boxes, _ = detect_face.detect_face( img, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR) faces = bounding_boxes[:, 0:4] for (x1, y1, x2, y2) in faces: x1 = int(x1) x2 = int(x2) y1 = int(y1) y2 = int(y2) img_num += 1 # Vẽ hình chữ nhật quanh mặt nhận được cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2) sampleNum = sampleNum + 1 # Ghi dữ liệu khuôn mặt vào thư mục dataSet if not path.isdir(os.getcwd() + "/Dataset/FaceData/processed/" + user_id): os.makedirs( os.getcwd() + "/Dataset/FaceData/processed/" + user_id) cv2.imwrite(os.getcwd() + "/Dataset/FaceData/processed/" + user_id + "/User." + user_id + '.' + str(sampleNum) + ".jpg", gray[y1:y2, x1:x2]) cv2.imshow('frame', img) # Check xem có bấm q hoặc trên 100 ảnh sample thì thoát if cv2.waitKey(100) & 0xFF == ord('q'): break elif sampleNum > 100: break cam.release() cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--path', help='Path of the video you want to test on.', default=0) args = parser.parse_args() MINSIZE = 20 THRESHOLD = [0.6, 0.7, 0.7] FACTOR = 0.709 IMAGE_SIZE = 182 INPUT_IMAGE_SIZE = 160 CLASSIFIER_PATH = './Models/custom/custom.pkl' VIDEO_PATH = args.path FACENET_MODEL_PATH = './Models/facenet/20180402-114759.pb' # Load The Custom Classifier with open(CLASSIFIER_PATH, 'rb') as file: model, class_names = pickle.load(file) with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): # Load the model print('Loading feature extraction model') facenet.load_model(FACENET_MODEL_PATH) update_bool(True) # 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] pnet, rnet, onet = detect_face.create_mtcnn(sess, "src/align") people_detected = set() person_detected = collections.Counter() cap = cv2.VideoCapture(VIDEO_PATH) while (cap.isOpened()): ret, frame = cap.read() bounding_boxes, _ = detect_face.detect_face( frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR) faces_found = bounding_boxes.shape[0] try: if faces_found > 0: det = bounding_boxes[:, 0:4] bb = np.zeros((faces_found, 4), dtype=np.int32) for i in range(faces_found): bb[i][0] = det[i][0] bb[i][1] = det[i][1] bb[i][2] = det[i][2] bb[i][3] = det[i][3] cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :] scaled = cv2.resize( cropped, (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE), interpolation=cv2.INTER_CUBIC) scaled = facenet.prewhiten(scaled) scaled_reshape = scaled.reshape( -1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3) feed_dict = { images_placeholder: scaled_reshape, phase_train_placeholder: False } emb_array = 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] best_name = class_names[best_class_indices[0]] print("Name: {}, Probability: {}".format( best_name, best_class_probabilities)) cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2) text_x = bb[i][0] text_y = bb[i][3] + 20 if best_class_probabilities > 0.3: name = class_names[best_class_indices[0]] else: name = "Unknown" cv2.putText(frame, name, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), thickness=1, lineType=2) cv2.putText(frame, str( round(best_class_probabilities[0], 3)), (text_x, text_y + 17), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), thickness=1, lineType=2) person_detected[best_name] += 1 is_open_to_speak = read_bool() # print(is_open_to_speak) if is_open_to_speak == '1': try: update_bool(False) subprocess.Popen([ 'python', 'text_to_speech.py', best_name ]) except Exception as e: print(e) print("Error: unable to start thread") except Exception as e: print(e) cv2.imshow('Face Recognition', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: # Read the file containing the pairs used for testing # 1. 读入之前的pairs.txt文件 # 读入后如[['Abel_Pacheco','1','4']] pairs = lfw.read_pairs(os.path.expanduser( args.lfw_pairs)) # 剪裁好了的图片 # Get the paths for the corresponding images # 获取文件路径和是否匹配关系对 paths, actual_issame = lfw.get_paths( os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) print("paths shape =", len(paths)) # 12000 print("paths=", paths[0:200]) print('actual_issame shape=', len(actual_issame)) # 6000 print('actual_issame', actual_issame[0:200]) # Load the 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") #image_size = images_placeholder.get_shape()[1] # For some reason this doesn't work for frozen graphs image_size = args.image_size embedding_size = embeddings.get_shape()[1] # 128 # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') batch_size = args.lfw_batch_size nrof_images = len(paths) # 12000 nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) # 12000* 128 for i in range(nrof_batches): 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) # 输出调试信息 embeddings1 = emb_array[0::2] # 6000张图片 是每一个Paris中的第一张 embeddings2 = emb_array[1::2] # 6000张图片 是每一个Paris中的第2张 diff = np.subtract(embeddings1, embeddings2) # 每一个Paris中的两个向量相减 dist = np.sum(np.square(diff), 1) # 向量二范数的平方,两个向量之间距离的平方。 每一个Pari之间的欧几里得距离的平方 # #也可以说是两张图片之间特征向量的相似度 print( '------------------------------------------------------------') print('dist.len=', len(dist)) # 把特征向量保存到文件中去 ,由于这里处理的数据不是很靠谱,所以,这里输出的特征无法直接用于聚类处理 f = open( r'E:\MyPythonProjects\HWDB1\train1\pairs/embeddingsOfChinesepairs.txt', 'w') for embedding in emb_array: for data in embedding: f.write(str(data) + ' ') f.write('\n') f.close() # 把误差保存到文件中去 f = open( r'E:\MyPythonProjects\HWDB1\train1\pairs/distInChinesePairs.txt', 'w') for d in dist: f.write(str(d) + '\n') f.close() # *************************************************************************** tpr, fpr, accuracy, val, val_std, far = lfw.evaluate( emb_array, actual_issame, nrof_folds=args.lfw_nrof_folds) print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy))) print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) auc = metrics.auc(fpr, tpr) print('Area Under Curve (AUC): %1.3f' % auc) eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.) print('Equal Error Rate (EER): %1.3f' % eer)
def detect_frame(dist_thre, capture_count, nrof_successfully_aligned, img, img_list, emb_list, file_paths, minsize, threshold, factor, save_path): output_dir_img = './datasets/mtcnn_160/img/' if not os.path.exists(output_dir_img): os.makedirs(output_dir_img) with tf.device('/gpu:0'): with tf.Graph().as_default(): config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: if len(file_paths) == 3: image_pnet = tf.placeholder(tf.float32, [None, None, None, 3]) pnet = PNet({'data': image_pnet}, mode='test') out_tensor_pnet = pnet.get_all_output() image_rnet = tf.placeholder(tf.float32, [None, 24, 24, 3]) rnet = RNet({'data': image_rnet}, mode='test') out_tensor_rnet = rnet.get_all_output() image_onet = tf.placeholder(tf.float32, [None, 48, 48, 3]) onet = ONet({'data': image_onet}, mode='test') out_tensor_onet = onet.get_all_output() saver_pnet = tf.train.Saver([ v for v in tf.global_variables() if v.name[0:5] == "pnet/" ]) saver_rnet = tf.train.Saver([ v for v in tf.global_variables() if v.name[0:5] == "rnet/" ]) saver_onet = tf.train.Saver([ v for v in tf.global_variables() if v.name[0:5] == "onet/" ]) saver_pnet.restore(sess, file_paths[0]) def pnet_fun(img): return sess.run(out_tensor_pnet, feed_dict={image_pnet: img}) saver_rnet.restore(sess, file_paths[1]) def rnet_fun(img): return sess.run(out_tensor_rnet, feed_dict={image_rnet: img}) saver_onet.restore(sess, file_paths[2]) def onet_fun(img): return sess.run(out_tensor_onet, feed_dict={image_onet: img}) else: saver = tf.train.import_meta_graph(file_paths[0]) saver.restore(sess, file_paths[1]) def pnet_fun(img): return sess.run( ('softmax/Reshape_1:0', 'pnet/conv4-2/BiasAdd:0'), feed_dict={'Placeholder:0': img}) def rnet_fun(img): return sess.run(('softmax_1/softmax:0', 'rnet/conv5-2/rnet/conv5-2:0'), feed_dict={'Placeholder_1:0': img}) def onet_fun(img): return sess.run(('softmax_2/softmax:0', 'onet/conv6-2/onet/conv6-2:0', 'onet/conv6-3/onet/conv6-3:0'), feed_dict={'Placeholder_2:0': img}) # Add a random key to the filename to allow alignment using multiple processes random_key = np.random.randint(0, high=99999) output_dir_bbox = './datasets/mtcnn_160/bbox/' if not os.path.exists(output_dir_bbox): os.makedirs(output_dir_bbox) bounding_boxes_filename = os.path.join( output_dir_bbox, 'bounding_boxes_%05d.txt' % random_key) with open(bounding_boxes_filename, "w") as text_file: start_time = time.time() rectangles, points = detect_face(img, minsize, pnet_fun, rnet_fun, onet_fun, threshold, factor) duration = time.time() - start_time print("detect time:", duration) print(type(rectangles)) nrof_faces = rectangles.shape[0] if nrof_faces > 0: facenet.load_model( '20180408-102900/20180408-102900.pb') # facenet.load_model('20190218-164145/20190218-164145.pb') image_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] det = rectangles[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: for i in range(nrof_faces): det_arr.append(np.squeeze(det[i])) else: det_arr.append(np.squeeze(det)) for i, det in enumerate(det_arr): output_filename = "{}{}{}".format( output_dir_img, capture_count, '.png') det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - 32 / 2, 0) bb[1] = np.maximum(det[1] - 32 / 2, 0) bb[2] = np.minimum(det[2] + 32 / 2, img_size[1]) bb[3] = np.minimum(det[3] + 32 / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize(cropped, (160, 160), interp='bilinear') scaled = cv2.cvtColor(scaled, cv2.COLOR_BGR2RGB) image = facenet.prewhiten(scaled) image_reshaped = image.reshape(-1, 160, 160, 3) emb_temp = np.zeros((1, embedding_size)) emb_temp[0, :] = sess.run( embeddings, feed_dict={ image_placeholder: image_reshaped, phase_train_placeholder: False })[0] if len(os.listdir(output_dir_img)) == 0: nrof_successfully_aligned += 1 output_peoplename = "{}{}{}".format( output_dir_img, nrof_successfully_aligned, '.png') misc.imsave(output_peoplename, scaled) print("\n save new.") img_list.append(image_reshaped) emb_list.append(emb_temp[0, :]) else: x = len(os.listdir(output_dir_img)) is_exist = False print(i + 1, 'face in capture', capture_count, ':') for k in range(x): dist = np.sqrt( np.sum( np.square( np.subtract( emb_temp[0, :], emb_list[k])))) print(' %1.4f ' % dist, end='') if (dist < dist_thre and dist > 0): print("\n already existed.") is_exist = True break if not is_exist: nrof_successfully_aligned += 1 output_peoplename = "{}{}{}".format( output_dir_img, nrof_successfully_aligned, '.png') misc.imsave(output_peoplename, scaled) print("\n save new.") emb_list.append(emb_temp[0, :]) img_list.append(image_reshaped) text_file.write( '%s %d %d %d %d\n' % (output_filename, bb[0], bb[1], bb[2], bb[3])) else: print('NO FACE in capture %d' % (capture_count)) text_file.write('%s\n' % (output_dir_img)) points = np.transpose(points) for rectangle in rectangles: cv2.putText(img, str(rectangle[4]), (int(rectangle[0]), int(rectangle[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) cv2.rectangle(img, (int(rectangle[0]), int(rectangle[1])), (int(rectangle[2]), int(rectangle[3])), (255, 0, 0), 2) for point in points: for i in range(0, 10, 2): cv2.circle(img, (int(point[i]), int(point[i + 1])), 4, (255, 0, 255), thickness=2) cv2.imwrite(save_path + str(capture_count) + '.jpg', img) # if cv2.waitKey(0) & 0xFF == ord('q'): # cv2.destroyAllWindows() return rectangles, nrof_successfully_aligned, img_list, emb_list
def __init__(self, model): self.sess = tf.Session() with self.sess.as_default(): facenet.load_model(model)
train_set, train_indices = radar_io.get_h5dataset(abspath, 7, 'TRAIN') test_set, test_indices = radar_io.get_h5dataset(abspath, 7, 'TEST') unknown_set, unknown_indices = radar_io.get_h5dataset(abspath, 7, 'UNKONWN') num_train_class = len(train_indices) num_unknown_class = len(unknown_indices) # plt.figure() # plt.imshow(images[1,:]) # plt.show() # print('askhnauisd') with tf.Graph().as_default(): with tf.Session() as sess: model = 'models/20191104-123934' # Load the model facenet.load_model(model) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("image_paths: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") batch_size = 10 # train_set : Run forward pass to calculate embeddings emb_train =np.zeros((len(train_set), 128)) for i in range(math.ceil(len(train_set)/batch_size)): indices = range(batch_size*i, min(batch_size*i+batch_size,len(train_set))) feed_dict = {images_placeholder: train_set[indices], phase_train_placeholder: False} emb_train[indices,:] = sess.run(embeddings, feed_dict=feed_dict) # test_set : Run forward pass to calculate embeddings
liveness_graph = tf.Graph() # Load The Custom Classifier with open(CLASSIFIER_PATH, 'rb') as file: recog_model, class_names = pickle.load(file) print("Custom Classifier, Successfully loaded") recog_sess = tf.Session(graph=recog_graph) liveness_sess = tf.Session(graph=liveness_graph) with recog_graph.as_default(): with recog_sess.as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) # Load the model print('Loading feature extraction model') facenet.load_model(FACENET_MODEL_PATH) # 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] pnet, rnet, onet = detect_face.create_mtcnn(recog_sess, "src/align") people_detected = set() person_detected = collections.Counter() recog_init = tf.local_variables_initializer()
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: # Read the file containing the pairs used for testing # 1. 读入之前的pairs.txt文件 # 读入后如[['Abel_Pacheco','1','4']] pairs = lfw.read_pairs(os.path.expanduser( args.lfw_pairs)) # 剪裁好了的图片 # Get the paths for the corresponding images # 获取文件路径和是否匹配关系对 all_paths, actual_issame = lfw.get_paths( os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) paths = all_paths[0::2] print("paths shape =", len(paths)) # 12000 print("paths=", paths[0:200]) print('actual_issame shape=', len(actual_issame)) # 6000 print('actual_issame', actual_issame[0:200]) # Load the 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") # image_size = images_placeholder.get_shape()[1] # For some reason this doesn't work for frozen graphs image_size = args.image_size embedding_size = embeddings.get_shape()[1] # 128 # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') batch_size = args.lfw_batch_size nrof_images = len(paths) # 12000 nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) # 12000* 128 for i in range(nrof_batches): 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) # # 输出调试信息 # embeddings1 = emb_array[0::2] # 6000张图片 是每一个Paris中的第一张 # embeddings2 = emb_array[1::2] # 6000张图片 是每一个Paris中的第2张 # diff = np.subtract(embeddings1, embeddings2) # 每一个Paris中的两个向量相减 # dist = np.sum(np.square(diff), 1) # 向量二范数的平方,两个向量之间距离的平方。 每一个Pari之间的欧几里得距离的平方 # # #也可以说是两张图片之间特征向量的相似度 # print('------------------------------------------------------------') # print('dist.len=', len(dist)) need_embeddings = emb_array # 使用embedding的信息计算相似度。 f = open(r'E:\MyPythonProjects\captchaOneChineseTrain/result.txt', 'w') for number_of_tests in range(10000): f.write(str(number_of_tests).zfill(4) + ',') # 每个里面有13张图片 :9 10 11 12 for i in range(9, 13): emb_i = need_embeddings[(number_of_tests * 13) + i] min_index = 0 min_dist = 999999 for j in range(9): emb_j = need_embeddings[(number_of_tests * 13) + j] # 这里计算相似度使用的是欧式距离 diff = np.subtract(emb_i, emb_j) dist = np.sum(np.square(diff)) # 使用余弦相似度 # dist=np.sum(emb_i*emb_j)/(np.linalg.norm(emb_i)*np.linalg.norm(emb_j)) if dist < min_dist: min_dist = dist min_index = j f.write(str(min_index)) f.write('\n') f.close()
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)#'[datasets\\test\\yeojingoo\\2019010302124_0.jpg',...] , labels_flat: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] 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_npy(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) # 여기서 임베딩값 np.save("emb.npy", emb_array) 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) #분석 print("분석") print(model.classes_) print(model.class_count_) print(model.class_prior_) # 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) with open('models\datasets_classifier.pkl', 'rb') as file: data_list = [] while True: try: data = pickle.load(file) except EOFError: print("end") break data_list.append(data) print(data_list) print('Loaded classifier model from file "%s"' % classifier_filename_exp) print("model: ", model) #예측 predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) print(predictions) best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]#49장 이미지에 대해 이미지 순서 - 최고 예측 이미지 인덱스 print("np.arange(len(best_class_indices)): ", np.arange(len(best_class_indices))) print("best_class_probabilities: ",best_class_probabilities) 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)