/gan-toolkit-pattern

Generating Fashion Images using GAN, without Writing Single Line of Code!

Primary LanguagePythonApache License 2.0Apache-2.0

Generating Fashion Images using GAN, without Writing Single Line of Code!

Introduction

Deep learning models used to perform classification tasks, are upper bounded by the number of images available in the training data. Data augmentation is often used to synthetically generated more data, looking very similar to the original data. GAN (Generative Advesarial Networks) are some state of the art models used for generating synthetic real-looking images.

Fashion MNIST is a 10-class classification dataset which is a drop-in replacement for MNIST digit classification dataset. Plenty of deep learning models are trained for performing classification on Fashion MNIST dataset (https://developer.ibm.com/patterns/train-a-model-on-fashion-dataset-using-tensorflow-with-ffdl/). The performance of these classifiers could be improved, if the training dataset could be augmented with more images.

How to Generate New Images

Consider, Deep Convolutional GAN (DCGAN) model which is a GAN for generating high quality fashion MNIST images. The DCGAN model is as shown below:

Fig. 1: Deep Convolutional GAN model (DCGAN) for generating fashion images

Let's try to implement the DCGAN model.

However, we propose a simple JSON representation of defining a GAN model, extending the modules explained in the previous section. The most simplistic realization of the DCGAN architecture is shown below:

{ 
    "generator":{
        "choice":"dcgan"
    },
    "discriminator":{
        "choice":"dcgan"
    },
    "data_path":"datasets/fmnist.p",
    "metric_evaluate":"MMD"
}

or, we could customize this architecture by using a DCGAN-Generator and a Vanilla-Discriminator, as follows:

{ 
   "generator":{
       "choice":"dcgan"
   },
   "discriminator":{
       "choice":"gan" #Just change the choice here! 
   },
   "data_path":"datasets/fmnist.p",
   "metric_evaluate":"MMD"
}

Flow Diagram

GAN Architecture Fig. 2: Modularized GAN architecture to be able to design any generator-discriminator combination in PyTorch

Featured Technologies

  • Python: Python is a programming language that lets you work more quickly and integrate your systems more effectively.
  • Flask: A lightweight Python web application framework.
  • PyTorch: An open source machine learning framework that accelerates the path from research prototyping to production deployment.

Build the Web App and Deploy in IBM Cloud

Follow these steps to setup and run this code pattern. The steps are described in detail below.

  1. Create an account with IBM Cloud
  2. Install IBM Cloud CLI
  3. Login to your IBM Cloud account using CLI
  4. Setup the IBM Cloud Target Org and Space
  5. Clone the Git Repo
  6. Create a GAN Config File
  7. Edit the Manifest File and ProcFile
  8. Push the App to a new Python Runtime in IBM Cloud
  9. Send the Config JSON file through a REST API Call
  10. Obtain the GAN Generated Images and Ouput

1. Create an account with IBM Cloud

Sign up for IBM Cloud. By clicking on create a free account you will get 30 days trial account.

2. Install IBM Cloud CLI

Download the latest installer for your specific OS and install the packge. To test the CLI, try running ibmcloud help in the terminal.

3. Login to your IBM Cloud account using CLI

Set up the IBM Cloud CLI Endpoint:

ibmcloud api https://api.ng.bluemix.net

Login to the IBM Cloud account:

ibmcloud login

4. Setup the IBM Cloud Target Org and Space

Setup the specific org, space, and resource group in which you would like to deploy the application.

ibmcloud target -o <org_name> -s <space_name> -g <resource_group_name>

5. Clone the Git Repo

Clone the entire code repository

```shell
$ git clone https://github.ibm.com/DARVIZ/gan-toolkit-code-patterns
$ cd gan-toolkit-code-patterns
```

6. Create a GAN Config File

The config file is a set of key-value pairs in JSON format. A collection of sample config files are provided here

The basic structure of the config json file is as follows,

    { 
        "generator":{
            "choice":"gan"
        },
        "discriminator":{
            "choice":"gan"
        },
        "data_path":"datasets/dataset1.p",
        "metric_evaluate":"MMD"
    }

The detailed documentation of the config files are provided here

7. Edit the Manifest File and ProcFile

Edit manifest.yaml in your root folder with the following content,

applications:
 - name: <application-name>
   random-route: true
   buildpack: python_buildpack
   command: python app.py
   disk_quota: 2G
   memory: 4G
   timeout: 600

Note: The <application-name> has to be unique at the entire IBM Cloud level

Edit Procfile in your root folder with the path of your GAN config file,

web: python app.py

Also note that, this app is tested extensively for Python 3.6.8. If you want to play around with different buildpack versions of Python, edit the runtime.txt file,

python-3.6.8

8. Push the App to a new Python Runtime in IBM Cloud

ibmcloud app push

Note: The app requires atleast 2GB in memory quota (as mentioned in the manifest file). The maximum memory quota for a Lite account is 256 MB and can be increased to 2GB only by upgrading to a billable account.

9. Send the Config JSON file through a REST API Call

The skeleton of the API call, through a CURL command would be as follows,

curl -X POST http://<application-name>.net/gan_model -F config_file=@<path-of-config-file>

Here is an example for the REST API call,

curl -X POST \
  http://gan-toolkit-all.mybluemix.net/gan_model -F config_file=@/agant/configs/gan_gan.json

10. Obtain the GAN Generated Images and Ouput

Default input and output paths (override these paths in the GAN config file)

* `logs/` : training logs
* `saved_models/` : saved trained models
* `train_results/` : saved all the intermediate generated images
* `datasets/` : input dataset path 

Login to IBM Cloud and open the corresponding Cloud Foundary Appliation from your dashboard. When you open the runtime on the left side pane, and connect to the runtime using SSH, you can find these folders with the outputs, as follows,

GAN Architecture Fig. 3: Obtain the output logs, trained model, and generated images from Python Runtime

(Or) Run Locally

  1. (Optional) If you want to setup an anaconda environment

    a. Install Anaconda from here

    b. Create a conda environment

    $ conda create -n gantoolkit python=3.6 anaconda

    c. Activate the conda environment

    $ source activate gantoolkit
  2. Clone the code

    $ git clone https://github.com/IBM/gan-toolkit
    $ cd gan-toolkit
  3. Install all the requirements. Tested for Python 3.6.x+

    $ pip install -r requirements.txt
  4. Train the model using a configuration file. (Many samples are provided in the configs folder)

    $ cd agant
    $ python main.py --config configs/gan_gan.json
  5. Default input and output paths (override these paths in the config file)

    • logs/ : training logs
    • saved_models/ : saved trained models
    • train_results/ : saved all the intermediate generated images
    • datasets/ : input dataset path

Analyze Results

The trained GAN model generates new images that looks very similar to the input dataset, it was trained on. The new generated images are found in the disk, in the agant/train_results/ folder. The obtained results and the newly generated images look like the following:

Fig. 4: (Left) Fashion images generated using DCGAN model. (Right) Fashion images generated using customized DCGAN-generator and GAN-discriminator

Related IBM Developer content

Related links

Acknowledgement

Acknowledging the contributions of our academic collaborators

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

Apache Software License (ASL) FAQ