我想比较一个参考窗口(补丁)和取自图像的所有其他窗口(补丁)之间的相似性。我的代码如下所示。
有人能帮我评估一下'ref‘(参考窗口)和变量'test’给出的所有其他10000个窗口之间的相似性吗?谢谢
详细说明:我尝试使用for循环。这很耗时。我尝试使用内置函数"ssim“,但它显示张量的维度不匹配。请建议执行此批处理的任何方法
# Read grayscale image from file.
Im = Image.open("cameraman.png")
#Resize it to desired shape (h,w)
Im = Im.resize((100,100))
# expand dimensions to get the shape [ no of batches, height, width, channel]
Im = np.expand_dims(Im,axis=0)
Im = np.expand_dims(Im,axis=0)
x = tf.convert_to_tensor(Im)
x=tf.reshape(x,[1,100,100,1]) # this is the required image shape in a tensor
# Break one image into windows of 11x11 (overlapping)
wsize=11
ws=50 # Index of centre window (this window is reference window)
#Extract windows of 11 x 11 around each pixel
p1=tf.extract_image_patches(x,sizes=[1,wsize,wsize,1],strides=[1,1,1,1],rates=[1,1,1,1],padding="SAME")
patches_shape = tf.shape(p1)
test=tf.reshape(p1, [tf.reduce_prod(patches_shape[0:3]), 11, 11, ]) # returns [#window_patches, h, w, c]
print(test.shape) #test has shape [ 10000, 11,11]
ref=test[5000,] # this is the reference window of shape [ 1, 11,11]
ref=tf.reshape(ref,[1,11,11])
print(im1.shape)
The following statement says size mismatch:
ssim1 = tf.image.ssim(ref, test, max_val=255, filter_size=11,filter_sigma=1.5, k1=0.01, k2=0.03)
**ValueError: Shapes (1, 11, 11) and (10000, 11, 11) are incompatible.**我希望打印出这些窗口和引用之间的距离。
发布于 2019-11-11 17:54:32
您需要对齐第一个尺寸。您可以迭代您的10000图像批次或广播您的原始补丁。但是,从性能的角度来看,建议使用tf.map_fn()遍历它们。
此外,您需要扩展最后一维,bc tf.image.ssim期望三阶张量。下面是一个使用tf 2.0进行测试的工作示例,并且可以立即执行:
arr1 = tf.convert_to_tensor(np.random.random([10000, 11, 11, 1]), dtype=tf.dtypes.float32)
arr2 = tf.convert_to_tensor(np.random.random([1, 11, 11, 1]), dtype=tf.dtypes.float32)
result_tensor = tf.map_fn(lambda x: tf.image.ssim(arr2[1:], x, 1), arr1)结果张量的形状为10000,0。来获得平均调用tf.reduce_mean。
但是,请将11x11的滤镜形状修改为11x11补丁,并提供下一次的工作示例。
https://stackoverflow.com/questions/58798502
复制相似问题