chriamue commited on
Commit
e391a54
1 Parent(s): c4be52f

adds train script

Browse files
Files changed (1) hide show
  1. train.py +100 -0
train.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from datasets import load_dataset
3
+ import evaluate
4
+ from transformers import EfficientNetImageProcessor, EfficientNetForImageClassification, TrainingArguments, Trainer
5
+ import numpy as np
6
+
7
+ print("Cuda availability:", torch.cuda.is_available())
8
+ cuda = torch.device('cuda') # Default HIP device
9
+ print("cuda: ", torch.cuda.get_device_name(device=cuda))
10
+
11
+ dataset = load_dataset("chriamue/bird-species-dataset")
12
+
13
+ model_name = "google/efficientnet-b2"
14
+ finetuned_model_name = "chriamue/bird-species-classifier"
15
+
16
+ #####
17
+ labels = dataset["train"].features["label"].names
18
+ label2id, id2label = dict(), dict()
19
+ for i, label in enumerate(labels):
20
+ label2id[label] = str(i)
21
+ id2label[str(i)] = label
22
+
23
+ preprocessor = EfficientNetImageProcessor.from_pretrained(model_name)
24
+ model = EfficientNetForImageClassification.from_pretrained(model_name, num_labels=len(
25
+ labels), id2label=id2label, label2id=label2id, ignore_mismatched_sizes=True)
26
+
27
+
28
+ training_args = TrainingArguments(
29
+ finetuned_model_name, remove_unused_columns=False,
30
+ evaluation_strategy="epoch",
31
+ save_strategy="epoch",
32
+ learning_rate=5e-5,
33
+ per_device_train_batch_size=32,
34
+ per_device_eval_batch_size=16,
35
+ num_train_epochs=1,
36
+ weight_decay=0.01,
37
+ load_best_model_at_end=True,
38
+ metric_for_best_model="accuracy"
39
+ )
40
+
41
+ metric = evaluate.load("accuracy")
42
+
43
+
44
+ def compute_metrics(eval_pred):
45
+ predictions, labels = eval_pred
46
+ predictions = np.argmax(predictions, axis=1)
47
+ return metric.compute(predictions=predictions, references=labels)
48
+
49
+
50
+ def transforms(examples):
51
+ pixel_values = [preprocessor(image, return_tensors="pt").pixel_values.squeeze(
52
+ 0) for image in examples["image"]]
53
+ examples["pixel_values"] = pixel_values
54
+ return examples
55
+
56
+
57
+ image = dataset["train"][0]["image"]
58
+
59
+ dataset["train"] = dataset["train"].shuffle(seed=42).select(range(1500))
60
+ # dataset["validation"] = dataset["validation"].select(range(100))
61
+ # dataset["test"] = dataset["test"].select(range(100))
62
+
63
+ dataset = dataset.map(transforms, remove_columns=["image"], batched=True)
64
+
65
+ trainer = Trainer(
66
+ model=model,
67
+ args=training_args,
68
+ train_dataset=dataset["train"],
69
+ eval_dataset=dataset["validation"],
70
+ compute_metrics=compute_metrics,
71
+ )
72
+
73
+ train_results = trainer.train(resume_from_checkpoint=True)
74
+
75
+ trainer.evaluate()
76
+
77
+ # trainer.save_model("test_trainer")
78
+
79
+ # trainer.save_model()
80
+ trainer.log_metrics("train", train_results.metrics)
81
+ # trainer.save_metrics("train", train_results.metrics)
82
+ # trainer.save_state()
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, export_params=True, opset_version=13, do_constant_folding=True, input_names=['input'], output_names=['output'], dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}})
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
+