Code Monkey home page Code Monkey logo

cyclegan's Introduction

CycleGAN

Tensorflow implementation of CycleGAN.

  1. Original implementation
  2. Paper

Blog

If you would like to understand the paper and see how to implement it by your own, you can have look at the blog by me

Both code and blog received mention on original project homepage over here

CycleGAN model

CycleGAN model can be summarized in the following image. For full details about implementation and understanding CycleGAN you can read the tutorial at this link

Model

Generator

Generator

Discriminator

Discriminator

Our Results

We ran the model for horse2zebra dataset but because of the lack of resources, we just ran the model for 100 epochs and got following results.

Results

Final Comments

  1. During training we noticed that the ouput results were sensitive to initialization. Thanks to vanhuyz for pointing this out and suggesting training multiple times to get best results. You might notice background color being reversed as in following image. This effect can be observed only after 10-20 epochs and you can try to run the code again.

Fail

  1. We also think that this model is not good fit to change the shape of object. We tried to run the model for converting a men's face to a look alike women's face. For that we used celebA dataset but the results are not good and images produced are quite distorted.

cyclegan's People

Contributors

architrathore avatar hardikbansal avatar kopytjuk 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

cyclegan's Issues

int object is not callable.

Hello, i am having tensorflow version 1.11.0
i had issue on initialize varaible but solved that issue by modifing to this (tf.global_variables_initializer(), tf.local_variables_initializer()), earlier it had only global initializer.

After that i am having another issue on this line
if(image_tensor.size() == img_sizebatch_sizeimg_layer):
it seems to be issue of size() func but displaying error of "int object is not callable"
i am running in python 3. and have horse2zebra images.

Questions regarding patch discriminator

I’m puzzled since your already use a 70X70 patch discriminator in gen_discriminator but you still use random crop to build another “patch discriminator ”. I guess 70X70 is the size of the receptive field instead of the size of a random crop.

Training on CycleGAN

Hello,

I am using your tensorflow implementation of the Cycle GAN for my own dataset.
However, there are few questions I want to ask about, and I hope you can be able to help me:

1- Before training, I create the csv file, which is pairing all images in both domains together randomly, but my question is, between every epoch and the other, do the batches get shuffled ? or for each epoch they are provided by the same sequence ?

2- I don't understand what is the "CycA" and "CycB" output images

3- If I don't want to use your network bidirectionally, is there something which I have/need to do ? I mean, I only want to translate from domain A -> B, and not vice versa

4- My dataset are images which are much higher in resolution than the provided dataset. They are 1242x375, will this be applicable for the deploying of the network ? or what shall I edit exactly in any part of the code to adapt my dataset

Thanks a lot in advance
Looking forward to hearing from you soon

Best Regards,
Mostafa

Change code to use in the newer version of TensorFlow

I'm using TensorFlow of version 1.10.0. I need to do the following change in file main.py to run this code:

  1. Change size() into size on line 99 & 104;
  2. Add tf.local_variables_initializer() after line 247&330 and add sess.run() after line 251&334.

Add I created a pull request for this. I also made some changes on the code style.

Need to initialize local variables

I should've sent a PR but anyway - I get the following error when I try and run the code

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value matching_filenames [[Node: matching_filenames/read = Identity[T=DT_STRING, _class=["loc:@matching_filenames"], _device="/job:localhost/replica:0/task:0/cpu:0"](matching_filenames)]]

This is resolved by
init = (tf.global_variables_initializer(), tf.local_variables_initializer())
as mentioned here

main.py can read 8 bit grey picture and report error while train dataset horse2zebra

While in source code main.py, below line can't procedure gray picture(8 bit color deep) in horse2zebra dataset(after unzip horse2zebra dataset, I find some picture is gray with 8 bit deep in trainB), and get err.

self.B_input[i] = image_tensor.reshape((batch_size,img_height, img_width, img_layer))
ValueError: cannot reshape array of size 65536 into shape (1,256,256,3)

Testing on training data?

Hello,

I don't see any use of the files under testA or testB in your code, are you testing the model on training data?

thanks!

FailedPreconditionError

Hi!
I am pretty sure that I am making a stupid mistake but when I try to run the code on the horse2zebra dataset I get the a FailedPreconditionError, details see below. The dataset is in the correct form in the cycleGan folder ./input/horse2zebra/trainA etc. I have a Windows machine with the recent version of tensorflow installed (I tried both pyton 3.5 and 3.6). I want to try how this cycle Gan implementation worksfor my research, so help would be much appreciated!
Thx!

Here is the full error message:

Traceback (most recent call last):
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1139, in _do_call
return fn(*args)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1121, in _run_fn
status, run_metadata)
File "C:\Users\Eulsen\Anaconda3\lib\contextlib.py", line 88, in exit
next(self.gen)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value matching_filenames
[[Node: matching_filenames/read = IdentityT=DT_STRING, _class=["loc:@matching_filenames"], _device="/job:localhost/replica:0/task:0/cpu:0"]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 362, in
main()
File "main.py", line 356, in main
model.train()
File "main.py", line 254, in train
self.input_read(sess)
File "main.py", line 87, in input_read
num_files_A = sess.run(self.queue_length_A)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
run_metadata_ptr)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value matching_filenames
[[Node: matching_filenames/read = IdentityT=DT_STRING, _class=["loc:@matching_filenames"], _device="/job:localhost/replica:0/task:0/cpu:0"]]

