当前位置:   article > 正文

python猫狗识别课程设计_pytorch实现kaggle猫狗识别

pytorch深度学习 dog and cat detection

# 创建模型

class Net(nn.Module):

def __init__(self):

super(Net, self).__init__()

self.conv1 = nn.Conv2d(3, 6, 5)

self.maxpool = nn.MaxPool2d(2, 2)

self.conv2 = nn.Conv2d(6, 16, 5)

self.fc1 = nn.Linear(16 * 53 * 53, 1024)

self.fc2 = nn.Linear(1024, 512)

self.fc3 = nn.Linear(512, 2)

def forward(self, x):

x = self.maxpool(F.relu(self.conv1(x)))

x = self.maxpool(F.relu(self.conv2(x)))

x = x.view(-1, 16 * 53 * 53)

x = F.relu(self.fc1(x))

x = F.relu(self.fc2(x))

x = self.fc3(x)

return x

class Net2(nn.Module):

def __init__(self):

super(Net2, self).__init__()

self.conv1 = nn.Conv2d(3, 6, 5)

self.maxpool = nn.MaxPool2d(2, 2)

self.conv2 = nn.Conv2d(6, 16, 5)

self.fc1 = nn.Linear(16 * 53 * 53, 1024)

torch.nn.Dropout(0.5)

self.fc2 = nn.Linear(1024, 512)

torch.nn.Dropout(0.5)

self.fc3 = nn.Linear(512, 2)

def forward(self, x):

x = self.maxpool(F.relu(self.conv1(x)))

x = self.maxpool(F.relu(self.conv2(x)))

x = x.view(-1, 16 * 53 * 53)

x = F.relu(self.fc1(x))

x = F.relu(self.fc2(x))

x = self.fc3(x)

return x

我们从conv1说起。conv1实际上就是定义一个卷积层,3,6,5分别是什么意思?3代表的是输入图像的像素数组的层数,一般来说就是你输入的图像的通道数,比如这里使用的小猫图像都是彩色图像,由R、G、B三个通道组成,所以数值为3;6代表的是我们希望进行6次卷积,每一次卷积都能生成不同的特征映射数组,用于提取小猫和小狗的6种特征。每一个特征映射结果最终都会被堆叠在一起形成一个图像输出,再作为下一步的输入;5就是过滤框架的尺寸,表示我们希望用一个5 * 5的矩阵去和图像中相同尺寸的矩阵进行点乘再相加,形成一个值。定义好了卷基层,我们接着定义池化层。池化层所做的事说来简单,其实就是因为大图片生成的像素矩阵实在太大了,我们需要用一个合理的方法在降维的同时又不失去物体特征,所以深度学习学者们想出了一个称为池化的技术,说白了就是从左上角开始,每四个元素(2 * 2)合并成一个元素,用这一个元素去代表四个元素的值,所以图像体积一下子降为原来的四分之一。再往下一行,我们又一次碰见了一个卷积层:conv2,和conv1一样,它的输入也是一个多层像素数组,输出也是一个多层像素数组,不同的是这一次完成的计算量更大了,我们看这里面的参数分别是6,16,5。之所以为6是因为conv1的输出层数为6,所以这里输入的层数就是6;16代表conv2的输出层数,和conv1一样,16代表着这一次卷积操作将会学习小猫小狗的16种映射特征,特征越多理论上能学习的效果就越好,大家可以尝试一下别的值,看看效果是否真的编变好。conv2使用的过滤框尺寸和conv1一样,所以不再重复。

关于53这个数字可以根据((n+2p-f)/ s)+1计算出来。而三个全连接层所做的事很类似,就是不断训练,最后输出一个二分类数值。net类的forward函数表示前向计算的整个过程。forward接受一个input,返回一个网络输出值,中间的过程就是一个调用init函数中定义的层的过程。F.relu是一个激活函数,把所有的非零值转化成零值。此次图像识别的最后关键一步就是真正的循环训练操作。

进行训练的代码:

1 def train():2

3 for epoch inrange(epochs):4 running_loss = 0.0

5 train_correct = 0

6 train_total = 0

