Image Classification
Transformers
Safetensors
cetaceanet
biology
biodiversity
custom_code
TristanGollmart commited on
Commit
db195b0
·
verified ·
1 Parent(s): 0f9c15e

Upload model

Browse files
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
  library_name: transformers
3
  tags: []
4
-
5
  co2_eq_emissions:
6
  emissions: 240
7
- source: "https://calculator.green-algorithms.org/"
8
- training_type: "pre-training"
9
- geographical_location: "Switzerland"
10
- hardware_used: "1 v100 GPU"
11
  ---
12
 
13
  # Model Card for Model ID
 
1
  ---
2
  library_name: transformers
3
  tags: []
 
4
  co2_eq_emissions:
5
  emissions: 240
6
+ source: https://calculator.green-algorithms.org/
7
+ training_type: pre-training
8
+ geographical_location: Switzerland
9
+ hardware_used: 1 v100 GPU
10
  ---
11
 
12
  # Model Card for Model ID
config.json CHANGED
@@ -1,85 +1,85 @@
1
- {
2
- "architectures": [
3
- "CetaceanClassifierModelForImageClassification"
4
- ],
5
- "aug": {
6
- "crop_l": 0.75,
7
- "crop_r": 1.3333333333333333,
8
- "crop_scale": 0.9,
9
- "p_affine": 0.5,
10
- "p_blur": 0.05,
11
- "p_bright_contrast": 0.5,
12
- "p_cutout": 0.05,
13
- "p_downscale": 0.0,
14
- "p_gray": 0.1,
15
- "p_noise": 0.05,
16
- "p_posterize": 0.2,
17
- "p_rain": 0.05,
18
- "p_shuffle": 0.3,
19
- "p_snow": 0.1,
20
- "rotate": 15,
21
- "shear": 3,
22
- "translate": 0.25
23
- },
24
- "auto_map": {
25
- "AutoConfig": "configuration_cetacean_classifier.CetaceanClassifierConfig",
26
- "AutoModelForImageClassification": "modeling_cetacean_classifier.CetaceanClassifierModelForImageClassification"
27
- },
28
- "batch_size": 8,
29
- "bbox_conf_threshold": 0.01,
30
- "bboxes": {
31
- "backfin": 0.15,
32
- "detic": 0.05,
33
- "fullbody": 0.6,
34
- "fullbody_charm": 0.15,
35
- "none": 0.05
36
- },
37
- "global_pool": {
38
- "arch": "GeM",
39
- "p": 3,
40
- "train": false
41
- },
42
- "image_size": [
43
- 768,
44
- 768
45
- ],
46
- "loss_fn": "CrossEntropy",
47
- "loss_id_ratio": 0.437338,
48
- "lr_backbone": 0.0016,
49
- "lr_decay_scale": 0.01,
50
- "lr_head": 0.016,
51
- "margin_coef_id": 0.27126,
52
- "margin_coef_species": 0.226253,
53
- "margin_cons_id": 0.05,
54
- "margin_cons_species": 0.05,
55
- "margin_power_id": -0.364399,
56
- "margin_power_species": -0.720133,
57
- "max_epochs": 30,
58
- "model_name": "tf_efficientnetv2_m",
59
- "model_type": "cetaceanet",
60
- "n_center_id": 2,
61
- "n_center_species": 2,
62
- "n_data": -1,
63
- "n_splits": -1,
64
- "normalization": "batchnorm",
65
- "num_classes": 15587,
66
- "num_species_classes": 26,
67
- "optimizer": "AdamW",
68
- "out_indices": [
69
- 3,
70
- 4
71
- ],
72
- "pretrained": true,
73
- "pseudo_conf_threshold": 0.6,
74
- "pseudo_label": "pseudo_labels/round2.csv",
75
- "s_id": 20.9588,
76
- "s_species": 33.1383,
77
- "test_bboxes": [
78
- "fullbody",
79
- "fullbody_charm"
80
- ],
81
- "torch_dtype": "float32",
82
- "transformers_version": "4.46.0",
83
- "val_bbox": "fullbody",
84
- "warmup_steps_ratio": 0.2
85
- }
 
