1. np.expand_dims
用于扩展数组的维度
执行程序后注意观察中括号[ ]的位置和数量
np.expand_dims(a, axis=0)表示在axis=0维度处扩展维度,加一层中括号[ ];
np.expand_dims(a, axis=1)表示在axis=1维度处扩展维度,加一层中括号[ ];
np.expand_dims(a, axis=2)表示在axis=2维度处扩展维度,加一层中括号[ ];
np.expand_dims(a, axis=-1)表示在axis=-1(最后)维度处扩展维度,加一层中括号[ ];
(py3.6) E:\PYTHON>ipython
Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.3 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: a = np.array([[1, 2, 3],[4, 5, 6]])
In [3]: a
Out[3]:
array([[1, 2, 3],
[4, 5, 6]])
In [4]: a.shape
Out[4]: (2, 3)
1.1 axis=0
In [5]: b = np.expand_dims(a, axis=0)
In [6]: b
Out[6]:
array([[[1, 2, 3],
[4, 5, 6]]])
In [7]: b.shape
Out[7]: (1, 2, 3)
1.2 axis=1
In [8]: c = np.expand_dims(a, axis=1)
In [9]: c
Out[9]:
array([[[1, 2, 3]],
[[4, 5, 6]]])
In [10]: c.shape
Out[10]: (2, 1, 3)
1.3 axis=2
In [11]: d = np.expand_dims(a, axis=2)
In [12]: d
Out[12]:
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]])
In [13]: d.shape
Out[13]: (2, 3, 1)
1.4 axis=-1
In [14]: e = np.expand_dims(a, axis=-1)
In [15]: e
Out[15]:
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]])
In [16]: e.shape
Out[16]: (2, 3, 1)
1.5 axis=3
In [17]: f = np.expand_dims(a, axis=3)
---------------------------------------------------------------------------
AxisError Traceback (most recent call last)
<ipython-input-16-d7316647942f> in <module>
----> 1 f = np.expand_dims(a, axis=3)
<__array_function__ internals> in expand_dims(*args, **kwargs)
D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\lib\site-packages\numpy\lib\shape_base.py in expand_dims(a, axis)
595
596 out_ndim = len(axis) + a.ndim
--> 597 axis = normalize_axis_tuple(axis, out_ndim)
598
599 shape_it = iter(a.shape)
D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\lib\site-packages\numpy\core\numeric.py in normalize_axis_tuple(axis, ndim, argname, allow_duplicate)
1325 pass
1326 # Going via an iterator directly is slower than via list comprehension.
-> 1327 axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
1328 if not allow_duplicate and len(set(axis)) != len(axis):
1329 if argname:
D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\lib\site-packages\numpy\core\numeric.py in <listcomp>(.0)
1325 pass
1326 # Going via an iterator directly is slower than via list comprehension.
-> 1327 axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
1328 if not allow_duplicate and len(set(axis)) != len(axis):
1329 if argname:
AxisError: axis 3 is out of bounds for array of dimension 3
实例:
素材:注意程序中用的是相对路径
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
import numpy as np
from tensorflow.keras.preprocessing import image
from skimage import io
image_path="../images/lena.jpg"
# 加载图像
img = io.imread(image_path)
# 显示图像
io.imshow(img)
io.show()
# 图像预处理
x = image.img_to_array(img)
y = np.expand_dims(x, axis = 0)
print("x:", x)
print("y:", y)
print(x.shape)
print(y.shape)
结果:
D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\python.exe E:\Python_files\Tensorflow_dl\TensorFlowProgramming\TensorFlowProgramming\ch6CNN\pool_op_demo2.py
2023-04-04 08:24:33.571665: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2023-04-04 08:24:33.571878: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
x: [[[221. 139. 125.]
[221. 139. 125.]
[222. 139. 125.]
...
[223. 136. 108.]
[216. 131. 102.]
[211. 126. 95.]]
[[221. 139. 125.]
[221. 139. 125.]
[222. 139. 125.]
...
[225. 139. 112.]
[217. 132. 103.]
[211. 126. 97.]]
[[223. 140. 124.]
[223. 140. 124.]
[223. 140. 124.]
...
[225. 141. 117.]
[214. 130. 104.]
[206. 122. 96.]]
...
[[ 81. 26. 58.]
[ 83. 28. 60.]
[ 84. 27. 59.]
...
[161. 72. 90.]
[163. 75. 91.]
[163. 75. 91.]]
[[ 80. 25. 56.]
[ 82. 27. 58.]
[ 83. 26. 58.]
...
[164. 76. 92.]
[167. 76. 93.]
[167. 76. 93.]]
[[ 80. 25. 56.]
[ 81. 26. 57.]
[ 82. 25. 57.]
...
[166. 78. 94.]
[169. 78. 95.]
[169. 78. 95.]]]
y: [[[[221. 139. 125.]
[221. 139. 125.]
[222. 139. 125.]
...
[223. 136. 108.]
[216. 131. 102.]
[211. 126. 95.]]
[[221. 139. 125.]
[221. 139. 125.]
[222. 139. 125.]
...
[225. 139. 112.]
[217. 132. 103.]
[211. 126. 97.]]
[[223. 140. 124.]
[223. 140. 124.]
[223. 140. 124.]
...
[225. 141. 117.]
[214. 130. 104.]
[206. 122. 96.]]
...
[[ 81. 26. 58.]
[ 83. 28. 60.]
[ 84. 27. 59.]
...
[161. 72. 90.]
[163. 75. 91.]
[163. 75. 91.]]
[[ 80. 25. 56.]
[ 82. 27. 58.]
[ 83. 26. 58.]
...
[164. 76. 92.]
[167. 76. 93.]
[167. 76. 93.]]
[[ 80. 25. 56.]
[ 81. 26. 57.]
[ 82. 25. 57.]
...
[166. 78. 94.]
[169. 78. 95.]
[169. 78. 95.]]]]
(512, 512, 3)
(1, 512, 512, 3)
Process finished with exit code 0
参考文章:
https://blog.csdn.net/qq_37924224/article/details/119816771
https://blog.csdn.net/hong615771420/article/details/83448878