def train(image_path, model_path, gpu, latent_width, color_channels, batch, epoch, shape, kl_ratio, mode): if not image_path[-1] == '/': image_path += '/' if not os.path.exists(os.path.dirname(image_path)): os.makedirs(os.path.dirname(image_path)) if model_path[-1] == '/': click.echo("Model Path should be a file path not a directory path. " "Removing trailing '/' ...") model_path = model_path[:-1] file_paths = [ os.path.join(image_path, f) for f in os.listdir(image_path) if os.path.isfile(os.path.join(image_path, f)) and not f.startswith('.') ] vae = VAE(img_width=shape[0], img_height=shape[1], encode_layers=[1000, 700, 300], decode_layers=[500, 700, 1000], kl_ratio=kl_ratio, flag_gpu=gpu, latent_width=latent_width, color_channels=color_channels, mode=mode) x_all = vae.load_images(file_paths) vae.fit(x_all, batch_size=batch, n_epochs=epoch) directory = os.path.dirname(model_path) name = os.path.basename(model_path) vae.save(directory, name)
def train(image_path, model_path, gpu, latent_width, color_channels, batch, epoch, shape, kl_ratio, mode): if not image_path[-1] == '/': image_path += '/' if not os.path.exists(os.path.dirname(image_path)): os.makedirs(os.path.dirname(image_path)) if model_path[-1] == '/': click.echo("Model Path should be a file path not a directory path. " "Removing trailing '/' ...") model_path = model_path[:-1] file_paths = [os.path.join(image_path, f) for f in os.listdir(image_path) if os.path.isfile(os.path.join(image_path, f)) and not f.startswith('.')] vae = VAE(img_width=shape[0], img_height=shape[1], encode_layers=[1000, 700, 300], decode_layers=[500, 700, 1000], kl_ratio=kl_ratio, flag_gpu=gpu, latent_width=latent_width, color_channels=color_channels, mode=mode) x_all = vae.load_images(file_paths) vae.fit(x_all, batch_size=batch, n_epochs=epoch) directory = os.path.dirname(model_path) name = os.path.basename(model_path) vae.save(directory, name)
def generate(model_path, optimizer_path, meta_path, img_dir, number, extremity, mean, format, image_multiplier): click.echo("Loading Model...") vae = VAE.load(model_path, optimizer_path, meta_path) click.echo("Model Loaded") click.echo("Saving Images to {0} as {1}".format(img_dir, format)) if not img_dir[-1] == '/': img_dir += '/' if not os.path.exists(os.path.dirname(img_dir)): os.makedirs(os.path.dirname(img_dir)) variance = extremity vec = np.random.normal(mean, variance, (number, vae.latent_width)).astype('float32') reconstructed = vae.inverse_transform(vec, test=True) * 255 for i in range(number): im = reconstructed[i] im = np.clip(image_multiplier * im, 0, 255) im = Image.fromarray(np.squeeze(im.astype(np.uint8))) fname = "{0}.{1}".format(i, format) path = os.path.join(img_dir, fname) im.save(path)
def generate(model_path, optimizer_path, meta_path, img_dir, number, extremity, mean, format, image_multiplier): click.echo("Loading Model...") vae = VAE.load(model_path, optimizer_path, meta_path) click.echo("Model Loaded") click.echo("Saving Images to {0} as {1}".format(img_dir, format)) if not img_dir[-1] == '/': img_dir += '/' if not os.path.exists(os.path.dirname(img_dir)): os.makedirs(os.path.dirname(img_dir)) variance = extremity vec = np.random.normal(mean, variance, (number, vae.latent_width)).astype('float32') reconstructed = vae.inverse_transform(vec, test=True) * 255 for i in range(number): im = reconstructed[i] im = np.clip(image_multiplier*im, 0, 255) im = Image.fromarray(np.squeeze(im.astype(np.uint8))) fname = "{0}.{1}".format(i, format) path = os.path.join(img_dir, fname) im.save(path)