# Storing and loading bottleneck features for transfer learning on large data sets (Keras)

I would like to apply transfer learning on a pretty large image data set in order to solve a classification problem. Currently I load a pre-trained net without the top layers, add my own top layers, freeze the base model and start training. Since the data set is too big to fit into memory I load the train and validation sets using prefetched TF Datasets:

train_ds = keras.preprocessing.image_dataset_from_directory (training_data_dir,
batch_size=batch_size,
image_size=img_size,
label_mode='categorical')
val_ds = keras.preprocessing.image_dataset_from_directory (validation_data_dir,
batch_size=batch_size,
image_size=img_size,
label_mode='categorical')
train_ds = train_ds.prefetch(buffer_size=buffer_size)
val_ds = val_ds.prefetch(buffer_size=buffer_size)


Now in order to reduce the training time I found a nice approach on this page: https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html Before training the model, they first run the base model for each input image and store the output of the base model, that is the bottleneck features. Training is then done on these bottleneck features. That means that during training the base network does not need to be executed, saving a lot of computation time. Now for their approach they have a data set that fits in memory and the set of all bottleneck features over all images is represented as a single numpy array which is stored in and loaded from a single file.

Unfortunately this is not possible in my case since the original image set is so large it will not fit into memory and also converted to a bottleneck feature set it is still to large. So I am looking for a solution where the bottleneck features of a single image are stored in a single file and where those bottleneck feature vectors can be loaded in a way that is similar to how image_dataset_from_directory loads images.

## 1 Answer

You can first write the bottleneck features into a tfrecords file, and then load them as a dataset for the training phase.

In the tensorflow documentation you can find complete examples of how to do both.