7 for step, data in enumerate(train_loader, 0):#第二个参数表示指定索引从0开始8 inputs, train_labels =data9 ifuse_gpu:10 inputs, labels =Variable(inputs.cuda()), Variable(train_labels.cuda())11 else:12 inputs, labels =Variable(inputs), Variable(train_labels)13 optimizer.zero_grad()14 outputs =net(inputs)15 _, train_predicted = torch.max(outputs.data, 1) #返回每一行最大值的数值和索引,索引对应分类16 train_correct += (train_predicted ==labels.data).sum()17 loss =cirterion(outputs, labels)18 loss.backward()19 optimizer.step()20 running_loss +=loss.item()21 train_total += train_labels.size(0)22

23 print('train %d epoch loss: %.3f acc: %.3f' %(24 epoch + 1, running_loss / train_total, 100 * train_correct /train_total))25 # 模型测试26 correct = 0

27 test_loss = 0.0

28 test_total = 0

29 test_total = 0

30 net.eval() #测试的时候整个模型的参数不再变化31 for data intest_loader:32 images, labels =data33 ifuse_gpu:34 images, labels =Variable(images.cuda()), Variable(labels.cuda())35 else:36 images, labels =Variable(images), Variable(labels)37 outputs =net(images)38 _, predicted = torch.max(outputs.data, 1)39 loss =cirterion(outputs, labels)40 test_loss +=loss.item()41 test_total += labels.size(0)42 correct += (predicted == labels.data).sum()

完整的代码如下

1 # coding=utf-8

2 import os3 import numpy asnp4 import torch5 import torch.nn asnn6 import torch.nn.functional asF7 import torch.optim asoptim8 fromtorch.autograd import Variable9 fromtorch.utils.data import Dataset10 fromtorchvision import transforms, datasets, models11 import shutil12 from matplotlib import pyplot asplt13 # 随机种子设置14 random_state = 42

15 np.random.seed(random_state)16 #接下来的数据是把原训练集90%的数据做训练,10%做测试集,其中把分为训练集的数据内的猫和狗分开,分为测试集的数据的猫和狗进行分开保存在新的各自的目录下17 # kaggle原始数据集地址18 original_dataset_dir = 'D:\\Code\\Python\\Kaggle-Dogs_vs_Cats_PyTorch-master\\data\\train'#训练集地址19 total_num = int(len(os.listdir(original_dataset_dir)) ) #训练集数据总数,包含猫和狗20 random_idx =np.array(range(total_num))21 np.random.shuffle(random_idx)#打乱图片顺序22

23 # 待处理的数据集地址24 base_dir = 'D:\\Code\\dogvscat\\data2'#把原训练集数据分类后的数据存储在该目录下25 ifnot os.path.exists(base_dir):26 os.mkdir(base_dir)27

28 # 训练集、测试集的划分29 sub_dirs = ['train', 'test']30 animals = ['cats', 'dogs']31 train_idx = random_idx[:int(total_num * 0.9)] #打乱后的数据的90%是训练集,10是测试集32 test_idx = random_idx[int(total_num * 0.9):int(total_num * 1)]33 numbers =[train_idx, test_idx]34 for idx, sub_dir inenumerate(sub_dirs):35 dir = os.path.join(base_dir, sub_dir)#'D:\\Code\\dogvscat\\data2\\train'或'D:\\Code\\dogvscat\\data2\\test'

36 ifnot os.path.exists(dir):37 os.mkdir(dir)38

39 animal_dir = ""

40

41 #fnames = ['.{}.jpg'.format(i) for i innumbers[idx]]42 fnames = ""

43 if sub_dir == 'train':44 idx = 0

45 else:46 idx =1

47 for i innumbers[idx]:48 #print(i)49 if i>=12500:#把数据保存在dogs目录下50 fnames = str('dog'+'.{}.jpg'.format(i))51 animal_dir = os.path.join(dir,'dogs')52

53 ifnot os.path.exists(animal_dir):54 os.mkdir(animal_dir)55 if i<12500:#图片是猫,数据保存在cats目录下56 fnames = str('cat'+'.{}.jpg'.format(i))57 animal_dir = os.path.join(dir, 'cats')58 ifnot os.path.exists(animal_dir):59 os.mkdir(animal_dir)60 src =os.path.join(original_dataset_dir, str(fnames)) #原数据地址61 #print(src)62 dst =os.path.join(animal_dir, str(fnames))#新地址63 #print(dst)64 shutil.copyfile(src, dst)#复制65

