Giter Site home page Giter Site logo

dagan's Introduction

DAGAN

Implementation of DAGAN: Data Augmentation Generative Adversarial Networks

Introduction

This is an implementation of DAGAN as described in https://arxiv.org/abs/1711.04340. The implementation provides data loaders, model builders, model trainers, and synthetic data generators for the Omniglot and VGG-Face datasets.

Installation

To use the DAGAN repository you must first install the project dependencies. This can be done by install miniconda3 from here with python 3 and running:

pip install -r requirements.txt

Datasets

The Omniglot and VGG-Face datasets can be obtained in numpy format here. They should then be placed in the datasets folder.

Training a DAGAN

After the datasets are downloaded and the dependencies are installed, a DAGAN can be trained by running:

python train_omniglot_dagan.py --batch_size 32 --generator_inner_layers 3 --discriminator_inner_layers 5 --num_generations 64 --experiment_title omniglot_dagan_experiment_default --num_of_gpus 1 --z_dim 100 --dropout_rate_value 0.5

Here, generator_inner_layers and discriminator_inner_layers refer to the number of inner layers per MultiLayer in the generator and discriminator respectively. num_generations refers to the number of samples generated for use in the spherical interpolations at the end of each epoch.

Multi-GPU Usage

Our implementation supports multi-GPU training. Simply pass --num_of_gpus <x> to the script to train on x GPUs (note that this only works if the GPUs are on the same machine).

Defining a new task for the DAGAN

If you want to train your own DAGAN on a new dataset you need to do the following:

  1. Edit data.py and define a new data loader class that inherits from either DAGANDataset or DAGANImblancedDataset. The first class is used when a dataset is balanced (i.e. every class has the same number of samples), the latter is for when this is not the case.

An example class for a balanced dataset is:

class OmniglotDAGANDataset(DAGANDataset):
    def __init__(self, batch_size, gan_training_index, reverse_channels, num_of_gpus, gen_batches):
        super(OmniglotDAGANDataset, self).__init__(batch_size, gan_training_index, reverse_channels, num_of_gpus,
                                                   gen_batches)

    def load_dataset(self, gan_training_index):

        self.x = np.load("datasets/omniglot_data.npy")
        x_train, x_test, x_val = self.x[:1200], self.x[1200:1600], self.x[1600:]
        x_train = x_train[:gan_training_index]

        return x_train, x_test, x_val

An example for an imbalanced dataset is:

class OmniglotImbalancedDAGANDataset(DAGANImbalancedDataset):
   def __init__(self, batch_size, gan_training_index, reverse_channels, num_of_gpus, gen_batches):
       super(OmniglotImbalancedDAGANDataset, self).__init__(batch_size, gan_training_index, reverse_channels,
                                                            num_of_gpus, gen_batches)

   def load_dataset(self, gan_training_index):

       x = np.load("datasets/omniglot_data.npy")
       x_temp = []
       for i in range(x.shape[0]):
           choose_samples = np.random.choice([i for i in range(1, 15)])
           x_temp.append(x[i, :choose_samples])
       self.x = np.array(x_temp)
       x_train, x_test, x_val = self.x[:1200], self.x[1200:1600], self.x[1600:]
       x_train = x_train[:gan_training_index]

       return x_train, x_test, x_val

In short, you need to define your own load_dataset function. This function should load your dataset in the form [num_classes, num_samples, im_height, im_width, im_channels]. Make sure your data values lie within the 0.0 to 1.0 range otherwise the system will fail to model them. Then you need to choose which classes go to each of your training, validation and test sets.

  1. Once your data loader is ready, use a template such as train_omniglot_dagan.py and change the data loader that is being passed. This should be sufficient to run experiments on any new image dataset.

To Generate Data

The model training automatically uses unseen data to produce generations at the end of each epoch. However, once you have trained a model to satisfication you can generate samples for the whole of the validation set using the following command:

