问题
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_test, y_test = prepare_batch(MNIST_paths_and_classes_test, batch_size=len(MNIST_paths_and_classes_test))
X_test = (12000, 299, 299)
Error in this section:
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")
回答1:
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)
Now the X_batch_rgb
array has the desired shape.
来源:https://stackoverflow.com/questions/55779099/can-i-use-cv2-cvtcolorimage-cv2-color-gray2rgb-on-a-batch-of-data