from fastermodels.hf import *
from fastai.vision.all import *
from fastai.callback.all import *
from fasterai.core.criteria import *
import torch_pruning as tp
from torch_pruning.pruner import function
from torch_pruning.pruner.algorithms.scheduler import linear_scheduler
import timm
import torch
import torch.nn as nn
import torch.nn.functional as F
from fasterbench.benchmark import *
from fasterai.prune.all import *
Usage
Tutorial
= 'tutorial'
hf_model_name = 'alexnet' model_name
Get a model
= globals()[model_name]()
model = get_model_size(model) model_size
def get_dls(size, bs):
= URLs.IMAGENETTE_160
path = untar_data(path)
source =(ImageBlock, CategoryBlock)
blocks= [RandomResizedCrop(size, min_scale=0.35), FlipItem(0.5)]
tfms = [Normalize.from_stats(*imagenet_stats)]
batch_tfms
= 'noisy_imagenette.csv'
csv_file = pd.read_csv(source/csv_file)
inp = DataBlock(blocks=blocks,
dblock =ColSplitter(),
splitter=ColReader('path', pref=source),
get_x=ColReader(f'noisy_labels_0'),
get_y=tfms,
item_tfms=batch_tfms)
batch_tfms
return dblock.dataloaders(inp, path=source, bs=bs)
= get_dls(160, 32)
dls = Learner(dls, model, metrics = [accuracy])
learn 5) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 2.190084 | 2.294868 | 0.154650 | 00:05 |
1 | 1.957930 | 1.917212 | 0.328153 | 00:05 |
2 | 1.689020 | 1.605206 | 0.442293 | 00:05 |
3 | 1.430839 | 1.361510 | 0.538344 | 00:05 |
4 | 1.343451 | 1.291558 | 0.572994 | 00:05 |
= learn.validate() loss, acc
Upload the model
= {
metadata "model_name": hf_model_name,
"architecture": model_name,
"framework": "PyTorch",
"datasets": ["ImageNet"],
"task": "Classification",
"metrics": {"Model Accuracy": acc},
"usage_example": """""",
"description": "A simple torchvision model",
"license": "mit",
"tags": ["classification"],
}
# Generate the README content
= generate_readme(metadata)
readme_content
# Save the README to a file
with open("README.md", "w") as f:
f.write(readme_content)
print("README.md has been generated.")
README.md has been generated.
f"Nathan12/{hf_model_name}")
create_repository(
f"Nathan12/{hf_model_name}", model, f"{model_name}", readme_path="README.md") save_and_upload_model(
Load the model from HuggingFace
from huggingface_hub import hf_hub_download
import torch
# Download the model from Hugging Face
= hf_hub_download(
model_file =f"Nathan12/{hf_model_name}",
repo_id=model_name
filename
)
# Load the model (architecture + weights)
= torch.load(model_file)
model
print("Model loaded successfully!")
/tmp/ipykernel_11566/2452638795.py:11: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
model = torch.load(model_file)
Model loaded successfully!
= Learner(dls, model, metrics = [accuracy])
learn 5) learn.fit_one_cycle(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 1.561073 | 1.590430 | 0.476688 | 00:05 |
1 | 1.508280 | 1.513178 | 0.504204 | 00:05 |
2 | 1.322130 | 1.321613 | 0.576815 | 00:05 |
3 | 1.143002 | 1.087752 | 0.644331 | 00:05 |
4 | 1.034787 | 1.018889 | 0.674140 | 00:05 |