-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
54 lines (48 loc) · 1.64 KB
/
classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
self.relu1 = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU()
self.fc = nn.Linear(64 * 56 * 56, 2)
def forward(self, x):
x = self.pool(self.relu1(self.conv1(x)))
x = self.pool(self.relu2(self.conv2(x)))
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def image_transform(imagepath):
transformer = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
image = Image.open(imagepath)
image = transformer(image).unsqueeze(0)
return image
def predict(imagepath, model):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval()
with torch.no_grad():
image = image_transform(imagepath).to(device)
outputs = model(image)
_, predicted = torch.max(outputs, 1)
return predicted
def main():
imagepath = 'IMG_0659.JPG'
model_path = 'model.pth'
model = CNN()
model.load_state_dict(torch.load(model_path))
print('Class:', predict(imagepath, model).item())
if predict(imagepath, model).item() == 1:
print('This is a cat.')
if __name__ == "__main__":
main()