66

67 # 验证训练集、测试集的划分的照片数目68 print(dir + 'total images : %d' % (len(os.listdir(dir+'\\dogs'))+len(os.listdir(dir+'\\cats'))))69 # coding=utf-8

70

71 # 配置参数72 random_state = 1

73 torch.manual_seed(random_state) # 设置随机数种子,确保结果可重复74 torch.cuda.manual_seed(random_state)# #为GPU设置种子用于生成随机数,以使得结果是确定的75 torch.cuda.manual_seed_all(random_state) #为所有GPU设置种子用于生成随机数,以使得结果是确定的76 np.random.seed(random_state)77 # random.seed(random_state)78

79 epochs = 10# 训练次数80 batch_size = 4# 批处理大小81 num_workers = 0# 多线程的数目82 use_gpu =torch.cuda.is_available()83 PATH='D:\\Code\\dogvscat\\model.pt'

84 # 对加载的图像作归一化处理, 并裁剪为[224x224x3]大小的图像85 data_transform =transforms.Compose([86 transforms.Resize(256),#重置图像分辨率87 transforms.CenterCrop(224), #中心裁剪88 transforms.ToTensor(),89 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) #归一化90 ])91

92 train_dataset = datasets.ImageFolder(root='D:\\Code\\dogvscat\\data2\\train',93 transform=data_transform)94 print(train_dataset)95 train_loader =torch.utils.data.DataLoader(train_dataset,96 batch_size=batch_size,97 shuffle=True,98 num_workers=num_workers)99

100 test_dataset = datasets.ImageFolder(root='D:\\Code\\dogvscat\\data2\\test', transform=data_transform)101 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)102

103

104 # 创建模型105 classNet(nn.Module):106 def __init__(self):107 super(Net, self).__init__()108 self.conv1 = nn.Conv2d(3, 6, 5)109 self.maxpool = nn.MaxPool2d(2, 2)110 self.conv2 = nn.Conv2d(6, 16, 5)111 self.fc1 = nn.Linear(16 * 53 * 53, 1024)112 self.fc2 = nn.Linear(1024, 512)113 self.fc3 = nn.Linear(512, 2)114

115 def forward(self, x):116 x =self.maxpool(F.relu(self.conv1(x)))117 x =self.maxpool(F.relu(self.conv2(x)))118 x = x.view(-1, 16 * 53 * 53)119 x =F.relu(self.fc1(x))120 x =F.relu(self.fc2(x))121 x =self.fc3(x)122

123 returnx124 classNet2(nn.Module):125 def __init__(self):126 super(Net2, self).__init__()127 self.conv1 = nn.Conv2d(3, 6, 5)128 self.maxpool = nn.MaxPool2d(2, 2)129 self.conv2 = nn.Conv2d(6, 16, 5)130 self.fc1 = nn.Linear(16 * 53 * 53, 1024)131 torch.nn.Dropout(0.5)132 self.fc2 = nn.Linear(1024, 512)133 torch.nn.Dropout(0.5)134 self.fc3 = nn.Linear(512, 2)135

136 def forward(self, x):137 x =self.maxpool(F.relu(self.conv1(x)))138 x =self.maxpool(F.relu(self.conv2(x)))139 x = x.view(-1, 16 * 53 * 53)140 x =F.relu(self.fc1(x))141 x =F.relu(self.fc2(x))142 x =self.fc3(x)143

144 returnx145

146

147 net =Net2()148 if(os.path.exists('D:\\Code\\dogvscat\\model.pt')):149 net=torch.load('D:\\Code\\dogvscat\\model.pt')150

151 ifuse_gpu:152 print('gpu is available')153 net =net.cuda()154 else:155 print('gpu is unavailable')156

157 print(net)158 trainLoss =[]159 trainacc =[]160 testLoss =[]161 testacc =[]162 x = np.arange(1,11)163 # 定义loss和optimizer164 cirterion =nn.CrossEntropyLoss()165 optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)166

167 def train():168

169 for epoch inrange(epochs):170 running_loss = 0.0

171 train_correct = 0

172 train_total = 0

