How to manually specify class labels in keras flow_from_directory?

前端 未结 3 1088
不知归路
不知归路 2021-02-01 19:38

Problem: I am training a model for multilabel image recognition. My images are therefore associated with multiple y labels. This is conflicting with the conveni

相关标签:
3条回答
  • 2021-02-01 19:55
    # Training the model
    history = model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=3, validation_data=val_generator,validation_steps=validation_steps, verbose=1,
                        callbacks= keras.callbacks.ModelCheckpoint(filepath='/content/results',monitor='val_accuracy', save_best_only=True,save_weights_only=False))
    

    The validation_steps or the steps_per_epoch might be exceeding than that of the original parameters.

    steps_per_epoch= (int(num_of_training_examples/batch_size) might help. Similarly validation_steps= (int(num_of_val_examples/batch_size) will help

    0 讨论(0)
  • 2021-02-01 20:05

    You could simply use the flow_from_directory and extend it to a multiclass in a following manner:

    def multiclass_flow_from_directory(flow_from_directory_gen, multiclasses_getter):
        for x, y in flow_from_directory_gen:
            yield x, multiclasses_getter(x, y)
    

    Where multiclasses_getter is assigning a multiclass vector / your multiclass representation to your images. Note that x and y are not a single examples but batches of examples, so this should be included in your multiclasses_getter design.

    0 讨论(0)
  • 2021-02-01 20:09

    You could write a custom generator class that would read the files in from the directory and apply the labeling. That custom generator could also take in an ImageDataGenerator instance which would produce the batches using flow().

    I am imagining something like this:

    class Generator():
    
        def __init__(self, X, Y, img_data_gen, batch_size):
            self.X = X
            self.Y = Y  # Maybe a file that has the appropriate label mapping?
            self.img_data_gen = img_data_gen  # The ImageDataGenerator Instance
            self.batch_size = batch_size
    
        def apply_labels(self):
            # Code to apply labels to each sample based on self.X and self.Y
    
        def get_next_batch(self):
            """Get the next training batch"""
            self.img_data_gen.flow(self.X, self.Y, self.batch_size)
    

    Then simply:

    img_gen = ImageDataGenerator(...)
    gen = Generator(X, Y, img_gen, 128)
    
    model.fit_generator(gen.get_next_batch(), ...)
    

    *Disclaimer: I haven't actually tested this, but it should work in theory.

    0 讨论(0)
提交回复
热议问题