Instructions to use BucketOfFish/huggingface_custom_model_tutorial with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use BucketOfFish/huggingface_custom_model_tutorial with Transformers:
# Load model directly from transformers import AutoImageProcessor, ResnetModelForImageClassification processor = AutoImageProcessor.from_pretrained("BucketOfFish/huggingface_custom_model_tutorial") model = ResnetModelForImageClassification.from_pretrained("BucketOfFish/huggingface_custom_model_tutorial") - Notebooks
- Google Colab
- Kaggle
| import timm | |
| import torch | |
| from transformers import PreTrainedModel | |
| from timm.models.resnet import BasicBlock, Bottleneck, ResNet | |
| import custom_configuration | |
| BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck} | |
| class ResnetModel(PreTrainedModel): | |
| config_class = custom_configuration.ResnetConfig # not necessary unless you want to register model with auto classes | |
| 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 = custom_configuration.ResnetConfig # not necessary unless you want to register model with auto classes | |
| 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} # this form, with a loss key, is usable by the Trainer class | |
| return {"logits": logits} | |
| if __name__ == "__main__": | |
| resnet50d_config = custom_configuration.ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) | |
| resnet50d = ResnetModelForImageClassification(resnet50d_config) | |
| # resnet50d.save_pretrained("resnet50d") | |
| # resnet50d.push_to_hub("resnet50d") | |
| # transfer weights from pretrained model | |
| pretrained_model = timm.create_model("resnet50d", pretrained=True) | |
| resnet50d.model.load_state_dict(pretrained_model.state_dict()) | |