- 导入相应的库
import torch.nn.functional as F
import torch.optim as optim
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms
import torchvision.datasets as datasets
- 设置超参数
BATCH_SIZE = 20
EPOCHS = 10
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- 图像处理与图像增强
transform = transforms.Compose([
transforms.Resize(100),
transforms.RandomVerticalFlip(),
transforms.RandomCrop(50),
transforms.RandomResizedCrop(150),
transforms.ColorJitter(brightness=0.5, contrast=0.5, hue=0.5),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
- 读取数据集和导入数据
dataset_train = datasets.ImageFolder('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small\\train', transform)
print(dataset_train.imgs)
print(dataset_train.class_to_idx)
dataset_test = datasets.ImageFolder('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small\\validation', transform)
print(dataset_test.class_to_idx)
train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=True)
- 定义网络模型
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.max_pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(32, 64, 3)
self.max_pool2 = nn.MaxPool2d(2)
self.conv3 = nn.Conv2d(64, 64, 3)
self.conv4 = nn.Conv2d(64, 64, 3)
self.max_pool3 = nn.MaxPool2d(2)
self.conv5 = nn.Conv2d(64, 128, 3)
self.conv6 = nn.Conv2d(128, 128, 3)
self.max_pool4 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(4608, 512)
self.fc2 = nn.Linear(512, 1)
def forward(self, x):
in_size = x.size(0)
x = self.conv1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.max_pool2(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max_pool3(x)
x = self.conv5(x)
x = F.relu(x)
x = self.conv6(x)
x = F.relu(x)
x = self.max_pool4(x)
x = x.view(in_size, -1)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
modellr = 1e-4
model = ConvNet().to(DEVICE)
optimizer = optim.Adam(model.parameters(), lr=modellr)
- 调整学习率
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
modellrnew = modellr * (0.1 ** (epoch // 5))
print("lr:",modellrnew)
for param_group in optimizer.param_groups:
param_group['lr'] = modellrnew
- 定义训练过程
def train(model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device).float().unsqueeze(1)
optimizer.zero_grad()
output = model(data)
loss = F.binary_cross_entropy(output, target)
loss.backward()
optimizer.step()
if (batch_idx + 1) % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, (batch_idx + 1) * len(data), len(train_loader.dataset),
100. * (batch_idx + 1) / len(train_loader), loss.item()))
def val(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device).float().unsqueeze(1)
output = model(data)
test_loss += F.binary_cross_entropy(output, target, reduction='mean').item()
pred = torch.tensor([[1] if num[0] >= 0.5 else [0] for num in output]).to(device)
correct += pred.eq(target.long()).sum().item()
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
- 定义保存模型和训练
for epoch in range(1, EPOCHS + 1):
adjust_learning_rate(optimizer, epoch)
train(model, DEVICE, train_loader, optimizer, epoch)
val(model, DEVICE, test_loader)
torch.save(model, 'E:\\Cat_And_Dog\\kaggle\\model.pth')
训练结果

- 准备预测的图片
- 进行测试
from __future__ import print_function, division
from PIL import Image
from torchvision import transforms
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.parallel
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.max_pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(32, 64, 3)
self.max_pool2 = nn.MaxPool2d(2)
self.conv3 = nn.Conv2d(64, 64, 3)
self.conv4 = nn.Conv2d(64, 64, 3)
self.max_pool3 = nn.MaxPool2d(2)
self.conv5 = nn.Conv2d(64, 128, 3)
self.conv6 = nn.Conv2d(128, 128, 3)
self.max_pool4 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(4608, 512)
self.fc2 = nn.Linear(512, 1)
def forward(self, x):
in_size = x.size(0)
x = self.conv1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.max_pool2(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max_pool3(x)
x = self.conv5(x)
x = F.relu(x)
x = self.conv6(x)
x = F.relu(x)
x = self.max_pool4(x)
x = x.view(in_size, -1)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
model_save_path = 'E:\\Cat_And_Dog\\kaggle\\model.pth'
transform_test = transforms.Compose([
transforms.Resize(100),
transforms.RandomVerticalFlip(),
transforms.RandomCrop(50),
transforms.RandomResizedCrop(150),
transforms.ColorJitter(brightness=0.5, contrast=0.5, hue=0.5),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
class_names = ['cat', 'dog']
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torch.load(model_save_path)
model.eval()
image_PIL = Image.open('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small\\test\\cats\\cat.1500.jpg')
image_tensor = transform_test(image_PIL)
image_tensor.unsqueeze_(0)
image_tensor = image_tensor.to(device)
out = model(image_tensor)
pred = torch.tensor([[1] if num[0] >= 0.5 else [0] for num in out]).to(device)
print(class_names[pred])
- 预测结果
从实际训练过程来看,总体准确率不高。经过测试发现,该模型只能识别猫,却误判了狗。[En]
From the actual training process, the overall accuracy is not high. After testing, it is found that the model can only identify cats, but misjudge dogs.
Original: https://blog.csdn.net/qq_43279579/article/details/117606669
Author: HarrietLH
Title: 基于Pytorch实现猫狗分类
相关阅读
Title: cv2.getRotationMatrix2D()
前言
最近考虑在对数据集做一些额外的数据增强(主要是随机以任意角度进行旋转),在阅读别人的源码时发现了cv2.getRotationMatrix2D()这一函数,顿时觉得该函数确实有用,查阅相关资料后,得到相关的用法总结( 为了保证阅读观感不会涉及到太多原理的矩阵推导,仅为直观理解)。
正文
旋转矩阵(逆时针)为:
M = ( c o s ( θ ) − s i n ( θ ) s i n ( θ ) c o s ( θ ) ) M= \begin{pmatrix} cos(\theta) & -sin(\theta) \ sin(\theta) & cos(\theta) \end{pmatrix}M =(cos (θ)s in (θ)−s in (θ)cos (θ))
而在opencv中,原点是默认在图片的左上角,单纯的对图片使用该旋转矩阵,只是让图片绕着左上角进行旋转,如下图所示(图示为顺时针旋转45度的效果)。
opencv的特点就是如此虽然图片的直观表现上是逆时针旋转,但是由于原点的位置关系,想要达到该效果需要顺时针旋转矩阵。

所以,我们需要的是能够让图片在任意一点进行旋转,又或者说在原点进行完旋转之后还需要进行相应的平移,将目标的中心点平移回原来的位置。所以我们真正需要的矩阵为:
M = ( c o s ( θ ) − s i n ( θ ) ( 1 − c o s ( θ ) ) × x c e n t e r + s i n ( θ ) × y c e n t e r s i n ( θ ) c o s ( θ ) − s i n ( θ ) × x c e n t e r + ( 1 − c o s ( θ ) ) × y c e n t e r ) M= \begin{pmatrix} cos(\theta) & -sin(\theta) & (1-cos(\theta))\times x_{center}+sin(\theta)\times y_{center}\ sin(\theta) & cos(\theta) & -sin(\theta)\times x_{center}+(1-cos(\theta))\times y_{center} \end{pmatrix}M =(cos (θ)s in (θ)−s in (θ)cos (θ)(1 −cos (θ))×x ce n t er +s in (θ)×y ce n t er −s in (θ)×x ce n t er +(1 −cos (θ))×y ce n t er )
多出来的第三列分别为x和y轴上所需要平移的距离。
所以opencv提供
cv2.getRotationMatrix2D()
就是方便我们进行上述矩阵的获取。其经常被使用到的参数有三个:
- 旋转中心
- 旋转角度
- 旋转后的缩放比例
具体在程序中如何表现请看下述代码:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('1.png')
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
res1 = cv2.warpAffine(img, M, (cols, rows))
plt.subplot(121)
plt.imshow(img)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title('原图')
plt.axis(False)
plt.subplot(122)
plt.imshow(res1)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title('绕中心点旋转的图像')
plt.axis(False)
plt.show()

拓展
虽然已经能够绕任意点旋转,但是图片并不能显示完全,所以还可以使用如下代码进行自适应调整图片大小。
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('1.png')
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
new_w = rows * sin + cols * cos
new_h = rows * cos + cols * sin
M[0, 2] += (new_w - cols) * 0.5
M[1, 2] += (new_h - rows) * 0.5
w = int(np.round(new_w))
h = int(np.round(new_h))
res2 = cv2.warpAffine(img, M, (w, h))
plt.subplot(121)
plt.imshow(img)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title('原图')
plt.axis(False)
plt.subplot(122)
plt.imshow(res2)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title('绕中心点旋转的图像')
plt.axis(False)
plt.show()

[update]
多角度自适应展示
旋转60度

旋转30度

旋转150度

; 结束
如有错误,欢迎各位指正!!!
Original: https://blog.csdn.net/qq_44109682/article/details/117434461
Author: K . U . I
Title: cv2.getRotationMatrix2D()
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/262385/
转载文章受原作者版权保护。转载请注明原作者出处!