forked from mrq/DL-Art-School
Don't apply jpeg corruption & noise corruption together
This causes some severe noise.
This commit is contained in:
parent
111450f4e7
commit
aba83e7497
|
@ -29,16 +29,17 @@ class ImageCorruptor:
|
||||||
rand_int_a = random.randint(1, 999999)
|
rand_int_a = random.randint(1, 999999)
|
||||||
|
|
||||||
corrupted_imgs = []
|
corrupted_imgs = []
|
||||||
|
applied_augs = augmentations + self.fixed_corruptions
|
||||||
for img in imgs:
|
for img in imgs:
|
||||||
for aug in augmentations:
|
for aug in augmentations:
|
||||||
img = self.apply_corruption(img, aug, rand_int_a)
|
img = self.apply_corruption(img, aug, rand_int_a, applied_augs)
|
||||||
for aug in self.fixed_corruptions:
|
for aug in self.fixed_corruptions:
|
||||||
img = self.apply_corruption(img, aug, rand_int_f)
|
img = self.apply_corruption(img, aug, rand_int_f, applied_augs)
|
||||||
corrupted_imgs.append(img)
|
corrupted_imgs.append(img)
|
||||||
|
|
||||||
return corrupted_imgs
|
return corrupted_imgs
|
||||||
|
|
||||||
def apply_corruption(self, img, aug, rand_int):
|
def apply_corruption(self, img, aug, rand_int, applied_augmentations):
|
||||||
if 'color_quantization' in aug:
|
if 'color_quantization' in aug:
|
||||||
# Color quantization
|
# Color quantization
|
||||||
quant_div = 2 ** ((rand_int % 3) + 2)
|
quant_div = 2 ** ((rand_int % 3) + 2)
|
||||||
|
@ -89,6 +90,7 @@ class ImageCorruptor:
|
||||||
noise_intensity = (rand_int % 4 + 2) / 255.0 # Between 1-4
|
noise_intensity = (rand_int % 4 + 2) / 255.0 # Between 1-4
|
||||||
img += np.random.randn(*img.shape) * noise_intensity
|
img += np.random.randn(*img.shape) * noise_intensity
|
||||||
elif 'jpeg' in aug:
|
elif 'jpeg' in aug:
|
||||||
|
if 'noise' not in applied_augmentations and 'noise-5' not in applied_augmentations:
|
||||||
if aug == 'jpeg':
|
if aug == 'jpeg':
|
||||||
lo=10
|
lo=10
|
||||||
range=20
|
range=20
|
||||||
|
@ -104,7 +106,7 @@ class ImageCorruptor:
|
||||||
img = (img * 255).astype(np.uint8)
|
img = (img * 255).astype(np.uint8)
|
||||||
img = Image.fromarray(img)
|
img = Image.fromarray(img)
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
img.save(buffer, "JPEG", quality=qf, optimice=True)
|
img.save(buffer, "JPEG", quality=qf, optimize=True)
|
||||||
buffer.seek(0)
|
buffer.seek(0)
|
||||||
jpeg_img_bytes = np.asarray(bytearray(buffer.read()), dtype="uint8")
|
jpeg_img_bytes = np.asarray(bytearray(buffer.read()), dtype="uint8")
|
||||||
img = read_img("buffer", jpeg_img_bytes, rgb=True)
|
img = read_img("buffer", jpeg_img_bytes, rgb=True)
|
||||||
|
@ -112,5 +114,7 @@ class ImageCorruptor:
|
||||||
# Lightening / saturation
|
# Lightening / saturation
|
||||||
saturation = float(rand_int % 10) * .03
|
saturation = float(rand_int % 10) * .03
|
||||||
img = np.clip(img + saturation, a_max=1, a_min=0)
|
img = np.clip(img + saturation, a_max=1, a_min=0)
|
||||||
|
elif 'none' not in aug:
|
||||||
|
raise NotImplementedError("Augmentation doesn't exist")
|
||||||
|
|
||||||
return img
|
return img
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import random
|
||||||
from bisect import bisect_left
|
from bisect import bisect_left
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
@ -43,14 +44,14 @@ class SingleImageDataset(BaseUnsupervisedImageDataset):
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
opt = {
|
opt = {
|
||||||
'name': 'amalgam',
|
'name': 'amalgam',
|
||||||
'paths': ['F:\\4k6k\\datasets\\images\\flickr\\testbed'],
|
'paths': ['F:\\4k6k\\datasets\\images\\mi1_256'],
|
||||||
'weights': [1],
|
'weights': [1],
|
||||||
'target_size': 128,
|
'target_size': 128,
|
||||||
'force_multiple': 32,
|
'force_multiple': 32,
|
||||||
'scale': 2,
|
'scale': 2,
|
||||||
'eval': False,
|
'eval': False,
|
||||||
'fixed_corruptions': ['jpeg'],
|
'fixed_corruptions': ['jpeg-broad'],
|
||||||
'random_corruptions': ['color_quantization', 'gaussian_blur', 'motion_blur', 'smooth_blur', 'noise', 'saturation'],
|
'random_corruptions': ['noise-5', 'none'],
|
||||||
'num_corrupts_per_image': 1
|
'num_corrupts_per_image': 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,9 +59,9 @@ if __name__ == '__main__':
|
||||||
import os
|
import os
|
||||||
os.makedirs("debug", exist_ok=True)
|
os.makedirs("debug", exist_ok=True)
|
||||||
for i in range(0, len(ds)):
|
for i in range(0, len(ds)):
|
||||||
o = ds[i]
|
o = ds[random.randint(0, len(ds))]
|
||||||
for k, v in o.items():
|
for k, v in o.items():
|
||||||
if 'path' not in k and 'center' not in k:
|
if 'LQ' in k and 'path' not in k and 'center' not in k:
|
||||||
#if 'full' in k:
|
#if 'full' in k:
|
||||||
#masked = v[:3, :, :] * v[3]
|
#masked = v[:3, :, :] * v[3]
|
||||||
#torchvision.utils.save_image(masked.unsqueeze(0), "debug/%i_%s_masked.png" % (i, k))
|
#torchvision.utils.save_image(masked.unsqueeze(0), "debug/%i_%s_masked.png" % (i, k))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user