CNN_classifier / dowload.py
MarkProMaster229's picture
Upload 3 files
e236cbb verified
raw
history blame
1.21 kB
import torch
import torch.nn as nn
import torch.nn.functional as F
from safetensors.torch import load_file
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
self.fc1 = nn.Linear(32*7*7, 64)
self.fc2 = nn.Linear(64, 26)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
model = CNN()
weights_dict = load_file("cnn_letters.safetensors")
model.load_state_dict(weights_dict)
model.eval()
#using
#from PIL import Image
#from torchvision import transforms
#get you image
#img = Image.open("my_letter.png").convert("L")
#transform = transforms.Compose([
# transforms.Resize((28,28)),
# transforms.ToTensor(),
# transforms.Normalize((0.5,), (0.5,))
#])
#x = transform(img).unsqueeze(0)
#with torch.no_grad():
# output = model(x)
# pred = output.argmax(dim=1)
#print(f"Predicted class: {pred.item() + 1}")