Caused by op 'matching_filenames/read', defined at:
File "main.py", line 362, in
main()
File "main.py", line 356, in main
model.train()
File "main.py", line 238, in train
self.input_setup()
File "main.py", line 56, in input_setup
filenames_A = tf.train.match_filenames_once("./input/horse2zebra/trainA/*.jpg")
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\training\input.py", line 69, in match_filenames_once
collections=[ops.GraphKeys.LOCAL_VARIABLES])
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1679, in variable
caching_device=caching_device, name=name, dtype=dtype)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 200, in init
expected_shape=expected_shape)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 319, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1303, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\Eulsen\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in init
self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value matching_filenames
[[Node: matching_filenames/read = IdentityT=DT_STRING, _class=["loc:@matching_filenames"], _device="/job:localhost/replica:0/task:0/cpu:0"]]

save_training_images dumping empty image

i tried running the code, after couple of epochs, save_training_images is dumping empty image. It doesn't look relisting. is it because yet to learn or something wrong in code?

Result problems

I appreciate your sharing highly !
Have you tested the results and compared with the original version ?

I think there are some problems in your function input_read() in main.py.

I think there are some problems in your function input_read() in main.py.
Shouldn't we add code such like "while not coord.should_stop():" to make that the variable image_file_A always get images from variable filename_queue_A ?
Anyway ,when I run you code, there are some problems. I am confused how you can get your results.
Hope for your help, thank you

placeholder input_a and real pic variable a_input have different width and height if input pic dataset is not square

From my opinion, paired model pix2pix is special case of unpaired model cyclegan, so i try to use labeled fake pic and real camera pic as input A AND B to train incyclegan model. both size of them are 96wx32h(not square w==h), then i find main.py cannot run and report reshape error(you can use no-square pic and train model then maybe get the error).after some guess and detail look on your codes, it seems input_A's width set to height of the A_input. But it can also run if train data and test data are both square. anyhow, after adjusted(swtich) position of w and h, i run the model trained and tested, also the final result is not as better as i image, maybe i need adjust lr or other Hparams.

thanks so much for provide tensorflow version of cyclegan.

finally, sorry for my poor English.

build_gen_discriminator and patch_discriminator

hello hardikbansal
I am fuzzy on build_gen_discriminator and patch_discriminator. Can you tell me the difference between build_gen_discriminato and patch_discriminator ?

def build_gen_discriminator(inputdisc, name="discriminator"):

with tf.variable_scope(name):
    f = 4
    o_c1 = general_conv2d(inputdisc, ndf, f, f, 2, 2, 0.02, "SAME", "c1", do_norm=False, relufactor=0.2)
    o_c2 = general_conv2d(o_c1, ndf*2, f, f, 2, 2, 0.02, "SAME", "c2", relufactor=0.2)
    o_c3 = general_conv2d(o_c2, ndf*4, f, f, 2, 2, 0.02, "SAME", "c3", relufactor=0.2)
    o_c4 = general_conv2d(o_c3, ndf*8, f, f, 1, 1, 0.02, "SAME", "c4",relufactor=0.2)
    o_c5 = general_conv2d(o_c4, 1, f, f, 1, 1, 0.02, "SAME", "c5",do_norm=False,do_relu=False)

    return o_c5

def patch_discriminator(inputdisc, name="discriminator"):

with tf.variable_scope(name):
    f= 4

    patch_input = tf.random_crop(inputdisc,[1,70,70,3])
    o_c1 = general_conv2d(patch_input, ndf, f, f, 2, 2, 0.02, "SAME", "c1", do_norm="False", relufactor=0.2)
    o_c2 = general_conv2d(o_c1, ndf*2, f, f, 2, 2, 0.02, "SAME", "c2", relufactor=0.2)
    o_c3 = general_conv2d(o_c2, ndf*4, f, f, 2, 2, 0.02, "SAME", "c3", relufactor=0.2)
    o_c4 = general_conv2d(o_c3, ndf*8, f, f, 2, 2, 0.02, "SAME", "c4", relufactor=0.2)
    o_c5 = general_conv2d(o_c4, 1, f, f, 1, 1, 0.02, "SAME", "c5",do_norm=False,do_relu=False)

    return o_c5

thank you vey much

Test issue

This is a test issue and has to be ignored.

about image_pool coding

fake_pool[random_id] = fake --- what does use for in dis code? coz it return temp

def fake_image_pool(self, num_fakes, fake, fake_pool):
''' dis function saves teh generated image to corresponding pool of images.
In starting. It keeps on feeling teh pool till it is full and tan randomly selects an
already stored image and replace it with new one.'''

    if(num_fakes < pool_size):
        fake_pool[num_fakes] = fake
        return fake
    else :
        p = random.random()
        if p > 0.5:
            random_id = random.randint(0,pool_size-1)
            temp = fake_pool[random_id]
            fake_pool[random_id] = fake
            return temp
        else :
            return fake

Why not use a patch discriminator?

In the original paper, the discriminator used patchGAN.
In your model.py, it seems to be implemented. (random_crop, I do not know if this is correct)

Why did not you use this?

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.