I am attempting to create an image classifier with VGG16 transfer learning using the ImageFolder dataset builder. The data is successfully built from a directory using this code:
def dataset_creator(directory=""): builder = tfds.folder_dataset.ImageFolder("/home/blabs/Documents/CompCore/visSystems/affect2/",shape=(224,224,1)) ds = builder.as_dataset(split='train', shuffle_files=True) # tfds.show_examples(ds, builder.info) # print(type(ds)) return ds
However, when I try to input the dataset into the training script using this code:
ds = dataset_creator() num_classes = 8 base_model = VGG16(weights="imagenet", include_top=False, input_shape=(224,224,3)) base_model.trainable = False normalization_layer = layers.Rescaling(scale=1./127.5, offset=-1) flatten_layer = layers.Flatten() dense_layer_1 = layers.Dense(160, activation='relu') dense_layer_2 = layers.Dense(80, activation='relu') prediction_layer = layers.Dense(num_classes, activation='softmax') model = models.Sequential([ base_model, normalization_layer, flatten_layer, dense_layer_1, dense_layer_2, prediction_layer ]) from tensorflow.keras.callbacks import EarlyStopping model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'], ) es = EarlyStopping(monitor='val_accuracy', mode='max', patience=5, restore_best_weights=True) model.fit(ds, epochs=10, callbacks=[es]) model.save(directory)
It results in the following ValueError:
ValueError: Missing data for input "vgg16_input". You passed a data dictionary with keys ['image', 'image/filename', 'label']. Expected the following keys: ['vgg16_input']
Due to this error, I have attempted to index the ds variable with [‘image’] and [‘label’], but to no avail. How can I proceed in using the Image_Folder dataset as training material for my VGG16 transfer learning CNN?
If the answer is obvious, please forgive me. This is my first time attempting to create a CNN with Tensorflow.
submitted by /u/blevlabs
[visit reddit] [comments]