예제 #1
0
	def __getitem__(self, idx):
		if not len(self.origin_imgs[idx]):
			origin_img = read_img(self.filenames[idx])

			if not (len(self.sigma_s) and len(self.sigma_c)):
				# all random
				min_log = np.log([0.000001])
				sigma_s = min_log + np.random.rand(1) * (np.log([0.002]) - min_log)
				sigma_s = np.exp(sigma_s)
				sigma_c = min_log + np.random.rand(1) * (np.log([0.001]) - min_log)
				sigma_c = np.exp(sigma_c)
			else:
				sigma_s = self.sigma_s
				sigma_c = self.sigma_c

			self.origin_imgs[idx], self.noise_imgs[idx] = self.isp.cbdnet_noise_generate_srgb(origin_img, sigma_s, sigma_c)

		origin_img = self.origin_imgs[idx]
		noise_img = self.noise_imgs[idx]

		patch_origin_img, patch_noise_img = get_patch(origin_img, noise_img, self.patch_size, self.random)

		patch_origin_img_chw = hwc_to_chw(patch_origin_img)
		patch_noise_img_chw = hwc_to_chw(patch_noise_img)

		return patch_noise_img_chw, patch_origin_img_chw
예제 #2
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    img_height = full_img.size[1]
    img_width = full_img.size[0]

    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)

    left_square, right_square = split_img_into_squares(img)

    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)

    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    with torch.no_grad():
        output_left = net(X_left)
        output_right = net(X_right)

        left_probs = F.sigmoid(output_left).squeeze(0)
        right_probs = F.sigmoid(output_right).squeeze(0)
        '''
        tf = transforms.Compose(
                [
                    transforms.ToPILImage(),
                    transforms.Resize(img_height),
                    transforms.ToTensor()
                ]
            )
        '''
        tf = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Scale(img_height),
            transforms.ToTensor()
        ])

        left_probs = tf(left_probs.cpu())
        right_probs = tf(right_probs.cpu())

        left_mask_np = left_probs.squeeze().cpu().numpy()
        right_mask_np = right_probs.squeeze().cpu().numpy()

    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    if use_dense_crf:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask > out_threshold
예제 #3
0
    def __getitem__(self, idx):
        clean_fn = random.choice(self.clean_fns[idx])

        clean_img = read_img(clean_fn)
        noise_img = read_img(clean_fn.replace('GT_SRGB', 'NOISY_SRGB'))

        if self.patch_size > 0:
            [clean_img, noise_img] = get_patch([clean_img, noise_img],
                                               self.patch_size)

        return hwc_to_chw(noise_img), hwc_to_chw(clean_img), np.zeros(
            (3, self.patch_size, self.patch_size)), np.zeros(
                (3, self.patch_size, self.patch_size))
예제 #4
0
    def __getitem__(self, idx):
        clean_fn = random.choice(self.clean_fns[idx])

        clean_img = read_img(clean_fn)
        noise_img = read_img(clean_fn.replace('GT_SRGB', 'NOISY_SRGB'))

        if self.patch_size > 0:
            clean_img, noise_img = get_patch(clean_img, noise_img,
                                             self.patch_size)

        clean_img_chw = hwc_to_chw(clean_img)
        noise_img_chw = hwc_to_chw(noise_img)

        return noise_img_chw, clean_img_chw
예제 #5
0
    def __getitem__(self, idx):
        clean_fn = random.choice(self.clean_fns[idx])

        clean_img = read_img(clean_fn)
        noise_img = read_img(clean_fn.replace('GT_SRGB', 'NOISY_SRGB'))
        sigma_img = read_img(clean_fn.replace(
            'GT_SRGB', 'SIGMA_SRGB')) / 15.  # inverse scaling

        if self.patch_size > 0:
            [clean_img, noise_img,
             sigma_img] = get_patch([clean_img, noise_img, sigma_img],
                                    self.patch_size)

        return hwc_to_chw(noise_img), hwc_to_chw(clean_img), hwc_to_chw(
            sigma_img), np.ones((3, self.patch_size, self.patch_size))
예제 #6
0
	def __getitem__(self, idx):
		batch_path = os.path.join(self.root_dir, self.batches[idx])

		if not len(self.origin_imgs[idx]):
			origin_fns = glob.glob(batch_path + '/Reference.png')
			noise_fns = glob.glob(batch_path + '/Noisy.png')

			self.origin_imgs[idx] = read_img(origin_fns[0])

			for noise_fn in noise_fns:
				self.noise_imgs[idx].append(read_img(noise_fn))
				
		origin_img = self.origin_imgs[idx]
		noise_img = self.noise_imgs[idx][np.random.randint(len(self.noise_imgs[idx]))]

		patch_origin_img, patch_noise_img = get_patch(origin_img, noise_img, self.patch_size, self.random)

		patch_origin_img_chw = hwc_to_chw(patch_origin_img)
		patch_noise_img_chw = hwc_to_chw(patch_noise_img)

		return patch_noise_img_chw, patch_origin_img_chw
