rykov8 / ssd_keras

Port of Single Shot MultiBox Detector to Keras
MIT License
1.1k stars 553 forks source link

Training on Keras 2 #84

Closed jogundas closed 7 years ago

jogundas commented 7 years ago

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 commented 7 years ago

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

jogundas commented 7 years ago

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)