Custom ResNet50d

A Transformers-compatible wrapper around the pretrained timm/resnet50d.ra2_in1k ImageNet-1k classifier, following the custom model tutorial. The pretrained weights are copied from timm without additional training.

The model has 25,576,264 parameters and predicts 1,000 ImageNet classes. The repository includes the custom Python code, safetensors weights, configuration, ImageNet class names, and image processor.

Usage

Install the dependencies in requirements.txt, then load the model and processor:

import torch
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification

repo_id = "Dadigua12581/custom-resnet50d"
processor = AutoImageProcessor.from_pretrained(repo_id)
model = AutoModelForImageClassification.from_pretrained(
    repo_id, trust_remote_code=True
).eval()

image = Image.open("image.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
with torch.inference_mode():
    logits = model(**inputs)["logits"]
label = logits.argmax(-1).item()
print(model.config.id2label[label])

The processor converts images to RGB, resizes the shorter edge to 256 pixels using bicubic interpolation, center-crops to 224 x 224, rescales to [0, 1], and normalizes with ImageNet mean and standard deviation.

Pass integer class indices as labels to compute cross-entropy loss:

outputs = model(**inputs, labels=torch.tensor([label]))
loss = outputs["loss"]

For spatial features, load AutoModel instead:

from transformers import AutoModel

backbone = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval()
with torch.inference_mode():
    features = backbone(**inputs)  # (batch, 2048, 7, 7) for 224 x 224 inputs

Reproduce the export

The local project uses the tutorial's Python package structure under src/:

src/
└── resnet_model/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ configuration_resnet.py
    └── modeling_resnet.py

Import the model and configuration as in the tutorial:

from resnet_model.configuration_resnet import ResnetConfig
from resnet_model.modeling_resnet import ResnetModel, ResnetModelForImageClassification

The top-level cfg.py and model.py retain their original import paths. save_pretrained() copies configuration_resnet.py and modeling_resnet.py to the exported model directory; the export script also includes __init__.py. modeling_resnet.py uses a relative import of .configuration_resnet. The saved auto_map points AutoConfig and AutoModel classes to these files.

uv run prepare_model.py --repo-id Dadigua12581/custom-resnet50d
hf upload Dadigua12581/custom-resnet50d artifacts/custom-resnet50d .

The export checks that AutoClass can reload both models, preserve all pretrained weights exactly, and reproduce timm's logits and spatial features within floating point tolerance on a sample input. It also checks that classification loss is finite. These checks do not measure model accuracy.

Intended use and limitations

This is a tutorial export for image classification and feature extraction. Predictions are restricted to ImageNet-1k classes. No additional fine-tuning, accuracy evaluation, or bias evaluation was performed for this export. See the source model card for details about the original training and results.

License

Apache-2.0, consistent with the source timm model. ResNet architecture: Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun, Deep Residual Learning for Image Recognition (2015).

Downloads last month
2,138
Safetensors
Model size
25.6M params
Tensor type
F32
Β·
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for Dadigua12581/custom-resnet50d

Finetuned
(1)
this model

Dataset used to train Dadigua12581/custom-resnet50d