Skillversitas

TorchHub and the Ecosystem

The ecosystem is the torch.hub (the repo the load the 1 line, the pretrain the weight the download the auto), the HuggingFace (the transformers the 50K the model, the pipeline the 1 line the infer), the timm (the CNN the 500 the ImageNet the weight), and the Lightning (the LightningModule the train the loop the structure, the Trainer the GPU the DDP the no the code). The trust_remote_code is the custom (the HuggingFace the model the code the repo), the pipeline is the NLP (the text the tokenize the model the decode), and the Trainer is the fit (the GPU the 8 the DDP the accelerator the 1 line). This chapter covers the torch.hub (the load, the trust), the HuggingFace (the pipeline, the transformers), the timm (the CNN the 500), and the Lightning (the LightningModule, the Trainer).

The torch.hub and the trust_remote_code

# the HUB (the repo the load, the 1 line, the
#   weight the download the auto):
model = torch.hub.load(
    "pytorch/vision", "resnet50",
    pretrained=True)   #   the ImageNet's
model.eval()
#   the 2: the transform's (the Hub's
#     the also, the match's):
tf = torch.hub.load(
    "pytorch/vision", "resnet50",
    pretrained=True)   #   the model
#   the pretrain's the 0.485/0.229 the
#     the Normalize the also (the fine-
#     tune's the match, the no-skip)

# the LOCAL (the no-download, the offline):
model = torch.hub.load(
    "./my-model", "model",
    source="local")   #   the dir's

# the TRUST (the custom code, the
#   HuggingFace's, the the repo's the
#   .py the run):
model = torch.hub.load(
    "huggingface/transformers",
    "resnet",
    trust_remote_code=True)
#   the warn: the code the repo's
#     the run (the trust's the yes)

The torch.hub.load is the 1 line: the repo (the pytorch/vision, the huggingface/transformers), the model name (the resnet50), and the pretrained=True (the weight the download the auto the 1 time, the cache the ~/.cache/torch/hub) - the transform the match (the 0.485/0.229 the ImageNet's, the fine-tune the same), and the source="local" the offline (the dir the no the download). The trust_remote_code=True is the custom: the HuggingFace the model the code the repo (the .py the run, the no the torch.hub the only), and the warn is the security (the code the repo's the execute, the trust the yes the know the source) - the no the trust the standard (the pytorch/vision the torchvision.models the prefer, the hub the fallback), and the next section's HuggingFace is the transformer the 50K the model.

The HuggingFace and the timm

# the PIPELINE (the 1 line, the NLP's, the
#   tokenize the model the decode):
from transformers import pipeline

clf = pipeline("sentiment-analysis",
    model="distilbert-base-uncased-finetuned-
    imdb")
out = clf("the movie is good")
#   the [{'label': 'POSITIVE',
#          'score': 0.9998}]
#   the the 3: the pipeline (the
#     the tokenizer, the model, the
#     the post-process) the 1 call

# the TIMM (the CNN the 500, the ImageNet's
#   weight, the no-hub):
import timm

model = timm.create_model(
    "effnetv2_s", pretrained=True)
#   the 500+: the resnet, the efficientnet,
#   the convnext, the vit, the swin
model.eval()

The HuggingFace is the transformer's 50K the model: the pipeline the 1 line (the sentiment-analysis the text the label, the tokenizer plus the model plus the post-process the 3 the wrap), the AutoModel the load (the config the repo the match, the weight the download the auto), and the trust_remote_code=True the custom (the .py the repo the run) - the NLP the CV the audio the same the pipeline (the text-classification, the image-classification, the automatic-speech-recognition). The timm is the CNN the 500 (the resnet, the efficientnet, the convnext, the vit, the swin): the create_model("effnetv2_s", pretrained=True) the ImageNet the weight (the no the hub the import the only), and the tf the match (the model.cfg the mean/std, the transform the auto the build) - the fine-tune the transfer chapter's path the timm the 500 the backbone. The Lightning is the next section: the train the loop the framework the no the code.

The Lightning: The LightningModule and the Trainer

# the LIGHTNINGMODULE (the train the loop
#   the structure, the fit the no code):
import pytorch_lightning as pl

class LitCNN(pl.LightningModule):
    def __init__(self):
        super().__init__()
        self.net = CNN()
    def training_step(self, batch, i):
        x, y = batch
        loss = nn.CrossEntropyLoss()(
            self.net(x), y)
        self.log("train_loss", loss)
        return loss
    def configure_optimizers(self):
        return optim.AdamW(
            self.parameters(), lr=1e-3)

# the TRAINER (the GPU the DDP the 1 line):
model = LitCNN()
trainer = pl.Trainer(
    accelerator="auto",   #   the CUDA/
                        #     the MPS/the
                        #     CPU's
    devices=8,            #   the 8 GPU
                        #     the DDP the
                        #     no code
    max_epochs=5,
    log_every_n_steps=50)
trainer.fit(model, train_loader)
trainer.validate(model, test_loader)

The Lightning is the train the loop the structure: the LightningModule the 4 the method (the training_step the 1 batch, the validation_step the eval, the configure_optimizers the opt, the forward the infer), and the Trainer is the fit (the loop the epoch the batch the no the code, the log the tensorboard the auto, the checkpoint the callback the auto). The accelerator="auto" is the CUDA (the MPS, the CPU the fallback), the devices=8 is the DDP (the 8 GPU the 1 line, the no the torchrun the DDP the wrap), and the log_every_n_steps is the cadence - the fit the 1 call (the 5 epoch the 8 GPU the no the distributed the code), and the rule is the one: the hub the 1 line the load, the pipeline the NLP the 1 line, the timm the CNN the 500, the Lightning the train the no the code - the next chapter's best-practices is the seed the determinism the eval the no_grad the checkpoint the rule.

The Working Rules

  • The torch.hub is the 1 line, the trust the custom. The repo plus the model plus the pretrained (the weight the auto, the cache the ~/.cache/torch/hub), the source="local" the offline, and the trust_remote_code=True the HuggingFace (the .py the repo the run, the warn the security) - the torchvision.models the prefer (the no the remote the code).
  • The pipeline is the NLP, the AutoModel the load. The sentiment-analysis the text the label (the tokenizer plus the model plus the post-process the wrap), and the AutoModel the config the match - the 50K the model the NLP the CV the audio the same.
  • The timm is the CNN, the 500 the backbone. The create_model("effnetv2_s", pretrained=True) the ImageNet the weight (the import the only), and the model.cfg the mean/std (the transform the match) - the resnet the convnext the vit the swin the 500 the fine-tune the transfer.
  • The Lightning is the no-code, the Trainer the fit. The LightningModule the 4 the method, the accelerator="auto" the CUDA, and the devices=8 the DDP (the no the torchrun, the no the DDP the wrap) - the fit the 1 call, the checkpoint the callback the auto.

Summary

The ecosystem is the 4 the tool: the torch.hub (the load the repo the 1 line, the pretrained the weight the auto, the trust_remote_code the custom the HuggingFace), the HuggingFace (the pipeline the 1 line the NLP, the AutoModel the 50K the model, the tokenizer plus the model plus the post-process the wrap), the timm (the CNN the 500 the ImageNet the weight, the cfg the transform the match), and the Lightning (the LightningModule the 4 the method, the Trainer the fit the GPU the DDP the no the code). The rule is the one: the torchvision.models the prefer (the no the remote the code), the hub the fallback, the pipeline the NLP, the timm the CNN, the Lightning the train - the next chapter's best-practices is the seed the determinism, the eval the no_grad, the checkpoint the every epoch, and the overfit the 1 batch the check.