Ejemplo n.º 1
0
def proc_dcrf():
    names = os.listdir(path_save_train_voc_prob)
    pool = Pool(4)
    pool.map(one_dcrf, names)
    #for name in tqdm(names):
    #    one_dcrf(name)
    miou = evaluate_iou(path_save_train_voc_crf, voc_save_gt_dir, c_output)
    return miou
Ejemplo n.º 2
0
def test(model):
    print("============================= TEST ============================")
    model.switch_to_eval()
    for i, (img, name, WW, HH) in tqdm(enumerate(voc_val_loader), desc='testing'):
        model.test(img, name, WW, HH)
    model.switch_to_train()
    miou = evaluate_iou(opt.results_dir, voc_val_gt_dir, c_output)
    model.performance = {'miou': miou}
    return miou
Ejemplo n.º 3
0
def test(model):
    print("============================= TEST ============================")
    model.switch_to_eval()
    for i, (img, name, WW, HH) in tqdm(enumerate(voc_test_loader), desc='testing'):
        model.test(img, name, WW, HH)
    model.switch_to_train()
    miou = evaluate_iou(opt.results_dir, voc_val_gt_dir, c_output)
    model.performance = {'miou': miou}
    print(miou)
    with open('val_voc.json', 'w') as f:
        json.dump(model.performance, f)
    return model.performance
Ejemplo n.º 4
0
def val_voc():
    net.eval()
    with torch.no_grad():
        for t in range(10):
            for it, (img, gt, batch_name, WW,
                     HH) in tqdm(enumerate(voc_loader), desc='train'):
                gt_cls = gt[:, None, ...] == torch.arange(c_output)[None, ...,
                                                                    None, None]
                gt_cls = (gt_cls.sum(3, keepdim=True).sum(2, keepdim=True) >
                          0).float().cuda()
                raw_img = img
                img = (img.cuda() - mean) / std
                batch_seg, _, seg32x = net(img)
                batch_seg[:, 1:] *= gt_cls[:, 1:]
                batch_prob = F.softmax(batch_seg, 1)
                _, batch_seg = batch_seg.detach().max(1)
                for n, name in enumerate(batch_name):
                    msk = batch_seg[n]
                    prob = batch_prob[n]
                    prob = prob.detach().cpu().numpy()
                    np.save(
                        '{}/{}_{}.npy'.format(path_save_train_voc_prob, name,
                                              t), prob)

                    _img = raw_img[n] * 255
                    _img = _img.numpy().astype(np.uint8)
                    _img = _img.transpose((1, 2, 0))
                    _img = Image.fromarray(_img)
                    _img.save('{}/{}_{}.jpg'.format(voc_save_img_dir, name, t))

                    _gt = gt[n]
                    _gt = _gt.numpy()
                    _gt = _gt.astype(np.uint8)
                    _gt = Image.fromarray(_gt.astype(np.uint8))
                    _gt = _gt.convert('P')
                    _gt.putpalette(palette_voc)
                    _gt.save('{}/{}_{}.png'.format(voc_save_gt_dir, name, t),
                             'PNG')

                    msk = msk.detach().cpu().numpy()
                    msk = Image.fromarray(msk.astype(np.uint8))
                    msk = msk.convert('P')
                    msk.putpalette(palette_voc)
                    msk.save(
                        '{}/{}_{}.png'.format(path_save_train_voc, name, t),
                        'PNG')
        miou = evaluate_iou(path_save_train_voc, voc_save_gt_dir, c_output)
        net.train()
        return miou
Ejemplo n.º 5
0
def val_voc():
    net.eval()
    with torch.no_grad():
        for it, (img, gt, batch_name, WW,
                 HH) in tqdm(enumerate(voc_val_loader), desc='train'):
            img = (img.cuda() - mean) / std
            outputs = net(img)
            batch_seg = outputs[0]
            _, batch_seg = batch_seg.detach().max(1)
            for n, name in enumerate(batch_name):
                msk = batch_seg[n]
                msk = msk.detach().cpu().numpy()
                w = WW[n]
                h = HH[n]
                msk = Image.fromarray(msk.astype(np.uint8))
                msk = msk.convert('P')
                msk.putpalette(palette_voc)
                msk = msk.resize((w, h))
                msk.save('{}/{}.png'.format(path_save_valid_voc, name), 'PNG')
        miou = evaluate_iou(path_save_valid_voc, voc_val_gt_dir, c_output)
        net.train()
        return miou
Ejemplo n.º 6
0
def syn(model):
    print("============================= TEST ============================")
    model.switch_to_eval()
    _rdir = model.opt.results_dir
    model.opt.results_dir = '/'.join(model.opt.results_dir.split('/')[:-1])+'/syn_train'
    if not os.path.exists(model.opt.results_dir):
        os.mkdir(model.opt.results_dir)
    for i, (img, gt, name, WW, HH) in tqdm(enumerate(voc_train_loader), desc='testing'):
        bsize = img.size(0)
        gt[gt == -1] = c_output
        gt = torch.zeros(bsize, c_output + 1, opt.imageSize, opt.imageSize).scatter(1, gt.unsqueeze(1), 1)
        gt = gt.sum(3).sum(2)
        gt = gt[:, :c_output]
        gt = gt[:, 1:]
        gt[gt>0] = 1
        model.test(img, name, WW, HH, gt, save_prob=True)
    model.switch_to_train()
    miou = evaluate_iou(model.opt.results_dir, voc_train_gt_dir, c_output)
    model.opt.results_dir = _rdir
    model.performance = {'miou': miou}
    print(miou)
    with open(model.save_dir+'/'+'syn-log.json', 'w') as outfile:
        json.dump(model.performance, outfile)
    return miou