# How do I make a submission of a CNN?

I have built a CNN to do image classification for images representing different weather conditions. I have 4 classes of images : Haze, Rainy, Snowy, Sunny.

I have built my CNN and evaluated the performances. N ow I have been given a blind test set, so images without a label, and I have to make a submission. So I have to buld a .csv file which contains should contain one line for each predicted class of images, so it should have the structure ,. Thus each line should be a string which identifies the image and its prediction.

Now the problem is that I don't understand how to do this. I am really confused because I have never done something similar.

My code is the following:

trainingset = '/content/drive/My Drive/Colab Notebooks/Train'
testset = '/content/drive/My Drive/Colab Notebooks/Test_HWI'

batch_size = 31
train_datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rescale = 1. / 255,\
zoom_range=0.1,\
rotation_range=10,\
width_shift_range=0.1,\
height_shift_range=0.1,\
horizontal_flip=True,\
vertical_flip=False)

train_generator = train_datagen.flow_from_directory(
directory=trainingset,
target_size=(256, 256),
color_mode="rgb",
batch_size=batch_size,
class_mode="categorical",
shuffle=True
)

test_datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rescale = 1. / 255
)

test_generator = test_datagen.flow_from_directory(
directory=testset,
target_size=(256, 256),
color_mode="rgb",
batch_size=batch_size,
class_mode="categorical",
shuffle=False
)

num_samples = train_generator.n
num_classes = train_generator.num_classes
input_shape = train_generator.image_shape

classnames = [k for k,v in train_generator.class_indices.items()]


then I build the network:

def Network(input_shape, num_classes, regl2 = 0.0001, lr=0.0001):

model = Sequential()

# C1 Convolutional Layer
# Pooling
# Batch Normalisation before passing it to the next layer

# C2 Convolutional Layer
# Batch Normalisation

# C3 Convolutional Layer
# Batch Normalisation

# C4 Convolutional Layer
#Pooling
# Batch Normalisation

# C5 Convolutional Layer
# Pooling
# Batch Normalisation

# C6 Convolutional Layer
# Pooling
# Batch Normalisation

# C7 Convolutional Layer
# Pooling
# Batch Normalisation

# C8 Convolutional Layer
# Pooling
# Batch Normalisation

# Flatten

flatten_shape = (input_shape[0]*input_shape[1]*input_shape[2],)

# D1 Dense Layer
# Dropout
# Batch Normalisation

# D2 Dense Layer
# Dropout
# Batch Normalisation

# D3 Dense Layer
# Dropout
# Batch Normalisation

# Output Layer

# Compile

return model

#create the model
model = Network(input_shape,num_classes)
model.summary()


I train the network:

steps_per_epoch=train_generator.n//train_generator.batch_size
val_steps=test_generator.n//test_generator.batch_size+1

try:
history = model.fit_generator(train_generator, epochs=100, verbose=1,\
steps_per_epoch=steps_per_epoch,\
validation_data=test_generator,\
validation_steps=val_steps)
except KeyboardInterrupt:
pass


now, I have the images without labels in the google drive, so I define the path to them:

blind_testSet = '/content/drive/My Drive/Colab Notebooks/blind_testset'


but now I don't know what shoul I do. I really don't know how to define the .csv file I mentioned above.

[EDIT] Ok I am trying to make the predictions on the blind test set, but it is taking really a long time. What I have done is the following:

blind_testSet = '/content/drive/My Drive/Colab
Notebooks/submission/blind_testset'

test_datagen_blind = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rescale = 1. / 255
)

test_generator_blind = test_datagen.flow_from_directory(
directory=blind_testSet,
target_size=(256, 256),
color_mode="rgb",
batch_size=batch_size,
class_mode="categorical",
shuffle=False
)

preds = model.predict_generator(test_generator_blind,verbose=1,steps=val_steps)


the images I have inside this blind test set are 1500, but is it normal that it takes so long? Thanks.

[EDIT 2] To try to make the submission I am trying to use a code similar to this:

def make_submission(model, filename="submission.csv"):
X = df.values / 255
X = X.reshape(X.shape[0], 28, 28, 1)
preds = model.predict_classes(X)
subm = pd.DataFrame(data=list(zip(range(1, len(preds) + 1), preds)), columns=["ImageId", "Label"])
subm.to_csv(filename, index=False)


# return subm

but it seems to not work in my case. I have also tried to keep only the last 2 lines and use them, so :

 subm = pd.DataFrame(data=list(zip(range(1, len(preds) + 1), preds)), columns=["ImageId", "Label"])
subm.to_csv(filename, index=False)


can someone help me creating this csv file? Thanks.

• The edit should probably be a new question instead. – Ben Reiniger Dec 13 '19 at 22:27

test_set=pd.read_csv(blind_testSet) test_set["predicted_labels"]=model.predict(quntified pictures from test set)