File size: 10,781 Bytes
844428c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""Model classes for RSP models compatible with transformers"""

import sys
import os
from pathlib import Path
import torch
import torch.nn as nn
from transformers import PreTrainedModel
from transformers.modeling_outputs import ImageClassifierOutputWithNoAttention
from safetensors.torch import load_file

# Import local modular model
from modular_resnet import ResNet, Bottleneck

# Import other models from sibling directories if needed
_parent_dir = Path(__file__).parent.parent
import importlib.util

# Import SwinTransformer from RSP-Swin-T
_swin_path = _parent_dir / "RSP-Swin-T" / "modular_swin.py"
if _swin_path.exists():
    spec = importlib.util.spec_from_file_location("modular_swin_swin", _swin_path)
    swin_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(swin_module)
    SwinTransformer = swin_module.SwinTransformer
else:
    SwinTransformer = None

# Import ViTAE from RSP-ViTAEv2-S
_vitae_path = _parent_dir / "RSP-ViTAEv2-S" / "modular_vitae_window_noshift.py"
if _vitae_path.exists():
    spec = importlib.util.spec_from_file_location("modular_vitae_window_noshift_vitae", _vitae_path)
    vitae_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(vitae_module)
    ViTAE_Window_NoShift_12_basic_stages4_14 = vitae_module.ViTAE_Window_NoShift_12_basic_stages4_14
else:
    ViTAE_Window_NoShift_12_basic_stages4_14 = None

# Import configuration - handle both relative and absolute imports
try:
    from configuration_rsp import RSPResNetConfig, RSPSwinConfig, RSPViTAEConfig
except ImportError:
    # Fallback: import from same directory
    import importlib.util
    config_path = Path(__file__).parent / "configuration_rsp.py"
    spec = importlib.util.spec_from_file_location("configuration_rsp", config_path)
    config_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(config_module)
    RSPResNetConfig = config_module.RSPResNetConfig
    RSPSwinConfig = config_module.RSPSwinConfig
    RSPViTAEConfig = config_module.RSPViTAEConfig


class RSPResNetForImageClassification(PreTrainedModel):
    """RSP ResNet model for image classification"""
    
    config_class = RSPResNetConfig
    
    def __init__(self, config):
        super().__init__(config)
        
        # Build ResNet model from config
        block = Bottleneck if config.block == "Bottleneck" else None
        if block is None:
            raise ValueError(f"Unsupported block type: {config.block}")
        
        self.model = ResNet(
            block=block,
            layers=config.layers,
            num_classes=config.num_labels
        )
    
    def forward(self, pixel_values=None, labels=None, return_dict=None, **kwargs):
        """
        Args:
            pixel_values: Input images (B, C, H, W)
            labels: Optional labels for loss computation
            return_dict: Whether to return a ModelOutput instead of a plain tuple
        """
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        
        if pixel_values is None:
            raise ValueError("pixel_values must be provided")
        
        logits = self.model(pixel_values)
        
        loss = None
        if labels is not None:
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1))
        
        if not return_dict:
            output = (logits,)
            return (loss,) + output if loss is not None else output
        
        return ImageClassifierOutputWithNoAttention(
            loss=loss,
            logits=logits,
            hidden_states=None,
        )
    
    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
        """Load model from pretrained checkpoint"""
        config = kwargs.pop("config", None)
        if config is None:
            config = RSPResNetConfig.from_pretrained(pretrained_model_name_or_path)
        
        model = cls(config)
        
        # Load weights from safetensors
        model_path = Path(pretrained_model_name_or_path)
        safetensors_path = model_path / "model.safetensors"
        
        if safetensors_path.exists():
            state_dict = load_file(str(safetensors_path))
            # Remove 'model.' prefix if present
            state_dict_clean = {}
            for k, v in state_dict.items():
                if k.startswith("model."):
                    state_dict_clean[k[6:]] = v
                else:
                    state_dict_clean[k] = v
            model.model.load_state_dict(state_dict_clean, strict=False)
        else:
            raise FileNotFoundError(f"Model weights not found at {safetensors_path}")
        
        return model


class RSPSwinForImageClassification(PreTrainedModel):
    """RSP Swin Transformer model for image classification"""
    
    config_class = RSPSwinConfig
    
    def __init__(self, config):
        super().__init__(config)
        
        # Build SwinTransformer model from config
        self.model = SwinTransformer(
            img_size=config.image_size,
            patch_size=config.patch_size,
            in_chans=config.num_channels,
            num_classes=config.num_labels,
            embed_dim=config.embed_dim,
            depths=config.depths,
            num_heads=config.num_heads,
            window_size=config.window_size,
            mlp_ratio=config.mlp_ratio,
            qkv_bias=config.qkv_bias,
            ape=config.ape,
            patch_norm=config.patch_norm,
        )
    
    def forward(self, pixel_values=None, labels=None, return_dict=None, **kwargs):
        """
        Args:
            pixel_values: Input images (B, C, H, W)
            labels: Optional labels for loss computation
            return_dict: Whether to return a ModelOutput instead of a plain tuple
        """
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        
        if pixel_values is None:
            raise ValueError("pixel_values must be provided")
        
        logits = self.model(pixel_values)
        
        loss = None
        if labels is not None:
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1))
        
        if not return_dict:
            output = (logits,)
            return (loss,) + output if loss is not None else output
        
        return ImageClassifierOutputWithNoAttention(
            loss=loss,
            logits=logits,
            hidden_states=None,
        )
    
    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
        """Load model from pretrained checkpoint"""
        config = kwargs.pop("config", None)
        if config is None:
            config = RSPSwinConfig.from_pretrained(pretrained_model_name_or_path)
        
        model = cls(config)
        
        # Load weights from safetensors
        model_path = Path(pretrained_model_name_or_path)
        safetensors_path = model_path / "model.safetensors"
        
        if safetensors_path.exists():
            state_dict = load_file(str(safetensors_path))
            # Remove 'model.' prefix if present
            state_dict_clean = {}
            for k, v in state_dict.items():
                if k.startswith("model."):
                    state_dict_clean[k[6:]] = v
                else:
                    state_dict_clean[k] = v
            model.model.load_state_dict(state_dict_clean, strict=False)
        else:
            raise FileNotFoundError(f"Model weights not found at {safetensors_path}")
        
        return model


class RSPViTAEForImageClassification(PreTrainedModel):
    """RSP ViTAE model for image classification"""
    
    config_class = RSPViTAEConfig
    
    def __init__(self, config):
        super().__init__(config)
        
        # Build ViTAE model from config
        # Note: ViTAE_Window_NoShift_12_basic_stages4_14 already sets most parameters as defaults:
        # - stages=4, embed_dims=[64, 64, 128, 256], token_dims=[64, 128, 256, 512]
        # - downsample_ratios=[4, 2, 2, 2], NC_depth=[2, 2, 8, 2], etc.
        # We only pass parameters that need to be overridden (img_size, num_classes)
        # The function accepts **kwargs, so we can pass window_size if needed
        self.model = ViTAE_Window_NoShift_12_basic_stages4_14(
            pretrained=False,
            img_size=config.image_size,
            num_classes=config.num_labels,
            window_size=7,
        )
    
    def forward(self, pixel_values=None, labels=None, return_dict=None, **kwargs):
        """
        Args:
            pixel_values: Input images (B, C, H, W)
            labels: Optional labels for loss computation
            return_dict: Whether to return a ModelOutput instead of a plain tuple
        """
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        
        if pixel_values is None:
            raise ValueError("pixel_values must be provided")
        
        logits = self.model(pixel_values)
        
        loss = None
        if labels is not None:
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1))
        
        if not return_dict:
            output = (logits,)
            return (loss,) + output if loss is not None else output
        
        return ImageClassifierOutputWithNoAttention(
            loss=loss,
            logits=logits,
            hidden_states=None,
        )
    
    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
        """Load model from pretrained checkpoint"""
        config = kwargs.pop("config", None)
        if config is None:
            config = RSPViTAEConfig.from_pretrained(pretrained_model_name_or_path)
        
        model = cls(config)
        
        # Load weights from safetensors
        model_path = Path(pretrained_model_name_or_path)
        safetensors_path = model_path / "model.safetensors"
        
        if safetensors_path.exists():
            state_dict = load_file(str(safetensors_path))
            # Remove 'model.' prefix if present
            state_dict_clean = {}
            for k, v in state_dict.items():
                if k.startswith("model."):
                    state_dict_clean[k[6:]] = v
                else:
                    state_dict_clean[k] = v
            model.model.load_state_dict(state_dict_clean, strict=False)
        else:
            raise FileNotFoundError(f"Model weights not found at {safetensors_path}")
        
        return model