python gen_omniglot_dagan.py -batch_size 32 --generator_inner_layers 3 --discriminator_inner_layers 5 --num_generations 64 --experiment_title omniglot_dagan_experiment_default --num_of_gpus 1 --z_dim 100 --dropout_rate_value 0.5 --continue_from_epoch 38

All the arguments must match the trained network's arguments and the continue_from_epoch argument must correspond to the epoch the trained model was at.

Additional generated data not shown in the paper

For further generated data please visit my Google Drive folder.

Acknowledgements

Special thanks to the CDT in Data Science at the University of Edinburgh for providing the funding and resources for this project. Furthermore, special thanks to my colleagues James Owers, Todor Davchev, Elliot Crowley, and Gavin Gray for reviewing this code and providing improvements and suggestions.

Furthermore, the interpolations used in this project are a result of the Sampling Generative Networks paper by Tom White. The code itself was found at https://github.com/dribnet/plat.

dagan's People

Contributors

antreasantoniou avatar ejcrowley avatar jamesowers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dagan's Issues

ValueError: No variables to optimize

I'm trying to run the command given in the readme to train the DAGAN. At first, I had to fix some compatibility issues between tensorflow versions 1.x and 2.9.1 such as tf_slim instead of tf.contrib and using keras layers. It compiles now, but during runtime, I get the error "No variables to optimize" in the train() method of dagan_networks_wgan.py on the following line:
opt_ops["g_opt_op"] = opts["g_opt"].minimize(losses["g_losses"],
var_list=self.g.variables,
colocate_gradients_with_ops=True)

It seems like there are no variables in the graph during the first time the train function is called? Has anyone gotten this to work in recent tensorflow versions?

Performance on Vanilla GAN ?

Did you trying using the Vanilla GAN before using W-GAN ? I tried your approach using Vanilla GAN on toy 2D data and it does not work and wanted to ask whether you faced similar problems.

Issues met when training DAGAN for new dataset

Hi, I am trying to train the DAGAN for a new dataset, which has been transformed into the standard numpy format [10, 28, 64, 64, 3] (10 people and 28 pictures for each)
I think this dataset is small enough. However, after I trained the model for over 200 epochs the loss of generator never converge. And images generated are not as expected. In the generated 16 by 16 images result, except for original images on the left top, all other images are the same, as shown below. I think the network didn't converge.
image
And this is the loss I got.
image
Could you please give me some suggestion/tricks on training the model? I will really appreciate!

Where is the .ckpt files?

I tried to replay the code with the given dataset and without any code modification, but it does not work at all.

I downloaded the 'vgg_face_data.npy' from here(https://drive.google.com/drive/folders/15x2C11OrNeKLMzBDHrv8NPOwyre6H3O5) and put it in /datasets.

(Command)
python train_face_dagan.py --batch_size 32 --generator_inner_layers 3 --discriminator_inner_layers 5 --num_generations 64 --num_of_gpus 8 --z_dim 100 --dropout_rate_value 0.5 --continue_from_epoch 38 > output.txt

(Error)
File "train_face_dagan.py", line 12, in
experiment.run_experiment()
File "experiment_builder.py", line 101, in run_experiment
ignore_missing_vars=True)
File "lib/python3.7/site-packages/tensorflow_core/contrib/framework/python/ops/variables.py", line 742, in assign_from_checkpoint_fn
reader = pywrap_tensorflow.NewCheckpointReader(model_path)
File "lib/python3.7/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py", line 885, in init
this = _pywrap_tensorflow_internal.new_CheckpointREader(filename)

tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceREader constructor: Failed to find any matching files for omniglot/dagan/experiment/saved_models/train_saved_model_omniglot_dagan_experiment_38.ckpt

tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceREader constructor: Failed to find any matching files for omniglot/dagan/experiment/saved_models/train_saved_model_omniglot_dagan_experiment_38.ckpt
tensorflow.python.gramework.errors_impl.NotFoundError:: command not found

