备注:
版本要求:halcon21.05++
import torch
import torch.nn as nn
import torchvision
import numpy as np
print("PyTorch Version: ",torch.__version__)
print("Torchvision Version: ",torchvision.__version__)
__all__ = ['ResNet50', 'ResNet101','ResNet152']
def Conv1(in_planes, places, stride=2):
return nn.Sequential(
nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
class Bottleneck(nn.Module):
def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 4):
super(Bottleneck,self).__init__()
self.expansion = expansion
self.downsampling = downsampling
self.bottleneck = nn.Sequential(
nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=places, out_channels=places*self.expansion, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(places*self.expansion),
)
if self.downsampling:
self.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(places*self.expansion)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
residual = x
out = self.bottleneck(x)
if self.downsampling:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self,blocks, num_classes=1000, expansion = 4):
super(ResNet,self).__init__()
self.expansion = expansion
self.conv1 = Conv1(in_planes = 3, places= 64)
self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)
self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)
self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)
self.avgpool = nn.AvgPool2d(7, stride=1)
self.fc = nn.Linear(2048,num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def make_layer(self, in_places, places, block, stride):
layers = []
layers.append(Bottleneck(in_places, places,stride, downsampling =True))
for i in range(1, block):
layers.append(Bottleneck(places*self.expansion, places))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def ResNet50():
return ResNet([3, 4, 6, 3])
def ResNet101():
return ResNet([3, 4, 23, 3])
def ResNet152():
return ResNet([3, 8, 36, 3])
if __name__=='__main__':
#model = torchvision.models.resnet50()
model = ResNet50()
print(model)
input = torch.randn(1, 3, 224, 224)
out = model(input)
print(out.shape)
*********************************************************************************
* InputWidth := 800
* InputHeight := 800
* InputDepth := 1
* ResNet := [3,4,6,3,'ResNet50']
* ResNet := [3,4,23,3,'ResNet101']
* ResNet := [3,8,36,3,'ResNet152']
* class_num := 15
*********************************************************************************
*输入层
create_dl_layer_input ('input', [InputWidth,InputHeight,InputDepth], 'allow_smaller_tuple', 'false', input)
*'conv1'
create_dl_layer_convolution (input, 'conv1', [7,7], [1,1], [2,2], 64, 1, 'half_kernel_size', 'none', \
['weight_filler','weight_filler_variance_norm','weight_filler_const_val','bias_filler',\
'bias_filler_variance_norm','bias_filler_const_val','bias_term'],\
['msra','norm_in',0.0,'const','norm_in',0.0,'true'], conv1)
*'batchnorm1'
create_dl_layer_batch_normalization (conv1, 'batchnorm1', 0.9, 0.0001, 'relu', \
['weight_filler','weight_filler_variance_norm','weight_filler_const_val','bias_filler','bias_filler_variance_norm','bias_filler_const_val','bias_term'], \
['xavier','norm_in',1.0,'const','norm_in',0.0,'true'], batchnorm1)
*'pool1'
create_dl_layer_pooling (batchnorm1, 'pool1', [3,3], [2,2], 'half_kernel_size', 'maximum', [], [], pool1)
***************************************'res0_block'******************************
*'res0_block0'
res0_sum_layer := 'res0_block0_sum'
res0_block0 (pool1, 0, res0_sum_layer)
*res0_block1~3
for Index := 1 to ResNet[0]-1 by 1
neme_conv := 'res0_block'+Index+'_conv'
res0_block (res0_sum_layer, Index, neme_conv)
res0_sum_layer_old := res0_sum_layer
res0_sum_layer := 'res0_block'+Index+'_sum'
create_dl_layer_elementwise ([res0_sum_layer_old,neme_conv], res0_sum_layer, 'sum', [], [], [], res0_sum_layer)
endfor
************************************res1_block***********************************
*res1_block0
res1_sum_layer := 'res1_block0_sum'
res1_block0 (res0_sum_layer, 0, res1_sum_layer)
*res2_block1~4
for Index := 1 to ResNet[1]-1 by 1
neme_conv := 'res1_block'+Index+'_conv'
res1_block (res1_sum_layer, Index, neme_conv)
res1_sum_layer_old := res1_sum_layer
res1_sum_layer := 'res1_block'+Index+'_sum'
create_dl_layer_elementwise ([res1_sum_layer_old,neme_conv], res1_sum_layer, 'sum', [], [], [], res1_sum_layer)
endfor
*************************************res2_block**********************************
*res2_block0
res2_sum_layer := 'res2_block0_sum'
res2_block0 (res1_sum_layer, 0, res2_sum_layer)
*res2_block1~23
for Index := 1 to ResNet[2]-1 by 1
neme_conv := 'res2_block'+Index+'_conv'
res2_block (res2_sum_layer, Index, neme_conv)
res2_sum_layer_old := res2_sum_layer
res2_sum_layer := 'res2_block'+Index+'_sum'
create_dl_layer_elementwise ([res2_sum_layer_old,neme_conv], res2_sum_layer, 'sum', [], [], [], res2_sum_layer)
endfor
*********************************res3_block**************************************
*res3_block0
res3_sum_layer := 'res3_block0_sum'
res3_block0 (res2_sum_layer, 0, res3_sum_layer)
*res2_block1~23
for Index := 1 to ResNet[3]-1 by 1
neme_conv := 'res3_block'+Index+'_conv'
res3_block (res3_sum_layer, Index, neme_conv)
res3_sum_layer_old := res3_sum_layer
res3_sum_layer := 'res3_block'+Index+'_sum'
create_dl_layer_elementwise ([res3_sum_layer_old,neme_conv], res3_sum_layer, 'sum', [], [], [], res3_sum_layer)
endfor
**********************************************************************************
*'bn2'
create_dl_layer_batch_normalization (res3_sum_layer, 'bn2', 0.9, 0.0001, 'relu', \
['weight_filler','weight_filler_variance_norm','weight_filler_const_val','bias_filler','bias_filler_variance_norm','bias_filler_const_val','bias_term'], \
['xavier','norm_in',1.0,'const','norm_in',0.0,'true'], bn2)
*'avgpool'
create_dl_layer_pooling (bn2, 'avgpool', [1,1], [1,1], 'none', 'global_average', [], [], avgpool)
*Flatten()
*'dense'
create_dl_layer_dense (avgpool, 'dense',class_num, \
['weight_filler','weight_filler_variance_norm','weight_filler_const_val','bias_filler', 'bias_filler_variance_norm','bias_filler_const_val','bias_term'], \
['xavier','norm_in',1.0,'const','norm_in',0.0,'true'], dense)
*'output'
create_dl_layer_softmax (dense, 'output', [], [], DLLayerSoftMax)
create_dl_layer_input ('image_label_id', [1,1,1], 'allow_smaller_tuple', 'false', DLLayerTarget)
create_dl_layer_input ('weights', [1,1,1], 'allow_smaller_tuple', 'false', DLLayerWeights)
*'ce2d_loss'
create_dl_layer_loss_cross_entropy (DLLayerSoftMax, DLLayerTarget, DLLayerWeights, 'loss_cross_entropy',1.0, [], [], DLLayerLossCrossEntropy)
create_dl_model (DLLayerLossCrossEntropy, DLModelHandle)
set_dl_model_param (DLModelHandle, 'type', 'classification')
set_dl_model_param (DLModelHandle, 'image_width', InputWidth)
set_dl_model_param (DLModelHandle, 'image_height', InputHeight)
get_dl_model_param (DLModelHandle, 'summary', NetworkSummary)
set_dl_model_param (DLModelHandle, 'weight_prior',0.001)
*'backbone_docking_layers'
set_dl_model_param (DLModelHandle, 'backbone_docking_layers', ['res0_block2_sum','res1_block3_sum'\
,'res2_block5_sum','res3_block2_sum'])
*'optimize_for_inference'
set_dl_model_param (DLModelHandle, 'optimize_for_inference', 'true')
*'batch_size'
set_dl_model_param (DLModelHandle, 'batch_size', 10)
*********************************************************************************
* get_dl_model_layer_param (DLModelHandle, 'convolution2', 'activation_mode', ActivationModeConvolution2)
* get_dl_layer_param (DLLayerConvolution2, 'input_layer', InputLayerConvolution2)
*
* set_dl_model_layer_param (DLModelHandle, 'convolution1', 'name', 'convolution1_with_relu')
* set_dl_model_layer_param (DLModelHandle, 'convolution2', 'name', 'convolution2_with_relu')
create_dict (MetaData)
set_dict_tuple (MetaData, 'model_creator', 'Deep-Bool')
* set_dict_tuple (MetaData, 'model_info', ResNet[4])
set_dict_tuple (MetaData, 'model_info', 'Deep-Bool:QQ480824932')
set_dl_model_param (DLModelHandle, 'meta_data', MetaData)
*保存模型
write_dl_model (DLModelHandle,ResNet[4]+'.hdl')
return ()