我正在尝试扩充MNIST数据集。这是我试过的。无法取得任何成功。
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
X = mnist.train.images
y = mnist.train.labels
def flip_images(X_imgs):
X_flip = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (28, 28, 1))
input_d = tf.reshape(X_imgs, [-1, 28, 28, 1])
tf_img1 = tf.image.flip_left_right(X)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for img in input_d:
flipped_imgs = sess.run([tf_img1], feed_dict = {X: img})
X_flip.extend(flipped_imgs)
X_flip = np.array(X_flip, dtype = np.float32)
return X_flip
flip = flip_images(X)
我做错了什么?我似乎无法弄明白。
错误:
Line: for img in input_d:
raise TypeError("'Tensor' object is not iterable.")
TypeError: 'Tensor' object is not iterable
答案 0 :(得分:2)
首先,请注意您的tf.reshape将类型从ndarray更改为张量。它将需要一个.eval()调用才能将其恢复。在for循环中,您尝试迭代张量(不是列表或真正的可迭代),考虑在数字上进行索引,如下所示:
X = mnist.train.images
y = mnist.train.labels
def flip_images(X_imgs):
X_flip = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (28, 28, 1))
input_d = tf.reshape(X_imgs, [-1, 28, 28, 1])
tf_img1 = tf.image.flip_left_right(X)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for img_ind in range(input_d.shape[0]):
img = input_d[img_ind].eval()
flipped_imgs = sess.run([tf_img1], feed_dict={X: img})
X_flip.extend(flipped_imgs)
X_flip = np.array(X_flip, dtype = np.float32)
return X_flip
flip = flip_images(X)
如果这可以解决您的问题,请告诉我们!可能希望将范围设置为一个小常量进行测试,如果你没有GPU,这可能需要一段时间。