Example #1
0
def my_demo(file_list, model_path):
    Net_OK = ['Res101_SFCN', 'LCN']
    if (cfg.NET not in Net_OK):
        print('net is not Res101_SFCN  demo not work')
        return
    net = CrowdCounter(cfg.GPU_ID, cfg.NET)

    new_weight_dict = torch.load(model_path)
    if (cfg.GPU_ID == [0]):
        new_weight_dict = re_name_weight(new_weight_dict)
    net.load_state_dict(new_weight_dict)
    net.cuda()
    net.eval()
    print('net eval is ok=================')

    f1 = plt.figure(1)
    for filename in file_list:
        print(filename)
        img = Image.open(filename)
        if img.mode == 'L':
            img = img.convert('RGB')
        img = img_transform(img)
        with torch.no_grad():
            img = Variable(img[None, :, :, :]).cuda()
            start = time.time()
            for i in range(1000):
                pred_map = net.test_forward(img)
            pred_map.cpu()
            end = time.time()
            density_pre = pred_map.squeeze().cpu().numpy() / 100.
            num_people = int(np.sum(density_pre))
            print('in this picture,there are ', num_people, ' people')
            print('Do once forward need {:.3f}ms '.format(
                (end - start) * 1000 / 100.0))
Example #2
0
def main(params):

    H, W = params['image_size']
    mean_std = ([0.452016860247, 0.447249650955, 0.431981861591],
                [0.23242045939, 0.224925786257, 0.221840232611])

    data_transform = transforms.Compose([
        transforms.Resize((H, W)),
        transforms.ToTensor(),
        transforms.Normalize(*mean_std)
    ])

    net = CrowdCounter([0], params['model'])
    net.load_state_dict(torch.load(params['model_path']))
    net.cuda()
    net.eval()

    video_list = np.sort(glob(params['dataset_path'] + '/*'))
    for v in video_list:
        print(v)
        outputdir = params['outputdir_prefix'] + '/%d_%d/' % (H, W)
        os.makedirs(outputdir, exist_ok=True)
        file_list = np.sort(glob(v + '/*.jpg'))

        imgs = torch.zeros(len(file_list), 3, H, W)
        for i, f in enumerate(tqdm(file_list)):
            imgs[i] = data_transform(Image.open(f))

        train_dataset = torch.utils.data.TensorDataset(
            imgs, torch.zeros(len(file_list)))
        train_loader = torch.utils.data.DataLoader(
            train_dataset, batch_size=params['batch_size'], shuffle=False)
        pred_map = []
        for x, y in tqdm(train_loader):
            tmp = net.test_forward(x.cuda()).squeeze().detach().cpu().numpy()
            if (len(tmp.shape) == 2):
                tmp = tmp[np.newaxis]
            pred_map.append(tmp)
        pred_map = np.concatenate(pred_map)
        np.savez_compressed(outputdir + os.path.basename(v), pred_map)
Example #3
0
def main():

    cfg_file = open('./config.py',"r")  
    cfg_lines = cfg_file.readlines()
    
    with open(log_txt, 'a') as f:
            f.write(''.join(cfg_lines) + '\n\n\n\n')
    if len(cfg.TRAIN.GPU_ID)==1:
        torch.cuda.set_device(cfg.TRAIN.GPU_ID[0])
    torch.backends.cudnn.benchmark = True

    net = CrowdCounter(ce_weights=train_set.wts).cuda()
    
    if len(cfg.TRAIN.GPU_ID)>1:
        net = torch.nn.DataParallel(net, device_ids=cfg.TRAIN.GPU_ID).cuda()
    else:
        net=net.cuda()       

    net.train()

    optimizer = optim.Adam([
                            {'params': [param for name, param in net.named_parameters() if 'seg' in name], 'lr': cfg.TRAIN.SEG_LR},
                            {'params': [param for name, param in net.named_parameters() if 'seg' not in name], 'lr': cfg.TRAIN.LR}
                          ])
    
    i_tb = 0
    for epoch in range(cfg.TRAIN.MAX_EPOCH):

        _t['train time'].tic()
        i_tb,model_path = train(train_loader, net, optimizer, epoch, i_tb)
        _t['train time'].toc(average=False)
        print 'train time of one epoch: {:.2f}s'.format(_t['train time'].diff)
        if epoch%cfg.VAL.FREQ!=0:
            continue
        _t['val time'].tic()
        validate(val_loader, model_path, epoch, restore_transform)
        _t['val time'].toc(average=False)
        print 'val time of one epoch: {:.2f}s'.format(_t['val time'].diff)
Example #4
0
def validate(val_loader, model_path, epoch, restore):
    net = CrowdCounter(ce_weights=train_set.wts)
    net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()
    print '=' * 50
    val_loss_mse = []
    val_loss_cls = []
    val_loss_seg = []
    val_loss = []
    mae = 0.0
    mse = 0.0

    for vi, data in enumerate(val_loader, 0):
        img, gt_map, gt_cnt, roi, gt_roi, gt_seg = data
        # pdb.set_trace()
        img = Variable(img, volatile=True).cuda()
        gt_map = Variable(gt_map, volatile=True).cuda()
        gt_seg = Variable(gt_seg, volatile=True).cuda()

        roi = Variable(roi[0], volatile=True).cuda().float()
        gt_roi = Variable(gt_roi[0], volatile=True).cuda()

        pred_map, pred_cls, pred_seg = net(img, gt_map, roi, gt_roi, gt_seg)
        loss1, loss2, loss3 = net.f_loss()
        val_loss_mse.append(loss1.data)
        val_loss_cls.append(loss2.data)
        val_loss_seg.append(loss3.data)
        val_loss.append(net.loss.data)

        pred_map = pred_map.data.cpu().numpy()
        gt_map = gt_map.data.cpu().numpy()

        pred_seg = pred_seg.cpu().max(1)[1].squeeze_(1).data.numpy()
        gt_seg = gt_seg.data.cpu().numpy()

        # pdb.set_trace()
        # pred_map = pred_map*pred_seg

        gt_count = np.sum(gt_map)
        pred_cnt = np.sum(pred_map)

        mae += abs(gt_count - pred_cnt)
        mse += ((gt_count - pred_cnt) * (gt_count - pred_cnt))

    # pdb.set_trace()
    mae = mae / val_set.get_num_samples()
    mse = np.sqrt(mse / val_set.get_num_samples())

    loss1 = np.mean(np.array(val_loss_mse))[0]
    loss2 = np.mean(np.array(val_loss_cls))[0]
    loss3 = np.mean(np.array(val_loss_seg))[0]
    loss = np.mean(np.array(val_loss))[0]

    print '=' * 50
    print exp_name
    print '    ' + '-' * 20
    print '    [mae %.1f mse %.1f], [val loss %.8f %.8f %.4f %.4f]' % (
        mae, mse, loss, loss1, loss2, loss3)
    print '    ' + '-' * 20
    print '=' * 50
Example #5
0
def test(file_list, model_path):

    net = CrowdCounter(cfg.GPU_ID, cfg.NET)
    net.load_state_dict(torch.load(model_path), strict=False)
    net.cuda()
    net.eval()

    f1 = plt.figure(1)

    gts = []
    preds = []

    for filename in file_list:
        print(filename)
        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)

        #gt = np.sum(den)
        with torch.no_grad():
            img = 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.})
        #sio.savemat(exp_name+'/gt/'+filename_no_ext+'.mat',{'data':den})

        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]

        pred = np.sum(pred_map) / 100.0
        pred_map = pred_map / np.max(pred_map + 1e-20)

        #den = den/np.max(den+1e-20)
        '''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
Example #6
0
def test(file_list, model_path):

    net = CrowdCounter(cfg.GPU_ID, 'RAZ_loc')
    net.cuda()
    net.load_state_dict(torch.load(model_path))
    net.eval()

    gts = []
    preds = []

    record = open('submmited_raz_loc_0.5-0512.txt', 'w+')
    for infos in file_list:
        filename = infos.split()[0]

        imgname = os.path.join(dataRoot, 'img', filename + '.jpg')
        img = Image.open(imgname)
        ori_img = Image.open(os.path.join(ori_data, filename + '.jpg'))
        ori_w,ori_h = ori_img.size
        w,h = img.size

        ratio_w = ori_w/w
        ratio_h = ori_h/h

        if img.mode == 'L':
            img = img.convert('RGB')
        img = img_transform(img)[None, :, :, :]
        with torch.no_grad():
            img = Variable(img).cuda()
            crop_imgs, crop_masks = [], []
            b, c, h, w = img.shape
            rh, rw = 576, 768
            for i in range(0, h, rh):
                gis, gie = max(min(h-rh, i), 0), min(h, i+rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w-rw, j), 0), min(w, j+rw)
                    crop_imgs.append(img[:, :, gis:gie, gjs:gje])
                    mask = torch.zeros(b, 1, h, w).cuda()
                    mask[:, :, gis:gie, gjs:gje].fill_(1.0)
                    crop_masks.append(mask)
            crop_imgs, crop_masks = map(lambda x: torch.cat(x, dim=0), (crop_imgs, crop_masks))

            # forward may need repeatng
            crop_preds = []
            nz, bz = crop_imgs.size(0), 1
            for i in range(0, nz, bz):
                gs, gt = i, min(nz, i+bz)
                crop_pred = net.test_forward(crop_imgs[gs:gt])

                crop_pred = F.softmax(crop_pred,dim=1).data[0,1,:,:]
                crop_pred = crop_pred[None,:,:]

                crop_preds.append(crop_pred)
            crop_preds = torch.cat(crop_preds, dim=0)

            # splice them to the original size
            idx = 0
            pred_map = torch.zeros(b, 1, h, w).cuda()
            for i in range(0, h, rh):
                gis, gie = max(min(h-rh, i), 0), min(h, i+rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w-rw, j), 0), min(w, j+rw)
                    pred_map[:, :, gis:gie, gjs:gje] += crop_preds[idx]
                    idx += 1

            # for the overlapping area, compute average value
            mask = crop_masks.sum(dim=0).unsqueeze(0)
            pred_map = pred_map / mask


        pred_map = F.avg_pool2d(pred_map,3,1,1)
        maxm = F.max_pool2d(pred_map,3,1,1)
        maxm = torch.eq(maxm,pred_map)
        pred_map = maxm*pred_map
        pred_map[pred_map<0.5]=0
        pred_map = pred_map.bool().long()
        pred_map = pred_map.cpu().data.numpy()[0,0,:,:]

        ids = np.array(np.where(pred_map==1)) #y,x
        ori_ids_y = ids[0,:]*ratio_h
        ori_ids_x = ids[1,:]*ratio_w
        ids = np.vstack((ori_ids_x,ori_ids_y)).astype(np.int16)#x,y

        loc_str = ''
        for i_id in range(ids.shape[1]):
            loc_str = loc_str + ' ' + str(ids[0][i_id]) + ' ' + str(ids[1][i_id]) # x, y

        pred = ids.shape[1]

        print(f'{filename} {pred:d}{loc_str}', file=record)
        print(f'{filename} {pred:d}')
    record.close()
Example #7
0
def test(file_list, model_path):

    net = CrowdCounter(cfg.GPU_ID, 'Res101_SFCN')
    net.cuda()
    lastest_state = torch.load(model_path)
    net.load_state_dict(lastest_state['net'])
    #net.load_state_dict(torch.load(model_path))
    net.eval()

    #f = open('submmited.txt', 'w+')
    for infos in file_list:
        filename = infos.split()[0]
        #print(filename)

        imgname = os.path.join(dataRoot, 'img', filename + '.jpg')
        img = Image.open(imgname)

        dotname = imgname.replace('img', 'dot').replace('jpg', 'png')
        dot_map = Image.open(dotname)
        dot_map = dot_transform(dot_map)
        if img.mode == 'L':
            img = img.convert('RGB')
        img = img_transform(img)[None, :, :, :]
        dot_map = dot_map[None, :, :, :]
        with torch.no_grad():
            img = Variable(img).cuda()
            dot_map = Variable(dot_map).cuda()
            algt = torch.sum(dot_map).item()
            crop_imgs, crop_dots, crop_masks = [], [], []
            b, c, h, w = img.shape
            rh, rw = 576, 768
            for i in range(0, h, rh):
                gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
                    crop_imgs.append(img[:, :, gis:gie, gjs:gje])
                    crop_dots.append(dot_map[:, :, gis:gie, gjs:gje])
                    mask = torch.zeros_like(dot_map).cuda()
                    mask[:, :, gis:gie, gjs:gje].fill_(1.0)
                    crop_masks.append(mask)
            crop_imgs, crop_dots, crop_masks = map(
                lambda x: torch.cat(x, dim=0),
                (crop_imgs, crop_dots, crop_masks))

            # forward may need repeatng
            crop_preds, crop_dens = [], []
            nz, bz = crop_imgs.size(0), 1
            for i in range(0, nz, bz):
                gs, gt = i, min(nz, i + bz)
                crop_pred, crop_den = net.forward(crop_imgs[gs:gt],
                                                  crop_dots[gs:gt])
                crop_preds.append(crop_pred)
                crop_dens.append(crop_den)
            crop_preds = torch.cat(crop_preds, dim=0)
            crop_dens = torch.cat(crop_dens, dim=0)

            # splice them to the original size
            idx = 0
            pred_map = torch.zeros_like(dot_map).cuda()
            den_map = torch.zeros_like(dot_map).cuda()
            for i in range(0, h, rh):
                gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
                    pred_map[:, :, gis:gie, gjs:gje] += crop_preds[idx]
                    den_map[:, :, gis:gie, gjs:gje] += crop_dens[idx]
                    idx += 1

            # for the overlapping area, compute average value
            mask = crop_masks.sum(dim=0).unsqueeze(0)
            pred_map = pred_map / mask
            den_map = den_map / mask

            pred_map /= LOG_PARA
            pred = torch.sum(pred_map).item()

        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]
        den_map = den_map.cpu().data.numpy()[0, 0, :, :]
        print(pred_map.sum(), den_map.sum())
        psnr = calc_psnr(den_map, pred_map)
        ssim = calc_ssim(den_map, pred_map)
        if psnr == 'NaN':
            plt.imsave(os.path.join(
                'pred',
                f'[{filename}]_[{pred:.2f}|{algt:.2f}]_[{psnr}]_[{ssim:.4f}].png'
            ),
                       pred_map,
                       cmap='jet')
        else:
            plt.imsave(os.path.join(
                'pred',
                f'[{filename}]_[{pred:.2f}|{algt:.2f}]_[{psnr:.2f}]_[{ssim:.4f}].png'
            ),
                       pred_map,
                       cmap='jet')
Example #8
0
def validate(val_loader, model_path, epoch, restore):
    net = CrowdCounter(ce_weights=train_set.wts)
    net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()
    print '='*50
    val_loss_mse = []
    val_loss_cls = []
    val_loss_seg = []
    val_loss = []
    mae = 0.0
    mse = 0.0

    for vi, data in enumerate(val_loader, 0):
        img, gt_map, gt_cnt, roi, gt_roi, gt_seg = data
        # pdb.set_trace()
        img = Variable(img, volatile=True).cuda()
        gt_map = Variable(gt_map, volatile=True).cuda()
        gt_seg = Variable(gt_seg, volatile=True).cuda()

        roi = Variable(roi[0], volatile=True).cuda().float()
        gt_roi = Variable(gt_roi[0], volatile=True).cuda()

        pred_map,pred_cls,pred_seg = net(img, gt_map, roi, gt_roi, gt_seg)
        loss1,loss2,loss3 = net.f_loss()
        val_loss_mse.append(loss1.data)
        val_loss_cls.append(loss2.data)
        val_loss_seg.append(loss3.data)
        val_loss.append(net.loss.data)

        pred_map = pred_map.data.cpu().numpy()
        gt_map = gt_map.data.cpu().numpy()

        pred_seg = pred_seg.cpu().max(1)[1].squeeze_(1).data.numpy()
        gt_seg = gt_seg.data.cpu().numpy()
        gt_count = np.sum(gt_map)
        pred_cnt = np.sum(pred_map)

        mae += abs(gt_count-pred_cnt)
        mse += ((gt_count-pred_cnt)*(gt_count-pred_cnt))

        x = []
        if vi==0:
            for idx, tensor in enumerate(zip(img.cpu().data, pred_map, gt_map, pred_seg, gt_seg)):
                if idx>cfg.VIS.VISIBLE_NUM_IMGS:
                    break
                # pdb.set_trace()
                pil_input = restore(tensor[0]/255.)
                pil_label = torch.from_numpy(tensor[2]/(tensor[2].max()+1e-10)).repeat(3,1,1)
                pil_output = torch.from_numpy(tensor[1]/(tensor[1].max()+1e-10)).repeat(3,1,1)
                
                pil_gt_seg = torch.from_numpy(tensor[4]).repeat(3,1,1).float()
                pil_pred_seg = torch.from_numpy(tensor[3]).repeat(3,1,1).float()
                # pdb.set_trace()
                
                x.extend([pil_to_tensor(pil_input.convert('RGB')), pil_label, pil_output, pil_gt_seg, pil_pred_seg])
            x = torch.stack(x, 0)
            x = vutils.make_grid(x, nrow=5, padding=5)
            writer.add_image(exp_name + '_epoch_' + str(epoch+1), (x.numpy()*255).astype(np.uint8))

    mae = mae/val_set.get_num_samples()
    mse = np.sqrt(mse/val_set.get_num_samples())

    '''
    loss1 = float(np.mean(np.array(val_loss_mse)))
    loss2 = float(np.mean(np.array(val_loss_cls)))
    loss3 = float(np.mean(np.array(val_loss_seg)))
    loss = float(np.mean(np.array(val_loss)))'''
    loss1 = np.mean(np.array(val_loss_mse))[0]
    loss2 = np.mean(np.array(val_loss_cls))[0]
    loss3 = np.mean(np.array(val_loss_seg))[0]
    loss = np.mean(np.array(val_loss))[0]    

    writer.add_scalar('val_loss_mse', loss1, epoch + 1)
    writer.add_scalar('val_loss_cls', loss2, epoch + 1)
    writer.add_scalar('val_loss_seg', loss3, epoch + 1)
    writer.add_scalar('val_loss', loss, epoch + 1)
    writer.add_scalar('mae', mae, epoch + 1)
    writer.add_scalar('mse', mse, epoch + 1)


    if mae < train_record['best_mae']:
        train_record['best_mae'] = mae
        train_record['mse'] = mse
        train_record['corr_epoch'] = epoch + 1
        train_record['corr_loss'] = loss        

    print '='*50
    print exp_name
    print '    '+ '-'*20
    print '    [mae %.1f mse %.1f], [val loss %.8f %.8f %.4f %.4f]' % (mae, mse, loss, loss1, loss2, loss3)         
    print '    '+ '-'*20
    # pdb.set_trace()
    print '[best] [mae %.1f mse %.1f], [loss %.8f], [epoch %d]' % (train_record['best_mae'], train_record['mse'], train_record['corr_loss'], train_record['corr_epoch'])
    print '='*50
Example #9
0
# model_path='./exp/VGG_Decoder_Original_NTU_normal_ab_only_50/05-18_01-23_NTU_VGG_DECODER_1e-06_normal_ab_only/all_ep_27_mae_0.70_mse_0.96.pth'
# model_path = './exp/Res50_Original_GCC_Inducing_CAP_0.0001_epochs_100_Finetuning/0.7/03-08_12-37_GCC_Res50__1e-05_finetuned_rd/all_ep_29_mae_32.5_mse_93.2.pth'
# pruned_model_path = './exp/Res50_Original_GCC_Inducing_CAP_0.0001_epochs_100_Pruning/0.7/resnet50_GCC_pruned_0.7.pth.tar'
# pruned_model_path = './exp/VGG_Decoder_GCC_Pretrained_Pruning/0.4/VGG_Decoder_GCC_pruned_0.4.pth.tar'

# model_path='05-ResNet-50_all_ep_35_mae_32.4_mse_76.1.pth'

net = CrowdCounter(cfg.GPU_ID, cfg.NET)
# net = CrowdCounter(cfg.GPU_ID,cfg.NET,cfg=torch.load(pruned_model_path)['cfg'])
state_dict = torch.load(args.model_path)

try:
    net.load_state_dict(state_dict['net'])
except KeyError:
    net.load_state_dict(state_dict)
net.cuda()
net.eval()
sum([param.nelement() for param in net.parameters()])


def get_concat_h(im1, im2):
    dst = Image.new('RGB', (im1.width + im2.width, im1.height))
    dst.paste(im1, (0, 0))
    dst.paste(im2, (im1.width, 0))
    return dst


cm = plt.get_cmap('jet')

file_folder = []
file_name = []
Example #10
0
def test(file_list, model_path):
    net = CrowdCounter(cfg.GPU_ID, cfg.NET)
    net.cuda()
    net.load_state_dict(torch.load(model_path))
    net.eval()

    gts = []
    preds = []
    f = open(f'submmited.txt', 'w+')

    for infos in file_list:
        filename = infos[:-1]

        imgname = os.path.join(dataRoot, 'img', filename + '.jpg')
        img = Image.open(imgname)
        if img.mode == 'L':
            img = img.convert('RGB')
        img = img_transform(img)[None, :, :, :]
        with torch.no_grad():
            img = Variable(img).cuda()
            crop_imgs, crop_masks = [], []
            b, c, h, w = img.shape
            rh, rw = 576, 768
            for i in range(0, h, rh):
                gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
                    crop_imgs.append(img[:, :, gis:gie, gjs:gje])
                    mask = torch.zeros(b, 1, h, w).cuda()
                    mask[:, :, gis:gie, gjs:gje].fill_(1.0)
                    crop_masks.append(mask)
            crop_imgs, crop_masks = map(lambda x: torch.cat(x, dim=0),
                                        (crop_imgs, crop_masks))

            # forward may need repeatng
            crop_preds = []
            nz, bz = crop_imgs.size(0), 1
            for i in range(0, nz, bz):
                gs, gt = i, min(nz, i + bz)
                crop_pred = net.test_forward(crop_imgs[gs:gt])
                crop_preds.append(crop_pred)
            crop_preds = torch.cat(crop_preds, dim=0)

            # splice them to the original size
            idx = 0
            pred_map = torch.zeros(b, 1, h, w).cuda()
            for i in range(0, h, rh):
                gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
                    pred_map[:, :, gis:gie, gjs:gje] += crop_preds[idx]
                    idx += 1

            # for the overlapping area, compute average value
            mask = crop_masks.sum(dim=0).unsqueeze(0)
            pred_map = pred_map / mask
        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]

        pred = np.sum(pred_map) / LOG_PARA

        print(f'{filename} {pred:.4f}', file=f)
        print(f'{filename} {pred:.4f}')
    f.close()
Example #11
0
def test(file_list, model_path):

    net = CrowdCounter()
    net.load_state_dict(torch.load(model_path))
    # net = tr_net.CNN()
    # net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()

    maes = []
    mses = []

    for filename in file_list:
        print filename
        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')

        # prepare
        wd_1, ht_1 = img.size
        # pdb.set_trace()

        if wd_1 < cfg.DATA.STD_SIZE[1]:
            dif = cfg.DATA.STD_SIZE[1] - wd_1
            img = ImageOps.expand(img, border=(0,0,dif,0), fill=0)
            pad = np.zeros([ht_1,dif])
            den = np.array(den)
            den = np.hstack((den,pad))
            
        if ht_1 < cfg.DATA.STD_SIZE[0]:
            dif = cfg.DATA.STD_SIZE[0] - ht_1
            img = ImageOps.expand(img, border=(0,0,0,dif), fill=0)
            pad = np.zeros([dif,wd_1])
            den = np.array(den)
            den = np.vstack((den,pad))

        img = img_transform(img)

        gt = np.sum(den)

        img = Variable(img[None,:,:,:],volatile=True).cuda()

        #forward
        pred_map = net.test_forward(img)

        pred_map = pred_map.cpu().data.numpy()[0,0,:,:]
        pred = np.sum(pred_map)/100.0

        maes.append(abs(pred-gt))
        mses.append((pred-gt)*(pred-gt))

        
        # vis
        pred_map = pred_map/np.max(pred_map+1e-20)
        pred_map = pred_map[0:ht_1,0:wd_1]
        
        
        den = den/np.max(den+1e-20)
        den = den[0:ht_1,0:wd_1]

        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+'/'+filename_no_ext+'_diff.mat',{'data':diff})
        
        print '[file %s]: [pred %.2f], [gt %.2f]' % (filename, pred, gt)
    print np.average(np.array(maes))
    print np.sqrt(np.average(np.array(mses)))
Example #12
0
def test(file_list, model_path):
    net = CrowdCounter(cfg.GPU_ID, cfg.NET)
    net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()

    maes = AverageMeter()
    mses = AverageMeter()

    step = 0
    time_sampe = 0
    for filename in file_list:
        step = step + 1
        print filename
        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 = sio.loadmat(dataRoot + '/den/' + filename_no_ext + '.mat')
        # den = den['map']

        den = den.astype(np.float32, copy=False)

        img = Image.open(imgname)

        if img.mode == 'L':
            img = img.convert('RGB')

        # prepare
        wd_1, ht_1 = img.size
        # pdb.set_trace()

        # if wd_1 < 1024:
        #     dif = 1024 - wd_1
        #     img = ImageOps.expand(img, border=(0, 0, dif, 0), fill=0)
        #     pad = np.zeros([ht_1, dif])
        #     den = np.array(den)
        #     den = np.hstack((den, pad))
        #
        # if ht_1 < 768:
        #     dif = 768 - ht_1
        #     img = ImageOps.expand(img, border=(0, 0, 0, dif), fill=0)
        #     pad = np.zeros([dif, wd_1])
        #     den = np.array(den)
        #     den = np.vstack((den, pad))

        img = img_transform(img)

        gt_count = np.sum(den)

        img = Variable(img[None, :, :, :], volatile=True).cuda()

        # forward
        pred_map = net.test_forward(img)

        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]
        pred_cnt = np.sum(pred_map) / 2550.0
        pred_map = pred_map / np.max(pred_map + 1e-20)
        pred_map = pred_map[0:ht_1, 0:wd_1]

        den = den / np.max(den + 1e-20)
        den = den[0:ht_1, 0:wd_1]

        maes.update(abs(gt_count - pred_cnt))
        mses.update((gt_count - pred_cnt) * (gt_count - pred_cnt))

    mae = maes.avg
    mse = np.sqrt(mses.avg)

    print '\n[MAE: %fms][MSE: %fms]' % (mae, mse)
Example #13
0
def test(file_list, model_path):
    net = CrowdCounter(cfg.GPU_ID, cfg.NET)
    net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()

    step = 0
    for filename in file_list:
        step = step + 1
    	print filename
        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')

        # prepare
        wd_1, ht_1 = img.size
        # pdb.set_trace()

        # if wd_1 < 1024:
        #     dif = 1024 - wd_1
        #     img = ImageOps.expand(img, border=(0,0,dif,0), fill=0)
        #     pad = np.zeros([ht_1,dif])
        #     den = np.array(den)
        #     den = np.hstack((den,pad))
        #
        # if ht_1 < 768:
        #     dif = 768 - ht_1
        #     img = ImageOps.expand(img, border=(0,0,0,dif), fill=0)
        #     pad = np.zeros([dif,wd_1])
        #     den = np.array(den)
        #     den = np.vstack((den,pad))

        # plt.figure("org-img")
        # plt.imshow(img)
        # plt.show()
        # print img.size



        img = img_transform(img)

        img = Variable(img[None,:,:,:],volatile=True).cuda()

        pred_map = net.test_forward(img)
        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]

        gt_count = np.sum(den)
        pred_cnt = np.sum(pred_map) / 2550.0
        print("gt_%f,et_%f",gt_count,pred_cnt)

        den = den / np.max(den + 1e-20)
        den = den[0:ht_1, 0:wd_1]
        plt.figure("gt-den" + filename)
        plt.imshow(den)
        plt.show()


        pred_map = pred_map / np.max(pred_map + 1e-20)
        pred_map = pred_map[0:ht_1, 0:wd_1]
        plt.figure("pre-den"+filename)
        plt.imshow(pred_map)
        plt.show()
Example #14
0
class Trainer():
    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
        self.net = CrowdCounter(cfg.GPU_ID, self.net_name).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.epoch = 0
        self.i_tb = 0

        self.mae = 1e5
        self.mse = 1e5
        self.ep = 0  # record which epoch gets the better performance

        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

        if cfg.RESUME:
            latest_state = torch.load(cfg.RESUME_PATH)
            self.net.load_state_dict(latest_state['net'])
            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)

    def forward(self):

        # self.validate_V3()
        for epoch in range(self.epoch, cfg.MAX_EPOCH):
            self.epoch = epoch
            if epoch > cfg.LR_DECAY_START:
                self.scheduler.step()

            # training
            self.timer['train time'].tic()
            self.train()
            self.timer['train time'].toc(average=False)

            print('train time: {:.2f}s'.format(self.timer['train time'].diff))
            print('=' * 20)

            # validation
            if (epoch % cfg.VAL_FREQ == 0
                    and epoch > 0) or epoch > cfg.VAL_DENSE_START:
                self.timer['val time'].tic()
                if self.data_mode in [
                        'SHHA', 'SHHB', 'QNRF', 'UCF50', 'Mall', 'FDST'
                ]:
                    self.validate_V1(epoch)
                elif self.data_mode is 'WE':
                    self.validate_V2()
                elif self.data_mode is 'GCC':
                    self.validate_V3()
                self.timer['val time'].toc(average=False)
                print('val time: {:.2f}s'.format(self.timer['val time'].diff))
            torch.save(self.net.cpu().state_dict(),
                       "./weights/Pre_model_{}.pth".format(epoch + 1))
            self.net.cuda()
        print('Best model:', self.ep, 'MAE:', self.mae, 'MSE:', self.mse)

    def train(self):  # training for all datasets
        self.net.train()
        for i, data in enumerate(self.train_loader, 0):
            self.timer['iter time'].tic()
            img, gt_map, img_p, gt_map_p = data
            img = Variable(img).cuda()
            gt_map = Variable(gt_map).cuda()
            img_p = Variable(img_p).cuda()
            gt_map_p = Variable(gt_map_p).cuda()

            self.optimizer.zero_grad()
            pred_map = self.net(img, gt_map, img_p)
            loss = self.net.loss
            loss.backward()
            self.optimizer.step()

            if (i + 1) % cfg.PRINT_FREQ == 0:
                self.i_tb += 1
                #                self.writer.add_scalar('train_loss', loss.item(), self.i_tb)
                self.timer['iter time'].toc(average=False)
                print( '[ep %d][it %d][loss %.4f][lr %.4f][%.2fs]' % \
                        (self.epoch + 1, i + 1, loss.item(), self.optimizer.param_groups[0]['lr']*10000, self.timer['iter time'].diff) )
                print('        [cnt: gt: %.1f pred: %.2f]' %
                      (gt_map[0].sum().data / self.cfg_data.LOG_PARA,
                       pred_map[0].sum().data / self.cfg_data.LOG_PARA))

    def validate_V1(self,
                    epoch):  # validate_V1 for SHHA, SHHB, UCF-QNRF, UCF50

        self.net.eval()

        losses = AverageMeter()
        maes = AverageMeter()
        mses = AverageMeter()

        for vi, data in enumerate(self.val_loader, 0):
            img, gt_map, img_p, gt_map_p = data

            with torch.no_grad():
                img = Variable(img).cuda()
                gt_map = Variable(gt_map).cuda()
                img_p = Variable(img_p).cuda()
                gt_map_p = Variable(gt_map_p).cuda()

                pred_map = self.net.forward(img, gt_map, img_p)

                pred_map = pred_map.data.cpu().numpy()
                gt_map = gt_map.data.cpu().numpy()

                for i_img in range(pred_map.shape[0]):

                    pred_cnt = np.sum(pred_map[i_img]) / self.cfg_data.LOG_PARA
                    gt_count = np.sum(gt_map[i_img]) / self.cfg_data.LOG_PARA

                    losses.update(self.net.loss.item())
                    maes.update(abs(gt_count - pred_cnt))
                    mses.update((gt_count - pred_cnt) * (gt_count - pred_cnt))
#                if vi==0:
#                    vis_results(self.exp_name, self.epoch, self.writer, self.restore_transform, img, pred_map, gt_map)

        mae = maes.avg
        mse = np.sqrt(mses.avg)
        if mae < self.mae:
            self.mae = mae
            self.ep = epoch
        if mse < self.mse:
            self.mse = mse
        loss = losses.avg

        print('[ep %d][loss %.4f][MAE %.4f][MSE %.4f][lr %.4f]' % \
                        (self.epoch + 1, loss, mae, mse, self.optimizer.param_groups[0]['lr']*10000))

#        self.writer.add_scalar('val_loss', loss, self.epoch + 1)
#        self.writer.add_scalar('mae', mae, self.epoch + 1)
#        self.writer.add_scalar('mse', mse, self.epoch + 1)

#        self.train_record = update_model(self.net,self.optimizer,self.scheduler,self.epoch,self.i_tb,self.exp_path,self.exp_name, \
#            [mae, mse, loss],self.train_record)
#        print_summary(self.exp_name,[mae, mse, loss],self.train_record)

    def validate_V2(self):  # validate_V2 for WE

        self.net.eval()

        losses = AverageCategoryMeter(5)
        maes = AverageCategoryMeter(5)

        roi_mask = []
        from datasets.WE.setting import cfg_data
        from scipy import io as sio
        for val_folder in cfg_data.VAL_FOLDER:

            roi_mask.append(
                sio.loadmat(
                    os.path.join(cfg_data.DATA_PATH, 'test',
                                 val_folder + '_roi.mat'))['BW'])

        for i_sub, i_loader in enumerate(self.val_loader, 0):

            mask = roi_mask[i_sub]
            for vi, data in enumerate(i_loader, 0):
                img, gt_map = data

                with torch.no_grad():
                    img = Variable(img).cuda()
                    gt_map = Variable(gt_map).cuda()

                    pred_map = self.net.forward(img, gt_map)

                    pred_map = pred_map.data.cpu().numpy()
                    gt_map = gt_map.data.cpu().numpy()

                    for i_img in range(pred_map.shape[0]):

                        pred_cnt = np.sum(
                            pred_map[i_img]) / self.cfg_data.LOG_PARA
                        gt_count = np.sum(
                            gt_map[i_img]) / self.cfg_data.LOG_PARA

                        losses.update(self.net.loss.item(), i_sub)
                        maes.update(abs(gt_count - pred_cnt), i_sub)
#                    if vi==0:
#                        vis_results(self.exp_name, self.epoch, self.writer, self.restore_transform, img, pred_map, gt_map)

        mae = np.average(maes.avg)
        loss = np.average(losses.avg)

        #        self.writer.add_scalar('val_loss', loss, self.epoch + 1)
        #        self.writer.add_scalar('mae', mae, self.epoch + 1)
        #        self.writer.add_scalar('mae_s1', maes.avg[0], self.epoch + 1)
        #        self.writer.add_scalar('mae_s2', maes.avg[1], self.epoch + 1)
        #        self.writer.add_scalar('mae_s3', maes.avg[2], self.epoch + 1)
        #        self.writer.add_scalar('mae_s4', maes.avg[3], self.epoch + 1)
        #        self.writer.add_scalar('mae_s5', maes.avg[4], self.epoch + 1)

        self.train_record = update_model(self.net,self.optimizer,self.scheduler,self.epoch,self.i_tb,self.exp_path,self.exp_name, \
            [mae, 0, loss],self.train_record)
#        print_WE_summary(self.log_txt,self.epoch,[mae, 0, loss],self.train_record,maes)

    def validate_V3(self):  # validate_V3 for GCC

        self.net.eval()

        losses = AverageMeter()
        maes = AverageMeter()
        mses = AverageMeter()

        c_maes = {
            'level': AverageCategoryMeter(9),
            'time': AverageCategoryMeter(8),
            'weather': AverageCategoryMeter(7)
        }
        c_mses = {
            'level': AverageCategoryMeter(9),
            'time': AverageCategoryMeter(8),
            'weather': AverageCategoryMeter(7)
        }

        for vi, data in enumerate(self.val_loader, 0):
            img, gt_map, attributes_pt = data

            with torch.no_grad():
                img = Variable(img).cuda()
                gt_map = Variable(gt_map).cuda()

                pred_map = self.net.forward(img, gt_map)

                pred_map = pred_map.data.cpu().numpy()
                gt_map = gt_map.data.cpu().numpy()

                for i_img in range(pred_map.shape[0]):

                    pred_cnt = np.sum(pred_map[i_img]) / self.cfg_data.LOG_PARA
                    gt_count = np.sum(gt_map[i_img]) / self.cfg_data.LOG_PARA

                    s_mae = abs(gt_count - pred_cnt)
                    s_mse = (gt_count - pred_cnt) * (gt_count - pred_cnt)

                    losses.update(self.net.loss.item())
                    maes.update(s_mae)
                    mses.update(s_mse)
                    attributes_pt = attributes_pt.squeeze()
                    c_maes['level'].update(s_mae, attributes_pt[i_img][0])
                    c_mses['level'].update(s_mse, attributes_pt[i_img][0])
                    c_maes['time'].update(s_mae, attributes_pt[i_img][1] / 3)
                    c_mses['time'].update(s_mse, attributes_pt[i_img][1] / 3)
                    c_maes['weather'].update(s_mae, attributes_pt[i_img][2])
                    c_mses['weather'].update(s_mse, attributes_pt[i_img][2])

#                if vi==0:
#                    vis_results(self.exp_name, self.epoch, self.writer, self.restore_transform, img, pred_map, gt_map)

        loss = losses.avg
        mae = maes.avg
        mse = np.sqrt(mses.avg)

        #        self.writer.add_scalar('val_loss', loss, self.epoch + 1)
        #        self.writer.add_scalar('mae', mae, self.epoch + 1)
        #        self.writer.add_scalar('mse', mse, self.epoch + 1)

        self.train_record = update_model(self.net,self.optimizer,self.scheduler,self.epoch,self.i_tb,self.exp_path,self.exp_name, \
            [mae, mse, loss],self.train_record)

        print_GCC_summary(self.log_txt, self.epoch, [mae, mse, loss],
                          self.train_record, c_maes, c_mses)
Example #15
0
def test(file_list, model_path, roi):

    net = CrowdCounter(ce_weights=wts)
    net.load_state_dict(torch.load(model_path))
    # net = tr_net.CNN()
    # net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()

    for filename in file_list:
        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).as_matrix()
        den = den.astype(np.float32, copy=False)

        img = Image.open(imgname)

        # prepare
        wd_1, ht_1 = img.size

        if wd_1 < cfg.DATA.STD_SIZE[1]:
            dif = cfg.DATA.STD_SIZE[1] - wd_1
            pad = np.zeros([ht_1, dif])
            img = np.array(img)
            den = np.array(den)
            img = np.hstack((img, pad))
            img = Image.fromarray(img.astype(np.uint8))
            den = np.hstack((den, pad))

        if ht_1 < cfg.DATA.STD_SIZE[0]:
            dif = cfg.DATA.STD_SIZE[0] - ht_1
            pad = np.zeros([dif, wd_1])
            img = np.array(img)
            den = np.array(den)
            # pdb.set_trace()
            img = np.vstack((img, pad))
            img = Image.fromarray(img.astype(np.uint8))

            den = np.vstack((den, pad))

        img = img_transform(img)

        gt = np.sum(den)
        # den = Image.fromarray(den)

        img = img * 255.

        img = Variable(img[None, :, :, :], volatile=True).cuda()

        #forward
        pred_map, pred_cls, pred_seg = net.test_forward(img, roi)

        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]
        pred = np.sum(pred_map)
        pred_map = pred_map / np.max(pred_map + 1e-20)
        pred_map = pred_map[0:ht_1, 0:wd_1]

        den = den / np.max(den + 1e-20)
        den = den[0:ht_1, 0:wd_1]

        den_frame = plt.gca()
        plt.imshow(den)
        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)
        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})
        '''pdb.set_trace()
