ECMM426-Template/Question 7 - YOLOv8.ipynb

49 KiB

None <html> <head> </head>

Question 7 - YOLOv8

In [1]:
import cv2
import torch
import numpy as np
from collections import Counter
import matplotlib.pyplot as plt

from ultralytics import YOLO

import os
import glob
from tqdm import tqdm
In [2]:
MODEL_NAME = "data/yolov8.pt"

Load the dataset

In [3]:
val_labels = "./data/MaskedFace/val/labels"
val_imgs = "./data/MaskedFace/val/images"

y_true = glob.glob(os.path.join(val_labels,"*.txt"))
images = glob.glob(os.path.join(val_imgs,"*.png"))
In [4]:
test_dataset = {
    'images': images,  # list of image paths
    'y_true': y_true,  # list of label paths
}
In [5]:
def count_obj(txt_file, n_class):
    with open(txt_file, 'r') as file:
        lines = file.readlines()
    # Extracting the class identifiers from each line
    class_ids = [int(line.split()[0]) for line in lines]

    # Counting the occurrences of each class
    class_counts = Counter(class_ids)

    # Sorting the dictionary by class id and converting it to a list of counts
    sorted_counts = [class_counts[i] if i in class_counts else 0 for i in range(n_class)]
    return sorted_counts
In [6]:
gt_counts = []
for idx , (img , txt) in enumerate(tqdm(zip(test_dataset['images'], test_dataset['y_true']))):
    # get ground truth
    obj_count = count_obj(txt, 3)
    gt_counts.append(obj_count)
85it [00:00, 1838.59it/s]

Load the model

In [7]:
model = YOLO(MODEL_NAME)

Test on the validation set

In [8]:
from collections import Counter

def calculate_mape(actual, forecast):
    if len(actual) != len(forecast):
        raise ValueError("The length of actual and forecast arrays must be the same.")
    
    n = len(actual)
    sum_error = 0
    
    for a, f in zip(actual, forecast):
        sum_error += abs(a - f) / max(a, 1)
    
    mape_value = (sum_error / n) * 100
    return mape_value

def count_masks(model, dataset):
    n_class = 3
    mape_scores = []
    all_pred_counts = []
    all_obj_counts = []
    for idx , (img , txt) in enumerate(tqdm(zip(dataset['images'],dataset['y_true']))):
        # get predicted list
        preds = model.predict(img)
        pred = preds[0]
        predict_list = [ box.cls[0].item() for box in pred.boxes]
        count = Counter(predict_list)
        predict_count = [count[i] if i in count else 0 for i in range(n_class)]
        # get ground truth
        obj_count = count_obj(txt, n_class)
        all_obj_counts.append(obj_count)
        all_pred_counts.append(predict_count)

    '''
    After the model was trained, I just found that I defined the format class in data.yaml is [without_mask, with_mask, mask_weared_incorrect] which is wrong in order. 
    Therefore, I will swap the true label and predicted label to [with_mask, without_mask, mask_weared_incorrect] in the count_masks function to return the values should respectively indicate the number of faces wearing mask, without mask and incorrectly wearing mask.
    The reason why I did not correct the data.yaml and train the model again because of the limitation of time.
    '''
    all_pred_counts = np.array(all_pred_counts)
    all_obj_counts = np.array(all_obj_counts)

#     all_pred_counts[:, [0, 1]] = all_pred_counts[:, [1, 0]]
#     all_obj_counts[:, [0, 1]] = all_obj_counts[:, [1, 0]]

    mape_scores = [calculate_mape(a, p) for a, p in zip(all_obj_counts, all_pred_counts)]

    # Convert all_pred_counts to int64 before returning
    all_pred_counts = all_pred_counts.astype(np.int64)
    
    return np.array(all_pred_counts), np.mean(mape_scores)
