Run this model with HF Jobs

Run BucketOfFish/huggingface_custom_model_tutorial on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/BucketOfFish/huggingface_custom_model_tutorial. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/BucketOfFish/huggingface_custom_model_tutorial on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
BucketOfFish's picture
Upload model
8269eca
Raw History Blame Contribute Delete
2.48 kB
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())