Python 扩充mnsit数据集tensorflow

Python 扩充mnsit数据集tensorflow,python,tensorflow,machine-learning,computer-vision,mnist,Python,Tensorflow,Machine Learning,Computer Vision,Mnist,我正在尝试扩充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.place

我正在尝试扩充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

首先,请注意tf.reforme将类型从一个数组更改为一个张量。需要调用.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,这可能需要一段时间

首先,请注意tf.reforme将类型从一个数组更改为一个张量。需要调用.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,这可能需要一段时间

您是否可以添加有关您正在接收的错误消息以及您期望的信息?问题不完整。更新的问题您可以添加有关您收到的错误消息以及您期望的信息吗?问题不完整。更新的问题在图像翻转后,你能帮我将图像展平吗?我应该把扁平化的占位符放在哪里?是的!你能澄清一下你的意思吗?你能帮我在图像翻转后将图像展平吗?我应该把扁平化的占位符放在哪里?是的!你能澄清你的意思吗?