-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathTransforms.py
125 lines (94 loc) · 3.79 KB
/
Transforms.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
#
# author: Sachin Mehta
# Project Description: This repository contains source code for semantically segmenting WSIs; however, it could be easily
# adapted for other domains such as natural image segmentation
# File Description: This file contains the source code for different types of augmentation and numpy to Tensor Conversion.
# ==============================================================================
import numpy as np
import torch
import random
import cv2
class Zoom(object):
"""
Resize the image to the larger size and randomly zoom into an area of the same size as of the original image.
"""
def __init__(self, w, h):
self.w = w
self.h = h
def __call__(self, img, label):
h1, w1 = img.shape[:2]
startH = random.randint(0, int(abs(self.h - h1) / 2))
startW = random.randint(0, int(abs(self.w - w1) / 2))
img = cv2.resize(img, (self.w, self.h))
label = cv2.resize(label, (self.w, self.h), interpolation=cv2.INTER_NEAREST)
img = img[startH:startH + h1, startW:startW + w1]
label = label[startH:startH + h1, startW:startW + w1]
return [img, label]
class RandomCropResize(object):
"""
Randomly crop and resize the given PIL image with a probability of 0.5
"""
def __init__(self, crop_area):
self.cw = crop_area
self.ch = crop_area
def __call__(self, img, label):
if random.random() < 0.5:
w, h = img.shape[:2]
x1 = random.randint(0, self.ch)
y1 = random.randint(0, self.cw)
img_crop = img[y1:h - y1, x1:w - x1]
label_crop = label[y1:h - y1, x1:w - x1]
img_crop = cv2.resize(img_crop, (w, h))
label_crop = cv2.resize(label_crop, (w, h), interpolation=cv2.INTER_NEAREST)
return img_crop, label_crop
else:
return [img, label]
class RandomHorizontalFlip(object):
"""Randomly flips (horizontally as well as vertically) the given PIL.Image with a probability of 0.5
"""
def __call__(self, image, label):
if random.random() < 0.5:
x1 = random.randint(0, 1)
if x1 == 0:
image = cv2.flip(image, 0) # horizontal flip
label = cv2.flip(label, 0) # horizontal flip
else:
image = cv2.flip(image, 1) # veritcal flip
label = cv2.flip(label, 1) # veritcal flip
return [image, label]
class Normalize(object):
"""Given mean: (R, G, B) and std: (R, G, B),
will normalize each channel of the torch.*Tensor, i.e.
channel = (channel - mean) / std
"""
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, image, label):
image = image.astype(np.float32)
for i in range(3):
image[:, :, i] -= self.mean[i]
for i in range(3):
image[:, :, i] /= self.std[i]
return [image, label]
class ToTensor(object):
def __init__(self, scale=1):
self.scale = scale
def __call__(self, image, label):
if self.scale != 1:
w, h = label.shape[:2]
label = cv2.resize(label, (int(w / self.scale), int(h / self.scale)), interpolation=cv2.INTER_NEAREST)
image = image.transpose((2, 0, 1))
image = image.astype(np.float32)
image_tensor = torch.from_numpy(image).div(255)
label_tensor = torch.LongTensor(np.array(label, dtype=np.int)) # torch.from_numpy(label)
return [image_tensor, label_tensor]
class Compose(object):
"""Composes several transforms together.
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, *args):
for t in self.transforms:
args = t(*args)
return args