代写 deep learning python network cuda GPU

201920201

17 3

1 1
1.1 1
1.2 2
2 3
2.1 4
2.2 5
2.2.1 5
2.2.1 5
2.2.2 6
2.2.3 7
2.2.4 8
2.3 8
2.3.1 PyCharm 8
2.3.2 PyCharm 9
3 9
3.1 9
3.2 9
3.2.1 9
3.2.2 10
3.2.3 10
4 10
4.1 10
4.1.1 10
4.1.2 Resnet50 11
4.2 12
4.2.1 12
4.1.2 VGG16 13
5 14
5.1 14
5.2 15
5.3 15
6 16
6.1 16
6.2 17
7 21
7.1 21
7.2 22
8 23
24
25

1
1.1

a
b
c
d Convolutional Neural NetworkCNN
Pooling Layer

Nair1retifiedRanzato2Susskind3RBMLuo4DBNlogisticCNNCNNCNN
1.2
CNN
5004001008020Resnet
CKangercontemptdisgustfearhappysadnesssurprise7981VGG19
2
Convolutional Neural Networks, CNNArtificial Neural NetworkANN 212080
1
activation function1

j
h
. . .
. . .
. . .
. . .

j
h
. . .
. . .
. . .
. . .
21.
2.1
Convolutional Neural Networks, CNN
visual perceptiongridlike topology
receptive fieldspatial dimension

22
22LeNetAlexNetVGGNetGoogleNetResnet
2.2
2.2.1
RGB
2.2.1

2
2

2
1
0
2
3
9
5
4
2
0
2
3
4
5
6
1
2
3
1
0
0
4
4
2
8
1
0
1
1
0
1
1
0
1

5
0
1

1
1
5

8
1
3

110012
150412
130415
0
23
233355
2.2.2
pooling
1
1
2
4
5
6
7
8
3
2
1
0
1
2
3
4
6
8
3
4

24
24
2.2.3
fully connected layersFC25
A1
A2
A3

W11
W12
W13
W21
W22
W23
W31
W32
W33
A1
A2
A3

W11
W12
W13
W21
W22
W23
W31
W32
W33
25
3
3
4

2.2.4

4
ReLuReLUReLUReLUsigmoidReLuReLU40CNNNReLU2N
2.3
2.3.1 PyCharm
PyCharmPython IDEPythonProjectIDEDjangoWeb
2.3.2 PyCharm
PyCharmIDE Project
PythonDjango
3
3.1
1.131

31
3.2
3.2.1

1
3.2.2

3.2.3

4
4.1
4.1.1
500re4:14001008020
41 re

bus
dinosaur
elephant
flower
horse
Trainfile
80
80
80
80
80
Testfile
20
20
20
20
20

41 re
4.1.2 Resnet50
Residual Network, ResNet2015ILSVRCGoogLeNetInception v3ResNet50221BNResNet5016

64d
256d
relu
relu
relu
relu
relu

64d
256d
relu
relu
relu
relu
relu

42

ResNet42
42 Resnet50
Layer name
output size
18layer
34layer
50layer
101layer
152layer
conv1
112112
77,64 stride2
conv2.x
5656
33 max pool ,stride2

conv3.x
2828

conv4.x
1414

conv5.x
77

11
Average pool,5d,softmax
FLOPs
1.8109
3.6109
3.8109
7.6109
11.3109
4.2
4.2.1
CKangercontemptdisgustfearhappysadnesssurprise7103
43 CK
anger
contempt
disgust
fear
happy
sadness
surprise
135
54
177
75
207
84
249

43 CK

4.1.2 VGG16
VGGNETVGG16133564128256512
VGG1633

44 VGG16

5

5.1

51
51
5.2

52
5
5.3

53
7
6
6.1
1Cnntest.py

2CnnCK.py

3resnet.py
Resnet50
4vgg.py
VGG16
5windowmain.py

6window1.py

7window2.py

8Recog1.py

6.2
1getvariableGPU
def getvariablex:
x Variablex
return x.cuda if torch.cuda.isavailable else x
2Inference
def inferencemodel, imgpath:
model.eval

img Image.openimgpath
img img.resize224, 224
tensor imgtotensorimg

tensor tensor.resize1, 3, 224, 224
tensor tensor.cuda GPU

result modelgetvariabletensor
resultnpy result.data.numpy CPUnumpy
maxindex np.argmaxresultnpy0

return maxindex
3Classifiprob
def Classifiprobmodel, imgpath:
model.eval

