|
"ref:https://huggingface.co/docs/transformers/custom_models#sharing-custom-models" |
|
from transformers import PreTrainedModel |
|
from timm.models.resnet import BasicBlock, Bottleneck, ResNet |
|
from configuration_resnet import ResnetConfig |
|
from transformers import AutoConfig, AutoModel, AutoModelForImageClassification |
|
import torch |
|
import timm |
|
|
|
|
|
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} |
|
|
|
|
|
resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) |
|
resnet50d_config.save_pretrained("/root/code/Huggingface_Toturials/resnet_model/custom-resnet") |
|
|
|
|
|
resnet50d_config = ResnetConfig.from_pretrained("/root/code/Huggingface_Toturials/resnet_model/custom-resnet") |
|
|
|
|
|
resnet50d = ResnetModelForImageClassification(resnet50d_config) |
|
|
|
|
|
pretrained_model = timm.create_model("resnet50d", pretrained=True) |
|
resnet50d.model.load_state_dict(pretrained_model.state_dict()) |
|
|
|
|
|
AutoConfig.register("resnet_demo", ResnetConfig) |
|
AutoModel.register(ResnetConfig, ResnetModel) |
|
AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification) |
|
|
|
|
|
|