def __getitem__(self, idx): img_lr, img_hr = self.dataset[idx] p = idx / (len(self.dataset) - 1) w_hr = round(self.size_min + (self.size_max - self.size_min) * p) img_hr = resize_fn(img_hr, w_hr) if self.augment: if random.random() < 0.5: img_lr = img_lr.flip(-1) img_hr = img_hr.flip(-1) if self.gt_resize is not None: img_hr = resize_fn(img_hr, self.gt_resize) hr_coord, hr_rgb = to_pixel_samples(img_hr) if self.sample_q is not None: sample_lst = np.random.choice(len(hr_coord), self.sample_q, replace=False) hr_coord = hr_coord[sample_lst] hr_rgb = hr_rgb[sample_lst] cell = torch.ones_like(hr_coord) cell[:, 0] *= 2 / img_hr.shape[-2] cell[:, 1] *= 2 / img_hr.shape[-1] return {'inp': img_lr, 'coord': hr_coord, 'cell': cell, 'gt': hr_rgb}
def __getitem__(self, idx): img_lr, img_hr = self.dataset[idx] s = img_hr.shape[-2] // img_lr.shape[-2] # assume int scale if self.inp_size is None: h_lr, w_lr = img_lr.shape[-2:] img_hr = img_hr[:, :h_lr * s, :w_lr * s] crop_lr, crop_hr = img_lr, img_hr else: w_lr = self.inp_size x0 = random.randint(0, img_lr.shape[-2] - w_lr) y0 = random.randint(0, img_lr.shape[-1] - w_lr) crop_lr = img_lr[:, x0: x0 + w_lr, y0: y0 + w_lr] w_hr = w_lr * s x1 = x0 * s y1 = y0 * s crop_hr = img_hr[:, x1: x1 + w_hr, y1: y1 + w_hr] if self.augment: hflip = random.random() < 0.5 vflip = random.random() < 0.5 dflip = random.random() < 0.5 def augment(x): if hflip: x = x.flip(-2) if vflip: x = x.flip(-1) if dflip: x = x.transpose(-2, -1) return x crop_lr = augment(crop_lr) crop_hr = augment(crop_hr) hr_coord, hr_rgb = to_pixel_samples(crop_hr.contiguous()) if self.sample_q is not None: sample_lst = np.random.choice( len(hr_coord), self.sample_q, replace=False) hr_coord = hr_coord[sample_lst] hr_rgb = hr_rgb[sample_lst] cell = torch.ones_like(hr_coord) cell[:, 0] *= 2 / crop_hr.shape[-2] cell[:, 1] *= 2 / crop_hr.shape[-1] return { 'inp': crop_lr, 'coord': hr_coord, 'cell': cell, 'gt': hr_rgb }
def __getitem__(self, idx): img = self.dataset[idx] s = random.uniform(self.scale_min, self.scale_max) if self.inp_size is None: h_lr = math.floor(img.shape[-2] / s + 1e-9) w_lr = math.floor(img.shape[-1] / s + 1e-9) img = img[:, :round(h_lr * s), :round(w_lr * s)] # assume round int img_down = resize_fn(img, (h_lr, w_lr)) crop_lr, crop_hr = img_down, img else: w_lr = self.inp_size w_hr = round(w_lr * s) x0 = random.randint(0, img.shape[-2] - w_hr) y0 = random.randint(0, img.shape[-1] - w_hr) crop_hr = img[:, x0: x0 + w_hr, y0: y0 + w_hr] crop_lr = resize_fn(crop_hr, w_lr) if self.augment: hflip = random.random() < 0.5 vflip = random.random() < 0.5 dflip = random.random() < 0.5 def augment(x): if hflip: x = x.flip(-2) if vflip: x = x.flip(-1) if dflip: x = x.transpose(-2, -1) return x crop_lr = augment(crop_lr) crop_hr = augment(crop_hr) hr_coord, hr_rgb = to_pixel_samples(crop_hr.contiguous()) if self.sample_q is not None: sample_lst = np.random.choice( len(hr_coord), self.sample_q, replace=False) hr_coord = hr_coord[sample_lst] hr_rgb = hr_rgb[sample_lst] cell = torch.ones_like(hr_coord) cell[:, 0] *= 2 / crop_hr.shape[-2] cell[:, 1] *= 2 / crop_hr.shape[-1] return { 'inp': crop_lr, 'coord': hr_coord, 'cell': cell, 'gt': hr_rgb }