Custom Architectures with HuggingFace: Building Your Own Model and Pipeline

Listen to this Post

Featured Image

Creating custom architectures using

Model Training with Baseline Architecture

Before diving into custom architectures, it’s important to start with a baseline model. Here, we’ll use a simple CNN to train on the MNIST dataset.

python

Copy code

import torch

from torch import nn, optim

import torchvision

from torchvision import datasets, transforms

import torch.nn.functional as F

from torch.utils.data import DataLoader

Load dataset

train_dataset = datasets.MNIST(root=./data, train=True, download=True, transform=transforms.ToTensor())

DataLoader setup

batch_size = 64

num_workers = 2

train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)

Define simple CNN model

class Net(nn.Module):

def __init__(self):

super(Net, self).__init__()

self.conv1 = nn.Conv2d(1, 10, kernel_size=5)

self.conv2 = nn.Conv2d(10, 20, kernel_size=5)

self.conv2_drop = nn.Dropout2d()

self.fc1 = nn.Linear(320, 50)

self.fc2 = nn.Linear(50, 10)

self.softmax = nn.Softmax(dim=-1)

def forward(self, x):

x = F.relu(F.max_pool2d(self.conv1(x), 2))

x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))

x = x.view(-1, 320)

x = F.relu(self.fc1(x))

x = F.dropout(x, training=self.training)

x = self.fc2(x)

output = self.softmax(x)

return output

Training setup

model = Net()

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(model.parameters(), lr=0.01)

Training loop

epochs = 10

for epoch in range(epochs):

running_loss = 0.0

for i, data in enumerate(train_dataloader, 0):

inputs, labels = data[0], data[1]

optimizer.zero_grad()

outputs = model(inputs)

loss = criterion(outputs, labels)

loss.backward()

optimizer.step()

running_loss += loss.item()

if i % 20 == 19:

print(f’Epoch [{epoch + 1}/{epochs}], Step [{i + 1}/{len(train_dataloader)}], Loss: {running_loss / 20:.3f}’)

running_loss = 0.0

Save model weights

torch.save({state_dict: model.state_dict()}, model.pth)

With the model trained, we can move on to creating a custom architecture that HuggingFace can use for image classification.

Creating Custom Architectures for HuggingFace

To create a model that is compatible with HuggingFace, we need to define three key components: a configuration file, a model definition, and a custom pipeline. This will ensure that our custom model integrates seamlessly with the HuggingFace ecosystem.

1. The Configuration File

The configuration file contains model parameters and is essential for defining the architecture. It extends PretrainedConfig from HuggingFace and allows us to set hyperparameters for layers such as conv1 and conv2 in our case.

python

Copy code

from transformers import PretrainedConfig

class MnistConfig(PretrainedConfig):

model_type = MobileNetV1

def __init__(self, conv1=10, conv2=20, kwargs):

self.conv1 = conv1

self.conv2 = conv2

super().__init__(kwargs)

2. Defining the Model

The model itself needs to inherit from PreTrainedModel. The configuration parameters are passed to the model at instantiation, ensuring flexibility in changing layer configurations.

python

Copy code

from transformers import PreTrainedModel

import torch.nn.functional as F

from torch import nn

class MnistModel(PreTrainedModel):

config_class = MnistConfig

def __init__(self, config):

super().__init__(config)

self.conv1 = nn.Conv2d(1, config.conv1, kernel_size=5)

self.conv2 = nn.Conv2d(config.conv1, config.conv2, kernel_size=5)

self.conv2_drop = nn.Dropout2d()

self.fc1 = nn.Linear(320, 50)

self.fc2 = nn.Linear(50, 10)

self.softmax = nn.Softmax(dim=-1)

self.criterion = nn.CrossEntropyLoss()

def forward(self, x, labels=None):

x = F.relu(F.max_pool2d(self.conv1(x), 2))

x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))

x = x.view(-1, 320)

x = F.relu(self.fc1(x))

x = F.dropout(x, training=self.training)

x = self.fc2(x)

logits = self.softmax(x)

if labels is not None:

loss = self.criterion(logits, labels)

return {loss: loss, logits: logits}

return logits

3. Push Model to HuggingFace Hub

Once the model and configuration are defined, we push both to HuggingFace’s repository for use across different tasks.

python

Copy code

from huggingface_hub import notebook_login

from transformers import AutoModelForImageClassification

notebook_login()

conf = MnistConfig()

HF_Model = MnistModel(conf)

weights = torch.load(model.pth)

HF_Model.load_state_dict(weights[state_dict])

conf.push_to_hub(MyRepo)

HF_Model.push_to_hub(MyRepo)

This will make the model and its configuration available on the HuggingFace Hub for later use.

What Undercode Says:

The journey from baseline to custom architecture in HuggingFace is a great example of how flexible and powerful the ecosystem is for fine-tuning and deploying models. By starting with a simple CNN, we can gradually build towards a more specialized architecture for tasks like image classification.

The beauty of using HuggingFace lies in the ability to decouple the configuration, model, and pipeline. This modular approach makes the entire system more scalable and reusable. With HuggingFace’s infrastructure, users can easily upload their trained models to the Hub, where they can be accessed and used by others or deployed into production.

One important takeaway here is the critical role of the configuration file. It allows users to tweak key hyperparameters without needing to modify the model’s code itself. This is a huge benefit for experimentation, as it enables users to explore different configurations rapidly.

Additionally, the custom pipeline feature is an excellent way to automate the process of loading data, passing it through the model, and handling the outputs. This kind of automation reduces the need for repetitive code and makes model inference much cleaner and more efficient.

Fact Checker Results

✅ Model Architecture: The CNN model architecture used here is a standard and simple approach for tasks like MNIST classification.
✅ Pipeline Integration: HuggingFace’s Pipeline class is designed to integrate smoothly with custom models, making the deployment process more streamlined.
✅ Deployment to Hub: The process of pushing models to the HuggingFace Hub is correct, with proper usage of the push_to_hub method.

Prediction 📊

The future of custom architectures in the HuggingFace ecosystem looks incredibly promising. As more models are uploaded to the Hub, the use of pre-built and fine-tuned models will become even more accessible. Additionally, the automation of model inference through custom pipelines will make it easier for developers to integrate AI models into their applications without extensive setup.

We expect HuggingFace’s offerings to evolve further, making it even more seamless to build, deploy, and share custom models at scale. As the community grows, so will the number of pre-trained models available for use, enriching the ecosystem for researchers and developers alike.

🕵️‍📝✔️Let’s dive deep and fact‑check.

References:

Reported By: huggingface.co
Extra Source Hub (Possible Sources for article):
https://www.twitter.com
Wikipedia
OpenAi & Undercode AI

Image Source:

Unsplash
Undercode AI DI v2
Bing

🔐JOIN OUR CYBER WORLD [ CVE News • HackMonitor • UndercodeNews ]

💬 Whatsapp | 💬 Telegram

📢 Follow UndercodeNews & Stay Tuned:

𝕏 formerly Twitter 🐦 | @ Threads | 🔗 Linkedin | 🦋BlueSky | 🐘Mastodon