Spaces:
Running
on
Zero
Running
on
Zero
File size: 6,360 Bytes
05fb4ab |
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 |
import numbers
import cv2
import numpy as np
from einops import rearrange
from scipy import ndimage
class Scale(object):
def __init__(self, size, order=2):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
self.order = order
def __call__(self, inputs, target, mask=None):
h, w, _ = inputs[0].shape
if (h,w) == self.size:
if mask is not None:
return inputs, target, mask
else:
return inputs, target
ratio_h = float(self.size[0])/float(h)
ratio_w = float(self.size[1])/float(w)
inputs[0] = ndimage.interpolation.zoom(inputs[0], (ratio_h,ratio_w,1), order=self.order)
inputs[1] = ndimage.interpolation.zoom(inputs[1], (ratio_h,ratio_w,1), order=self.order)
target = ndimage.interpolation.zoom(target, (ratio_h,ratio_w,1), order=self.order)
target[:, :, 0] *= ratio_w
target[:, :, 1] *= ratio_h
if mask is not None:
mask = ndimage.interpolation.zoom(mask, (ratio_h, ratio_w), order=self.order)
return inputs, target, mask
else:
return inputs, target
def resize_flow(flow, size):
flow = flow.clone()
h_o, w_o = flow.size()[-2:]
h, w = size[-2:]
ratio_h = float(h)/float(h_o)
ratio_w = float(w)/float(w_o)
flow[:, 0, :, :] *= ratio_w
flow[:, 1, :, :] *= ratio_h
flow = F.interpolate(flow, size=(h, w), mode='bilinear', align_corners=True)
return flow
def tile_image(img, tile_shape=(128, 128)):
"""
Arguments:
img: tensor shape of (3, 512, 512)
tile_shape: tuple
"""
_, H, W = img.shape
tile = rearrange(img, 'C (T1 H) (T2 W) -> (T1 T2) C H W', H=tile_shape[0], W=tile_shape[1])
return tile
def tile_to_image(tile):
T = int((tile.shape[0]) ** 0.5)
return rearrange(tile, '(T1 T2) C H W -> C (T1 H) (T2 W)', T1=T, T2=T)
def resize_keeping_aspect_ratio(image, size=512):
_, C, H, W = image.shape
if H > W:
ratio = size / H
H_ = size
W_ = int(W * ratio)
else:
ratio = size / W
H_ = int(H * ratio)
W_ = size
image = F.interpolate(image.float(), size=(H_, W_), mode='bilinear', align_corners=True)
image = F.pad(image, pad=(0, size - W_, 0, size - H_), mode='constant')
return image, ratio
def resize_keeping_aspect_ratio(image, size):
h, w, _ = image.shape
if h > w:
ratio = float(size) / float(h)
else:
ratio = float(size) / float(w)
new_h = int(h*ratio)
new_w = int(w*ratio)
return cv2.resize(image, (new_w, new_h)), ratio
def pad_to_same_shape(im1, im2):
# pad to same shape
if im1.shape[0] <= im2.shape[0]:
pad_y_1 = im2.shape[0] - im1.shape[0]
pad_y_2 = 0
else:
pad_y_1 = 0
pad_y_2 = im1.shape[0] - im2.shape[0]
if im1.shape[1] <= im2.shape[1]:
pad_x_1 = im2.shape[1] - im1.shape[1]
pad_x_2 = 0
else:
pad_x_1 = 0
pad_x_2 = im1.shape[1] - im2.shape[1]
im1 = cv2.copyMakeBorder(im1, 0, pad_y_1, 0, pad_x_1, cv2.BORDER_CONSTANT)
im2 = cv2.copyMakeBorder(im2, 0, pad_y_2, 0, pad_x_2, cv2.BORDER_CONSTANT)
shape = im1.shape
return im1, im2
def pad_to_size(im, size):
# load_size first h then w
if not isinstance(size, tuple):
size = (size, size)
# pad to same shape
if im.shape[0] < size[0]:
pad_y_1 = size[0] - im.shape[0]
else:
pad_y_1 = 0
if im.shape[1] < size[1]:
pad_x_1 = size[1] - im.shape[1]
else:
pad_x_1 = 0
im = cv2.copyMakeBorder(im, 0, pad_y_1, 0, pad_x_1, cv2.BORDER_CONSTANT)
return im
def center_pad(im, size):
# load_size first h then w
if not isinstance(size, tuple):
size = (size, size)
# pad to same shape
if im.shape[0] < size[0]:
pad_y_1 = size[0] - im.shape[0]
else:
pad_y_1 = 0
if im.shape[1] < size[1]:
pad_x_1 = size[1] - im.shape[1]
else:
pad_x_1 = 0
im = cv2.copyMakeBorder(im, pad_y_1//2, pad_y_1-pad_y_1//2, pad_x_1//2, pad_x_1-pad_x_1//2, cv2.BORDER_CONSTANT)
return im
def center_crop(img, size):
"""
Get the center crop of the input image
Args:
img: input image [HxWxC]
size: load_size of the center crop (tuple) (width, height)
Output:
img_pad: center crop
x, y: coordinates of the crop
"""
if not isinstance(size, tuple):
size = (size, size)
#load_size is W,H
img = img.copy()
h, w = img.shape[:2]
pad_w = 0
pad_h = 0
if w < size[0]:
pad_w = np.int(np.ceil((size[0] - w) / 2))
if h < size[1]:
pad_h = np.int(np.ceil((size[1] - h) / 2))
img_pad = cv2.copyMakeBorder(img,
pad_h,
pad_h,
pad_w,
pad_w,
cv2.BORDER_CONSTANT,
value=[0, 0, 0])
h, w = img_pad.shape[:2]
x1 = w // 2 - size[0] // 2
y1 = h // 2 - size[1] // 2
img_pad = img_pad[y1:y1 + size[1], x1:x1 + size[0], :]
return img_pad, x1, y1
def crop(img, size, x1, y1):
"""
Get the center crop of the input image
Args:
img: input image [HxWxC]
size: load_size of the center crop (tuple) (width, height)
Output:
img_pad: center crop
x, y: coordinates of the crop
"""
if not isinstance(size, tuple):
size = (size, size)
#load_size is W,H
img = img.copy()
h, w = img.shape[:2]
pad_w = 0
pad_h = 0
if w < (x1 + size[0]):
pad_w = np.int(np.ceil(((size[0] + x1) - w) / 2))
if h < (y1+size[1]):
pad_h = np.int(np.ceil(((y1+size[1]) - h) / 2))
img_pad = cv2.copyMakeBorder(img,
pad_h,
pad_h,
pad_w,
pad_w,
cv2.BORDER_CONSTANT,
value=[0, 0, 0])
h, w = img_pad.shape[:2]
img_pad = img_pad[y1:y1 + size[1], x1:x1 + size[0], :]
return img_pad, x1, y1 |