-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
156 lines (122 loc) · 4.69 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
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
def load_image(img_path, img_size=None):
image = Image.open(img_path)
if img_size is not None:
image = image.resize((img_size, img_size)) # change image size to (3, img_size, img_size)
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), # this is from ImageNet dataset
])
image = transform(image)[:3, :, :].unsqueeze(0)
return image
def load_image2(img_path, img_height=None,img_width =None):
image = Image.open(img_path)
if img_width is not None:
image = image.resize((img_width, img_height)) # change image size to (3, img_size, img_size)
transform = transforms.Compose([
transforms.ToTensor(),
])
image = transform(image)[:3, :, :].unsqueeze(0)
return image
def im_convert(tensor):
image = tensor.to("cpu").clone().detach()
image = image.numpy().squeeze(0) # change size to (channel, height, width)
image = image.transpose(1,2,0)
image = image * np.array((0.229, 0.224, 0.225)) + np.array((0.485, 0.456, 0.406)) # change into unnormalized image
image = image.clip(0, 1) # in the previous steps, we change PIL image(0, 255) into tensor(0.0, 1.0), so convert it
return image
def im_convert2(tensor):
""" Display a tensor as an image. """
image = tensor.to("cpu").clone().detach()
image = image.numpy().squeeze(0) # change size to (channel, height, width)
image = image.transpose(1,2,0)
# change into unnormalized image
image = image.clip(0, 1) # in the previous steps, we change PIL image(0, 255) into tensor(0.0, 1.0), so convert it
return image
def get_features(image, model, layers=None):
if layers is None:
layers = {'0': 'conv1_1',
'5': 'conv2_1',
'10': 'conv3_1',
'19': 'conv4_1',
'21': 'conv4_2',
'28': 'conv5_1',
'31': 'conv5_2'
}
features = {}
x = image
for name, layer in model._modules.items():
x = layer(x)
if name in layers:
features[layers[name]] = x
return features
def rand_bbox(size, res):
W = size
H = size
cut_w = res
cut_h = res
tx = np.random.randint(0,W-cut_w)
ty = np.random.randint(0,H-cut_h)
bbx1 = tx
bby1 = ty
return bbx1, bby1
def rand_sampling(args,content_image):
bbxl=[]
bbyl=[]
bbx1, bby1 = rand_bbox(args.img_size, args.crop_size)
crop_img = content_image[:,:,bby1:bby1+args.crop_size,bbx1:bbx1+args.crop_size]
return crop_img
def rand_sampling_all(args):
bbxl=[]
bbyl=[]
out = []
for cc in range(50):
bbx1, bby1 = rand_bbox(args.img_size, args.crop_size)
bbxl.append(bbx1)
bbyl.append(bby1)
return bbxl,bbyl
class RandomScale(object):
def __init__(self, scale_range=(0.8, 1.2), min_size=None):
super(RandomScale, self).__init__()
self.scale_range = scale_range
self.min_size = min_size if min_size is not None else 0
def __call__(self, img):
if isinstance(img, torch.Tensor):
height, width = img.shape[-2:]
else:
width, height = img.size[-2:]
s = np.random.uniform(*self.scale_range)
resize_h = max(int(height * s), self.min_size)
resize_w = max(int(width * s), self.min_size)
size = (resize_h, resize_w)
return transforms.Resize(size)(img)
class RandomSizeCrop(object):
def __init__(self, min_cover):
super(RandomSizeCrop, self).__init__()
self.min_cover = min_cover
def __call__(self, img):
if self.min_cover == 1:
return img
if isinstance(img, torch.Tensor):
h, w = img.shape[-2:]
else:
w, h = img.size[-2:]
s = np.random.uniform(self.min_cover, 1)
size_h = int(h * s)
size_w = int(w * s)
return transforms.RandomCrop((size_h, size_w))(img)
class DivisibleCrop(object):
def __init__(self, d):
super(DivisibleCrop, self).__init__()
self.d = d
def __call__(self, img):
if isinstance(img, torch.Tensor):
h, w = img.shape[-2:]
else:
w, h = img.size[-2:]
h = h - h % self.d
w = w - w % self.d
return transforms.CenterCrop((h, w))(img)