如何在图像前面添加一个维度



现在我已经通过以下方法将图像(32,32(更改为(32,32,1(

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.expand_dims(img, axis=-1)
img = img.astype(np.float32)/255
img = tf.image.resize(img, [32,32])

但现在,我想从(32,32,1(改为(1,32,32,1(,所以我尝试使用

img = img.reshape(1,32,32,1)

但是,它显示了'EagerTensor' object has no attribute 'reshape'.,那么我还可以使用其他什么方法呢?

尝试:

img = tf.random.normal((64, 64, 1))
img = tf.image.resize(img, [32,32])
img = tf.reshape(img, (1,32,32,1))

img = tf.expand_dims(img, axis=0)

最新更新