img Image.openimgpath
img img.resize224, 224
tensor imgtotensorimg
tensor tensor.resize1, 3, 224, 224
result modelgetvariabletensor
softmax torch.nn.Softmax
result softmaxresult
resultnpy result.data.numpy
return resultnpy0
4vggclassifi
def vggclassifirawimg:
gray rgb2grayrawimg
gray resizegray, 48, 48, modesymmetric.astypenp.uint8
img gray:, :, np.newaxis
img np.concatenateimg, img, img, axis2
img Image.fromarrayimg
inputs transformtestimg
net vgg.VGGVGG19
checkpoint torch.loados.path.joinmodel, PrivateTestmodel.t7, maplocationcpu
net.loadstatedictcheckpointnet
net.eval
ncrops, c, h, w np.shapeinputs
inputs inputs.view1, c, h, w
inputs Variableinputs, volatileTrue
outputs netinputs
outputsavg outputs.viewncrops, 1.mean0 avg over crops
softmax torch.nn.Softmax
score softmaxoutputsavg
scorenpy score.data.numpy
return score
5train
def trainepoch:
printnEpoch: d epoch
global Trainacc
net.train
trainloss 0
correct 0
total 0

if epoch learningratedecaystart and learningratedecaystart 0:
frac epoch learningratedecaystart learningratedecayevery
decayfactor learningratedecayrate frac
currentlr opt.lr decayfactor
utils.setlroptimizer, currentlr set the decayed rate
else:
currentlr opt.lr
printlearningrate: s strcurrentlr

for batchidx, inputs, targets in enumeratetrainloader:
if usecuda:
inputs, targets inputs.cuda, targets.cuda
optimizer.zerograd
inputs, targets Variableinputs, Variabletargets
outputs netinputs
loss criterionoutputs, targets
loss.backward
utils.clipgradientoptimizer, 0.1
optimizer.step

trainloss loss.data0
, predicted torch.maxoutputs.data, 1
total targets.size0
correct predicted.eqtargets.data.cpu.sum

utils.progressbarbatchidx, lentrainloader, Loss: .3f Acc: .3f dd
trainlossbatchidx1, 100.correcttotal, correct, total

Trainacc 100.correcttotal
6test
def testepoch:
global Testacc
global bestTestacc
global bestTestaccepoch
net.eval
PrivateTestloss 0
correct 0
total 0
for batchidx, inputs, targets in enumeratetestloader:
bs, ncrops, c, h, w np.shapeinputs
inputs inputs.view1, c, h, w

if usecuda:
inputs, targets inputs.cuda, targets.cuda
inputs, targets Variableinputs, volatileTrue, Variabletargets
outputs netinputs
outputsavg outputs.viewbs, ncrops, 1.mean1 avg over crops
loss criterionoutputsavg, targets
PrivateTestloss loss.data0
, predicted torch.maxoutputsavg.data, 1
total targets.size0
correct predicted.eqtargets.data.cpu.sum
utils.progressbarbatchidx, lentestloader, Loss: .3f Acc: .3f dd
PrivateTestloss batchidx 1, 100. correct total, correct, total
Save checkpoint.
Testacc 100.correcttotal
if Testacc bestTestacc:
printSaving..
printbestTestacc: 0.3f Testacc
state net: net.statedict if usecuda else net,
bestTestacc: Testacc,
bestTestaccepoch: epoch,

if not os.path.isdiropt.dataset opt.model:
os.mkdiropt.dataset opt.model
if not os.path.isdirpath:
os.mkdirpath
torch.savestate, os.path.joinpath, Testmodel.t7
bestTestacc Testacc
bestTestaccepoch epoch
7OpenImg
def OpenImg:
global imgpng
global testimage
fnamel filedialog.askopenfilenametitle, filetypesS2out, .jpg, All Files,
testimage fnamel
Img Image.openfnamel
Img Img.resize300, 200, Image.ANTIALIAS
imgpng ImageTk.PhotoImageImg
labelImg tk.Labelwindow, imageimgpng
labelImg.placex0, y150
8Resultshow
def Resultshow:
global imgpngc
namelist bus,dinosaur,elephant,flower,horse
numlist Classifiprobresmodel,testimage
colors BEE7E9, 19caad,718dbf, e84d60, E6CEAC
plt.barhrangelennumlist, numlist, colorcolors, ticklabelnamelist
for a, b in ziprangelennumlist, numlist:
plt.textb0.01, a0.05, :.2f .formatb100, hacenter, vatop, fontsize11
plt.savefigresult.jpg
plt.figure
Img Image.openresult.jpg
Img Img.resize550, 350, Image.ANTIALIAS
imgpngc ImageTk.PhotoImageImg
labelImgc tk.Labelwindow, imageimgpngc
labelImgc.placex300, y50
tk.Labelwindow, width40, text,:.2f.formatnamelistnumlist.tolist.indexmaxnumlist,maxnumlist100,
font, 13.placex500, y450
9Exit
def Exit:
sys.exit

