|
| 1 | +import os, sys, math, random, glob, cv2 |
| 2 | +import numpy as np |
| 3 | +### torch lib |
| 4 | +import torch |
| 5 | +import torch.utils.data as data |
| 6 | +### custom lib |
| 7 | +import utils |
| 8 | +import pdb |
| 9 | +import torchvision.transforms as transforms |
| 10 | + |
| 11 | + |
| 12 | +class RandomCrop(object): |
| 13 | + def __init__(self, image_size, crop_size): |
| 14 | + self.ch, self.cw = crop_size |
| 15 | + ih, iw = image_size |
| 16 | + self.h1 = random.randint(0, ih - self.ch) |
| 17 | + self.w1 = random.randint(0, iw - self.cw) |
| 18 | + self.h2 = self.h1 + self.ch |
| 19 | + self.w2 = self.w1 + self.cw |
| 20 | + def __call__(self, img): |
| 21 | + if len(img.shape) == 3: |
| 22 | + return img[self.h1 : self.h2, self.w1 : self.w2, :] |
| 23 | + else: |
| 24 | + return img[self.h1 : self.h2, self.w1 : self.w2] |
| 25 | + |
| 26 | +class MultiFramesDataset(data.Dataset): |
| 27 | + def __init__(self, opts, mode): |
| 28 | + super(MultiFramesDataset, self).__init__() |
| 29 | + self.transform = transforms.Compose([transforms.ToTensor()]) |
| 30 | + self.opts = opts |
| 31 | + self.mode = mode |
| 32 | + self.task_videos = [] |
| 33 | + self.num_frames = [] |
| 34 | + self.dataset_task_list = [] |
| 35 | + |
| 36 | + list_filename = os.path.join(opts.list_dir, "train_tasks_%s.txt" %(opts.datasets_tasks)) |
| 37 | + with open(list_filename) as f: |
| 38 | + for line in f.readlines(): |
| 39 | + if line[0] != "#": |
| 40 | + self.dataset_task_list.append(line.strip().split()) |
| 41 | + self.num_tasks = len(self.dataset_task_list) |
| 42 | + for dataset, task in self.dataset_task_list: |
| 43 | + list_filename = os.path.join(opts.list_dir, "%s_%s.txt" %(dataset, mode)) |
| 44 | + print("[%s] Read %s (Task %s)" %(self.__class__.__name__, list_filename, task)) |
| 45 | + with open(list_filename) as f: |
| 46 | + videos = [line.rstrip() for line in f.readlines()] |
| 47 | + for video in videos: |
| 48 | + self.task_videos.append([task, os.path.join(dataset, video)]) |
| 49 | + input_dir = os.path.join(self.opts.data_dir, self.mode, "input", dataset, video) |
| 50 | + frame_list = glob.glob(os.path.join(input_dir, '*.png')) |
| 51 | + if len(frame_list) == 0: |
| 52 | + raise Exception("No frames in %s" %input_dir) |
| 53 | + self.num_frames.append(len(frame_list)) |
| 54 | + print("[%s] Total %d videos (%d frames), %d tasks" %(self.__class__.__name__, len(self.task_videos), sum(self.num_frames), self.num_tasks)) |
| 55 | + |
| 56 | + def __len__(self): |
| 57 | + return len(self.task_videos) |
| 58 | + def __getitem__(self, index): |
| 59 | + ## random select starting frame index t between [0, N - #sample_frames] |
| 60 | + N = self.num_frames[index] |
| 61 | + T = random.randint(0, N - self.opts.sample_frames) |
| 62 | + task = self.task_videos[index][0] |
| 63 | + video = self.task_videos[index][1] |
| 64 | + ## load input and processed frames |
| 65 | + input_dir = os.path.join(self.opts.data_dir, self.mode, "input") |
| 66 | + process_dir = os.path.join(self.opts.data_dir, self.mode, "processed", task) |
| 67 | + ## sample from T to T + #sample_frames - 1 |
| 68 | + frame_i = [] |
| 69 | + frame_p = [] |
| 70 | + frame_i_tmp = [] |
| 71 | + frame_p_tmp = [] |
| 72 | + for t in range(T, T + self.opts.sample_frames): |
| 73 | + frame_i_tmp.append( utils.read_img(os.path.join(input_dir, video, "%08d.png" %t) ) ) |
| 74 | + frame_p_tmp.append( utils.read_img(os.path.join(process_dir, video, "%08d.png" %t) ) ) |
| 75 | + ## data augmentation |
| 76 | + if self.mode == 'train': |
| 77 | + for t in range(self.opts.sample_frames): |
| 78 | + frame_i_tmp[t], frame_p_tmp[t] = utils.get_patch(frame_i_tmp[t], frame_p_tmp[t], self.opts.crop_size, 4, multi_scale=False) |
| 79 | + frame_i.append(frame_i_tmp[t]) |
| 80 | + frame_p.append(frame_p_tmp[t]) |
| 81 | + if self.opts.geometry_aug: |
| 82 | + ### random rotate |
| 83 | + rotate = random.randint(0, 3) |
| 84 | + if rotate != 0: |
| 85 | + for t in range(self.opts.sample_frames): |
| 86 | + frame_i[t] = np.rot90(frame_i[t], rotate) |
| 87 | + frame_p[t] = np.rot90(frame_p[t], rotate) |
| 88 | + ## horizontal flip |
| 89 | + if np.random.random() >= 0.5: |
| 90 | + for t in range(self.opts.sample_frames): |
| 91 | + frame_i[t] = cv2.flip(frame_i[t], flipCode=0) |
| 92 | + frame_p[t] = cv2.flip(frame_p[t], flipCode=0) |
| 93 | + elif self.mode == "test": |
| 94 | + ## resize image to avoid size mismatch after downsampline and upsampling |
| 95 | + H_i = frame_i[0].shape[0] |
| 96 | + W_i = frame_i[0].shape[1] |
| 97 | + |
| 98 | + H_o = int(math.ceil(float(H_i) / self.opts.size_multiplier) * self.opts.size_multiplier) |
| 99 | + W_o = int(math.ceil(float(W_i) / self.opts.size_multiplier) * self.opts.size_multiplier) |
| 100 | + for t in range(self.opts.sample_frames): |
| 101 | + frame_i_tmp[t], frame_p_tmp[t] = utils.get_patch(frame_i_tmp[t], frame_p_tmp[t], self.opts.crop_size, 4, multi_scale=False) |
| 102 | + frame_i.append(frame_i_tmp[t]) |
| 103 | + frame_p.append(frame_p_tmp[t]) |
| 104 | + |
| 105 | + else: |
| 106 | + raise Exception("Unknown mode (%s)" %self.mode) |
| 107 | + ### convert (H, W, C) array to (C, H, W) tensor |
| 108 | + data = [] |
| 109 | + for t in range(self.opts.sample_frames): |
| 110 | + data.append(torch.from_numpy(frame_i[t].transpose(2, 0, 1).astype(np.float32)).contiguous()) |
| 111 | + data.append(torch.from_numpy(frame_p[t].transpose(2, 0, 1).astype(np.float32)).contiguous()) |
| 112 | + return data |
0 commit comments