Inference
This is the process of predicting unseen data to the model. First we need to make the submission images really for inference.
This is done by filtering the images from the dataset
folder with SampleSubmisson.csv
file to create the images for inference.
def filter_submission():
for i in range(len(df_submission)):
sub_image = df_submission.loc[i, "image"]
dir = f"./dataset/{sub_image}"
target = "./image/sub/sub"
shutil.copy(dir, target)
filter_submission
will filter the images from dataset
folder in image\sub\sub
directory for inference.
Data Preparation
The images should be prepared in the format that the model understand this mean transfroming them into tensors then reshaping in the the shape are the trained dataset.
def sub_tensor():
# Converting the test images to tensors
image_tensor = []
image_label = []
for i in range(len(df_submission)):
image = df_submission.loc[i, "image"]
dir = f"./image/sub/sub/{image}"
img = Image.open(dir)
numpy_img = img_to_array(img.convert('RGB').resize((256, 256)))
tensor_image = tf.convert_to_tensor(numpy_img/255)
image_tensor.append(tensor_image)
image_label.append(image)
return image_tensor, image_label
Model prediction
As now the inference dataset is ready for prediction and its feed to the model. The model out puts will be 1
or 0
as the train labels for the dataset.
After predcition we will create a csv file mapping the image file name and its prediction of 1
(with mask) or 0
(no mask).
sub_class = model.predict(tf.convert_to_tensor(np.array(image_tensor)))
predicted_value = [round(sub_class[i][0], 2) for i in range(len(sub_class))]
sub_data = {"image": image_labels, "target": predicted_value}
submission = pd.DataFrame(sub_data)
submission.to_csv('submission.csv', encoding = 'utf-8-sig', index=False)
files.download('submission.csv')
This file will be upload to zindi (opens in a new tab) as a submission file for the challenge.