<jupyter_start><jupyter_text>Load the model<jupyter_code>detector = Detector(MODEL_PATH, visible_device_list='0')<jupyter_output><empty_output><jupyter_text>Get an image<jupyter_code>names = os.listdir(IMAGES_FOLDER)
n = random.choice(names)
path = os.path.join(IMAGES_FOLDER, n)

image = Image.open(path)
image = image.resize((640, 640))
image<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>image_array = np.array(image)
outputs = detector(image_array, score_threshold=0.0001)<jupyter_output><empty_output><jupyter_text>Show detections<jupyter_code>EDGES = [
    (0, 1), (0, 2),
    (1, 3), (2, 4),
    (5, 7), (7, 9), (6, 8), (8, 10),
    (11, 13), (13, 15), (12, 14), (14, 16),
    (3, 5), (4, 6),
    (5, 11), (6, 12)
]
outputs['keypoint_scores']
def draw_everything(image, outputs):

    image_copy = image.copy()
    image_copy.putalpha(255)
    draw = ImageDraw.Draw(image_copy, 'RGBA')
    width, height = image_copy.size
    
    scaler = np.array([height, width, height, width])
    boxes = scaler * outputs['boxes']

    for i, box in enumerate(boxes):
        
        ymin, xmin, ymax, xmax = box
        draw.rectangle([(xmin, ymin), (xmax, ymax)], outline='red')
       
        keypoints = outputs['keypoint_positions'][i]
        keypoints = keypoints[:, [1, 0]].copy()
        keypoints *= np.array([xmax - xmin, ymax - ymin])
        keypoints += np.array([xmin, ymin])
        visibility = np.ones([17, 1], dtype=np.float32)
        keypoints = np.concatenate([keypoints, visibility], axis=1)

        for (p, q) in EDGES:

            x1, y1, v1 = keypoints[p]
            x2, y2, v2 = keypoints[q]

            both_visible = v1 > 0 and v2 > 0
            if both_visible:
                draw.line([(x1, y1), (x2, y2)])

        for j in range(17):
            x, y, v = keypoints[j]
            if v > 0:
                s = 2
                draw.ellipse([
                    (x - s, y - s),
                    (x + s, y + s)
                ], fill='red')

    return image_copy
draw_everything(image, outputs)
segmentation_masks = outputs['segmentation_masks'].copy()
m = segmentation_masks.min()
M = segmentation_masks.max()
segmentation_masks = 255.0 * (segmentation_masks - m)/(M - m)
segmentation_masks = Image.fromarray(segmentation_masks.astype('uint8'))
segmentation_masks<jupyter_output><empty_output><jupyter_text>Measure speed<jupyter_code>times = []
for _ in range(110):
    start = time.perf_counter()
    result = detector(image_array, score_threshold=0.25)
    times.append(time.perf_counter() - start)
    
times = np.array(times)
times = times[10:]
print(times.mean(), times.std())<jupyter_output><empty_output><jupyter_text>Show heatmaps<jupyter_code>import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

cmap = plt.cm.get_cmap('autumn')
new_cmap = cmap(np.arange(cmap.N))
new_cmap[:, -1] = np.sqrt(np.linspace(0, 1, cmap.N))  # set alpha
cmap = ListedColormap(new_cmap)  # create new colormap
ORDER = {
    0: 'nose',
    1: 'left eye', 2: 'right eye',
    3: 'left ear', 4: 'right ear',
    5: 'left shoulder', 6: 'right shoulder',
    7: 'left elbow', 8: 'right elbow',
    9: 'left wrist', 10: 'right wrist',
    11: 'left hip', 12: 'right hip',
    13: 'left knee', 14: 'right knee',
    15: 'left ankle', 16: 'right ankle'
}


def plot_maps(image, heatmaps, segmentation_mask):
    """
    Arguments:
        image: a float numpy array with shape [h, w, 3].
        heatmaps: a float numpy array with shape [h / 4, w / 4, 17].
        segmentation_mask: a float numpy array with shape [h / 4, w / 4].
        loss_mask: a float numpy array with shape [h / 4, w / 4].
    """

    h, w, _ = image.shape
    h, w = (h // 2), (w // 2)
    background = Image.new('RGBA', (w, h * 18), (255, 255, 255, 255))
    draw = ImageDraw.Draw(background, 'RGBA')
    
    image = Image.fromarray(image)
    image = image.resize((w, h), Image.LANCZOS)
    image.putalpha(255)

    heatmaps = (255 * cmap(heatmaps)).astype('uint8')
    # it has shape [h, w, 17, 4]
    
    heats = []
    for j, name in ORDER.items():

        heat = Image.fromarray(heatmaps[:, :, j])
        heat = heat.resize((w, h), Image.LANCZOS)
        heat = Image.alpha_composite(image, heat)
        background.paste(heat, (0, j * h))
        draw.text((0, j * h), name, fill='red')
        
    def draw_mask(mask):
        mask = np.clip(mask, 0.0, 1.0)
        mask = (255 * mask).astype('uint8')
        mask = Image.fromarray(mask)
        mask = mask.resize((w, h), Image.LANCZOS).convert('RGB')
        mask.putalpha(mask.convert('L'))
        mask = Image.alpha_composite(image, mask)
        return mask
    
    mask = draw_mask(segmentation_mask)
    background.paste(mask, (0, 17 * h))
    draw.text((0, 17 * h), 'segmentation mask', fill='red')

    return background
h = outputs['keypoint_heatmaps']
m = h.min(0).min(0)
M = h.max(0).max(0)
h = (h - m)/(M - m)

plot_maps(image_array, h, outputs['segmentation_masks'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1589232303090728, 'fraction_numerical': 0.03569292123629113, 'mean_word_length': 2.9341176470588235, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '619907', 'license': 'MIT', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1832, 'n_words': 432, 'path': 'inference/predict.ipynb', 'repo_name': 'TropComplique/MultiPoseNet', 'size': '5015'}