MCPcopy Create free account
hub / github.com/JunlinHan/DCLGAN / SingleImageDataset

Class SingleImageDataset

data/singleimage_dataset.py:10–108  ·  view source on GitHub ↗

This dataset class can load unaligned/unpaired datasets. It requires two directories to host training images from domain A '/path/to/data/trainA' and from domain B '/path/to/data/trainB' respectively. You can train the model with the dataset flag '--dataroot /path/to/data'. Sim

Source from the content-addressed store, hash-verified

8
9
10class SingleImageDataset(BaseDataset):
11 """
12 This dataset class can load unaligned/unpaired datasets.
13
14 It requires two directories to host training images from domain A '/path/to/data/trainA'
15 and from domain B '/path/to/data/trainB' respectively.
16 You can train the model with the dataset flag '--dataroot /path/to/data'.
17 Similarly, you need to prepare two directories:
18 '/path/to/data/testA' and '/path/to/data/testB' during test time.
19 """
20
21 def __init__(self, opt):
22 """Initialize this dataset class.
23
24 Parameters:
25 opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
26 """
27 BaseDataset.__init__(self, opt)
28
29 self.dir_A = os.path.join(opt.dataroot, 'trainA') # create a path '/path/to/data/trainA'
30 self.dir_B = os.path.join(opt.dataroot, 'trainB') # create a path '/path/to/data/trainB'
31
32 if os.path.exists(self.dir_A) and os.path.exists(self.dir_B):
33 self.A_paths = sorted(make_dataset(self.dir_A, opt.max_dataset_size)) # load images from '/path/to/data/trainA'
34 self.B_paths = sorted(make_dataset(self.dir_B, opt.max_dataset_size)) # load images from '/path/to/data/trainB'
35 self.A_size = len(self.A_paths) # get the size of dataset A
36 self.B_size = len(self.B_paths) # get the size of dataset B
37
38 assert len(self.A_paths) == 1 and len(self.B_paths) == 1,\
39 "SingleImageDataset class should be used with one image in each domain"
40 A_img = Image.open(self.A_paths[0]).convert('RGB')
41 B_img = Image.open(self.B_paths[0]).convert('RGB')
42 print("Image sizes %s and %s" % (str(A_img.size), str(B_img.size)))
43
44 self.A_img = A_img
45 self.B_img = B_img
46
47 # In single-image translation, we augment the data loader by applying
48 # random scaling. Still, we design the data loader such that the
49 # amount of scaling is the same within a minibatch. To do this,
50 # we precompute the random scaling values, and repeat them by |batch_size|.
51 A_zoom = 1 / self.opt.random_scale_max
52 zoom_levels_A = np.random.uniform(A_zoom, 1.0, size=(len(self) // opt.batch_size + 1, 1, 2))
53 self.zoom_levels_A = np.reshape(np.tile(zoom_levels_A, (1, opt.batch_size, 1)), [-1, 2])
54
55 B_zoom = 1 / self.opt.random_scale_max
56 zoom_levels_B = np.random.uniform(B_zoom, 1.0, size=(len(self) // opt.batch_size + 1, 1, 2))
57 self.zoom_levels_B = np.reshape(np.tile(zoom_levels_B, (1, opt.batch_size, 1)), [-1, 2])
58
59 # While the crop locations are randomized, the negative samples should
60 # not come from the same location. To do this, we precompute the
61 # crop locations with no repetition.
62 self.patch_indices_A = list(range(len(self)))
63 random.shuffle(self.patch_indices_A)
64 self.patch_indices_B = list(range(len(self)))
65 random.shuffle(self.patch_indices_B)
66
67 def __getitem__(self, index):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected