def main(): if not torch.cuda.is_available(): logging.info('no gpu device available') sys.exit(1) torch.cuda.set_device(args.gpu) cudnn.benchmark = True cudnn.enabled = True logging.info('gpu device = %d' % args.gpu) logging.info("args = %s", args) genotype = eval("genotypes.%s" % args.arch) logging.info(genotype) dataset = params.datasets['ImageNet'] network_params = { 'C': args.init_channels, 'num_classes': dataset.num_classes, 'layers': args.layers, 'genotype': genotype, } model = Network(**network_params) if args.calc_flops: from thop import profile, clever_format input = torch.randn(1, dataset.num_channels, dataset.hw[0], dataset.hw[1]) flops, num_params = profile(model, inputs=(input, )) flops, num_params = clever_format([flops, num_params], "%.2f") utils.load(model, args.model_path) model = model.cuda() val_transform = data_transforms_imagenet_valid() validdir = os.path.join(args.data, 'val') valid_data = dset.ImageFolder(validdir, val_transform) valid_queue = torch.utils.data.DataLoader(valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=0) with torch.no_grad(): val_acc, infer_time = infer(valid_queue, model, args.report_freq) if args.calc_flops: logging.info( 'Validation Accuracy: %.2f%% | Number of parameters: %s | Inference time: %2.2fms | Flops: %s', val_acc, num_params, infer_time * 1000, flops) else: logging.info('Validation Accuracy: %.2f%% | Inference time: %2.2fms', val_acc, infer_time * 1000)
def upload_face_img(): # 업로드된 파일을 서버에 저장 file = request.files['file'] new_filename = file.filename save_file_path = os.path.join(app.config['INPUT_DIR'], new_filename) file.save(save_file_path) # Inference pred_vec = infer(INFER_MODEL, DETECTOR, save_file_path, FACE_DEFAULT_SHAPE, is_debug=False) if type(pred_vec) == type(None): return render_template('index.html', query_face=save_file_path, sim_face_list=[]) # 학습 데이터와 쿼리 이미지간 유사도 구하기 sim = cosine_similarity(TRAIN_VEC, pred_vec) sorted_similar = np.argsort(sim * -1, axis=0) if np.max(sim) < THRESH: return render_template('index.html', query_face=save_file_path, sim_face_list=[]) # 라벨별 유사도의 평균을 구하고, 1~3위의 라벨 구하기 TRAIN_DF['sim'] = sim TRAIN_DF.loc[TRAIN_DF['sim'] < THRESH, 'sim'] = 0 df = TRAIN_DF.groupby(['label'])['sim'].mean().reset_index() df = df.sort_values(by=['sim'], ascending=False) df = df.reset_index() sim_label_1 = df['label'].values[0] sim_label_2 = df['label'].values[1] sim_label_3 = df['label'].values[2] # 1~3위의 라벨의 이미지와 유사도 구하기 max_sim_img_1, max_sim_1 = get_max_sim_img(sim_label_1, sim, TRAIN_DF) max_sim_img_2, max_sim_2 = get_max_sim_img(sim_label_2, sim, TRAIN_DF) max_sim_img_3, max_sim_3 = get_max_sim_img(sim_label_3, sim, TRAIN_DF) face_file_list = [max_sim_img_1, max_sim_img_2, max_sim_img_3] face_file_list = [ os.path.join(app.config['FACE_DIR'], f) for f in face_file_list ] sim_list = [round(max_sim_1, 3), round(max_sim_2, 3), round(max_sim_3, 3)] return render_template('index.html', query_face=save_file_path, sim_face_list=face_file_list, sim_list=sim_list)
def infer(args: Namespace): if not args.src.endswith('/test/'): logging.warning("LMDB dataset here is expected to be a test set.") test_dataset = dataset.lmdbDataset(root=args.src, transform=dataset.ResizeNormalize( (cfg.imgW, cfg.imgH), crop=cfg.crop)) test_loader = DataLoader(test_dataset, shuffle=False, batch_size=2 * cfg.batchSize, num_workers=int(cfg.workers)) device = torch.device("cuda") codec = dataset.Codec(cfg.alphabet) nClass = len(cfg.alphabet) + 1 model = crnn.CRNN(cfg.imgH, cfg.nc, nClass, cfg.nh).to(device) print(model) para = torch.load( os.path.join("/home/chuan/captcha/crnn/weights", args.para)) model.load_state_dict(para) utils.infer(model, test_loader, device, codec) return
def _column(column_args, row_id, data_values, M_c, X_L_list, X_D_list, T, engine, numsamples): col_idx = column_args[0] confidence = column_args[1] if confidence is None or not numpy.isnan(T[row_id][col_idx]): return du.convert_code_to_value(M_c, col_idx, T[row_id][col_idx]) else: ## Do impute. Y = [(row_id, cidx, T[row_id][cidx]) for cidx in M_c['name_to_idx'].values() \ if not numpy.isnan(T[row_id][cidx])] code = utils.infer(M_c, X_L_list, X_D_list, Y, row_id, col_idx, numsamples, confidence, engine) if code is not None: # Inferred successfully! Fill in the new value. value = du.convert_code_to_value(M_c, col_idx, code) return value else: return du.convert_code_to_value(M_c, col_idx, T[row_id][col_idx])
def make_prediction(self): model_path = os.listdir("results") latest = -1 path_save = "" for m in model_path: if not m.endswith(".caffemodel"): continue if os.path.getmtime(os.path.join("results",m)) > latest: latest = os.path.getmtime(os.path.join("results",m)) path_save = m model_path = os.path.join("results", path_save) results = utils.infer(self.titleEdit.text(), proto_path="./lib/deploy.prototxt", model_path=model_path) load_classes = json.load(open("./dataset_namelist/classes.json")) sorted_prediction = np.argsort(-1. * results) sorted_results = results[sorted_prediction] sorted_classes = [load_classes[k] for k in sorted_prediction] return [[k, v] for k, v in zip(sorted_classes, sorted_results)]
vocab_file = '../Data/Dataset.dict.pkl' if __name__ == '__main__': options = parser.parse_args() if options.toy: test_dataset = Dataset(data_type='test', length=100) else: test_dataset = Dataset(data_type='test') test_loader = DataLoader(dataset=test_dataset, batch_size=options.bs, shuffle=False, collate_fn=batch_collect, drop_last=True) model = Model(options) model = model.to(device) saved_state = torch.load(options.sustain, map_location=device) model.load_state_dict(saved_state) print('Already Load Pre Train Model') dic = {} print('Load Vocab File') with open(vocab_file, 'rb') as f: dict_data = pickle.load(f) for t in tqdm(dict_data): token, index, _, _ = t dic[index] = token dic[options.total_words] = 'PAD' generation = infer(options, model, device, test_loader, 5, dic) print('It is time to save generation sentences') with open('../data/generation.pkl', 'wb') as f: pickle.dump(generation, f)
result = get_mtccn_faces(args=args, ctx=mx.gpu(args.gpu), image=frame) if result is not None: aligned_faces, bboxes = result if len(bboxes) == 0: print('no face') continue img_size = 112 margin = 0 img_h, img_w, _ = frame.shape start_time = time.time() results, score = infer(model=model, conf=conf, faces=aligned_faces, target_embs=targets, tta=False) print('Duration: {}'.format(time.time() - start_time)) # results, score = infer(model=model, conf=conf, faces=aligned_faces, target_embs=targets, tta=True) for idx, bbox in enumerate(bboxes): x1, y1, x2, y2 = bbox[0], bbox[1], bbox[2], bbox[3] xw1 = max(int(x1 - margin), 0) yw1 = max(int(y1 - margin), 0) xw2 = min(int(x2 + margin), img_w - 1) yw2 = min(int(y2 + margin), img_h - 1) bbox = [xw1, yw1, xw2, yw2] # frame = draw_box_name(bbox, names[results[idx] + 1] + '_{:.2f}'.format(score[idx]), frame) frame = draw_box_name(bbox, names[results[idx] + 1], frame) # frame = cv2.resize(frame, dsize=None ,fx=0.25, fy=0.25)
G.load_state_dict( torch.load("Checkpoints/G_22.pt", map_location=torch.device('cpu'))) GR.load_state_dict( torch.load("Checkpoints/GR_7.pt", map_location=torch.device('cpu'))) img = plt.imread(path_i) faces = detect_face(img) if len(faces) == 0: print("No faces detected") exit() resz = cv2.resize(faces[0], (100, 100)) plt.imsave("out_ld.png", resz) resz = resz.reshape(1, 100, 100, 3) resz = np.transpose(resz, (0, 3, 1, 2)) resz = torch.from_numpy(resz) resz = resz.float() inp = scale(resz) out1 = infer(G, inp) out2 = infer(GR, inp) inp = rescale(inp) out1 = rescale(out1) out2 = rescale(out2) w = 0.5 out = out1 * w + out2 * (1 - w) imsave(out[0], "out_hd.png")
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', required=True) parser.add_argument('--debug', default=False) args = parser.parse_args() directory = os.getcwd() model_name = args.model model_path = os.path.join(directory, 'model', model_name + '.tflite') debug = args.debug # Load all annotation files in .txt format ground_truth_files = os.path.join(directory, 'dataset') + '/ground_truths' ground_truths = get_ground_truth(ground_truth_files) images = glob.glob(os.path.join(directory, 'dataset') + '/images/*') total_predictions = [] total_ground_truths = [] ignored_predictions = [] ignored_ground_truths = [] count = 0 if len(ground_truths) == len(images): for image, gt in zip(images, ground_truths): count += 1 print(f'Image number : {count}/{len(images)}') debug and print("Current Image : ", image) predicted_classes = [] prediction1 = infer(model_path, image) # inference happens here # Extract classes only (already sorted by x coordinate) for key in prediction1: predicted_classes.append(key[0]) debug and print("Predicted Classes: ", predicted_classes) debug and print("Ground Truth: ", gt) # Filter out all incomplete predictions if len(predicted_classes) == 9: total_predictions.append(predicted_classes) total_ground_truths.append(gt) debug and print("===Sample Added=== \n") else: ignored_predictions.append(predicted_classes) ignored_ground_truths.append(gt) debug and print("===Sample Ignored=== \n") else: print( "Unequal number of images and their GT annotations, please verify the 100 files" ) # Flatten list of all predictions total_predictions = [ item for sublist in total_predictions for item in sublist ] total_ground_truths = [ item for sublist in total_ground_truths for item in sublist ] # Dummy data for testing # total_predictions = [0, 5, 4, 9, 0, 0, 8, 1, 8, 0, 5, 5, 5, 8, 0, 6, 1, 2] # total_ground_truths = [0, 5, 4, 9, 0, 3, 8, 1, 8, 0, 5, 5, 5, 8, 0, 6, 1, 3] draw_heatmap(total_predictions, total_ground_truths)
) #call to the trainer function from trainer import trainer path_to_save = None #Define it path_to_checkpoint = None #Define it trainer( model , dataloader_train , dataloader_val , -1 , path_to_save , checkpoint_path) #define liar_data_test as datasets.dataset with test data and test_dataloader on this #dataset with batch_size = 1 liar_dataset_test = liar_dataset_val = dataset(prep_Data_from='test', purpose='test_class') test_dataloader = DataLoader(liar_dataset_test, batch_size) #function call to the infer function from utils. from utils import infer infer(model, test_dataloader) module_list = [liar_dataset_train, liar_dataset_val, dataloader_train, dataloader_val, statement_encoder, justification_encoder, multiheadAttention, positionFeedForward, model] del liar_dataset_val, liar_dataset_train, dataloader_train, dataloader_val liar_dataset_test = dataset(prep_Data_from='test') test_dataloader = DataLoader(dataset=liar_dataset_test, batch_size=1) infer(model=model, dataloader=test_dataloader)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--models', nargs='+', help='<Required> Set flag', required=True) parser.add_argument('--debug', default=False) directory = os.getcwd() args = parser.parse_args() debug = args.debug model_paths = [] models = args.models for model in models: model_paths.append(os.path.join(directory, 'model', model + '.tflite')) # Load all annotation files in .txt format ground_truth_files = os.path.join(directory, 'dataset') + '/ground_truths' ground_truths = get_ground_truth(ground_truth_files) # Load all 100 test images from hold out set T images = glob.glob(os.path.join(directory, 'dataset') + '/images/*') total_predictions = [] total_ground_truths = [] ignored_predictions = [] ignored_ground_truths = [] count = 0 if len(ground_truths) == len(images): for image, gt in zip(images, ground_truths): net_prediction = [] count += 1 print(f'Image number : {count}/{len(images)}') debug and print("Current Image : ", image) for model_path in model_paths: # Iterate over all models predictions = [] predicted_classes = [] results = infer(model_path, image) # Infer from the current model # Get classes and confidences as well in case Soft Voting is required (already sorted by x coordinate) for key in results: predictions.append([key[0], round(key[1], 3)]) predicted_classes.append((key[0])) # Filter out all incomplete predictions if len(predictions) == 9: net_prediction.append(predictions) debug and print("===Sample Added===\n") else: debug and print("===Sample Ignored===\n") # Send all three model predictions for voting # (P01, P02,...,P09) + (P11, P12,...,P19) + (P21, P22,...,P29) final_vote = calculate_vote(net_prediction, debug) debug and print("Ground Truth: ", gt) debug and print("Predicted Classes: ", final_vote) debug and print("\n\n\n\n") # Add the voting result, i.e, the final prediction for one image to the total predictions total_predictions.append(final_vote) total_ground_truths.append(gt) else: print( "Unequal number of images and their GT annotations, please verify the 100 files" ) # Flatten list of all predictions total_predictions = [ item for sublist in total_predictions for item in sublist ] total_ground_truths = [ item for sublist in total_ground_truths for item in sublist ] draw_heatmap(total_predictions, total_ground_truths)
def main(): if not torch.cuda.is_available(): logging.info('no gpu device available') sys.exit(1) torch.cuda.set_device(args.gpu) cudnn.benchmark = True cudnn.enabled = True logging.info('gpu device = %d' % args.gpu) logging.info("args = %s", args) genotype = eval("genotypes.%s" % args.arch) logging.info(genotype) dataset = params.datasets[args.dset_name] network_params = { 'C': args.init_channels, 'num_classes': dataset.num_classes, 'layers': args.layers, 'num_reductions': args.num_reductions, 'reduction_location_mode': args.reduction_location_mode, 'genotype': genotype, 'stem_multiplier': dataset.num_channels, 'do_SE': args.do_SE } model = Network(**network_params) logging.info("Loading model parameters from %s", args.model_path) utils.load(model, args.model_path) flops, num_params = None, None if args.calc_flops: from thop import profile, clever_format input = torch.randn(1, dataset.num_channels, dataset.hw[0], dataset.hw[1]) flops, num_params = profile(model, inputs=(input, )) flops, num_params = clever_format([flops, num_params], "%.2f") model = model.cuda() test_transform = data_transforms_cifar10() test_data = dset.CIFAR10(root=args.data, train=False, download=True, transform=test_transform) test_queue = torch.utils.data.DataLoader(test_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=0) with torch.no_grad(): test_acc, infer_time = infer(test_queue, model, args.report_freq) if args.calc_flops: logging.info( 'Test Accuracy: %.2f%% | Number of parameters: %s | Inference time: %2.2fms | Flops: %s', test_acc, num_params, infer_time * 1000, flops) else: logging.info('Test Accuracy: %.2f%% | Inference time: %2.2fms', test_acc, infer_time * 1000)