mxnet的卷积 kernel = 3  pad=1边界补充0后,不管stride是否1还是2,imgw = 奇数或者偶数, 都是从图像位置(0,0)开始卷积

tensorlfow的卷积 kernel = 3 pad=‘SAME’边界补充0后,

imgw = 偶数

stride=1, 是从图像位置(0,0)开始卷积

stride=2, 是从图像位置(1,1)开始卷积 与mxnet不同

imgw = 奇数

stride=1, 是从图像位置(0,0)开始卷积

stride=2, 是从图像位置(0,0)开始卷积

tensorlfow的卷积 kernel = 3 pad=‘VAILD’ 边界不补充0,

不管stride是否1还是2,imgw = 奇数或者偶数, 都是从图像位置(1,1)开始卷积

#coding=utf-8
import math
import mxnet as mx
import numpy as np
import tensorflow as tf
from mxnet.gluon import nn
from mxnet import ndarray as nd
# import tensorlayer as tl
# from tensorflow.contrib.layers.python.layers import utils
# import collections
# from tensorlayer.layers import Layer, list_remove_repeat def out_dim(input, kernel, stride, pad, dilate):
x = input
p = pad
s = stride
d = dilate
k = kernel
output = math.floor((x + 2 * p - d * (k - 1) - 1) / s) + 1
return output #比较mxnet与tensorflow的conv batchnorm prelu的计算
#mxnet卷积层
# 输入数据格式是:batch * inchannel * height * width
# 输出数据格式是:batch * outchannel * height * width
# 权重格式: output_channels * in_channels * height * width
#(1)比较卷积
height = 6
width = 6
inchannel = 1
outchannel = 1 # #conv0 (64, 112, 112) kernel (3, 3) stride (1, 1) pad (1, 1)
# wkernel = 3
# stride = 1
# pad = 1
# dilate = 1
# output_height = out_dim(height, wkernel, stride, pad, dilate)
# if output_height == height:
# print("input: ", height, width, " wkernel", wkernel, " stride: ", stride, " pad:", pad, " output_height: ", output_height, output_height, "SAME")
# else:
# print("input: ", height, width, " wkernel", wkernel, " stride: ", stride, " pad:", pad, " output_height: ", output_height, output_height, "VALID") #stage1_unit1_conv2 (64, 56, 56) kernel (3, 3) stride (2, 2) pad (1, 1)
wkernel = 3
stride = 2
pad = 1
dilate = 1
output_height = out_dim(height, wkernel, stride, pad, dilate)
if output_height == height:
print("input: ", height, width, " wkernel", wkernel, " stride: ", stride, " pad:", pad, " output_height: ", output_height, output_height, "SAME")
else:
print("input: ", height, width, " wkernel", wkernel, " stride: ", stride, " pad:", pad, " output_height: ", output_height, output_height, "VALID") # #stage1_unit1_conv1sc (64, 56, 56) kernel (1, 1) stride (2, 2) pad (0, 0)
# wkernel = 1
# stride = 2
# pad = 0
# dilate = 1
# output_height = out_dim(height, wkernel, stride, pad, dilate)
# if output_height == height:
# print("input: ", height, width, " wkernel", wkernel, " stride: ", stride, " pad:", pad, " output_height: ", output_height, output_height, "SAME")
# else:
# print("input: ", height, width, " wkernel", wkernel, " stride: ", stride, " pad:", pad, " output_height: ", output_height, output_height, "VALID") w = nd.arange(wkernel * wkernel * inchannel * outchannel).reshape((outchannel,inchannel,wkernel,wkernel))
b = nd.array([0])
data = nd.arange(height * width * inchannel).reshape((1,inchannel,height,width))
# mxnet直接nd卷积运算nd.Convolution
#out = nd.Convolution(data,w,b,kernel=w.shape[2:],num_filter=outchannel,stride=(1,1), pad=(1, 1))
print('input:',data)
print('weight:',w)
#print('bias:',b)
# print('mxnet ndarray output:',out)
# print('\n') #mxnet利用symbol计算卷积
ws = mx.sym.Variable('w')
bs = mx.sym.Variable('b')
datas = mx.sym.Variable('data')
# outs = mx.sym.Convolution(data=datas, weight=ws, bias=bs, num_filter=w.shape[1], kernel=w.shape[2:], stride=(1,1), pad=(0, 0),
# no_bias=False, name="conv0")
outs = mx.sym.Convolution(data=datas, weight=ws, num_filter=outchannel, kernel=w.shape[2:], stride=(stride,stride), pad=(pad, pad),
no_bias=True, name="conv0")
#outs = mx.sym.Convolution(datas,ws,bs,kernel=w.shape[2:],num_filter=w.shape[1])
ex=outs.bind(mx.cpu(), {'data':data, 'w':w, 'b':b})
ex.forward()
mxnetresult = ex.outputs[0].asnumpy()
print("mxnet symbol:\n", ex.outputs[0].asnumpy())
# output_height = out_dim(height, w.shape[2], stride, pad, dilate)
# output_width = out_dim(width, w.shape[2], stride, pad, dilate)
# print("input_height: ", height, width)
# print("output_height: ", output_height, output_width) #tensorflow计算卷积
#(W-F + 2 p / S)+ 1
# 输入数据格式是:batch * height * width * inchannel
# 输出数据格式是:batch * height * width * outchannel
# 权重格式: height * width * in_channels * output_channels
# w = np.arange(4).reshape((2,2,1,1))
# b = np.array([0])
# data = np.arange(9).reshape((1,3,3,1))
# w = w.reshape((wkernel,wkernel,inchannel,outchannel)).asnumpy()
# data = data.reshape((1, height,width,inchannel)).asnumpy() data = data.asnumpy().transpose(0,2,3,1)
w = w.asnumpy().transpose(2,3,1,0)
b = b.asnumpy()
# print('input:',data)
# print('inputshape:',data.shape)
# print('weight:',w)
# print('weight:',w.shape)
input = tf.Variable(data, dtype=np.float32)
#input_reshape = tf.reshape(input, [1,inchannel,height,width])
filter = tf.Variable(w, dtype=np.float32)
#filter_reshape = tf.reshape(filter, [outchannel,inchannel,wkernel,wkernel])
if pad == 0:
conv = tf.nn.conv2d(input, filter, strides=[1, stride, stride, 1], padding='VALID')
else:
conv = tf.nn.conv2d(input, filter, strides=[1, stride, stride, 1], padding='SAME') kernel_size = 3
kernel_size_effective = kernel_size
pad_total = kernel_size_effective - 1
pad_beg = pad_total // 2
pad_end = pad_total - pad_beg
print(pad_beg, pad_end)
input_PadLayer = tf.pad(input, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]], name='padding') if stride==2:
conv_padlayer = tf.nn.conv2d(input_PadLayer, filter, strides=[1, stride, stride, 1], padding='VALID') # nets = tl.layers.Conv2d(inputs, n_filter=num_outputs, filter_size=(kernel_size, kernel_size), b_init=None,
# strides=(strides, strides), W_init=w_init, act=None, padding='VALID', name=scope,
# use_cudnn_on_gpu=True)
#nets = BatchNormLayer(nets, act=tf.identity, is_train=True, trainable=trainable, name=scope+'bn3')
#conv_reshape = tf.reshape(conv, [1,outchannel,output_height,output_height])
#conv_reshape = tf.reshape(conv, [1,1,2,2]) init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
#print("input: \n", sess.run(input))
input_reshape = sess.run(input).transpose(0,3,1,2)
#print("input_reshape: \n", input_reshape)
#print("filter: \n", sess.run(filter))
filter_reshape = sess.run(filter).transpose(3,2,0,1)
#print("filter_reshape: \n", filter_reshape)
#print("conv ", sess.run(conv))
conv_reshape = sess.run(conv).transpose(0,3,1,2)
print("conv_reshape: \n", conv_reshape)
if stride==2:
input_PadLayer_reshape = sess.run(input_PadLayer).transpose(0,3,1,2)
print("input_PadLayer_reshape: \n", input_PadLayer_reshape) conv_padlayer_reshape = sess.run(conv_padlayer).transpose(0,3,1,2)
print("conv_padlayer_reshape: \n", conv_padlayer_reshape) tensorflowresult = conv_padlayer_reshape
else:
tensorflowresult = conv_reshape
#print("tf_height", op2_reshape.shape.as_list())
#print("tf_height", op2_reshape.shape.as_list())
if (tensorflowresult==mxnetresult).all():
print("success ")
else:
print("failed ")

