Use different target vectors for CNN


I wish to use different target vectors (not the standard one-hot encoded) for training my CNN. My image data lies in 10 different folders (10 different categories). How do I use my desired target vectors? The flow_from_directory() outputs a one-hot encoded array of labels. I have the label vectors stored in a dictionary. Also, the names of the folders are the labels, if that helps.


Well as you may know the <a href="https://keras.io/preprocessing/image/#imagedatagenerator-class" rel="nofollow">ImageDataGenerator</a> in Keras is a python generator (if you are not familiar with python generators you can read more about them <a href="https://stackoverflow.com/questions/1756096/understanding-generators-in-python" rel="nofollow">here</a>). Since you want to use customized target vectors (and not the ones generated from <a href="https://keras.io/preprocessing/image/#flow_from_directory" rel="nofollow">flow_from_directory()</a>) you can manipulate the behavior of image generator by wrapping it inside another function. Here is how:

First we need to store our custom targets as a numpy array:

<pre class="lang-py prettyprint-override"># a numpy array containing the custom targets for each class # custom_target[0] is target vector of class #1 images # custom_target[1] is target vector of class #2 images # etc. custom_targets = your_custom_targets

Secondly, we create an image generator as usual and use the flow_from_directory to read images from disk. You need to set class_mode argument to 'sparse' to obtain the class index of each image. Further, you can set classes argument to a list containing the name of classes (i.e. directories). If you don't set this argument the order of the classes, which will map to the label indices, will be alphanumeric (i.e. 0 for class with the highest in alphabetical order, and so on):

<pre class="lang-py prettyprint-override">train_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( 'data/train', target_size=(150, 150), batch_size=32, class_mode='sparse') # NOTE: set class_mode to sparse to generate integer indices

(<strong>Note:</strong> If you don't set classes argument, make sure that custom_target[i] corresponds to the i-th class in alphabetical order.)

Now we can wrap our generator inside another function and generate batches of images and their corresponding numeric labels which we use to generate our own labels:

<pre class="lang-py prettyprint-override">def custom_generator(generator): for data, labels in generator: # get the custom labels corresponding to each class custom_labels = custom_targets[labels] yield data, custom_labels

And that's it! Now we have a custom generator that we can pass it to fit_generator (or predict_generator or evaluate_generator for inference time) like any other generator:

<pre class="lang-py prettyprint-override">model.fit_generator(custom_generator(train_generator), # the rest of args)


  • Copying weights from one Conv2D layer to another
  • Getting ImportError while importing keras
  • Why do we need to include_top=False if we need to change the input_shape?
  • DynamoDb “Query key condition not supported”
  • Location-aware geocoding on Android (just like Google Maps)
  • In Keras code: Assign new values to certain tensor elements
  • How to get Getopt::Long + pod2usage working?
  • Scons: create late targets
  • Get-AdComputer -filter parameter not accepting Get-Date output
  • How to write Delphi ActiveX control which can be used in MS Word/Excel/Powerpoint?
  • Is this hack a defined behavior for T4
  • Passing and ArrayList through intent
  • Sticky Service not restarting after RAM full on Xiaomi / Huawei / Lava
  • Can I customize a Jackson ObjectMapper by adding a module?
  • Price range slider for my website
  • Escaping single quotes in JDBC with MySql
  • css font-size and line-height not matching the baseline
  • Build Matrix of Comparisons in SQl Server
  • c++ using primitive types as a base class
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Creating NSCollectionView with datasource programatically
  • Compare struct to a constant in C
  • import scipy.sparse failed
  • How can Delete be both a DDL and a DML statement
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Regex to match a string not followed by anything
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Android application: how to use the camera and grab the image bytes?
  • Inline R code in YAML for rmarkdown doesn't run
  • QLineEdit password safety
  • How to set/get protobuf's extension field in Go?
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • Importing jscolor library in angular 2
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file