def __init__(self, min_face_size=20.0, thresholds=[0.6, 0.7, 0.8], nms_thresholds=[0.7, 0.7, 0.7], device=None): # Selece t the device if device in ['gpu', 'cuda']: if not torch.cuda.is_available(): print("cuda not available, using cpu instead") self.device = torch.device('cpu') self.device = torch.device('cuda') elif device in ['cpu', 'none']: self.device = torch.device('cpu') else: self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') print("Using {}...\n".format(self.device)) self.thresholds = thresholds self.nms_thresholds = nms_thresholds self.min_face_size = min_face_size self.empty_float = torch.tensor([], dtype=torch.float, device=self.device) self.pnet = PNet().to(device=self.device).eval() self.rnet = RNet().to(device=self.device).eval() self.onet = ONet().to(device=self.device).eval()
def load(self): sess = self.m_session p_path, r_path, o_path = self.m_model_path[0], None, None if len(self.m_model_path) >= 2: r_path = self.m_model_path[1] if len(self.m_model_path) == 3: o_path = self.m_model_path[2] if p_path: with tf.variable_scope('pnet'): data = tf.placeholder(tf.float32, (None, None, None, 3), 'input') pnet = PNet({'data': data}) pnet.load(os.path.join(self.m_model_path[0], 'det1.npy'), sess) self.pnet = lambda img: sess.run( ('pnet/conv4-2/BiasAdd:0', 'pnet/prob1:0'), feed_dict={'pnet/input:0': img}) if r_path: with tf.variable_scope('rnet'): data = tf.placeholder(tf.float32, (None, 24, 24, 3), 'input') rnet = RNet({'data': data}) rnet.load(os.path.join(self.m_model_path[1], 'det2.npy'), sess) self.rnet = lambda img: sess.run( ('rnet/conv5-2/conv5-2:0', 'rnet/prob1:0'), feed_dict={'rnet/input:0': img}) if o_path: with tf.variable_scope('onet'): data = tf.placeholder(tf.float32, (None, 48, 48, 3), 'input') onet = ONet({'data': data}) onet.load(os.path.join(self.m_model_path[2], 'det3.npy'), sess) self.onet = lambda img: sess.run( ('onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'), feed_dict={'onet/input:0': img})
def test_camera(): video_capture = cv2.VideoCapture(0) pnet = PNet() rnet = RNet() onet = ONet() frame_counter = 0 boxes, landmarks = None, None while True: ret, frame = video_capture.read() if ret: if frame_counter % 10 == 0: boxes = pnet.detect(frame) if len(boxes) > 0: boxes = rnet.detect(frame, boxes) if len(boxes) > 0: boxes, landmarks = onet.detect(frame, boxes) else: boxes, landmarks = None, None if boxes is not None: draw(frame, boxes, landmarks) cv2.imshow('image', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break frame_counter += 1
def __init__(self, min_face=20, thresh=[0.6, 0.7, 0.7], scale=0.79, stride=2, cellsize=12, use_cuda=True): self.min_face = min_face self.thresh = thresh self.scale = scale self.stride = stride self.cellsize = cellsize self.pnet = PNet() self.rnet = RNet() self.onet = ONet() self._load_state(self.pnet) self._load_state(self.rnet) self._load_state(self.onet) if cuda.is_available() and use_cuda: self.pnet.cuda() self.rnet.cuda() self.onet.cuda() self.pnet.eval() self.rnet.eval() self.onet.eval() self.use_cuda = use_cuda
def export_to_pb(): pnet = PNet() rnet = RNet() onet = ONet() pnet.export_to_pb() rnet.export_to_pb() onet.export_to_pb()
def main(): global w_emb, c_emb, initial_learning_rate with open(w_emb, 'rb') as handle: w_emb = pickle.load(handle) with open(c_emb, 'rb') as handle: c_emb = pickle.load(handle) train, val = get_batch() handle = tf.placeholder(tf.string, shape=[]) iterator = tf.data.Iterator.from_string_handle(handle, train.output_types, train.output_shapes) result = [] current_best_loss = 20 model = RNet(iterator, w_emb[1], c_emb[1]) learning_rate = initial_learning_rate print("start training...") print("save every " +str(save_freq)+" iterations") print("check loss every " +str(learning_rate_change_freq)+" iterations") with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=500) train_handle = sess.run(train.make_one_shot_iterator().string_handle()) val_handle = sess.run(val.make_one_shot_iterator().string_handle()) sess.run(tf.assign(model.is_train, tf.constant(True, dtype=tf.bool))) sess.run(tf.assign(model.lr, tf.constant(learning_rate))) for x in range(1, iterations + 1): loss, _ = sess.run([model.loss, model.train_op], feed_dict={handle: train_handle}) if x % learning_rate_change_freq == 0: sess.run(tf.assign(model.is_train, tf.constant(False, dtype=tf.bool))) val_loss, _ = sess.run([model.loss, model.train_op], feed_dict={ handle: val_handle}) sess.run(tf.assign(model.is_train, tf.constant(True, dtype=tf.bool))) if val_loss < current_best_loss: current_best_loss = val_loss else: print("learning rate changed") learning_rate *= 0.5 sess.run(tf.assign(model.lr, tf.constant(learning_rate))) result.append((val_loss,loss)) if x % save_freq == 0: filename = os.path.join("./model", "model_{}.ckpt".format(x)) saver.save(sess, filename)
def test_img(): pnet = PNet() rnet = RNet() onet = ONet() img = cv2.imread('C:\\Users\\lenovo\\Desktop\\0_Parade_Parade_0_693.jpg') boxes = pnet.detect(img) boxes = rnet.detect(img, boxes) boxes, landmarks = onet.detect(img, boxes) draw(img, boxes, landmarks) cv2.imshow('image', img) cv2.waitKey()
def main(argv): global rf, w_emb_d, c_emb_d, model_d with open(w_emb_d, 'rb') as handle: w_emb = pickle.load(handle) with open(c_emb_d, 'rb') as handle: c_emb = pickle.load(handle) test = make_example(argv[0]) model = RNet(tf.data.TFRecordDataset(rf).map( parser()).repeat().batch(batch_size).make_one_shot_iterator(), w_emb[1], c_emb[1], trainable=False) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, model_d) sess.run(tf.assign(model.is_train, tf.constant(False, dtype=tf.bool))) final = [] for _ in range(len(test) // batch_size + 1): ys = model.ys qa_id, _, y1, y2 = sess.run( [model.qa_id, model.loss, ys[0], ys[1]]) qq = [] for qid, p1, p2 in zip(qa_id.tolist(), y1.tolist(), y2.tolist()): qq.append( (test[str(qid)]["uuid"], test[str(qid)]["spans"][p1][0], test[str(qid)]["spans"][p2][1] - 1)) final.append(qq) f = open(argv[1], 'w') w = csv.writer(f) w.writerow(['id', 'answer']) count = 0 for l in final: for k in l: if count < len(test): w.writerow([ k[0], " ".join(str(x) for x in range(k[1], k[2] + 1)) ]) print(k) count += 1 f.close()
def __init__(self): use_cuda = torch.cuda.is_available() if use_cuda: self.device = 'cuda' self.tensor = torch.cuda.FloatTensor else: self.device = 'cpu' self.tensor = torch.FloatTensor self._pnet = PNet().to(self.device).eval() self._rnet = RNet().to(self.device).eval() self._onet = ONet().to(self.device).eval() self.scales = [0.3, 0.15, 0.07, 0.035] self.thresholds = [0.7, 0.8, 0.9] self.nms_thresholds = [0.7, 0.7, 0.7]
def test(cfg): logging.info('Model is loading...') with open(cfg['dev_eval_file'], "r") as fh: dev_eval_file = json.load(fh) dev_dataset = SQuADDataset(cfg['dev_record_file'], -1, cfg['batch_size'], cfg['word2ind_file']) model_args = pickle.load(open(cfg['args_filename'], 'rb')) model = RNet(**model_args) model.load_state_dict(torch.load(cfg['dump_filename'])) model.to(device) metrics, answer_dict = evaluation(model, dev_dataset, dev_eval_file, len(dev_dataset)) with open('logs/answers.json', 'w') as f: json.dump(answer_dict, f) logging.info("TEST loss %f F1 %f EM %f\n", metrics["loss"], metrics["f1"], metrics["exact_match"])
def __init__(self, min_face=20, thresh=[0.6, 0.7, 0.7], scale=0.79, stride=2, cellsize=12): self.min_face = min_face self.thresh = thresh self.scale = scale self.stride = stride self.cellsize = cellsize self.pnet = PNet() self.rnet = RNet() self.onet = ONet() self._load_state(self.pnet) self._load_state(self.rnet) self._load_state(self.onet)
def train(args): if torch.cuda.is_available(): device = torch.device("cuda") torch.cuda.set_device(args.cuda) else: device = torch.device("cpu") if args.net == "pnet": model = PNet(device) elif args.net == "rnet": model = RNet() elif args.net == "onet": model = ONet() else: raise Exception("Net Type Error!") loss_func = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), args.lr, args.momentum) transformed_data = WIDER_Dataset( data_path, anno_filename, transforms.Compose([Resize((12, 12)), Normalize(), To_Tensor()])) trainloader = DataLoader(transformed_data, batch_size=1, shuffle=True, collate_fn=transformed_data.collate_fn, num_workers=4, pin_memory=True) #model.to(device=device) for epoch in range(args.epoch): model.train() for i_batch, (images, boxes) in enumerate(trainloader): images.type(torch.DoubleTensor) images.to(device=device) boxes[0].to(device=device, dtype=torch.float) output = model(images) ptint(output.cpu())
full_size = opt.full_size print('===> Loading datasets') train_set = get_training_set(opt.upscale_factor, opt.full_size) test_set = get_test_set(opt.upscale_factor, opt.full_size) training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True) testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=opt.testBatchSize, shuffle=True) print('===> Building model') model = RNet(upscale_factor=opt.upscale_factor, full_size=opt.full_size) model.to(device) criterion = nn.MSELoss() #Three optimizers, one for each output optimizerLow = optim.Adam(model.parameters(), lr=opt.lr) optimizerInt1 = optim.Adam(model.parameters(), lr=opt.lr) optimizerInt2 = optim.Adam(model.parameters(), lr=opt.lr) def train(epoch): low_loss = 0 int1_loss = 0 int2_loss = 0 for iteration, batch in enumerate(training_data_loader, 1): inimg, int1, int2, target = batch[0].to(device), batch[1].to( device), batch[2].to(device), batch[3].to(device)
def train(model_params, launch_params): with open(launch_params['word_emb_file'], "r") as fh: word_mat = np.array(json.load(fh), dtype=np.float32) with open(launch_params['char_emb_file'], "r") as fh: char_mat = np.array(json.load(fh), dtype=np.float32) with open(launch_params['train_eval_file'], "r") as fh: train_eval_file = json.load(fh) with open(launch_params['dev_eval_file'], "r") as fh: dev_eval_file = json.load(fh) writer = SummaryWriter(os.path.join(launch_params['log'], launch_params['prefix'])) lr = launch_params['learning_rate'] base_lr = 1.0 warm_up = launch_params['lr_warm_up_num'] model_params['word_mat'] = word_mat model_params['char_mat'] = char_mat logging.info('Load dataset and create model.') dev_dataset = SQuADDataset(launch_params['dev_record_file'], launch_params['test_num_batches'], launch_params['batch_size'], launch_params['word2ind_file']) if launch_params['fine_tuning']: train_dataset = SQuADDataset(launch_params['train_record_file'], launch_params['fine_tuning_steps'], launch_params['batch_size'], launch_params['word2ind_file']) model_args = pickle.load(open(launch_params['args_filename'], 'rb')) model = RNet(**model_args) model.load_state_dict(torch.load(launch_params['dump_filename'])) model.to(device) else: train_dataset = SQuADDataset(launch_params['train_record_file'], launch_params['num_steps'], launch_params['batch_size'], launch_params['word2ind_file']) model = RNet(**model_params).to(device) launch_params['fine_tuning_steps'] = 0 params = filter(lambda param: param.requires_grad, model.parameters()) optimizer = optim.Adam(params, lr=base_lr, betas=(launch_params['beta1'], launch_params['beta2']), eps=1e-7, weight_decay=3e-7) cr = lr / log2(warm_up) scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda ee: cr * log2(ee + 1) if ee < warm_up else lr) qt = False logging.info('Start training.') for iter in range(launch_params['num_steps']): try: passage_w, passage_c, question_w, question_c, y1, y2, ids = train_dataset[iter] passage_w, passage_c = passage_w.to(device), passage_c.to(device) question_w, question_c = question_w.to(device), question_c.to(device) y1, y2 = y1.to(device), y2.to(device) loss, p1, p2 = model.train_step([passage_w, passage_c, question_w, question_c], y1, y2, optimizer, scheduler) if iter % launch_params['train_interval'] == 0: logging.info('Iteration %d; Loss: %f', iter+launch_params['fine_tuning_steps'], loss) writer.add_scalar('Loss', loss, iter+launch_params['fine_tuning_steps']) if iter % launch_params['train_sample_interval'] == 0: start = torch.argmax(p1[0, :]).item() end = torch.argmax(p2[0, start:]).item()+start passage = train_dataset.decode(passage_w) question = train_dataset.decode(question_w) generated_answer = train_dataset.decode(passage_w[:, start:end+1]) real_answer = train_dataset.decode(passage_w[:, y1[0]:y2[0]+1]) logging.info('Train Sample:\n Passage: %s\nQuestion: %s\nOriginal answer: %s\nGenerated answer: %s', passage, question, real_answer, generated_answer) if iter % launch_params['test_interval'] == 0: metrics, _ = evaluation(model, dev_dataset, dev_eval_file, launch_params['test_num_batches']) logging.info("TEST loss %f F1 %f EM %f", metrics['loss'], metrics['f1'], metrics['exact_match']) writer.add_scalar('Test_loss', metrics['loss'], iter) writer.add_scalar('Test_f1', metrics['f1'], iter) writer.add_scalar('Test_em', metrics['exact_match'], iter) except RuntimeError as e: logging.error(str(e)) except KeyboardInterrupt: break torch.save(model.cpu().state_dict(), launch_params['dump_filename']) pickle.dump(model_params, open(launch_params['args_filename'], 'wb')) logging.info('Model has been saved.')
from model import detect_faces, show_bboxes, PNet, RNet, ONet from PIL import Image import numpy as np def test(filename, save_name, model): image = Image.open(filename) bounding_boxes, landmarks = detect_faces(image, model) show_bboxes(image, bounding_boxes, facial_landmarks=landmarks).save(save_name) if __name__ == "__main__": pnet = PNet() rnet = RNet() onet = ONet() model = (pnet, rnet, onet) # test("test_1.jpg", "1.jpg", model) # test("test_2.jpg", "2.jpg", model) # test("test_3.jpg", "3.jpg", model) test("test.jpg", "1.jpg", model)
default='data/train_data.pkl', help='Train Set', type=str) parser.add_argument('--valid_data', default='data/valid_data.pkl', help='Validation Set', type=str) # parser.add_argument('model', help='Model to evaluate', type=str) args = parser.parse_args() print('Creating the model...', end='') word_vector_dim = args.word_vector_dim model = RNet(hdim=args.hdim, dropout_rate=args.dropout, N=None, M=None, word2vec_dim=word_vector_dim, char_level_embeddings=args.char_level_embeddings) print('Done!') print('Compiling Keras model...', end='') optimizer_config = { 'class_name': args.optimizer, 'config': { 'lr': args.lr } if args.lr else {} } model.compile(optimizer=optimizer_config, loss=args.loss, metrics=['accuracy']) print('Done!') print('Loading datasets...', end='')
parser.add_argument('--train_data', default='data/train_data.pkl', help='Train Set', type=str) parser.add_argument('--valid_data', default='data/valid_data.pkl', help='Validation Set', type=str) args = parser.parse_args() print('Creating the model...', end='') model = RNet(hdim=args.hdim, dropout_rate=args.dropout, N=None, M=None, char_level_embeddings=args.char_level_embeddings) print('Done!') print('Compiling Keras model...', end='') optimizer_config = { 'class_name': args.optimizer, 'config': { 'lr': args.lr } if args.lr else {} } model.compile(optimizer=optimizer_config, loss=args.loss, metrics=['accuracy']) print('Done!') print('Loading datasets...', end='')