如何将 Tensorflow 数据集转换为 2D numpy 数组

ple*_*ant 6 python tensorflow

我有一个 TensorFlow 数据集,其中包含近 15000 个分辨率为 168*84 的彩色图像,每个图像都有一个标签。它的类型和形状是这样的:

< ConcatenateDataset shapes: ((168, 84, 3), ()), types: (tf.float32, tf.int32)>
Run Code Online (Sandbox Code Playgroud)

我需要用它来训练我的网络。这就是为什么我需要将它作为参数传递给我在其中构建层的函数:

def cnn_model_fn(features, labels, mode):

  input_layer = tf.reshape(features["x"], [-1, 168, 84, 3])
  # Convolutional Layer #1
  conv1 = tf.layers.conv2d(
     inputs=input_layer,
     filters=32,
     kernel_size=[5, 5],
     padding="same",
     activation=tf.nn.relu)
.
.
.
Run Code Online (Sandbox Code Playgroud)

我尝试使用 tf.eval() 和 np.ravel() 将每个张量转换为 np.array(我猜这是上面函数的正确类型)。但我失败了。

那么,如何将此数据集转换为正确的类型以将其传递给函数?

我对 python 和 TensorFlow 很陌生,如果我们不能直接使用数据集来构建层,我认为我不明白为什么会有数据集(顺便说一句,我正在遵循 TensorFlow 网站上的教程)。

谢谢。

Mas*_*r M 5

您可以尝试急切执行,之前我通过会话运行给出了答案(如下所示)。
在急切执行期间,在张量上使用 .numpy() 会将该张量转换为 numpy 数组。
示例代码(来自我的用例):


    #enable eager execution
    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow as tf
    tf.enable_eager_execution()
    print('Is executing eagerly?',tf.executing_eagerly())      

    #load datasets
    import tensorflow_datasets as tfds
    dataset, metadata = tfds.load('cycle_gan/horse2zebra',
                                  with_info=True, as_supervised=True)
    train_horses, train_zebras = dataset['trainA'], dataset['trainB']

    #load dataset in to numpy array 
    train_A=train_horses.batch(1000).make_one_shot_iterator().get_next()[0].numpy()
    print(train_A.shape)

    #preview one of the images
    import matplotlib.pyplot as plt
    %matplotlib inline
    import numpy as np
    print(train_A.shape)
    plt.imshow(train_A[1])
    plt.show()
Run Code Online (Sandbox Code Playgroud)

旧的,会话运行,答案:

我最近遇到了这个问题,我是这样做的:


    #load datasets
    import tf
    import tensorflow_datasets as tfds
    dataset, metadata = tfds.load('cycle_gan/horse2zebra',
                                  with_info=True, as_supervised=True)
    train_horses, train_zebras = dataset['trainA'], dataset['trainB']

    #load dataset in to numpy array
    sess = tf.compat.v1.Session()
    tra=train_horses.batch(1000).make_one_shot_iterator().get_next()
    train_A=np.array(sess.run(tra)[0])
    print(train_A.shape)
    sess.close()

    #preview one of the images
    import matplotlib.pyplot as plt
    %matplotlib inline
    import numpy as np
    print(train_A.shape)
    plt.imshow(train_A[1])
    plt.show()

Run Code Online (Sandbox Code Playgroud)


Dav*_*rks 3

听起来不像您使用 Tensorflow 数据集管道进行设置,以下是这样做的指南:

https://www.tensorflow.org/programmers_guide/datasets

您可以遵循该方法(这是正确的方法,但有一个小的学习曲线来适应它),或者您可以将 numpy 数组作为参数sess.run的一部分传递feed_dict。如果您采用这种方式,那么您应该只创建一个tf.placeholder将由 中的值填充的值feed_dict。这里的许多基本教程示例都遵循这种方法:

https://github.com/aymericdamien/TensorFlow-Examples