akarshzingade / image-similarity-deep-ranking

369 stars 103 forks source link

Index out of bound when running model #5

Closed victorcadena closed 6 years ago

victorcadena commented 6 years ago

To give some sort of pre-setup, my python version is 3.6, I and do not have GPUs to run on Tensor Flow.

My folder structure is this one

screen shot 2018-05-03 at 11 44 44 am

My images are structured under the folder "shoes" like this with similar images under every subfolder to form the triplets using the script

screen shot 2018-05-03 at 10 58 39 am

I ran this command to form the triplets file

screen shot 2018-05-03 at 11 40 10 am

Once I have the file, I made some changes to configure the deepranking.py

screen shot 2018-05-03 at 11 36 55 am

And I removed this line because there is no file

screen shot 2018-05-03 at 11 36 39 am

Once I made this, I ran

python deepRanking.py

And at the end, it shows this error Found 28 images belonging to 7 classes. Epoch 1/25 1/629 [..............................] - ETA: 8:39:04 - loss: 0.9963Traceback (most recent call last): File "deepRanking.py", line 152, in <module> epochs=train_epocs File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 2194, in fit_generator generator_output = next(output_generator) File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 793, in get six.reraise(value.__class__, value, value.__traceback__) File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 658, in _data_generator_task generator_output = next(self._generator) File "/Users/victorcadena/Documents/similarity/image-similarity-deep-ranking-master/ImageDataGeneratorCustom.py", line 721, in __next__ return self.next(*args, **kwargs) File "/Users/victorcadena/Documents/similarity/image-similarity-deep-ranking-master/ImageDataGeneratorCustom.py", line 1049, in next for i, label in enumerate(self.classes[index_array]): IndexError: index 84 is out of bounds for axis 1 with size 84

The line with the error (1049) is this one:

screen shot 2018-05-03 at 12 03 09 pm

Could you give me some guidance to solve this problem? What am I doing wrong?

AmiraAyadi commented 6 years ago

Hi! Try to print the classes in the ImageDataGeneratorCustom file by adding "print(self.classes)" in the DirectoryIterator class, just before the next function. Maybe it will help you identify then fix the problem

lorenzoferrante commented 6 years ago

Can someone help please? I get the same error. Thanks

akarshzingade commented 6 years ago

Hi All! I think I figured out the issue. I will soon release an updated script with a fix for this. For now, as a hacky solution, can you make sure the number of triplets in your triplets.txt file is a multiple of the batch_size that you have entered in the script?

Ex: For now, the deepranking.py script uses a batch size of 8*3 images per batch. That's 24 images. If your triplets.txt has 44 triplets, just copy paste 4 triplets to make it 48 triplets (this is a multiple of 24).

Please let me know if this hacky solution works. If not, please share the error along with your batch_size number, number of images in your directory and the number of triplets in your triplets.txt file.

victorcadena commented 6 years ago

Hi!, Thanks for your response!... I keep having the same issue, here are the settings.

  1. The triplets file has 1680 lines.
  2. I have 7 classes and 4 images on each class.
  3. The command I run to generate the file is this one MacBook-Pro-2:image-similarity-deep-ranking-master victorcadena$ python tripletSampler.py --input_directory shoes --output_directory triplet_folder --num_pos_images 3 --num_neg_images 20 Input Directory: shoes Output Directory: triplet_folder Number of Positive image per Query image: 3 Number of Negative image per Query image: 20
  4. The resulting file is in the triplet_folder and the end looks like this
screen shot 2018-05-13 at 10 46 14 am
  1. The classes in the folder shoes all have 4 images in format jpg|jpeg
screen shot 2018-05-13 at 10 42 53 am

1680 mod 24 = 0 hence 1680 mod 3 = 0 too

When I run python deepRanking.py the result is