예제 #7
0
def predict_img(net,
                full_img,
                device,
                scale_factor=1,
                out_threshold=0.5,
                use_dense_crf=False):
    net.eval()
    img_height = full_img.size[1]

    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)
    img = hwc_to_chw(img)

    X = torch.from_numpy(img).unsqueeze(0)

    X = X.to(device=device)

    with torch.no_grad():
        output = net(X)

        if net.n_classes > 1:
            probs = F.softmax(output, dim=1)
        else:
            probs = torch.sigmoid(output)

        probs = probs.squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.Resize(img_height),
                transforms.ToTensor()
            ]
        )

        probs = tf(probs.cpu())

        full_mask = probs.squeeze().cpu().numpy()

    if use_dense_crf:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask > out_threshold
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    net.eval()
    img_height = full_img.size[1]
    img_width = full_img.size[0]

    img = resize_and_crop_y(full_img, scale=scale_factor)
    img = normalize(img)
    img = hwc_to_chw(img)
    X_img = torch.from_numpy(img).unsqueeze(0)

    if use_gpu:
        X_img = X_img.cuda()

    with torch.no_grad():
        output_img = net(X_img)
        img_probs = output_img.squeeze(0)

        tf = transforms.Compose([
            transforms.ToPILImage(),
            #transforms.Resize(),
            transforms.ToTensor()
        ])

        img_probs = tf(img_probs.cpu())
        img_mask = img_probs.squeeze().cpu().numpy()

    #full_mask = merge_masks(left_mask_np, right_mask_np, img_width)
    full_mask = img_mask
    print(full_mask)

    #if use_dense_crf:
    #    full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    #return full_mask > out_threshold
    return full_mask
예제 #9
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):
    net.eval()


    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)
    img = hwc_to_chw(img)
    X = torch.from_numpy(img).unsqueeze(0)

    if use_gpu:
        X = X.cuda()

    with torch.no_grad():
        output = net(X)
        probs = output.squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.ToTensor()
            ]
        )

        probs = tf(probs.cpu())
        print("Probs - ", probs.shape)
        mask = probs.squeeze().cpu().numpy()
        print("Mask - ", mask.shape)
        plt.imshow(mask)
        plt.show()

    if use_dense_crf:
        mask = dense_crf(np.array(full_img).astype(np.uint8), mask)

    return mask > out_threshold
예제 #10
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    net.eval()
    img_height = full_img.size[0]
    img_width = full_img.size[1]

    # img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(full_img)

    # left_square, right_square = split_img_into_squares(img)

    img = hwc_to_chw(img)

    img = torch.from_numpy(img).unsqueeze(0)

    if use_gpu:
        img = img.cuda()

    with torch.no_grad():
        output_img = net(img)

        img_probs = output_img.squeeze(0)
        img_mask_np = img_probs.squeeze().cpu().numpy()
        # img_mask_np=np.transpose(img_mask_np, axes=[1.txt, 2, 0])
        mask_pred = (img_mask_np > 0.5).float()
        # out_img=np.zeros((mask_pred.shape[0],mask_pred.shape[1.txt],3))
        # for i in range(mask_pred.shape[0]):
        #     for j in range(mask_pred.shape[1.txt]):
        #         out_img[i,j]=colormap[np.argmax(mask_pred[i,j])]

    return mask_pred
예제 #11
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    # eval mode fixes BN and dropout, which yields bad results
    # net.eval()

    img_tensor = torch.from_numpy(hwc_to_chw(full_img))
    if use_gpu:
        img_tensor = img_tensor.cuda()

    with torch.no_grad():
        input = img_tensor.unsqueeze(0)
        print("input.shape: ", input.shape)
        full_mask = net(input).squeeze().cpu().numpy()
        print("full_mask.shape: ", full_mask.shape)

    if out_threshold < 0:
        return full_mask

    return full_mask > out_threshold