7
7.1

71 a

72 b

7.2

73 a

74 b

8

1 Nair V, Hinton G E. Rectified linear units improve restricted boltzmann machinesC International Conference on International Conference on Machine Learning. Omnipress, 2010:807814.
2 Ranzato M, Susskind J, Mnih V, et al. On deep generative models with applications to recognitionC Computer Vision and Pattern Recognition. IEEE, 2011:28572864.
3 Susskind J, Memisevic R, Hinton G, et al. Modeling the joint density of two images under a variety of transformationsC IEEE Conference on Computer Vision and Pattern Recognition. IEEE Computer Society, 2011:27932800.
4 Luo P. Hierarchical face parsing via deep learningC IEEE Conference on Computer Vision and Pattern Recognition. IEEE Computer Society, 2012:24802487.
5 . CNNJ. . 201604: 871878.
6 . CNNJ. . 201608: 4550.
7 . J. . 201604: 10331038.
8 . J. . 201609: 5557.
9 . J. . 201601: 1622.

Cnntest.py
import torch
from torch.utils.data import DataLoader
from torchvision import datasets,models,transforms
from torch.autograd import Variable
import time

def getvariablex:
x Variablex
return x.cuda if torch.cuda.isavailable else x

datatransform transforms.Composetransforms.Resize224,224,
transforms.ToTensor,
transforms.Normalizemean0.5,0.5,0.5,
std0.5,0.5,0.5

traindatasets datasets.ImageFolderrootC:UsersAdministratorDesktopretrain,
transformdatatransform

testdatasets datasets.ImageFolderrootC:UsersAdministratorDesktopretest,
transformdatatransform

traindataloader DataLoaderdatasettraindatasets,
batchsize20,
shuffleTrue
testdataloader DataLoaderdatasettestdatasets,
batchsize20,
shuffleTrue
Xexample,Yexamplenextitertraindataloader
exampleclassestraindatasets.classes
indexclassestraindatasets.classtoidx

modelmodels.vgg19pretrainedTrue

Usegputorch.cuda.isavailable

for param in model.parameters:
param.requiresgridFalse

model.fctorch.nn.Linear2048,5

if Usegpu:
modelmodel.cuda

lossftorch.nn.CrossEntropyLoss
optimizertorch.optim.Adammodel.fc.parameters,lr0.00001

epochn50
timeopentime.time

train
for epoch in rangeepochn:
runningloss 0.0
runningacc 0.0
for i,img, labels in enumeratetraindataloader,1:
img getvariableimg
labels getvariablelabels
optimizer.zerograd
outputs modelimg
loss lossfoutputs, labels
loss.backward
optimizer.step

runningloss loss.item
, predict torch.maxoutputs, 1
correctnum torch.sumpredict labels
runningacc correctnum.item
runningloss lentraindataloader
runningacc lentraindataloader
if i 1 1 0:
printTrain Epoch:dd Iter dd .1f Loss: .5f epoch 1, epochn, ilentraindataloader,lentraindataloader.dataset, 100ilentraindataloaderlentraindataloader.dataset,loss.item
timeend time.time timeopen
printtimeend
runningloss 0.0
runningacc 0.0

test
correct 0
total 0
for j,images, labels in enumeratetestdataloader,1:
images getvariableimages
labels getvariablelabels
outputs modelimages
, predicted torch.maxoutputs.data, 1
total labels.size0
correct predicted labels.data.sum
printTest Acc:dd d correct,total,100 correct total
timeend time.time timeopen
printtimeend

torch.savemodel,C:UsersAdministratorDesktopmodel.pkl

CnnCK.py
from future import printfunction

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import transforms as transforms
import numpy as np
import os
import argparse
import utils
from CK import CK
from torch.autograd import Variable
from models import
import vgg

parser argparse.ArgumentParserdescriptionPyTorch CK CNN Training
parser.addargumentmodel, typestr, defaultVGG19, helpCNN architecture
parser.addargumentdataset, typestr, defaultCK, helpdataset
parser.addargumentfold, default1, typeint, helpk fold number
parser.addargumentbs, default128, typeint, helpbatchsize
parser.addargumentlr, default0.01, typefloat, helplearning rate
parser.addargumentresume, r, actionstoretrue, helpresume from checkpoint
opt parser.parseargs

