2
$\begingroup$

I am doing image classification, and until now I have built my network and evaluated it. The only thing that remains to do is to do the submission, so I have a blind test set which contains images with no labels, and using the model I have created, I have to make predictions on this blind test set.

I have 4 classes: HAZE, RAINY, SUNNY, SNOWY. My code is the following:

blind_testSet = '/content/drive/My Drive/Colab Notebooks/submission'

test_datagen_blind = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rescale = 1. / 255
   )


test_generator_blind = test_datagen.flow_from_directory(
    directory=blind_testSet,
    target_size=(256, 256),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=False
)

preds =transfer_model.predict_generator(test_generator_blind,verbose=1,steps=val_steps)


import pandas as pd
import numpy as np

predicted_class_indices=np.argmax(preds,axis=1)
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
print(labels)
predictions = [labels[k] for k in predicted_class_indices]
filenames=test_generator_blind.filenames
results=pd.DataFrame({"image ID":filenames,
                  "Predictions":preds})
results.to_csv("submission.csv",index=False)

the problem is that I get the error:

    Exception                                 Traceback (most recent call last)
<ipython-input-22-37b4cd72c225> in <module>()
      9 filenames=test_generator_blind.filenames
     10 results=pd.DataFrame({"image ID":filenames,
---> 11                       "Predictions":preds})
     12 results.to_csv("submission.csv",index=False)

4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in 
sanitize_array(data, index, dtype, copy, raise_cast_failure)
    727     elif subarr.ndim > 1:
    728         if isinstance(data, np.ndarray):
--> 729             raise Exception("Data must be 1-dimensional")
    730         else:
    731             subarr = com.asarray_tuplesafe(data, dtype=dtype)

Exception: Data must be 1-dimensional

Can someone help me? Thanks.

[EDIT]I have also tried to to :

    preds =transfer_model.predict_generator(test_generator_blind,verbose=1,steps=val_steps).reshape(-1,1)

but it does not work and I get the same error.

$\endgroup$
1
  • 1
    $\begingroup$ For the record your last few questions are about programming, you could ask this kind of question on stackoverflow.com. Also if you are interested in data science it looks like you could spend a bit of time improving your programming skills, it will save you a lot of time in the future. $\endgroup$ – Erwan Dec 14 '19 at 13:00
4
$\begingroup$

Python tells you that the data you give for the column "predictions" is not 1-dimensional (i.e. it's not a flat list). And indeed preds is not a 1-dimensional array, what you want to give is the corresponding labels that you collected in predictions.

So Instead of this:

results=pd.DataFrame({"image ID":filenames,
                  "Predictions":preds})

you probably want:

results=pd.DataFrame({"image ID":filenames,
                  "Predictions":predictions})
$\endgroup$
2
  • $\begingroup$ Thanks for answering. I changed the code as you suggested, but I get the following error : ValueError: arrays must all be same length $\endgroup$ – J.D. Dec 14 '19 at 13:01
  • 4
    $\begingroup$ you need to print your variables in order to understand why. Welcome to debugging ;) $\endgroup$ – Erwan Dec 14 '19 at 13:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.