MCPcopy
hub / github.com/alibaba/EasyCV / gaussianBlur

Function gaussianBlur

easycv/utils/preprocess_function.py:123–162  ·  view source on GitHub ↗
(image, kernel_size=22, apply_prob=0.5)

Source from the content-addressed store, hash-verified

121
122
123def gaussianBlur(image, kernel_size=22, apply_prob=0.5):
124 expand_batch_dim = len(image.size()) == 3
125 if expand_batch_dim:
126 image = image.unsqueeze(0)
127
128 batch_size = image.size(0)
129 result = torch.zeros_like(image, device=image.device)
130
131 for index in range(batch_size):
132 if random.random() < apply_prob:
133 sigma = random.uniform(0.1, 2.0)
134 radius = int(kernel_size / 2)
135 kernel_size = radius * 2 + 1
136 # x = torch.arange(-radius, radius + 1).cuda()
137 x = torch.arange(-radius, radius + 1, device=image.device)
138 x = x.to(image.dtype)
139 blur_filter = torch.exp(-torch.pow(x, 2.0) / (2.0 * (sigma**2)))
140 blur_filter = blur_filter.div(blur_filter.sum())
141 blur_v = torch.reshape(blur_filter, [1, 1, kernel_size, 1])
142 blur_h = torch.reshape(blur_filter, [1, 1, 1, kernel_size])
143
144 num_channels, _, _ = image.size(1), image.size(2), image.size(3)
145 blur_h = blur_h.repeat(num_channels, 1, 1, 1)
146 blur_v = blur_v.repeat(num_channels, 1, 1, 1)
147 pad_length = int((kernel_size - 1) / 2)
148 blurred = F.conv2d(
149 image[index:index + 1],
150 blur_h,
151 stride=1,
152 padding=(0, pad_length),
153 groups=3)
154 blurred = F.conv2d(
155 blurred, blur_v, stride=1, padding=(pad_length, 0), groups=3)
156 if expand_batch_dim:
157 blurred = blurred.squeeze(0)
158 result[index] = blurred
159 else:
160 result[index] = image[index]
161
162 return result
163
164
165def randomGrayScale(image, apply_prob=0.2):

Callers

nothing calls this directly

Calls 3

sizeMethod · 0.45
randomMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected