|
|
|
@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
__version__ = '0.1'
|
|
|
|
__version__ = '0.1'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import bz2
|
|
|
|
import bz2
|
|
|
|
@ -38,6 +39,9 @@ add_arg('files', nargs='*', default=[])
|
|
|
|
add_arg('--scales', default=2, type=int, help='How many times to perform 2x upsampling.')
|
|
|
|
add_arg('--scales', default=2, type=int, help='How many times to perform 2x upsampling.')
|
|
|
|
add_arg('--model', default='small', type=str, help='Name of the neural network to load/save.')
|
|
|
|
add_arg('--model', default='small', type=str, help='Name of the neural network to load/save.')
|
|
|
|
add_arg('--train', default=False, type=str, help='File pattern to load for training.')
|
|
|
|
add_arg('--train', default=False, type=str, help='File pattern to load for training.')
|
|
|
|
|
|
|
|
add_arg('--train-blur', default=None, type=float, help='Sigma value for gaussian blur preprocess.')
|
|
|
|
|
|
|
|
add_arg('--train-noise', default=None, type=float, help='Sigma of normal distribution in preproc.')
|
|
|
|
|
|
|
|
add_arg('--train-jpeg', default=None, type=int, help='JPEG compression level in preprocessing.')
|
|
|
|
add_arg('--epochs', default=10, type=int, help='Total number of iterations in training.')
|
|
|
|
add_arg('--epochs', default=10, type=int, help='Total number of iterations in training.')
|
|
|
|
add_arg('--epoch-size', default=72, type=int, help='Number of batches trained in an epoch.')
|
|
|
|
add_arg('--epoch-size', default=72, type=int, help='Number of batches trained in an epoch.')
|
|
|
|
add_arg('--save-every', default=10, type=int, help='Save generator after every training epoch.')
|
|
|
|
add_arg('--save-every', default=10, type=int, help='Save generator after every training epoch.')
|
|
|
|
@ -100,11 +104,10 @@ os.environ.setdefault('THEANO_FLAGS', 'floatX=float32,device={},force_device=Tru
|
|
|
|
|
|
|
|
|
|
|
|
# Scientific & Imaging Libraries
|
|
|
|
# Scientific & Imaging Libraries
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
import scipy.optimize, scipy.ndimage, scipy.misc
|
|
|
|
import scipy.ndimage, scipy.misc, PIL.Image
|
|
|
|
|
|
|
|
|
|
|
|
# Numeric Computing (GPU)
|
|
|
|
# Numeric Computing (GPU)
|
|
|
|
import theano
|
|
|
|
import theano, theano.tensor as T
|
|
|
|
import theano.tensor as T
|
|
|
|
|
|
|
|
T.nnet.softminus = lambda x: x - T.nnet.softplus(x)
|
|
|
|
T.nnet.softminus = lambda x: x - T.nnet.softplus(x)
|
|
|
|
|
|
|
|
|
|
|
|
# Support ansi colors in Windows too.
|
|
|
|
# Support ansi colors in Windows too.
|
|
|
|
@ -147,35 +150,47 @@ class DataLoader(threading.Thread):
|
|
|
|
def run(self):
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
random.shuffle(self.files)
|
|
|
|
random.shuffle(self.files)
|
|
|
|
|
|
|
|
|
|
|
|
for f in self.files:
|
|
|
|
for f in self.files:
|
|
|
|
filename = os.path.join(self.cwd, f)
|
|
|
|
self.add_to_buffer(f)
|
|
|
|
try:
|
|
|
|
|
|
|
|
img = scipy.ndimage.imread(filename, mode='RGB')
|
|
|
|
def add_to_buffer(self, f):
|
|
|
|
except Exception as e:
|
|
|
|
filename = os.path.join(self.cwd, f)
|
|
|
|
warn('Could not load `{}` as image.'.format(filename),
|
|
|
|
try:
|
|
|
|
' - Try fixing or removing the file before next run.')
|
|
|
|
img = scipy.ndimage.imread(filename, mode='RGB').astype(np.float32)
|
|
|
|
files.remove(f)
|
|
|
|
if img.shape[0] < args.batch_shape or img.shape[1] < args.batch_shape:
|
|
|
|
continue
|
|
|
|
raise ValueError('Image is too small for training with size {}'.format(img.shape))
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
for _ in range(args.buffer_similar):
|
|
|
|
warn('Could not load `{}` as image.'.format(filename),
|
|
|
|
copy = img[:,::-1] if random.choice([True, False]) else img
|
|
|
|
' - Try fixing or removing the file before next run.')
|
|
|
|
h = random.randint(0, copy.shape[0] - self.orig_shape)
|
|
|
|
self.files.remove(f)
|
|
|
|
w = random.randint(0, copy.shape[1] - self.orig_shape)
|
|
|
|
return
|
|
|
|
copy = copy[h:h+self.orig_shape, w:w+self.orig_shape]
|
|
|
|
|
|
|
|
|
|
|
|
img = scipy.ndimage.gaussian_blur(img, sigma=args.train_blur) if args.train_blur else img
|
|
|
|
while len(self.available) == 0:
|
|
|
|
img += scipy.random.normal(scale=args.train_noise) if args.train_noise else 0.0
|
|
|
|
self.data_copied.wait()
|
|
|
|
if args.train_jpeg:
|
|
|
|
self.data_copied.clear()
|
|
|
|
buffer = io.BytesIO()
|
|
|
|
|
|
|
|
scipy.misc.toimage(img, cmin=0, cmax=255).save(buffer, format='jpeg', quality=args.train_jpeg)
|
|
|
|
i = self.available.pop()
|
|
|
|
with PIL.Image.open(buffer) as compressed:
|
|
|
|
self.orig_buffer[i] = np.transpose(copy / 255.0 - 0.5, (2, 0, 1))
|
|
|
|
img = scipy.misc.fromimage(compressed, mode='RGB')
|
|
|
|
seed = scipy.misc.imresize(copy, size=(self.seed_shape, self.seed_shape), interp='bilinear')
|
|
|
|
|
|
|
|
self.seed_buffer[i] = np.transpose(seed / 255.0 - 0.5, (2, 0, 1))
|
|
|
|
for _ in range(args.buffer_similar):
|
|
|
|
self.ready.add(i)
|
|
|
|
copy = img[:,::-1] if random.choice([True, False]) else img
|
|
|
|
|
|
|
|
h = random.randint(0, copy.shape[0] - self.orig_shape)
|
|
|
|
if len(self.ready) >= args.batch_size:
|
|
|
|
w = random.randint(0, copy.shape[1] - self.orig_shape)
|
|
|
|
self.data_ready.set()
|
|
|
|
copy = copy[h:h+self.orig_shape, w:w+self.orig_shape]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while len(self.available) == 0:
|
|
|
|
|
|
|
|
self.data_copied.wait()
|
|
|
|
|
|
|
|
self.data_copied.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i = self.available.pop()
|
|
|
|
|
|
|
|
self.orig_buffer[i] = np.transpose(copy / 255.0 - 0.5, (2, 0, 1))
|
|
|
|
|
|
|
|
seed = scipy.misc.imresize(copy, size=(self.seed_shape, self.seed_shape), interp='bilinear')
|
|
|
|
|
|
|
|
self.seed_buffer[i] = np.transpose(seed / 255.0 - 0.5, (2, 0, 1))
|
|
|
|
|
|
|
|
self.ready.add(i)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(self.ready) >= args.batch_size:
|
|
|
|
|
|
|
|
self.data_ready.set()
|
|
|
|
|
|
|
|
|
|
|
|
def copy(self, origs_out, seeds_out):
|
|
|
|
def copy(self, origs_out, seeds_out):
|
|
|
|
self.data_ready.wait()
|
|
|
|
self.data_ready.wait()
|
|
|
|
|