示例#1
0
    def __getitem__(self, index):
        path = self.test_path[index]
        images = {}

        images.update({'name': path['save']})
        input_ = cv2.imread(path['input'])
        input_ = cv2.cvtColor(input_, cv2.COLOR_BGR2RGB)

        target_ = cv2.imread(path['target'])
        target_ = utils.modcrop(target_, self.sr_factor)
        target_ = cv2.cvtColor(target_, cv2.COLOR_BGR2RGB)

        if not self.rgb:
            input_out = np.copy(input_)
            input_out = utils.np2tensor(input_out, self.rgb_range)
            # print(input_out)
            input_ = utils.rgb2ycbcr(input_)
            input_cbcr = input_[:, :, 1:]
            input_ = np.expand_dims(input_[:, :, 0], 2)
            input_cbcr = utils.np2tensor(input_cbcr, self.rgb_range)
            images.update({'input_cbcr': input_cbcr, 'input_rgb': input_out})

        if self.target_down:
            target_down = imresize(target_, scalar_scale=1 / self.sr_factor)
            target_down = utils.np2tensor(target_down, self.rgb_range)
            images.update({'target_down': target_down})

        input_ = utils.np2tensor(input_, self.rgb_range)
        target_ = utils.np2tensor(target_, self.rgb_range)

        images.update({'input': input_, 'target': target_})
        return images
示例#2
0
    def __getitem__(self, index):
        input_ = self.input[index, :, :, :]
        target_ = self.target[index, :, :, :]

        subim_in, subim_tar = get_patch(input_, target_, self.patch_size,
                                        self.sr_factor)

        if not self.rgb:
            subim_in = utils.rgb2ycbcr(subim_in)
            subim_tar = utils.rgb2ycbcr(subim_tar)
            subim_in = np.expand_dims(subim_in[:, :, 0], 2)
            subim_tar = np.expand_dims(subim_tar[:, :, 0], 2)

        if self.input_up:
            subim_bic = imresize(subim_in, scalar_scale=self.sr_factor)
            subim_in = utils.np2tensor(subim_in, self.rgb_range)
            subim_tar = utils.np2tensor(subim_tar, self.rgb_range)
            subim_bic = utils.np2tensor(subim_bic, self.rgb_range)
            return {
                'input': subim_in,
                'target': subim_tar,
                'input_up': subim_bic
            }

        subim_in = utils.np2tensor(subim_in, self.rgb_range)
        subim_tar = utils.np2tensor(subim_tar, self.rgb_range)

        return {'input': subim_in, 'target': subim_tar}
示例#3
0
    def __init__(self, app: QApplication, img: str) -> None:
        super().__init__()
        self.setStyleSheet('background-color: gray;')
        self.margin = 300
        img = Image.open(img)
        #w, h = img.size
        #img = img.resize((2 * w, 2 * h), Image.NEAREST)
        self.img = np.array(img)
        self.img_tensor = utils.np2tensor(self.img).cuda()
        self.img_h = self.img.shape[0]
        self.img_w = self.img.shape[1]

        self.offset_h = self.margin
        self.offset_w = self.img_w + 2 * self.margin

        window_h = self.img_h + 2 * self.margin
        window_w = 2 * self.img_w + 3 * self.margin

        monitor_resolution = app.desktop().screenGeometry()
        screen_h = monitor_resolution.height()
        screen_w = monitor_resolution.width()

        screen_offset_h = (screen_h - window_h) // 2
        screen_offset_w = (screen_w - window_w) // 2

        self.setGeometry(screen_offset_w, screen_offset_h, window_w, window_h)
        self.reset_cps()
        self.line_order = ('tl', 'tr', 'br', 'bl')
        self.grab = None

        self.inter = cv2.INTER_CUBIC
        self.inter_idx = 2
        self.backend = 'opencv'
        self.update()
        return
示例#4
0
    def __getitem__(self, index):
        path = self.train_path[index]
        images = {}
        if self.npy_reader:
            input_ = np.load(path['input'], allow_pickle=False)

            target_ = np.load(path['target'], allow_pickle=False)
            target_ = utils.modcrop(target_, self.sr_factor)
        else:
            input_ = cv2.imread(path['input'])
            input_ = cv2.cvtColor(input_, cv2.COLOR_BGR2RGB)

            target_ = cv2.imread(path['target'])
            target_ = utils.modcrop(target_, self.sr_factor)
            target_ = cv2.cvtColor(target_, cv2.COLOR_BGR2RGB)

        # for i in range(10):
        #     subim_in, subim_tar = get_patch(input_, target_, self.patch_size, self.sr_factor)
        # win_mean = ndimage.uniform_filter(subim_in[:, :, 0], (5, 5))
        # win_sqr_mean = ndimage.uniform_filter(subim_in[:, :, 0]**2, (5, 5))
        # win_var = win_sqr_mean - win_mean**2
        #
        # if np.sum(win_var) / (win_var.shape[0]*win_var.shape[1]) > 30:
        #     break

        subim_in, subim_tar = get_patch(input_, target_, self.patch_size,
                                        self.sr_factor)

        if not self.rgb:
            subim_in = utils.rgb2ycbcr(subim_in)
            subim_tar = utils.rgb2ycbcr(subim_tar)
            subim_in = np.expand_dims(subim_in[:, :, 0], 2)
            subim_tar = np.expand_dims(subim_tar[:, :, 0], 2)

        if self.target_down:
            subim_target_down = imresize(subim_tar,
                                         scalar_scale=1 / self.sr_factor)
            subim_target_down = utils.np2tensor(subim_target_down,
                                                self.rgb_range)
            images.update({'target_down': subim_target_down})

        subim_in = utils.np2tensor(subim_in, self.rgb_range)
        subim_tar = utils.np2tensor(subim_tar, self.rgb_range)
        images.update({'input': subim_in, 'target': subim_tar})
        return images
