python
22 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
import torch.nn as nn
class SmallCNN(nn.Module):
def __init__(self, num_classes: int) -> None:
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d((1, 1)),
)
self.classifier = nn.Linear(128, num_classes)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
return self.classifier(x)
1 file · python
Explain with highlit
For image work, I start with a compact CNN before reaching for heavy pretrained models. That baseline helps confirm whether labels, normalization, and augmentation are sane. It also makes failure cases easier to explain because the model architecture is still small enough to reason about directly.
Related snips
python
import cv2
image = cv2.imread('receipt.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresholded = cv2.adaptiveThreshold(
OpenCV image preprocessing for OCR and vision pipelines
opencv
image-processing
computer-vision
by Dr. Elena Vasquez
1 tab
python
import pandas as pd
import torch
from PIL import Image
from torch.utils.data import Dataset, DataLoader
class ProductImageDataset(Dataset):
Custom Datasets and DataLoaders for robust training input pipelines
pytorch
dataloader
dataset
by Dr. Elena Vasquez
1 tab
python
import torch
best_val_loss = float('inf')
for epoch in range(1, num_epochs + 1):
model.train()
A clean PyTorch training loop with validation and checkpoints
pytorch
training-loop
checkpoints
by Dr. Elena Vasquez
1 tab
python
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
features = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True, device=device)
weights = torch.tensor([[0.2], [0.8]], requires_grad=True, device=device)
PyTorch tensor basics and automatic differentiation
pytorch
tensors
autograd
by Dr. Elena Vasquez
1 tab
python
import torch.nn as nn
from torchvision.models import resnet50, ResNet50_Weights
model = resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
for parameter in model.parameters():
Transfer learning with pretrained torchvision backbones
pytorch
transfer-learning
torchvision
by Dr. Elena Vasquez
1 tab
Share this code
Here's the card — post it anywhere.