Using Keras ImageDataGenerator in a regression model

后端 未结 4 1556
别跟我提以往
别跟我提以往 2020-12-30 02:29

I want to use the

flow_from_directory

method of the

ImageDataGenerator

to generate training data for a

4条回答
  •  自闭症患者
    2020-12-30 03:01

    At this moment (newest version of Keras from January 21st 2017) the flow_from_directory could only work in a following manner:

    1. You need to have a directories structured in a following manner:

      directory with images\
          1st label\
              1st picture from 1st label
              2nd picture from 1st label
              3rd picture from 1st label
              ...
          2nd label\
              1st picture from 2nd label
              2nd picture from 2nd label
              3rd picture from 2nd label
              ...
          ...
      
    2. flow_from_directory returns batches of a fixed size in a format of (picture, label).

    So as you can see it could only be used for a classification case and all options provided in a documentation specify only a way in which the class is provided to your classifier. But, there is a neat hack which could make a flow_from_directory useful for a regression task:

    1. You need to structure your directory in a following manner:

      directory with images\
          1st value (e.g. -0.95423)\
              1st picture from 1st value
              2nd picture from 1st value
              3rd picture from 1st value
              ...
          2nd value (e.g. - 0.9143242)\
              1st picture from 2nd value
              2nd picture from 2nd value
              3rd picture from 2nd value
              ...
         ...
      
    2. You also need to have a list list_of_values = [1st value, 2nd value, ...]. Then your generator is defined in a following manner:

      def regression_flow_from_directory(flow_from_directory_gen, list_of_values):
          for x, y in flow_from_directory_gen:
              yield x, list_of_values[y]
      

    And it's crucial for a flow_from_directory_gen to have a class_mode='sparse' to make this work. Of course this is a little bit cumbersome but it works (I used this solution :) )

提交回复
热议问题