Cats and Dogs Machine learning

Can anyone help me with this problem, I have tried to rename all the file but that didn’t help, the function worked well with the train and val files tho

Welcome to the forums, @hoang150703.

It’s hard to tell from a screenshot what all is happening with your notebook; it would be easier to get assistance if you share a link to the notebook so that we have more information. I can’t tell from the error and code that is in the screenshot completely, but you may need a classes=['test'] parameter in the function that is returning test_data_gen since there is only one directory (class) and not two like with the training and validation sets. I know that not having that parameter (and it appears to be missing) causes an error, but I don’t remember the error.

If that doesn’t help, post a link to the notebook and maybe someone can be more helpful.

Good luck.

1 Like

Sorry for the late reply, I was having final exams so I can’t check this.
I believe the error occurred due to the files name of the test are numbers so i tried to loop through the file and change their name but it didn’t work. I don’t know if it is because anything else. There is also another error but I don’t know how to deal with it too.
And this is the link to the notebook: here

So in the image generator cell where you have the error, there are two things. The easy one is setting the rescale parameter. For images like this, it should be 1.0/255.0 to normalize the color data that goes from 0 to 255 on each channel.

Second, the error is coming from what the file loaders expect and how the directory is actually structured. The directories look like

cats_and_dogs
  train
    cats
    dogs
  validation
    cats
    dogs
  test

The image loaders expect that the directory that you specify has subdirectories of images for each class of image. So, the train and validation directories are good since both have subdirectories of cats and dogs. The problem is the test directory with no subdirectories. There are two solutions.

One, use the example a few cells down in the image augmentation cell for train_data_gen and pass it the PATH directory and the classes = ['test'] parameter like I mentioned before.

The other is to use tf.keras.preprocessing.image_dataset_from_directory() like you currently have with the PATH/test directory and class_names = ['test'], but you will have to move all the images in test to test/test (I think; I didn’t try this but it should work but I’m not sure you can move files from inside a notebook). The second method will fail with the directory set to PATH and class_names = ['test'] because the directories and class names do not match. I would use the first method.

Good luck.

2 Likes

Thanks to you, I realized that I was doing the wrong method since the beginning.
So I did it again and it worked perfectly but the classes actually need to be classes=[''] for it to work (I believe) since it said that the test has no subdirectories and the images are not labeled.

Thank you very much for helping me.