0
$\begingroup$

Losely following this tutorial, I'm trying to apply Keras' ImageDataGenerator preprocessing on my custom object dataset. Here is the code:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import matplotlib.pyplot as plt
%matplotlib inline

os.chdir('/home/pc3/deep_object/')

mobile = tf.keras.applications.mobilenet.MobileNet()


cwd = os.getcwd()

# Print the current working directory
print("Current working directory to generate: {0}".format(cwd))



train_path = 'data/Object-samples/train'
valid_path = 'data/Object-samples/valid'
test_path =  'data/Object-samples/test'

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet.preprocess_input).flow_from_directory(
    directory=r'data/Object-samples/train', target_size=(224,224), batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet.preprocess_input).flow_from_directory(
    directory=valid_path, target_size=(224,224), batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet.preprocess_input).flow_from_directory(
    directory=test_path, target_size=(224,224), batch_size=10, shuffle=False)

However I get 0 pictures, despite the fact that the folders are already filled with pictures.

Current working directory to generate: /home/pc3/deep_object
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.

my directory structure is like this:

~/deep_object$ tree -L 2
.
 
├── data
│   ├── Object-samples
│   ├── dogs-vs-cats
│   └── MobileNet-samples
├── deeplizard_tutorial_side_effects_example.ipynb
├── Mobilenet-finetunning-my-dataset.ipynb

So I'm wondering what is wrong here?

$\endgroup$
2
  • 1
    $\begingroup$ Do you have directories for every single class that you have? For eg: inside /Object-samples/ you'd have two sub-directories '/Object-samples/0/' and '/Object-samples/1/' which would contain images belonging to that class. $\endgroup$ Jun 11, 2022 at 11:51
  • 1
    $\begingroup$ No I did not, and that was the problem. Thanks for the tip. Please answer and I'll accept. $\endgroup$
    – blnks
    Jun 12, 2022 at 4:00

1 Answer 1

1
$\begingroup$

Tensorflow expects sub-directories for every single class that you have inside the primary directory. For example: inside /Object-samples/ you'd have two sub-directories /Object-samples/0/ and /Object-samples/1/ which would contain images belonging to that class.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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