The directory "omniglot/dagan/experiment/saved_models", was made by "build_experiment_folder(self.experiment_name)", so it exists.
However, I cannot find the code that makes "train_saved_model_omniglot_dagan_experiment_38.ckpt".

Do I need to add "train_saved_model_omniglot_dagan_experiment_38.ckpt" additionaly?
If not, why this code is not working?

I do not changed anything, I just did git clone this repository.

And I am totally newcomer for tensorflow.

Question about wgan-gp computation

Hi, Antreas,

I have a question regarding wgan-gp computation.
In your code,
slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1])).
Here, "reduction_indices=[1]" leads to the shape of (32, 28, 1) not (32, 1) when Omniglot training.
Is it correct?

Thanks,
Sungwoong.

BUG report: change height and width in architecture

Hi,

When I use my own dataset whose width and height is not the same. I noticed that there is a small mistake in the codes.

Because the data you used has the same width and height, so it's ok for your current version. But when data with different height and width is used, it can not work.

In file "dagan_architectures.py",

line 270, it should be
w = concat_shape[len(encoder_layers) - 1 - i][2]

line 318, it should be
w_size=upscale_shape[2],

line 319, it shoule be
h_size=upscale_shape[1]

line 355, it should be
dim_upscale=True, local_inner_layers=decoder_inner_layers, w_size=upscale_shape[2],

line 336, it shoule be
h_size=upscale_shape[1], dropout_rate=dropout_rate)

ResourceExhaustedError when using a new dataset with only 3000 images

I try to use a new dataset to train DAGAN. The commend is this:
nohup python train_face_dagan.py --batch_size 8 --generator_inner_layers 3 --discriminator_inner_layers 5 --num_generations 64
--experiment_title pubfig_dagan_experiment_default --num_of_gpus 3 --z_dim 5 --dropout_rate_value 0.5 >dagan_face_log.txt 2>&1 &

I tried different batch size(32, 16, 8), and the ResourceExhaustedError always exists. May I ask the resourse requirement of this code? I have 3 GTX TITANX GPUs.

Any suggestion of generate 128*128 RGB images?

I'm trying to use this network to generate 128*128 images by current setting,
python train_omniglot_dagan.py --batch_size 32 --generator_inner_layers 3 --discriminator_inner_layers 5 --num_generations 64 --experiment_title omniglot_dagan_experiment_default --num_of_gpus 1 --z_dim 100 --dropout_rate_value 0.5
but the result never get better during 12 hours training.
Is there any suggestion about the parameter setting?
For example, generator_layers, discriminator_layers, generator_inner_layers, discriminator_inner_layers, etc.

OSError

Thanks for this interesting repository. I tried it using the instructions but got the error below:
OSError: Failed to interpret file 'datasets/omniglot_data.npy' as a pickle

Cross Domain Dataset

I'm wondering if this method can be applied to datasets containing different domains such as dataset of ImageNet, which we have cats, flowers, dog, etc?

AC-GAN like Discriminator

The synthesized results of VGG-Face dataset seem to contain many other identities.
Have you tried AC-GAN like discriminator to avoid it? Or Is it not suitable for one-shot generation task?

augment new images

Hi,

I have 224x224 images belong different classes. To augment my image set should I convert these to .npy format and put them datasets folder and run the code? Is it enough?

Regards.

Do we need resampling of noise variable for updating generator parameters ?

Normally while training W-GAN we iteratively update the discriminator parameters and generator parameters sequentially. Before updating the generator parameters, we resample the noise variable again as shown in Page 8 of https://arxiv.org/pdf/1701.07875.pdf. But in your code in Lines 165-168 of https://github.com/AntreasAntoniou/DAGAN/blob/master/dagan_networks_wgan.py you use the same noise variables for generator and discriminator update. Is there any particular reason for doing so or both works ?

Question about experiment setting in the paper

Hi Antoniou !

I cannot understand the following statement in your paper.

The real or fake label was also passed to the network

It means that adding one extra class "fake" to final layer of CNN?

