注1:
Process finished with exit code 137 (interrupted by signal 9: SIGKILL)
这个错误是爆内存引发的。
注2:
input中图片的大小,并不严格,只要能正常计算即可。
测试 注2:
input 为
(3,488,488)
if __name__ == '__main__':
# test()
a = vgg16_bn()
# print(a)
# b = torch.Tensor(50, 3, 488, 488)
# # b = torch.Tensor(50, 3, 500, 500)
# c = a(b)
b=(3,488,488)
a.cuda()
from torchsummary import summary
summary(a,b)
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 244, 244] 1,792
BatchNorm2d-2 [-1, 64, 244, 244] 128
ReLU-3 [-1, 64, 244, 244] 0
Conv2d-4 [-1, 64, 244, 244] 36,928
BatchNorm2d-5 [-1, 64, 244, 244] 128
ReLU-6 [-1, 64, 244, 244] 0
MaxPool2d-7 [-1, 64, 122, 122] 0
Conv2d-8 [-1, 128, 122, 122] 73,856
BatchNorm2d-9 [-1, 128, 122, 122] 256
ReLU-10 [-1, 128, 122, 122] 0
Conv2d-11 [-1, 128, 122, 122] 147,584
BatchNorm2d-12 [-1, 128, 122, 122] 256
ReLU-13 [-1, 128, 122, 122] 0
MaxPool2d-14 [-1, 128, 61, 61] 0
Conv2d-15 [-1, 256, 61, 61] 295,168
BatchNorm2d-16 [-1, 256, 61, 61] 512
ReLU-17 [-1, 256, 61, 61] 0
Conv2d-18 [-1, 256, 61, 61] 590,080
BatchNorm2d-19 [-1, 256, 61, 61] 512
ReLU-20 [-1, 256, 61, 61] 0
Conv2d-21 [-1, 256, 61, 61] 590,080
BatchNorm2d-22 [-1, 256, 61, 61] 512
ReLU-23 [-1, 256, 61, 61] 0
MaxPool2d-24 [-1, 256, 30, 30] 0
Conv2d-25 [-1, 512, 30, 30] 1,180,160
BatchNorm2d-26 [-1, 512, 30, 30] 1,024
ReLU-27 [-1, 512, 30, 30] 0
Conv2d-28 [-1, 512, 30, 30] 2,359,808
BatchNorm2d-29 [-1, 512, 30, 30] 1,024
ReLU-30 [-1, 512, 30, 30] 0
Conv2d-31 [-1, 512, 30, 30] 2,359,808
BatchNorm2d-32 [-1, 512, 30, 30] 1,024
ReLU-33 [-1, 512, 30, 30] 0
MaxPool2d-34 [-1, 512, 15, 15] 0
Conv2d-35 [-1, 512, 15, 15] 2,359,808
BatchNorm2d-36 [-1, 512, 15, 15] 1,024
ReLU-37 [-1, 512, 15, 15] 0
Conv2d-38 [-1, 512, 15, 15] 2,359,808
BatchNorm2d-39 [-1, 512, 15, 15] 1,024
ReLU-40 [-1, 512, 15, 15] 0
Conv2d-41 [-1, 512, 15, 15] 2,359,808
BatchNorm2d-42 [-1, 512, 15, 15] 1,024
ReLU-43 [-1, 512, 15, 15] 0
MaxPool2d-44 [-1, 512, 7, 7] 0
Linear-45 [-1, 4096] 102,764,544
ReLU-46 [-1, 4096] 0
Dropout-47 [-1, 4096] 0
Linear-48 [-1, 1470] 6,022,590
================================================================
Total params: 123,510,270
Trainable params: 123,510,270
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 2.73
Forward/backward pass size (MB): 380.43
Params size (MB): 471.15
Estimated Total Size (MB): 854.31
----------------------------------------------------------------
Process finished with exit code 0
input 为
b=(3,500,500)
if __name__ == '__main__':
# test()
a = vgg16_bn()
# print(a)
# b = torch.Tensor(50, 3, 488, 488)
# # b = torch.Tensor(50, 3, 500, 500)
# c = a(b)
b=(3,500,500)
a.cuda()
from torchsummary import summary
summary(a,b)
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 250, 250] 1,792
BatchNorm2d-2 [-1, 64, 250, 250] 128
ReLU-3 [-1, 64, 250, 250] 0
Conv2d-4 [-1, 64, 250, 250] 36,928
BatchNorm2d-5 [-1, 64, 250, 250] 128
ReLU-6 [-1, 64, 250, 250] 0
MaxPool2d-7 [-1, 64, 125, 125] 0
Conv2d-8 [-1, 128, 125, 125] 73,856
BatchNorm2d-9 [-1, 128, 125, 125] 256
ReLU-10 [-1, 128, 125, 125] 0
Conv2d-11 [-1, 128, 125, 125] 147,584
BatchNorm2d-12 [-1, 128, 125, 125] 256
ReLU-13 [-1, 128, 125, 125] 0
MaxPool2d-14 [-1, 128, 62, 62] 0
Conv2d-15 [-1, 256, 62, 62] 295,168
BatchNorm2d-16 [-1, 256, 62, 62] 512
ReLU-17 [-1, 256, 62, 62] 0
Conv2d-18 [-1, 256, 62, 62] 590,080
BatchNorm2d-19 [-1, 256, 62, 62] 512
ReLU-20 [-1, 256, 62, 62] 0
Conv2d-21 [-1, 256, 62, 62] 590,080
BatchNorm2d-22 [-1, 256, 62, 62] 512
ReLU-23 [-1, 256, 62, 62] 0
MaxPool2d-24 [-1, 256, 31, 31] 0
Conv2d-25 [-1, 512, 31, 31] 1,180,160
BatchNorm2d-26 [-1, 512, 31, 31] 1,024
ReLU-27 [-1, 512, 31, 31] 0
Conv2d-28 [-1, 512, 31, 31] 2,359,808
BatchNorm2d-29 [-1, 512, 31, 31] 1,024
ReLU-30 [-1, 512, 31, 31] 0
Conv2d-31 [-1, 512, 31, 31] 2,359,808
BatchNorm2d-32 [-1, 512, 31, 31] 1,024
ReLU-33 [-1, 512, 31, 31] 0
MaxPool2d-34 [-1, 512, 15, 15] 0
Conv2d-35 [-1, 512, 15, 15] 2,359,808
BatchNorm2d-36 [-1, 512, 15, 15] 1,024
ReLU-37 [-1, 512, 15, 15] 0
Conv2d-38 [-1, 512, 15, 15] 2,359,808
BatchNorm2d-39 [-1, 512, 15, 15] 1,024
ReLU-40 [-1, 512, 15, 15] 0
Conv2d-41 [-1, 512, 15, 15] 2,359,808
BatchNorm2d-42 [-1, 512, 15, 15] 1,024
ReLU-43 [-1, 512, 15, 15] 0
MaxPool2d-44 [-1, 512, 7, 7] 0
Linear-45 [-1, 4096] 102,764,544
ReLU-46 [-1, 4096] 0
Dropout-47 [-1, 4096] 0
Linear-48 [-1, 1470] 6,022,590
================================================================
Total params: 123,510,270
Trainable params: 123,510,270
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 2.86
Forward/backward pass size (MB): 398.36
Params size (MB): 471.15
Estimated Total Size (MB): 872.37
----------------------------------------------------------------
Code:
# -*- coding: utf-8 -*-
"""
@Time : 2020/08/12 18:30
@Author : Bryce
@File : vgg_yolo.py
@Noice :
@Modificattion :
@Author :
@Time :
@Detail :
"""
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
import math
import torch
import torch.nn.functional as F
__all__ = [
'VGG', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn',
'vgg19_bn', 'vgg19',
]
model_urls = {
'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
}
class VGG(nn.Module):
def __init__(self, features, num_classes=1000, image_size=448):
super(VGG, self).__init__()
self.features = features
self.image_size = image_size
# self.classifier = nn.Sequential(
# nn.Linear(512 * 7 * 7, 4096),
# nn.ReLU(True),
# nn.Dropout(),
# nn.Linear(4096, 4096),
# nn.ReLU(True),
# nn.Dropout(),
# nn.Linear(4096, num_classes),
# )
# if self.image_size == 448:
# self.extra_conv1 = conv_bn_relu(512,512)
# self.extra_conv2 = conv_bn_relu(512,512)
# self.downsample = nn.MaxPool2d(kernel_size=2, stride=2)
self.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 1470),
)
self._initialize_weights()
def forward(self, x):
x = self.features(x)
# if self.image_size == 448:
# x = self.extra_conv1(x)
# x = self.extra_conv2(x)
# x = self.downsample(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
# x = F.sigmoid(x) #归一化到0-1
x = torch.sigmoid(x)
x = x.view(-1,7,7,30)
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def make_layers(cfg, batch_norm=False):
layers = []
in_channels = 3
s = 1
first_flag=True
for v in cfg:
s=1
if (v==64 and first_flag):
s=2
first_flag=False
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, stride=s, padding=1)
if batch_norm:
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)
def conv_bn_relu(in_channels,out_channels,kernel_size=3,stride=2,padding=1):
return nn.Sequential(
nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size,padding=padding,stride=stride),
nn.BatchNorm2d(out_channels),
nn.ReLU(True)
)
cfg = {
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
def vgg11(pretrained=False, **kwargs):
"""VGG 11-layer model (configuration "A")
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['A']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg11']))
return model
def vgg11_bn(pretrained=False, **kwargs):
"""VGG 11-layer model (configuration "A") with batch normalization
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['A'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg11_bn']))
return model
def vgg13(pretrained=False, **kwargs):
"""VGG 13-layer model (configuration "B")
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['B']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg13']))
return model
def vgg13_bn(pretrained=False, **kwargs):
"""VGG 13-layer model (configuration "B") with batch normalization
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['B'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg13_bn']))
return model
def vgg16(pretrained=False, **kwargs):
"""VGG 16-layer model (configuration "D")
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['D']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg16']))
return model
def vgg16_bn(pretrained=False, **kwargs):
"""VGG 16-layer model (configuration "D") with batch normalization
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['D'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg16_bn']))
return model
def vgg19(pretrained=False, **kwargs):
"""VGG 19-layer model (configuration "E")
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['E']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg19']))
return model
def vgg19_bn(pretrained=False, **kwargs):
"""VGG 19-layer model (configuration 'E') with batch normalization
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['E'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg19_bn']))
return model
def test():
import torch
from torch.autograd import Variable
model = vgg16()
model.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 1470),
)
print(model.classifier[6])
#print(model)
img = torch.rand(2,3,224,224)
img = Variable(img)
output = model(img)
print(output.size())
if __name__ == '__main__':
# test()
a=vgg16_bn()
b=torch.Tensor(50,3,488,488)
c=a(b)
得到的
vgg16_bn 网络结构:
VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ReLU(inplace=True)
(6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(7): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(9): ReLU(inplace=True)
(10): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(12): ReLU(inplace=True)
(13): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(14): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(15): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(16): ReLU(inplace=True)
(17): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(18): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(19): ReLU(inplace=True)
(20): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(21): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(22): ReLU(inplace=True)
(23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(24): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(25): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(26): ReLU(inplace=True)
(27): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(28): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(29): ReLU(inplace=True)
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(31): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(32): ReLU(inplace=True)
(33): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(35): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(36): ReLU(inplace=True)
(37): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(38): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(39): ReLU(inplace=True)
(40): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(41): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(42): ReLU(inplace=True)
(43): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(classifier): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace=True)
(2): Dropout(p=0.5, inplace=False)
(3): Linear(in_features=4096, out_features=1470, bias=True)
)
)