import torch import torch.nn as nn import torch.nn.functional as F from safetensors.torch import load_file from PIL import Image from torchvision import transforms import string torch.set_num_threads(20) class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(1,64,kernel_size=3,padding=1) self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1) self.conv4 = nn.Conv2d(256, 512, kernel_size=3, padding=1) self.conv5 = nn.Conv2d(512, 1024, kernel_size=3, padding=1) self.fc1 = nn.Linear(1024*8*8, 512) self.fc2 = nn.Linear(512, 256) self.fc3 = nn.Linear(256, 128) self.fc4 = nn.Linear(128, 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 = F.relu(self.conv3(x)) x = F.max_pool2d(x, 2) x = F.relu(self.conv4(x)) x = F.relu(self.conv5(x)) x = x.view(x.size(0), -1) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return x device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = CNN().to(device) 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((64,64)), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) x = transform(img).unsqueeze(0).to(device) with torch.no_grad(): output = model(x) pred_idx = output.argmax(dim=1).item() letters = list(string.ascii_uppercase) pred_letter = letters[pred_idx] print(f"Predicted class: {pred_idx + 1}, Letter: {pred_letter}")