Saurav Maheshkar
Saurav Maheshkar

Reputation: 202

<keras.preprocessing.image.DirectoryIterator> Object returns TypeError: unsupported operand type(s) for +: 'int' and 'str'

I've used the following code snippet to create <keras.preprocessing.image.DirectoryIterator> objects for training and validation generators.

class DataLoader:

    @staticmethod
    def load_data(data_config, prefix = "blond"):
      
      train_datagen =  tf.keras.preprocessing.image.ImageDataGenerator(
            preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
            rotation_range=30,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True
        )

      valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
            preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
            rotation_range=30,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True
        )
      train_generator = train_datagen.flow_from_directory(
            'data/celeba-dataset/{}-train'.format(prefix),
                target_size=(data_config.data.IMG_HEIGHT, data_config.data.IMG_WIDTH),
                batch_size=data_config.train.BATCH_SIZE)

Then I create another Model class containing the following load_data and train functions

class Model(BaseModel):

    def __init__(self, config):
        super().__init__(config)
        self.img_height = int(self.config.data.IMG_HEIGHT)
        self.img_width = int(self.config.data.IMG_WIDTH)
        self.base_model = tf.keras.applications.InceptionV3(weights='imagenet',
                        include_top=False,
                        input_shape=(self.img_height, self.img_width, 3))

        self.model = None
        self.training_samples = int(self.config.data.TRAINING_SAMPLES)
        self.batch_size = int(self.config.train.BATCH_SIZE)
        self.steps_per_epoch = int(self.training_samples) // int(self.batch_size)
        self.num_epochs = int(self.config.train.EPOCHS)

        self.train_generator = None
        self.validation_generator = None
    
    def load_data(self):

        self.train_generator, self.validation_generator = DataLoader().load_data(self.config)

    def train(self):
        self.model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, momentum=0.9), 
               loss='categorical_crossentropy', 
               metrics=[tf.keras.metrics.TopKCategoricalAccuracy(k = 1)])

        checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='weights.best.inc.blond.hdf5', 
                               verbose=1, save_best_only=True)

        model_history = self.model.fit(self.train_generator,
                                        validation_data = self.validation_generator,
                                        steps_per_epoch= self.steps_per_epoch,
                                        epochs = self.num_epochs,
                                        class_weight='auto',
                                        callbacks=[checkpointer])
        return model_history.history['loss'], model_history.history['val_loss']

While running the following code,

model = Model(CFG)
model.load_data()
model.build()
model.train()

I get the following Traceback

Traceback (most recent call last):
  File "/Users/sauravmaheshkar/github/compression/train.py", line 14, in <module>
    model.train()
  File "/Users/sauravmaheshkar/github/compression/model/ForgetModel.py", line 70, in train
    callbacks=[checkpointer, ],
  File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1064, in fit
    steps_per_execution=self._steps_per_execution)
  File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__
    model=model)
  File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 898, in __init__
    self._size = len(x)
  File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 68, in __len__
    return (self.n + self.batch_size - 1) // self.batch_size  # round up
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The Config.py File

"""Project Config in JSON"""

CFG = {
    "data": {
        "data_folder" : "data/CelebA/",
        "images_folder" : "data/CelebA/img_align_celeba/img_align_celeba/",
        "IMG_HEIGHT": "218",
        "IMG_WIDTH": "178",
        "TRAINING_SAMPLES": "10000",
        "VALIDATION_SAMPLES": "2000",
        "TEST_SAMPLES": "2000",
    },
    "train": {
        "BATCH_SIZE": "64",
        "EPOCHS": "10",
    }   
}

Package Versions

Expected Output

Run the normal tensorflow training loop

Upvotes: 1

Views: 1636

Answers (1)

MAltakrori
MAltakrori

Reputation: 324

I think the problem is in the DataLoader class, in the load_data function. Specifically, in this line:

batch_size=data_config.train.BATCH_SIZE.

That line, and your data config file tell me that you just need to add int, and your problem would be solved. So, replace that line with:

batch_size=int(data_config.train.BATCH_SIZE)

I suggest you check the other parameters as well.

Alternatively, I think just removing the quotation marks from the int values in your Json file would also work.

As you can see here numbers/ints don't need quotations.

Upvotes: 1

Related Questions