adds evaluate script
Browse files- evaluate_model.py +35 -0
- train.py +5 -13
evaluate_model.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import urllib.request
|
3 |
+
from PIL import Image
|
4 |
+
from datasets import load_dataset
|
5 |
+
from transformers import EfficientNetImageProcessor, EfficientNetForImageClassification
|
6 |
+
|
7 |
+
dataset = load_dataset("chriamue/bird-species-dataset")
|
8 |
+
|
9 |
+
#####
|
10 |
+
labels = dataset["test"].features["label"].names
|
11 |
+
label2id, id2label = dict(), dict()
|
12 |
+
for i, label in enumerate(labels):
|
13 |
+
label2id[label] = str(i)
|
14 |
+
id2label[str(i)] = label
|
15 |
+
|
16 |
+
preprocessor = EfficientNetImageProcessor.from_pretrained("google/efficientnet-b2")
|
17 |
+
model = EfficientNetForImageClassification.from_pretrained("chriamue/bird-species-classifier", num_labels=len(
|
18 |
+
labels), id2label=id2label, label2id=label2id, ignore_mismatched_sizes=True)
|
19 |
+
|
20 |
+
image = dataset["validation"][0]["image"]
|
21 |
+
|
22 |
+
url = 'https://upload.wikimedia.org/wikipedia/commons/a/a9/Common_Blackbird.jpg'
|
23 |
+
image = Image.open(urllib.request.urlretrieve(url)[0])
|
24 |
+
|
25 |
+
|
26 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
27 |
+
model = model.to(device)
|
28 |
+
|
29 |
+
inputs = preprocessor(image, return_tensors="pt")
|
30 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
31 |
+
|
32 |
+
with torch.no_grad():
|
33 |
+
logits = model(**inputs).logits
|
34 |
+
predicted_label = logits.argmax(-1).item()
|
35 |
+
print(labels[predicted_label])
|
train.py
CHANGED
@@ -72,29 +72,21 @@ trainer = Trainer(
|
|
72 |
|
73 |
train_results = trainer.train(resume_from_checkpoint=True)
|
74 |
|
75 |
-
trainer.evaluate()
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
# trainer.save_model()
|
80 |
trainer.log_metrics("train", train_results.metrics)
|
81 |
-
|
82 |
-
|
83 |
|
84 |
dummy_input = torch.randn(1, 3, 224, 224)
|
85 |
model = model.to('cpu')
|
86 |
output_onnx_path = 'model.onnx'
|
87 |
-
torch.onnx.export(model, dummy_input, output_onnx_path,
|
88 |
-
|
89 |
-
|
90 |
-
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
91 |
-
#model = model.to(device)
|
92 |
|
93 |
inputs = preprocessor(image, return_tensors="pt")
|
94 |
-
#inputs = {k: v.to(device) for k, v in inputs.items()}
|
95 |
|
96 |
with torch.no_grad():
|
97 |
logits = model(**inputs).logits
|
98 |
predicted_label = logits.argmax(-1).item()
|
99 |
print(labels[predicted_label])
|
100 |
-
|
|
|
72 |
|
73 |
train_results = trainer.train(resume_from_checkpoint=True)
|
74 |
|
75 |
+
print(trainer.evaluate())
|
76 |
|
77 |
+
trainer.save_model()
|
|
|
|
|
78 |
trainer.log_metrics("train", train_results.metrics)
|
79 |
+
trainer.save_metrics("train", train_results.metrics)
|
80 |
+
trainer.save_state()
|
81 |
|
82 |
dummy_input = torch.randn(1, 3, 224, 224)
|
83 |
model = model.to('cpu')
|
84 |
output_onnx_path = 'model.onnx'
|
85 |
+
torch.onnx.export(model, dummy_input, output_onnx_path, opset_version=13)
|
|
|
|
|
|
|
|
|
86 |
|
87 |
inputs = preprocessor(image, return_tensors="pt")
|
|
|
88 |
|
89 |
with torch.no_grad():
|
90 |
logits = model(**inputs).logits
|
91 |
predicted_label = logits.argmax(-1).item()
|
92 |
print(labels[predicted_label])
|
|