173 for step, data in enumerate(train_loader, 0):#第二个参数表示指定索引从0开始174 inputs, train_labels =data175 ifuse_gpu:176 inputs, labels =Variable(inputs.cuda()), Variable(train_labels.cuda())177 else:178 inputs, labels =Variable(inputs), Variable(train_labels)179 optimizer.zero_grad()180 outputs =net(inputs)181 _, train_predicted = torch.max(outputs.data, 1) #返回每一行最大值的数值和索引,索引对应分类182 train_correct += (train_predicted ==labels.data).sum()183 loss =cirterion(outputs, labels)184 loss.backward()185 optimizer.step()186 running_loss +=loss.item()187 train_total += train_labels.size(0)188

189 print('train %d epoch loss: %.3f acc: %.3f' %(190 epoch + 1, running_loss / train_total, 100 * train_correct /train_total))191 # 模型测试192 correct = 0

193 test_loss = 0.0

194 test_total = 0

195 test_total = 0

196 net.eval() #测试的时候整个模型的参数不再变化197 for data intest_loader:198 images, labels =data199 ifuse_gpu:200 images, labels =Variable(images.cuda()), Variable(labels.cuda())201 else:202 images, labels =Variable(images), Variable(labels)203 outputs =net(images)204 _, predicted = torch.max(outputs.data, 1)205 loss =cirterion(outputs, labels)206 test_loss +=loss.item()207 test_total += labels.size(0)208 correct += (predicted ==labels.data).sum()209

210 print('test %d epoch loss: %.3f acc: %.3f' % (epoch + 1, test_loss / test_total, 100 * correct /test_total))211 trainLoss.append(running_loss /train_total)212 trainacc.append(100 * train_correct /train_total)213 testLoss.append(test_loss /test_total)214 testacc.append(100 * correct /test_total)215 plt.figure(1)216 plt.title('train')217 plt.plot(x,trainacc,'r')218 plt.plot(x,trainLoss,'b')219 plt.show()220 plt.figure(2)221 plt.title('test')222 plt.plot(x,testacc,'r')223 plt.plot(x,testLoss,'b')224 plt.show()225

226

227

228 torch.save(net, 'D:\\Code\\dogvscat\\model.pt')229

230

231 train()

看一下某次的运行结果

D:\anaconda\anaconda\pythonw.exe D:/Code/Python/pytorch入门与实践/第六章_pytorch实战指南/猫和狗二分类.py

D:\Code\dogvscat\data2\train\cats total images :11253D:\Code\dogvscat\data2\test\dogs total images :1253Net(

(conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))

(maxpool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))

(fc1): Linear(in_features=44944, out_features=1024, bias=True)

(fc2): Linear(in_features=1024, out_features=512, bias=True)

(fc3): Linear(in_features=512, out_features=2, bias=True)

)

train1 epoch loss: 0.162 acc: 61.000test1 epoch loss: 0.153 acc: 66.000train2 epoch loss: 0.148 acc: 68.000test2 epoch loss: 0.143 acc: 71.000train3 epoch loss: 0.138 acc: 71.000test3 epoch loss: 0.138 acc: 72.000train4 epoch loss: 0.130 acc: 74.000test4 epoch loss: 0.137 acc: 72.000train5 epoch loss: 0.119 acc: 77.000test5 epoch loss: 0.132 acc: 74.000train6 epoch loss: 0.104 acc: 81.000test6 epoch loss: 0.129 acc: 75.000train7 epoch loss: 0.085 acc: 85.000test7 epoch loss: 0.132 acc: 75.000train8 epoch loss: 0.060 acc: 90.000test8 epoch loss: 0.146 acc: 75.000train9 epoch loss: 0.036 acc: 94.000test9 epoch loss: 0.200 acc: 74.000train10 epoch loss: 0.022 acc: 97.000test10 epoch loss: 0.207 acc: 75.000Process finished with exit code0

发现这个程序运行结果训练集准确率很高,测试集准确率为75%左右,因此Net类有点过拟合,Net2加入了Dropout降低网络复杂度处理过拟合。这个程序属于最基础的分类算法,因此准确率并不是很高,但是我认为初学者可以先会这个程序,再继续提高网络的准确率。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/366461
推荐阅读
相关标签
  

闽ICP备14008679号