def prepare_model(self): l1_dense = sym.FullyConnected(data=sym.Variable("data"), num_hidden=80, no_bias=False, name="l1_dense") l2_dense = sym.FullyConnected(data=l1_dense, num_hidden=120, no_bias=True, name="l2_dense") l2_activation = sym.Activation(data=l2_dense, act_type="relu", name="l2_activation") l3_dense = sym.FullyConnected(data=l2_activation, num_hidden=60, no_bias=True, name="l3_dense") l3_activation = sym.Activation(data=l3_dense, act_type="relu", name="l3_activation") l4_dense = sym.FullyConnected(data=l3_activation, num_hidden=20, no_bias=True, name="l4_dense") l4_activation = sym.Activation(data=l4_dense, act_type="relu", name="l4_activation") self.l5_dense = sym.FullyConnected(data=l4_activation, num_hidden=1, no_bias=False, name="l5_dense") output = sym.MAERegressionOutput(data=self.l5_dense, label=sym.Variable("target")) self.train_module = mod.Module(symbol=output, data_names=["data"], label_names=["target"])
def _check_onnx_export(net, group_outputs=False, shape_type=tuple, extra_params={}): net.initialize() data = nd.random.uniform(0, 1, (1, 1024)) output = _force_list(net(data)) # initialize weights net_sym = _optional_group(net(sym.Variable('data')), group_outputs) net_params = { name: param._reduce() for name, param in net.collect_params().items() } net_params.update(extra_params) with tempfile.TemporaryDirectory() as tmpdirname: onnx_file_path = os.path.join(tmpdirname, 'net.onnx') export_path = onnx_mxnet.export_model( sym=net_sym, params=net_params, input_shape=[shape_type(data.shape)], onnx_file_path=onnx_file_path) assert export_path == onnx_file_path # Try importing the model to symbol _assert_sym_equal(net_sym, onnx_mxnet.import_model(export_path)[0]) # Try importing the model to gluon imported_net = onnx_mxnet.import_to_gluon(export_path, ctx=None) _assert_sym_equal( net_sym, _optional_group(imported_net(sym.Variable('data')), group_outputs)) # Confirm network outputs are the same imported_net_output = _force_list(imported_net(data)) for out, imp_out in zip(output, imported_net_output): mx.test_utils.assert_almost_equal(out.asnumpy(), imp_out.asnumpy())
def __init__(self, model: BinaryBinaryRBM): self.model = model self.X = sym.Variable('X') self.n_visible = model.n_visible self.n_hidden = model.n_hidden self.W = sym.Variable('W', dtype='float32') self.v_bias = sym.Variable('v_bias', dtype='float32') self.h_bias = sym.Variable('h_bias', dtype='float32')
def get_symbol(num_classes=10, flag='training', add_stn=False, **kwargs): data = sym.Variable('data') if add_stn: data = sym.SpatialTransformer(data=data, loc=get_loc(data), target_shape=(28, 28), transform_type="affine", sampler_type="bilinear") # first conv conv1 = sym.Convolution(data=data, kernel=(5, 5), num_filter=10) relu1 = sym.Activation(data=conv1, act_type="relu") pool1 = sym.Pooling(data=relu1, pool_type="max", kernel=(2, 2), stride=(2, 2)) # second conv conv2 = sym.Convolution(data=pool1, kernel=(5, 5), num_filter=20) relu2 = sym.Activation(data=conv2, act_type="relu") pool2 = sym.Pooling(data=relu2, pool_type="max", kernel=(2, 2), stride=(2, 2)) drop1 = mx.sym.Dropout(data=pool2) # first fullc flatten = sym.Flatten(data=drop1) fc1 = sym.FullyConnected(data=flatten, num_hidden=50) relu3 = sym.Activation(data=fc1, act_type="relu") # second fullc drop2 = mx.sym.Dropout(data=relu3, mode=flag) fc2 = sym.FullyConnected(data=drop2, num_hidden=num_classes) # loss lenet = sym.SoftmaxOutput(data=fc2, name='softmax') return lenet
def getSSD(): """ This is for generation of ssd network symbols :return: """ net = sym.Variable('data') net = sym.Convolution(data=net, num_filter=128) pass
def _get_onnx_sym(model: gluon.HybridBlock, num_inputs: int) -> sym.Symbol: """ Returns a symbolic graph for the model :param model: gluon HybridBlock that constructs the symbolic graph :param num_inputs: number of inputs to the graph :return: symbol for the network """ var_args = [ sym.Variable('Data{}'.format(i)) for i in range(num_inputs) ] with ScopedOnnxEnable(model): return sym.Group( gluon.block._flatten(model(*var_args), "output")[0])
def test_onnx_export_multi_output(): class MultiOutputBlock(nn.HybridBlock): def __init__(self): super(MultiOutputBlock, self).__init__() with self.name_scope(): self.net = nn.HybridSequential() for i in range(10): self.net.add(nn.Dense(100 + i * 10, activation='relu')) def hybrid_forward(self, F, x): out = tuple(block(x) for block in self.net._children.values()) return out net = MultiOutputBlock() assert len(sym.Group(net(sym.Variable('data'))).list_outputs()) == 10 _check_onnx_export(net, group_outputs=True)
def test(): x = nd.random.normal(0, 1, (100, 3, 16, 16)) nd_iter = mx.io.NDArrayIter(data={'data': x}, batch_size=25) in_var = sym.Variable('data') vn = getConvLayer(in_var, "conv1", 1, (3, 3)) mx.viz.plot_network(symbol=vn, node_attrs={ "shape": "oval", "fixedsize": "false" }) net = mod.Module(symbol=getConvLayer(in_var, 'conv1', 1, (3, 3))) net.bind(data_shapes=nd_iter.provide_data, label_shapes=nd_iter.provide_label) net.init_params() net.forward(nd_iter.provide_data()) print(x)
def _cbam(x, name, channels, reduction, act_type='relu', spatial_dilate=0): """ 启用 CBAM :param x: 输入 :param name: operator name :param channels: 输出 channels :param reduction: MLP reduction :param act_type: MLP activation :param spatial_dilate: spatial_dilate > 0,对于 spatial 新添加一个 dilate conv :return: """ # =============================== channel # Pooling [N, C, 1, 1] max_pool = sym.Pooling(x, pool_type='max', global_pool=True, name=f'{name}_max') avg_pool = sym.Pooling(x, pool_type='avg', global_pool=True, name=f'{name}_avg') # MLP FC1 [N, C // reduction, 1, 1] mlp_fc1_weight = sym.Variable(f'{name}_mlp_fc1_weight', shape=(channels // reduction, 0, 1, 1)) mlp_fc1_bias = sym.Variable(f'{name}_mlp_fc1_bias', shape=(channels // reduction, ), init=mx.init.Constant(0.)) max_pool = sym.Convolution(max_pool, num_filter=channels // reduction, kernel=(1, 1), stride=(1, 1), pad=(0, 0), weight=mlp_fc1_weight, bias=mlp_fc1_bias, name=f'{name}_max_fc1') avg_pool = sym.Convolution(avg_pool, num_filter=channels // reduction, kernel=(1, 1), stride=(1, 1), pad=(0, 0), weight=mlp_fc1_weight, bias=mlp_fc1_bias, name=f'{name}_avg_fc1') max_pool = _activate(max_pool, act_type, name=f'{name}_max_fc1') avg_pool = _activate(avg_pool, act_type, name=f'{name}_avg_fc1') # MLP FC2 [N, C, 1, 1] mlp_fc2_weight = sym.Variable(f'{name}_mlp_fc2_weight', shape=(channels, 0, 1, 1)) mlp_fc2_bias = sym.Variable(f'{name}_mlp_fc2_bias', shape=(channels, ), init=mx.init.Constant(0.)) max_pool = sym.Convolution(max_pool, num_filter=channels, kernel=(1, 1), stride=(1, 1), pad=(0, 0), weight=mlp_fc2_weight, bias=mlp_fc2_bias, name=f'{name}_max_fc2') avg_pool = sym.Convolution(avg_pool, num_filter=channels, kernel=(1, 1), stride=(1, 1), pad=(0, 0), weight=mlp_fc2_weight, bias=mlp_fc2_bias, name=f'{name}_avg_fc2') channel_attention = _activate(max_pool + avg_pool, 'sigmoid', name=f'{name}_channel') y = sym.broadcast_mul(x, channel_attention, name=f'{name}_channel_out') # =============================== spatial max_spatial = sym.max(y, axis=1, keepdims=True, name=f'{name}_max_spatial') avg_spatial = sym.mean(y, axis=1, keepdims=True, name=f'{name}_avg_spatial') spatial = sym.Concat(max_spatial, avg_spatial, dim=1, name=f'{name}_spatial_concat') if spatial_dilate > 0: dilate_spatial = _conv(y, f'{name}_spatial_dilate{spatial_dilate}', 1, kernel=3, stride=1, pad=spatial_dilate, dilate=spatial_dilate, no_bias=False) spatial = sym.Concat(spatial, dilate_spatial, dim=1, name=f'{name}_spatial_concat_dilate') spatial_attention = _conv(spatial, f'{name}_spatial_conv', 1, kernel=7, stride=1, pad=3, groups=1, act_type='sigmoid') y = sym.broadcast_mul(y, spatial_attention, name=f'{name}_spatial_out') return y
def getBaseNet(): net = sym.Variable('data') pass