A plug-and-play GAN image generator.
Project description
🌄 imagegengan
A plug-and-play GAN image generator using a Keras/Tensorflow backend. [1]
⬇️ Installation
pip install imagegengan
✨ Features
- Completely plug-and-play DCGAN implementation. [2]
- Takes images of any dimensions as inputs and outputs to the dimensions of your choice.
- Supports custom generative and discriminative models, if you need that.
- Built-in image augmentation to mitigate effects of small training datasets.
🏃♂️ Quick Start
from imagegengan import imagegengan
# This is a relative path to a directory containing
# a ton of different-sized images.
images_dir = "dog_images/"
# These are the dimensions of our generated image.
# The third dimension is the # of channels, so use
# 3 for a color (RGB) image and 1 for grayscale.
img_shape = (200, 200, 3)
# Create the image generator.
image_generator = imagegengan(img_shape=img_shape)
# Train the image generator.
image_generator.train(from_dir=images_dir, epochs=150, batch_size=32)
# Generate a new image (outputs a numpy array
# that can be displayed or saved as an image).
generated_image = image_generator.generate()
Or, without the comments:
from imagegengan import imagegengan
image_generator = imagegengan(img_shape=(200, 200, 3))
image_generator.train(from_dir="dog_images/", epochs=150, batch_size=32)
generated_image = image_generator.generate()
📖 Documentation
imagegenrnn
Class
__init__(...)
Initializes a new imagegenrnn object.
img_shape
: The image shape as a three-dimensional tuple (height, width, channels). (default(224, 224, 3)
)generator
: The generator model to use. (defaultGenerator.Default
)discriminator
: The discriminator to use. (defaultDiscriminator.Default
)upsample_layers
: The sequence of layers for upsampling in the generator and downsampling in the discriminator. (default[1024, 512, 256, 128, 64]
)kernel_size
: The kernel size for convolutions in the GAN. 5x5 is the standard so we advise against changing this. (default(5, 5)
)noise_depth
: The length of the input noise vector. 100 is the standard so we advise against changing this. (default100
)lr
: The learning rate of both the generator and discriminator. (default0.00015
)
train(...)
Train the GAN model on either images from a directory, images from a list, or images preprepared with the imagegengan.prepare_images function. Only one of such inputs must be provided.
from_dir
: The directory to train from. The images here can be different sizes and the resizing function will standardize them. Do not supply from_list or from_prepared if you choose to train from a directory. (defaultNone
)from_list
: A numpy list containing unprepared images to train from. The images here can be different sizes and the resizing function will standardize them. Do not supply from_dir or from_prepared if you choose to train from a list. (default[]
)from_prepared
: A numpy list or string .npy filename containing prepared images using the imagegengan.prepare_images function. The images here must be the same size as the image shape of the imagegengan class. Do not supply from_dir or from_list if you choose to train from a prepared list. (default[]
)epochs
: The number of epochs to train on. (default100
)batch_size
: The size of each training batch. Must be less than or equal to the number of input images. It is advised to keep this number small (~8) to avoid tensor overflows. (default8
)lr
: The learning rate of the GAN (not of the generator or discriminator that were initialized in the constructor). Defaults to the generator's and discriminator's learning rate. (defaultNone
)noise_level
: The coefficient of noise when training the discriminator. (default0.2
)save_interval
: The interval (in epochs) of saving the GAN to a file. If save_to is not None, defaults to 1/10th of the number of epochs. (defaultNone
)save_to
: The directory to save to on the save_interval. If None, the GAN is not saved. (defaultNone
)grayscale
: Boolean indicating whether or not the images should be converted into grayscale. (defaultFalse
)verbose
: Boolean indicating whether the training's status should be outputted. (defaultTrue
)resizing
: The resizing method. Use Resizing.CONTAIN or 0 to pad the images, Resizing.STRETCH or 1 to stretch the images and ignore aspect ratio, and Resizing.COVER or 2 to crop the images while maintaining aspect ratio. (defaultResizing.COVER
)padding_color
: The color of the padding used if the resizing mode is Resizing.CONTAIN or 0. Defaults to 0, which is black. (default0
)horizontal_flip
: Boolean indicating whether the training images can be horizontally flipped during image data augmentation. This helps the GAN generator more kinds of images. (defaultFalse
)seed
: Integer seed for the random image augmenter. (default1
)rounds
: Number of rounds to go through during image augmentation. The more rounds, the longer it takes to train, but the more results yielded. (default1
)limit
: Limits the number of training images used, mainly for debugging or retraining purposes. (defaultNone
)shuffle
: Boolean indicating whether or not to shuffle the training images. Often helpful during retraining. (defaultFalse
)
generate(...)
Generate a single image or a batch of images in numpy format.
num_outputs
: The number of images to output. If 1, returns a single image. Otherwise, returns an array of images. (default1
)save_to_dir
: The directory to save the images to, if not None. (defaultNone
)file_prefix
: The prefix of the generated image files. Does not do anything if save_to_dir is None. (default"generated_img"
)verbose
: Boolean indicating whether the status of image generation should be outputted. (defaultTrue
)
returns
A single numpy array output image or a list of such images.
save(...)
Save the imagegengan to a .h5 file. Use like
gen.save('myfile')
.
filename
: The name of the file to save the image as.
load(...)
Load the imagegengan from a .h5 file. Use like
gen.load('myfile')
.
filename
: The name of the file to load the image from.
prepare_images(...)
(static method)
Creates a numpy image list of resized images from either images from a directory or images from a list. Only one of such inputs must be provided.
img_shape
: The image shape as a three-dimensional tuple (height, width, channels).from_dir
: The directory to train from. The images here can be different sizes and the resizing function will standardize them. Do not supply from_list or from_prepared if you choose to train from a directory. (defaultNone
)from_list
: A numpy list containing unprepared images to train from. The images here can be different sizes and the resizing function will standardize them. Do not supply from_dir or from_prepared if you choose to train from a list. (default[]
)save_to_npy
: A string filename to where the numpy array output can be saved. Saving prepared images to a .npy file and inputting these images into imagegengan.train using from_prepared is the recommended (but not easiest) training method. If None, the output images are not saved. (defaultNone
)verbose
: Boolean indicating whether the image preparation status should be outputted. (defaultTrue
)grayscale
: Boolean indicating whether or not the images should be converted into grayscale. (defaultFalse
)resizing
: The resizing method. Use Resizing.CONTAIN or 0 to pad the images, Resizing.STRETCH or 1 to stretch the images and ignore aspect ratio, and Resizing.COVER or 2 to crop the images while maintaining aspect ratio. (defaultResizing.COVER
)padding_color
: The color of the padding used if the resizing mode is Resizing.CONTAIN or 0. Defaults to 0, which is black. (default0
)limit
: Limits the number of training images used, mainly for debugging or retraining purposes. (default None)shuffle
: Boolean indicating whether or not to shuffle the training images. Often helpful during retraining. (defaultFalse
)
returns
A numpy array of prepared images that are all the same size.
📄 Changelist
1.0.0
- Initial work; used Tiago Freitas's implementation, Mitchell Jolly's implementation, and this issue on ImageDataGenerator flows for reference.
📚 References
[1] Chollet, Francois et al. "Keras." https://keras.io. (2015).
[2] Alec Radford, et al. "Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks." (2015).
Author
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file imagegengan-1.0.2.tar.gz
.
File metadata
- Download URL: imagegengan-1.0.2.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
d449179e03207ca2fc336d047931324ed2bd77968efa2645c9eec8845798859a
|
|
MD5 |
30ce7691b2f0552539fe8b7289e33cad
|
|
BLAKE2b-256 |
4e396a39f92b3c1fd5fdfbed5a8b48c23404dec05808df1494565929d00eb171
|
File details
Details for the file imagegengan-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: imagegengan-1.0.2-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
5152589c6558d0845c3cb957c1585b7ddb5f314778974423a50a57d9286d1c12
|
|
MD5 |
344483891577dd2c3dace5d0e103f8ed
|
|
BLAKE2b-256 |
1a658d1682be409c57f30038b20f41ed839c5ac21b5939fc1aa3e69a4b197c3a
|