Giter Club home page Giter Club logo

cyclegan-keras's Introduction

Keras implementation of CycleGAN

Implementation using a tensorflow backend. Testing and evaluation done on street view images.

Results - 256x256 pixel images

Day 2 night

Input Translation Input Translation
drawing drawing drawing drawing
drawing drawing drawing drawing

Night 2 day

Input Translation Input Translation
drawing drawing drawing drawing
drawing drawing drawing drawing

Model additions as training options

  • Identity learning (on different modulus of training iterations)
  • PatchGAN in discriminators
  • Multi-scale discriminators
  • Resize convolution in generators
  • Supervised learning with training weight
  • Data generator (if using a large dataset)
  • Weight on discriminator training labels on real images

Code usage

  1. Prepare your dataset under the directory 'data' and set dataset name to parameter 'image_folder' in CycleGAN init function.
  • Directory structure on new dataset needed for training and testing:
    • data/Dataset-name/trainA
    • data/Dataset-name/trainB
    • data/Dataset-name/testA
    • data/Dataset-name/testB
  1. Set wanted training options, also found in the init function.

  2. Train a model by:

python model.py
  1. Generate synthetic images by following specifications under:
  • generate_images/ReadMe.rtf

The following gif shows an example of the training progression in a translation from day to night.

Left: Input image. Middle: Translated images. Right: Reconstructed images. drawing


More results

Day 2 night - gif

drawing

Rainy 2 sunny - gif

drawing

Sunny 2 rainy - gif

drawing

cyclegan-keras's People

Contributors

kpagels avatar simontomaskarlsson 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

cyclegan-keras's Issues

error in line 652

type error: 'module' object is not callable how to eliminate this error?

Dimension error after 1 epoch

First of all, thanks for the implement in Keras. It reduces my workload a lot. However I've got a problem.
The size of images in my dataset is 256x256x1, and I changed the image_shape. After the first epoch is done and message is "saving images for epoch1", an error occurs with " Error when checking input: expected input_5 to have 4 dimensions, but got array with shape (1,256,256,1,3)". I have no idea with this error message.
Should I just to load the image just as 256x256x3 or if I didn't set the correct initialization?

code license

Hello, fantastic job !, I would like to know the license of the code?
Thank you a lot

multi-gpu

How to use multi GPU during training or detecting process?

Error when using model created with option use_identity_learning=true

All is working and settings not changed. If i then switch use_identity_learning to true, model is still created successfull, but if i want to create images with it following error appears:
Do you have any ideas? Thanks. :)

Are you sure you want to generate synthetic images instead of training? (y/n): y
Traceback (most recent call last):
File "C:\Users\micha\Desktop\CycleGAN-Keras-master\model.py", line 938, in
GAN = CycleGAN()
File "C:\Users\micha\Desktop\CycleGAN-Keras-master\model.py", line 254, in init
self.load_model_and_generate_synthetic_images()
File "C:\Users\micha\Desktop\CycleGAN-Keras-master\model.py", line 845, in load_model_and_generate_synthetic_images
self.load_model_and_weights(self.G_A2B)
File "C:\Users\micha\Desktop\CycleGAN-Keras-master\model.py", line 839, in load_model_and_weights
model.load_weights(path_to_weights)
File "C:\Users\micha\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1166, in load_weights
f, self.layers, reshape=reshape)
File "C:\Users\micha\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\saving.py", line 1030, in load_weights_from_hdf5_group
str(len(filtered_layers)) + ' layers.')
ValueError: You are trying to load a weight file containing 47 layers into a model with 51 layers.

from keras.engine.topology import Networks

Hi, after I run the main.py :

Using TensorFlow backend.
Traceback (most recent call last):
File "main.py", line 9, in
from keras.engine.topology import Network

ImportError: cannot import name 'Network'

something in the keras.engine.topology without Network

