def test_AvgPool(): X = ad.Variable(name="X") y = ad.avg_pool2d_op(X, kernel_H=2, kernel_W=2, padding=0, stride=2) executor = ad.Executor([y], ctx=ctx) X_val = rand.normal(scale=0.1, size=(batch_size, 10, 10, 10)).astype(np.float32) res = executor.run(feed_dict={X: X_val}) Check(executor, res, [X], [y], [X_val]) print(sys._getframe().f_code.co_name, 'pass!')
def cnn(executor_ctx=None, num_epochs=10, print_loss_val_each_epoch=False): print("Build CNN model...") W1 = init.random_normal((32, 1, 5, 5), stddev=0.1, name='W1') W2 = init.random_normal((64, 32, 5, 5), stddev=0.1, name='W2') W3 = init.random_normal((7 * 7 * 64, 10), stddev=0.1, name='W3') b3 = init.random_normal((10, ), stddev=0.1, name='b3') X = ad.Variable(name="X") z1 = ad.conv2d_op(X, W1, padding=2, stride=1) z2 = ad.relu_op(z1) z3 = ad.avg_pool2d_op(z2, kernel_H=2, kernel_W=2, padding=0, stride=2) z4 = ad.conv2d_op(z3, W2, padding=2, stride=1) z5 = ad.relu_op(z4) z6 = ad.avg_pool2d_op(z5, kernel_H=2, kernel_W=2, padding=0, stride=2) z6_flat = ad.array_reshape_op(z6, (-1, 7 * 7 * 64)) y = ad.matmul_op(z6_flat, W3) + b3 executor = ad.Executor([y], ctx=executor_ctx) rand = np.random.RandomState(seed=123) X_val = rand.normal(scale=0.1, size=(batch_size, 1, 28, 28)).astype(np.float32) ath = executor.run(feed_dict={X: X_val}) hx.hetu2onnx.export(executor, [X], [y], 'ath.onnx') # # sess = rt.InferenceSession("ath.onnx") input = sess.get_inputs()[0].name pre = sess.run(None, {input: X_val.astype(np.float32)})[0] np.testing.assert_allclose(ath[0].asnumpy(), pre, rtol=1e-2)
def version_1(cls, node, tensor_dict, **kwargs): inputs = [ tensor_dict.get(inp, None) for inp in node.input_tensor_names ] assert len(inputs) == 1 kernel_shape = node.get_attr_value('kernel_shape') strides = node.get_attr_value('strides') assert len(kernel_shape) == 2 and len(strides) == 2 assert strides[0] == strides[1], 'strides 0 and 1 must be equal now!' #todo,here padding set to 0. check. y = ad.avg_pool2d_op(inputs[0], kernel_H=kernel_shape[0], kernel_W=kernel_shape[1], padding=0, stride=strides[0]) tensor_dict[node.output_tensor_names[0]] = y return y
def conv_relu_avg(x, shape): weight = init.random_normal(shape=shape, stddev=0.1) x = ad.conv2d_op(x, weight, padding=2, stride=1) x = ad.relu_op(x) x = ad.avg_pool2d_op(x, kernel_H=2, kernel_W=2, padding=0, stride=2) return x
def resnet_block(x, in_channel, num_blocks, is_first=False, name=''): if is_first: out_channel = in_channel identity = x x = conv2d(x, in_channel, out_channel, stride=1, padding=1, name=name + '_conv1') x = batch_norm_with_relu(x, out_channel, name + '_bn1') x = conv2d(x, out_channel, out_channel, stride=1, padding=1, name=name + '_conv2') x = x + identity else: out_channel = 2 * in_channel identity = x x = batch_norm_with_relu(x, in_channel, name + '_bn0') x = ad.pad_op(x, [[0, 0], [0, 0], [0, 1], [0, 1]]) x = conv2d(x, in_channel, out_channel, stride=2, padding=0, name=name + '_conv1') x = batch_norm_with_relu(x, out_channel, name + '_bn1') x = conv2d(x, out_channel, out_channel, stride=1, padding=1, name=name + '_conv2') identity = ad.avg_pool2d_op(identity, kernel_H=2, kernel_W=2, padding=0, stride=2) identity = ad.pad_op( identity, [[0, 0], [in_channel // 2, in_channel // 2], [0, 0], [0, 0]]) x = x + identity for i in range(1, num_blocks): identity = x x = batch_norm_with_relu(x, out_channel, name + '_bn%d' % (2 * i)) x = conv2d(x, out_channel, out_channel, stride=1, padding=1, name=name + '_conv%d' % (2 * i + 1)) x = batch_norm_with_relu(x, out_channel, name + '_bn%d' % (2 * i + 1)) x = conv2d(x, out_channel, out_channel, stride=1, padding=1, name=name + '_conv%d' % (2 * i + 2)) x = x + identity return x