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
?