[304dd3]: / datasets / augmentations.py

Download this file

111 lines (75 with data), 3.2 kB

  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
import scipy
import numpy as np
from scipy.ndimage.interpolation import *
from PIL import Image
from skimage.transform import resize
import skimage
def translateit(image, offset, isseg=False):
order = 0 if isseg == True else 5
return shift(image, (int(offset[0]), int(offset[1]), int(offset[2])), order=order, mode='constant')
def scaleit(image, factor, isseg=False):
order = 0 if isseg == True else 3
height, width, depth= image.shape
zheight = int(np.round(factor * height))
zwidth = int(np.round(factor * width))
zdepth = depth
if factor < 1.0:
newimg = np.zeros_like(image)
row = (height - zheight) // 2
col = (width - zwidth) // 2
layer = (depth - zdepth) // 2
newimg[row:row+zheight, col:col+zwidth, layer:layer+zdepth] = zoom(image, (float(factor), float(factor), 1.0), order=order, mode='nearest')[0:zheight, 0:zwidth, 0:zdepth]
return newimg
elif factor > 1.0:
row = (zheight - height) // 2
col = (zwidth - width) // 2
layer = (zdepth - depth) // 2
newimg = zoom(image[row:row+zheight, col:col+zwidth, layer:layer+zdepth], (float(factor), float(factor), 1.0), order=order, mode='nearest')
extrah = (newimg.shape[0] - height) // 2
extraw = (newimg.shape[1] - width) // 2
extrad = (newimg.shape[2] - depth) // 2
newimg = newimg[extrah:extrah+height, extraw:extraw+width, extrad:extrad+depth]
return newimg
else:
return image
def resampleit(image, dims, isseg=False):
order = 0 if isseg == True else 5
image = zoom(image, np.array(dims)/np.array(image.shape, dtype=np.float32), order=order, mode='nearest')
if image.shape[-1] == 3: #rgb image
return image
else:
return image if isseg else (image-image.min())/(image.max()-image.min())
def flipit(image):
lr_thred = np.random.uniform(0,1,1)[0]
ud_thred = np.random.uniform(0,1,1)[0]
if lr_thred<=0.5:
image = np.fliplr(image)
if ud_thred>=0.5:
image = np.flipud(image)
return image
def intensifyit(image, factor):
return image*float(factor)
def rotateit(image, axes, theta, isseg=False):
order = 0 if isseg == True else 5
return rotate(image, float(theta), axes=axes, reshape=False, order=order, mode='constant')
class CustomResize(object):
def __init__(self, trg_size):
self.trg_size = trg_size
def __call__(self, img):
resized_img = self.resize_image(img, self.trg_size)
return resized_img
def resize_image(self, img_array, trg_size):
res = resize(img_array, trg_size)
# type check
if type(res) != np.ndarray:
raise "type error!"
# PIL image cannot handle 3D image, only return ndarray type, which ToTensor accepts
return res
class CustomToTensor(object):
def __init__(self):
pass
def __call__(self, pic):
if isinstance(pic, np.ndarray):
img = torch.from_numpy(pic.transpose((2, 0, 1)))
# backward compatibility
return img.float()