【问题标题】:Convert a list of images and labels to np array to train tensorflow将图像和标签列表转换为 np 数组以训练 tensorflow
【发布时间】:2017-10-20 17:40:11
【问题描述】:

我正在使用关于卷积神经网络的教程。在这个函数中,我使用:

# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": X_train},
    y=y_train,
    batch_size=100,
    num_epochs=None,
    shuffle=True)
 mnist_classifier.train(
    input_fn=train_input_fn,
    steps=20000,
    hooks=[logging_hook])

在哪里

type(X_train) 

列表

type(y_train)

列表

y_train[0]

'0'

X_train[0].shape

(30, 29, 3)

type(X_train[0])

numpy.ndarray

len(X_train)

39209

len(y_train)

39209

我收到以下错误: AttributeError: 'list' 对象没有属性 'shape'

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    X_train 看起来像一个 list of numpy 数组,而 tensorflow 需要一个 numpy 数组,您可以通过以下方式简单地将其转换为 numpy 数组:

    X_train = np.array(X_train)
    

    或使用numpy.asarray 函数,其作用与上述完全相同:

    X_train = np.asarray(X_train)
    

    请记住,您的所有图像都应该具有相同的尺寸才能转换为工作。

    【讨论】:

    • 谢谢。如何在不使用 opencv 的情况下将图像转换为相同大小。我想要 32 x 32 x 3 的图像。
    • @Maggie,OpenCV 会让事情变得更简单,但您也可以使用 scipy 的图像相关库,尤其是imresize。但是如果你只想使用 numpy,你可以很容易地 crop or pad images 使用 vanilla numpy 函数,而调整大小会复杂得多。
    猜你喜欢
    • 2020-09-23
    • 2018-08-28
    • 2020-10-18
    • 2021-07-02
    • 2022-08-03
    • 2020-10-04
    • 1970-01-01
    • 2021-11-12
    • 2022-10-24
    相关资源
    最近更新 更多