1
+ {
2
+ "architectures": [
3
+ "CetaceanClassifierModelForImageClassification"
4
+ ],
5
+ "aug": {
6
+ "crop_l": 0.75,
7
+ "crop_r": 1.3333333333333333,
8
+ "crop_scale": 0.9,
9
+ "p_affine": 0.5,
10
+ "p_blur": 0.05,
11
+ "p_bright_contrast": 0.5,
12
+ "p_cutout": 0.05,
13
+ "p_downscale": 0.0,
14
+ "p_gray": 0.1,
15
+ "p_noise": 0.05,
16
+ "p_posterize": 0.2,
17
+ "p_rain": 0.05,
18
+ "p_shuffle": 0.3,
19
+ "p_snow": 0.1,
20
+ "rotate": 15,
21
+ "shear": 3,
22
+ "translate": 0.25
23
+ },
24
+ "auto_map": {
25
+ "AutoConfig": "configuration_cetacean_classifier.CetaceanClassifierConfig",
26
+ "AutoModelForImageClassification": "modeling_cetacean_classifier.CetaceanClassifierModelForImageClassification"
27
+ },
28
+ "batch_size": 8,
29
+ "bbox_conf_threshold": 0.01,
30
+ "bboxes": {
31
+ "backfin": 0.15,
32
+ "detic": 0.05,
33
+ "fullbody": 0.6,
34
+ "fullbody_charm": 0.15,
35
+ "none": 0.05
36
+ },
37
+ "global_pool": {
38
+ "arch": "GeM",
39
+ "p": 3,
40
+ "train": false
41
+ },
42
+ "image_size": [
43
+ 768,
44
+ 768
45
+ ],
46
+ "loss_fn": "CrossEntropy",
47
+ "loss_id_ratio": 0.437338,
48
+ "lr_backbone": 0.0016,
49
+ "lr_decay_scale": 0.01,
50
+ "lr_head": 0.016,
51
+ "margin_coef_id": 0.27126,
52
+ "margin_coef_species": 0.226253,
53
+ "margin_cons_id": 0.05,
54
+ "margin_cons_species": 0.05,
55
+ "margin_power_id": -0.364399,
56
+ "margin_power_species": -0.720133,
57
+ "max_epochs": 30,
58
+ "model_name": "tf_efficientnetv2_m",
59
+ "model_type": "cetaceanet",
60
+ "n_center_id": 2,
61
+ "n_center_species": 2,
62
+ "n_data": -1,
63
+ "n_splits": -1,
64
+ "normalization": "batchnorm",
65
+ "num_classes": 15587,
66
+ "num_species_classes": 26,
67
+ "optimizer": "AdamW",
68
+ "out_indices": [
69
+ 3,
70
+ 4
71
+ ],
72
+ "pretrained": true,
73
+ "pseudo_conf_threshold": 0.6,
74
+ "pseudo_label": "pseudo_labels/round2.csv",
75
+ "s_id": 20.9588,
76
+ "s_species": 33.1383,
77
+ "test_bboxes": [
78
+ "fullbody",
79
+ "fullbody_charm"
80
+ ],
81
+ "torch_dtype": "float32",
82
+ "transformers_version": "4.46.0",
83
+ "val_bbox": "fullbody",
84
+ "warmup_steps_ratio": 0.2
85
+ }
configuration_cetacean_classifier.py CHANGED
@@ -1,12 +1,12 @@
1
- from transformers import PretrainedConfig
2
- from typing import List
3
-
4
-
5
- class CetaceanClassifierConfig(PretrainedConfig):
6
- model_type = "cetaceanet"
7
-
8
- def __init__(
9
- self,
10
- **kwargs,
11
- ):
12
- super().__init__(**kwargs)
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+
5
+ class CetaceanClassifierConfig(PretrainedConfig):
6
+ model_type = "cetaceanet"
7
+
8
+ def __init__(
9
+ self,
10
+ **kwargs,
11
+ ):
12
+ super().__init__(**kwargs)
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:7c9afc61a269bf406f5b23389c57e4efe365eb4b67aa62730b731916fb62b6f0
3
  size 296028464
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:faf081f38c0224790f5819d763475e9827667fa50af1a86a61e233c52876ae11
3
  size 296028464
