def train(image_path, model_path, gpu, latent_width, color_channels, batch, epoch, shape, kl_ratio): 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('.') ] iae = ImageAutoEncoder(img_width=shape[0], img_height=shape[1], encode_layers=[1000, 700, 300], decode_layers=[500, 700, 1000], rec_kl_ratio=kl_ratio, flag_gpu=gpu, latent_width=latent_width, color_channels=color_channels) iae.load_images(file_paths) iae.fit(batch_size=batch, n_epochs=epoch) iae.dump(model_path)
def train(image_path, model_path, gpu, latent_width, color_channels, batch, epoch, shape, kl_ratio): 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('.')] iae = ImageAutoEncoder(img_width=shape[0], img_height=shape[1], encode_layers=[1000, 700, 300], decode_layers=[500, 700, 1000], rec_kl_ratio=kl_ratio, flag_gpu=gpu, latent_width=latent_width, color_channels=color_channels) iae.load_images(file_paths) iae.fit(batch_size=batch, n_epochs=epoch) iae.dump(model_path)
def generate(model_path, img_dir, number, extremity, mean, format, image_multiplier): click.echo("Loading Model...") model = ImageAutoEncoder.load(model_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, model.latent_dim)).astype('float32') reconstructed = model.inverse_transform(vec) * 255.0 for i in range(number): im = reconstructed[i] im = np.clip(image_multiplier*im/im.max(), 0, 255) im = Image.fromarray(np.uint8(reconstructed[i])) fname = "{0}.{1}".format(i, format) path = os.path.join(img_dir, fname) im.save(path)
def generate(model_path, img_dir, number, extremity, mean, format, image_multiplier): click.echo("Loading Model...") model = ImageAutoEncoder.load(model_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, model.latent_dim)).astype('float32') reconstructed = model.inverse_transform(vec) * 255.0 for i in range(number): im = reconstructed[i] im = np.clip(image_multiplier * im / im.max(), 0, 255) im = Image.fromarray(np.uint8(reconstructed[i])) fname = "{0}.{1}".format(i, format) path = os.path.join(img_dir, fname) im.save(path)