1
votes

im beginner on Machine learning and currently trying to apply VGG net for my neural network

Im facing this kind of error which is

listdir: path should be string, bytes, os.PathLike or None, not ImageDataGenerator

Im currently use Jupyter notebook as editor and here is my code that i faced error

from tensorflow.keras.preprocessing.image import ImageDataGenerator
#Training Set
train_set = train_datagen.flow_from_directory('train')

#Training Set
valid_set = train_datagen.flow_from_directory('test')


train_size, validation_size, test_size = 200, 100, 100
img_width, img_height = 224, 224  # Default input size for VGG16

# Extract features
import os, shutil


datagen = ImageDataGenerator(rescale=1./255)
batch_size = 32

def extract_features(directory, sample_count):
    features = np.zeros(shape=(sample_count, 7, 7, 512))  # Must be equal to the output of the convolutional base
    labels = np.zeros(shape=(sample_count))
    # Preprocess data
    generator = datagen.flow_from_directory(directory,
                                            target_size=(img_width,img_height),
                                            batch_size = batch_size,
                                            class_mode='categorical')
    # Pass data through convolutional base
    i = 0
    for inputs_batch, labels_batch in generator:
        features_batch = conv_base.predict(inputs_batch)
        features[i * batch_size: (i + 1) * batch_size] = features_batch
        labels[i * batch_size: (i + 1) * batch_size] = labels_batch
        i += 1
        if i * batch_size >= sample_count:
            break
    return features, labels

train_features, train_labels = extract_features(train_set, train_size)  # Agree with our small dataset size
validation_features, validation_labels = extract_features(validation_dir, validation_size)
test_features, test_labels = extract_features(test_dir, test_size)

this is the error occur

Found 714 images belonging to 10 classes. Found 100 images belonging

to 10 classes. --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 36 return features, labels 37 ---> 38 train_features, train_labels = extract_features(train_set, train_size) # Agree with our small dataset size 39 validation_features, validation_labels = extract_features(validation_dir, validation_size) 40 test_features, test_labels = extract_features(test_dir, test_size)

in extract_features(directory, sample_count) 24 target_size=(img_width,img_height), 25 batch_size = batch_size, ---> 26 class_mode='categorical') 27 # Pass data through convolutional base 28 i = 0

~\Anaconda3\envs\tensorflow_cpu\lib\site-packages\keras_preprocessing\image\image_data_generator.py in flow_from_directory(self, directory, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation) 538 follow_links=follow_links, 539 subset=subset, --> 540 interpolation=interpolation 541 ) 542

~\Anaconda3\envs\tensorflow_cpu\lib\site-packages\keras_preprocessing\image\directory_iterator.py in init(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype) 104 if not classes: 105 classes = [] --> 106 for subdir in sorted(os.listdir(directory)): 107 if os.path.isdir(os.path.join(directory, subdir)): 108 classes.append(subdir)

TypeError: listdir: path should be string, bytes, os.PathLike or None, not DirectoryIterator

1

1 Answers

0
votes

you are passing a data generator to another data generator, in this line:

generator = datagen.flow_from_directory(directory,
                                        target_size=(img_width,img_height),
                                        batch_size = batch_size,
                                        class_mode='categorical')

first argument, directory, should be a directory, not a data generator, it should be something like: 'path/to/my/train_set/' . which is just 'train' in your case I guess, since you have them in the same folder as your notebook.