Spaces:
Sleeping
Sleeping
File size: 8,658 Bytes
c20a1af |
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
import cv2
import random
import numpy as np
from PIL import Image
def convert_OpenCV_to_PIL(image):
return Image.fromarray(image[..., ::-1])
def convert_PIL_to_OpenCV(image):
return np.asarray(image)[..., ::-1]
class RandomResize:
def __init__(self, min_image_size, max_image_size):
self.min_image_size = min_image_size
self.max_image_size = max_image_size
self.modes = [Image.BICUBIC, Image.NEAREST]
def __call__(self, image, mode=Image.BICUBIC):
rand_image_size = random.randint(self.min_image_size, self.max_image_size)
w, h = image.size
if w < h:
scale = rand_image_size / h
else:
scale = rand_image_size / w
size = (int(round(w*scale)), int(round(h*scale)))
if size[0] == w and size[1] == h:
return image
return image.resize(size, mode)
class RandomResize_For_Segmentation:
def __init__(self, min_image_size, max_image_size):
self.min_image_size = min_image_size
self.max_image_size = max_image_size
self.modes = [Image.BICUBIC, Image.NEAREST]
def __call__(self, data):
image, mask = data['image'], data['mask']
rand_image_size = random.randint(self.min_image_size, self.max_image_size)
w, h = image.size
if w < h:
scale = rand_image_size / h
else:
scale = rand_image_size / w
size = (int(round(w*scale)), int(round(h*scale)))
if size[0] == w and size[1] == h:
pass
else:
data['image'] = image.resize(size, Image.BICUBIC)
data['mask'] = mask.resize(size, Image.NEAREST)
return data
class RandomHorizontalFlip:
def __init__(self):
pass
def __call__(self, image):
if bool(random.getrandbits(1)):
return image.transpose(Image.FLIP_LEFT_RIGHT)
return image
class RandomHorizontalFlip_For_Segmentation:
def __init__(self):
pass
def __call__(self, data):
image, mask = data['image'], data['mask']
if bool(random.getrandbits(1)):
data['image'] = image.transpose(Image.FLIP_LEFT_RIGHT)
data['mask'] = mask.transpose(Image.FLIP_LEFT_RIGHT)
return data
class Normalize:
def __init__(self, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
self.mean = mean
self.std = std
def __call__(self, image):
image = np.asarray(image)
norm_image = np.empty_like(image, np.float32)
norm_image[..., 0] = (image[..., 0] / 255. - self.mean[0]) / self.std[0]
norm_image[..., 1] = (image[..., 1] / 255. - self.mean[1]) / self.std[1]
norm_image[..., 2] = (image[..., 2] / 255. - self.mean[2]) / self.std[2]
return norm_image
class Normalize_For_Segmentation:
def __init__(self, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
self.mean = mean
self.std = std
def __call__(self, data):
image, mask = data['image'], data['mask']
image = np.asarray(image, dtype=np.float32)
mask = np.asarray(mask, dtype=np.int64)
norm_image = np.empty_like(image, np.float32)
norm_image[..., 0] = (image[..., 0] / 255. - self.mean[0]) / self.std[0]
norm_image[..., 1] = (image[..., 1] / 255. - self.mean[1]) / self.std[1]
norm_image[..., 2] = (image[..., 2] / 255. - self.mean[2]) / self.std[2]
data['image'] = norm_image
data['mask'] = mask
return data
class Top_Left_Crop:
def __init__(self, crop_size, channels=3):
self.bg_value = 0
self.crop_size = crop_size
self.crop_shape = (self.crop_size, self.crop_size, channels)
def __call__(self, image):
h, w, c = image.shape
ch = min(self.crop_size, h)
cw = min(self.crop_size, w)
cropped_image = np.ones(self.crop_shape, image.dtype) * self.bg_value
cropped_image[:ch, :cw] = image[:ch, :cw]
return cropped_image
class Top_Left_Crop_For_Segmentation:
def __init__(self, crop_size, channels=3):
self.bg_value = 0
self.crop_size = crop_size
self.crop_shape = (self.crop_size, self.crop_size, channels)
self.crop_shape_for_mask = (self.crop_size, self.crop_size)
def __call__(self, data):
image, mask = data['image'], data['mask']
h, w, c = image.shape
ch = min(self.crop_size, h)
cw = min(self.crop_size, w)
cropped_image = np.ones(self.crop_shape, image.dtype) * self.bg_value
cropped_image[:ch, :cw] = image[:ch, :cw]
cropped_mask = np.ones(self.crop_shape_for_mask, mask.dtype) * 255
cropped_mask[:ch, :cw] = mask[:ch, :cw]
data['image'] = cropped_image
data['mask'] = cropped_mask
return data
class RandomCrop:
def __init__(self, crop_size, channels=3, with_bbox=False):
self.bg_value = 0
self.with_bbox = with_bbox
self.crop_size = crop_size
self.crop_shape = (self.crop_size, self.crop_size, channels)
def get_random_crop_box(self, image):
h, w, c = image.shape
ch = min(self.crop_size, h)
cw = min(self.crop_size, w)
w_space = w - self.crop_size
h_space = h - self.crop_size
if w_space > 0:
cont_left = 0
img_left = random.randrange(w_space + 1)
else:
cont_left = random.randrange(-w_space + 1)
img_left = 0
if h_space > 0:
cont_top = 0
img_top = random.randrange(h_space + 1)
else:
cont_top = random.randrange(-h_space + 1)
img_top = 0
dst_bbox = {
'xmin' : cont_left, 'ymin' : cont_top,
'xmax' : cont_left+cw, 'ymax' : cont_top+ch
}
src_bbox = {
'xmin' : img_left, 'ymin' : img_top,
'xmax' : img_left+cw, 'ymax' : img_top+ch
}
return dst_bbox, src_bbox
def __call__(self, image, bbox_dic=None):
if bbox_dic is None:
dst_bbox, src_bbox = self.get_random_crop_box(image)
else:
dst_bbox, src_bbox = bbox_dic['dst_bbox'], bbox_dic['src_bbox']
cropped_image = np.ones(self.crop_shape, image.dtype) * self.bg_value
cropped_image[dst_bbox['ymin']:dst_bbox['ymax'], dst_bbox['xmin']:dst_bbox['xmax']] = \
image[src_bbox['ymin']:src_bbox['ymax'], src_bbox['xmin']:src_bbox['xmax']]
if self.with_bbox:
return cropped_image, {'dst_bbox':dst_bbox, 'src_bbox':src_bbox}
else:
return cropped_image
class RandomCrop_For_Segmentation(RandomCrop):
def __init__(self, crop_size):
super().__init__(crop_size)
self.crop_shape_for_mask = (self.crop_size, self.crop_size)
def __call__(self, data):
image, mask = data['image'], data['mask']
dst_bbox, src_bbox = self.get_random_crop_box(image)
cropped_image = np.ones(self.crop_shape, image.dtype) * self.bg_value
cropped_image[dst_bbox['ymin']:dst_bbox['ymax'], dst_bbox['xmin']:dst_bbox['xmax']] = \
image[src_bbox['ymin']:src_bbox['ymax'], src_bbox['xmin']:src_bbox['xmax']]
cropped_mask = np.ones(self.crop_shape_for_mask, mask.dtype) * 255
cropped_mask[dst_bbox['ymin']:dst_bbox['ymax'], dst_bbox['xmin']:dst_bbox['xmax']] = \
mask[src_bbox['ymin']:src_bbox['ymax'], src_bbox['xmin']:src_bbox['xmax']]
data['image'] = cropped_image
data['mask'] = cropped_mask
return data
class Transpose:
def __init__(self):
pass
def __call__(self, image):
return image.transpose((2, 0, 1))
class Transpose_For_Segmentation:
def __init__(self):
pass
def __call__(self, data):
# h, w, c -> c, h, w
data['image'] = data['image'].transpose((2, 0, 1))
return data
class Resize_For_Mask:
def __init__(self, size):
self.size = (size, size)
def __call__(self, data):
mask = Image.fromarray(data['mask'].astype(np.uint8))
mask = mask.resize(self.size, Image.NEAREST)
data['mask'] = np.asarray(mask, dtype=np.uint64)
return data
|