예제 #12
0
def predict_img_batch(net,
                      full_img,
                      scale_factor=0.5,
                      out_threshold=0.5,
                      use_dense_crf=True,
                      use_gpu=True):
    """return fullmask with size (C, H, W)"""
    net.eval()
    img_height = full_img.size[1]
    img_width = full_img.size[0]
    print('imgheight', img_height)
    print('imgwidth', img_width)
    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)

    if len(img.shape) == 2:
        img = img[..., np.newaxis]

    print('img.shape', img.shape)
    left_square, right_square = split_img_into_squares(img)

    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)

    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    with torch.no_grad():
        output_left = net(X_left)
        output_right = net(X_right)

        print('output_left.shape', output_left.shape)
        print('output_right.shape', output_right.shape)
        left_probs = output_left.squeeze(0)
        right_probs = output_right.squeeze(0)
        #
        # if not scale_factor==1:
        #     tf = transforms.Compose(
        #         [
        #             transforms.ToPILImage(),
        #             transforms.Resize(img_height),
        #             transforms.ToTensor()
        #         ]
        #     )
        #
        #     left_probs = tf(left_probs.cpu())
        #     right_probs = tf(right_probs.cpu())
        # print('left_probs', left_probs.shape)
        # print('right_probs', right_probs.shape)
        left_mask_np = left_probs.squeeze().cpu().numpy()
        right_mask_np = right_probs.squeeze().cpu().numpy()
        left_mask_np = np.transpose(left_mask_np, axes=[1, 2, 0])
        right_mask_np = np.transpose(right_mask_np, axes=[1, 2, 0])
        if not scale_factor == 1:
            right_mask_np = resize_np(right_mask_np, 1 / scale_factor)
            left_mask_np = resize_np(left_mask_np, 1 / scale_factor)
        print('left_mask_np', left_mask_np.shape)
        print('right_mask_np', right_mask_np.shape)
    left_mask_np = np.transpose(left_mask_np, axes=[2, 0, 1])
    right_mask_np = np.transpose(right_mask_np, axes=[2, 0, 1])
    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    # if use_dense_crf:
    if 0:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask
예제 #13
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=False,
                use_gpu=True):
    img_height = full_img.size[1]
    img = full_img.resize((128, 128))
    img = np.array(img, dtype=np.float32)
    imgs_switched = hwc_to_chw(img)
    imgs_normalized = normalize(imgs_switched)
    imgs_normalized = torch.from_numpy(imgs_normalized).unsqueeze(0)
    if use_gpu:
        imgs_normalized = imgs_normalized.cuda()
    with torch.no_grad():
        output = net(imgs_normalized)
        prob = torch.sigmoid(output).squeeze(0)

        tf = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize(img_height),
            transforms.ToTensor()
        ])

        prob = tf(prob.cpu())
        full_mask = prob.squeeze().cpu().numpy()
    '''#full_img = full_img.resize((128,128))
    img_height = full_img.size[1]
    img_width = full_img.size[0]
    
    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)

    left_square, right_square = split_img_into_squares(img)

    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)
    
    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    with torch.no_grad():
        output_left = net(X_left)
        output_right = net(X_right)

        left_probs = torch.sigmoid(output_left).squeeze(0)
        right_probs = torch.sigmoid(output_right).squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.Resize(img_height),
                transforms.ToTensor()
            ]
        )
        
        left_probs = tf(left_probs.cpu())
        right_probs = tf(right_probs.cpu())

        left_mask_np = left_probs.squeeze().cpu().numpy()
        right_mask_np = right_probs.squeeze().cpu().numpy()
        #print(left_mask_np.shape, right_mask_np.shape)
    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)'''
    ipdb.set_trace()
    if use_dense_crf:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask > out_threshold
예제 #14
0
parser.add_argument('input_filename', type=str)
parser.add_argument('output_filename', type=str)
args = parser.parse_args()

save_dir = './save_model/'

model = Network()
model.cuda()
model = nn.DataParallel(model)

model.eval()

if os.path.exists(os.path.join(save_dir, 'checkpoint.pth.tar')):
    # load existing model
    model_info = torch.load(os.path.join(save_dir, 'checkpoint.pth.tar'))
    model.load_state_dict(model_info['state_dict'])
else:
    print('Error: no trained model detected!')
    exit(1)

input_image = read_img(args.input_filename)
input_var = torch.from_numpy(hwc_to_chw(input_image)).unsqueeze(0).cuda()

with torch.no_grad():
    _, output = model(input_var)

output_image = chw_to_hwc(output[0, ...].cpu().numpy())
output_image = np.uint8(np.round(np.clip(output_image, 0, 1) *
                                 255.))[:, :, ::-1]

