计算2D输入图像的2D卷积时断言误差
计算2D图像的2D卷积时,我的断言错误。我正在努力分配i
和j
的正确范围,然后计算farter
查看的关键字。这给我带来了断言错误。有人可以告诉我我犯了什么错误?在使用yart> tart
关键字时,我应该评估2D卷积? i
和j
的正确范围应该是什么?
import numpy as np
def convolve_2d(image, kernel_size=(3, 3)):
""" yields (view, i, j) of a 2D convolution of
the 2D input image
"""
h, w = image.shape[0:2] # it should work for gray input images, as well as for colored
for i in range(0, 2):
for j in range(0, 2):
# view is the current "kernel" wide view of the image in the convolution
view = image.copy()
yield view, i, j
res = list(convolve_2d(
np.array([
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
[41, 42, 43, 44]
]),
kernel_size=(3, 3)
)
)
assert (len(res) == 4)
assert ([x[0].sum() for x in res] == [198, 207, 288, 297])
assert ((res[0][0] == [[11, 12, 13], [21, 22, 23], [31, 32, 33]]).all())
assert ((res[1][0] == [[12, 13, 14], [22, 23, 24], [32, 33, 34]]).all())
assert ((res[2][0] == [[21, 22, 23], [31, 32, 33], [41, 42, 43]]).all())
assert ((res[3][0] == [[22, 23, 24], [32, 33, 34], [42, 43, 44]]).all())
I am having assertion error while computing 2D convolution of an 2d image. I am struggling to assign correct range of i
and j
and then computing Yield
keyword for view. It's giving me assertion error. Can somebody tell me what mistake I am committing and what should I evaluate 2D convolution while using Yield
keyword? what should be the correct range of i
and j
?
import numpy as np
def convolve_2d(image, kernel_size=(3, 3)):
""" yields (view, i, j) of a 2D convolution of
the 2D input image
"""
h, w = image.shape[0:2] # it should work for gray input images, as well as for colored
for i in range(0, 2):
for j in range(0, 2):
# view is the current "kernel" wide view of the image in the convolution
view = image.copy()
yield view, i, j
res = list(convolve_2d(
np.array([
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
[41, 42, 43, 44]
]),
kernel_size=(3, 3)
)
)
assert (len(res) == 4)
assert ([x[0].sum() for x in res] == [198, 207, 288, 297])
assert ((res[0][0] == [[11, 12, 13], [21, 22, 23], [31, 32, 33]]).all())
assert ((res[1][0] == [[12, 13, 14], [22, 23, 24], [32, 33, 34]]).all())
assert ((res[2][0] == [[21, 22, 23], [31, 32, 33], [41, 42, 43]]).all())
assert ((res[3][0] == [[22, 23, 24], [32, 33, 34], [42, 43, 44]]).all())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论