def cal_mae(img_root,gt_dmap_root,model_param_path): ''' Calculate the MAE of the test data. img_root: the root of test image data. gt_dmap_root: the root of test ground truth density-map data. model_param_path: the path of specific mcnn parameters. ''' device=torch.device("cpu") model=CANNet() model.load_state_dict(torch.load(model_param_path)) model.to(device) dataset=CrowdDataset(img_root,gt_dmap_root,8,phase='test') dataloader=torch.utils.data.DataLoader(dataset,batch_size=1,shuffle=False) model.eval() mae=0 with torch.no_grad(): for i,(img,gt_dmap) in enumerate(tqdm(dataloader)): img=img.to(device) gt_dmap=gt_dmap.to(device) # forward propagation et_dmap=model(img) mae+=abs(et_dmap.data.sum()-gt_dmap.data.sum()).item() del img,gt_dmap,et_dmap print("model_param_path:"+model_param_path+" mae:"+str(mae/len(dataloader)))
def main_live(args): capture = cv2.VideoCapture(args.stream) num_gpus = torch.cuda.device_count() device = 'cuda' if num_gpus >= 1 else 'cpu' model = CANNet().to(device) model.load_state_dict( torch.load(args.weights, map_location=torch.device(device))) transform = transforms.Compose([transforms.ToTensor()]) model.eval() while (capture.isOpened()): ret, frame = capture.read() if (ret): key = cv2.waitKey(1) & 0xFF copy_frame = copy.copy(frame) copy_frame = transform(copy_frame) # putting the batch dimension on tensor copy_frame = copy_frame.unsqueeze(0) copy_frame = copy_frame.to(device) result = model(copy_frame) crowd_count = int(result.data.sum().item()) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(frame, "Crowd Couting: " + str(crowd_count), (10, 35), font, 1.3, (0, 0, 255), 3, cv2.LINE_AA) cv2.imshow("Frame", frame) print(crowd_count) if key == ord("q"): break
def estimate_density_map(img_root,gt_dmap_root,model_param_path,index): ''' Show one estimated density-map. img_root: the root of test image data. gt_dmap_root: the root of test ground truth density-map data. model_param_path: the path of specific mcnn parameters. index: the order of the test image in test dataset. ''' device=torch.device("cuda") model=CANNet().to(device) model.load_state_dict(torch.load(model_param_path)) dataset=CrowdDataset(img_root,gt_dmap_root,8,phase='test') dataloader=torch.utils.data.DataLoader(dataset,batch_size=1,shuffle=False) model.eval() for i,(img,gt_dmap) in enumerate(dataloader): if i==index: img=img.to(device) gt_dmap=gt_dmap.to(device) # forward propagation et_dmap=model(img).detach() et_dmap=et_dmap.squeeze(0).squeeze(0).cpu().numpy() print(et_dmap.shape) plt.imshow(et_dmap,cmap=CM.jet) plt.show() break
def estimate_density_map(img_root,gt_dmap_root,model_param_path): ''' Show one estimated density-map. img_root: the root of test image data. gt_dmap_root: the root of test ground truth density-map data. model_param_path: the path of specific mcnn parameters. index: the order of the test image in test dataset. ''' model=CANNet().cuda() model.load_state_dict(torch.load(model_param_path)) model.eval() dataset=CrowdDataset(img_root,gt_dmap_root,8,phase='test') dataloader=torch.utils.data.DataLoader(dataset,batch_size=1,shuffle=False) print('dataloader = ',dataloader) for img,gt_dmap,img_name in dataloader: print('img_shape = ',img.shape) st = time.time() img=img.cuda() gt_dmap=gt_dmap.cuda() # forward propagation et_dmap=model(img).detach() et_dmap=et_dmap.squeeze(0).squeeze(0).cpu().numpy() pred_frame = plt.gca() plt.imshow(et_dmap,cmap=CM.jet) plt.show() 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( 'result/'+str(img_name)+'_out' + '.png',bbox_inches='tight', pad_inches=0, dpi=200) plt.close() print('tt = ',time.time()-st) break
help="Use cpu or gpu? For gpu cuda is mandatory") parser.add_argument("--opacity", default=0.7, type=float, help="Opacity value for the density map overlap") args = parser.parse_args() # setup the model if args.device == "cpu": device = torch.device("cpu") elif args.device == "gpu": device = torch.device("cuda") torch.backends.cudnn.enabled = True # use cudnn? else: raise Exception("Unknown device. Use \"cpu\" or \"gpu\".") model = CANNet().to(device) model.load_state_dict(torch.load(args.model)) model.eval() torch.no_grad() # open the video stream / file cap = cv2.VideoCapture(args.video) while (cap.isOpened()): _, frame = cap.read() # convert to pytorch tensor and normalize tensor = torchvision.transforms.ToTensor()(cv2.cvtColor( frame, cv2.COLOR_BGR2RGB)) # ---------- to change after new norm training ------------------------ tensor = torchvision.transforms.functional.normalize( tensor, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
@contact: [email protected] @project: Context-Aware_Crowd_Counting-pytorch @file: demo_video.py @time: 6/27/19 2:37 PM @desc: test videos for serials frame ''' import torch import cv2 import random import matplotlib.pyplot as plt import matplotlib.cm as CM from torchvision import transforms from cannet import CANNet from my_dataset import CrowdDataset model = CANNet() model_param_path = './checkpoints/cvpr2019CAN_353model.pth' model.load_state_dict(torch.load(model_param_path)) model.cuda() model.eval() torch.backends.cudnn.enabled = False def read_img(img, gt_downsample): img = img / 255 if len(img.shape) == 2: img = img[:, :, np.newaxis] img = np.concatenate((img, img, img), 2) if gt_downsample > 1: ds_rows = int(img.shape[0] // gt_downsample)
def main(args): wandb.init(project="crowd", config=args) args = wandb.config # print(args) # vis=visdom.Visdom() torch.cuda.manual_seed(args.seed) model=CANNet().to(args.device) criterion=nn.MSELoss(reduction='sum').to(args.device) # optimizer=torch.optim.SGD(model.parameters(), args.lr, # momentum=args.momentum, # weight_decay=0) optimizer=torch.optim.Adam(model.parameters(), args.lr, weight_decay=args.decay) train_dataset = CrowdDataset(args.train_image_root, args.train_dmap_root, gt_downsample=8, phase='train') train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True) val_dataset = CrowdDataset(args.val_image_root, args.val_dmap_root, gt_downsample=8, phase='test') val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False) if not os.path.exists('./checkpoints'): os.mkdir('./checkpoints') min_mae = 10000 min_epoch = 0 for epoch in tqdm(range(0, args.epochs)): # training phase model.train() model.zero_grad() train_loss = 0 train_mae = 0 train_bar = tqdm(train_loader) for i, (img,gt_dmap) in enumerate(train_bar): # print(img.shape, gt_dmap.shape) img = img.to(args.device) gt_dmap = gt_dmap.to(args.device) # forward propagation et_dmap = model(img) # calculate loss # print(et_dmap.shape, gt_dmap.shape) loss = criterion(et_dmap, gt_dmap) train_loss += loss.item() train_mae += abs(et_dmap.data.sum()-gt_dmap.data.sum()).item() loss = loss/args.gradient_accumulation_steps loss.backward() if (i+1)%args.gradient_accumulation_steps == 0: optimizer.step() model.zero_grad() train_bar.set_postfix(loss=train_loss/(i+1), mae=train_mae/(i+1)) optimizer.step() model.zero_grad() # print("epoch:",epoch,"loss:",epoch_loss/len(dataloader)) torch.save(model.state_dict(),'./checkpoints/epoch_'+str(epoch)+".pth") # testing phase model.eval() val_loss = 0 val_mae = 0 for i, (img,gt_dmap) in enumerate((val_loader)): img = img.to(args.device) gt_dmap = gt_dmap.to(args.device) # forward propagation et_dmap = model(img) loss = criterion(et_dmap, gt_dmap) val_loss += loss.item() val_mae += abs(et_dmap.data.sum()-gt_dmap.data.sum()).item() del img,gt_dmap,et_dmap if val_mae/len(val_loader) < min_mae: min_mae = val_mae/len(val_loader) min_epoch = epoch # print("epoch:" + str(epoch) + " error:" + str(mae/len(val_loader)) + " min_mae:"+str(min_mae) + " min_epoch:"+str(min_epoch)) wandb.log({"loss/train": train_loss/len(train_loader), "mae/train": train_mae/len(train_loader), "loss/val": val_loss/len(val_loader), "mae/val": val_mae/len(val_loader), }, commit=False) # show an image index = random.randint(0, len(val_loader)-1) img, gt_dmap = val_dataset[index] gt_dmap = gt_dmap.squeeze(0).detach().cpu().numpy() wandb.log({"image/img": [wandb.Image(img)]}, commit=False) wandb.log({"image/gt_dmap": [wandb.Image(gt_dmap/(gt_dmap.max())*255, caption=str(gt_dmap.sum()))]}, commit=False) img = img.unsqueeze(0).to(args.device) et_dmap = model(img) et_dmap = et_dmap.squeeze(0).detach().cpu().numpy() wandb.log({"image/et_dmap": [wandb.Image(et_dmap/(et_dmap.max())*255, caption=str(et_dmap.sum()))]}) import time print(time.strftime('%Y.%m.%d %H:%M:%S',time.localtime(time.time())))
test_dmap_root = './data/Shanghai_part_A/test_data/ground_truth' gpu_or_cpu = 'cuda' # use cuda or cpu lr = 1e-7 batch_size = 1 momentum = 0.95 epochs = 20000 steps = [-1, 1, 100, 150] scales = [1, 1, 1, 1] workers = 4 seed = time.time() print_freq = 30 vis = visdom.Visdom() device = torch.device(gpu_or_cpu) torch.cuda.manual_seed(seed) model = CANNet().to(device) criterion = nn.MSELoss(size_average=False).to(device) optimizer = torch.optim.SGD(model.parameters(), lr, momentum=momentum, weight_decay=0) # optimizer=torch.optim.Adam(model.parameters(),lr) train_dataset = CrowdDataset(train_image_root, train_dmap_root, gt_downsample=8) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True) test_dataset = CrowdDataset(test_image_root, test_dmap_root, gt_downsample=8)
test_dmap_root='./data/Shanghai_part_A/test_data/ground_truth' gpu_or_cpu='cuda' # use cuda or cpu lr = 1e-7 batch_size = 1 momentum = 0.95 epochs = 20000 steps = [-1,1,100,150] scales = [1,1,1,1] workers = 4 seed = time.time() print_freq = 30 vis=visdom.Visdom() device=torch.device(gpu_or_cpu) torch.cuda.manual_seed(seed) model=CANNet().to(device) criterion=nn.MSELoss(size_average=False).to(device) optimizer=torch.optim.SGD(model.parameters(),lr, momentum=momentum, weight_decay=0) # optimizer=torch.optim.Adam(model.parameters(),lr) train_dataset=CrowdDataset(train_image_root,train_dmap_root,gt_downsample=8,phase='train') train_loader=torch.utils.data.DataLoader(train_dataset,batch_size=1,shuffle=True) test_dataset=CrowdDataset(test_image_root,test_dmap_root,gt_downsample=8,phase='test') test_loader=torch.utils.data.DataLoader(test_dataset,batch_size=1,shuffle=False) if not os.path.exists('./checkpoints'): os.mkdir('./checkpoints') min_mae=10000 min_epoch=0 train_loss_list=[]