pwcnet中的函数

本文详细解析了PWCNET中的多个关键函数,包括tf.unstack、tf.gather_nd、tf.floor、tf.clip_by_value和tf.pad等。通过实例展示了它们的功能和用法,如tf.gather_nd用于多维数据的索引,tf.floor用于向下取整,tf.keras.layers.Cropping2D用于图像的裁剪。此外,还讨论了训练过程中的glob模块、groupby函数和使用yield的生成器等。
摘要由CSDN通过智能技术生成

t=np.array(range(300)).reshape(3,436,1024,2)  假如此为一张图片,3为batch,4为通道的话,应该这么想象。batch的想象没有问题,主要是(436,1024,2)这里。这里使用spyder可视化,选择axis=2之后,下图1尺寸为(436,1024),因为axis2=2,表示可以选择两张这种尺寸的图。

                                                                                      图1

假如看axis=0的话,那么显示如下,尺寸为(1024,2),有436个这样的,下图为图片的第一行,下图虽然竖着,但是其实放图片中就是第一行,把两个通道都显示了

假如看axis=1的话,那么尺寸为(436,2),有1024个这样的,就是图像的第一列。

 

 

tf.unstack

给定tensor(A,B,C,D),

假如axis=0,那么输出的第i个tensor的shape为(B,C,D)。代表切片(i,B,C,D)

假如axis=1,那么输出的第i个tensor的shape为(A,C,D)。代表切片(A,i,C,D)

Unpacks the given dimension of a rank-R tensor into rank-(R-1) tensors.

given dimension of 是修饰rank-r tensor,就像a large number of。

tf.gather_nd

直接上例子

import tensorflow as tf



indices = [1]                                             #(1,)
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [['a1', 'b1'], ['c1', 'd1']]                 ##(2,2)


indices = [[1]]                                          #(1,1)
params = [[['a0', 'b0'], ['c0', 'd0']],    
          [['a1', 'b1'], ['c1', 'd1']]]
output = [[['a1', 'b1'], ['c1', 'd1']]]  #############1 (1,2,2)

indices = [[0, 1], [1, 0]]                                ##(2,2)
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]                  ##(2,2,2)
output = [['c0', 'd0'], ['a1', 'b1']]##################2  (2,2)

indices = [[0, 0, 1], [1, 0, 1]]                          #(2,3)
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]] 
output = ['b0', 'b1']                 ###################3  (2,)

x_00 = tf.gather_nd(params, indices)
print (x_00)

上面代码一共4个例子,[1]索引到第一个(2,2)数组,[[1]]索引到(2,2)但是因为[[1]],外面还有一个括号,所以最终得到(1,2,2),但是和[1]的结果是一样的。

第一个例子,params的shape为2,2,2.indices为[[1]],indices中最外侧的[]在output中不改变,indices里面的[1]代表选取params的slices,这个1代表选取params中第一个2,2.把这个2,2尺寸的矩阵放到indices中最外侧的[]中。

第二个例子,indices最外侧的[]不变,从indices中可以看出,output中有两个元素,第一个元素对应params中0,1的位置,0代表第一个矩阵,1代表举证中第二行。

以下代码为此函数更深入理解:

params=np.array(range(48)).reshape(2,2,3,4)  
 
indices=[[[[0,1],
          [1,1],
          [1,2]],
          [[1,1],
          [1,0],
          [0,1]]],
          [[[1,1],
          [1,0],
          [1,1]],
          [[1,0],
          [0,1],
          [0,1]]]]

a=tf.Variable(params)     ##<tf.Variable 'Variable_14:0' shape=(2, 2, 3, 4) dtype=int64_ref>
print (a)
b=tf.Variable(indices) ###<tf.Variable 'Variable_15:0' shape=(2, 2, 3, 2) dtype=int32_ref>
print (b
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值