MCPcopy Create free account
hub / github.com/SLDGroup/EMCAD / RandomSizedCrop

Class RandomSizedCrop

utils/joint_transforms.py:97–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95
96
97class RandomSizedCrop(object):
98 def __init__(self, size):
99 self.size = size
100
101 def __call__(self, img, mask):
102 assert img.size == mask.size
103 for attempt in range(10):
104 area = img.size[0] * img.size[1]
105 target_area = random.uniform(0.45, 1.0) * area
106 aspect_ratio = random.uniform(0.5, 2)
107
108 w = int(round(math.sqrt(target_area * aspect_ratio)))
109 h = int(round(math.sqrt(target_area / aspect_ratio)))
110
111 if random.random() < 0.5:
112 w, h = h, w
113
114 if w <= img.size[0] and h <= img.size[1]:
115 x1 = random.randint(0, img.size[0] - w)
116 y1 = random.randint(0, img.size[1] - h)
117
118 img = img.crop((x1, y1, x1 + w, y1 + h))
119 mask = mask.crop((x1, y1, x1 + w, y1 + h))
120 assert (img.size == (w, h))
121
122 return img.resize((self.size, self.size), Image.BILINEAR), mask.resize((self.size, self.size),
123 Image.NEAREST)
124
125 # Fallback
126 scale = Scale(self.size)
127 crop = CenterCrop(self.size)
128 return crop(*scale(img, mask))
129
130
131class RandomRotate(object):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected