-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
67 lines (59 loc) · 1.88 KB
/
predict.py
File metadata and controls
67 lines (59 loc) · 1.88 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import configparser
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
# 定义CNN模型
class CNN(nn.Module):
def __init__(self, num_classes=8):
super(CNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2))
self.fc1 = nn.Linear(64 * 15 * 15, 512)
self.fc2 = nn.Linear(512, num_classes)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.view(out.size(0), -1)
out = self.dropout(self.fc1(out))
out = self.fc2(out)
return out
# 加载配置文件
config = configparser.ConfigParser()
config.read('config.ini')
# 获取配置参数
model_path = config['DEFAULT']['ModelPath']
image_path = config['DEFAULT']['ImagePath']
labels = config['DEFAULT']['Labels'].split(',')
# 图像预处理
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
# 加载模型
model = CNN(num_classes=len(labels))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
# 预测函数
def predict(image_path):
image = Image.open(image_path).convert('RGB')
image = transform(image).unsqueeze(0).to(device)
output = model(image)
_, predicted = torch.max(output.data, 1)
return labels[predicted.item()]
# 进行预测
predicted_class = predict(image_path)
print(f'预测类别是: {predicted_class}')