jhayes14 / GAN

Generate images via a Generative Adversarial Network (GAN)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to solve this error.

firestonelib opened this issue Β· comments

Dear sir,
when I run : python train.py --path /mnt/data1/GAN/heart --batch_size 8 --epochs 8 --TYPE train
it return error: I wanna why this happened.
Traceback (most recent call last):
File "train.py", line 196, in
train(path = args.path, batch_size = args.batch_size, EPOCHS = args.epochs)
File "train.py", line 64, in train
discriminator_on_generator = model.generator_containing_discriminator(generator, discriminator)
File "/mnt/data1/daniel/codes/GAN/GAN/model.py", line 102, in generator_containing_discriminator
model.add(discriminator)
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/models.py", line 463, in add
output_tensor = layer(self.outputs[0])
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/engine/topology.py", line 578, in call
output = self.call(inputs, **kwargs)
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/models.py", line 520, in call
return self.model.call(inputs, mask)
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/engine/topology.py", line 1989, in call
output_tensors, _, _ = self.run_internal_graph(inputs, masks)
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/engine/topology.py", line 2140, in run_internal_graph
output_tensors = _to_list(layer.call(computed_tensor, **kwargs))
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/layers/convolutional.py", line 164, in call
dilation_rate=self.dilation_rate)
File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2893, in conv2d
data_format='NHWC')
File "/mnt/data1/daniel/tensorflow/_python_build/tensorflow/python/ops/nn_ops.py", line 650, in convolution
num_spatial_dims]))
ValueError: number of input channels does not match corresponding dimension of filter, 3 != 64

I got this error too

Probably a channel ordering mismatch in your keras backend. https://keras.io/backend/

I change my backend into theano, but I got another error message
Traceback (most recent call last):
File "train.py", line 196, in
train(path = args.path, batch_size = args.batch_size, EPOCHS = args.epochs)
File "train.py", line 64, in train
discriminator_on_generator = model.generator_containing_discriminator(generator, discriminator)
File "/home/w007878/workspace/GAN/model.py", line 102, in generator_containing_discriminator
model.add(discriminator)
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 332, in add
output_tensor = layer(self.outputs[0])
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 572, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 635, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 166, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 386, in call
return self.model.call(x, mask)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2247, in call
output_tensors, output_masks, output_shapes = self.run_internal_graph(inputs, masks)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2420, in run_internal_graph
shapes = to_list(layer.get_output_shape_for(computed_tensors[0]._keras_shape))
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 821, in get_output_shape_for
assert input_shape[-1] and input_shape[-1] == self.input_dim
AssertionError

Alright, I have fixed this one.
Here is my keras.json

{
    "image_dim_ordering": "th", 
    "epsilon": 1e-07, 
    "floatx": "float32", 
    "backend": "theano"
}

It may help

Hi, thank you @w007878 ,but where is the keras.json?

@Designbook1 It is under your home directory ~/.keras
If you cannot find it, just create one.
To change the backend, you must have theano installed.
I hope it can work to you πŸ˜„

@w007878 when I fixed the keras.json, it errors:

Traceback (most recent call last):
  File "train.py", line 6, in <module>
    import model
  File "/mnt/data1/daniel/codes/GAN/GAN/model.py", line 1, in <module>
    from keras.models import Sequential, Model
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/__init__.py", line 3, in <module>
    from . import activations
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/activations.py", line 3, in <module>
    from . import backend as K
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/backend/__init__.py", line 70, in <module>
    from .theano_backend import *
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 3, in <module>
    import theano
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/__init__.py", line 66, in <module>
    from theano.compile import (
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/compile/__init__.py", line 10, in <module>
    from theano.compile.function_module import *
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/compile/function_module.py", line 21, in <module>
    import theano.compile.mode
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/compile/mode.py", line 10, in <module>
    import theano.gof.vm
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/gof/vm.py", line 662, in <module>
    from . import lazylinker_c
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/gof/lazylinker_c.py", line 127, in <module>
    preargs=args)
  File "/mnt/data1/daniel/Python-2.7.13/build/lib/python2.7/site-packages/theano/gof/cmodule.py", line 2316, in compile_str
    (status, compile_stderr.replace('\n', '. ')))
Exception: Compilation failed (return status=1): /usr/bin/ld: /mnt/data1/daniel/Python-2.7.13/build/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC. /mnt/data1/daniel/Python-2.7.13/build/lib/libpython2.7.a: could not read symbols: wrong value. collect2: Error:ld return 1. 

@Designbook1 I think there may be something wrong with your theano installation, how about try to reinstall it ?

I keep getting an error too
I type in : python2 train.py --path /test --TYPE 'train' --batch_size 10 --epochs 10

and get:
Using Theano backend.
Loading paths..
Got paths..
Number of batches 0
Batch size is 10

Epoch 0

Traceback (most recent call last):
File "train.py", line 196, in
train(path = args.path, batch_size = args.batch_size, EPOCHS = args.epochs)
File "train.py", line 153, in train
for i, img in enumerate(generated_images[:5]):
UnboundLocalError: local variable 'generated_images' referenced before assignment

