def __init__(self): # Load models self.recognition_graph = tf.Graph() self.sess = tf.Session(graph=self.recognition_graph) print('Loading feature extraction model') with self.sess.as_default(): with self.recognition_graph.as_default(): facenet.load_model(BASE_DIR + PATH_TO_CKPT)
def __init__(self): # Load models self.recognition_graph = tf.Graph() config = tf.ConfigProto(device_count={'GPU': 1}) #config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.3 self.sess = tf.Session(graph=self.recognition_graph, config=config) #print('Loading feature extraction model') with self.sess.as_default(): with self.recognition_graph.as_default(): facenet.load_model(BASE_DIR + PATH_TO_CKPT)
def __init__(self,\ PATH_TO_CKPT = 'model/20170512-110547.pb',\ input_image_size = 160,\ BASE_DIR = os.path.dirname(__file__)): # Load models self.recognition_graph = tf.Graph() self.sess = tf.Session(graph=self.recognition_graph) print('Loading feature extraction model') with self.sess.as_default(): with self.recognition_graph.as_default(): facenet.load_model(os.path.join(BASE_DIR, PATH_TO_CKPT))
def __init__(self): # Load models self.recognition_graph = tf.Graph() self.sess = tf.Session(config=tf.ConfigProto( gpu_options=tf.GPUOptions(allow_growth=True), log_device_placement=False), graph=self.recognition_graph) print('Loading feature extraction model') with self.sess.as_default(): with self.recognition_graph.as_default(): facenet.load_model(BASE_DIR + PATH_TO_CKPT)
def __init__(self, model_path=None): self.model = None if model_path is None: return elif model_path == 'default': model_path = BASE_DIR+PATH_TO_CKPT # Load models self.recognition_graph = tf.Graph() self.sess = tf.Session(graph=self.recognition_graph) print('Loading feature extraction model') with self.sess.as_default(): with self.recognition_graph.as_default(): facenet.load_model(BASE_DIR + PATH_TO_CKPT)
def face2database(picture_path, model_path, database_path, batch_size=90, image_size=160): # 提取特征到数据库 # picture_path为人脸文件夹的所在路径 # model_path为facenet模型路径 # database_path为人脸数据库路径 with tf.Graph().as_default(): with tf.Session() as sess: dataset = facenet.get_dataset(picture_path) 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 = 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) np.savez(database_path, emb=emb_array, lab=labels) print("数据库特征提取完毕!")
def __init__(self, sess, data_path='output', model='20180402-114759', classifier_name='result1'): self.sess = sess self.dataset = facenet.get_dataset(data_path) # 얼굴영역만 저장된 폴더들의 경로를 가져온다. self.class_names = [cls.name.replace('_', ' ') for cls in self.dataset] # Dataset 폴더 내 각각의 폴더(Class)명 self.paths, self.labels = facenet.get_image_paths_and_labels(self.dataset) # 이미지들의 경로 및 Class 명 print('Number of classes: %d' % len(self.dataset)) print('Number of images: %d' % len(self.paths)) print('Loading feature extraction model') model_path = 'model/{}/{}.pb'.format(model, model) # 네트워크 모델(얼굴인식)이 저장된 경로 facenet.load_model(model_path) self.model = SVC(kernel='linear', probability=True) # 분류모델 생성 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] classifier_path = 'classifier/classifier_{0}.pkl'.format(classifier_name) # 분류모델이 저장될 이름 및 경로 self.classifier_path = os.path.expanduser(classifier_path) self.batch_size = 1000 self.image_size = 160
def detect(opt, save_img=False): out, source, weights, view_img, save_txt, imgsz, facenet_model_path, svc_path, database_path = \ opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size, opt.facenet_model_path, opt.svc_path, opt.database_path webcam = source == '0' or source.startswith('rtsp') or source.startswith( 'http') or source.endswith('.txt') # Initialize device = torch_utils.select_device(opt.device) if os.path.exists(out): shutil.rmtree(out) # delete output folder os.makedirs(out) # make new output folder half = device.type != 'cpu' # half precision only supported on CUDA # Load model google_utils.attempt_download(weights) model = torch.load(weights, map_location=device)['model'].float() # load to FP32 # torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning # model.fuse() model.to(device).eval() if half: model.half() # to FP16 # Second-stage classifier classify = False if classify: modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize modelc.load_state_dict( torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights modelc.to(device).eval() # Set Dataloader vid_path, vid_writer = None, None if webcam: view_img = True cudnn.benchmark = True # set True to speed up constant image size inference dataset = LoadStreams(source, img_size=imgsz) else: save_img = True dataset = LoadImages(source, img_size=imgsz) # Get names and colors names = model.names if hasattr(model, 'names') else model.modules.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] # Run inference t0 = time.time() img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img _ = model(img.half() if half else img ) if device.type != 'cpu' else None # run once # ************************************************************************ with tf.Graph().as_default(): with tf.Session() as sess: # Load the model print('Loading feature extraction model') facenet.load_model(facenet_model_path) with open(svc_path, 'rb') as infile: (classifymodel, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % svc_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] Database = np.load(database_path) corpbbox = None # ************************************************************ for path, img, im0s, vid_cap in dataset: img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 if img.ndimension() == 3: img = img.unsqueeze(0) # Inference t1 = torch_utils.time_synchronized() pred = model(img, augment=opt.augment)[0] # Apply NMS pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms) t2 = torch_utils.time_synchronized() # Apply Classifier if classify: pred = apply_classifier(pred, modelc, img, im0s) # Process detections for i, det in enumerate(pred): # detections per image if webcam: # batch_size >= 1 p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() else: p, s, im0 = path, '', im0s # ****************************** image = Image.fromarray( cv2.cvtColor(im0, cv2.COLOR_BGR2RGB)) image = np.array(image) img_size = np.array(image.shape)[0:2] # ******************************** save_path = str(Path(out) / Path(p).name) s += '%gx%g ' % img.shape[2:] # print string gn = torch.tensor( im0.shape)[[1, 0, 1, 0]] # normalization gain whwh if det is not None and len(det): # Rescale boxes from img_size to im0 size det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() # Print results for c in det[:, -1].unique(): n = (det[:, -1] == c).sum() # detections per class s += '%g %ss, ' % (n, names[int(c)] ) # add to string # Write results for *xyxy, conf, cls in det: if save_txt: # Write to file xywh = ( xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh with open( save_path[:save_path.rfind('.')] + '.txt', 'a') as file: file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format if save_img or view_img: # Add bbox to image # *************************************************** x1 = np.maximum(int(xyxy[0]) - 16, 0) y1 = np.maximum(int(xyxy[1]) - 16, 0) x2 = np.minimum(int(xyxy[2]) + 16, img_size[1]) y2 = np.minimum(int(xyxy[3]) + 16, img_size[0]) crop_img = image[y1:y2, x1:x2] scaled = np.array( Image.fromarray(crop_img).resize( (160, 160))) # scaled = misc.imresize(crop_img, (160, 160), interp='bilinear') img = load_image(scaled, False, False, 160) img = np.reshape(img, (-1, 160, 160, 3)) feed_dict = { images_placeholder: img, phase_train_placeholder: False } embvecor = sess.run(embeddings, feed_dict=feed_dict) embvecor = np.array(embvecor) # 利用SVM对人脸特征进行分类 predictions = classifymodel.predict_proba( embvecor) best_class_indices = np.argmax(predictions, axis=1) tmp_lable = class_names[best_class_indices] best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] print(class_names, predictions) if best_class_probabilities < 0.95: tmp_lable = "others" print(tmp_lable) # *************************************************** # label = '%s %.2f' % (names[int(cls)], conf) label = '%s %.2f' % (tmp_lable, best_class_probabilities) plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) # Print time (inference + NMS) print('%sDone. (%.3fs)' % (s, t2 - t1)) cv2.imshow("", im0) cv2.waitKey(5) # *********************************************************** # ************************************************************************ # Stream results if view_img: cv2.imshow(p, im0) if cv2.waitKey(1) == ord('q'): # q to quit raise StopIteration # Save results (image with detections) if save_img: if dataset.mode == 'images': cv2.imwrite(save_path, im0) else: if vid_path != save_path: # new video vid_path = save_path if isinstance(vid_writer, cv2.VideoWriter): vid_writer.release( ) # release previous video writer fps = vid_cap.get(cv2.CAP_PROP_FPS) w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) vid_writer = cv2.VideoWriter( save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h)) vid_writer.write(im0) if save_txt or save_img: print('Results saved to %s' % os.getcwd() + os.sep + out) if platform == 'darwin': # MacOS os.system('open ' + save_path) print('Done. (%.3fs)' % (time.time() - t0))
import numpy as np import scipy.misc import cv2 from recognition import facenet image_size = 160 #don't need equal to real image size, but this value should not small than this modeldir = 'recognition/model/20180402-114759.pb' #change to your model dir image_name1 = 'media/test3.JPG' #change to your image name image_name2 = 'media/test4.JPG' #change to your image name image_name3 = 'media/test1.JPG' #change to your image name print('facenet embedding') tf.Graph().as_default() sess = tf.Session() facenet.load_model(modeldir) images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] print('facenet embedding') scaled_reshape = [] image1 = scipy.misc.imread(image_name1, mode='RGB') image1 = cv2.resize(image1, (image_size, image_size), interpolation=cv2.INTER_CUBIC) image1 = facenet.prewhiten(image1) scaled_reshape.append(image1.reshape(-1,image_size,image_size,3)) emb_array1 = np.zeros((1, embedding_size)) emb_array1[0, :] = sess.run(embeddings, feed_dict={images_placeholder: scaled_reshape[0], phase_train_placeholder: False })[0]
''' Prints all tensors in a frozen graph ''' import tensorflow as tf import argparse from recognition import facenet parser = argparse.ArgumentParser() parser.add_argument("--frozen_graph", default="recognition/model/20180402-114759.pb", help="Frozen graph to use.") args = parser.parse_args() with tf.Graph().as_default() as graph: facenet.load_model(args.frozen_graph) nodes = [ n.name for n in graph.as_graph_def().node if not (n.name.startswith("InceptionResnetV1")) ] #or n.name.startswith("MobilenetV2"))] print("\n".join(nodes)) print(tf.contrib.graph_editor.get_tensors(tf.get_default_graph()))