我可以对一批数据使用cv2.cvtColor(image,cv2.COLOR_GRAY2RGB)吗? [英] Can I use cv2.cvtColor(image,cv2.COLOR_GRAY2RGB) on a batch of data?

查看:2440
本文介绍了我可以对一批数据使用cv2.cvtColor(image,cv2.COLOR_GRAY2RGB)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Inception v3,尝试使用MNIST JPG图像作为预测数据集.当我需要将训练批次输入模型时,我遇到了一个问题.错误是由于形状. X_batch的形状为(?,299,299),其中第一层需要的形状为(?,299,299,3).在代码的不同部分中,我替换了一些示例,我能够使用example_image = cv2.cvtColor(example_image,cv2.COLOR_GRAY2RGB)将示例转换为RGB,从而得到example_image.shape为(299,299,3).我的问题是,我是否可以使用cv2将X_batch转换为RGB或代码的一部分,以使X_batch的形状为(?,299,299,3)?

I am working with Inception v3, trying to use MNIST JPG images as the dataset to predict. I am running into a problem when its time to input training batches into the model. The error is because of the shape. X_batch produces a shape of (?,299,299), where as the first layer needs a shape of (?, 299, 299, 3). In a different part of my code that displaces a few examples I was able to use example_image = cv2.cvtColor(example_image,cv2.COLOR_GRAY2RGB) to convert the examples into RGB which gave example_image.shape of (299, 299, 3). My question is, am I able to use the cv2 to convert my X_batch into RGB or a part of the code to have X_batch have a shape of (?,299, 299, 3)?

这是我需要进行转换的代码部分:

This is the part of the code that I need to have the conversion for:

from random import sample

def prepare_batch(MNIST_paths_and_classes, batch_size):
    batch_paths_and_classes = sample(MNIST_paths_and_classes, batch_size)
    images = [mpimg.imread(path)[:, :] for path, labels in batch_paths_and_classes]
    prepared_images = [prepare_image(image) for image in images]
    X_batch = 2 * np.stack(prepared_images) - 1 # Inception expects colors ranging from -1 to 1
    y_batch = np.array([labels for path, labels in batch_paths_and_classes], dtype=np.int32)
    return X_batch, y_batch

X_batch, y_batch = prepare_batch(MNIST_paths_and_classes_train, batch_size=4)

X_batch =(4,299,299) y_batch =(4,)

X_batch = (4, 299, 299) y_batch = (4,)

  X_test, y_test = prepare_batch(MNIST_paths_and_classes_test, batch_size=len(MNIST_paths_and_classes_test))

X_test =(12000,299,299)

X_test = (12000, 299, 299)

此部分中的错误:

ValueError:无法为形状为((?,299,299,3)'的张量'X:0'输入形状(40,299,299)的值

ValueError: Cannot feed value of shape (40, 299, 299) for Tensor 'X:0', which has shape '(?, 299, 299, 3)'

n_epochs = 10
batch_size = 40
n_iterations_per_epoch = len(MNIST_paths_and_classes_train) // batch_size

with tf.Session() as sess:
    init.run()
    inception_saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)

    for epoch in range(n_epochs):
        print("Epoch", epoch, end="")
        for iteration in range(n_iterations_per_epoch):
            print(".", end="")
            X_batch, y_batch = prepare_batch(MNIST_paths_and_classes_train, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch, training: True})

        acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
        print("  Train accuracy:", acc_train)

        save_path = saver.save(sess, "./my_MNIST_model")

推荐答案

我不明白是什么使您感到困惑.如您所说,cv2.cvtColor将提供正确的形状,因此只需将X_batch中的图像一张一张地转换.

I don't understand what confuses you. As you said, cv2.cvtColor would give the correct shape, so just convert images in X_batch one by one.

X_batch_rgb = np.copy(X_batch)
for i in len(X_batch):
    X_batch_rgb[i, ...] = cv2.cvtColor(X_batch[i, ...],cv2.COLOR_GRAY2RGB)

现在X_batch_rgb数组具有所需的形状.

Now the X_batch_rgb array has the desired shape.

这篇关于我可以对一批数据使用cv2.cvtColor(image,cv2.COLOR_GRAY2RGB)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