1,今天首先对softmax的使用方法进行了实验。
import tensorflow as tf
"""
Created on Mon Oct 8 15:55:59 2018
@author: Lenovo
"""
matrix1=tf.constant(
[
[
[1.0,2.0,3.0,7.2],
[2.0,4.1,4.0,2.5],
[5.0,2.2,6.1,3.4]
],
[
[5.2,3.0,7.2,2.5],
[3.3,5.2,1.1,5.6],
[4.6,6.8,2.3,3.4]
],
[
[2.7,7.3,4.4,6.2],
[2.1,3.6,2.2,7.3],
[6.9,8.2,2.7,3.5]
],
[
[2.2,7.3,4.4,6.2],
[2.2,3.6,8.2,7.3],
[6.4,3.2,2.7,2.5]
],
[
[2.2,7.3,4.4,6.2],
[2.2,3.6,8.2,7.3],
[6.4,3.2,2.7,2.5]
]
])
print(matrix1.shape)
matrix2=tf.nn.softmax(matrix1,axis=2)
print(matrix2.shape)
matrix_sum=tf.reduce_sum(matrix2,axis=2)
sum3=0
print(matrix_sum.shape)
sess=tf.Session()
matrix1=sess.run(matrix1)
matrix2=sess.run(matrix2)
matrix_sum=sess.run(matrix_sum)
print(matrix1)
print('###########################')
print(matrix2)
print('###########################')
print(matrix_sum)
得出了结论,tf.nn.softmax和tf.reduce_sum针对的维数要相同的时候才能达到效果。
2,发现一个奇怪的问题,我明明对于不同的costvolume使用了不同的权值,为什么还会出现规律性的相同的值。
现在输出的值变成这样的,而且重复的次数刚好是Disparty的值,我觉得有点太巧了。
[[ 1.00000012 2.00000048 2.99999976 ..., 14.00000095 14.99999905
16.00000381]
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]
[ 1. 1.99999988 3.00000048 ..., 14.00000191 15.00000191
15.99999905]
...,
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]
[ 1. 1.99999988 3.00000048 ..., 14.00000191 15.00000191
15.99999905]
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]]
[[ 1.00000012 2.00000048 2.99999976 ..., 14.00000095 14.99999905
16.00000381]
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]
[ 1. 1.99999988 3.00000048 ..., 14.00000191 15.00000191
15.99999905]
...,
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]
[ 1. 1.99999988 3.00000048 ..., 14.00000191 15.00000191
15.99999905]
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]]
[[ 1.00000012 2.00000048 2.99999976 ..., 14.00000095 14.99999905
16.00000381]
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]
[ 1. 1.99999988 3.00000048 ..., 14.00000191 15.00000191
15.99999905]
...,
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]
[ 1. 1.99999988 3.00000048 ..., 14.00000191 15.00000191
15.99999905]
[ 1. 2.00000072 3. ..., 14.00000286 14.99999905
16.00000572]]
这是部分最终的Dispmap的输出值。可以发现,实际上程序输出了不止一张图片,也就是输出了很多个二维矩阵。找到这个循坏所在就是关键。
3,刚刚又一个新的猜想,很大可能可以解决重复纹理问题。让我试试。把下面这句
logits=tf.reshape(tf.stack(all_cost_vol), [BATCH_SIZE,DISPARITY,IMG_H,IMG_W,IMAGE_CHANNEL])
改为:
logits=tf.reshape(tf.vstack(all_cost_vol), [BATCH_SIZE,DISPARITY,IMG_H,IMG_W,IMAGE_CHANNEL])
没有用
Created on Mon Oct 8 21:31:31 2018
@author: Lenovo
"""
a = tf.constant([1,2,3])
b = tf.constant([4,5,6])
c=tf.constant([7,8,9])
c = tf.stack([a,b],axis=0)
d = tf.stack([a,b],axis=1)
e = tf.unstack(d,axis=0)
f = tf.unstack(d,axis=1)
with tf.Session() as sess:
print('原始向量数据:')
print(sess.run(a))
print(sess.run(b))
print('矩阵拼接的函数示例,得到一个矩阵:') #返回值是多维矩阵
print('以"0维"的方式进行拼接')
print(sess.run(c))
print('以"1维"的方式进行拼接')
print(sess.run(d))
print('矩阵分解的函数示例,得到一个list:') #返回值是一个list
print('以"0维"的方式进行分解')
print(sess.run(e))
print('以"1维"的方式进行分解')
print(sess.run(f))
在睡觉之前,通过改正了两个地方的代码,我的图的效果终于有了一定的改进。
def computeSoftArgMin(logits,BATCH_SIZE):
softmax = tf.nn.softmax(logits,axis=2)
#sess=tf.Session()
#sftmax_=sess.run(softmax)
f1 = open("dispmap.txt", "w") #将值输出来看一下
# print(softmax_,file=f1)
#
print("softmax shape")
print(softmax.shape)
disp = tf.range(1, (DISPARITY+1), 1)
disp = tf.cast(disp, tf.float32)
disp_mat = []
disp_mat = []
for i in range(IMG_H*IMG_W):
disp_mat.append(disp)
disp_mat = tf.reshape(tf.stack(disp_mat,1), [IMG_H,IMG_W,DISPARITY])
disp_mat=tf.to_float(disp_mat)
print("disp_mat shape")
print(disp_mat.shape)
print(disp_mat,file=f1)
result = tf.multiply(softmax, disp_mat)
print("result shape before reduce")
print(result.shape)
result = tf.reduce_sum(result, 2)
print("softmax result shape")
result=tf.squeeze(result)
print(result.shape)
f1.close()
return result
#######################网络的修改部分######################################
。。。。。。
x37 = -x37
x37=tf.reduce_mean(x37,1)
x37=tf.squeeze(x37)
print("tf.reduce_mean(x37,1)")
print(x37.shape)
all_cost_vol.append(x37)
print("now all_cost_vol")
print(len(all_cost_vol))
if d==6:
old_cost1=x37
if d==3:
old_cost2=x37
#assert print(operator.eq(old_cost1,old_cost2))
all_cost_vol=tf.stack(all_cost_vol,1)
print('all_cost_vol')
print(all_cost_vol.shape)
logits=tf.reshape(all_cost_vol, [IMG_H,IMG_W,DISPARITY])
#dispmap=computeSoftArgMin(logits,BATCH_SIZE)
dispmap=logits
return dispmap,all_cost_vol