Thank you!

How to make *.npy file?

Images from "vgg_face_data" have so many different sizes.

For example,

https://www.contactmusic.com/pics/ld/active_for_life_arrivals_090110/a.j_buckley_2706152.jpg is 500x750

http://www.kino-teatr.ru/acter/album/73932/50682.jpg is 400x600

and so on.

'vgg_face_data.npy' is reshaped into (2354, 100, 64, 64, 3).

It might be, 64x64 images with 3 channels RGB.

2354 means number of classes and 100 stands number of samples.

But the size of vgg face data set is various, all the images would be reshaped into 64x64?

can I use DAGAN for binary classes

Hello,
I really need your help, it will be very much appreciated as I can't solve it, unfortunately.
I have a training dataset of classes 1 and 0. 6000x50x50x3 per each class. I want to generate more data for both classes. Can I do it using DAGAN? If yes I was wondering how to define this: " Then you need to choose which classes go to each of your training, validation and test sets".

In your code snippet from load_dataset will this be correct?
x_train, x_test, x_val = self.x[:1], self.x[1:2], self.x[1:2]
x_train = x_train[:gan_training_index]
return x_train, x_test, x_val
I guess here synthetic images for only one class will be generated, what should I do to generate for another class as well?

Is the purpose of this code deliberately creating an unbalanced data set?

Is the purpose of this code deliberately creating an unbalanced data set?

Quoted from data.OmniglotImbalancedDAGANDataset.load_dataset

for i in range(x.shape[0]):
    choose_samples = np.random.choice([i for i in range(1, 15)])
    x_temp.append(x[i, :choose_samples])
self.x = np.array(x_temp)

This means that I don't need to perform this weird step when actually loading an unbalanced data set. Is my understanding correct?

Thanks for your help and code!

Issues met when generating data

Hi, I finished training with omniglot and am trying to generate data, but Error happen.
Error code is shown below. I think that using tensorflow-gpu latest version is the reason ,but I could not modify your code.

Traceback (most recent call last):
File "gen_omniglot_dagan.py", line 24, in
experiment.run_experiment()
File "/ ////generation_builder.py", line 89, in run_experiment
ignore_missing_vars=True)
File "///////python3.6/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 674, in assign_from_checkpoint_fn
reader = pywrap_tensorflow.NewCheckpointReader(model_path)
File "///////python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 290, in NewCheckpointReader
return CheckpointReader(compat.as_bytes(filepattern), status)
File "///////python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in exit
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for omniglot/dagan/experiment/default/saved_models//omniglot_dagan_experiment_default_4.ckpt

how to make the environment

I see the requirements in your file,but I want to know if I am not make the envs like that, will I run your code success fully,because I can not find the version that too earlier, and if I install the version you given ,that would remained me that there some conflict way in your installation.wish you could help me.

training time

Thank you very much for sharing your code. I am learning your method, but can you tell me how long it takes to run and train two data sets? I'm not sure if there was an error during the run, but it has taken a lot of time.
look forward to your reply.

What is the role of Linear Projection?

Hi, Antoniou!

What is the role of Linear Projection form z to representation space?
I wonder there is a problem if we just put Gaussian noise on representation space.

can you give your dataset?

Hello, thanks for a good repo.. I need your dataset to see the results.. can you provide your dataset?

DenseNet classifier

Thank you for your impressive work!
I want to replicate classification experiment with my own dataset.

  1. I know the detail is well explained in the paper, but it's very helpful if you could tell me which DenseNet repository you used for classification experiment.
  2. Plus, did you normalize generated data to 0-1 before train classifier?

Thank you in advance!

vgg_face_data.npy dtype

Thank you for sharing your code. But when I tried to load the file 'vgg_face_data.npy', I got the error below:
ValueError: cannot reshape array of size 1073479997 into shape (2354,100,64,64,3)

Could you tell me the the np.dtype when you packed the VGG-Face datasets? Look forward to your kind reply.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.