usecuda torch.cuda.isavailable

bestTestacc 0 best PrivateTest accuracy
bestTestaccepoch 0
startepoch 0 start from epoch 0 or last checkpoint epoch

learningratedecaystart 20 50
learningratedecayevery 1 5
learningratedecayrate 0.8 0.9

cutsize 44
totalepoch 60

path os.path.joinopt.dataset opt.model, stropt.fold

Data
print Preparing data..
transformtrain transforms.Compose
transforms.RandomCropcutsize,
transforms.RandomHorizontalFlip,
transforms.ToTensor,

transformtest transforms.Compose
transforms.TenCropcutsize,
transforms.Lambdalambda crops: torch.stacktransforms.ToTensorcrop for crop in crops,

trainset CKsplit Training, fold opt.fold, transformtransformtrain
trainloader torch.utils.data.DataLoadertrainset, batchsizeopt.bs, shuffleTrue, numworkers1
testset CKsplit Testing, fold opt.fold, transformtransformtest
testloader torch.utils.data.DataLoadertestset, batchsize5, shuffleFalse, numworkers1

Model
if opt.model VGG19:
net vgg.VGGVGG19
elif opt.model Resnet18:
net ResNet18

if opt.resume:
Load checkpoint.
print Resuming from checkpoint..
assert os.path.isdirpath, Error: no checkpoint directory found!
checkpoint torch.loados.path.joinpath,Testmodel.t7

net.loadstatedictcheckpointnet
bestTestacc checkpointbestTestacc
bestTestaccepoch checkpointbestTestaccepoch
startepoch bestTestaccepoch 1
else:
print Building model..

if usecuda:
net.cuda

criterion nn.CrossEntropyLoss
optimizer optim.SGDnet.parameters, lropt.lr, momentum0.9, weightdecay5e4

Training
def trainepoch:
printnEpoch: d epoch
global Trainacc
net.train
trainloss 0
correct 0
total 0

if epoch learningratedecaystart and learningratedecaystart 0:
frac epoch learningratedecaystart learningratedecayevery
decayfactor learningratedecayrate frac
currentlr opt.lr decayfactor
utils.setlroptimizer, currentlr set the decayed rate
else:
currentlr opt.lr
printlearningrate: s strcurrentlr

for batchidx, inputs, targets in enumeratetrainloader:
if usecuda:
inputs, targets inputs.cuda, targets.cuda
optimizer.zerograd
inputs, targets Variableinputs, Variabletargets
outputs netinputs
loss criterionoutputs, targets
loss.backward
utils.clipgradientoptimizer, 0.1
optimizer.step

trainloss loss.data0
, predicted torch.maxoutputs.data, 1
total targets.size0
correct predicted.eqtargets.data.cpu.sum

utils.progressbarbatchidx, lentrainloader, Loss: .3f Acc: .3f dd
trainlossbatchidx1, 100.correcttotal, correct, total

Trainacc 100.correcttotal

def testepoch:
global Testacc
global bestTestacc
global bestTestaccepoch
net.eval
PrivateTestloss 0
correct 0
total 0
for batchidx, inputs, targets in enumeratetestloader:
bs, ncrops, c, h, w np.shapeinputs
inputs inputs.view1, c, h, w

if usecuda:
inputs, targets inputs.cuda, targets.cuda
inputs, targets Variableinputs, volatileTrue, Variabletargets
outputs netinputs
outputsavg outputs.viewbs, ncrops, 1.mean1 avg over crops

loss criterionoutputsavg, targets
PrivateTestloss loss.data0
, predicted torch.maxoutputsavg.data, 1
total targets.size0
correct predicted.eqtargets.data.cpu.sum

utils.progressbarbatchidx, lentestloader, Loss: .3f Acc: .3f dd
PrivateTestloss batchidx 1, 100. correct total, correct, total
Save checkpoint.
Testacc 100.correcttotal

if Testacc bestTestacc:
printSaving..
printbestTestacc: 0.3f Testacc
state net: net.statedict if usecuda else net,
bestTestacc: Testacc,
bestTestaccepoch: epoch,

if not os.path.isdiropt.dataset opt.model:
os.mkdiropt.dataset opt.model
if not os.path.isdirpath:
os.mkdirpath
torch.savestate, os.path.joinpath, Testmodel.t7
bestTestacc Testacc
bestTestaccepoch epoch

for epoch in rangestartepoch, totalepoch:
trainepoch
testepoch

