randomize order of distortions

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

View file

@ -217,30 +217,10 @@ 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))
img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC)
img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC)
# Apply random noise
if random_noise:
noise_type = np.random.choice(['gaussian', 'laplace', 'poisson'])
noise_scale = (20 * np.random.random() + 20)
if noise_type == 'gaussian':
noise = np.random.normal(scale=noise_scale, size=img.shape)
img += noise / 255.0
elif noise_type == 'laplace':
noise = np.random.laplace(scale=noise_scale, size=img.shape)
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
# Apply random blur # Apply random blur
if random_blur: if random_distortion == 'blur' and random_blur:
blur_type = np.random.choice(['motion', 'gaussian']) blur_type = np.random.choice(['motion', 'gaussian'])
if blur_type == 'motion': if blur_type == 'motion':
@ -259,14 +239,36 @@ class SampleProcessor(object):
img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma)
# Apply random noise
if random_distortion == 'noise' and random_noise:
noise_type = np.random.choice(['gaussian', 'laplace', 'poisson'])
noise_scale = (20 * np.random.random() + 20)
if noise_type == 'gaussian':
noise = np.random.normal(scale=noise_scale, size=img.shape)
img += noise / 255.0
elif noise_type == 'laplace':
noise = np.random.laplace(scale=noise_scale, size=img.shape)
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
# Apply random jpeg compression # Apply random jpeg compression
if random_jpeg: if random_distortion == 'jpeg' and random_jpeg:
img = np.clip(img*255, 0, 255).astype(np.uint8) img = np.clip(img*255, 0, 255).astype(np.uint8)
jpeg_compression_level = np.random.randint(50, 85) jpeg_compression_level = np.random.randint(50, 85)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level] encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level]
_, enc_img = cv2.imencode('.jpg', img, encode_param) _, enc_img = cv2.imencode('.jpg', img, encode_param)
img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0 img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0
# Apply random downsampling
if random_distortion == 'down' and random_downsample:
down_res = np.random.randint(int(0.125*resolution), int(0.25*resolution))
img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC)
img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC)
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)