How to pass one_hot encoding labels into flow_from_dataframe?












2















I am using flow_from_dataframe to pass in my images to the generator. However I worked using cifar10 data. In the flow from dataframe method there is a parameter "y_col" that needs to be set which contains the column for the labels. I am having my file as the following.



           Name  1   2   3   4   5   6   7   8   9   10   11   12   13   14 
00001522_000.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
00023313_000.png 1 0 0 0 0 0 0 0 0 0 0 0 0 0
00023313_001.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
00023313_002.png 0 0 1 0 0 0 0 1 0 0 1 0 0 0


If we look at it the last image falls into multiple classes.



Now the file with which I learnt the method, the file looks like the following:



id,label
1,frog
2,truck
3,truck
4,deer


Below is the code I have used



import pandas as pd
df=pd.read_csv(r".train.csv")

datagen=ImageDataGenerator(rescale=1./255)
train_generator=datagen.flow_from_dataframe(dataframe=df,directory=".train_imgs", x_col="id", y_col="label", has_ext=False, class_mode="categorical", target_size=(32,32), batch_size=32)

model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(32,32,3)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(optimizers.rmsprop(lr=0.0001,loss="categorical_crossentropy", metrics=["accuracy"])

STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=10)


How can I arrange my label columns so that there can be a single column for the "y_col" parameter just like the second file. Or is there any other of of without changing the labels I can pass it into the y_col?










