randomize order of distortions

This commit is contained in:
Jeremy Hummel 2021-05-23 22:27:11 -07:00
commit 5f89a12e01

View file

@ -217,55 +217,57 @@ class SampleProcessor(object):
ct_sample_bgr = ct_sample.load_bgr() ct_sample_bgr = ct_sample.load_bgr()
img = imagelib.color_transfer (ct_mode, img, cv2.resize( ct_sample_bgr, (resolution,resolution), interpolation=cv2.INTER_LINEAR ) ) img = imagelib.color_transfer (ct_mode, img, cv2.resize( ct_sample_bgr, (resolution,resolution), interpolation=cv2.INTER_LINEAR ) )
# Apply random downsampling randomization_order = np.random.shuffle(['blur', 'noise', 'jpeg', 'down'])
if random_downsample: for random_distortion in randomization_order:
down_res = np.random.randint(int(0.125*resolution), int(0.25*resolution)) # Apply random blur
img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC) if random_distortion == 'blur' and random_blur:
img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC) blur_type = np.random.choice(['motion', 'gaussian'])
# Apply random noise if blur_type == 'motion':
if random_noise: blur_k = np.random.randint(10, 20)
noise_type = np.random.choice(['gaussian', 'laplace', 'poisson']) blur_angle = 360 * np.random.random()
noise_scale = (20 * np.random.random() + 20) img = LinearMotionBlur(img, blur_k, blur_angle)
elif blur_type == 'gaussian':
blur_sigma = 5 * np.random.random() + 3
if noise_type == 'gaussian': if blur_sigma < 5.0:
noise = np.random.normal(scale=noise_scale, size=img.shape) kernel_size = 2.9 * blur_sigma # 97% of weight
img += noise / 255.0 else:
elif noise_type == 'laplace': kernel_size = 2.6 * blur_sigma # 95% of weight
noise = np.random.laplace(scale=noise_scale, size=img.shape) kernel_size = int(kernel_size)
img += noise / 255.0 kernel_size = kernel_size + 1 if kernel_size % 2 == 0 else kernel_size
elif noise_type == 'poisson':
noise_lam = (15 * np.random.random() + 15)
noise = np.random.poisson(lam=noise_lam, size=img.shape)
img += noise / 255.0
# Apply random blur img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma)
if random_blur:
blur_type = np.random.choice(['motion', 'gaussian'])
if blur_type == 'motion': # Apply random noise
blur_k = np.random.randint(10, 20) if random_distortion == 'noise' and random_noise:
blur_angle = 360 * np.random.random() noise_type = np.random.choice(['gaussian', 'laplace', 'poisson'])
img = LinearMotionBlur(img, blur_k, blur_angle) noise_scale = (20 * np.random.random() + 20)
elif blur_type == 'gaussian':
blur_sigma = 5 * np.random.random() + 3
if blur_sigma < 5.0: if noise_type == 'gaussian':
kernel_size = 2.9 * blur_sigma # 97% of weight noise = np.random.normal(scale=noise_scale, size=img.shape)
else: img += noise / 255.0
kernel_size = 2.6 * blur_sigma # 95% of weight elif noise_type == 'laplace':
kernel_size = int(kernel_size) noise = np.random.laplace(scale=noise_scale, size=img.shape)
kernel_size = kernel_size + 1 if kernel_size % 2 == 0 else kernel_size img += noise / 255.0
elif noise_type == 'poisson':
noise_lam = (15 * np.random.random() + 15)
noise = np.random.poisson(lam=noise_lam, size=img.shape)
img += noise / 255.0
img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) # Apply random jpeg compression
if random_distortion == 'jpeg' and random_jpeg:
img = np.clip(img*255, 0, 255).astype(np.uint8)
jpeg_compression_level = np.random.randint(50, 85)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level]
_, enc_img = cv2.imencode('.jpg', img, encode_param)
img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0
# Apply random jpeg compression # Apply random downsampling
if random_jpeg: if random_distortion == 'down' and random_downsample:
img = np.clip(img*255, 0, 255).astype(np.uint8) down_res = np.random.randint(int(0.125*resolution), int(0.25*resolution))
jpeg_compression_level = np.random.randint(50, 85) img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level] img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC)
_, enc_img = cv2.imencode('.jpg', img, encode_param)
img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0
img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate)
img = np.clip(img.astype(np.float32), 0, 1) img = np.clip(img.astype(np.float32), 0, 1)