def visualize(model: str, images: [str], occlusion_window: int, occlusion_stride: int, no_occlussion: bool, no_gradient: bool): device = torch.device('cuda:0' if cuda.is_available() else 'cpu') click.secho('Using device={}'.format(device), fg='blue') net = Net() net.to(device) click.secho('Loading model from \'{}\''.format(model), fg='yellow') net.load_state_dict(torch.load(model, map_location=device)) net.eval() for path in images: image = utils.load_image(path).to(device) output = net(image) _, predicted = torch.max(output.data, 1) click.echo('Image \'{}\' most likely represents a \'{}\''.format( path, classes[predicted])) if not no_occlussion: occlustion(net, image, predicted, k=occlusion_window, stride=occlusion_stride) if not no_gradient: gradient(net, image, predicted)
def load_model_vqa(map_location): global ques_ix global net_vqa global max_token global token_to_ix global ix_to_ans model_path = 'CKPT/epoch19.pkl' train_path = 'data/CLEVR_train_questions.json' print("Load model...") state_dict = torch.load(model_path, map_location=map_location)['state_dict'] ques_stat = json.load(open(train_path, 'r'))['questions'] stat_ans = json.load(open(train_path, 'r'))['questions'] token_to_ix, pretrained_emb, max_token = tokenize(ques_stat, False) ans_to_ix, ix_to_ans = ans_stat(stat_ans) ans_size = ans_to_ix.__len__() token_size = token_to_ix.__len__() print("token_size:", token_size) print("ans_size:", ans_size) net_vqa = Net(pretrained_emb, token_size, ans_size) net_vqa.load_state_dict(state_dict) net_vqa.eval()
def main(): if args.use_cuda: torch.cuda.set_device(args.gpu) dataloader = DataLoader(dict_path=args.dict_path, glove_path=args.glove_path, data_path=args.data_path, batch_size=args.batch_size, use_glove=args.use_glove) model = Net(no_words=dataloader.tokenizer.no_words, lstm_size=args.lstm_size, emb_size=args.emb_size, depth=args.depth) if args.use_cuda: model = model.cuda() if args.start_iter != 0: # load the model state from pre-specified iteration (saved model available) model.load_state_dict(torch.load( os.path.join(args.save_dir, 'iter_%d.pth' % (args.start_iter))), strict=False) tokenizer = Tokenizer(args.dict_path) optimizer = optim.Adam(model.parameters(), lr=args.lr) train(dataloader, model, optimizer, tokenizer)
def predict(): model = Net(model_name).to(device) model_save_path = os.path.join(config.model_path, '{}.bin'.format(model_name)) model.load_state_dict(torch.load(model_save_path)) # data_len = len(os.listdir(config.image_test_path)) # test_path_list = ['{}/{}.jpg'.format(config.image_test_path, x) for x in range(0, data_len)] # test_data = np.array(test_path_list) test_df = pd.read_csv(config.test_path) test_df['FileID'] = test_df['FileID'].apply(lambda x: '{}/{}.jpg'.format(config.image_test_path, x)) print('test:{}'.format(test_df.shape[0])) test_dataset = MyDataset(test_df, test_transform, 'test') test_loader = DataLoader(test_dataset, batch_size=config.batch_size, shuffle=False) model.eval() pred_list = [] with torch.no_grad(): for batch_x, _ in tqdm(test_loader): batch_x = batch_x.to(device) # compute output probs = model(batch_x) preds = torch.argmax(probs, dim=1) pred_list += [p.item() for p in preds] submission = pd.DataFrame({"FileID": range(len(pred_list)), "SpeciesID": pred_list}) submission.to_csv('submission.csv', index=False, header=False)
def train(load_model: str, save_model: str, train_dataset: str, test_dataset: str, no_train: bool, no_test: bool, epochs: int, batch_size: int, learning_rate: float): device = torch.device('cuda:0' if cuda.is_available() else 'cpu') click.secho('Using device={}'.format(device), fg='blue') net = Net() net.to(device) if load_model is not None: click.secho('Loading model from \'{}\''.format(load_model), fg='yellow') net.load_state_dict(torch.load(load_model, map_location=device)) if not no_train: click.echo('Training model using {}'.format(train_dataset)) net.train() train_net(net, data_path=train_dataset, batch_size=batch_size, num_epochs=epochs, learning_rate=learning_rate) if not no_train and save_model is not None: click.secho('Saving model as \'{}\''.format(save_model), fg='yellow') torch.save(net.state_dict(), save_model) if not no_test: click.echo('Testing model using {}'.format(test_dataset)) net.eval() accuracy = test_net(net, data_path=test_dataset, batch_size=batch_size) color = 'green' if accuracy > 97. else 'red' click.secho('Accuracy={}'.format(accuracy), fg=color)
def generate(self): self.num_classes = len(self.class_names) + 1 model = Net("test") self.net = model print('-- Loading model from {} ...'.format(self.args.model_path)) model.load_state_dict(torch.load(self.args.model_path, map_location='cpu')) self.net = torch.nn.DataParallel(self.net) cudnn.benchmark = True # self.net = self.net.cuda() # 为每个类别的外接框设置不同颜色 hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors))
def multi_model_predict(): preds_dict = dict() for model_name in model_name_list: for fold_idx in range(5): model = Net(model_name).to(device) model_save_path = os.path.join(config.model_path, '{}_fold{}.bin'.format(model_name, fold_idx)) model.load_state_dict(torch.load(model_save_path)) pred_list = predict(model) submission = pd.DataFrame(pred_list) # submission = pd.DataFrame({"id": range(len(pred_list)), "label": pred_list}) submission.to_csv('{}/{}_fold{}_submission.csv' .format(config.submission_path, model_name, fold_idx), index=False, header=False) preds_dict['{}_{}'.format(model_name, fold_idx)] = pred_list pred_list = get_pred_list(preds_dict) submission = pd.DataFrame({"id": range(len(pred_list)), "label": pred_list}) submission.to_csv('submission.csv', index=False, header=False)
def load_model_vqa(map_location): global net_vqa global token_to_ix global ix_to_ans # Base path model_path = './model/muan.pkl' token_to_ix_path = './data/token_to_ix.json' pretrained_emb_path = './data/pretrained_emb.npy' ans_dict_path = './data/vqa_answer_dict.json' '''Pre-load''' # Load token_to_ix token_to_ix = json.load(open(token_to_ix_path, 'r')) token_size = len(token_to_ix) print(' ========== Question token vocab size:', token_size) # print('token_to_ix:', token_to_ix) # Load pretrained_emb pretrained_emb = np.load(pretrained_emb_path) print('pretrained_emb shape:', pretrained_emb.shape) # Answers statistic ans_to_ix, ix_to_ans = json.load(open(ans_dict_path, 'r')) ans_size = len(ans_to_ix) print( ' ========== Answer token vocab size (occur more than {} times):'. format(8), ans_size) # print('ix_to_ans:\n', ix_to_ans) '''Load the pre-trained model''' # Load model ckpt time_start = time.time() print('\nLoading ckpt from: {}'.format(model_path)) state_dict = torch.load(model_path, map_location=map_location)['state_dict'] print('state_dict num:', len(state_dict.keys())) print('Finish load state_dict!') # Load model net_vqa = Net(pretrained_emb, token_size, ans_size) net_vqa.load_state_dict(state_dict) net_vqa.cuda() net_vqa.eval() # del state_dict # print('net:', net) time_end = time.time() print('Finish load net model!') print('Model load time: {:.3f}s\n'.format(time_end - time_start))
def model_predict(model_name): test_path_list = ['{}/{}.jpg'.format(config.image_test_path, x) for x in range(0, data_len)] test_data = np.array(test_path_list) test_dataset = MyDataset(test_data, test_transform, 'test') test_loader = DataLoader(test_dataset, batch_size=config.batch_size, shuffle=False) preds_dict = dict() for fold_idx in range(5): model = Net(model_name).to(device) model_save_path = os.path.join(config.model_path, '{}_fold{}.bin'.format(model_name, fold_idx)) model.load_state_dict(torch.load(model_save_path)) pred_list = predict(model, test_loader) submission = pd.DataFrame(pred_list) # submission = pd.DataFrame({"id": range(len(pred_list)), "label": pred_list}) submission.to_csv('{}/{}_fold{}_submission.csv' .format(config.submission_path, model_name, fold_idx), index=False, header=False) preds_dict['{}_{}'.format(model_name, fold_idx)] = pred_list pred_list = get_pred_list(preds_dict) submission = pd.DataFrame({"id": range(len(pred_list)), "label": pred_list}) submission.to_csv('submission.csv', index=False, header=False)
def run(load_last_checkpoint=False): save_dir = f'{OUTPUT_PATH}/models/' os.makedirs(save_dir, exist_ok=True) neural_net = Net() loss_fn = Loss() optim = torch.optim.SGD(neural_net.parameters(), DEFAULT_LR, momentum=0.9, weight_decay=1e-4) starting_epoch = 0 initial_loss = None if load_last_checkpoint: model_paths = glob(f'''{save_dir}*.ckpt''') model_names = [int(i.split('/')[-1][:-5]) for i in model_paths] latest_model_path = f'''{save_dir}{max(model_names)}.ckpt''' print('loading latest model from:', latest_model_path) checkpoint = torch.load(latest_model_path) neural_net.load_state_dict(checkpoint['model_state_dict']) optim.load_state_dict(checkpoint['optimizer_state_dict']) starting_epoch = checkpoint['epoch'] initial_loss = checkpoint['loss'] if torch.cuda.is_available(): neural_net = neural_net.cuda() loss_fn = loss_fn.cuda() print(f'''Training from epoch: {starting_epoch} towards: {TOTAL_EPOCHS}, with learning rate starting from: {get_lr(starting_epoch)}, and loss: {initial_loss}''') meta = pd.read_csv(f'{OUTPUT_PATH}/augmented_meta.csv', index_col=0).sample(frac=1).reset_index(drop=True) meta_group_by_series = meta.groupby(['seriesuid']).indices list_of_groups = [{i: list(meta_group_by_series[i])} for i in meta_group_by_series.keys()] random.Random(0).shuffle(list_of_groups) val_split = int(VAL_PCT * len(list_of_groups)) val_indices = list(itertools.chain(*[list(i.values())[0] for i in list_of_groups[:val_split]])) train_indices = list(itertools.chain(*[list(i.values())[0] for i in list_of_groups[val_split:]])) ltd = LunaDataSet(train_indices, meta) lvd = LunaDataSet(val_indices, meta) train_loader = DataLoader(ltd, batch_size=1, shuffle=False) val_loader = DataLoader(lvd, batch_size=1, shuffle=False) for ep in range(starting_epoch, TOTAL_EPOCHS): train(train_loader, neural_net, loss_fn, ep, optim, get_lr, save_dir=save_dir) validate(val_loader, neural_net, loss_fn)
def multi_model_predict(): preds_dict = dict() for model_name in model_name_list: model = Net(model_name).to(device) model_save_path = os.path.join(config.model_path, '{}.bin'.format(model_name)) print() print('model path is : ',model_save_path) #model_save_path = './save_model/model_1/se_densenet121.bin' model = nn.DataParallel(model) model.load_state_dict(torch.load(model_save_path)) pred_list, test_data = predict(model) test_data = list(test_data) submission = pd.DataFrame(pred_list) # submission = pd.DataFrame({"id": range(len(pred_list)), "label": pred_list}) submission.to_csv('{}/{}_val.csv' .format(config.submission_path, model_name), index=False, header=False) #submission.to_csv('./submission_test/se_densenet121/submission_sedensenet121_val.csv') preds_dict['{}'.format(model_name)] = pred_list pred_list = get_pred_list(preds_dict) #print() #print(preds_dict.keys()) #a = preds_dict['se_densenet121'] #print(np.shape(a)) #print() #print(pred_list) #print(np.shape(pred_list)) #exit() #submission = pd.DataFrame({"id": test_data, "label": pred_list}) test_data = pd.DataFrame(test_data) pred_list = pd.DataFrame(pred_list) submission = pd.concat([test_data,pred_list],axis=1) #submission = pd.DataFrame({"id": range(len(pred_list)), "label": pred_list}) submission.to_csv('submission_val.csv', index=False, header=False)
time_end = time.time() print('Image feature process time: {:.3f}s'.format(time_end - time_start)) # Load model ckpt print('\nLoading ckpt from: {}'.format(model_path)) time_start = time.time() state_dict = torch.load(model_path, map_location=map_location)['state_dict'] print('state_dict num:', len(state_dict.keys())) print('Finish load state_dict!') # Load model net = Net(pretrained_emb, token_size, ans_size) net.cuda() net.eval() net.load_state_dict(state_dict) # print('net:', net) time_end = time.time() print('Finish load net model!') print('Model load time: {:.3f}s\n'.format(time_end - time_start)) # Predict time_start = time.time() pred = net(imgfeat_batch, bboxfeat_batch, quesix_batch) pred_np = pred.cpu().data.numpy() pred_argmax = np.argmax(pred_np, axis=1)[0] pred_ans = ix_to_ans[str(pred_argmax)] print('pred_argmax:', pred_argmax) print('pred_ans:', pred_ans) if language in ['ZH', 'zh']: chinese_ans, trans_type = process_translate(pred_ans)
def train(train_data, val_data, fold_idx=None): train_data = MyDataset(train_data, train_transform) train_loader = DataLoader(train_data, batch_size=config.batch_size, shuffle=True) val_data = MyDataset(val_data, val_transform) val_loader = DataLoader(val_data, batch_size=config.batch_size, shuffle=False) model = Net(model_name).to(device) criterion = nn.CrossEntropyLoss() # criterion = FocalLoss(0.5) optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.1) best_val_acc = 0 last_improved_epoch = 0 adjust_lr_num = 0 if fold_idx is None: print('start') model_save_path = os.path.join(config.model_path, '{}.bin'.format(model_name)) else: print('start fold: {}'.format(fold_idx + 1)) model_save_path = os.path.join( config.model_path, '{}_fold{}.bin'.format(model_name, fold_idx)) if os.path.isfile(model_save_path): print('加载之前的训练模型') model.load_state_dict(torch.load(model_save_path)) for cur_epoch in range(config.epochs_num): start_time = int(time.time()) model.train() print('epoch: ', cur_epoch + 1) cur_step = 0 for batch_x, batch_y in train_loader: batch_x, batch_y = batch_x.to(device), batch_y.to(device) optimizer.zero_grad() probs = model(batch_x) train_loss = criterion(probs, batch_y) train_loss.backward() optimizer.step() cur_step += 1 if cur_step % config.train_print_step == 0: train_acc = accuracy(probs, batch_y) msg = 'the current step: {0}/{1}, train loss: {2:>5.2}, train acc: {3:>6.2%}' print( msg.format(cur_step, len(train_loader), train_loss.item(), train_acc[0].item())) val_loss, val_acc = evaluate(model, val_loader, criterion) if val_acc >= best_val_acc: best_val_acc = val_acc torch.save(model.state_dict(), model_save_path) improved_str = '*' last_improved_epoch = cur_epoch else: improved_str = '' # msg = 'the current epoch: {0}/{1}, train loss: {2:>5.2}, train acc: {3:>6.2%}, ' \ # 'val loss: {4:>5.2}, val acc: {5:>6.2%}, {6}' msg = 'the current epoch: {0}/{1}, val loss: {2:>5.2}, val acc: {3:>6.2%}, cost: {4}s {5}' end_time = int(time.time()) print( msg.format(cur_epoch + 1, config.epochs_num, val_loss, val_acc, end_time - start_time, improved_str)) if cur_epoch - last_improved_epoch > config.patience_epoch: print("No optimization for a long time, adjust lr...") scheduler.step() last_improved_epoch = cur_epoch adjust_lr_num += 1 if adjust_lr_num > config.adjust_lr_num: print("No optimization for a long time, auto stopping...") break del model gc.collect()
def main(): # Init Video Capture global hand_hist, in_data is_hand_hist_created = False capture = cv2.VideoCapture(0) # Init Model model = Net() model.load_state_dict( torch.load('./model/model_sl.pt', map_location=lambda storage, location: storage)) step = 0 detection_label = 0 X_list = [] Y_list = [] while capture.isOpened(): pressed_key = cv2.waitKey(1) _, frame = capture.read() # Start / Stop Detection when 'z' pressed if pressed_key & 0xFF == ord('z'): if is_hand_hist_created: is_hand_hist_created = False else: is_hand_hist_created = True hand_hist = hand_histogram(frame) elif pressed_key & 0xFF == ord('0'): detection_label = 0 elif pressed_key & 0xFF == ord('1'): detection_label = 1 elif pressed_key & 0xFF == ord('2'): detection_label = 2 elif pressed_key & 0xFF == ord('3'): detection_label = 3 elif pressed_key & 0xFF == ord('4'): detection_label = 4 elif pressed_key & 0xFF == ord('5'): detection_label = 5 elif pressed_key & 0xFF == ord('6'): detection_label = 6 elif pressed_key & 0xFF == ord('7'): detection_label = 7 elif pressed_key & 0xFF == ord('8'): detection_label = 8 elif pressed_key & 0xFF == ord('9'): detection_label = 9 elif pressed_key & 0xFF == ord('r'): if len(X_list) > 0: del X_list[-1] del Y_list[-1] elif pressed_key & 0xFF == ord('c'): if is_hand_hist_created: X_list.append(cv2.cvtColor(in_data, cv2.COLOR_BGR2GRAY)) Y_list.append(label_to_label_index_dict[detection_label]) elif pressed_key & 0xFF == ord('s'): if len(X_list) > 0: sdatetime = datetime.datetime.now().strftime("%Y%m%d%H%M%S") onehot_Y_buff = np.zeros((len(Y_list), 10)) onehot_Y_buff[np.arange(len(Y_list)), Y_list] = 1 np.save(open('X_{}.npy'.format(sdatetime), 'wb'), np.array(X_list)) np.save(open('Y_{}.npy'.format(sdatetime), 'wb'), onehot_Y_buff) time.sleep(2) if is_hand_hist_created: frame = manage_image_opr(frame, hand_hist) else: frame = draw_rect(frame) frame[:30, :270, :] = 0 if len(X_list) > 0: frame[:100, :270, :] = 0 cv2.putText( frame, 'LAST LABEL : {}'.format( label_index_to_label_dict[Y_list[-1]]), (5, 65), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) cv2.putText(frame, 'COUNT : {}'.format(len(Y_list)), (5, 95), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) frame[36:36 + 64, 270:270 + 64, :] = np.expand_dims(X_list[-1], axis=-1) cv2.putText(frame, 'LABEL : {}'.format(detection_label), (5, 25), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) # Render to Screen if is_hand_hist_created and in_data is not None: frame[:64, -64:, :] = np.expand_dims(cv2.cvtColor( in_data, cv2.COLOR_BGR2GRAY), axis=-1) cv2.imshow("FunTorch Data Collector", rescale_frame(frame)) # Close if ESC pressed if pressed_key == 27: break cv2.destroyAllWindows() capture.release()
if __name__ == "__main__": # 1.获取命令行参数、创建网络、加载网络参数 args = getArgs() model = Net('train') print('-- Loading weights into state dict...') pretrained_dict = torch.load( args.weight_path, map_location='cuda' if torch.cuda.is_available() else 'cpu') model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v) } model_dict.update(pretrained_dict) model.load_state_dict(model_dict) print('-- Loading weights finished.') # 2.多GPU并行 if torch.cuda.is_available(): model = torch.nn.DataParallel(model) cudnn.benchmark = True model = model.cuda() # 3.创建计算loss的类 criterion = MultiBoxLoss() # 4.创建优化器 optimizer = optim.Adam(model.parameters(), lr=args.lr) model.train() # 5.读取数据开始训练Epoch轮 for epoch in range(args.Epoch): # 5.1每轮使用不同学习率 if epoch % 10 == 0:
def main(): # Init running label sayToMe('I am your gesture assistant. Speed : 1 terahertz, memory : 1 zigabyte.') sayToMe('Perform one of the below gestures') print("I am your gesture assistant...") print("############################################################################") print("Show the numbers below to the camera to begin.....") print("############################################################################") print("1) Open Gmail") print("2) Start Video Player") print("3) Start Microsoft Word") print("4) Open Play Music") print("5) Open Devpost.com") print("############################################################################") open_facebook = False; open_two = False; open_three = False; open_four = False; open_five = False; open_zero = False; one_count=0 two_count=0 three_count=0 four_count=0 five_count=0 last_10_detection = np.zeros(10) # Init Video Capture global hand_hist, in_data is_hand_hist_created = False capture = cv2.VideoCapture(0) # Init Model model = Net() model.load_state_dict(torch.load('./model/model_sl_3968.pt', map_location=lambda storage, location: storage)) model.eval() # step = 0 detection_result = 'None' while capture.isOpened(): pressed_key = cv2.waitKey(1) _, frame = capture.read() #cv2.rectangle(frame,(100,100),(300,300),(0,255,0),0) # Start / Stop Detection when 'z' pressed if pressed_key & 0xFF == ord('z'): if is_hand_hist_created: is_hand_hist_created = False else: is_hand_hist_created = True hand_hist = hand_histogram(frame) # Reinit running label last_10_detection = np.zeros(10) if is_hand_hist_created: frame = manage_image_opr(frame, hand_hist) else: frame = draw_rect(frame) # Perform Detection if in_data is not None and is_hand_hist_created: g_img = cv2.cvtColor(in_data, cv2.COLOR_BGR2GRAY) x = torch.FloatTensor(g_img).view(1,1,64,64) / 255 x = (x - 0.5) / 0.5 with torch.no_grad(): y = model(x) y_idx = F.softmax(y, dim=-1).argmax().numpy() # Update likelihood last_10_detection[y_idx] += 2 last_10_detection = last_10_detection - 1 last_10_detection = np.clip(last_10_detection, 0, 8) # print(y_idx, label_dict[int(y_idx)], F.softmax(y, dim=-1)) detection_result = label_dict[int(np.argmax(last_10_detection))] if detection_result == "ONE": one_count=one_count+1 two_count=0 three_count=0 four_count=0 five_count=0 print(one_count) #print(str(one_count).join(" "), end='') if(one_count==10): sayToMe('Opening gmail') #print("One count reached") url = 'https://www.gmail.com/' webbrowser.open(url) one_count=0 if detection_result == "TWO": one_count=0 two_count=two_count+1 three_count=0 four_count=0 five_count=0 print(two_count) if(two_count==10): #print("Two count reached") sayToMe('Starting Video Player') os.system("start D:/Devpost_Hackathons/pytorch/test_video1.mp4") two_count=0 if detection_result == "THREE": one_count=0 two_count=0 three_count=three_count+1 four_count=0 five_count=0 print(three_count) if(three_count==10): #print("Three count reached") sayToMe('Starting Microsoft word') os.system("start winword") three_count=0 if detection_result == "FOUR": one_count=0 two_count=0 three_count=0 four_count=four_count+1 five_count=0 print(four_count) if(four_count==10): #print("Four count reached") sayToMe('Opening Play Music') #print("One count reached") url = 'https://play.google.com/music/listen?u=0#/home' webbrowser.open(url) four_count=0 if detection_result == "FIVE": one_count=0 two_count=0 three_count=0 four_count=0 five_count=five_count+1 print(five_count) if(five_count==10): #print("Five count reached") sayToMe('Opening Devpost') #print("One count reached") url = 'https://devpost.com' webbrowser.open(url) five_count=0 else: detection_result = 'None' # Render to Screen if is_hand_hist_created : if in_data is not None: frame[:75,:180,:] = 0 frame[:64,-64:,:] = np.expand_dims(cv2.cvtColor(in_data, cv2.COLOR_BGR2GRAY), axis=-1) cv2.putText(frame,'DETECTED',(5,30), cv2.FONT_HERSHEY_DUPLEX, 1, (255,255,255), 1, cv2.LINE_AA) cv2.putText(frame,'{}'.format(detection_result),(5,65), cv2.FONT_HERSHEY_DUPLEX, 1, (255,255,255), 1, cv2.LINE_AA) cv2.imshow("FunTorch", rescale_frame(frame)) # Close if ESC pressed if pressed_key == 27: break cv2.destroyAllWindows() capture.release()
def train(train_data, val_data, fold_idx=None): train_data = MyDataset(train_data, train_transform) train_loader = DataLoader(train_data, batch_size=config.batch_size, shuffle=True) val_data = MyDataset(val_data, val_transform) val_loader = DataLoader(val_data, batch_size=config.batch_size, shuffle=False) model = Net(model_name).to(device) criterion = nn.CrossEntropyLoss() # criterion = FocalLoss(0.5) optimizer = torch.optim.AdamW(model.parameters(), lr=5e-6, weight_decay=1e-4) # scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.1) # optimizer = Ranger(model.parameters(), lr=1e-3, weight_decay=0.0005) # scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=4) if fold_idx is None: print('start') model_save_path = os.path.join(config.model_path, '{}.bin'.format(model_name)) else: print('start fold: {}'.format(fold_idx + 1)) model_save_path = os.path.join( config.model_path, '{}_fold{}.bin'.format(model_name, fold_idx)) if os.path.isfile(model_save_path): print('加载之前的训练模型') model.load_state_dict(torch.load(model_save_path)) best_val_score = 0 best_val_score_cnt = 0 last_improved_epoch = 0 adjust_lr_num = 0 for cur_epoch in range(config.epochs_num): start_time = int(time.time()) model.train() print('epoch:{}, step:{}'.format(cur_epoch + 1, len(train_loader))) cur_step = 0 for batch_x, batch_y in train_loader: batch_x, batch_y = batch_x.to(device), batch_y.to(device) optimizer.zero_grad() probs = model(batch_x) train_loss = criterion(probs, batch_y) train_loss.backward() optimizer.step() cur_step += 1 if cur_step % config.train_print_step == 0: train_acc = accuracy(probs, batch_y) msg = 'the current step: {0}/{1}, train loss: {2:>5.2}, train acc: {3:>6.2%}' print( msg.format(cur_step, len(train_loader), train_loss.item(), train_acc[0].item())) val_loss, val_score = evaluate(model, val_loader, criterion) if val_score >= best_val_score: if val_score == best_val_score: best_val_score_cnt += 1 best_val_score = val_score torch.save(model.state_dict(), model_save_path) improved_str = '*' last_improved_epoch = cur_epoch else: improved_str = '' msg = 'the current epoch: {0}/{1}, val loss: {2:>5.2}, val acc: {3:>6.2%}, cost: {4}s {5}' end_time = int(time.time()) print( msg.format(cur_epoch + 1, config.epochs_num, val_loss, val_score, end_time - start_time, improved_str)) if cur_epoch - last_improved_epoch >= config.patience_epoch or best_val_score_cnt >= 3: if adjust_lr_num >= config.adjust_lr_num: print("No optimization for a long time, auto stopping...") break print("No optimization for a long time, adjust lr...") # scheduler.step() last_improved_epoch = cur_epoch # 加上,不然会连续更新的 adjust_lr_num += 1 best_val_score_cnt = 0 # scheduler.step() del model gc.collect() if fold_idx is not None: model_score[fold_idx] = best_val_score
prompt: d.get(prompt)['predictions'] for prompt in d } return self if __name__ == '__main__': args = parser.parse_args() # Loading the evaluation dataset print("Loading dataset") data_params_json_path = os.path.join(args.data_dir, 'params.json') data_params = utils.DataParams.from_json(data_params_json_path) val_dataset = DoulingoDataset(data_params, split='val') # Loading the model print("Loading model...") checkpoint = os.path.join(args.model_dir, f"runs/{args.checkpoint}.pth.tar") config = utils.Params(cuda=torch.cuda.is_available(), src='en', trg='hu') model = Net(config) checkpoint = torch.load(checkpoint) model.load_state_dict(checkpoint['state_dict']) print("Finished Loading") # Evaluation ... print("Starting Evaluation..") if not os.path.exists(args.results_dir): os.mkdir(args.results_dir) metrics = evaluate_model(model.cuda(), val_dataset, args.results_dir) result_json = os.path.join(args.results_dir, 'metrics.json') utils.save_dict_to_json(metrics, result_json)
def main(): # Init running label last_10_detection = np.zeros(10) # Init Video Capture global hand_hist, in_data is_hand_hist_created = False capture = cv2.VideoCapture(0) # Init Model model = Net() model.load_state_dict( torch.load('./model/model_sl_3968.pt', map_location=lambda storage, location: storage)) model.eval() step = 0 detection_result = 'None' while capture.isOpened(): pressed_key = cv2.waitKey(1) _, frame = capture.read() # Start / Stop Detection when 'z' pressed if pressed_key & 0xFF == ord('z'): if is_hand_hist_created: is_hand_hist_created = False else: is_hand_hist_created = True hand_hist = hand_histogram(frame) # Reinit running label last_10_detection = np.zeros(10) if is_hand_hist_created: frame = manage_image_opr(frame, hand_hist) else: frame = draw_rect(frame) # Perform Detection if in_data is not None and is_hand_hist_created: g_img = cv2.cvtColor(in_data, cv2.COLOR_BGR2GRAY) x = torch.FloatTensor(g_img).view(1, 1, 64, 64) / 255 x = (x - 0.5) / 0.5 with torch.no_grad(): y = model(x) y_idx = F.softmax(y, dim=-1).argmax().numpy() # Update likelihood last_10_detection[y_idx] += 2 last_10_detection = last_10_detection - 1 last_10_detection = np.clip(last_10_detection, 0, 8) # print(y_idx, label_dict[int(y_idx)], F.softmax(y, dim=-1)) detection_result = label_dict[int( np.argmax(last_10_detection))] else: detection_result = 'None' # Render to Screen if is_hand_hist_created: if in_data is not None: frame[:75, :180, :] = 0 frame[:64, -64:, :] = np.expand_dims(cv2.cvtColor( in_data, cv2.COLOR_BGR2GRAY), axis=-1) cv2.putText(frame, 'DETECTED', (5, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) cv2.putText(frame, '{}'.format(detection_result), (5, 65), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) cv2.imshow("FunTorch", rescale_frame(frame)) # Close if ESC pressed if pressed_key == 27: break cv2.destroyAllWindows() capture.release()