I have a subset of ImageNet data contained in sub-folders locally, where each sub-folder represents a class of images. There are potentially hundreds of classes, and therefore sub-folders, and each subfolder can contain hundreds of images. Here is an example of this structure with a subset of folders. I want to train a classification model in tensorflow, but I am not sure how to format and load the data given this structure of different image classes in different folders and the class label being the name of the folder. Normally I've just used datasets that already exist in tensorflow like mnist or cifar10 which are formatted and easy to use.
How to load data in tensorflow from subdirectories
2.8k views Asked by Jane Sully At
2
There are 2 answers
2
Gerry P
On
you can us ImageDataGenerator.flow_from_directory. Documentation is here. Assume your sub directories reside in a directory called main_dir. Set the size of the images you want to process, below I used 224 X 224, also specified color images. class_mode is set to 'categorical' so when you compile your model use categorical cross entropy as the loss. Then use the code below.
train_gen=ImageDataGenerator(validation_split=.2,rescale=1/255)
train_gen=train_gen.flow_from_directory(main_dir, target_size=(256, 256),
color_mode="rgb", class_mode="categorical", batch_size=32, shuffle=True,
seed=123, subset='training)
valid_gen=train_gen.flow_from_directory(main_dir, target_size=(224, 224),
color_mode="rgb", class_mode="categorical", batch_size=32, shuffle=False,
seed=123, subset='validation)
# make and compile your model then fit the model per below
history=model.fit(x=train_gen, epochs=20, verbose=1, validation_data=valid_gen,
shuffle=True, initial_epoch=0)
Related Questions in TENSORFLOW
- (Tensorflow)Does the op assign change the gradient computation?
- Tensorflow Windows Accessing Folders Denied:"NewRandomAccessFile failed to Create/Open: Access is denied. ; Input/output error"
- Android App TensorFlow Google Cloud ML
- Convert Tensorflow model to Caffe model
- Google Tensorflow LSTMCell Variables Mapping to Hochreiter97_lstm.pdf paper
- additive Gaussian noise in Tensorflow
- TFlearn evaluate method results meaning
- Regularization losses Tensorflow - TRAINABLE_VARIABLES to Tensor Array
- feed picture to model tensorflow for training
- Fail to read the new format of tensorflow checkpoint?
- I got a error when running a github project in tensorflow
- Tensorflow R0.12 softmax_cross_entropy_with_logits ASSERT Error
- RuntimeError in run_one_batch of TensorFlowDataFrame in tensorflow
- Same output in neural network for each input after training
- ConvNet : Validation Loss not strongly decreasing but accuracy is improving
Related Questions in KERAS
- Intermediate layer in keras to fetch the weights, convert and feed to the network
- Updating Shared Variables in Keras
- Import theano gives the AttributeError: module 'theano' has no attribute 'gof'
- How to Implement "Multidirectional" LSTMs?
- Error in running keras for deep learning in ubuntu 14.04
- Issue with setting TensorFlow as the session in Keras
- Multiple outputs in Keras gives value error
- Strange behavior of a frozen inceptionV3 net in Keras
- Tensorflow Image Shape Error
- Rounding Error at a python neural network made by Keras
- K fold cross validation using keras
- Keras Binary Classifier Model.Predict() class association?
- How to predict a layer's weights using another model in an end to end fashion?
- CNN model why the data is too large?
- Keras How to use max_value in Relu activation function
Related Questions in SUBDIRECTORY
- EnvDTE iterate through subfolders within solution
- Get all items of a sharepoint list
- Google drive PHP API: unable to insert files or folders into subfolders
- Edit HTACCESS file to prevent direct access to a particular file within a particular folder
- Is there a way to associate data with a file in a folder hierarchy?
- Recursively expand and search pattern of specific subdirectories
- Win7 Batch File - Moving Subfolders(& Files) to Grand-Parent Directory
- How to link subdirectories using subdomain? (VirtualHost, mod_rewrite, htaccess, DNS)
- How can I download entire directory from remote machine with using scp libssh library in c++?
- Rewrite wildcard subdomain to the same name folder and get the subdomain name
- Recurse through selected level of subdirectories
- Iterate through the folder and find a particular file
- How to load folder structure into treeview?
- Traverse directory and zip certain subdirectories in place
- Python creating subdirectory with user input
Related Questions in TENSORFLOW-DATASETS
- tf.train.MonitoredTrainingSession and reinitializable iterator from Dataset
- How do I create padded batches in Tensorflow for tf.train.SequenceExample data using the DataSet API?
- How to use feedable iterator from Tensorflow Dataset API along with MonitoredTrainingSession?
- How to map a function with additional parameter using the new Dataset api in TF1.3?
- Skip Dataset entries in TFRecordDataset.map()
- output layers' name in C api Tensorflow
- How to create mini-batches using tensorflow.data.experimental.CsvDataset compatible with model's input shape?
- Pixel values change when loading image with TensorFlow Keras preprocessing
- Export data from PoseNet app by tensorflow-lite in android
- Filter NaN values in Tensorflow dataset
- Understanding the structure of Tensorflow datasets
- How to save and use a Tensorflow dataset using the Experimental save and load mehods?
- Autoencoder Custom Dataset tensorflow 2.3 ValueError: `y` argument is not supported when using dataset as input
- How to iterate on keras Dataset and edit content
- Importing data to tensorflow autoencoders through ImageDataGenerator
Related Questions in LOADDATA
- Load data Query with regular expression
- How to trigger function after using .loaddata
- Loading data from a text file Informix
- Mysql load data in file making a lot of read operations
- How to display progressbar while load data and fill into recyclerview?
- Android html image prob
- Android webview loaddata giving NullPointerException
- load 2 tableviews in a single view iOS
- Android webview loadData
- Load data files within a function using Matlab
- How to load data in tensorflow from subdirectories
- load data into table got following exception
- Problem with using Read10x function of Seurat
- Load data functions not working correctly on CSV(Comma Delimited) file
- Is there any way to load data while scrolling in jquery for Iphone (mobile websites)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)

You can use
tf.keras.preprocessing.image_dataset_from_directory().Your directory structure would be something like this but with many more classes:
I would suggest you split the dataset before this step as I think the data is split here randomly and not by stratified sampling(if your datasets are imbalanced then do this first and do not use the validation split to do it for you as I am not sure of the nature of how splitting is done as there is no mention of it).
Example:
Important things you have to set:
Labels must be inferred where the labels of the images are generated based on the directory structure so it follows the order of the classes.
Label mode has to be set to "categorical" which encodes the labels as a categorical vector.
Class names you can set this yourself where you would have to list the order of the folders in the directory otherwise the order is based on alphanumeric ordering. What you can do here as you have lots of folders is use
os.walk(directory)to get the list of the directories in the order that they are.Image size you can resize the images to be of the same size. Do so according to the model that you are using i.e., MobileNet takes in (224,224) so you can set this to (224,224).
More information here.