Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Training on Keras 2 #84

Closed
jogundas opened this issue Jun 8, 2017 · 2 comments
Closed

Training on Keras 2 #84

jogundas opened this issue Jun 8, 2017 · 2 comments

Comments

@jogundas
Copy link

jogundas commented Jun 8, 2017

I am trying to train on Keras 2. However, something goes wrong with the generator.

Detection works after implementing minor changes, as per here.

However, there is some problem with generation for training when executing SSD_training.ipynb. To just try if it works, I am using the provided annotation file gt_pascal.pkl and copying some image over:

from shutil import copyfile
for someKey in keys:
    copyfile('/tmp/airplanesC.png', '../../frames/'+someKey)

When running

history = model.fit_generator(gen.generate(True), gen.train_batches,
                              nb_epoch, verbose=1,
                              callbacks=callbacks,
                              validation_data=gen.generate(False),
                              nb_val_samples=gen.val_batches,
                              nb_worker=1)

I get a shape mismatch error,

/home/user1/tensorflow/lib/python2.7/site-packages/ipykernel_launcher.py:7: UserWarning: Update your `fit_generator` call to the Keras 2 API: `fit_generator(<generator..., 4651, 30, verbose=1, workers=1, validation_data=<generator..., callbacks=[<keras.ca..., validation_steps=1163)`
  import sys
/home/user1/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gradients_impl.py:93: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/user1/tensorflow/local/lib/python2.7/site-packages/keras/engine/training.py", line 612, in data_generator_task
    generator_output = next(self._generator)
  File "<ipython-input-5-0501eef3a2f4>", line 152, in generate
    img = jitter(img)
  File "<ipython-input-5-0501eef3a2f4>", line 57, in contrast
    gs = self.grayscale(rgb).mean() * np.ones_like(rgb)
  File "<ipython-input-5-0501eef3a2f4>", line 41, in grayscale
    return rgb.dot([0.299, 0.587, 0.114])
ValueError: shapes (300,300,4) and (3,) not aligned: 4 (dim 2) != 3 (dim 0)

Any ideas?

@jogundas
Copy link
Author

jogundas commented Jun 9, 2017

Interestingly, the same error pops up when using the recommended versions of keras, tf, and cv2.

@jogundas
Copy link
Author

jogundas commented Jun 9, 2017

Apparently there was an issue with the format of my image: closing the issue.

In particular, my images were grayscale. I've solved the issue by adding the following hack in the generate function of the Generator class right after the line img = imread(img_path).astype('float32') in the training notebook.

try:
    # The input image is not grayscale but has a weird number of channels
    if img.shape[2] != 3:
        print "Problem with input image "+key+" shape - investigate!"
except:
    # The input image has only 1 channel (grayscale), copy it over to 3 channels
    img.resize((img.shape[0], img.shape[1], 1))
    img = np.repeat(img, 3, 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant