File size: 2,192 Bytes
e7251dc
 
4e86085
 
 
920b410
e7251dc
 
 
 
 
 
920b410
e7251dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
920b410
 
e7251dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import PreTrainedModel
from timm.models.resnet import BasicBlock, Bottleneck, ResNet
# NOTE: below line must use relative import, as this file and configuration_resnet.py will be downloaded into a directory.
# NOTE: export_files_for_hf.py etc must put outside of dir of this file. otherwise, when run 'python export_files_for_hf.py', will get error: ImportError: attempted relative import with no known parent package. 
# Note: for why, see https://github.com/zhangfaen/python-pytorch-tips/blob/main/relative_import_key_tips.md
from .configuration_resnet import ResnetConfig
import torch

BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck}


class ResnetModel(PreTrainedModel):
    config_class = ResnetConfig

    def __init__(self, config):
        super().__init__(config)
        block_layer = BLOCK_MAPPING[config.block_type]
        self.model = ResNet(
            block_layer,
            config.layers,
            num_classes=config.num_classes,
            in_chans=config.input_channels,
            cardinality=config.cardinality,
            base_width=config.base_width,
            stem_width=config.stem_width,
            stem_type=config.stem_type,
            avg_down=config.avg_down,
        )

    def forward(self, tensor):
        return self.model.forward_features(tensor)
    

class ResnetModelForImageClassification(PreTrainedModel):
    config_class = ResnetConfig

    def __init__(self, config):
        super().__init__(config)
        block_layer = BLOCK_MAPPING[config.block_type]
        self.model = ResNet(
            block_layer,
            config.layers,
            num_classes=config.num_classes,
            in_chans=config.input_channels,
            cardinality=config.cardinality,
            base_width=config.base_width,
            stem_width=config.stem_width,
            stem_type=config.stem_type,
            avg_down=config.avg_down,
        )

    def forward(self, tensor, labels=None):
        logits = self.model(tensor)
        if labels is not None:
            loss = torch.nn.cross_entropy(logits, labels)
            return {"loss": loss, "logits": logits}
        return {"logits": logits}