In [9]:
predicted_counts, mape_score = count_masks(model, test_dataset)
0it [00:00, ?it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-011.png: 448x640 26 with_masks, 1 mask_weared_incorrect, 311.0ms
Speed: 5.7ms preprocess, 311.0ms inference, 6.6ms postprocess per image at shape (1, 3, 448, 640)
1it [00:02,  2.99s/it]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-019.png: 384x640 3 with_masks, 2 without_masks, 1 mask_weared_incorrect, 237.2ms
Speed: 6.9ms preprocess, 237.2ms inference, 1.7ms postprocess per image at shape (1, 3, 384, 640)
2it [00:03,  1.39s/it]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-023.png: 480x640 1 with_mask, 293.8ms
Speed: 6.0ms preprocess, 293.8ms inference, 2.8ms postprocess per image at shape (1, 3, 480, 640)
3it [00:03,  1.11it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-032.png: 384x640 2 with_masks, 187.6ms
Speed: 3.9ms preprocess, 187.6ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)
4it [00:03,  1.58it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-036.png: 384x640 6 with_masks, 1 without_mask, 190.1ms
Speed: 5.0ms preprocess, 190.1ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)
5it [00:04,  2.06it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-041.png: 480x640 8 with_masks, 220.2ms
Speed: 5.4ms preprocess, 220.2ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)
6it [00:04,  2.48it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-043.png: 640x448 1 with_mask, 255.3ms
Speed: 4.9ms preprocess, 255.3ms inference, 2.2ms postprocess per image at shape (1, 3, 640, 448)
7it [00:04,  2.75it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-058.png: 448x640 13 with_masks, 245.6ms
Speed: 7.3ms preprocess, 245.6ms inference, 1.0ms postprocess per image at shape (1, 3, 448, 640)
8it [00:04,  2.98it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-080.png: 384x640 1 with_mask, 201.2ms
Speed: 5.1ms preprocess, 201.2ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
9it [00:05,  3.31it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-086.png: 480x640 3 with_masks, 1 without_mask, 256.2ms
Speed: 5.1ms preprocess, 256.2ms inference, 1.8ms postprocess per image at shape (1, 3, 480, 640)
10it [00:05,  3.37it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-094.png: 384x640 5 with_masks, 1 without_mask, 1 mask_weared_incorrect, 199.7ms
Speed: 4.1ms preprocess, 199.7ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
11it [00:05,  3.64it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-095.png: 640x512 1 without_mask, 292.5ms
Speed: 6.2ms preprocess, 292.5ms inference, 3.0ms postprocess per image at shape (1, 3, 640, 512)
12it [00:05,  3.45it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-098.png: 448x640 4 with_masks, 1 without_mask, 230.2ms
Speed: 5.6ms preprocess, 230.2ms inference, 1.0ms postprocess per image at shape (1, 3, 448, 640)
13it [00:06,  3.57it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-123.png: 512x640 3 with_masks, 253.3ms
Speed: 4.0ms preprocess, 253.3ms inference, 0.0ms postprocess per image at shape (1, 3, 512, 640)
14it [00:06,  3.56it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-130.png: 640x544 2 with_masks, 2 without_masks, 264.4ms
Speed: 10.2ms preprocess, 264.4ms inference, 2.1ms postprocess per image at shape (1, 3, 640, 544)
15it [00:06,  3.50it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-131.png: 448x640 4 with_masks, 2 without_masks, 222.3ms
Speed: 2.4ms preprocess, 222.3ms inference, 0.0ms postprocess per image at shape (1, 3, 448, 640)
16it [00:06,  3.64it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-139.png: 384x640 17 with_masks, 2 without_masks, 2 mask_weared_incorrects, 175.8ms
Speed: 4.0ms preprocess, 175.8ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)
17it [00:07,  3.94it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-143.png: 640x512 1 with_mask, 231.2ms
Speed: 6.1ms preprocess, 231.2ms inference, 2.1ms postprocess per image at shape (1, 3, 640, 512)
18it [00:07,  3.92it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-148.png: 608x640 2 with_masks, 3 without_masks, 295.3ms
Speed: 8.5ms preprocess, 295.3ms inference, 0.0ms postprocess per image at shape (1, 3, 608, 640)
19it [00:07,  3.59it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-149.png: 480x640 7 with_masks, 1 without_mask, 229.2ms
Speed: 6.6ms preprocess, 229.2ms inference, 8.1ms postprocess per image at shape (1, 3, 480, 640)
20it [00:08,  3.64it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-152.png: 480x640 8 with_masks, 209.4ms
Speed: 4.9ms preprocess, 209.4ms inference, 8.1ms postprocess per image at shape (1, 3, 480, 640)
21it [00:08,  3.78it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-169.png: 640x512 1 with_mask, 242.2ms
Speed: 6.1ms preprocess, 242.2ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 512)
22it [00:08,  3.75it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-180.png: 640x512 1 with_mask, 232.8ms
Speed: 8.4ms preprocess, 232.8ms inference, 7.1ms postprocess per image at shape (1, 3, 640, 512)
23it [00:08,  3.75it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-182.png: 448x640 2 with_masks, 1 mask_weared_incorrect, 222.5ms
Speed: 5.3ms preprocess, 222.5ms inference, 0.0ms postprocess per image at shape (1, 3, 448, 640)
24it [00:09,  3.83it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-184.png: 352x640 16 with_masks, 172.3ms
Speed: 2.0ms preprocess, 172.3ms inference, 7.1ms postprocess per image at shape (1, 3, 352, 640)
25it [00:09,  4.12it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-197.png: 384x640 1 with_mask, 182.8ms
Speed: 2.5ms preprocess, 182.8ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
26it [00:09,  4.32it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-201.png: 384x640 12 with_masks, 177.6ms
Speed: 3.8ms preprocess, 177.6ms inference, 2.1ms postprocess per image at shape (1, 3, 384, 640)
27it [00:09,  4.49it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-209.png: 352x640 4 with_masks, 13 without_masks, 168.5ms
Speed: 14.7ms preprocess, 168.5ms inference, 7.6ms postprocess per image at shape (1, 3, 352, 640)
28it [00:09,  4.56it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-226.png: 384x640 1 with_mask, 171.7ms
Speed: 3.9ms preprocess, 171.7ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)
29it [00:10,  4.68it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-227.png: 448x640 11 with_masks, 223.3ms
Speed: 4.7ms preprocess, 223.3ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
30it [00:10,  4.47it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-248.png: 640x512 1 without_mask, 237.8ms
Speed: 5.6ms preprocess, 237.8ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 512)
31it [00:10,  4.20it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-256.png: 448x640 13 with_masks, 222.1ms
Speed: 4.2ms preprocess, 222.1ms inference, 1.0ms postprocess per image at shape (1, 3, 448, 640)
32it [00:10,  4.16it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-260.png: 480x640 50 with_masks, 290.6ms
Speed: 6.5ms preprocess, 290.6ms inference, 2.4ms postprocess per image at shape (1, 3, 480, 640)
33it [00:11,  3.76it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-269.png: 480x640 2 with_masks, 240.2ms
Speed: 4.7ms preprocess, 240.2ms inference, 2.0ms postprocess per image at shape (1, 3, 480, 640)
34it [00:11,  3.75it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-275.png: 640x544 1 with_mask, 259.9ms
Speed: 6.7ms preprocess, 259.9ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 544)
35it [00:11,  3.66it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-280.png: 448x640 14 with_masks, 7 without_masks, 221.5ms
Speed: 3.9ms preprocess, 221.5ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
36it [00:11,  3.76it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-286.png: 480x640 3 with_masks, 286.4ms
Speed: 5.1ms preprocess, 286.4ms inference, 2.6ms postprocess per image at shape (1, 3, 480, 640)
37it [00:12,  3.56it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-292.png: 480x640 1 with_mask, 226.3ms
Speed: 7.4ms preprocess, 226.3ms inference, 2.1ms postprocess per image at shape (1, 3, 480, 640)
38it [00:12,  3.65it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-296.png: 384x640 27 with_masks, 15 without_masks, 199.4ms
Speed: 4.5ms preprocess, 199.4ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
39it [00:12,  3.84it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-305.png: 480x640 6 with_masks, 9 without_masks, 4 mask_weared_incorrects, 268.3ms
Speed: 6.8ms preprocess, 268.3ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)
40it [00:13,  3.67it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-307.png: 384x640 2 with_masks, 1 mask_weared_incorrect, 182.9ms
Speed: 3.7ms preprocess, 182.9ms inference, 1.6ms postprocess per image at shape (1, 3, 384, 640)
41it [00:13,  3.96it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-323.png: 608x640 2 with_masks, 261.5ms
Speed: 9.3ms preprocess, 261.5ms inference, 0.0ms postprocess per image at shape (1, 3, 608, 640)
42it [00:13,  3.75it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-328.png: 640x512 1 with_mask, 230.3ms
Speed: 6.3ms preprocess, 230.3ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 512)
43it [00:13,  3.78it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-368.png: 448x640 9 with_masks, 225.7ms
Speed: 2.9ms preprocess, 225.7ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
44it [00:14,  3.84it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-370.png: 640x512 1 without_mask, 232.2ms
Speed: 7.4ms preprocess, 232.2ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 512)
45it [00:14,  3.83it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-371.png: 384x640 1 with_mask, 1 mask_weared_incorrect, 232.0ms
Speed: 3.5ms preprocess, 232.0ms inference, 2.4ms postprocess per image at shape (1, 3, 384, 640)
46it [00:14,  3.84it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-373.png: 448x640 13 with_masks, 2 without_masks, 222.2ms
Speed: 4.5ms preprocess, 222.2ms inference, 3.0ms postprocess per image at shape (1, 3, 448, 640)
47it [00:14,  3.86it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-377.png: 480x640 1 with_mask, 282.4ms
Speed: 5.3ms preprocess, 282.4ms inference, 2.0ms postprocess per image at shape (1, 3, 480, 640)
48it [00:15,  3.66it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-383.png: 640x512 1 with_mask, 251.2ms
Speed: 4.3ms preprocess, 251.2ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 512)
49it [00:15,  3.64it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-387.png: 384x640 5 with_masks, 1 mask_weared_incorrect, 182.5ms
Speed: 3.4ms preprocess, 182.5ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)
50it [00:15,  3.93it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-410.png: 448x640 18 with_masks, 2 without_masks, 1 mask_weared_incorrect, 261.8ms
Speed: 7.9ms preprocess, 261.8ms inference, 1.9ms postprocess per image at shape (1, 3, 448, 640)
51it [00:15,  3.76it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-411.png: 448x640 7 with_masks, 1 without_mask, 326.2ms
Speed: 4.8ms preprocess, 326.2ms inference, 1.5ms postprocess per image at shape (1, 3, 448, 640)
52it [00:16,  3.42it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-444.png: 416x640 3 with_masks, 247.0ms
Speed: 6.0ms preprocess, 247.0ms inference, 2.1ms postprocess per image at shape (1, 3, 416, 640)
53it [00:16,  3.48it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-450.png: 480x640 4 with_masks, 290.4ms
Speed: 8.8ms preprocess, 290.4ms inference, 2.0ms postprocess per image at shape (1, 3, 480, 640)
54it [00:16,  3.36it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-461.png: 448x640 8 with_masks, 228.1ms
Speed: 8.0ms preprocess, 228.1ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
55it [00:17,  3.50it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-512.png: 448x640 8 with_masks, 195.1ms
Speed: 5.8ms preprocess, 195.1ms inference, 7.5ms postprocess per image at shape (1, 3, 448, 640)
56it [00:17,  3.72it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-515.png: 640x512 1 with_mask, 241.7ms
Speed: 7.8ms preprocess, 241.7ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 512)
57it [00:17,  3.71it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-528.png: 640x512 1 without_mask, 287.1ms
Speed: 6.2ms preprocess, 287.1ms inference, 1.8ms postprocess per image at shape (1, 3, 640, 512)
58it [00:17,  3.52it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-554.png: 480x640 5 with_masks, 262.2ms
Speed: 4.4ms preprocess, 262.2ms inference, 0.0ms postprocess per image at shape (1, 3, 480, 640)
59it [00:18,  3.50it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-555.png: 384x640 4 with_masks, 203.6ms
Speed: 4.0ms preprocess, 203.6ms inference, 2.4ms postprocess per image at shape (1, 3, 384, 640)
60it [00:18,  3.72it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-560.png: 448x640 1 with_mask, 259.8ms
Speed: 3.6ms preprocess, 259.8ms inference, 0.0ms postprocess per image at shape (1, 3, 448, 640)
61it [00:18,  3.65it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-562.png: 352x640 1 with_mask, 1 mask_weared_incorrect, 178.5ms
Speed: 0.0ms preprocess, 178.5ms inference, 1.0ms postprocess per image at shape (1, 3, 352, 640)
62it [00:18,  4.00it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-574.png: 384x640 5 with_masks, 181.9ms
Speed: 4.9ms preprocess, 181.9ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)
63it [00:19,  4.21it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-581.png: 640x512 1 with_mask, 197.9ms
Speed: 8.3ms preprocess, 197.9ms inference, 6.7ms postprocess per image at shape (1, 3, 640, 512)
64it [00:19,  4.25it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-589.png: 640x448 1 with_mask, 1 mask_weared_incorrect, 197.9ms
Speed: 5.3ms preprocess, 197.9ms inference, 6.1ms postprocess per image at shape (1, 3, 640, 448)
65it [00:19,  4.31it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-594.png: 448x640 8 with_masks, 204.8ms
Speed: 3.6ms preprocess, 204.8ms inference, 0.0ms postprocess per image at shape (1, 3, 448, 640)
66it [00:19,  4.32it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-598.png: 640x512 1 with_mask, 204.8ms
Speed: 6.3ms preprocess, 204.8ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 512)
67it [00:20,  4.31it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-606.png: 448x640 4 with_masks, 239.3ms
Speed: 4.0ms preprocess, 239.3ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
68it [00:20,  4.14it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-607.png: 448x640 2 with_masks, 1 without_mask, 177.7ms
Speed: 4.7ms preprocess, 177.7ms inference, 0.0ms postprocess per image at shape (1, 3, 448, 640)
69it [00:20,  4.33it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-610.png: 352x640 5 with_masks, 158.2ms
Speed: 4.2ms preprocess, 158.2ms inference, 0.0ms postprocess per image at shape (1, 3, 352, 640)
70it [00:20,  4.61it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-621.png: 384x640 3 with_masks, 2 without_masks, 175.1ms
Speed: 4.0ms preprocess, 175.1ms inference, 7.1ms postprocess per image at shape (1, 3, 384, 640)
71it [00:20,  4.68it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-637.png: 384x640 6 with_masks, 171.3ms
Speed: 4.1ms preprocess, 171.3ms inference, 9.2ms postprocess per image at shape (1, 3, 384, 640)
72it [00:21,  4.75it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-660.png: 640x512 1 without_mask, 212.6ms
Speed: 4.5ms preprocess, 212.6ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 512)
73it [00:21,  4.59it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-690.png: 416x640 19 with_masks, 1 without_mask, 181.0ms
Speed: 4.0ms preprocess, 181.0ms inference, 0.0ms postprocess per image at shape (1, 3, 416, 640)
74it [00:21,  4.64it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-699.png: 448x640 1 with_mask, 6 without_masks, 212.2ms
Speed: 3.6ms preprocess, 212.2ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
75it [00:21,  4.50it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-719.png: 384x640 4 with_masks, 1 without_mask, 206.2ms
Speed: 2.6ms preprocess, 206.2ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
76it [00:22,  4.46it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-742.png: 640x512 1 with_mask, 233.0ms
Speed: 8.2ms preprocess, 233.0ms inference, 2.7ms postprocess per image at shape (1, 3, 640, 512)
77it [00:22,  4.23it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-745.png: 640x512 1 with_mask, 317.3ms
Speed: 8.0ms preprocess, 317.3ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 512)
78it [00:22,  3.71it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-765.png: 640x512 1 with_mask, 329.8ms
Speed: 5.8ms preprocess, 329.8ms inference, 0.7ms postprocess per image at shape (1, 3, 640, 512)
79it [00:23,  3.38it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-793.png: 640x512 1 with_mask, 232.5ms
Speed: 11.2ms preprocess, 232.5ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 512)
80it [00:23,  3.48it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-796.png: 448x640 12 with_masks, 185.0ms
Speed: 4.4ms preprocess, 185.0ms inference, 8.2ms postprocess per image at shape (1, 3, 448, 640)
81it [00:23,  3.76it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-802.png: 448x640 4 with_masks, 190.2ms
Speed: 3.0ms preprocess, 190.2ms inference, 2.0ms postprocess per image at shape (1, 3, 448, 640)
82it [00:23,  4.01it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-819.png: 384x640 1 with_mask, 177.0ms
Speed: 4.0ms preprocess, 177.0ms inference, 1.6ms postprocess per image at shape (1, 3, 384, 640)
83it [00:23,  4.27it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-829.png: 320x640 6 with_masks, 1 without_mask, 203.6ms
Speed: 3.2ms preprocess, 203.6ms inference, 2.0ms postprocess per image at shape (1, 3, 320, 640)
84it [00:24,  4.30it/s]
image 1/1 F:\ECMM426_CV_Workshop\Marking\Template\data\MaskedFace\val\images\mask-833.png: 384x640 2 with_masks, 178.7ms
Speed: 7.6ms preprocess, 178.7ms inference, 2.1ms postprocess per image at shape (1, 3, 384, 640)
85it [00:24,  3.49it/s]

MAPE

In [10]:
def compute_mape(prediction, truth):
    mape = np.mean( np.abs(truth - prediction) / np.maximum(truth, np.ones_like(truth)) ) * 100
    return mape
In [11]:
# X2d0f9f39
# predicted_counts[:, [0, 1]] = predicted_counts[:, [1, 0]]
In [12]:
predicted_counts[:, [1, 2]] = predicted_counts[:, [2, 1]]
In [13]:
MAPE = compute_mape(predicted_counts, gt_counts)
In [14]:
print(MAPE)
16.415378480087284

Final Score

In [15]:
if MAPE <= 10:
    print("Score: ", 25*1.0)
elif MAPE <= 15:
    print("Score: ", 25*0.875)
elif MAPE <= 20:
    print("Score: ", 25*0.75)
elif MAPE <= 25:
    print("Score: ", 25*0.625)
elif MAPE <= 30:
    print("Score: ", 25*0.5)
else:
    print("Score: ", 0)
Score:  18.75
In [ ]:

</html>