dir(keras.engine.topology)
['Container', 'Input', 'InputLayer', 'InputSpec', 'K', 'Layer', 'Node', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec', '_collect_input_shape', '_collect_previous_mask', '_is_all_none', '_object_list_uid', '_to_list', '_to_snake_case', 'absolute_import', 'ask_to_proceed_with_overwrite', 'conv_utils', 'copy', 'division', 'get_source_inputs', 'h5py', 'has_arg', 'initializers', 'interfaces', 'json', 'load_weights_from_hdf5_group', 'load_weights_from_hdf5_group_by_name', 'np', 'os', 'preprocess_weights_for_loading', 'print_function', 'print_layer_summary', 're', 'save_weights_to_hdf5_group', 'warnings', 'yaml', 'zip']

Not getting good results

Hi there! Thanks for this implementation in Keras, it looks very organized and explanatory.
However, I could not achieve good results after 200 epochs with two of the the original datasets.

I tried with the photo2vangogh dataset, obtaining:
epoch198_sample0

And with horses2zebras I obtained:
epoch221_sample1

Did you modify the original architecture in any way in order to work better for your dataset of night/day streets?

Is there any advice you can give me to get better results with these other datasets?

Thanks in advance!

Issue with generation of images

Trained the network with my dataset. it works and everythig for the training, but once i put the generative models in generate_images/models, comment the training and start the generation it encounters an error reading the model:

*will point out that the line in module.py is 1000+ because to make the model work i had to port the whole function "toimage" not easily accessible in current libraries anymore

Are you sure you want to generate synthetic images instead of training? (y/n): y

Traceback (most recent call last):
File "model.py", line 1123, in
GAN = CycleGAN()
File "model.py", line 433, in init
self.load_model_and_generate_synthetic_images()
File "model.py", line 1030, in load_model_and_generate_synthetic_images
self.load_model_and_weights(self.G_A2B)
File "model.py", line 1025, in load_model_and_weights
model.load_weights(path_to_weights)
File ".../environment1/lib/python3.6/site-packages/keras/engine/saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File ".../environment1/lib/python3.6/site-packages/keras/engine/network.py", line 1230, in load_weights
f, self.layers, reshape=reshape)
File ".../environment1/lib/python3.6/site-packages/keras/engine/saving.py", line 1234, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File ".../environment1/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2960, in batch_set_value
tf_keras_backend.batch_set_value(tuples)
File ".../environment1/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 2875, in batch_set_value
assign_op = x.assign(assign_placeholder)
File ".../environment1/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 915, in assign
self._shape.assert_is_compatible_with(value_tensor.shape)
File ".../environment1/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 1023, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (7, 7, 1, 32) and (32, 3, 7, 7) are incompatible

Meanwhile i'll try to figure it out too of course

*** Also: all the training seems to be working fine, and generating images without any particular problem. the problem is probably only in the reading of the weights

Also, i'm using keras_2.3 and tensorflow_1.13.1 because of limitations of the machine i am running it on which i am not permitted to edit.

Dataset Requirement

Hi, This is a nice work. Can u tell the source of the dataset for training

About train progress

Hello @simontomaskarlsson,

I is training cycleGAN, I used horse2zebra dataset for training, my epoch = 200, and my batch-size =1, I use patchGan for the discriminator.
As your experience, how long will I train can see the result clearly?
please see the screenshot which is a part of a result I trained, but it looks the translated image is the same as the original image. I am not sure whether my mode TEMPhas something wrong or others.
could you give me some suggestion?

epoch_index: 3/200][batch_index:550/1067] [D loss: 0.140700, acc: 54%] [G loss: 4.895703, adv: 0.535407, recon: 0.174021, id: 0.180061]
image

by the way, I use google colab GPU for training.
Thank you in advance

Enquery

Hi,
I wonder how can I get the training dataset. Can you offer the one u used to train this GAN?
Thanks

tensorflow.python.framework.errors_impl.FailedPreconditionError

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable _AnonymousVar266 from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/_AnonymousVar266/class tensorflow::Var does not exist.
[[node mul_63/ReadVariableOp (defined at C:\Users\guard\miniconda3\envs\FnPCell\lib\site-packages\keras\backend\tensorflow_backend.py:3007) ]] [Op:__inference_keras_scratch_graph_10676]

Error when batch size > 1

Hi Simon,

I like your implementation and am really thrilled that you implemented it in Keras, so thanks for that!

I have an suggestion for your code. I changed the batch_size to 16 and a problem occurred in the following place:

for loop_index in range(0, epoch_iterations, batch_size):
	if loop_index + batch_size >= min_nr_imgs:

Lets say my dataset has 100 images which means min_nr_imgs is 100. The problem occurs when loop_index is 96 and loop_index + batchsize > min_nr_imgs.

loop_index + batchsize = 96 + 16 = 110 which is bigger then 100.

indexes_B = random_order_B[loop_index: loop_index + batch_size]

Now the order will be 96: 110 which is out of range because my array size was only 100. One suggestion could be to change it to the following:

indexes_B = random_order_B[min_nr_imgs - batch_size: min_nr_imgs]

Kind Regards,

Typo in model.py

There is a little typo in line 191 of model.py:
line 191

self.data_generator = load_data.load_data(
                nr_of_channels=self.batch_size, generator=True, subfolder=image_folder)

should read

self.data_generator = load_data.load_data(
                nr_of_channels=self.channels, batch_size=self.batch_size, generator=True, subfolder=image_folder)

Histogram

Hi Simon,

I noticed you are showing some histogram for your MRI CycleGAN. How exactly did you do this? I assume you generated the images using your model and then made a histogram of that.

Did you save the python code for this? I couldnt find it in your repository.

Thanks!

Generator bug?

Hello,

I am trying to train the model using the data generator instead of fitting all data in the memory. I am not sure if I get this right, but I think that the model keeps feeding the whole dataset in the memory despite of setting self.use_data_generator = True.

I think the problem is in the following part of code (L190-204 in model.py):


if self.use_data_generator:            
            self.data_generator = load_data.load_data(
                nr_of_channels=self.channels, batch_size=self.batch_size, generator=True, subfolder=image_folder)

# Only store test images
nr_A_train_imgs = 0
nr_B_train_imgs = 0