share|improve this question





























    2















    I am using flow_from_dataframe to pass in my images to the generator. However I worked using cifar10 data. In the flow from dataframe method there is a parameter "y_col" that needs to be set which contains the column for the labels. I am having my file as the following.



               Name  1   2   3   4   5   6   7   8   9   10   11   12   13   14 
    00001522_000.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    00023313_000.png 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    00023313_001.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    00023313_002.png 0 0 1 0 0 0 0 1 0 0 1 0 0 0


    If we look at it the last image falls into multiple classes.



    Now the file with which I learnt the method, the file looks like the following:



    id,label
    1,frog
    2,truck
    3,truck
    4,deer


    Below is the code I have used



    import pandas as pd
    df=pd.read_csv(r".train.csv")

    datagen=ImageDataGenerator(rescale=1./255)
    train_generator=datagen.flow_from_dataframe(dataframe=df,directory=".train_imgs", x_col="id", y_col="label", has_ext=False, class_mode="categorical", target_size=(32,32), batch_size=32)

    model = Sequential()
    model.add(Conv2D(32, (3, 3), padding='same',
    input_shape=(32,32,3)))
    model.add(Activation('relu'))
    model.add(Conv2D(32, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, (3, 3), padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(10, activation='softmax'))

    model.compile(optimizers.rmsprop(lr=0.0001,loss="categorical_crossentropy", metrics=["accuracy"])

    STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
    STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
    model.fit_generator(generator=train_generator,
    steps_per_epoch=STEP_SIZE_TRAIN,
    validation_data=valid_generator,
    validation_steps=STEP_SIZE_VALID,
    epochs=10)


    How can I arrange my label columns so that there can be a single column for the "y_col" parameter just like the second file. Or is there any other of of without changing the labels I can pass it into the y_col?










    share|improve this question



























      2












      2








      2








      I am using flow_from_dataframe to pass in my images to the generator. However I worked using cifar10 data. In the flow from dataframe method there is a parameter "y_col" that needs to be set which contains the column for the labels. I am having my file as the following.



                 Name  1   2   3   4   5   6   7   8   9   10   11   12   13   14 
      00001522_000.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      00023313_000.png 1 0 0 0 0 0 0 0 0 0 0 0 0 0
      00023313_001.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      00023313_002.png 0 0 1 0 0 0 0 1 0 0 1 0 0 0


      If we look at it the last image falls into multiple classes.



      Now the file with which I learnt the method, the file looks like the following:



      id,label
      1,frog
      2,truck
      3,truck
      4,deer


      Below is the code I have used



      import pandas as pd
      df=pd.read_csv(r".train.csv")

      datagen=ImageDataGenerator(rescale=1./255)
      train_generator=datagen.flow_from_dataframe(dataframe=df,directory=".train_imgs", x_col="id", y_col="label", has_ext=False, class_mode="categorical", target_size=(32,32), batch_size=32)

      model = Sequential()
      model.add(Conv2D(32, (3, 3), padding='same',
      input_shape=(32,32,3)))
      model.add(Activation('relu'))
      model.add(Conv2D(32, (3, 3)))
      model.add(Activation('relu'))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.25))

      model.add(Conv2D(64, (3, 3), padding='same'))
      model.add(Activation('relu'))
      model.add(Conv2D(64, (3, 3)))
      model.add(Activation('relu'))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.25))

      model.add(Flatten())
      model.add(Dense(512))
      model.add(Activation('relu'))
      model.add(Dropout(0.5))
      model.add(Dense(10, activation='softmax'))

      model.compile(optimizers.rmsprop(lr=0.0001,loss="categorical_crossentropy", metrics=["accuracy"])

      STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
      STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
      model.fit_generator(generator=train_generator,
      steps_per_epoch=STEP_SIZE_TRAIN,
      validation_data=valid_generator,
      validation_steps=STEP_SIZE_VALID,
      epochs=10)


      How can I arrange my label columns so that there can be a single column for the "y_col" parameter just like the second file. Or is there any other of of without changing the labels I can pass it into the y_col?










      share|improve this question
















      I am using flow_from_dataframe to pass in my images to the generator. However I worked using cifar10 data. In the flow from dataframe method there is a parameter "y_col" that needs to be set which contains the column for the labels. I am having my file as the following.



                 Name  1   2   3   4   5   6   7   8   9   10   11   12   13   14 
      00001522_000.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      00023313_000.png 1 0 0 0 0 0 0 0 0 0 0 0 0 0
      00023313_001.png 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      00023313_002.png 0 0 1 0 0 0 0 1 0 0 1 0 0 0


      If we look at it the last image falls into multiple classes.



      Now the file with which I learnt the method, the file looks like the following:



      id,label
      1,frog
      2,truck
      3,truck
      4,deer


      Below is the code I have used



      import pandas as pd
      df=pd.read_csv(r".train.csv")

      datagen=ImageDataGenerator(rescale=1./255)
      train_generator=datagen.flow_from_dataframe(dataframe=df,directory=".train_imgs", x_col="id", y_col="label", has_ext=False, class_mode="categorical", target_size=(32,32), batch_size=32)

      model = Sequential()
      model.add(Conv2D(32, (3, 3), padding='same',
      input_shape=(32,32,3)))
      model.add(Activation('relu'))
      model.add(Conv2D(32, (3, 3)))
      model.add(Activation('relu'))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.25))

      model.add(Conv2D(64, (3, 3), padding='same'))
      model.add(Activation('relu'))
      model.add(Conv2D(64, (3, 3)))
      model.add(Activation('relu'))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.25))

      model.add(Flatten())
      model.add(Dense(512))
      model.add(Activation('relu'))
      model.add(Dropout(0.5))
      model.add(Dense(10, activation='softmax'))

      model.compile(optimizers.rmsprop(lr=0.0001,loss="categorical_crossentropy", metrics=["accuracy"])

      STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
      STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
      model.fit_generator(generator=train_generator,
      steps_per_epoch=STEP_SIZE_TRAIN,
      validation_data=valid_generator,
      validation_steps=STEP_SIZE_VALID,
      epochs=10)


      How can I arrange my label columns so that there can be a single column for the "y_col" parameter just like the second file. Or is there any other of of without changing the labels I can pass it into the y_col?







      python keras neural-network deep-learning






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 16:45







      Souradip Roy

















      asked Nov 19 '18 at 21:11









      Souradip RoySouradip Roy

      184




      184
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382694%2fhow-to-pass-one-hot-encoding-labels-into-flow-from-dataframe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382694%2fhow-to-pass-one-hot-encoding-labels-into-flow-from-dataframe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?