cv2.imwrite(args.output_filename, output_image)
예제 #15
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    net.eval()
    img_height = full_img.size[1]
    img_width = full_img.size[0]

    # img = resize_and_crop(full_img, scale=scale_factor)
    # img = normalize(img)
    img = np.array(full_img, dtype=np.float32)
    img = normalize(img)

    # left_square, right_square = split_img_into_squares(img)
    #
    # left_square = hwc_to_chw(left_square)
    # right_square = hwc_to_chw(right_square)
    square = hwc_to_chw(img)
    #
    # X_left = torch.from_numpy(left_square).unsqueeze(0)
    # X_right = torch.from_numpy(right_square).unsqueeze(0)
    img = torch.from_numpy(square).unsqueeze(0)
    
    if use_gpu:
        # X_left = X_left.cuda()
        # X_right = X_right.cuda()
        img = img.cuda()

    with torch.no_grad():
        # output_left = net(X_left)
        # output_right = net(X_right)
        output = net(img)

        # left_probs = output_left.squeeze(0)
        # right_probs = output_right.squeeze(0)
        # probs = output # .squeeze(0)
        # arr_probs = np.array(probs.cpu())
        # arr_probs = np.transpose

        # tf = transforms.Compose(
        #     [
        #         # transforms.ToPILImage(),
        #         # transforms.Resize(img_height),
        #         transforms.ToTensor()
        #     ]
        # )
        
        # left_probs = tf(left_probs.cpu())
        # right_probs = tf(right_probs.cpu())
        # probs = tf(arr_probs)

        # left_mask_np = left_probs.squeeze().cpu().numpy()
        # right_mask_np = right_probs.squeeze().cpu().numpy()
        mask_np = output.squeeze().cpu().numpy()

    # full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    if use_dense_crf:
        # full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), mask_np)

    # return full_mask > out_threshold
    return mask_np > out_threshold
예제 #16
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_gpu=False):
    pst = time.perf_counter()
    net.eval()
    #print(' 0 running time: %s seconds ' %(( time.clock() -pst)))

    img_height = full_img.size[1]
    # print(img_height)
    img_width = full_img.size[0]
    # print(full_img.size)  # 2048,1229
    img = resize_and_crop(full_img, scale=scale_factor)
    #pdb.set_trace()
    #print(' 1 running time: %s seconds ' %(( time.clock() -pst)))

    img = normalize(img)

    #print(' 2 running time: %s seconds ' %(( time.clock() -pst)))
    # print(img.shape)   # 614,1024,3
    left_square, right_square = split_img_into_squares(img)
    # print(right_square.shape)     # 614,614,3
    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    #print(' 3 running time: %s seconds ' %(( time.clock() -pst)))

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)

    #print(' 4 running time: %s seconds ' %(( time.clock() -pst)))

    #outstart = time.clock()
    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    #print(' 5 running time: %s seconds ' %(( time.clock() -pst)))

    with torch.no_grad():
        torch.cuda.synchronize()
        st = time.perf_counter()
        # print(X_left.shape)   # 1,3,614,614
        output_left = net(X_left)
        output_right = net(X_right)
        torch.cuda.synchronize()
        st1 = time.perf_counter()
        #outend = time.clock()
        print(' Unet++ --------------------> running time: %s seconds ' %
              (st1 - st))
        left_probs = output_left.squeeze(0)
        right_probs = output_right.squeeze(0)
        # print(' squeeze running time: %s seconds ' %((time.perf_counter()-st1)))

        if (left_probs.shape[1] != img_height):
            tf = transforms.Compose([
                transforms.ToPILImage(),
                transforms.Resize(img_height),
                transforms.ToTensor()
            ])
            left_probs = tf(left_probs.cpu())
            right_probs = tf(right_probs.cpu())
            print("Transform done!")
        #print(' 8  running time: %s seconds ' %(( time.clock() -pst)))
        #lstart = time.clock()

        #left_probs.cpu()
        #print(' transforms running time: %s seconds ' %(( time.time() -pst)))
        st = time.perf_counter()
        left_mask_np = left_probs.squeeze().cpu().numpy()
        end1 = time.perf_counter() - st
        #print(left_probs.shape)
        #pdb.set_trace()
        # print(' tonumpy1 running time: %s seconds ' %(end1))
        st = time.perf_counter()
        right_mask_np = right_probs.squeeze().cpu().numpy()
        end2 = time.perf_counter() - st
        # print(' tonumpy2 running time: %s seconds ' %(end2))

    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    # print(' 9 running time: %s seconds ' %(( time.perf_counter() -pst)))

    #pdb.set_trace()
    full_mask[full_mask >= out_threshold] = 1
    full_mask[full_mask < out_threshold] = 0
    #-------------------------------------------------------------------------------

    #newmask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)
    #lend = time.clock()
    #print(' running time: %s seconds ' %((lend-pst)))
    #pdb.set_trace()
    return full_mask > out_threshold