经检查发现我生成的valjoint数据与作者提供的不同。所以我想可视化看一下究竟是哪里不同。决定用matplotlib库来将valjoint形状为(4418,3,150,27,1)的numpy数据进行可视化。
通过相减我生成的数据与作者提供的数据
diff = val_from_myself - val_from_author
然后可视化出来看一下究竟是哪些数据与作者生成的不同,然后看一下差别有多大。
可视化的操作来源于stack overflow上的https://stackoverflow.com/questions/36410321/matplotlib-how-to-represent-array-as-image
import numpy as np
import matplotlib.pyplot as plt
# 引入包
shape = (4418,3,150,27,1)
# diff = val_from_myself - val_from_author
diff = np.random.random_sample(shape)
先生成一个与我预计展示的diff形状相同的random的array进行测试,
diff[0].shape
(3, 150, 27, 1)
diff[0]
array([[[[0.04071577],
[0.95986482],
[0.52586994],
...,
[0.35571531],
[0.82076305],
[0.86118058]],
[[0.22357145],
[0.71822266],
[0.27387145],
...,
[0.33893953],
[0.40765062],
[0.53080625]],
[[0.75792293],
[0.34517699],
[0.63563691],
...,
[0.05881489],
[0.57042857],
[0.16468755]],
...,
[[0.1187414 ],
[0.89411739],
[0.3068006 ],
...,
[0.19535109],
[0.10965052],
[0.72253673]],
[[0.43927226],
[0.96473494],
[0.15795009],
...,
[0.27780695],
[0.06872122],
[0.21858591]],
[[0.17150297],
[0.47254316],
[0.52471497],
...,
[0.79195137],
[0.06003133],
[0.22222626]]],
[[[0.4168927 ],
[0.75330351],
[0.03326249],
…,
[0.58373469],
[0.12104278],
[0.76293658]],
[[0.71412816],
[0.64042844],
[0.55803296],
...,
[0.97373851],
[0.96451841],
[0.6614665 ]],
[[0.48990274],
[0.68564298],
[0.49324841],
...,
[0.45652626],
[0.48045541],
[0.58097244]],
...,
[[0.32700639],
[0.56813866],
[0.70563916],
...,
[0.86540404],
[0.30111993],
[0.88154087]],
[[0.3529664 ],
[0.20931985],
[0.89013229],
...,
[0.36872604],
[0.93041892],
[0.33902597]],
[[0.73275822],
[0.39793688],
[0.19584355],
...,
[0.04259638],
[0.11689313],
[0.77362658]]],
[[[0.92409916],
[0.00150939],
[0.49043064],
…,
[0.7677901 ],
[0.3357452 ],
[0.91712289]],
[[0.61773657],
[0.71698287],
[0.06918023],
...,
[0.97509528],
[0.76807735],
[0.24350673]],
[[0.35511086],
[0.1770509 ],
[0.17058109],
...,
[0.33747421],
[0.69032098],
[0.37825793]],
...,
[[0.35713963],
[0.65227242],
[0.3922617 ],
...,
[0.18300791],
[0.76696578],
[0.98278218]],
[[0.72316591],
[0.6511162 ],
[0.21332253],
...,
[0.84889062],
[0.89810454],
[0.76794124]],
[[0.52053393],
[0.55935167],
[0.84717704],
...,
[0.59245906],
[0.13250955],
[0.29863322]]]])
diff_squ = np.squeeze(diff)
print(diff_squ.shape)
print(diff_squ[0])
print(diff_squ[0].shape)
(4418, 3, 150, 27)
[[[0.04071577 0.95986482 0.52586994 ... 0.35571531 0.82076305 0.86118058]
[0.22357145 0.71822266 0.27387145 ... 0.33893953 0.40765062 0.53080625]
[0.75792293 0.34517699 0.63563691 ... 0.05881489 0.57042857 0.16468755]
...
[0.1187414 0.89411739 0.3068006 ... 0.19535109 0.10965052 0.72253673]
[0.43927226 0.96473494 0.15795009 ... 0.27780695 0.06872122 0.21858591]
[0.17150297 0.47254316 0.52471497 ... 0.79195137 0.06003133 0.22222626]]
[[0.4168927 0.75330351 0.03326249 ... 0.58373469 0.12104278 0.76293658]
[0.71412816 0.64042844 0.55803296 ... 0.97373851 0.96451841 0.6614665 ]
[0.48990274 0.68564298 0.49324841 ... 0.45652626 0.48045541 0.58097244]
...
[0.32700639 0.56813866 0.70563916 ... 0.86540404 0.30111993 0.88154087]
[0.3529664 0.20931985 0.89013229 ... 0.36872604 0.93041892 0.33902597]
[0.73275822 0.39793688 0.19584355 ... 0.04259638 0.11689313 0.77362658]]
[[0.92409916 0.00150939 0.49043064 ... 0.7677901 0.3357452 0.91712289]
[0.61773657 0.71698287 0.06918023 ... 0.97509528 0.76807735 0.24350673]
[0.35511086 0.1770509 0.17058109 ... 0.33747421 0.69032098 0.37825793]
...
[0.35713963 0.65227242 0.3922617 ... 0.18300791 0.76696578 0.98278218]
[0.72316591 0.6511162 0.21332253 ... 0.84889062 0.89810454 0.76794124]
[0.52053393 0.55935167 0.84717704 ... 0.59245906 0.13250955 0.29863322]]]
(3, 150, 27)
现在我要plt.show出一个diff[0][0]的,shape是150*27的长方形的东西
diff_squ[0][0].shape
(150, 27)
plt.imshow(diff_squ[0][0], cmap='hot')
plt.colorbar()
plt.show()
好了,现在我需要在纵向维度上连接diff的dim2也就是长度为3的那个维度。平铺展示出来diff[0]的(3, 150, 27)
可以先将问题转化为如何将一个shape为(3,6,2)的矩阵横向拼接为(6,6):
test = np.random.randint(5,size=(3,6,2))
test
array([[[1, 3],
[0, 4],
[1, 3],
[0, 2],
[0, 0],
[3, 1]],
[[1, 3],
[1, 0],
[2, 0],
[1, 0],
[1, 2],
[4, 0]],
[[4, 3],
[2, 0],
[3, 2],
[0, 0],
[1, 0],
[1, 1]]])
这是我想要的效果:
f = plt.figure()
n=3
for i in range(n):
# Debug, plot figure
f.add_subplot(1, n, i + 1)
plt.imshow(test[i], cmap='hot')
plt.colorbar()
plt.show()
np.reshape(test,(6,6))
array([[1, 3, 0, 4, 1, 3],
[0, 2, 0, 0, 3, 1],
[1, 3, 1, 0, 2, 0],
[1, 0, 1, 2, 4, 0],
[4, 3, 2, 0, 3, 2],
[0, 0, 1, 0, 1, 1]])
plt.imshow(np.reshape(test,(6,6)), cmap='hot')
plt.colorbar()
plt.show()
如果只是简单的reshape 可以看出numpy是将二维展开然后重新平铺,与我想要的不一样。
所以需要引入numpy的二维拼接中的横向拼接功能,根据文章https://blog.csdn.net/leviopku/article/details/82717355
我可以直到只需要进行水平拼接数组np.hstack就可以了
np.hstack((test[i] for i in range(len(test))))
<ipython-input-33-316d9d2c5a32>:1: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
np.hstack((test[i] for i in range(len(test))))
array([[1, 3, 1, 3, 4, 3],
[0, 4, 1, 0, 2, 0],
[1, 3, 2, 0, 3, 2],
[0, 2, 1, 0, 0, 0],
[0, 0, 1, 2, 1, 0],
[3, 1, 4, 0, 1, 1]])
test[0]
array([[1, 3],
[0, 4],
[1, 3],
[0, 2],
[0, 0],
[3, 1]])
a = test[0]
for i in range(len(test)-1):
a = np.hstack((a, test[i+1] ))
a
0
1
array([[1, 3, 1, 3, 4, 3],
[0, 4, 1, 0, 2, 0],
[1, 3, 2, 0, 3, 2],
[0, 2, 1, 0, 0, 0],
[0, 0, 1, 2, 1, 0],
[3, 1, 4, 0, 1, 1]])
在陈总的帮助下成功了,现在可以比较一下,说明确实是成功进行了拼接:
f = plt.figure()
n=3
for i in range(n):
# Debug, plot figure
f.add_subplot(1, n, i + 1)
plt.imshow(test[i], cmap='hot')
plt.colorbar()
plt.show()
plt.imshow(np.reshape(np.hstack((test[i] for i in range(len(test)))),(6,6)), cmap='hot')
plt.colorbar()
plt.show()
<ipython-input-34-43f265a4cc40>:10: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
plt.imshow(np.reshape(np.hstack((test[i] for i in range(len(test)))),(6,6)), cmap='hot')
好了,现在就差最后一步拼接最外层的维度4418了。可以把问题简化为一个shape为(a,150,27*3)的矩阵如何化为2维的问题
可以将a分为x,y分别为横向和纵向,分别进行拼接。
先将问题简化为a = 64
import math
a = 452
test2 = np.random.randint(100,size=(a,6,6))
x = int(math.sqrt(a))
x
21
y = a / x
y
21.523809523809526
当a=452时,一行可以列21个,然后一共有超过21行。
现在可以拼接了:
x_len = int(math.sqrt(test2))
y_len = a / x
a = test2[0]
for i in len(test2):
col = int (i/x_len)
for x in range(x_len):
a = np.hstack((a, test[i+1] ))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-57-078fb9979486> in <module>
----> 1 x_len = int(math.sqrt(test2))
2 y_len = a / x
3 a = test2[0]
4 for i in len(test2):
5 col = int (i/x_len)
TypeError: only size-1 arrays can be converted to Python scalars
上面用算的再纵横拼接很麻烦,我就没有用了
从https://www.jb51.net/article/188978.htm
中得到灵感,尝试能不能直接把图拼出来,通过plt.subplots
import math
a = 452
test2 = np.random.randint(100,size=(a,3,6,2))
x_len = int(math.sqrt(len(test2)))
y_len = math.ceil( a / x_len )
figsize=(x_len,y_len)
# test2_tran = [np.hstack((test2[i][j] for j in range(test.shape[1]))) for i in range(test.shape[0])
# np.hstack((test[i] for i in range(len(test))))
plt.style.use('seaborn-whitegrid')
fig, ax = plt.subplots(x_len, y_len, figsize=figsize)
fig.subplots_adjust(hspace=0, wspace=0)
print(x_len,y_len)
# try:
for x in range(x_len):
for y in range(y_len):
if x * x_len + y == len(test2):
break
img = np.hstack((test2[x * x_len + y][j] for j in range(test2.shape[1])))
ax[x, y].xaxis.set_major_locator(plt.NullLocator())
ax[x, y].yaxis.set_major_locator(plt.NullLocator())
ax[x, y].imshow(img, cmap='hot')
else:
continue
break
plt.show()
# except:
# print('x:',x,' y:',y)
21 22
<ipython-input-91-8fccaa6731f0>:19: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
img = np.hstack((test2[x * x_len + y][j] for j in range(test2.shape[1])))
大功告成