input: 6 6 wkernel 3 stride: 2 pad: 1 output_height: 3 3 VALID
input:
[[[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10. 11.]
[ 12. 13. 14. 15. 16. 17.]
[ 18. 19. 20. 21. 22. 23.]
[ 24. 25. 26. 27. 28. 29.]
[ 30. 31. 32. 33. 34. 35.]]]]
<NDArray 1x1x6x6 @cpu(0)>
weight:
[[[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]]]
<NDArray 1x1x3x3 @cpu(0)>
mxnet symbol:
[[[[ 103. 196. 262.]
[ 411. 618. 690.]
[ 735. 1050. 1122.]]]]
1 1
conv_reshape:
[[[[ 366. 438. 294.]
[ 798. 870. 546.]
[ 451. 481. 271.]]]]
input_PadLayer_reshape:
[[[[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 1. 2. 3. 4. 5. 0.]
[ 0. 6. 7. 8. 9. 10. 11. 0.]
[ 0. 12. 13. 14. 15. 16. 17. 0.]
[ 0. 18. 19. 20. 21. 22. 23. 0.]
[ 0. 24. 25. 26. 27. 28. 29. 0.]
[ 0. 30. 31. 32. 33. 34. 35. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0.]]]]
conv_padlayer_reshape:
[[[[ 103. 196. 262.]
[ 411. 618. 690.]
[ 735. 1050. 1122.]]]]
success

