赞
踩
本文主要是复习pytorch实战计算机视觉的内容,模型采用CNN,数据集是手写体
class Model(torch.nn.Module): def __init__(self): super(Model,self).__init__() self.conv1 = torch.nn.Sequential( torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1), torch.nn.ReLU(), torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(stride=2,kernel_size=2) ) self.fc = torch.nn.Sequential( torch.nn.Linear(14*14*128,1024), torch.nn.ReLU(), torch.nn.Dropout(p=0.5), torch.nn.Linear(1024,10) ) def forward(self,x): x = self.conv1(x) #卷积处理 x = x.view(-1,14*14*128) # 传入全连接层是必须扁平化处理否则会报错 x = self.fc(x) return x
cost = torch.nn.CrossEntropyLoss()#损失函数采用交叉熵 optimizer = torch.optim.Adam(model.parameters())#参数优化采用Adam优化的是model中所有参数 if Use_gpu: model = model.cuda() n_epochs = 5 for epoch in range(n_epochs): running_loss = 0.0 runing_correct = 0.0 print("Epoch{}/{}".format(epoch,n_epochs)) for data in data_loader_train: X_train,y_train = data if Use_gpu: X_train,y_train = Variable(X_train.cuda()),Variable(y_train.cuda()) else: X_train,y_train = Variable(X_train),Variable(y_train) outputs = model(X_train) _,pred = torch.max(outputs.data,1) optimizer.zero_grad() #梯度清零 loss = cost(outputs,y_train) #计算损失值 loss.backward() #反向传播 optimizer.step() running_loss+=loss.data runing_correct += torch.sum(pred==y_train.data) testing_correct=0 for data in data_loader_test: X_test,y_test = data if Use_gpu: X_test,y_test = Variable(X_test.cuda()),Variable(y_test.cuda()) else: X_test,y_test = Variable(X_test),Variable(y_test) outputs = model(X_test) _,pred = torch.max(outputs.data,1) testing_correct += torch.sum(pred==y_test.data) print("Loss is:{:.4f},Train acc:{:.4f},Test acc:{:.4f}" .format(running_loss/len(data_train), 100*runing_correct/len(data_train), 100*testing_correct/len(data_test))) 输出: Epoch0/5 Loss is:0.0007,Train acc:98.0000,Test acc:98.0000 Epoch1/5 Loss is:0.0005,Train acc:99.0000,Test acc:98.0000 Epoch2/5 Loss is:0.0003,Train acc:99.0000,Test acc:98.0000 Epoch3/5 Loss is:0.0003,Train acc:99.0000,Test acc:98.0000 Epoch4/5 Loss is:0.0002,Train acc:99.0000,Test acc:98.0000
data_loader_test = torch.utils.data.DatalLoader(dataset=data_test,batch_size=4,shuffle=True) X_test,y_test = next(iter(data_loader_test)) if Use_gpu: X_test = Variable(X_test.cuda()) else: X_test = Variable(X_test) pred = model(X_test) _,pred = torch.max(pred,1) print("predict Label is:",[i for i in pred.data]) print("Real Label is:",[i for i in y_test]) img = torchvision.utils.make_grid(X_test) img = img.cpu.numpy().transpose(1,2,0) #cuda数据类型不能直接转numpy需要先变成cpu std = [0.5,0.5,0.5] mean = [0.5,0.5,0.5] img = img*std + mean plt.imshow(img)
深度学习之pytorch实战计算机视觉
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。