Example #16
0
def test2(file_list, model_path):

    net = CrowdCounter(cfg.GPU_ID, cfg.NET)
    net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()

    f1 = plt.figure(1)

    gts = []
    preds = []

    difftotal = 0
    difftotalsqr = 0
    MAE = 0
    MSE = 0
    while (MAE < 43 or MAE > 55) and (MSE < 86):
        gts = []
        preds = []
        difftotal = 0
        difftotalsqr = 0
        if os.path.exists(exp_name):
            shutil.rmtree(exp_name)
        if not os.path.exists(exp_name):
            os.mkdir(exp_name)

        if not os.path.exists(exp_name + '/pred'):
            os.mkdir(exp_name + '/pred')

        if not os.path.exists(exp_name + '/gt'):
            os.mkdir(exp_name + '/gt')

        for filename in file_list:
            print(filename)
            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)

            _, ts_hd, ts_wd = img.shape
            dst_size = [256, 512]

            gt = 0
            imgp = img
            denp = den
            it = 0
            while gt < 25 and it < 10:
                it = it + 1
                x1 = random.randint(0, ts_wd - dst_size[1])
                y1 = random.randint(0, ts_hd - dst_size[0])
                x2 = x1 + dst_size[1]
                y2 = y1 + dst_size[0]

                imgp = img[:, y1:y2, x1:x2]
                denp = den[y1:y2, x1:x2]

                gt = np.sum(denp)
                if gt < 20 and it == 10:
                    it = 0

            with torch.no_grad():
                imgp = Variable(imgp[None, :, :, :]).cuda()
                pred_map = net.test_forward(imgp)

            sio.savemat(exp_name + '/pred/' + filename_no_ext + '.mat',
                        {'data': pred_map.squeeze().cpu().numpy() / 100.})
            sio.savemat(exp_name + '/gt/' + filename_no_ext + '.mat',
                        {'data': denp})

            pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]

            pred = np.sum(pred_map) / 100.0
            pred_map = pred_map / np.max(pred_map + 1e-20)

            denp = denp / np.max(denp + 1e-20)

            den_frame = plt.gca()
            plt.imshow(denp, '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()

            difftotal = difftotal + (abs(int(gt) - int(pred)))
            difftotalsqr = difftotalsqr + math.pow(int(gt) - int(pred), 2)

            # sio.savemat(exp_name+'/'+filename_no_ext+'_pred_'+str(float(pred))+'.mat',{'data':pred_map})

            diff = denp - 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+'/'+filename_no_ext+'_diff.mat',{'data':diff})
        MAE = float(difftotal) / 182
        MSE = math.sqrt(difftotalsqr / 182)
        print('MAE : ' + str(MAE))
        print('MSE : ' + str(MSE))
