数据集分割
import shutil
import numpy as np
import os
# kaggle原始数据集地址
original_dataset_dir = 'D:\\data1\\data'
total_num = int(len(os.listdir(original_dataset_dir))/2) # total_num=12500
# os.listdir() 可以查看当前目录下的文件和目录个数
random_idx = np.array(range(total_num))
np.random.shuffle(random_idx) # np.random.shuffle() 对第一维的随机打乱
base_dir = 'D:\\data1\\target_data' # 待处理的数据集地址
if not os.path.exists(base_dir):
os.mkdir(base_dir) # 创建目录
# 训练集、测试集的划分
sub_dirs = ['train', 'test']
animals = ['cats', 'dogs']
train_idx = random_idx[:int(total_num * 0.8):] # train_idx=10000
test_idx = random_idx[int(total_num * 0.8)::] # test_idx=2500
numbers = [train_idx, test_idx]
for idx, sub_dir in enumerate(sub_dirs):
dir = os.path.join(base_dir, sub_dir) # os.path.join()函数连接两个或更多的路径名组件
if not os.path.exists(dir):
os.mkdir(dir)
for animal in animals:
animal_dir = os.path.join(dir, animal)
if not os.path.exists(animal_dir):
os.mkdir(animal_dir)
fnames = [animal[:-1] + '.{}.jpg'.format(i) for i in numbers[idx]]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(animal_dir, fname)
shutil.copyfile(src, dst) # src复制到dst中去
# 验证训练集、验证集、测试集的划分的照片数目
print(animal_dir + ' total images : %d' % (len(os.listdir(animal_dir))))
模型的训练
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from matplotlib import pyplot as plt
from torchvision import datasets, transforms
# 设置超参数
BATCH_SIZE = 50
EPOCHS = 2
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 数据预处理
transform = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
# 读取数据
root = 'D:\\dog VS cat\\target_data'
dataset_train = datasets.ImageFolder(root + '\\train', transform)
dataset_test = datasets.ImageFolder(root + '\\test', transform)
# 导入数据
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, 128, 3)
self.max_pool3 = nn.MaxPool2d(2)
self.conv4 = nn.Conv2d(128, 128, 3)
self.max_pool4 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(7*7*128, 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.max_pool3(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max_pool4(x)
# 展开
x = x.view(-1, 7*7*128)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
# 实例化模型并且移动到GPU
model = ConvNet().to(DEVICE)
# 选择简单暴力的Adam优化器,学习率调低
optimizer = optim.Adam(model.parameters(), lr=1e-4)
# 定义训练过程
def train(model, device, train_loader, optimizer, epoch):
model.train()
correct = 0
for batch_idx, (data, target) in enumerate(train_loader):
# 增加一个维度而不是reshape,否则要是提取的不是50的倍数就会报错(例如最后一个batch)
data, target = data.to(device), target.to(device).float().unsqueeze(-1)
optimizer.zero_grad()
output = model(data)
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(output)
loss = F.binary_cross_entropy(output, target)
loss.backward()
optimizer.step()
if (batch_idx + 1) % 10 == 0:
loss.item()
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()))
print('\ntrain set: Accuracy: {}/{} ({:.0f}%)\n'.format(
correct, len(train_loader.dataset),
100. * correct / len(train_loader.dataset)))
# 定义测试过程
def test(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)
# print(output)
test_loss += F.binary_cross_entropy(output, target, reduction='sum').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):
train(model, DEVICE, train_loader, optimizer, epoch)
test(model, DEVICE, test_loader)
预测
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
from PIL import Image
from torchvision import transforms
class ConvNet(nn.Module):
def __init__(self,classes=2):
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, 128, 3)
self.max_pool3 = nn.MaxPool2d(2)
self.conv4 = nn.Conv2d(128, 128, 3)
self.max_pool4 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(128*7*7, 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.max_pool3(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max_pool4(x)
# 展开
# x = x.view(in_size, -1)
x = x.view(-1, 128*7*7)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
model = ConvNet()
model.load_state_dict(torch.load('7.pth'))
model.eval()
path = 'C://Users//Administrator//Pictures//dog2.jpg'
input_image = Image.open(path)
plt.imshow(input_image)
plt.axis('off')
plt.show()
input_image = transforms.ToTensor()(input_image)
input_image = transforms.RandomResizedCrop(150)(input_image)
input_image = transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5])(input_image)
input_image = torch.unsqueeze(input_image, 0)
output = model(input_image)
print(output)
target = torch.Tensor([0.5])
if output <= target:
print("识别结果是猫")
else:
print("识别结果是狗")