Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
Β·
b2a8272
1
Parent(s):
d4a9766
dev: added development snippet for evaluating the model on several images
Browse files- dev/call_hf_batch.py +94 -0
dev/call_hf_batch.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from huggingface_hub import HfApi
|
| 3 |
+
import cv2
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from transformers import AutoModelForImageClassification
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
'''
|
| 12 |
+
how to use this script:
|
| 13 |
+
1. get data from the kaggle competition, including images and the train.csv file
|
| 14 |
+
edit the "base" variable, assuming the following layout
|
| 15 |
+
|
| 16 |
+
ceteans/
|
| 17 |
+
βββ images
|
| 18 |
+
βΒ Β βββ 00021adfb725ed.jpg
|
| 19 |
+
βΒ Β βββ 000562241d384d.jpg
|
| 20 |
+
βΒ Β βββ ...
|
| 21 |
+
βββ train.csv
|
| 22 |
+
|
| 23 |
+
2. inspect the df_results dataframe to see how the model is performing
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
'''
|
| 27 |
+
# setup for the ML model on huggingface (our wrapper)
|
| 28 |
+
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
|
| 29 |
+
rev = 'main'
|
| 30 |
+
|
| 31 |
+
# load the model
|
| 32 |
+
cetacean_classifier = AutoModelForImageClassification.from_pretrained(
|
| 33 |
+
"Saving-Willy/cetacean-classifier",
|
| 34 |
+
revision=rev,
|
| 35 |
+
trust_remote_code=True)
|
| 36 |
+
|
| 37 |
+
# get ready to load images
|
| 38 |
+
base = Path('~/Documents/ceteans/').expanduser()
|
| 39 |
+
df = pd.read_csv(base / 'train.csv')
|
| 40 |
+
|
| 41 |
+
i_max = 100 # put a limit on the number of images to classify in this test (or None)
|
| 42 |
+
|
| 43 |
+
# for each file in the folder base/images, 1/ load image, 2/ classify, 3/ compare against the relevant row in df
|
| 44 |
+
# also keep track of the time it takes to classify each image
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
classifications = []
|
| 48 |
+
|
| 49 |
+
img_pth = base / 'images'
|
| 50 |
+
img_files = list(img_pth.glob('*.jpg'))
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
for i, img_file in enumerate(img_files):
|
| 54 |
+
# lets check we can get the right target.
|
| 55 |
+
img_id = img_file.name # includes .jpg
|
| 56 |
+
target = df.loc[df['image'] == img_id, 'species'].item()
|
| 57 |
+
#print(img_id, target)
|
| 58 |
+
|
| 59 |
+
start_time = time.time()
|
| 60 |
+
image = cv2.imread(str(img_file))
|
| 61 |
+
load_time = time.time() - start_time
|
| 62 |
+
|
| 63 |
+
start_time = time.time()
|
| 64 |
+
out = cetacean_classifier(image) # get top 3 matches
|
| 65 |
+
classify_time = time.time() - start_time
|
| 66 |
+
|
| 67 |
+
whale_prediction1 = out['predictions'][0]
|
| 68 |
+
|
| 69 |
+
# comparison
|
| 70 |
+
ok = whale_prediction1 == target
|
| 71 |
+
any = target in [x for x in out['predictions']]
|
| 72 |
+
row = [img_id, target, ok, any, load_time, classify_time] + list(out['predictions'])
|
| 73 |
+
|
| 74 |
+
print(i, row)
|
| 75 |
+
|
| 76 |
+
classifications.append(row)
|
| 77 |
+
|
| 78 |
+
if i_max is not None and i >= i_max:
|
| 79 |
+
break
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
df_results = pd.DataFrame(classifications, columns=['img_id', 'target', 'ok', 'any', 'load_time', 'classify_time'] + [f'pred_{i}' for i in range(3)])
|
| 83 |
+
|
| 84 |
+
# print out a few summary stats
|
| 85 |
+
# mean time to load and classify (formatted 3dp), +- std dev (formatted to 2dp),
|
| 86 |
+
print(f"Mean load time: {df_results['load_time'].mean():.3f} +- {df_results['load_time'].std():.2f} s")
|
| 87 |
+
print(f"Mean classify time: {df_results['classify_time'].mean():.3f} +- {df_results['classify_time'].std():.2f} s")
|
| 88 |
+
|
| 89 |
+
# accuracy: count of ok / count of any
|
| 90 |
+
print(f"Accuracy: correct with top prediction: {df_results['ok'].sum()} | any of top 3 correct: {df_results['any'].sum():.3f} (of total {df_results.shape[0]})")
|
| 91 |
+
|
| 92 |
+
# diversity: is the model just predicting one class for everything it sees?
|
| 93 |
+
print("Which classes are predicted?")
|
| 94 |
+
print(df_results.pred_0.value_counts())
|