Example #17
0
def test(file_list, model_path):

    net = CrowdCounter(cfg.GPU_ID, 'CANNet')
    net.cuda()
    net.load_state_dict(torch.load(model_path))
    net.eval()

    gts = []
    preds = []

    for i in range(len(img_paths)):
        try:
            img = Image.open(img_paths[i])
        except:
            #img_paths.remove(img_paths[i])
            print(img_paths[i])
            preds.append(10)
            continue
        if img.mode == 'L':
            img = img.convert('RGB')
        img = img_transform(img)[None, :, :, :]
        with torch.no_grad():
            img = Variable(img).cuda()
            crop_imgs, crop_masks = [], []
            b, c, h, w = img.shape
            rh, rw = 576, 768
            for i in range(0, h, rh):
                gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
                    crop_imgs.append(img[:, :, gis:gie, gjs:gje])
                    mask = torch.zeros(b, 1, h, w).cuda()
                    mask[:, :, gis:gie, gjs:gje].fill_(1.0)
                    crop_masks.append(mask)
            crop_imgs, crop_masks = map(lambda x: torch.cat(x, dim=0),
                                        (crop_imgs, crop_masks))

            # forward may need repeatng
            crop_preds = []
            nz, bz = crop_imgs.size(0), 1
            for i in range(0, nz, bz):
                gs, gt = i, min(nz, i + bz)
                crop_pred = net.test_forward(crop_imgs[gs:gt])
                #print('cropsize',crop_pred.size(),crop_imgs[gs:gt].size())
                crop_preds.append(crop_pred)
            crop_preds = torch.cat(crop_preds, dim=0)

            #print(img_paths[i],b,h,w,crop_imgs.size())

            # splice them to the original size
            idx = 0
            pred_map = torch.zeros(b, 1, h, w).cuda()
            for i in range(0, h, rh):
                gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
                for j in range(0, w, rw):
                    gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
                    #print('in for',crop_preds[idx].size())
                    pred_map[:, :, gis:gie, gjs:gje] += crop_preds[idx]
                    idx += 1

            # for the overlapping area, compute average value
            mask = crop_masks.sum(dim=0).unsqueeze(0)
            pred_map = pred_map / mask
        pred_map = pred_map.cpu().data.numpy()[0, 0, :, :]

        pred = np.sum(pred_map) / LOG_PARA
        preds.append(pred)
    df = pd.DataFrame()
    df['file'] = [os.path.basename(x) for x in img_paths]
    df['man_count'] = preds
    df['man_count'] = df['man_count'].round()
    df['man_count'] = df['man_count'].astype(int)
    df.loc[df['man_count'] > 100, 'man_count'] = 100
    df.loc[df['man_count'] < 0, 'man_count'] = 0
    df.to_csv('newonline_21.csv', index=None)