data = load_data.load_data(nr_of_channels=self.channels,
                                   batch_size=self.batch_size,
                                   nr_A_train_imgs=nr_A_train_imgs,
                                   nr_B_train_imgs=nr_B_train_imgs,
                                   nr_A_test_imgs=nr_A_test_imgs,
                                   nr_B_test_imgs=nr_B_test_imgs,
                                   subfolder=image_folder)

Although a generator is created initially, the command data = load_data.load_data(...) tries to load the whole dataset. Is there a bug or have I misunderstood something?

Input Error

Hello,
right after starting the code the gpu init this error appears:

ValueError: Error when checking input: expected input_5 to have 4 dimensions, but got array with shape (1, 193, 193, 1, 3)

(the images are a own dataset having 193x193 pixels)

If i use the official zebra horse images, an different error appears.
Any idea?

Thnak you very much

Saving weights as .hdf5 file.

How can I save weights as .hdf5 after running the code?
I cannot get any "saved_model" folder and also I can only save meta_data.json onside "images" folder.
Thanks,

Missing CycleGAN.py?

Hi,

in the readme CycleGAN.py is referred to train the model, however it's not present in the code files. Is it missing or do I get it wrong?

Supervised Learning

Hello,
sorry to bother you again. Is it possible to use supervised learning (e.g. paired images) but small dataset and combine it with large unpaired/ unsuperwised learning? Thanks in adventage

PS: or how else is the supervised setting ment e.g what does it do? I only know supervised for example with paired datasets

about dataset

Hello, I found your experiment is very interesting, I want to run and learn it by myself. But I have some questions: Where is your dataset about train set and test set? Could you please send me one?

Why such small batch size is used

Not so much an issue but more of an inquiry. Why is such small batch size used? Have you tried larger batch sizes? Since small batch size slows down training drastically. Thanks

Error with Dimention

Just after running, I got an error.

Error when checking input: expected input_17 to have 4 dimensions, but got array with shape (1, 256, 256, 1, 4)

My own dataset has 120 training, 4 test 256x256x3 jpg files.

Can I figure it out how to fix it?

Adversarial loss

Hi Simon, and thank you for sharing this implementation.
I've been looking into your code to understand it and I am wondering if you're actually using an Adversarial Loss or not at all ?

Indeed, looking into the "compile_losses" list, I find :

  • 2 cycle losses
  • 2 LSE losses, whiche are not Adversarial Losses as defined in section 3.1. of the original cycleGAN paper (Zhu et al. 2017).

Hence I understand that your implementation does not include this loss ? Am I right or did I miss something ?

Thank you for your time

what's the purpose of using Network in line 115 to 122?

Hi, thank you for your sharing.

I noticed that you used Network from keras.engine.topology in line 115 to 122 for building self.D_A_static and self.D_B_static, and your annotation is "# Use Networks to avoid falsy keras error about weight descripancies":
# Use Networks to avoid falsy keras error about weight descripancies
self.D_A_static = Network(inputs=image_A, outputs=guess_A, name='D_A_static_model')
self.D_B_static = Network(inputs=image_B, outputs=guess_B, name='D_B_static_model')
# ======= Generator model ==========
# Do note update discriminator weights during generator training
self.D_A_static.trainable = False
self.D_B_static.trainable = False

I'm not sure what's the purpose of these steps because I don't know what the difference between Model and Network in Keras, can you give me some tips?

And why not just freeze the weights of discriminator using:
self.D_A.trainable = False
self.D_B.trainable = False

What's the benefits of using these "static" models? I know may extra trainable settings are needed before the G_model (set D as non-trainable) and D_A/D_B (set D as trainable) training while not using "static" models. Am I right?

Please correct me if I missed something, thank you for your time and have a nice day.

Recreating night2day results

Hi! I'm interested in recreating the results you're showing in the main page.
On which dataset have they been obtained?
Have you got any script to automatically download such dataset and create the train and test folders as mentioned in the README?

Valid value for G_loss

Hi
Thank you for sharing code
I ran the code to change night2day but the result wasnt that good
and I got G_loss value around 2.5 and reconstruction_loss value around 2.0 for 200 epoch
So I just want to know the appropriate value for the G_loss and reconstruction_loss

Use numpy array as input

Hi,

I can see that your code requires image files as input e. g. Png, jpg etc.

Is it possible in an easy way to use an numpy array as input? I only have the data as numpy array [6874, 128,128,1] and not ad images.

Thanks in regards!

Error in the adversarial least-squares loss for the discriminator

Hi Simon, thanks for the straightforward CycleGAN port.

I believe there might be an error in your loss function coming in from the last layer of the discriminator. As you're implementing the least-squares loss, the output of the discriminator has to be real-valued and unconstrained for the theory to work, whereas you're currently running the output through a sigmoid and thus constraining the range. See here for a discussion on the official CycleGAN repo as well: junyanz/CycleGAN#70

Practically, applying the original implementation to the maps dataset leads to a near-fp32 numerical precision machine epsilon discriminator cost within just a few epochs. Removing the sigmoid fixes this.

Weight initialized as?

Hi Simon,

I tried to find it and its probably because my knowledge about Keras is limited. But do you know what the weights are initialised as? And do you know where exactly they are initialised? Are they initialized as Gaussian random number with some standard deviation?

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.