示例#5
0
    def __getitem__(self, index):
        input_ = np.load(self.input_path[index])
        target_ = np.load(self.target_path[index])

        if not self.rgb:
            input_ = utils.rgb2ycbcr(input_)
            target_ = utils.rgb2ycbcr(target_)

        if self.input_up:
            input_bic = imresize(input_, scalar_scale=self.sr_factor).round()
            input_ = utils.np2tensor(input_, self.rgb_range)
            target_ = utils.np2tensor(target_, self.rgb_range)
            input_bic_ = utils.np2tensor(input_bic, self.rgb_range)
            return {'input': input_, 'target': target_, 'input_up': input_bic_}

        input_ = utils.np2tensor(input_, self.rgb_range)
        target_ = utils.np2tensor(target_, self.rgb_range)
        return {'input': input_, 'target': target_}
示例#6
0
    def __getitem__(self, index):
        img_path = self.images_path[index]
        target_ = cv2.imread(img_path)
        input_ = imresize(target_, scalar_scale=1 / self.sr_factor)

        if not self.rgb:
            input_ = utils.rgb2ycbcr(input_)
            target_ = utils.rgb2ycbcr(target_)

        if self.input_up:
            input_bic = imresize(input_, scalar_scale=self.sr_factor).round()
            input_ = utils.np2tensor(input_, self.rgb_range)
            target_ = utils.np2tensor(target_, self.rgb_range)
            input_bic_ = utils.np2tensor(input_bic, self.rgb_range)
            return {'input': input_, 'target': target_, 'input_up': input_bic_}

        input_ = utils.np2tensor(input_, self.rgb_range)
        target_ = utils.np2tensor(target_, self.rgb_range)
        return {'input': input_, 'target': target_}
示例#7
0
    def __getitem__(self, index):
        path = self.img_paths_list[index]
        print(path)
        images = {}

        images.update({'name': path['save']})
        input_ = cv2.imread(path['input'])
        input_ = cv2.cvtColor(input_, cv2.COLOR_BGR2RGB)

        input_ = utils.np2tensor(input_, self.rgb_range)

        images.update({'input': input_, 'target': input_})
        return images
示例#8
0
def main(args):
    X, y = get_appro_syn_data(args.n_samples)
    mean, std = np.mean(y), np.std(y)
    min_y = mean-std
    max_y = mean+std 

    X_train, X_val, X_test, y_train, y_val, y_test = \
        train_val_test_split(X, y, val_size=args.val_size, test_size=args.test_size, random_state=args.seed)

    X_train_noise = corrupt_X(X_train, args.p)
    X_val_noise = corrupt_X(X_val, args.p)

    X_train = np2tensor(X_train)
    X_train_noise = np2tensor(X_train_noise)
    X_val = np2tensor(X_val)
    X_val_noise = np2tensor(X_val_noise)
    X_test = np2tensor(X_test)
    y_train = np2tensor(y_train)
    y_val = np2tensor(y_val)
    y_test = np2tensor(y_test)

    model = Model(args.hid1, args.hid2)

    if torch.cuda.is_available():
        model = model.cuda()

    optimizer = torch.optim.Adam(filter(lambda p : p.requires_grad, model.parameters()), lr=args.lr, weight_decay=args.wdc) 
    
    best_val = 1e20 
    result = None 

    for epoch in tqdm(range(args.n_epochs)):
        #TODO: Mini-batch training
        model.train()
        optimizer.zero_grad()
        y_pred = model(X_train_noise)

        loss = rmse(y_pred,y_train)

        if args.adapt:
            adapt_loss =  appro_loss(y_pred, min_y, max_y)
        else:
            adapt_loss = 0

        total_loss = loss + args.lam * adapt_loss

        total_loss.backward()
        optimizer.step()

        model.eval()
        
        if args.noise_free:
            y_pred = model(X_val)
        else:
            y_pred = model(X_val_noise)

        val_loss = rmse(y_pred,y_val).item()
        if val_loss < best_val:
            best_val = val_loss
            y_pred = model(X_test)
            result = rmse(y_pred,y_test).item()

        if args.debug:
            print("Epoch ", epoch, total_loss.item(), val_loss, result)

    print(result)
