def __getitem__(self, idx): raw = rawpy.imread(self.input_paths[idx]) raw_img = utils.pack_raw(raw) * self.ratios[idx] raw_img = np.clip(raw_img, 0, 1) H, W = raw_img.shape[0], raw_img.shape[1] rand_x = np.random.randint(0, W - 512) rand_y = np.random.randint(0, H - 512) # use random patch of size 512x512 from input img = raw_img[rand_y:rand_y + 512, rand_x:rand_x + 512, :] img = cv2.resize(img, (256, 256), cv2.INTER_LINEAR) img = img.transpose((2, 0, 1)) img = torch.from_numpy(img) # use random patch of size 512x512 from gt raw_gt = rawpy.imread(self.gt_paths[idx]) gt_img = utils.pack_raw(raw_gt) gt_img = gt_img[rand_y:rand_y + 512, rand_x:rand_x + 512, :] gt_img = cv2.resize(gt_img, (256, 256), cv2.INTER_LINEAR) gt_img = gt_img.transpose((2, 0, 1)) gt_img = torch.from_numpy(gt_img) return img, gt_img
def __getitem__(self, idx): raw = rawpy.imread(self.input_paths[idx]) raw_img = utils.pack_raw(raw) * self.ratios[idx] raw_img = np.clip(raw_img, 0, 1) # use random patch of size 512x512 from input H, W = raw_img.shape[0], raw_img.shape[1] rand_x = np.random.randint(0, W - 512) rand_y = np.random.randint(0, H - 512) img = raw_img[rand_y: rand_y + 512, rand_x: rand_x + 512, :] img = torch.from_numpy(img.copy()) img = img.to(self.device) img = img.permute(2, 0, 1) img = self.buildInput(img) preprocess_gt_path = self.gt_paths[idx].split('/')[-1].split('.')[0] gt_img = cv2.imread('dataset/gt/' + preprocess_gt_path + '.png') print(gt_img) gt_img = cv2.cvtColor(gt_img, cv2.COLOR_BGR2RGB) gt_img = gt_img/255. xx = rand_x*2 yy = rand_y*2 gt_img = gt_img[yy: yy+ 512*2, xx: xx + 512*2, :] gt_img = torch.from_numpy(gt_img.copy()) gt_img = gt_img.permute(2, 0, 1) return img, gt_img
def read_input(path, ratio = 100): inp_raw = rawpy.imread(path) inp = utils.pack_raw(inp_raw) inp = inp * ratio inp = np.clip(inp, 0, 1) inp = inp.transpose(2, 0, 1) inp_tensor = torch.Tensor(inp) inp_tensor = inp_tensor.unsqueeze(0) return inp_tensor
def __getitem__(self, idx): raw_path = self.raw_dir[idx] raw_image = np.asarray(imageio.imread(raw_path)) if self.in_nc == 4: raw_image = pack_raw(raw_image).astype(np.float32) / (4 * 255) else: raw_image = pack_raw_v2(raw_image).astype(np.float32) / (4 * 255) raw_image = torch.tensor(raw_image) img_path = self.dslr_dir[idx] dslr_image = imageio.imread(img_path) return raw_image, self.transform(dslr_image)
parse.add_argument("-in_nc", type=int, default=3) parse.add_argument("-dataset", type=str, default="AIM2020_ISP_validation_raw") config = parse.parse_args() device = "cuda" if torch.cuda.is_available() else "cpu" transform = transforms.ToTensor() model = Raw2Rgb(input_nc=config.in_nc).to(device) model.eval() model.load_state_dict(torch.load(config.weights)) model.half() raw_path = config.dataset total_time = 0 count = 0 for path in os.listdir(raw_path): raw_image = np.asarray(imageio.imread(os.path.join(raw_path, path))) if config.in_nc == 4: raw_image = pack_raw(raw_image).astype(np.float32) / (4 * 255) else: raw_image = pack_raw_v2(raw_image).astype(np.float32) / (4 * 255) raw_image = torch.tensor(raw_image) raw_image = raw_image.unsqueeze(0).to(device).half() with torch.no_grad(): start = time.time() img_hat = model(raw_image) torch.cuda.synchronize() stamp = time.time() - start print("process %s time:%f" % (path, stamp), img_hat.shape) total_time += stamp count += 1 torchvision.utils.save_image(img_hat, "raw2rgb/test/" + os.path.basename(path))
def __init__(self, t_ids, t_paths, gt_paths, patch_size=512, transforms=None): super(LearningToSeeInTheDarkDataset, self).__init__() # We only look at 00 pictures self.t_ids = np.unique(t_ids) self.t_paths = t_paths self.gt_paths = gt_paths self.gt_images = np.array([None] * 6000) #self.gt_images self.t_images = {} self.t_images['100'] = np.array([None] * len(self.t_ids)) #self.t_images['100'][:] = None self.t_images['250'] = np.array([None] * len(self.t_ids)) #self.t_images['250'][:] = None self.t_images['300'] = np.array([None] * len(self.t_ids)) #self.t_images['300'][:] = None print("Loading data into memory", file=sys.stderr, flush=True) for index, t_id in enumerate(self.t_ids): print(t_id) t_files = glob.glob(self.t_paths + '%05d_00*.ARW' % t_id) gt_files = glob.glob(self.gt_paths + '%05d_00*.ARW' % t_id) gt_path = gt_files[0] _, gt_fn = os.path.split(gt_path) gt_exposure = float(gt_fn[9:-5]) for t_path in t_files: _, t_fn = os.path.split(t_path) t_exposure = float(t_fn[9:-5]) ratio = min(gt_exposure / t_exposure, 300) ratio_key = str(ratio)[0:3] t_raw = rawpy.imread(t_path) self.t_images[ratio_key][index] = np.expand_dims( pack_raw(t_raw), axis=0) * ratio gt_raw = rawpy.imread(gt_path) im = gt_raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16) self.gt_images[index] = np.expand_dims(np.float32(im / 65535.0), axis=0) if index % 10 == 0: print("Loaded %d ids " % index, file=sys.stderr, flush=True) print("Completed loading data into memory", file=sys.stderr, flush=True) self.patch_size = patch_size self.transforms = transforms
in_files = glob.glob(input_dir + '%05d_00*.ARW'%test_id) for k in range(len(in_files)): in_path = in_files[k] _, in_fn = os.path.split(in_path) print(in_fn) gt_files = glob.glob(gt_dir + '%05d_00*.ARW'%test_id) gt_path = gt_files[0] _, gt_fn = os.path.split(gt_path) in_exposure = float(in_fn[9:-5]) gt_exposure = float(gt_fn[9:-5]) ratio = min(gt_exposure/in_exposure,300) raw = rawpy.imread(in_path) im = raw.raw_image_visible.astype(np.float32) im = im[:1024, :1024] input_full = np.expand_dims(pack_raw(im),axis=0) *ratio im = raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16) im = im[:1024, :1024] scale_full = np.expand_dims(np.float32(im/65535.0),axis = 0) gt_raw = rawpy.imread(gt_path) im = gt_raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16) im = im[:1024, :1024] gt_full = np.expand_dims(np.float32(im/65535.0),axis = 0) input_full = np.minimum(input_full,1.0) in_img = torch.from_numpy(input_full).permute(0,3,1,2).to(device) out_img = model(in_img) output = out_img.permute(0, 2, 3, 1).cpu().data.numpy()
def load_input_output(idx, nb, ps=512): ## file path for training id. train_id = train_ids[idx] in_files = glob.glob(input_dir + '%05d_00*.ARW' % train_id) in_path = in_files[np.random.random_integers(0, len(in_files) - 1)] in_fn = os.path.basename(in_path) ## file paths for the burst and exposure ratio. in_paths, complete = utils.get_burst_paths(in_path, max_burst) gt_files = glob.glob(gt_dir + '%05d_00*.ARW' % train_id) gt_path = gt_files[0] gt_fn = os.path.basename(gt_path) in_exposure = float(in_fn[9:-5]) gt_exposure = float(gt_fn[9:-5]) ratio = min(gt_exposure / in_exposure, 300) ## read burst images if input_images[0][str(ratio)[0:3]][idx] is None: for k in range(len(in_paths)): in_path = in_paths[k] if os.path.isfile(in_path): raw = rawpy.imread(in_path) raw = utils.pack_raw(raw)*ratio if train_coarse == True: raw = utils.resize(raw, r=2) raw = np.expand_dims(raw, 0) raw = np.minimum(raw, 1.0) input_images[k][str(ratio)[0:3]][idx] = raw ## read gt. gt_raw = rawpy.imread(gt_path) im = gt_raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16) im = np.float32(im/65535.0) raw_im = utils.pack_raw(gt_raw) if train_coarse == True: im = utils.resize(im, r=2) raw_im = utils.resize(raw_im, r=2) im = np.expand_dims(im, axis=0) raw_im = np.expand_dims(raw_im, 0) im = np.minimum(im, 1.0) raw_im = np.minimum(raw_im, 1.0) gt_images[idx] = im gt_image_raws[idx] = raw_im ## get inputs and output gt_full = gt_images[idx] gt_full_raw = gt_image_raws[idx] input_patches = [] for k in range(len(in_paths)): input_full = input_images[k][str(ratio)[0:3]][idx] input_patches.append(input_full) ## preprocessing st = time.time() input_patches, gt_patch, gt_patch_raw = utils.crop_samples(input_patches, gt_full, gt_full_raw, ps=ps) input_patches, gt_patch, gt_patch_raw = utils.augment_samples(input_patches, gt_patch, gt_patch_raw) input_patches = utils.shuffle_samples(input_patches) input_patches_low = utils.resize_samples(input_patches) if train_coarse == False: gt_patch_raw = np.expand_dims(utils.resize(gt_patch_raw[0, :, :, :], r=2), 0) return input_patches[:nb, :,:,:], input_patches_low[:nb, :,:,:], gt_patch, gt_patch_raw, complete
in_path = in_files[k] in_paths, complete = utils.get_burst_paths(in_path, n_burst=n_burst) in_fn = os.path.basename(in_path) gt_files = glob.glob(gt_dir + '%05d_00*.ARW' % test_id) gt_path = gt_files[0] gt_fn = os.path.basename(gt_path) in_exposure = float(in_fn[9:-5]) gt_exposure = float(gt_fn[9:-5]) ratio = min(gt_exposure / in_exposure, 300) print(in_fn) ## Pack and multiply with exp. ratio. inputs = [] for in_path in in_paths: raw = rawpy.imread(in_path) input_full = utils.pack_raw(raw) * ratio input_full = np.expand_dims(input_full, 0) input_full = np.minimum(input_full, 1.0) inputs.append(input_full) inputs = np.array(inputs) inputs_low = utils.resize_samples(inputs) ## Read gt. gt_raw = rawpy.imread(gt_path) gt_full = gt_raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16) gt_full = np.expand_dims(np.float32(gt_full / 65535.0), axis=0) gt_full_raw = np.expand_dims(utils.pack_raw(gt_raw), 0)
def __getitem__(self, ind): # Get the path from image id train_id = self.train_ids[ind] train_id = 2 # train_id = 00001_00_10s.ARW in_files = glob.glob(input_dir + '%05d_00*.ARW' % train_id) # print(in_files) in_path = in_files[np.random.random_integers(0, len(in_files) - 1)] _, in_fn = os.path.split(in_path) gt_files = glob.glob(gt_dir + '%05d_00*.ARW' % train_id) gt_path = gt_files[0] # print(in_path, gt_path) _, gt_fn = os.path.split(gt_path) in_exposure = float(in_fn[9:-5]) gt_exposure = float(gt_fn[9:-5]) ratio = min(gt_exposure / in_exposure, 300) # Read raw image if self.input_images[str(ratio)[0:3]][ind] is None: raw = rawpy.imread(in_path) self.input_images[str(ratio)[0:3]][ind] = np.expand_dims( pack_raw(raw), axis=0) * ratio gt_raw = rawpy.imread(gt_path) im = gt_raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16) self.gt_images[ind] = np.expand_dims(np.float32(im / 65535.0), axis=0) # Crop H = self.input_images[str(ratio)[0:3]][ind].shape[1] W = self.input_images[str(ratio)[0:3]][ind].shape[2] xx = W // 4 yy = H // 2 input_patch = self.input_images[str(ratio)[0:3]][ind][:, yy:yy + ps, xx:xx + ps, :] gt_patch = self.gt_images[ind][:, yy * 2:yy * 2 + ps * 2, xx * 2:xx * 2 + ps * 2, :] # Random flip or transpose if np.random.randint(2, size=1)[0] == 1: input_patch = np.flip(input_patch, axis=1) gt_patch = np.flip(gt_patch, axis=1) if np.random.randint(2, size=1)[0] == 1: input_patch = np.flip(input_patch, axis=0) gt_patch = np.flip(gt_patch, axis=0) if np.random.randint(2, size=1)[0] == 1: input_patch = np.transpose(input_patch, (0, 2, 1, 3)) gt_patch = np.transpose(gt_patch, (0, 2, 1, 3)) # Clip the images input_patch = np.minimum(input_patch, 1.0) gt_patch = np.maximum(gt_patch, 0.0) # Place onto device and torch it in_img = torch.from_numpy(input_patch).permute(0, 3, 1, 2) gt_img = torch.from_numpy(gt_patch).permute(0, 3, 1, 2) return in_img, gt_img, train_id, ratio