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. 【Unity】6.4 Transform--移动、旋转和缩放游戏对象

    分类:Unity.C#.VS2015 创建日期:2016-04-20 一.简介 Unity引擎提供了丰富的组件和类库,为游戏开发提供了非常大的便利,熟练掌握和使用这些API,对于游戏开发的效率提高很重 ...

  2. Vue.js 入门指南

    1.Vue.js是什么? Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注 ...

  3. 微网站|手机端html弹窗、弹层、提示框、加载条

    layer mobile是为移动设备(手机.平板等webkit内核浏览器/webview)量身定做的弹层支撑,采用Native JavaScript编写,完全独立于PC版的layer,您需要按照场景选 ...

  4. javascript基础拾遗(十二)

    1.javascript的单线程特性 在javascript中,所有的代码都是单线程的 因此所有的网络操作,浏览器事件,都必须是异步执行的,异步执行的逻辑是回调. function callback( ...

  5. linux命令(49):显示文件的指定行,打印中间几行

    linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...

  6. 如何让 Xcode 在读写上提速100倍?

    如何让 Xcode 在读写上提速100倍? 上个月参加了一场西雅图当地的线下 iOS 开发者聚会.Jeff Szuhay 作为一个有20+年开发经验的资深程序员,跟我讲了一套提高 iOS 开发效率的方 ...

  7. FFmpeg Basics学习笔记(2)

    帧率 fps的概念 帧率,单位FPS(frame per second), 用于衡量视频每秒的处理帧数,对于编码器而言说明编码器在1s的编码的速度,通常可以使用一帧的编码时间倒数简单计算:对于解码器而 ...

  8. asp.net gridview 如何实现行点击事件

    第一步:绑定行点击事件 protected void GV_DATA_RowDataBound( object sender, GridViewRowEventArgs e ) { if( e.Row ...

  9. spark1.6内存管理

      Spark从1.6.0版本开始,内存管理模块就发生了改变,旧版本的内存管理模块是实现了StaticMemoryManager 类,现在被称为"legacy"."Leg ...

  10. virtualbox 安装 USB 扩展功能

    virtualbox USB 扩展包下载 扩展包下载地址: http://download.virtualbox.org/virtualbox/ 选择你的 virtualbox 版本 看版本在 vir ...