mxnet与tensorflow的卷积实现细节比较的更多相关文章

  1. 使用TensorFlow的卷积神经网络识别自己的单个手写数字,填坑总结

    折腾了几天,爬了大大小小若干的坑,特记录如下.代码在最后面. 环境: Python3.6.4 + TensorFlow 1.5.1 + Win7 64位 + I5 3570 CPU 方法: 先用MNI ...

  2. CNN中的卷积核及TensorFlow中卷积的各种实现

    声明: 1. 我和每一个应该看这篇博文的人一样,都是初学者,都是小菜鸟,我发布博文只是希望加深学习印象并与大家讨论. 2. 我不确定的地方用了"应该"二字 首先,通俗说一下,CNN ...

  3. 线性回归模型的 MXNet 与 TensorFlow 实现

    本文主要探索如何使用深度学习框架 MXNet 或 TensorFlow 实现线性回归模型?并且以 Kaggle 上数据集 USA_Housing 做线性回归任务来预测房价. 回归任务,scikit-l ...

  4. tensorflow prelu的实现细节

    tensorflow prelu的实现细节 output = tf.nn.leaky_relu(input, alpha=tf_gamma_data,name=name) #tf.nn.leaky_r ...

  5. TensorFlow实现卷积神经网络

    1 卷积神经网络简介 在介绍卷积神经网络(CNN)之前,我们需要了解全连接神经网络与卷积神经网络的区别,下面先看一下两者的结构,如下所示: 图1 全连接神经网络与卷积神经网络结构 虽然上图中显示的全连 ...

  6. TensorFlow中卷积

    CNN中的卷积核及TensorFlow中卷积的各种实现 声明: 1. 我和每一个应该看这篇博文的人一样,都是初学者,都是小菜鸟,我发布博文只是希望加深学习印象并与大家讨论. 2. 我不确定的地方用了“ ...

  7. tensorflow中卷积、转置卷积具体实现方式

    卷积和转置卷积,都涉及到padding, 那么添加padding 的具体方式,就会影响到计算结果,所以搞清除tensorflow中卷积和转置卷积的具体实现有助于模型的灵活部署应用. 一.卷积 举例说明 ...

  8. TensorFlow的 卷积层

    用 TensorFlow 做卷积 让我们用所学知识在 TensorFlow 里构建真的 CNNs.在下面的练习中,你需要设定卷积核滤波器(filters)的维度,weight,bias.这在很大程度上 ...

  9. tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图

    tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...

随机推荐

  1. HAProxy压测及参数调优

    背景 小米容器云平台,在构建云厂商集群时,需要通过HAProxy将云厂商LB流量从宿主机转到容器中,但对于HAProxy的性能没有把握.参考网上的一篇HAProxy压测文章,文章中提到HAProxy ...

  2. 菜鸟学Java(十五)——Java反射机制(二)

    上一篇博文<菜鸟学编程(九)——Java反射机制(一)>里面,向大家介绍了什么是Java的反射机制,以及Java的反射机制有什么用.上一篇比较偏重理论,理论的东西给人讲出来总感觉虚无缥缈, ...

  3. lua -- 事件响应与局部变量

    -- 这里要注意的点是:虽然nAmount是局部变量,却在控件的响应函数中使用 -- 因为控件的响应函数是在该变量的区域内,所以可以用 -- 如果控件的响应函数在外部,那么该变量就要声明成为全局变量 ...

  4. [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET

    How to use the Windows Azure Blob Storage Service in .NET version 1.7 version 2.0 This guide will de ...

  5. Linux--多网卡的7种Bond模式【转】

    转自:http://www.cnblogs.com/lcword/p/5914089.html 网卡bond是通过把多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡.在应用部署中是一 ...

  6. SpringMVC中的 JSR 303 数据校验框架说明

    JSR 303 是java为Bean数据合法性校验提供的标准框架,它已经包含在JavaEE 6.0中. JSR 303 通过在Bean属性上标注类似于@NotNull.@Max等标准的注解指定校验规则 ...

  7. IntelliJ IDEA 14.1.4破解方法-通过程序根据用户名生成注册码

    将下面的代码拷贝到Eclipse或者其他的开发工具中,在main方法中指定自己的用户名(随意),运行main方法,在控制台即可生成注册码.然后启动IntelliJ IDEA 14.1.4,然后根据提示 ...

  8. java中日期的换算处理

    JAVA8中的日期API是JSR-310的实现,并且是工作在ISO-8601日历系统基础上的,但我们也可以在非ISO的日历上.JDK8的日期API大致分为以下几个包: java.time包:JDK8中 ...

  9. css优化建议

    1.不要使用过小的图片做背景平铺.这就是为何很多人都不用 1px 的原因,这才知晓.宽高 1px 的图片平铺出一个宽高 200px 的区域,需要 200*200=40, 000 次,占用资源. 2.无 ...

  10. Web程序中的懒加载异常说明及解决方案

    所谓懒加载(lazy)就是延时加载,延迟加载. 什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载. 至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适, 因为内存容量有 ...