python deepRanking.py /Users/victorcadena/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype fromfloattonp.floatingis deprecated. In future, it will be treated asnp.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters Using TensorFlow backend. 2018-05-13 10:51:17.262032: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA input_1 (None, None, None, 3) block1_conv1 (None, None, None, 64) block1_conv2 (None, None, None, 64) block1_pool (None, None, None, 64) block2_conv1 (None, None, None, 128) block2_conv2 (None, None, None, 128) block2_pool (None, None, None, 128) block3_conv1 (None, None, None, 256) block3_conv2 (None, None, None, 256) block3_conv3 (None, None, None, 256) block3_pool (None, None, None, 256) block4_conv1 (None, None, None, 512) block4_conv2 (None, None, None, 512) block4_conv3 (None, None, None, 512) block4_pool (None, None, None, 512) block5_conv1 (None, None, None, 512) block5_conv2 (None, None, None, 512) block5_conv3 (None, None, None, 512) block5_pool (None, None, None, 512) input_2 (None, 224, 224, 3) input_3 (None, 224, 224, 3) global_average_pooling2d_1 (None, 512) conv2d_1 (None, 14, 14, 96) conv2d_2 (None, 7, 7, 96) dense_1 (None, 4096) max_pooling2d_1 (None, 4, 4, 96) max_pooling2d_2 (None, 4, 4, 96) dropout_1 (None, 4096) flatten_1 (None, 1536) flatten_2 (None, 1536) dense_2 (None, 4096) lambda_2 (None, 1536) lambda_3 (None, 1536) dropout_2 (None, 4096) concatenate_1 (None, 3072) lambda_1 (None, 4096) concatenate_2 (None, 7168) dense_3 (None, 4096) lambda_4 (None, 4096) Found 28 images belonging to 7 classes. Epoch 1/25 1/629 [..............................] - ETA: 7:29:42 - loss: 0.9868Traceback (most recent call last): File "deepRanking.py", line 152, in <module> epochs=train_epocs File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 2194, in fit_generator generator_output = next(output_generator) File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 793, in get six.reraise(value.__class__, value, value.__traceback__) File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/Users/victorcadena/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 658, in _data_generator_task generator_output = next(self._generator) File "/Users/victorcadena/Documents/similarity/image-similarity-deep-ranking-master/ImageDataGeneratorCustom.py", line 721, in __next__ return self.next(*args, **kwargs) File "/Users/victorcadena/Documents/similarity/image-similarity-deep-ranking-master/ImageDataGeneratorCustom.py", line 1051, in next for i, label in enumerate(self.classes[index_array]): IndexError: index 84 is out of bounds for axis 1 with size 84

Thanks a lot for your help!

akarshzingade commented 6 years ago

Hey, victorcadena. Thank you for the detailed description. I am able to replicate the issue. Let me work on it and get back to you :)

AmiraAyadi commented 6 years ago

Hi! Look at the ImageDataGeneratorCustom script, line 994 : self.classes = np.zeros((self.samples*3,), dtype='int32').

Your numpy array do not have the right size for that much triplets. I had this error, and I just did : self.classes = np.zeros((self.samples3 9,), dtype='int32').

victorcadena commented 6 years ago

Hi! I will look at that (but should the initialization be in terms of (triplets/samples) instead of just some fixed number)? If so, is the code storing the number of triplets somewhere?

Thanks a lot!

akarshzingade commented 6 years ago

Hey, guys. I think I have fixed this issue. I have committed the code into the dev branch. You can find the file here: https://github.com/akarshzingade/image-similarity-deep-ranking/blob/dev/ImageDataGeneratorCustom.py

Please try it out. Let me know if it works for you so that I can merge it into the Master branch.

lorenzoferrante commented 6 years ago

Yes, now it seems it doesn't have any problem! What was the problem? Thanks for the fix!

akarshzingade commented 6 years ago

The "for i, label in enumerate(self.classes[index_array])" is supposed to create the labels for each batch. The out of bound error occurs when the size of self.classes is less than batch_size or when the index_array outputs a value greater than the batch_size. Fixed that.

alteest commented 6 years ago

Yes. Now it works!