Views
No views yet


384pxmivolo librarypip install git+https://github.com/WildChlamydia/MiVOLO.git1from transformers import AutoModelForImageClassification, AutoConfig, AutoImageProcessor
2import torch
3import cv2
4import numpy as np
5import requests
6
7# load model and image processor
8config = AutoConfig.from_pretrained(
9 "iitolstykh/mivolo_v2", trust_remote_code=True
10)
11mivolo_model = AutoModelForImageClassification.from_pretrained(
12 "iitolstykh/mivolo_v2", trust_remote_code=True, torch_dtype=torch.float16
13)
14image_processor = AutoImageProcessor.from_pretrained(
15 "iitolstykh/mivolo_v2", trust_remote_code=True
16)
17
18# download test image
19resp = requests.get('https://variety.com/wp-content/uploads/2023/04/MCDNOHA_SP001.jpg')
20arr = np.asarray(bytearray(resp.content), dtype=np.uint8)
21image = cv2.imdecode(arr, -1)
22
23# face crops
24x1, y1, x2, y2 = [625, 46, 686, 121]
25faces_crops = [image[y1:y2, x1:x2]] # may be [None] if bodies_crops is not None
26
27# body crops
28x1, y1, x2, y2 = [534, 16, 790, 559]
29bodies_crops = [image[y1:y2, x1:x2]] # may be [None] if faces_crops is not None
30
31# prepare BGR inputs
32faces_input = image_processor(images=faces_crops)["pixel_values"]
33body_input = image_processor(images=bodies_crops)["pixel_values"]
34
35faces_input = faces_input.to(dtype=mivolo_model.dtype, device=mivolo_model.device)
36body_input = body_input.to(dtype=mivolo_model.dtype, device=mivolo_model.device)
37
38# inference
39output = mivolo_model(faces_input=faces_input, body_input=body_input)
40
41# print results
42age = output.age_output[0].item()
43print(f"age: {round(age, 2)}")
44
45id2label = config.gender_id2label
46gender = id2label[output.gender_class_idx[0].item()]
47gender_prob = output.gender_probs[0].item()
48print(f"gender: {gender} [{int(gender_prob * 100)}%]")
49| Model | Test Dataset | Age Accuracy | Gender Accuracy |
|---|---|---|---|
| mivolov2_384x384 (fp16) | Adience | 70.2 | 97.3 |
1@article{mivolo2023,
2 Author = {Maksim Kuprashevich and Irina Tolstykh},
3 Title = {MiVOLO: Multi-input Transformer for Age and Gender Estimation},
4 Year = {2023},
5 Eprint = {arXiv:2307.04616},
6}1@article{mivolo2024,
2 Author = {Maksim Kuprashevich and Grigorii Alekseenko and Irina Tolstykh},
3 Title = {Beyond Specialization: Assessing the Capabilities of MLLMs in Age and Gender Estimation},
4 Year = {2024},
5 Eprint = {arXiv:2403.02302},
6}