def handle_data(): global frameCount global filePath global fileName if request.method == 'POST': if request.form.get('pcaselect'): use_eigenface = True use_lbp = False model = "none" loss = "none" pcaselect = "yes" lbpselect = "no" elif request.form.get('lbpselect'): use_eigenface = False use_lbp = True model = "none" loss = "none" pcaselect = "no" lbpselect = "yes" else: use_eigenface = False use_lbp = False pcaselect = "no" lbpselect = "no" model = request.form['model'] loss = request.form['loss'] model_pth = 'backbone/{}_{}.pth'.format(model, loss) if model == '10': args.backbone = 'spherenet10' elif model == '20': args.backbone = 'spherenet20' elif model == '64': args.backbone = 'spherenet64' netModel = CreateModel(args) netModel.backbone.load_state_dict(torch.load(model_pth)) if 'file' not in request.files: if frameCount > 1: image = Image.open(filePath) landmarks = get_landmarks(image) aligned_face = get_alignedface(image, landmarks) aligned_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'aligned_face.jpg') cv2.imwrite(aligned_face_path, aligned_face) if use_eigenface: recon_face, eigenface_result = eigenface_evaluation( aligned_face) recon_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'reconstructed_face.jpg') cv2.imwrite(recon_face_path, recon_face) face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = eigenface_result elif use_lbp: lbp_face = local_binary_pattern( cv2.cvtColor(aligned_face, cv2.COLOR_BGR2GRAY), 8, 1) lbp_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'lbp_face.jpg') cv2.imwrite(lbp_face_path, lbp_face) face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = lbp_evaluation( aligned_face) else: face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = evaluation( netModel.backbone, aligned_face) face_1_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_1.jpg') face_2_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_2.jpg') face_3_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_3.jpg') cv2.imwrite(face_1_path, face_1) cv2.imwrite(face_2_path, face_2) cv2.imwrite(face_3_path, face_3) imageName = fileName.rsplit('_', 1)[0] name1 = name1.rsplit('_', 1)[0] name2 = name2.rsplit('_', 1)[0] name3 = name3.rsplit('_', 1)[0] if use_eigenface: mean_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'meanface.jpg') eigenface1 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface1.jpg') eigenface2 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface2.jpg') eigenface3 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface3.jpg') eigenface4 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface4.jpg') eigenface5 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface5.jpg') eigenface6 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface6.jpg') eigenface7 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface7.jpg') eigenface8 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface8.jpg') eigenface9 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface9.jpg') return render_template('pca_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, mean_face=mean_face_path, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, eigenface1=eigenface1, eigenface2=eigenface2, eigenface3=eigenface3, eigenface4=eigenface4, eigenface5=eigenface5, eigenface6=eigenface6, eigenface7=eigenface7, eigenface8=eigenface8, eigenface9=eigenface9, recon_face=recon_face_path, num_faces=args.num_faces) elif use_lbp: return render_template('lbp_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, lbp_face=lbp_face_path, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, num_faces=args.num_faces) else: return render_template('net_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, num_faces=args.num_faces) else: flash('No file part') return redirect(request.url) file = request.files['file'] if file.filename == '': if frameCount > 1: image = Image.open(filePath) landmarks = get_landmarks(image) aligned_face = get_alignedface(image, landmarks) aligned_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'aligned_face.jpg') cv2.imwrite(aligned_face_path, aligned_face) if use_eigenface: recon_face, eigenface_result = eigenface_evaluation( aligned_face) recon_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'reconstructed_face.jpg') cv2.imwrite(recon_face_path, recon_face) face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = eigenface_result elif use_lbp: lbp_face = local_binary_pattern( cv2.cvtColor(aligned_face, cv2.COLOR_BGR2GRAY), 8, 1) lbp_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'lbp_face.jpg') cv2.imwrite(lbp_face_path, lbp_face) face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = lbp_evaluation( aligned_face) else: face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = evaluation( netModel.backbone, aligned_face) face_1_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_1.jpg') face_2_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_2.jpg') face_3_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_3.jpg') cv2.imwrite(face_1_path, face_1) cv2.imwrite(face_2_path, face_2) cv2.imwrite(face_3_path, face_3) imageName = fileName.rsplit('_', 1)[0] name1 = name1.rsplit('_', 1)[0] name2 = name2.rsplit('_', 1)[0] name3 = name3.rsplit('_', 1)[0] # return '', 204 # return render_template('gui.html', accuracy='2', image=file_path) if use_eigenface: mean_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'meanface.jpg') eigenface1 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface1.jpg') eigenface2 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface2.jpg') eigenface3 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface3.jpg') eigenface4 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface4.jpg') eigenface5 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface5.jpg') eigenface6 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface6.jpg') eigenface7 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface7.jpg') eigenface8 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface8.jpg') eigenface9 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface9.jpg') return render_template('pca_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, mean_face=mean_face_path, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, eigenface1=eigenface1, eigenface2=eigenface2, eigenface3=eigenface3, eigenface4=eigenface4, eigenface5=eigenface5, eigenface6=eigenface6, eigenface7=eigenface7, eigenface8=eigenface8, eigenface9=eigenface9, recon_face=recon_face_path, num_faces=args.num_faces) elif use_lbp: return render_template('lbp_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, lbp_face=lbp_face_path, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, num_faces=args.num_faces) else: return render_template('net_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, num_faces=args.num_faces) # return '', 204 else: flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): frameCount = frameCount + 1 file_name = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], file_name) file.save(file_path) filePath = file_path fileName = file_name image = Image.open(file_path) landmarks = get_landmarks(image) aligned_face = get_alignedface(image, landmarks) aligned_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'aligned_face.jpg') cv2.imwrite(aligned_face_path, aligned_face) if use_eigenface: recon_face, eigenface_result = eigenface_evaluation( aligned_face) recon_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'reconstructed_face.jpg') cv2.imwrite(recon_face_path, recon_face) face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = eigenface_result elif use_lbp: lbp_face = local_binary_pattern( cv2.cvtColor(aligned_face, cv2.COLOR_BGR2GRAY), 8, 1) lbp_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'lbp_face.jpg') cv2.imwrite(lbp_face_path, lbp_face) face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = lbp_evaluation( aligned_face) else: face_1, face_2, face_3, name1, name2, name3, score1, score2, score3 = evaluation( netModel.backbone, aligned_face) face_1_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_1.jpg') face_2_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_2.jpg') face_3_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_3.jpg') cv2.imwrite(face_1_path, face_1) cv2.imwrite(face_2_path, face_2) cv2.imwrite(face_3_path, face_3) imageName = file_name.rsplit('_', 1)[0] name1 = name1.rsplit('_', 1)[0] name2 = name2.rsplit('_', 1)[0] name3 = name3.rsplit('_', 1)[0] # return '', 204 # return render_template('gui.html', accuracy='2', image=file_path) if use_eigenface: mean_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'meanface.jpg') eigenface1 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface1.jpg') eigenface2 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface2.jpg') eigenface3 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface3.jpg') eigenface4 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface4.jpg') eigenface5 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface5.jpg') eigenface6 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface6.jpg') eigenface7 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface7.jpg') eigenface8 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface8.jpg') eigenface9 = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface9.jpg') return render_template('pca_result.html', imageName=imageName, input_face=filePath, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, mean_face=mean_face_path, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, eigenface1=eigenface1, eigenface2=eigenface2, eigenface3=eigenface3, eigenface4=eigenface4, eigenface5=eigenface5, eigenface6=eigenface6, eigenface7=eigenface7, eigenface8=eigenface8, eigenface9=eigenface9, recon_face=recon_face_path, num_faces=args.num_faces) elif use_lbp: return render_template('lbp_result.html', imageName=imageName, input_face=file_path, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, lbp_face=lbp_face_path, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, num_faces=args.num_faces) else: return render_template('net_result.html', imageName=imageName, input_face=file_path, aligned_face=aligned_face_path, image1=face_1_path, name1=name1, score1=score1, image2=face_2_path, name2=name2, score2=score2, image3=face_3_path, name3=name3, score3=score3, pcaselect=pcaselect, lbpselect=lbpselect, model=model, loss=loss, num_faces=args.num_faces) else: return redirect(request.url)
eval_start_time = time.time() for step, batch in enumerate(batches): if step >= steps_per_epoch: break labels.extend(batch['label']) probs.extend(model.forward(batch['X'])) util.over_print("Step: %d/%d \tTime: %f\r" % (step+1, steps_per_epoch, time.time()-eval_start_time)) loss, roc_auc, accuracy = util.measure_performance(labels, probs) return loss, roc_auc, accuracy with tf.Session(config=gpu_config) as sess: dataloader = CreateDataLoader(opt, sess) coord = tf.train.Coordinator() dataloader.start_enqueue_threads_for_training(coord) model = CreateModel(opt, sess, pos_weight=dataloader.pos_ratio) logger = SimpleLogger(opt) total_steps = -1 best_valid_roc_auc = 0 best_test_roc_auc = [0]*len(dataloader.test_datasets) if opt.load_epoch == "best_test": load_epoch = 0 else: load_epoch = int(opt.load_epoch[-1]) for epoch in range(0, opt.nepoch): # Skip epoch to continue training from specifed load epoch if opt.continue_train and epoch < load_epoch:
def camresult(): global frameCount if request.method == 'POST': if request.form.get('pcaselect'): use_eigenface = True use_lbp = False model = "none" loss = "none" pcaselect = "yes" lbpselect = "no" elif request.form.get('lbpselect'): use_eigenface = False use_lbp = True model = "none" loss = "none" pcaselect = "no" lbpselect = "yes" else: use_eigenface = False use_lbp = False pcaselect = "no" lbpselect = "no" model = request.form['model'] loss = request.form['loss'] model_pth = 'backbone/{}_{}.pth'.format(model, loss) if model == '10': args.backbone = 'spherenet10' elif model == '20': args.backbone = 'spherenet20' elif model == '64': args.backbone = 'spherenet64' netModel = CreateModel(args) netModel.backbone.load_state_dict(torch.load(model_pth)) data_url = request.form['data'] content = data_url.split(';')[1] image_encoded = content.split(',')[1] body = base64.decodebytes(image_encoded.encode('utf-8')) webcam_img_path = os.path.join(app.config['UPLOAD_FOLDER'], 'webcam_img.jpg') with open(webcam_img_path, "wb") as fh: fh.write(body) image = Image.open(webcam_img_path) landmarks = get_landmarks(image) aligned_face = get_alignedface(image, landmarks) webcam_alignedimg_path = os.path.join(app.config['UPLOAD_FOLDER'], 'webcam_alignedimg.jpg') cv2.imwrite(webcam_alignedimg_path, aligned_face) if use_eigenface: recon_face, eigenface_result = eigenface_evaluation(aligned_face) recon_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'reconstructed_face.jpg') cv2.imwrite(recon_face_path, recon_face) face_1, face_2, face_3, name_1, name_2, name_3, score_1, score_2, score_3 = eigenface_result elif use_lbp: lbp_face = local_binary_pattern( cv2.cvtColor(aligned_face, cv2.COLOR_BGR2GRAY), 8, 1) lbp_face_path = os.path.join(app.config['UPLOAD_FOLDER'], 'lbp_face.jpg') cv2.imwrite(lbp_face_path, lbp_face) face_1, face_2, face_3, name_1, name_2, name_3, score_1, score_2, score_3 = lbp_evaluation( aligned_face) else: face_1, face_2, face_3, name_1, name_2, name_3, score_1, score_2, score_3 = evaluation( netModel.backbone, aligned_face) face_1_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_1.jpg') face_2_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_2.jpg') face_3_path = os.path.join(app.config['UPLOAD_FOLDER'], 'face_3.jpg') cv2.imwrite(face_1_path, face_1) cv2.imwrite(face_2_path, face_2) cv2.imwrite(face_3_path, face_3) name1 = name_1.rsplit('_', 1)[0] name2 = name_2.rsplit('_', 1)[0] name3 = name_3.rsplit('_', 1)[0] session['use_eigenface'] = use_eigenface session['use_lbp'] = use_lbp session['aligned_face'] = webcam_alignedimg_path session['image1'] = face_1_path session['name1'] = name1 session['score1'] = score_1 session['image2'] = face_2_path session['name2'] = name2 session['score2'] = score_2 session['image3'] = face_3_path session['name3'] = name3 session['score3'] = score_3 session['pcaselect'] = pcaselect session['lbpselect'] = lbpselect session['model'] = model session['loss'] = loss if use_eigenface: session['mean_face_path'] = os.path.join( app.config['UPLOAD_FOLDER'], 'meanface.jpg') session['eigenface1'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface1.jpg') session['eigenface2'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface2.jpg') session['eigenface3'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface3.jpg') session['eigenface4'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface4.jpg') session['eigenface5'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface5.jpg') session['eigenface6'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface6.jpg') session['eigenface7'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface7.jpg') session['eigenface8'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface8.jpg') session['eigenface9'] = os.path.join(app.config['UPLOAD_FOLDER'], 'eigenface9.jpg') session['recon_face_path'] = recon_face_path return redirect(url_for('result_cam')) elif use_lbp: session['lbp_face'] = lbp_face_path return redirect(url_for('result_cam')) else: return redirect(url_for('result_cam')) else: return redirect(request.url)
np.random.seed(opt.seed) random.seed(opt.seed) tf.set_random_seed(opt.seed) result_dict = { "split_0_result": None, "split_1_result": None, "split_2_result": None, "split_3_result": None, "split_4_result": None, } with tf.Session(config=gpu_config) as sess: model = CreateModel(opt, sess) data_loader = CreateDataLoader(opt, sess) coord = tf.train.Coordinator() data_loader.start_enqueue_threads_for_testing(coord) data_loader.init_test_generator() # Measure Performance on Test Datasets print("Measuring Test Loss and Accuracy...") test_roc_aucs = [] for split, dataset in enumerate(data_loader.test_datasets): if not opt.load_epoch: model.load("best_test_{}".format(split)) print("Data root: ", dataset['dataroot'])
from models.models import CreateModel from common.loader import get_loader from common.util import save_log from arguments import train_args from tqdm import tqdm import val_verification import val_identification if __name__ == '__main__': args = train_args.get_args() dataloader = get_loader(args, 'training', args.bs) model = CreateModel(args, class_num=dataloader.num_class) model.train_setup( ) # setup the optimizer, scheduler and initialization for training # model.eval() # import hiddenlayer as hl # import torch # input = torch.randn(1, 3, 64, 64) # graph = hl.build_graph(model.backbone, input) # graph = graph.build_dot() # graph.render('/Users/aaron/Desktop/spherenet.png', view=True, format='png') # from torchsummary import summary # summary(model.backbone, (3, 96, 96)) # summary(your_model, input_size=(channels, H, W)) pbar = tqdm(range(1, args.epochs + 1), ncols=0) best_acc = 0 best_auc = 0.50 for epoch in pbar: model.train()
import data_label opt = TestOptions().parse() opt.batchSize = 128 gpu_config = tf.ConfigProto(device_count={'GPU': opt.gpu_count}) dst = "./visualization/" util.mkdirs(dst) ################################################ # Acquire prediction results on all samples # ################################################ with tf.Session(config=gpu_config) as sess: model = CreateModel(opt, sess) data_loader = CreateDataLoader(opt, sess) coord = tf.train.Coordinator() data_loader.start_enqueue_threads_for_testing(coord) data_loader.init_test_generator() y_true = [] y_pred = [] step_count = 0.0 for step, batch in enumerate(data_loader.test_datasets[0]['test_batch']): probs = model.forward(batch['X']) y_true.extend(batch['label']) y_pred.extend(probs) step_count += 1 util.over_print("Step count: {} \r".format(str(step_count)))
import torch import torch.nn as nn from models.models import CreateModel from arguments import test_args import pandas as pd import test_identification import test_verification _backbone_model = '/Users/aaron/Desktop/11785-Intro to Deep Learning/Homework Part2/Homework2/src/checkpoints/0223-1/19_net_backbone.pth' _criterion_model = '/Users/aaron/Desktop/11785-Intro to Deep Learning/Homework Part2/Homework2/src/checkpoints/0223-1/19_net_criterion.pth' if __name__ == '__main__': args = test_args.get_args() model = CreateModel(args, class_num=args.class_num) model.backbone.load_state_dict(torch.load(_backbone_model)) model.criterion.load_state_dict(torch.load(_criterion_model)) if torch.cuda.is_available(): if len(args.gpu_ids) > 1: model.backbone = nn.DataParallel(model.backbone) test_identification_id, test_identification_label = test_identification.run( model, args) d = {'id': test_identification_id, 'Category': test_identification_label} df = pd.DataFrame(data=d) df.to_csv('hw2p2_identification_test.csv', header=True, index=False) print( 'Identification testing is done, result is saved to hw2p2_identification_test.csv' ) test_verification_trial, test_verification_score = test_verification.run(