def __init__(self, model): # Setup the net torch.device("cuda") torch.cuda.set_device(0) torch.backends.cudnn.enabled = True self.net = CrowdCounter(cfg.GPU_ID, cfg.NET, torch.nn.MSELoss(), pytorch_ssim.SSIM(window_size=11)) self.net.load_state_dict(torch.load(model)) self.net.cuda() self.net.eval() # IMPORTANT: when changing model, make sure you change config for the dataset it was trained in to make sure mean and std are correct self.mean, self.std = cfg.MEAN_STD
def __init__(self, args): # setup the net torch.device("cuda") torch.cuda.set_device(0) torch.backends.cudnn.enabled = True self.net = CrowdCounter(cfg.GPU_ID, cfg.NET, torch.nn.MSELoss(), pytorch_ssim.SSIM(window_size=11)) self.net.load_state_dict(torch.load(args.model)) self.net.cuda() self.net.eval() # IMPORTANT: when changing model, make sure you change config for the dataset it was trained in to make sure mean, std, h and w are correct self.mean, self.std = cfg.MEAN_STD self.h, self.w = cfg.STD_SIZE print(self.net) # print net structure # video capture object self.vid = VideoCapture(args.video)
def __init__(self, dataloader, cfg_data, pwd): self.cfg_data = cfg_data self.data_mode = cfg.DATASET self.exp_name = cfg.EXP_NAME self.exp_path = cfg.EXP_PATH self.pwd = pwd self.net_name = cfg.NET if self.net_name in ['SANet']: loss_1_fn = nn.MSELoss() from misc import pytorch_ssim loss_2_fn = pytorch_ssim.SSIM(window_size=11) self.net = CrowdCounter(cfg.GPU_ID, self.net_name, loss_1_fn, loss_2_fn).cuda() self.optimizer = optim.Adam(self.net.CCN.parameters(), lr=cfg.LR, weight_decay=1e-4) # self.optimizer = optim.SGD(self.net.parameters(), cfg.LR, momentum=0.95,weight_decay=5e-4) self.scheduler = StepLR(self.optimizer, step_size=cfg.NUM_EPOCH_LR_DECAY, gamma=cfg.LR_DECAY) self.train_record = { 'best_mae': 1e20, 'best_mse': 1e20, 'best_model_name': '' } self.timer = { 'iter time': Timer(), 'train time': Timer(), 'val time': Timer() } self.writer, self.log_txt = logger(self.exp_path, self.exp_name, self.pwd, 'exp') self.i_tb = 0 self.epoch = -1 if cfg.PRE_GCC: net.load_state_dict(torch.load(cfg.PRE_GCC_MODEL)) self.train_loader, self.val_loader, self.restore_transform = dataloader( )
help="Colormap to use. Commons are 2 for Jet or 11 for Hot.", choices=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) parser.add_argument("--mode", default='Add', type=str, help="Blend mode to use.", choices=['Add', 'Lighten', 'Mix', 'Multiply']) args = parser.parse_args() print("Crowd Counter Demo running. Press Q to quit.") # setup the model device = torch.device("cuda") torch.cuda.set_device(0) torch.backends.cudnn.enabled = True # use cudnn? net = CrowdCounter(cfg.GPU_ID, cfg.NET, torch.nn.MSELoss(), pytorch_ssim.SSIM(window_size=11)) net.load_state_dict(torch.load(args.model)) net.cuda() net.eval() mean, std = cfg.MEAN_STD # open the video stream / file cap = cv2.VideoCapture(args.video) while (cap.isOpened()): _, frame = cap.read() if frame is None: break # convert to pytorch tensor and normalize tensor = torchvision.transforms.ToTensor()(cv2.cvtColor( frame, cv2.COLOR_BGR2RGB)) tensor = torchvision.transforms.functional.normalize(tensor,
def test(file_list, model_path): loss_1_fn = torch.nn.MSELoss() loss_2_fn = pytorch_ssim.SSIM(window_size=11) net = CrowdCounter(cfg.GPU_ID, cfg.NET, loss_1_fn, loss_2_fn) net.load_state_dict(torch.load(model_path)) net.cuda() net.eval() f1 = plt.figure(1) gts = [] preds = [] for filename in file_list: print(filename, end=', ') imgname = dataRoot + '/img/' + filename filename_no_ext = filename.split('.')[0] denname = dataRoot + '/den/' + filename_no_ext + '.csv' den = pd.read_csv(denname, sep=',', header=None).values den = den.astype(np.float32, copy=False) img = Image.open(imgname) if img.mode == 'L': img = img.convert('RGB') img = img_transform(img) if slicing: xr = (8 - img.shape[2] % 8) % 8 yr = (8 - img.shape[1] % 8) % 8 img = torch.nn.functional.pad(img, (xr, xr, yr, yr), 'constant', 0) pred_maps = [] x4 = img.shape[2] # full image x1 = x4 // 2 # half image x2 = x1 // 2 # quarter image x3 = x1 + x2 y4 = img.shape[1] y1 = y4 // 2 y2 = y1 // 2 y3 = y1 + y2 img_list = [ img[:, 0:y1, 0:x1], img[:, 0:y1, x2:x3], img[:, 0:y1, x1:x4], img[:, y2:y3, 0:x1], img[:, y2:y3, x2:x3], img[:, y2:y3, x1:x4], img[:, y1:y4, 0:x1], img[:, y1:y4, x2:x3], img[:, y1:y4, x1:x4] ] for inputs in img_list: with torch.no_grad(): img = torch.autograd.Variable(inputs[None, :, :, :]).cuda() pred_maps.append(net.test_forward(img)) x3, x5 = int(x4 * 3 / 8), int(x4 * 5 / 8) y3, y5 = int(y4 * 3 / 8), int(y4 * 5 / 8) x32, x52, x51, x41 = x3 - x2, x5 - x2, x5 - x1, x4 - x1 y32, y52, y51, y41 = y3 - y2, y5 - y2, y5 - y1, y4 - y1 slice0 = pred_maps[0].cpu().data.numpy()[0, 0, 0:y3, 0:x3] slice1 = pred_maps[1].cpu().data.numpy()[0, 0, 0:y3, x32:x52] slice2 = pred_maps[2].cpu().data.numpy()[0, 0, 0:y3, x51:x41] slice3 = pred_maps[3].cpu().data.numpy()[0, 0, y32:y52, 0:x3] slice4 = pred_maps[4].cpu().data.numpy()[0, 0, y32:y52, x32:x52] slice5 = pred_maps[5].cpu().data.numpy()[0, 0, y32:y52, x51:x41] slice6 = pred_maps[6].cpu().data.numpy()[0, 0, y51:y41, 0:x3] slice7 = pred_maps[7].cpu().data.numpy()[0, 0, y51:y41, x32:x52] slice8 = pred_maps[8].cpu().data.numpy()[0, 0, y51:y41, x51:x41] pred_map = np.vstack((np.hstack( (slice0, slice1, slice2)), np.hstack((slice3, slice4, slice5)), np.hstack((slice6, slice7, slice8)))) sio.savemat(exp_name + '/pred/' + filename_no_ext + '.mat', {'data': pred_map / 100.}) else: with torch.no_grad(): img = torch.autograd.Variable(img[None, :, :, :]).cuda() pred_map = net.test_forward(img) sio.savemat(exp_name + '/pred/' + filename_no_ext + '.mat', {'data': pred_map.squeeze().cpu().numpy() / 100.}) pred_map = pred_map.cpu().data.numpy()[0, 0, :, :] pred = np.sum(pred_map) / 100.0 preds.append(pred) gt = np.sum(den) gts.append(gt) sio.savemat(exp_name + '/gt/' + filename_no_ext + '.mat', {'data': den}) pred_map = pred_map / np.max(pred_map + 1e-20) den = den / np.max(den + 1e-20) if save_graphs: den_frame = plt.gca() plt.imshow(den, 'jet') den_frame.axes.get_yaxis().set_visible(False) den_frame.axes.get_xaxis().set_visible(False) den_frame.spines['top'].set_visible(False) den_frame.spines['bottom'].set_visible(False) den_frame.spines['left'].set_visible(False) den_frame.spines['right'].set_visible(False) plt.savefig(exp_name + '/' + filename_no_ext + '_gt_' + str(int(gt)) + '.png', bbox_inches='tight', pad_inches=0, dpi=150) plt.close() # sio.savemat(exp_name+'/'+filename_no_ext+'_gt_'+str(int(gt))+'.mat',{'data':den}) pred_frame = plt.gca() plt.imshow(pred_map, 'jet') pred_frame.axes.get_yaxis().set_visible(False) pred_frame.axes.get_xaxis().set_visible(False) pred_frame.spines['top'].set_visible(False) pred_frame.spines['bottom'].set_visible(False) pred_frame.spines['left'].set_visible(False) pred_frame.spines['right'].set_visible(False) plt.savefig(exp_name + '/' + filename_no_ext + '_pred_' + str(float(pred)) + '.png', bbox_inches='tight', pad_inches=0, dpi=150) plt.close() # sio.savemat(exp_name+'/'+filename_no_ext+'_pred_'+str(float(pred))+'.mat',{'data':pred_map}) diff = den - pred_map diff_frame = plt.gca() plt.imshow(diff, 'jet') plt.colorbar() diff_frame.axes.get_yaxis().set_visible(False) diff_frame.axes.get_xaxis().set_visible(False) diff_frame.spines['top'].set_visible(False) diff_frame.spines['bottom'].set_visible(False) diff_frame.spines['left'].set_visible(False) diff_frame.spines['right'].set_visible(False) plt.savefig(exp_name + '/' + filename_no_ext + '_diff.png', bbox_inches='tight', pad_inches=0, dpi=150) plt.close() # sio.savemat(exp_name+'/diff/'+filename_no_ext+'_diff.mat',{'data':diff}) preds = np.asarray(preds) gts = np.asarray(gts) print('\nMAE= ' + str(np.mean(np.abs(gts - preds)))) print('MSE= ' + str(np.sqrt(np.mean((gts - preds)**2))))
def __init__(self, dataloader, cfg_data, pwd): self.cfg_data = cfg_data self.data_mode = cfg.DATASET self.exp_name = cfg.EXP_NAME self.exp_path = cfg.EXP_PATH self.pwd = pwd self.net_name = cfg.NET if self.net_name in ['SANet']: loss_1_fn = nn.MSELoss() from misc import pytorch_ssim loss_2_fn = pytorch_ssim.SSIM(window_size=11) if 'OAI' in self.net_name: loss_1_fn = nn.SmoothL1Loss() from misc import pytorch_ssim loss_2_fn = pytorch_ssim.SSIM(window_size=11) self.net = CrowdCounter(cfg.GPU_ID, self.net_name, loss_1_fn, loss_2_fn, cfg.PRE).cuda() if not cfg.FINETUNE: self.optimizer = optim.Adam(self.net.CCN.parameters(), lr=cfg.LR, weight_decay=1e-4) print('using ADAM') else: self.optimizer = optim.SGD(self.net.parameters(), cfg.LR, momentum=0.95, weight_decay=5e-4) print('using SGD') if cfg.LR_CHANGER == 'step': self.scheduler = StepLR(self.optimizer, step_size=cfg.NUM_EPOCH_LR_DECAY, gamma=cfg.LR_DECAY) elif cfg.LR_CHANGER == 'cosann': self.scheduler = CosineAnnealingLR(self.optimizer, 2 * cfg.EPOCH_DIS, eta_min=5e-9, last_epoch=-1) elif cfg.LR_CHANGER == 'expotential': self.scheduler = ExponentialLR(self.optimizer, 0.95, last_epoch=-1) elif cfg.LR_CHANGER == 'rop': self.scheduler = ReduceLROnPlateau(self.optimizer, mode='min', verbose=True, eps=1e-10) self.train_record = { 'best_mae': 1e20, 'best_mse': 1e20, 'best_model_name': '' } self.timer = { 'iter time': Timer(), 'train time': Timer(), 'val time': Timer() } self.epoch = 0 self.i_tb = 0 if cfg.PRE_GCC: self.net.load_state_dict(torch.load(cfg.PRE_GCC_MODEL)) self.train_loader, self.val_loader, self.restore_transform = dataloader( ) cfg.PRINT_FREQ = min(len(self.train_loader), cfg.ITER_DIS) if cfg.RESUME: latest_state = torch.load(cfg.RESUME_PATH) self.net.load_state_dict(latest_state['net']) if not cfg.FINETUNE: self.optimizer.load_state_dict(latest_state['optimizer']) self.scheduler.load_state_dict(latest_state['scheduler']) self.epoch = latest_state['epoch'] + 1 self.i_tb = latest_state['i_tb'] self.train_record = latest_state['train_record'] self.exp_path = latest_state['exp_path'] self.exp_name = latest_state['exp_name'] self.writer, self.log_txt = logger(self.exp_path, self.exp_name, self.pwd, 'exp', resume=cfg.RESUME)