printbestTestacc: 0.3f bestTestacc
printbestTestaccepoch: d bestTestaccepoch

resnet.py
import torch
import torch.nn as nn
import torch.nn.functional as F

from torch.autograd import Variable

class BasicBlocknn.Module:
expansion 1

def initself, inplanes, planes, stride1:
superBasicBlock, self.init
self.conv1 nn.Conv2dinplanes, planes, kernelsize3, stridestride, padding1, biasFalse
self.bn1 nn.BatchNorm2dplanes
self.conv2 nn.Conv2dplanes, planes, kernelsize3, stride1, padding1, biasFalse
self.bn2 nn.BatchNorm2dplanes

self.shortcut nn.Sequential
if stride ! 1 or inplanes ! self.expansionplanes:
self.shortcut nn.Sequential
nn.Conv2dinplanes, self.expansionplanes, kernelsize1, stridestride, biasFalse,
nn.BatchNorm2dself.expansionplanes

def forwardself, x:
out F.reluself.bn1self.conv1x
out self.bn2self.conv2out
out self.shortcutx
out F.reluout
return out

class Bottlenecknn.Module:
expansion 4

def initself, inplanes, planes, stride1:
superBottleneck, self.init
self.conv1 nn.Conv2dinplanes, planes, kernelsize1, biasFalse
self.bn1 nn.BatchNorm2dplanes
self.conv2 nn.Conv2dplanes, planes, kernelsize3, stridestride, padding1, biasFalse
self.bn2 nn.BatchNorm2dplanes
self.conv3 nn.Conv2dplanes, self.expansionplanes, kernelsize1, biasFalse
self.bn3 nn.BatchNorm2dself.expansionplanes

self.shortcut nn.Sequential
if stride ! 1 or inplanes ! self.expansionplanes:
self.shortcut nn.Sequential
nn.Conv2dinplanes, self.expansionplanes, kernelsize1, stridestride, biasFalse,
nn.BatchNorm2dself.expansionplanes

def forwardself, x:
out F.reluself.bn1self.conv1x
out F.reluself.bn2self.conv2out
out self.bn3self.conv3out
out self.shortcutx
out F.reluout
return out

class ResNetnn.Module:
def initself, block, numblocks, numclasses7:
superResNet, self.init
self.inplanes 64

self.conv1 nn.Conv2d3, 64, kernelsize3, stride1, padding1, biasFalse
self.bn1 nn.BatchNorm2d64
self.layer1 self.makelayerblock, 64, numblocks0, stride1
self.layer2 self.makelayerblock, 128, numblocks1, stride2
self.layer3 self.makelayerblock, 256, numblocks2, stride2
self.layer4 self.makelayerblock, 512, numblocks3, stride2
self.linear nn.Linear512, numclasses

def makelayerself, block, planes, numblocks, stride:
strides stride 1numblocks1
layers
for stride in strides:
layers.appendblockself.inplanes, planes, stride
self.inplanes planes block.expansion
return nn.Sequentiallayers

def forwardself, x:
out F.reluself.bn1self.conv1x
out self.layer1out
out self.layer2out
out self.layer3out
out self.layer4out
out F.avgpool2dout, 4
out out.viewout.size0, 1
out F.dropoutout, p0.5, trainingself.training
out self.linearout
return out

def ResNet18:
return ResNetBasicBlock, 2,2,2,2

vgg.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

cfg
VGG11: 64, M, 128, M, 256, 256, M, 512, 512, M, 512, 512, M,
VGG13: 64, 64, M, 128, 128, M, 256, 256, M, 512, 512, M, 512, 512, M,
VGG16: 64, 64, M, 128, 128, M, 256, 256, 256, M, 512, 512, 512, M, 512, 512, 512, M,
VGG19: 64, 64, M, 128, 128, M, 256, 256, 256, 256, M, 512, 512, 512, 512, M, 512, 512, 512, 512, M,

class VGGnn.Module:
def initself, vggname:
superVGG, self.init
self.features self.makelayerscfgvggname
self.classifier nn.Linear512, 7

def forwardself, x:
out self.featuresx
out out.viewout.size0, 1
out F.dropoutout, p0.5, trainingself.training
out self.classifierout
return out

def makelayersself, cfg:
layers
inchannels 3
for x in cfg:
if x M:
layers nn.MaxPool2dkernelsize2, stride2
else:
layers nn.Conv2dinchannels, x, kernelsize3, padding1,
nn.BatchNorm2dx,
nn.ReLUinplaceTrue
inchannels x
layers nn.AvgPool2dkernelsize1, stride1
return nn.Sequentiallayers