-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
133 lines (111 loc) · 3.87 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
import argparse
import os
# import torch
from torchvision.utils import save_image
from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
tf_toPILImage = ToPILImage()
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
def save_images(images, epoch=None, path='results'):
for i in range(len(images)):
image = images[i]
if epoch == None:
img_path = os.path.join(path, 'defensed_images')
createFolder(img_path)
img_path = os.path.join(img_path, '%d-images.jpg'%(i))
save_image(image, img_path, normalize=False)
else:
epoch = int(epoch)
img_path = os.path.join(path, '%d'%(epoch))
createFolder(img_path)
img_path = os.path.join(img_path, '%d-images.jpg'%(i+1))
save_image(image, img_path)
def ganimation_save_image(imgtensor, dir, filemame):
createFolder(dir)
save_image((imgtensor+1)/2, os.path.join(dir, filemame))
def PIL_save_images(images, dir):
'''
in ganimation
'''
for i in range(len(images)):
path=os.path.join(dir, '%d-images.jpg'%(i+1))
images[i].save(path)
print(path)
def str2bool(v):
return v.lower() in ('true')
'''def get_celebA_sample_image2(data_loader, num, device):
image_list = list()
for i, (x_real, c_org) in enumerate(data_loader):
if i < num:
if i==0: print(x_real.shape)
# Prepare input images and target domain labels.
x_real = x_real.to(device)
image_list.append(x_real)
return image_list
def get_celebA_sample_image(data_loader, num, device):
image_list = list()
for i, (x_real, c_org) in enumerate(data_loader):
if i < num:
if i==0: print(x_real.shape)
# Prepare input images and target domain labels.
x_real = x_real.to(device)
image_list.append(x_real)
return image_list
'''
def get_concat_h(imglist):
'''
tensor img list -> 1 Pillow img
'''
dst = Image.new('RGB', (imglist[0].width *len(imglist), imglist[0].height))
for i in range(len(imglist)):
dst.paste(imglist[i], (i*imglist[0].width, 0))
return dst
def ganimation_get_concat_h(img_t_list):
# print((img_t_list[0].size()))
# print((((img_t_list[0]+1)/2).size()))
img = tf_toPILImage((img_t_list[0][0]+1)/2)
dst = Image.new('RGB', (img.width *len(img_t_list), img.height))
for i in range(len(img_t_list)):
for j in range(len(img_t_list[0])):
img=tf_toPILImage((img_t_list[i][j]+1)/2)
dst.paste(img, (i*img.width, 0))
return dst
def print_config(name: str, config):
print('--------------------',name,' configration--------------------')
print(config)
print('\n\n\n')
def save_config_dict(config_dict: dict, file_path: str):
list_keys = list(config_dict.keys())
with open(file_path, "w") as f:
for key in list_keys:
temp_str = key + ": " + str(config_dict[key]) + '\n'
f.write(temp_str)
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def to_numpy(data):
"""Convert other data type to numpy. If the data itself
is numpy type, then a copy will be made and returned.
Returns
-------
numpy.array
Numpy array of passed data.
"""
if 'mxnet' in str(type(data)):
data = data.asnumpy()
elif 'torch' in str(type(data)):
data = data.cpu().numpy()
elif 'numpy' in str(type(data)):
data = np.copy(data)
return data