airlabshare commited on
Commit
6feb200
·
verified ·
1 Parent(s): 7112a5d

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +42 -0
model.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from transformers import PreTrainedModel, Dinov2Model, Dinov2Config
5
+
6
+ class AnyThermalConfig(Dinov2Config):
7
+ model_type = "anythermal"
8
+
9
+ class AnyThermalSegmentationModel(PreTrainedModel):
10
+ config_class = AnyThermalConfig
11
+
12
+ def __init__(self, config):
13
+ super().__init__(config)
14
+ # Initialize the backbone with DINOv2 architecture [cite: 100, 104]
15
+ self.backbone = Dinov2Model(config)
16
+
17
+ # Build the segmentation head based on your non-linear-64 architecture [cite: 130]
18
+ # num_labels is defined in the subfolder config.json
19
+ self.head = nn.Sequential(
20
+ nn.Conv2d(config.hidden_size, 64, kernel_size=3, padding=1),
21
+ nn.ReLU(inplace=True),
22
+ nn.Conv2d(64, config.num_labels, kernel_size=1)
23
+ )
24
+ self.post_init()
25
+
26
+ def forward(self, pixel_values, **kwargs):
27
+ # Extract features [cite: 109, 131]
28
+ outputs = self.backbone(pixel_values, **kwargs)
29
+
30
+ # Use patch tokens (skipping CLS token at 0) for segmentation [cite: 109, 131]
31
+ features = outputs.last_hidden_state[:, 1:, :]
32
+ B, L, C = features.shape
33
+ H = W = int(L**0.5)
34
+ features = features.permute(0, 2, 1).reshape(B, C, H, W)
35
+
36
+ logits = self.head(features)
37
+
38
+ # Upscale to original resolution (14x) [cite: 131]
39
+ return F.interpolate(logits, scale_factor=14, mode='bilinear', align_corners=False)
40
+
41
+ # Register for AutoModel discovery
42
+ AnyThermalSegmentationModel.register_for_auto_class("AutoModel")