DeepWeeds icon indicating copy to clipboard operation
DeepWeeds copied to clipboard

Can u Help Me Type Error

Open AhmetBAGBARS opened this issue 6 years ago • 9 comments

sikinti Good luck with. I have encountered such an error while instructing the model according to your instructions. I get a Type error in "train_data_generator" in "deepweeds.py". How can I solve this error? If you help. I'm glad. Thank you good work.

AhmetBAGBARS avatar Mar 06 '19 10:03 AhmetBAGBARS

Can you solve this error? I get a commin error. If you help. I'm glad. Thank you good work.

qiqi17 avatar Jun 03 '19 16:06 qiqi17

Hello, did you get any solution for that?

linaashaji avatar Aug 20 '19 12:08 linaashaji

Hi all. Sorry for the (very) late response. Unfortunately, I am unable to replicate this error. My first suggestion is to ensure you have the identical version of Keras and Pandas, which successful use of the "flow_from_dataframe" function may depend on.

AlexOlsen avatar Oct 02 '19 13:10 AlexOlsen

Hi Alex, Thanks for sharing your work. I have created a new environmet with identical versions but I still get the same error. Any other suggestion for us?

Atached you can find the error and version of Keras and Panda:)

Capture1 Capture2

NegarTavakoli avatar Oct 07 '19 01:10 NegarTavakoli

Hey guys, i had the same problems and it was extremely strange to find a workaround. The actual reason for the error message I couldn't find, there must have been something changed in the newer versions of keras_preprocessing and thus in dataframe_iterator.py, but I can't say what.

The following changes work for me: To the existing CLASSES of type list, I added a CLASSES_str also of type list. CLASSES_str = ['0', '1', '2', '3', '4', '5', '6', '7', '8']

Then I read the dataframes for train, val and test with dtype=str train_dataframe = pd.read_csv(train_label_file,dtype=str) val_dataframe = pd.read_csv(val_label_file,dtype=str) test_dataframe = pd.read_csv(test_label_file,dtype=str)

This prevents the error message: "TypeError: If class_mode="categorical", y_col="label" column values must be type string, list or tuple." but leads to a new error: "Found 0 validated image filenames belonging to 9 classes."

I don't know why, all the tutorials I could find suggested that "train_dataframe" was correct, some recommended using absolute filenames. That's why I did: I set the IMG_DIRECTORY to an absolute path, eg.

IMG_DIRECTORY = "D:/DeepWeeds/images/"

and then

train_dataframe['Filenamefull'] = IMG_DIRECTORY+train_dataframe['Filename'] val_dataframe['Filenamefull'] = IMG_DIRECTORY+val_dataframe['Filename'] test_dataframe['Filenamefull'] = IMG_DIRECTORY+test_dataframe['Filename']

An finally I called train_data_generator.flow_from_dataframe as followed: train_data_generator = train_data_generator.flow_from_dataframe( train_dataframe, IMG_DIRECTORY, x_col='Filenamefull', y_col='Label', target_size=RAW_IMG_SIZE, batch_size=BATCH_SIZE, has_ext=True, classes=CLASSES_str, class_mode='categorical')

Here I put the predefined list of strings into classes=CLASSES_str. That works.

Arne-van-Au avatar Oct 14 '19 11:10 Arne-van-Au

Thanks @Arne-van-Au. Does this fix work for you, @NegarTavakoli?

AlexOlsen avatar Oct 29 '19 05:10 AlexOlsen

Thanks Alex for pushing. I hope my raw kind of coding is not too terrible. :-)

Arne-van-Au avatar Oct 30 '19 10:10 Arne-van-Au

Same issue encountered, I cannot find the changelog of Keras to see when they changed this behaviour, but indeed they do not accept the data in the form presented in this repo anymore:

if class_mode is "categorical" (default value) it must include the y_col column with the class/es of each image. Values in column can be string/list/tuple if a single class

source: https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator#flow_from_dataframe

What I did is similar to what Arne proposes, however I decided to use the full category names, instead of stringified indices:

train_dataframe = pd.read_csv(train_label_file)
val_dataframe = pd.read_csv(val_label_file)
test_dataframe = pd.read_csv(test_label_file)
train_df.Label = train_df.Label.apply(lambda x: CLASS_NAMES[x])
val_df.Label = val_df.Label.apply(lambda x: CLASS_NAMES[x])
test_df.Label = test_df.Label.apply(lambda x: CLASS_NAMES[x])

later, when calling flow_from_dataframe(), you need to change the classes parameter to CLASS_NAMES AND you need to remove the has_ext parameter, because it has been deprecated as well:

test_data_generator = test_data_generator.flow_from_dataframe(
    test_dataframe,
    IMG_DIRECTORY,
    x_col="Filename",
    y_col="Label",
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    shuffle=False,
    classes=CLASS_NAMES,
    class_mode='categorical')

note you need to change this 3 times for each dataframe.

Finally, if needed, you can call training_generator.class_indices if you need the translation dictionary from index to label name. Take care that the order of the labels has been changed, as they are sorted alphabetically. If the order is important, I suggest sticking with Arne's solution above.

galadash avatar Nov 26 '20 10:11 galadash

unzip these zips is the way to solve the problem.

cq2019git avatar Dec 19 '20 06:12 cq2019git