-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
127 lines (96 loc) · 3.74 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
import torch
import torch.optim as optim
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
from torch.utils import data
import os
import functools
import numpy as np
import matplotlib.pyplot as plt
class NpzDataset(data.Dataset):
def __init__(self, root):
self.root_folder = root
self.image_paths = os.listdir(root)
def __len__(self):
return len(self.image_paths)
def __getitem__(self, index):
# Select sample
image_path = self.image_paths[index]
# Load data and get label
image = np.load(self.root_folder + '/' + image_path)['data']
return np.array([image]).astype(np.float32)
def create_dataset_npz(root):
return NpzDataset(root)
# Deprecated for now
def create_dataset(root):
return dset.ImageFolder(root=root,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]))
def mkdir(paths):
if not isinstance(paths, (list, tuple)):
paths = list(paths)
for path in paths:
if not os.path.isdir(path):
os.makedirs(path)
# deprecated for now
def create_dir_map(root_dir, target_dirs):
"""
Create a directory map for ImageFolder
"""
dir_map = {}
for target_dir in target_dirs:
dir_map[target_dir] = os.path.join(root_dir, 'link_' + target_dir)
mkdir(dir_map.values())
for key in dir_map:
try:
os.remove(os.path.join(dir_map[key], '0'))
except:
pass
os.symlink(os.path.abspath(os.path.join(root_dir, key)),
os.path.join(dir_map[key], '0'))
return dir_map
def gen_dir_map(dataroot, sub_dirs):
dir_map = {}
for sub_dir in sub_dirs:
dir_map[sub_dir] = dataroot + '/' + sub_dir
return dir_map
def create_gan_datasets(dataroot, sub_dirs):
"""
Create 4 datasets for cycle GAN model
return (trainA, trainB, testA, testB)
"""
dir_map = gen_dir_map(dataroot, sub_dirs)
trainA = create_dataset_npz(dir_map[sub_dirs[0]])
trainB = create_dataset_npz(dir_map[sub_dirs[1]])
testA = create_dataset_npz(dir_map[sub_dirs[2]])
testB = create_dataset_npz(dir_map[sub_dirs[3]])
return (trainA, trainB, testA, testB)
def create_gan_dataloaders(trainA, trainB, testA, testB, batch_sizes, workers):
trainA_loader = torch.utils.data.DataLoader(trainA, batch_size=batch_sizes[0], shuffle=True, num_workers=workers)
trainB_loader = torch.utils.data.DataLoader(trainB, batch_size=batch_sizes[1], shuffle=True, num_workers=workers)
testA_loader = torch.utils.data.DataLoader(testA, batch_size=batch_sizes[2], shuffle=True, num_workers=workers)
testB_loader = torch.utils.data.DataLoader(testB, batch_size=batch_sizes[3], shuffle=True, num_workers=workers)
return (trainA_loader, trainB_loader, testA_loader, testB_loader)
def display_batch(loader, device, title="Images", num_row=8, num_col=8):
sample_batch = next(iter(loader))
plt.figure(figsize=(num_row, num_col))
plt.axis("off")
plt.title(title)
plt.imshow(np.transpose(vutils.make_grid(
sample_batch[0].to(device)[:num_row * num_col], padding=2, normalize=True).cpu(),(1,2,0)))
plt.show()
# CUDA helpers
def cuda_devices(gpu_ids):
gpu_ids = [str(i) for i in gpu_ids]
os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(gpu_ids)
def cuda(xs):
if torch.cuda.is_available():
if not isinstance(xs, (list, tuple)):
return xs.cuda()
else:
return [x.cuda() for x in xs]
return xs