from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf imgs = [[[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 0, 0], [0, 255, 0], [0, 0, 255]]],
[[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 0, 0], [0, 255, 0], [0, 0, 255]]]]
imgs = tf.reshape(imgs, [2, 4, 3, 3])
coords = [[[[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]],
[[0.5, 2], [1.3, 2], [0.2, 1.6]],
[[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]],
[[0.5, 2], [1.3, 2], [0.2, 1.6]]],
[[[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]],
[[0.5, 2], [1.3, 2], [0.2, 1.6]],
[[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]],
[[0.5, 2], [1.3, 2], [0.2, 1.6]]]]
coords = tf.reshape(coords, [2, 4, 3, 2]) cam_coords = [[[[1, 2, 3, 1], [2, 3, 4, 1], [3, 4, 5, 1]],
[[4, 5, 6, 1], [7, 8, 9, 1], [10, 11, 12, 1]],
[[1, 0, 3, 1], [3, 3, 4, 1], [3, 5, 5, 1]],
[[4, 4, 6, 1], [7, 7, 9, 1], [10, 12, 12, 1]]],
[[[1, 2, 3, 1], [2, 3, 4, 1], [3, 4, 5, 1]],
[[4, 5, 6, 1], [7, 8, 9, 1], [10, 11, 12, 1]],
[[1, 0, 3, 1], [3, 3, 4, 1], [3, 5, 5, 1]],
[[4, 4, 6, 1], [7, 7, 9, 1], [10, 12, 12, 1]]]]
cam_coords = tf.reshape(cam_coords, [2, 4, 3, 4]) def bilinear_sampler(imgs, coords, cam_coords):
def _repeat(x, n_repeats): # x = tf.cast(tf.range(4), 'float32') * 53248 n_repeats = 53248。
rep = tf.transpose(
tf.expand_dims(tf.ones(shape=tf.stack([
n_repeats,
])), 1), [1,
0]) # 最后得到[1,53248]大小的全一矩阵。tf.stack其作用类似于tf.concat,都是拼接两个张量,而不同之处在于,tf.concat拼接的是两个shape完全相同的张量,并且产生的张量的阶数不会发生变化,而tf.stack则会在新的张量阶上拼接,产生的张量的阶数将会增加
rep = tf.cast(rep, 'float32')
x = tf.matmul(tf.reshape(x, (-1, 1)),
rep) # reshape为一列,得到[[ 0.][ 53248.][106496.][159744.]]*rep,最后得到shape=(4, 53248)的矩阵。
return tf.reshape(x, [-1]) # 最后又化为一列Tensor("Reshape_1:0", shape=(212992,), dtype=float32) with tf.name_scope('image_sampling'):
coords_x, coords_y = tf.split(coords, [1, 1], axis=3)
inp_size = imgs.get_shape()
coord_size = coords.get_shape()
out_size = coords.get_shape().as_list()
out_size[3] = imgs.get_shape().as_list()[3] coords_x = tf.cast(coords_x, 'float32')
coords_y = tf.cast(coords_y, 'float32') x0 = tf.floor(coords_x)
x1 = x0 + 1
y0 = tf.floor(coords_y)
y1 = y0 + 1 y_max = tf.cast(tf.shape(imgs)[1] - 1, 'float32')
x_max = tf.cast(tf.shape(imgs)[2] - 1, 'float32')
zero = tf.zeros([1], dtype='float32') x0_safe = tf.clip_by_value(x0, zero, x_max)
y0_safe = tf.clip_by_value(y0, zero, y_max)
x1_safe = tf.clip_by_value(x1, zero, x_max)
y1_safe = tf.clip_by_value(y1, zero, y_max) ## bilinear interp weights, with points outside the grid having weight 0#判断是否相等,相等为1,不相等为0.
# wt_x0 = (x1 - coords_x) * tf.cast(tf.equal(x0, x0_safe), 'float32')
# wt_x1 = (coords_x - x0) * tf.cast(tf.equal(x1, x1_safe), 'float32')
# wt_y0 = (y1 - coords_y) * tf.cast(tf.equal(y0, y0_safe), 'float32')
# wt_y1 = (coords_y - y0) * tf.cast(tf.equal(y1, y1_safe), 'float32') wt_x0 = x1_safe - coords_x
wt_x1 = coords_x - x0_safe
wt_y0 = y1_safe - coords_y
wt_y1 = coords_y - y0_safe ## indices in the flat image to sample from
dim2 = tf.cast(inp_size[2], 'float32')
dim1 = tf.cast(inp_size[2] * inp_size[1], 'float32')
base = tf.reshape(
_repeat(
tf.cast(tf.range(coord_size[0]), 'float32') * dim1,
coord_size[1] * coord_size[2]),
[out_size[0], out_size[1], out_size[2],
1]) # tf.reshape(_repeat(tf.cast(tf.range(4), 'float32') * 128 * 416, 128 * 416), [4, 128, 416, 1])
# 上面最后得base=Tensor("Reshape_2:0", shape=(4, 128, 416, 1), dtype=float32)。中间有[ 0.][ 53248.][106496.][159744.]四种数。
base_y0 = base + y0_safe * dim2
base_y1 = base + y1_safe * dim2 # 考虑进有4个batch,所以不同batch要加上不同的基数。
idx00 = tf.reshape(x0_safe + base_y0, [-1]) # 加上基数之后构成了四个像素值的索引。
idx01 = x0_safe + base_y1
idx10 = x1_safe + base_y0
idx11 = x1_safe + base_y1 ## sample from imgs
imgs_flat = tf.reshape(imgs, tf.stack([-1, inp_size[3]]))
imgs_flat = tf.cast(imgs_flat, 'float32')
im00 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx00, 'int32')), out_size) # 每一个输出都有对应的四个像素点的值参与运算。
im01 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx01, 'int32')), out_size)
im10 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx10, 'int32')), out_size)
im11 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx11, 'int32')), out_size) w00 = wt_x0 * wt_y0 ######这里横轴和纵轴的距离乘机就算距离了。
w01 = wt_x0 * wt_y1
w10 = wt_x1 * wt_y0
w11 = wt_x1 * wt_y1 output = tf.add_n([
w00 * im00, w01 * im01,
w10 * im10, w11 * im11
]) # 以下为自定义代码
batch, height, width, channels = imgs.get_shape().as_list()
cam_coords = cam_coords[:, :, :, 0:-1]
cam_coords = tf.cast(cam_coords, 'float32')
euclidean = tf.sqrt(tf.reduce_sum(tf.square(cam_coords), 3))
euclidean = tf.reshape(euclidean, [2, -1]) xy00 = tf.concat([x0, y0], axis=3)
for i in range(batch):
cam_coordsi = cam_coords[i, :, :, :]
euclideani = euclidean[i, :]
euclideani = tf.reshape(euclideani, [-1, 1])
xy00_batchi = xy00[i, :, :, :] # 将横纵坐标合在一起,取batch1.
xy00_batchi = tf.reshape(xy00_batchi, [-1, 2])
xy00_batchi = tf.cast(xy00_batchi, tf.int32)
xy10_batchi = xy00_batchi + [1, 0]
xy01_batchi = xy00_batchi + [0, 1]
xy11_batchi = xy00_batchi + [1, 1] mask0 = tf.ones(shape=[height * width], dtype='float32') def true_1():
h = tf.cond(pred=tf.less(euclideani[h2, 0], euclideani[h1, 0]), true_fn=lambda: h1, false_fn=lambda: h2)
one_hot_true = tf.one_hot(indices=h, depth=12, axis=0)
return one_hot_true def false_1():
one_hot_false = tf.zeros([tf.shape(mask0)[0]])
return one_hot_false for h1 in range(xy00_batchi.get_shape().as_list()[0] - 1):
for h2 in range(h1 + 1, xy00_batchi.get_shape().as_list()[0]):
one_hot = tf.cond(pred=tf.reduce_all(tf.equal(xy00_batchi[h1, :], xy00_batchi[h2, :])), true_fn=true_1,
false_fn=false_1)
mask0 = mask0 - one_hot
mask0 = tf.clip_by_value(mask0, 0, 1)
mask0 = tf.reshape(mask0, [height, width])
mask1 = np.zeros(shape=[height, width], dtype='float32')
for l1 in range(xy00_batchi.get_shape().as_list()[0]):
q001 = xy00_batchi[l1, 0]
q002 = xy00_batchi[l1, 1]
q101 = xy10_batchi[l1, 0]
q102 = xy10_batchi[l1, 1]
q011 = xy01_batchi[l1, 0]
q012 = xy01_batchi[l1, 1]
q111 = xy11_batchi[l1, 0]
q112 = xy11_batchi[l1, 1]
var001 = tf.one_hot(indices=q002, depth=width, on_value=q001, off_value=height, axis=-1)
var002 = tf.one_hot(indices=var001, depth=height, axis=0)
var101 = tf.one_hot(indices=q102, depth=width, on_value=q101, off_value=height, axis=-1)
var102 = tf.one_hot(indices=var101, depth=height, axis=0)
var011 = tf.one_hot(indices=q012, depth=width, on_value=q011, off_value=height, axis=-1)
var012 = tf.one_hot(indices=var011, depth=height, axis=0)
var111 = tf.one_hot(indices=q112, depth=width, on_value=q111, off_value=height, axis=-1)
var112 = tf.one_hot(indices=var111, depth=height, axis=0)
mask1 = mask1 + var002 + var102 + var012 + var112
mask1 = tf.clip_by_value(mask1, 0, 1)
if i == 0:
mask0_stack = mask0
mask1_stack = mask1
else:
mask0_stack = tf.stack([mask0_stack, mask0], axis=0)
mask1_stack = tf.stack([mask1_stack, mask1], axis=0)
return output, mask0_stack, mask1_stack output_img, mask0_stack, mask1_stack = bilinear_sampler(imgs, coords, cam_coords)
with tf.Session() as sess:
# print(output_img)
# print(sess.run(output))
print(sess.run(output_img))
print(sess.run(mask0_stack))
print(sess.run(mask1_stack))
print(mask0_stack)
print(mask1_stack)

