在深度学习中,图像数据通道格式有两种:
**结论:**在训练模型时,使用GPU,适合NCHW格式;在CPU中做推理时,适合NHWC格式。采用什么格式排列,由计算硬件的特点决定。OpenCV在设计时是在CPU上运算的,所以默认HWC格式。TensorFlow的默认格式是NHWC,也支持cuDNN的NCHW
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("data/images/bus.jpg")
img = cv2.resize(img, (5,4)) #(x, y) -> (W, H)
print(img.shape) # (H,W,C)
plt.figure()
plt.imshow(img)
plt.show()
line1 = img[1,...]
line1 = np.expand_dims(line1, axis=0)
print(line1.shape)
print(line1)
plt.figure()
plt.imshow(line1)
plt.show()
print("BGR->RGB")
rgb_line1 = cv2.cvtColor(line1, cv2.COLOR_BGR2RGB)
print(rgb_line1.shape)
print(rgb_line1)
print()
print("HWC->CHW")
CHW_line1 = np.transpose(line1, (2,0,1))
print(CHW_line1.shape)
print(CHW_line1)
运行结果如下:
作者:LabVIEW_Python
链接:https://www.jianshu.com/p/61de601bc90f
来源:简书