Views
No views yet
train/: Contains the training images of dog breeds.valid/: Contains the validation images of dog breeds.test/: Contains the test images of dog breeds.1# Install Required Libraries
2pip install torch torchvision huggingface_hub
3
4import torch
5import torch.nn as nn
6from torchvision import models
7from huggingface_hub import hf_hub_download
8
9# Load the fine-tuned AlexNet model
10model = models.alexnet(pretrained=False)
11num_features = model.classifier[6].in_features
12model.classifier[6] = nn.Linear(num_features, 10)
13
14# Download model weights from Hugging Face Hub
15model_path = hf_hub_download(repo_id="pramudyalyza/dog-breeds-alexnet", filename="alexnet_model.bin")
16
17# Load the model state
18model.load_state_dict(torch.load(model_path))
19model.eval()
20
21# Example inference
22from PIL import Image
23from torchvision import transforms
24
25# Define transformation
26transform = transforms.Compose([
27 transforms.Resize((227, 227)),
28 transforms.ToTensor(),
29 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
30])
31
32# Load and preprocess an example image
33image = Image.open("path_to_image.jpg")
34image = transform(image).unsqueeze(0)
35
36# Perform inference
37with torch.no_grad():
38 output = model(image)
39 predicted_class = output.argmax(dim=1)
40 print(f"Predicted Dog Breed: {predicted_class.item()}")