modeling_cetacean_classifier.py CHANGED
@@ -1,71 +1,91 @@
1
- from transformers import PreTrainedModel
2
- from PIL import Image
3
- import numpy as np
4
- import torch
5
-
6
- from .configuration_cetacean_classifier import CetaceanClassifierConfig
7
- from .train import SphereClassifier
8
-
9
-
10
- WHALE_CLASSES = np.array(
11
- [
12
- "beluga",
13
- "blue_whale",
14
- "bottlenose_dolphin",
15
- "brydes_whale",
16
- "commersons_dolphin",
17
- "common_dolphin",
18
- "cuviers_beaked_whale",
19
- "dusky_dolphin",
20
- "false_killer_whale",
21
- "fin_whale",
22
- "frasiers_dolphin",
23
- "gray_whale",
24
- "humpback_whale",
25
- "killer_whale",
26
- "long_finned_pilot_whale",
27
- "melon_headed_whale",
28
- "minke_whale",
29
- "pantropic_spotted_dolphin",
30
- "pygmy_killer_whale",
31
- "rough_toothed_dolphin",
32
- "sei_whale",
33
- "short_finned_pilot_whale",
34
- "southern_right_whale",
35
- "spinner_dolphin",
36
- "spotted_dolphin",
37
- "white_sided_dolphin",
38
- ]
39
- )
40
-
41
-
42
- class CetaceanClassifierModelForImageClassification(PreTrainedModel):
43
- config_class = CetaceanClassifierConfig
44
-
45
- def __init__(self, config):
46
- super().__init__(config)
47
-
48
- self.model = SphereClassifier(cfg=config.to_dict())
49
-
50
- # load_from_checkpoint("cetacean_classifier/last.ckpt")
51
- # self.model = SphereClassifier.load_from_checkpoint("cetacean_classifier/last.ckpt")
52
-
53
- self.model.eval()
54
-
55
- def preprocess_image(self, img: Image) -> torch.Tensor:
56
- image_resized = img.resize((480, 480))
57
- image_resized = np.array(image_resized)[None]
58
- image_resized = np.transpose(image_resized, [0, 3, 2, 1])
59
- image_tensor = torch.Tensor(image_resized)
60
- return image_tensor
61
-
62
- def forward(self, img: Image, labels=None):
63
- tensor = self.preprocess_image(img)
64
- head_id_logits, head_species_logits = self.model(tensor)
65
- head_species_logits = head_species_logits.detach().numpy()
66
- sorted_idx = head_species_logits.argsort()[0]
67
- sorted_idx = np.array(list(reversed(sorted_idx)))
68
- top_three_logits = sorted_idx[:3]
69
- top_three_whale_preds = WHALE_CLASSES[top_three_logits]
70
-
71
- return {"predictions": top_three_whale_preds}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import albumentations as A
2
+ from transformers import PreTrainedModel
3
+ from PIL import Image
4
+ import numpy as np
5
+ import torch
6
+ import cv2
7
+ from configuration_cetacean_classifier import CetaceanClassifierConfig
8
+ from train import SphereClassifier
9
+
10
+
11
+ WHALE_CLASSES = np.array(
12
+ [
13
+ "beluga",
14
+ "blue_whale",
15
+ "bottlenose_dolphin",
16
+ "brydes_whale",
17
+ "commersons_dolphin",
18
+ "common_dolphin",
19
+ "cuviers_beaked_whale",
20
+ "dusky_dolphin",
21
+ "false_killer_whale",
22
+ "fin_whale",
23
+ "frasiers_dolphin",
24
+ "gray_whale",
25
+ "humpback_whale",
26
+ "killer_whale",
27
+ "long_finned_pilot_whale",
28
+ "melon_headed_whale",
29
+ "minke_whale",
30
+ "pantropic_spotted_dolphin",
31
+ "pygmy_killer_whale",
32
+ "rough_toothed_dolphin",
33
+ "sei_whale",
34
+ "short_finned_pilot_whale",
35
+ "southern_right_whale",
36
+ "spinner_dolphin",
37
+ "spotted_dolphin",
38
+ "white_sided_dolphin",
39
+ ]
40
+ )
41
+
42
+
43
+ class CetaceanClassifierModelForImageClassification(PreTrainedModel):
44
+ config_class = CetaceanClassifierConfig
45
+
46
+ def __init__(self, config):
47
+ super().__init__(config)
48
+
49
+ self.model = SphereClassifier(cfg=config.to_dict())
50
+
51
+ # load_from_checkpoint("cetacean_classifier/last.ckpt")
52
+ # self.model = SphereClassifier.load_from_checkpoint("cetacean_classifier/last.ckpt")
53
+
54
+ self.model.eval()
55
+ self.config = config
56
+ self.transforms = self.make_transforms(data_aug=True)
57
+
58
+ def make_transforms(self, data_aug: bool):
59
+ augments = []
60
+ if data_aug:
61
+ aug = self.config.aug
62
+ augments = [
63
+ A.RandomResizedCrop(
64
+ self.config.image_size[0],
65
+ self.config.image_size[1],
66
+ scale=(aug["crop_scale"], 1.0),
67
+ ratio=(aug["crop_l"], aug["crop_r"]),
68
+ ),]
69
+ return A.Compose(augments)
70
+
71
+ def preprocess_image(self, img) -> torch.Tensor:
72
+ rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
73
+ image = cv2.resize(rgb, self.config.image_size, interpolation=cv2.INTER_CUBIC)
74
+ image = self.transforms(image=image)["image"]
75
+ return torch.Tensor(image).transpose(2, 0).unsqueeze(0)
76
+ #image_resized = img.resize((480, 480))
77
+ #image_resized = np.array(image_resized)[None]
78
+ #image_resized = np.transpose(image_resized, [0, 3, 2, 1])
79
+ #image_tensor = torch.Tensor(image_resized)
80
+ #return image_tensor
81
+
82
+ def forward(self, img, labels=None):
83
+ tensor = self.preprocess_image(img)
84
+ head_id_logits, head_species_logits = self.model(tensor)
85
+ head_species_logits = head_species_logits.detach().numpy()
86
+ sorted_idx = head_species_logits.argsort()[0]
87
+ sorted_idx = np.array(list(reversed(sorted_idx)))
88
+ top_three_logits = sorted_idx[:3]
89
+ top_three_whale_preds = WHALE_CLASSES[top_three_logits]
90
+
91
+ return {"predictions": top_three_whale_preds}