我们尝试将随机分配的张量从0到255转换为直方图,并对直方图应用平滑滤波器。我试图将过滤操作的结果添加到一个新的数组张量,但我得到了一个关于形状的错误。请解决它。Tensorflow version 2.0.0
x = tf.random.uniform(shape=[32,32], minval=0, maxval=255, dtype=tf.float32)
x = tf.reshape(x, [1024])
print("x",x)
#H = get2dHistogram(x, y, value_range=[[0.0,1.0], [0.0,1.0]], nbins=100, dtype=tf.dtypes.int32)
H = tf.histogram_fixed_width(x, value_range=[0, 255], nbins=256)
H = tf.cast(H, tf.float32)
print(H)
print("shape: ",np.shape(H))
filter_size = 7
zero_n = int(filter_size/2)
zeros = tf.constant([0.0]*zero_n)
print(zeros)
new = tf.concat([zeros, H], 0)
print(new)
print("shape: ",np.shape(new))
new = tf.concat([new, zeros], 0)
print(new)
print("shape: ",np.shape(new))
filter_size = 7
filter_list = []
for i in range(filter_size): # make filter array
filter_list.append(float(1/filter_size))
filter_array = np.array(filter_list, dtype = np.float32)
filter_array_tf = tf.constant(filter_array, dtype=tf.float32)
print("filter_array_tf:", filter_array_tf)
sm_hist = []
sm_hist = np.array(sm_hist, dtype=np.float32)
sm_hist_tf = tf.constant(sm_hist, dtype=tf.float32)
for i in range(0, 256):
alist = new[i:i+filter_size]
alist = tf.multiply(alist, filter_array_tf)
alist = tf.reduce_sum(alist)
print("alist:", alist)
print("sm_hist_tf:", sm_hist_tf)
sm_hist_tf = tf.concat([sm_hist_tf, alist], 0)
print(sm_hist_tf)
我得到的错误:
InvalidArgumentError: ConcatOp : Ranks of all input tensors should match: shape[0] = [0] vs. shape[1] = [] [Op:ConcatV2] name: concat
将for循环的最后一行改为:
sm_hist_tf = tf.concat([sm_hist_tf, tf.expand_dims(alist,0)], 0)