Am I doing something wrong or do I have wrong libraries installed?

The number of batches in your code is zero. This means lines 96-152 won't be executed and so generated_images won't be assigned resulting in the error. How many images are in your path?

I have 11 .jpg images in my path now

tree view over my directory
β”œβ”€β”€ [ 3353] 7.jpg
β”œβ”€β”€ [ 2642] Epoch_13_example.jpg
β”œβ”€β”€ [ 4697] README.md
β”œβ”€β”€ [ 5458] TEST.jpg
β”œβ”€β”€ [ 4191] model.py
β”œβ”€β”€ [ 4499] model.pyc
β”œβ”€β”€ [ 5062] model_tests.py
β”œβ”€β”€ [ 476] test
β”‚   β”œβ”€β”€ [ 3389] boxy_stripes2.jpg
β”‚   β”œβ”€β”€ [ 3089] boxy_stripes2_120.jpg
β”‚   β”œβ”€β”€ [ 3582] boxy_stripes2_150.jpg
β”‚   β”œβ”€β”€ [ 3402] boxy_stripes2_180.jpg
β”‚   β”œβ”€β”€ [ 3071] boxy_stripes2_210.jpg
β”‚   β”œβ”€β”€ [ 3601] boxy_stripes2_240.jpg
β”‚   β”œβ”€β”€ [ 3435] boxy_stripes2_270.jpg
β”‚   β”œβ”€β”€ [ 3065] boxy_stripes2_30.jpg
β”‚   β”œβ”€β”€ [ 3101] boxy_stripes2_300.jpg
β”‚   β”œβ”€β”€ [ 3625] boxy_stripes2_60.jpg
β”‚   └── [ 3379] boxy_stripes2_90.jpg
└── [ 7291] train.py

Can you confirm the images are being loaded? Line 52

Yes now they seem to be loading (changed) my path but then I get another error.

python2 train.py --path test --TYPE 'train' --batch_size 1 --epochs 2
Using Theano backend.
Loading paths..
Got paths..
printing paths
[[[[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]]


 [[[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]]


 [[[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]]


 ...,
 [[[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]]


 [[[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]]


 [[[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]

  [[ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   ...,
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]
   [ 1.  1.  1. ...,  1.  1.  1.]]]]
end path printing
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:48: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (5, 5), padding="same", strides=(2, 2), input_shape=(3, 64, 64...)`
  model.add(Convolution2D(128, 5, 5, subsample=(2, 2), input_shape=(3, 64, 64), border_mode = 'same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:51: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (5, 5), padding="same", strides=(2, 2))`
  model.add(Convolution2D(256, 5, 5, subsample=(2, 2), border_mode = 'same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:54: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (5, 5), padding="same", strides=(2, 2))`
  model.add(Convolution2D(512, 5, 5, subsample=(2, 2), border_mode = 'same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:57: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (5, 5), padding="same", strides=(2, 2))`
  model.add(Convolution2D(1024, 5, 5, subsample=(2, 2), border_mode = 'same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:61: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=1)`
  model.add(Dense(output_dim=1))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:25: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=16384, input_dim=100)`
  model.add(Dense(input_dim=inputdim, output_dim=1024*xdim*ydim))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:30: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (5, 5), padding="same")`
  model.add(Convolution2D(512, 5, 5, border_mode='same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:34: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (5, 5), padding="same")`
  model.add(Convolution2D(256, 5, 5, border_mode='same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:38: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (5, 5), padding="same")`
  model.add(Convolution2D(128, 5, 5, border_mode='same'))
/Users/einar/Desktop/GDA_Y3/ResearchLab/7/GAN/model.py:42: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(3, (5, 5), padding="same")`
  model.add(Convolution2D(3, 5, 5, border_mode='same'))
Number of batches 11
Batch size is 1

Epoch 0

Epoch 0 Batch 0
Traceback (most recent call last):
  File "train.py", line 198, in <module>
    train(path = args.path, batch_size = args.batch_size, EPOCHS = args.epochs)
  File "train.py", line 109, in train
    Xd = np.concatenate((image_batch, generated_images))
ValueError: all the input array dimensions except for the concatenation axis must match exactly

You don't need to use Theano backend for it. Here is my keras.json:

{
    "floatx": "float32",
    "epsilon": 1e-07,
    "backend": "tensorflow",
    "image_data_format": "channels_first"
}

The only difference between original Keras configuration is image data format convention: by default "image_data_format" is "channels_last".

@einarkurbitur did you find the solution to your problem?
"ValueError: all the input array dimensions except for the concatenation axis must match exactly"

@innarid
No sorry I just gave up eventually if I remember correctly.

@einarkurbitur thanks all the same)

@sevazhidkov solution worked for me
cd ~/.keras/
sudo nano keras.json
{
"backend": "tensorflow",
"epsilon": 1e-07,
"floatx": "float32",
"image_data_format": "channels_first"
}