-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
240 lines (196 loc) · 10.9 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import torch
from torchvision import transforms
from torch.utils.data import Dataset
from PIL import Image
import os
from zipfile import ZipFile
import requests
from torchvision.utils import save_image
import gdown
class Horse2zebraDataset(Dataset):
"""Horse2zebra dataset"""
base_folder = dataset_name = "horse2zebra"
def __init__(self, root, transform, train):
unzip_dataset(self.dataset_name, self.base_folder, root)
if train:
self.dataset_pathA = os.path.join(root, self.base_folder, "trainA")
self.dataset_pathB = os.path.join(root, self.base_folder, "trainB")
self.image_pathsA = [os.path.join(self.dataset_pathA, file) for file in os.listdir(self.dataset_pathA)]
self.image_pathsB = [os.path.join(self.dataset_pathB, file) for file in os.listdir(self.dataset_pathB)]
else:
self.dataset_pathA = os.path.join(root, self.base_folder, "testA")
self.dataset_pathB = os.path.join(root, self.base_folder, "testB")
self.image_pathsA = [os.path.join(self.dataset_pathA, file) for file in self.sort_files(os.listdir(self.dataset_pathA))]
self.image_pathsB = [os.path.join(self.dataset_pathB, file) for file in self.sort_files(os.listdir(self.dataset_pathB))]
self.transform = transform
def __getitem__(self, index):
if index < len(self.image_pathsA): imageA = self.read_image(self.image_pathsA[index])
else: imageA = self.read_image(self.image_pathsA[torch.randint(len(self.image_pathsA), (1, ))])
if index < len(self.image_pathsB): imageB = self.read_image(self.image_pathsB[index])
else: imageB = self.read_image(self.image_pathsB[torch.randint(len(self.image_pathsB), (1, ))])
return self.transform(imageA), self.transform(imageB)
def __len__(self):
return max(len(self.image_pathsA), len(self.image_pathsB))
def read_image(self, img_path):
return Image.open(img_path)
def sort_files(self, files):
"""Sorts based on file indices for a given list of files"""
f = lambda x: int(os.path.splitext(x)[0].split("_")[-1])
return sorted(files, key=f)
class Monet2photoDataset(Dataset):
"""Monet2photo dataset"""
base_folder = dataset_name = "monet2photo"
def __init__(self, root, transform, train, download):
if download:
download_dataset(self.dataset_name, root)
self.unzip_dataset(self.base_folder, root)
if train:
self.dataset_pathA = os.path.join(root, self.base_folder, "trainA")
self.dataset_pathB = os.path.join(root, self.base_folder, "trainB")
self.image_pathsA = [os.path.join(self.dataset_pathA, file) for file in os.listdir(self.dataset_pathA)]
self.image_pathsB = [os.path.join(self.dataset_pathB, file) for file in os.listdir(self.dataset_pathB)]
else:
self.dataset_pathA = os.path.join(root, self.base_folder, "testA")
self.dataset_pathB = os.path.join(root, self.base_folder, "testB")
self.image_pathsA = [os.path.join(self.dataset_pathA, file) for file in self.sort_files(os.listdir(self.dataset_pathA))]
self.image_pathsB = [os.path.join(self.dataset_pathB, file) for file in self.sort_files(os.listdir(self.dataset_pathB))]
self.transform = transform
def __getitem__(self, index):
if index < len(self.image_pathsA): imageA = self.read_image(self.image_pathsA[index])
else: imageA = self.read_image(self.image_pathsA[torch.randint(len(self.image_pathsA), (1, ))])
if index < len(self.image_pathsB): imageB = self.read_image(self.image_pathsB[index])
else: imageB = self.read_image(self.image_pathsB[torch.randint(len(self.image_pathsB), (1, ))])
return self.transform(imageA), self.transform(imageB)
def __len__(self):
return max(len(self.image_pathsA), len(self.image_pathsB))
def read_image(self, img_path):
return Image.open(img_path)
def sort_files(self, files):
"""Sorts based on file indices for a given list of files"""
return sorted(files)
def unzip_dataset(self, base_folder, root):
"""Unzip monet2photo.zip dataset"""
if os.path.exists(os.path.join(root, base_folder)):
print(f"Directory {os.path.join(root, base_folder)} already exists. No operation done")
return
file_names = ["monet2photo.zip"]
extract_to = os.path.join(root, "")
for file_name in file_names:
with ZipFile(os.path.join(root, file_name), "r") as zip_file:
zip_file.extractall(extract_to)
class Latex2handwrittenDataset(Dataset):
"""Latex2handwritten dataset"""
base_folder = dataset_name = "latex2handwritten"
def __init__(self, root, transform, train):
self.download_dataset(__class__.dataset_name, root)
unzip_dataset(__class__.dataset_name, __class__.base_folder, root)
if train:
self.dataset_pathA = os.path.join(root, self.base_folder, "trainA")
self.dataset_pathB = os.path.join(root, self.base_folder, "trainB")
self.image_pathsA = [os.path.join(self.dataset_pathA, file) for file in os.listdir(self.dataset_pathA)]
self.image_pathsB = [os.path.join(self.dataset_pathB, file) for file in os.listdir(self.dataset_pathB)]
else:
self.dataset_pathA = os.path.join(root, self.base_folder, "testA")
self.dataset_pathB = os.path.join(root, self.base_folder, "testB")
self.image_pathsA = [os.path.join(self.dataset_pathA, file) for file in self.sort_files(os.listdir(self.dataset_pathA))]
self.image_pathsB = [os.path.join(self.dataset_pathB, file) for file in self.sort_files(os.listdir(self.dataset_pathB))]
self.transform = transform
def __getitem__(self, index):
if index < len(self.image_pathsA): imageA = self.read_image(self.image_pathsA[index])
else: imageA = self.read_image(self.image_pathsA[torch.randint(len(self.image_pathsA), (1, ))])
if index < len(self.image_pathsB): imageB = self.read_image(self.image_pathsB[index])
else: imageB = self.read_image(self.image_pathsB[torch.randint(len(self.image_pathsB), (1, ))])
return self.transform(imageA), self.transform(imageB)
def __len__(self):
return max(len(self.image_pathsA), len(self.image_pathsB))
def read_image(self, img_path):
return Image.open(img_path)
def sort_files(self, files):
"""Sorts based on file indices for a given list of files"""
#f = lambda x: int(os.path.splitext(x)[0].split("_")[-1])
return sorted(files)
def download_dataset(self, dataset_name, root):
"""Downloads dataset for given dataset name"""
# file_name_id = {"latex2handwritten": ["latex2handwritten.zip",
# "1yx9cCjdKTednizft1piqMchsYZW4E5yp"]}
file_name_id = {"latex2handwritten": ["latex2handwritten.zip",
"1IdyDJJ8HTUGj_8ZipeKHhng3tQyQ1Dj_"]}
file_name, file_id = file_name_id[dataset_name]
if os.path.exists(os.path.join(root, file_name)):
print(f"File exists! No operation done: {os.path.join(root, file_name)}")
return
else:
url = f"https://drive.google.com/uc?id={file_id}"
gdown.download(url, output=os.path.join(root, file_name), quiet=False)
def unzip_dataset(dataset_name, base_folder, root):
"""Unzip dataset for given dataset name"""
if dataset_name == "horse2zebra":
file_names = ["horse2zebraA.zip", "horse2zebraB.zip"]
elif dataset_name == "monet2photo":
file_names = ["monet2photo.zip"]
elif dataset_name == "latex2handwritten":
file_names = ["latex2handwritten.zip"]
else:
raise ValueError(f"Undefined dataset name: {dataset_name}")
extract_to = os.path.join(root, base_folder)
print(dataset_name, extract_to)
if os.path.exists(extract_to):
print(f"Directory {extract_to} already exists. No operation done")
return
os.mkdir(extract_to)
for file_name in file_names:
with ZipFile(os.path.join(root, file_name), "r") as zip_file:
zip_file.extractall(extract_to)
def download_dataset(dataset_name, root, url=None):
"""Downloads dataset into root/ directory"""
if url is None:
url = f"https://efrosgans.eecs.berkeley.edu/cyclegan/datasets/{dataset_name}.zip"
file_path = os.path.join(root, f"{dataset_name}.zip")
if os.path.exists(file_path):
print(f"File {file_path} already exists. No operation done!")
return
print(f"Sending request to url {url}...")
response = requests.get(url)
if response.status_code == 200:
with open(file_path, "wb") as file:
file.write(response.content)
print(f"File {file_path} downloaded successfully.")
else:
print("Failed to download file. Status code:", response.status_code)
class ImageBuffer(object):
"""Keeps images in a specified-size buffer"""
def __init__(self, buffer_capacity=None):
self.buffer = []
self.buffer_capacity = buffer_capacity
def get_tensor(self, images):
"""Returns images from buffer"""
return torch.cat([self._push_and_pop(image.detach()[None]) for image in images])
def _push_and_pop(self, image):
"""Pushes (if available) into buffer and returns (if possible) given image"""
if self.buffer_capacity == 0: return image
if self.size() < self.buffer_capacity:
self.buffer.append(image)
else:
if torch.rand(1) > 0.5:
idx = torch.randint(self.buffer_capacity, (1, ))
image, self.buffer[idx] = self.buffer[idx], image
return image
def size(self):
"""Returns the size of the buffer"""
return len(self.buffer)
def state_dict(self):
"""Returns state dictionary of image-buffer class"""
return {"buffer": self.buffer, "buffer_capacity": self.buffer_capacity}
def load_state_dict(self, state_dict):
"""Loads given buffer state dictionary"""
self.buffer = state_dict["buffer"]
self.buffer_capacity = state_dict["buffer_capacity"]
def download_checkpoint(dataset_name, root, base_folder="checkpoints"):
"""Downloads pretrained checkpoint for given dataset name"""
file_name_id = {"horse2zebra": ["pretrained_horse2zebra_checkpoint_219.pth",
"10ZlokluOgzIfaeSN_CDJ277fJYAoIOXj"]}
file_name, file_id = file_name_id[dataset_name]
url = f"https://drive.google.com/uc?id={file_id}"
gdown.download(url, output=os.path.join(root, base_folder, file_name), quiet=False)
if __name__ == "__main__":
latex2handwritten = Latex2handwrittenDataset("datasets", lambda x: x, True)