MXnet公司
我在学MXnet
https://gluon-cv.mxnet.io/index.html
)。为了从工程师那里学习编程技能,我用
.我用来初始化这个类的参数与官方的相同
网络
.
from gluoncv.model_zoo.ssd import SSD
import mxnet as mx
name = 'resnet50_v1'
base_size = 512
features=['stage3_activation5', 'stage4_activation2']
filters=[512, 512, 256, 256]
sizes=[51.2, 102.4, 189.4, 276.4, 363.52, 450.6, 492]
ratios=[[1, 2, 0.5]] + [[1, 2, 0.5, 3, 1.0/3]] * 3 + [[1, 2, 0.5]] * 2
steps=[16, 32, 64, 128, 256, 512]
classes=('car', 'pedestrian', 'truck', 'trafficLight', 'biker')
pretrained=True
net = SSD(network = name, base_size = base_size, features = features,
num_filters = filters, sizes = sizes, ratios = ratios, steps = steps,
pretrained=pretrained, classes=classes)
数据X
x = mx.nd.zeros(shape=(batch_size,3,base_size,base_size))
cls_preds, box_preds, anchors = net(x)
RuntimeError: Parameter 'ssd0_expand_trans_conv0_weight' has not been initialized. Note that you should initialize parameters and create Trainer with Block.collect_params() instead of Block.params because the later does not include Parameters of nested child Blocks
这是合理的。SSD使用功能
在“”上添加新层_
_我忘了初始化它们。所以,我使用以下代码。
net.initialize()
v.initialize(None, ctx, init, force_reinit=force_reinit)
C:\Users\Bird\AppData\Local\conda\conda\envs\ssd\lib\site-packages\mxnet\gluon\parameter.py:687: UserWarning: Parameter 'ssd0_resnetv10_stage4_batchnorm9_running_mean' is already initialized, ignoring. Set force_reinit=True to re-initialize.
v.initialize(None, ctx, init, force_reinit=force_reinit)
C:\Users\Bird\AppData\Local\conda\conda\envs\ssd\lib\site-packages\mxnet\gluon\parameter.py:687: UserWarning: Parameter 'ssd0_resnetv10_stage4_batchnorm9_running_var' is already initialized, ignoring. Set force_reinit=True to re-initialize.
v.initialize(None, ctx, init, force_reinit=force_reinit)
的
“这是SSD的基础,因此无法安装这些参数。然而,这些警告很烦人。
不过,在这里,
net.save_params('F:/Temps/Models_tmp/' +'myssd.params')
的参数文件_
97.7毫巴
9.96兆字节
为了测试这项新技术,我打开了
一个新的控制台
重建同一个网络。然后,我加载保存的参数并向其提供数据。
net.load_params('F:/Temps/Models_tmp/' +'myssd.params')
x = mx.nd.zeros(shape=(batch_size,3,base_size,base_size))
初始化错误再次出现
.
运行时错误:参数'ssd0_expand_trans_conv0_weight'尚未初始化。请注意,您应该初始化参数并使用block.collect_params()而不是block.params创建trainer,因为后者不包括嵌套子块的参数。
这是不正确的,因为保存的文件'mysd.params'应包含我的网络的所有已安装参数。
找到街区_
SSD0_展开_trans_conv0
_,我在__做了更深入的研究
Gluoncv.nn.特性。功能扩展器
_ _我用__
mxnet.胶子。神经传导二维
_
MX.SYM.卷积
__中的_
功能扩展器
_
'''
y = mx.sym.Convolution(
y, num_filter=num_trans, kernel=(1, 1), no_bias=use_bn,
name='expand_trans_conv{}'.format(i), attr={'__init__': weight_init})
'''
Conv1 = nn.Conv2D(channels = num_trans,kernel_size = (1, 1),use_bias = use_bn,weight_initializer = weight_init)
y = Conv1(y)
Conv1.initialize(verbose = True)
'''
y = mx.sym.Convolution(
y, num_filter=f, kernel=(3, 3), pad=(1, 1), stride=(2, 2),
no_bias=use_bn, name='expand_conv{}'.format(i), attr={'__init__': weight_init})
'''
Conv2 = nn.Conv2D(channels = f,kernel_size = (3, 3),padding = (1, 1),strides = (2, 2),use_bias = use_bn, weight_initializer = weight_init)
y = Conv2(y)
Conv2.initialize(verbose = True)
这些新块可以手动初始化。然而,MXnet仍然报告
同样的错误
.
手动初始化似乎没有效果。
如何保存网络的所有参数并恢复它们?