关于双线性插值中重叠像素与空白像素掩膜函数的一种迭代batch的写法的更多相关文章

  1. 【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式

    参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html ================================== java紫色代表迭 ...

  2. 关于CSS中的PX值(像素)

    场景: 人物:前端实习生「阿树」与 切图工程师「玉凤」事件:设计师出设计稿,前端实现页面 玉凤:树,设计稿发给你啦,差那么点像素,就叼死你┏(  ̄へ ̄)=☞阿树:~(>_<)~毛问题噶啦~ ...

  3. CSS中的px与物理像素、逻辑像素、1px边框问题

    一直不太清楚CSS中的1px与逻辑像素.物理像素是个什么关系(作为一名前端感觉很惭愧 -_-!),今天终于花时间彻底弄清楚了,其实弄清楚之后就觉得事情很简单,但也只有在弄清楚之后,才会觉得简单(语出& ...

  4. Opencv中图像的遍历与像素操作

    Opencv中图像的遍历与像素操作 OpenCV中表示图像的数据结构是cv::Mat,Mat对象本质上是一个由数值组成的矩阵.矩阵的每一个元素代表一个像素,对于灰度图像,像素是由8位无符号数来表示(0 ...

  5. HTML-HTML5+CSS3权威指南阅读(五、设备像素和CSS像素的概念)

    在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如<html>元素,也包括窗口和屏幕 这篇文章是关于桌面浏览器的,其唯一目的就是为移动浏览器中相似 ...

  6. 响应式设计:理解设备像素,CSS像素和屏幕分辨率

    概述 屏幕分辨率.设备像素和CSS像素这些术语,在非常多语境下,是可互换的,但也因此easy在有差异的地方引起混淆,实际上它们是不同的概念. 屏幕分辨率和设备像素是物理概念,而CSS像素是WEB编程的 ...

  7. 设备像素,设备独立像素,CSS像素

    之前学了移动端的开发对设备像素.设备独立像素.CSS像素弄得不太清楚,所以趁周末的时间查了一下,稍加整理 一些概念 在进行具体的分析之前,首先得知道下面这些关键性基本概念. CSS像素 CSS像素是W ...

  8. 【Android 应用开发】Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19698511 . 最近遇到了一系列的屏幕适配问题, 以及 ...

  9. CSS像素、物理像素、逻辑像素、设备像素比、PPI、Viewport

    1.PX(CSS pixels) 1.1 定义 虚拟像素,可以理解为“直觉”像素,CSS和JS使用的抽象单位,浏览器内的一切长度都是以CSS像素为单位的,CSS像素的单位是px. 1.2 注意 在CS ...

随机推荐

  1. 【Blazor】在ASP.NET Core中使用Blazor组件 - 创建一个音乐播放器

    前言 Blazor正式版的发布已经有一段时间了,.NET社区的各路高手也创建了一个又一个的Blazor组件库,其中就包括了我和其他小伙伴一起参与的AntDesign组件库,于上周终于发布了第一个版本0 ...

  2. WeChat小程序开发-初学者笔记(一)

    WeChat小程序开发学习第一天: 完成学习目标: 1.安装并了解Wechat小程序的基本环境, 2.可以利用已学知识的结合简单实现helloWorld界面. 学习过程: 1.首先在微信平台上进行相关 ...

  3. (一)pandas的两种对象

    将鱼图像数据进行操作,使用numpy知识 import numpy as np import matplotlib.pyplot as plt %matplotlib inline #咱们可以不用sh ...

  4. 李航统计学习方法(第二版)(十):决策树CART算法

    1 简介 1.1 介绍 1.2 生成步骤 CART树算法由以下两步组成:(1)决策树生成:基于训练数据集生成决策树,生成的决策树要尽量大;(2)决策树剪枝:用验证数据集对己生成的树进行剪枝并选择最优子 ...

  5. 数据分析06 /pandas高级操作相关案例:人口案例分析、2012美国大选献金项目数据分析

    数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 目录 数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 1. ...

  6. Cyber Security - Palo Alto Firewall Security Zones

    Firewall Security Zones Zones: The foundational aspect of every Firewall. Police network traffic Enf ...

  7. Burp Suite Target Module - 目标模块

    模块目的之一:获取网站分析 1.从Proxy - HTTP history界面选中需要加入Target Scope的Host 地址,右击,选中Add to Scope. 2.打开Target - Sc ...

  8. webpack源码-打包资源输出到本地

    webpack收集完依赖是怎么打包资源的呢? 入口compiler.js: this.applyPluginsParallel("make", compilation, err = ...

  9. 2019CSP-J T4 加工零件

    题目描述 凯凯的工厂正在有条不紊地生产一种神奇的零件,神奇的零件的生产过程自然也很神奇.工厂里有 n 位工人,工人们从 1 ∼n 编号.某些工人之间存在双向的零件传送带.保证每两名工人之间最多只存在一 ...

  10. Flarum 的安装与配置

    Flarum 是一款非常棒的开源论坛程序,本鸽子的论坛 就是用 Flarum 搭建的.之前有人问过我 Flarum 如何搭建,所以下面讲一下 Flarum 的搭建过程. 前提 域名需要提前解析. 有一 ...