tensorflow对图像进行拼接的例子


Posted in Python onFebruary 05, 2020

tensorflow对图像进行多个块的行列拼接tf.concat(), tf.stack()

在深度学习过程中,通过卷积得到的图像块大小是8×8×1024的图像块,对得到的图像块进行reshape得到[8×8]×[32×32],其中[8×8]是图像块的个数,[32×32]是小图像的大小。通过tf.concat对小块的图像进行拼接。

-在做图像卷积的过程中,做了这样一个比较麻烦的拼接,现在还没想到更好的拼接方法,因为是块拼接,开始的时候使用了reshape,但是得到的结果不对,需要确定清楚数据的维度,对于数据的维度很是问题。

import tensorflow as tf
def tensor_concat(f, axis):
 x1 = f[0, :, :]
 for i in range(1, 8):
  x1 = tf.concat([x1, f[i, :, :]], axis=axis)
 return x1

def block_to_image(f): 
 x1 = tf.reshape(f, [64, 1024])
 x1 = tf.reshape(x1, [64, 32, 32])
 m2 = tensor_concat(x1[0:8, :, :], axis=1)
 for i in range(1, 8):
  m1 = tensor_concat(x1[i*8:(i+1)*8, :, :], axis=1)
  m2 = tf.concat([m2, m1], axis=0)
 x2 = tf.reshape(m2, [256, 256, 1])
 return x2

x = tf.random_normal([ 8, 8, 1024])
with tf.Session() as sess:
 m = sess.run(x)
 m1 = sess.run(block_to_image(m))

最后通过行拼接和列拼接得到图像大小为256×256×1大小的图像。

对[batch_size, height, weight, channel] 的图像进行1一样的图像块拼接:

在深度神经网络中,会有batch_size个图像大小[256×256×1]的图像进行块的拼接,对于多了一个维度的图像拼接起来,由[batch_size, 8, 8, 1024]拼接为[batch_size,256, 256, 1]。在做着部分时batch_size这部分实在是不知道怎么处理,所以还是用了本办法,使用的函数是append和tf.stack()

def tensor_concat(f, axis):
 x1 = f[0, :, :]
 for i in range(1, 8):
  x1 = tf.concat([x1, f[i, :, :]], axis=axis)
 return x1

def block_to_image(f):
 x3 =[]
 for k in range(f.shape[0]):
  x = f[k, :, :, :]
  x1 = tf.reshape(x, [64, 1024])
  x1 = tf.reshape(x1, [64, 32, 32])
  m2 = tensor_concat(x1[0:8, :, :], axis=1)
  for i in range(1, 8):
   m1 = tensor_concat(x1[i*8:(i+1)*8, :, :], axis=1)
   m2 = tf.concat([m2, m1], axis=0)
  x2 = tf.reshape(m2, [256, 256, 1])
  x3.append(x2)
  x4 = tf.stack(x3)
 return x4 
x = tf.random_normal([10, 8, 8, 1024])
with tf.Session() as sess:
 m = sess.run(x)
 m1 = sess.run(block_to_image1(m))

在学习过程中对tensor不能直接赋值,比如不能写:

x2 = tf.reshape(m2, [256, 256, 1]) 

x3[k, :, :, 1] = x2

这样的代码,会出现错误:'Tensor' object does not support item assignment

对于带有类似索引的赋值,参考的办法是:

x3 = [] 

x3.append(x2)

这时候得到的是list的格式,所以接下来将list转化为array,使用的是tf.stack(x3)

以上这篇tensorflow对图像进行拼接的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详细介绍Python语言中的按位运算符
Nov 26 Python
python基础教程之类class定义使用方法
Feb 20 Python
使用Python的Tornado框架实现一个一对一聊天的程序
Apr 25 Python
Python的Django框架中的数据过滤功能
Jul 17 Python
用Python写冒泡排序代码
Apr 12 Python
Python微信操控itchat的方法
May 31 Python
Django之提交表单与前后端交互的方法
Jul 19 Python
python3 requests库文件上传与下载实现详解
Aug 22 Python
浅谈python中统计计数的几种方法和Counter详解
Nov 07 Python
快速查找Python安装路径方法
Feb 06 Python
Python实现密钥密码(加解密)实例详解
Apr 26 Python
Python 阶乘详解
Oct 05 Python
Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例
Feb 05 #Python
Python实现新型冠状病毒传播模型及预测代码实例
Feb 05 #Python
基于Tensorflow批量数据的输入实现方式
Feb 05 #Python
Python操作注册表详细步骤介绍
Feb 05 #Python
Python类继承和多态原理解析
Feb 05 #Python
Python模块 _winreg操作注册表
Feb 05 #Python
python3操作注册表的方法(Url protocol)
Feb 05 #Python
You might like
浅谈Windows下 PHP4.0与oracle 8的连接设置
2006/10/09 PHP
图象函数中的中文显示
2006/10/09 PHP
sqlyog 中文乱码问题的设置方法
2008/10/19 PHP
php写app用的框架整理
2019/09/29 PHP
javascript下arguments,caller,callee,call,apply示例及理解
2009/12/24 Javascript
浅谈Javascript Base64 加密解密
2014/12/28 Javascript
仅30行代码实现Javascript中的MVC
2016/02/15 Javascript
jQuery使用正则表达式限制文本框只能输入数字
2016/06/18 Javascript
js判断空对象的实例(超简单)
2016/07/26 Javascript
Nodejs下用submit提交表单提示cannot post错误的解决方法
2016/11/21 NodeJs
react+redux的升级版todoList的实现
2017/12/18 Javascript
vue.js2.0 实现better-scroll的滚动效果实例详解
2018/08/13 Javascript
详解react内联样式使用webpack将px转rem
2018/09/13 Javascript
mpvue项目中使用第三方UI组件库的方法
2018/09/30 Javascript
vue的.vue文件是怎么run起来的(vue-loader)
2018/12/10 Javascript
详解nodejs 开发企业微信第三方应用入门教程
2019/03/12 NodeJs
node Buffer缓存区常见操作示例
2019/05/04 Javascript
Vue常用传值方式、父传子、子传父及非父子实例分析
2020/02/24 Javascript
Vue+Java 通过websocket实现服务器与客户端双向通信操作
2020/09/22 Javascript
JavaScript枚举选择jquery插件代码实例
2020/11/17 jQuery
python基于windows平台锁定键盘输入的方法
2015/03/05 Python
python opencv3实现人脸识别(windows)
2018/05/25 Python
解决Python下imread,imwrite不支持中文的问题
2018/12/05 Python
Django给admin添加Action的步骤详解
2019/05/01 Python
opencv3/C++实现视频读取、视频写入
2019/12/11 Python
python2.7使用scapy发送syn实例
2020/05/05 Python
基于Python实现简单学生管理系统
2020/07/24 Python
Python 的 __str__ 和 __repr__ 方法对比
2020/09/02 Python
美国现代家具网站:Design Within Reach
2018/07/19 全球购物
意外伤害赔偿协议书
2014/09/16 职场文书
一次性工伤赔偿协议书范本
2014/11/25 职场文书
烟台的海导游词
2015/02/02 职场文书
宝宝满月宴答谢词
2015/09/30 职场文书
职场:企业印章管理制度(模板)
2019/10/18 职场文书
spring boot中nativeQuery的用法
2021/07/26 Java/Android
Python数据可视化之Seaborn的安装及使用
2022/04/19 Python