def index(): mat = sio.loadmat("rgbstate.mat") global rgb rgb = mat['rgbMean'].reshape(1, 1, 3) #rgbMean is computed after norm to [0-1] max_num_list = {0: 22, 1: 7} # set label_indice label_indice = np.arange(0.5, max_num_list[0] + 0.5 / 2, 0.5) add = np.array( [1e-6, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45]) label_indice = np.concatenate((add, label_indice)) # init networks label_indice = Tensor(label_indice) class_num = len(label_indice) + 1 global net net = SDCNet_VGG16_classify(class_num, label_indice, psize=64, pstride=64, div_times=2, load_weights=True) # use the trained model mod_path = "best_epoch.pth" if os.path.exists(mod_path): all_state_dict = load(mod_path, map_location=device('cpu')) net.load_state_dict(all_state_dict['net_state_dict']) return render_template('index.html') else: return render_template('fail.html')
def main(opt): # ============================================================================= # inital setting # ============================================================================= # 1.Initial setting # --1.1 dataset setting dataset = opt['dataset'] root_dir = opt['root_dir'] num_workers = opt['num_workers'] img_subsubdir = 'images'; tar_subsubdir = 'gtdens' dataset_transform = ToTensor() # --1.2 use initial setting to generate # set label_indice if opt['partition'] =='one_linear': label_indice = np.arange(opt['step'],opt['max_num']+opt['step']/2,opt['step']) add = np.array([1e-6]) label_indice = np.concatenate( (add,label_indice) ) elif opt['partition'] =='two_linear': label_indice = np.arange(opt['step'],opt['max_num']+opt['step']/2,opt['step']) add = np.array([1e-6,0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45]) label_indice = np.concatenate( (add,label_indice) ) # print(label_indice) opt['label_indice'] = label_indice opt['class_num'] = label_indice.size+1 #test settings img_dir = os.path.join(root_dir,'test',img_subsubdir) tar_dir = os.path.join(root_dir,'test',tar_subsubdir) rgb_dir = os.path.join(root_dir,'rgbstate.mat') testset = myDataset(img_dir,tar_dir,rgb_dir,transform=dataset_transform,\ if_test=True, IF_loadmem=opt['IF_savemem_test']) testloader = DataLoader(testset, batch_size=opt['test_batch_size'], shuffle=False, num_workers=num_workers) # init networks label_indice = torch.Tensor(label_indice) class_num = len(label_indice)+1 div_times = 2 net = SDCNet_VGG16_classify(class_num,label_indice,psize=opt['psize'],\ pstride = opt['pstride'],div_times=div_times,load_weights=True).cuda() # test the exist trained model mod_path='best_epoch.pth' mod_path=os.path.join(opt['trained_model_path'],mod_path) if os.path.exists(mod_path): all_state_dict = torch.load(mod_path) net.load_state_dict(all_state_dict['net_state_dict']) log_save_path = os.path.join(opt['trained_model_path'],'log-trained-model.txt') # test mae,rmse,me = test_phase(opt,net,testloader,log_save_path=log_save_path) log_str = '%10s\t %8s\t &%8s\t &%8s\t\\\\' % (' ','mae','rmse','me')+'\n' log_str+= '%-10s\t %8.3f\t %8.3f\t %8.3f\t' % ( 'test',mae,rmse,me ) + '\n' print(log_str)
def main(model_path, input_source, SDCNet=False): if os.path.isfile(input_source): video_name = input_source.split("/")[-1] video_path = input_source.replace(video_name,"") print("Processing video {} in {}".format(colored(video_name, 'red'), colored(video_path, 'green'))) cap = cv2.VideoCapture(input_source) else: cap = VideoStream(input_source).start() # --1.2 use initial setting to generate # set label_indice label_indice = np.arange(0.5, 22+0.5/2, 0.5) add = np.array([1e-6, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45]) label_indice = np.concatenate((add, label_indice)) # init networks label_indice = torch.Tensor(label_indice) class_num = len(label_indice)+1 div_times = 2 if SDCNet: net = SDCNet_VGG16_classify( class_num, label_indice, load_weights=True) else: net = SSDCNet_classify(class_num, label_indice, div_times=div_times, frontend_name='VGG16', block_num=5, IF_pre_bn=False, IF_freeze_bn=False, load_weights=True, parse_method='maxp') if os.path.exists(model_path): print("Adding Weights ....") all_state_dict = torch.load(model_path,map_location=torch.device('cpu')) net.load_state_dict(all_state_dict['net_state_dict']) net.eval() else: print("Can't find trained weights!!") exit() first_frame = True while True: flag, image = cap.read() output_image = np.copy(image) if first_frame: rois = cv2.selectROIs("frame", image, False, False) first_frame = False # print(roi) if flag: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) else: break sum = 0 for roi in rois: roi_image = image[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])] roi_image = cv2.resize(roi_image, (256, 256)) roi_image = np.transpose(roi_image, (2, 0, 1)) roi_image = torch.Tensor(roi_image[None, :, :, :]) # w = image.shape[-1] # h = image.shape[-2] # pad_w = 64 - w%64 # padding_left = int(pad_w/2) # padding_right = pad_w - padding_left # pad_h = 64 - h%64 # padding_top = int(pad_h/2) # padding_bottom = pad_h - padding_top # image = torch.nn.functional.pad(image, (padding_left, padding_right, padding_top, padding_bottom)) with torch.no_grad(): features = net(roi_image) div_res = net.resample(features) merge_res = net.parse_merge(div_res) if SDCNet: outputs = merge_res['div'+str(net.args['div_times'])] else: outputs = merge_res['div'+str(net.div_times)] del merge_res cv2.rectangle(output_image, (int(roi[0]), int(roi[1])), (int( roi[0]+roi[2]), int(roi[1]+roi[3])), (255, 0, 0), thickness=3) sum += int(outputs.sum().item()) cv2.putText(output_image, "{}".format(sum), (30, 50), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 3) cv2.imshow("frame", output_image) if cv2.waitKey(1) == ord('q'): cap.release() exit() cap.release()
opt['label_indice'] = label_indice opt['class_num'] = label_indice.size + 1 train_data = SHTA(train_im, phase='train', preload=False,crop=9,scale=[1]) val_data = SHTA(val_im, phase='test') train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.bs, shuffle=True, num_workers=args.works) val_loader = torch.utils.data.DataLoader(val_data, batch_size=args.bs, shuffle=False, num_workers=args.works) label_indice = torch.Tensor(label_indice) class_num = len(label_indice) + 1 div_times = 2 net = SDCNet_VGG16_classify(class_num, label_indice, psize=opt['psize'], \ pstride=opt['pstride'], div_times=div_times, load_weights=True).cuda() optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, weight_decay=0.9) if args.show_model: torch.save(net, 'model.pth') if args.resume: if args.test or args.finetune: model = savemodel + 'best_mae.pth' else: model = savemodel + 'last_check.pth' cprint('=> loading checkpoint : %s ' % model, color='yellow') checkpoint = torch.load(model)
def main(opt): num_workers = opt['num_workers'] if opt['partition'] == 'one_linear': label_indice = np.arange(opt['step'], opt['max_num'] + opt['step'] / 2, opt['step']) add = np.array([1e-6]) label_indice = np.concatenate((add, label_indice)) elif opt['partition'] == 'two_linear': label_indice = np.arange(opt['step'], opt['max_num'] + opt['step'] / 2, opt['step']) add = np.array( [1e-6, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45]) label_indice = np.concatenate((add, label_indice)) opt['label_indice'] = label_indice opt['class_num'] = label_indice.size + 1 # init networks label_indice = torch.Tensor(label_indice) class_num = len(label_indice) + 1 div_times = 2 net = SDCNet_VGG16_classify(class_num,label_indice,psize=opt['psize'],\ pstride = opt['pstride'],div_times=div_times,load_weights=True) # test the exist trained model mod_path = 'best_epoch.pth' mod_path = os.path.join(opt['trained_model_path'], mod_path) if os.path.exists(mod_path): all_state_dict = torch.load(mod_path, map_location=torch.device('cpu')) net.load_state_dict(all_state_dict['net_state_dict']) batch_size = 32 while (True): ## for deployment """ f = open("globalQueue.txt", "r") l = f.read() f.close() l = l.split(",") f = open("globalQueue.txt", "w") f.write(','.join(l[32:])) f.close() """ #workingset = l[:batch_size] ## for testing workingset = [ 'football_20200123-101548.jpg', 'street_20200402-123414.jpg' ] transformer = ToTensor() testset = customDataset(workingset, transform=transformer) loader = DataLoader(testset, shuffle=False, num_workers=num_workers) predictions = test_phase(opt, net, loader) print(predictions) x = threading.Thread(target=writer, args=(predictions, )) x.start() """ for i in workingset: prediction.append([i, test_phase(opt,net, i)]) """ print(predictions) break else: print("trained model does not exist, please download")
global rgb rgb = mat['rgbMean'].reshape(1, 1, 3) #rgbMean is computed after norm to [0-1] max_num_list = {0: 22, 1: 7} # set label_indice label_indice = np.arange(0.5, max_num_list[0] + 0.5 / 2, 0.5) add = np.array( [1e-6, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45]) label_indice = np.concatenate((add, label_indice)) # init networks label_indice = Tensor(label_indice) class_num = len(label_indice) + 1 global net net = SDCNet_VGG16_classify(class_num, label_indice, psize=64, pstride=64, div_times=2, load_weights=True) # use the trained model mod_path = "best_epoch.pth" if os.path.exists(mod_path): all_state_dict = load(mod_path, map_location=device('cpu')) net.load_state_dict(all_state_dict['net_state_dict']) app.run(debug=True) else: quit()