Instructions to use Mamba824/custom_resnet50d with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Mamba824/custom_resnet50d with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="Mamba824/custom_resnet50d", trust_remote_code=True) pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# Load model directly from transformers import AutoImageProcessor, AutoModelForImageClassification processor = AutoImageProcessor.from_pretrained("Mamba824/custom_resnet50d", trust_remote_code=True) model = AutoModelForImageClassification.from_pretrained("Mamba824/custom_resnet50d", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 2,099 Bytes
24e9baf | 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 | from transformers import PreTrainedModel
from timm.models.resnet import BasicBlock, Bottleneck, ResNet
from configuration_resnet import ResnetConfig, resnet50d_config
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,
layers=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,
layers=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.functional.cross_entropy(logits, labels)
return {'loss': loss, 'logits': logits}
return {'logits': logits}
resnet50d = ResnetModelForImageClassification(resnet50d_config)
import timm
pretrained_model = timm.create_model('resnet50d', pretrained=True)
resnet50d.model.load_state_dict(pretrained_model.state_dict())
|