【问题标题】:How to convert grayscale image shape with 1 channel to coloured image shape with 3 channels?如何将具有 1 个通道的灰度图像形状转换为具有 3 个通道的彩色图像形状?
【发布时间】:2021-05-14 18:31:26
【问题描述】:

我想将 mnist 数据集加载到 mobilenet V1 CNN 然后,我遇到了这个问题

ValueError: Error when checking input: expected input_1 to have shape (32, 32, 3) but got array with shape (28, 28, 1)

下面是我的代码

image_data, label_data = data['image'], data['label']


idx_list = {}
for i in range(10):
    idx_list[i] = np.where(label_data == i)  # return tuple dtype (rows indices, column indices)


selected_test_sample_indices = {}
for label in range(10):
    selected_test_sample_indices[label] = random.sample(set(idx_list[label][0]), int(len(idx_list[label][0]) * 0.2))


selected_train_sample_indicies = {}

for label in range(10):
    selected_train_sample_indicies[label] = list(set(idx_list[label][0])- set(selected_test_sample_indices[label]))


train_data_indicies, test_data_indicies = [],[]


for label, indicies in selected_train_sample_indicies.items():
    train_data_indicies = train_data_indicies + indicies # merge 2 list

for label, indicies in selected_test_sample_indices.items():
    test_data_indicies = test_data_indicies + indicies

random.shuffle(train_data_indicies)
random.shuffle(test_data_indicies)


y_train_data = np.array([label_data[idx] for idx in train_data_indicies])
X_train_data = np.array([image_data[idx] for idx in train_data_indicies])

y_test_data = np.array([label_data[idx] for idx in test_data_indicies])
X_test_data = np.array([image_data[idx] for idx in test_data_indicies])

number_of_classes = 10
y_train = y_train_data
y_test = y_test_data


X_train = X_train_data.reshape(X_train_data.shape[0], img_rows, img_cols, 1)
X_test = X_test_data.reshape(X_test_data.shape[0], img_rows, img_cols, 1)```

当我尝试重塑时出现以下错误

ValueError: cannot reshape array of size 11146912 into shape (14218,32,32,1)

当我将其更改为 (4500,32,32,3) 时,总和低于 11146912 这真的让我很困惑。 请帮我修复这个错误。

【问题讨论】:

    标签: python arrays numpy tensorflow


    【解决方案1】:

    在将灰度图像转换为 rgb 图像后,图像的形状会发生变化

    28 x 28 x 128 x 28 x 3

    然后你需要将它调整为 32。你可以使用 openCV 库。

    resized_image = cv2.resize(image, (32, 32))
    

    那么您的 resized_image 形状将是 32 x 32 x 3

    【讨论】:

      【解决方案2】:

      MNIST 数据集包含大小为 28x28 像素的灰度图像。这就是为什么每个图像的形状都是 (28, 28, 1),每个值都在 0-255 之间。这是 another stackoverflow 的问题,有同样的问题。最有效的答案是将灰度图像转换为 rgb 图像,然后调整图像大小。

      【讨论】:

      • 感谢您的解释,我浏览了那个网站,它只将批次更改为 3,但是对于 col,row 的值,我无法将其更改为 (32, 32),它也改变我'ValueError:无法将大小为11146912的数组重塑为形状(14218,32,32,3)'”,我该如何解决这个问题。
      • @JovanMei 你可以查看 opencv 来调整图像大小
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2019-03-15
      • 2020-11-08
      • 2018-03-29
      • 2012-04-15
      相关资源
      最近更新 更多