pytorch 图片识别,pytorch调用训练好的模型识别新的图片
这篇文章主要介绍了框架实现图像识别(实战),文章主要分享实现代码,但也具有一定的参考价值,需要的小伙伴可以才可以一下,希望对你有所帮助
目录
1.代码讲解1.1 导库1.2 标准化、转换、设置GPU1.3预处理数据1.4 建立模型1.5 训练模型1.6 测试模型1.7结果
1. 代码讲解
1.1 导库
导入os.path
从操作系统导入列表目录
将数组作为铭牌导入
进口熊猫作为螺纹中径
从太平航运进口图片
进口火炬
将torch.nn作为神经网络导入
导入火炬. nn .功能为F
将火炬. optim作为使最优化导入
从torch.nn导入AdaptiveAvgPool2d
从火炬. utils.data.sampler导入水下取样器
从torch.utils.data导入数据集
将火炬视觉.转换作为转换导入
从sklearn.model_selection导入训练_测试_拆分
1.2 标准化、transform、设置GPU
设备=手电筒。设备( cuda :0 如果火炬。cuda。is _ available()else CPU )
正常化=转换。正常化(
均值=[0.485,0.456,0.406],
std=[0.229,0.224,0.225]
)
转换=转换。撰写([转换ToTensor()、normalize]) #转换
1.3 预处理数据
类狗数据集(数据集):
# 定义变量
def __init__(self,img_paths,img_labels,size_of_images):
self.img _ paths=img _ paths
self.img _ labels=img _ labels
self.size _ of _ images=size的大小
# 多少长图片
def __len__(self):
返回len(self.img_paths)
# 打开每组图片并处理每张图片
def __getitem__(self,index):
PIL _图像=图像。开放(自我。img _ paths[index]).调整大小(图片大小)
张量图像=变换(PIL图像)
label=self.img_labels[index]
返回张量_图像,标签
print(len(listdir(r c : \ Users \ ai axit \ Desktop \ Deep Learning project \ Deep _ Learning _ Data \ dog-breed-identificati on \ train ))
打印(镜头(PD。read _ CSV(r c : \ Users \ ai axit \ Desktop \ Deep Learning project \ Deep _ Learning _ Data \ dog-breed-identificati on \ labels。CSV’))
print(len(listdir(r c : \ Users \ ai axit \ Desktop \ Deep Learning project \ Deep _ Learning _ Data \ dog-breed-identificati on \ test ))
train_paths=[]
测试路径=[]
标签=[]
# 训练集图片路径
train_paths_lir = rC:\Users\AIAXIT\Desktop\DeepLearningProject\Deep_Learning_Data\dog-breed-identification\train
for path in listdir(train_paths_lir):
train_paths.append(os.path.join(train_paths_lir, path))
# 测试集图片路径
labels_data = pd.read_csv(rC:\Users\AIAXIT\Desktop\DeepLearningProject\Deep_Learning_Data\dog-breed-identification\labels.csv)
labels_data = pd.DataFrame(labels_data)
# 把字符标签离散化,因为数据有120种狗,不离散化后面把数据给模型时会报错:字符标签过多。把字符标签从0-119编号
size_mapping = {}
value = 0
size_mapping = dict(labels_data[breed].value_counts())
for kay in size_mapping:
size_mapping[kay] = value
value += 1
# print(size_mapping)
labels = labels_data[breed].map(size_mapping)
labels = list(labels)
# print(labels)
print(len(labels))
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(train_paths, labels, test_size=0.2)
train_set = DogDataset(X_train, y_train, (32, 32))
test_set = DogDataset(X_test, y_test, (32, 32))
train_loader = torch.utils.data.DataLoader(train_set, batch_size=64)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=64)
1.4 建立模型
class LeNet(nn.Module):def __init__(self):
super(LeNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5),
nn.ReLU(),
nn.AvgPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5),
nn.ReLU(),
nn.AvgPool2d(kernel_size=2, stride=2)
)
self.classifier = nn.Sequential(
nn.Linear(16 * 5 * 5, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 120)
)
def forward(self, x):
batch_size = x.shape[0]
x = self.features(x)
x = x.view(batch_size, -1)
x = self.classifier(x)
return x
model = LeNet().to(device)
criterion = nn.CrossEntropyLoss().to(device)
optimizer = optim.Adam(model.parameters())
TRAIN_LOSS = [] # 损失
TRAIN_ACCURACY = [] # 准确率
1.5 训练模型
def train(epoch):model.train()
epoch_loss = 0.0 # 损失
correct = 0 # 精确率
for batch_index, (Data, Label) in enumerate(train_loader):
# 扔到GPU中
Data = Data.to(device)
Label = Label.to(device)
output_train = model(Data)
# 计算损失
loss_train = criterion(output_train, Label)
epoch_loss = epoch_loss + loss_train.item()
# 计算精确率
pred = torch.max(output_train, 1)[1]
train_correct = (pred == Label).sum()
correct = correct + train_correct.item()
# 梯度归零、反向传播、更新参数
optimizer.zero_grad()
loss_train.backward()
optimizer.step()
print(Epoch: , epoch, Train_loss: , epoch_loss / len(train_set), Train correct: , correct / len(train_set))
1.6 测试模型
和训练集差不多。
def test():model.eval()
correct = 0.0
test_loss = 0.0
with torch.no_grad():
for Data, Label in test_loader:
Data = Data.to(device)
Label = Label.to(device)
test_output = model(Data)
loss = criterion(test_output, Label)
pred = torch.max(test_output, 1)[1]
test_correct = (pred == Label).sum()
correct = correct + test_correct.item()
test_loss = test_loss + loss.item()
print(Test_loss: , test_loss / len(test_set), Test correct: , correct / len(test_set))
1.7结果
epoch = 10for n_epoch in range(epoch):
train(n_epoch)
test()
到此这篇关于pytorch实现图像识别(实战)的文章就介绍到这了,更多相关pytorch实现图像识别内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。