示例#9
0
def main(args):
    (X1, y1), (X2, y2), (X3, y3) = get_mono_syn_data(args.n_samples)

    ### Split train val test set
    idx_train, idx_val, idx_test, _, _, _ = \
        train_val_test_split(np.arange(args.n_samples), np.arange(args.n_samples), val_size=args.val_size, test_size=args.test_size, random_state=args.seed)

    X1_train = X1[idx_train]
    X2_train = X2[idx_train]
    X3_train = X3[idx_train]
    X_train = np.vstack([X1_train, X2_train, X3_train])
    y1_train = y1[idx_train]
    y2_train = y2[idx_train]
    y3_train = y3[idx_train]

    X1_val = X1[idx_val]
    X2_val = X2[idx_val]
    X3_val = X3[idx_val]
    X_val = np.vstack([X1_val, X2_val, X3_val])
    y1_val = y1[idx_val]
    y2_val = y2[idx_val]
    y3_val = y3[idx_val]
    y_val = np.concatenate([y1_val, y2_val, y3_val])

    X1_test = X1[idx_test]
    X2_test = X2[idx_test]
    X3_test = X3[idx_test]
    X_test = np.vstack([X1_test, X2_test, X3_test])
    y1_test = y1[idx_test]
    y2_test = y2[idx_test]
    y3_test = y3[idx_test]
    y_test = np.concatenate([y1_test, y2_test, y3_test])

    ### Corrupt data
    y1_train_noise, y2_train_noise1 = corrupt_Y(y1_train, y2_train, args.p)
    y2_train_noise2, y3_train_noise = corrupt_Y(y2_train, y3_train, args.p)

    y1_val_noise, y2_val_noise1 = corrupt_Y(y1_val, y2_val, args.p)
    y2_val_noise2, y3_val_noise = corrupt_Y(y2_val, y3_val, args.p)

    X_train = np2tensor(X_train)
    X_val = np2tensor(X_val)
    X_test = np2tensor(X_test)

    y1_train_noise = np2tensor(y1_train_noise)
    y2_train_noise1 = np2tensor(y2_train_noise1)
    y2_train_noise2 = np2tensor(y2_train_noise2)
    y3_train_noise = np2tensor(y3_train_noise)

    y1_val_noise = np2tensor(y1_val_noise)
    y2_val_noise1 = np2tensor(y2_val_noise1)
    y2_val_noise2 = np2tensor(y2_val_noise2)
    y3_val_noise = np2tensor(y3_val_noise)

    y_test = np2tensor(y_test)

    model = Model(args.hid1, args.hid2)

    if torch.cuda.is_available():
        model = model.cuda()

    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                        model.parameters()),
                                 lr=args.lr,
                                 weight_decay=args.wdc)

    best_val = 1e20
    result = None

    for epoch in tqdm(range(args.n_epochs)):
        #TODO: Mini-batch training
        model.train()
        optimizer.zero_grad()
        y_pred = model(X_train)

        loss = rmse(y_pred[:len(idx_train)],y1_train_noise) + \
            rmse(y_pred[len(idx_train):-len(idx_train)],y2_train_noise1) + \
            rmse(y_pred[len(idx_train):-len(idx_train)],y2_train_noise2) + \
            rmse(y_pred[-len(idx_train):],y3_train_noise)

        if args.adapt:
            adapt_loss = mono_loss(y_pred[:len(idx_train)], y_pred[len(idx_train):-len(idx_train)]) + \
                        mono_loss(y_pred[len(idx_train):-len(idx_train)], y_pred[-len(idx_train):])
        else:
            adapt_loss = 0
        # import pdb;pdb.set_trace()
        total_loss = loss + args.lam * adapt_loss

        total_loss.backward()
        optimizer.step()

        model.eval()

        y_pred = model(X_val)
        if args.noise_free:
            val_loss = rmse(y_pred, y_val)
        else:
            val_loss = rmse(y_pred[:len(idx_val)],y1_train_noise) + \
                    rmse(y_pred[len(idx_val):-len(idx_val)],y2_train_noise1) + \
                    rmse(y_pred[len(idx_val):-len(idx_val)],y2_train_noise2) + \
                    rmse(y_pred[-len(idx_val):],y3_train_noise)
        val_loss = val_loss.item()
        # import pdb;pdb.set_trace()
        if val_loss < best_val:
            best_val = val_loss
            y_pred = model(X_test)
            result = rmse(y_pred, y_test).item()
        if args.debug:
            print("Epoch ", epoch, total_loss.item(), val_loss, result)

    print(result)