ValueError:无法使用tensorflow CNN将NumPy数组转换为张量(不支持的对象类型numpy.ndarray)

时间:2020-08-02 18:44:27

标签: python tensorflow machine-learning keras scikit-learn

我正在尝试建立一个模型,该模型可以对图片是否具有动物进行分类,但是我在处理数据时遇到了麻烦。我尝试运行我的代码:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D
from tensorflow.keras.layers import Activation, MaxPooling2D, Dropout, Flatten, Reshape
from tensorflow.keras.optimizers import RMSprop
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from matplotlib import pyplot
from matplotlib.image import imread
import tensorflow as tf
import os
import numpy as np

base = '/home/jose/Programming/aiml/Data/naturewatch'
# Directory of all the pictures with an animal 
critter = base + '/critter/'
# Directory of all the pictures without an animal
no_critter = base + '/no_critter/'

def load_data():
    data = []
    labels = []
    for raw in os.listdir(critter):
        # The array of values
        image = np.array(imread(critter + raw))
        data.append(image)
        # 1 for yes critter
        labels.append(1)
        # image.shape = (1088, 1920, 3)

    for raw in os.listdir(no_critter):
        # load image pixels
        image = np.array(imread(no_critter + raw))
        data.append(image)
        # 0 for no critter 
        labels.append(0)
        # image.shape = (1088, 1920, 3)
    data = np.array(data)
    labels = np.array(labels)
    return data, labels

data, labels = load_data()

# (2308,)
print(data.shape) 
print(labels.shape)

X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=101)

print(X_train.shape) # (1846,)
print(X_test.shape)
print(y_train.shape) # (462,)
print(y_test.shape)

# Plot 9 images
for i, image in enumerate(X_train[:9]):
    # define subplot
    pyplot.subplot(330 + 1 + i)
    pyplot.imshow(image)
    print('image', image.shape, 'label', y_train[i])
# show the figure
pyplot.show()

dropout = 0.2
model = Sequential()
# Reshape image to a much smaller size
model.add(Reshape((272, 480, 3)))

model.add(Conv2D(32, (3, 3), padding='same'))
model.add(Activation('relu'))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(dropout))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(dropout))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(dropout))
model.add(Dense(2))
model.add(Activation('softmax'))

# initiate RMSprop optimizer
opt = RMSprop(lr=0.0001, decay=1e-6)

# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
                optimizer=opt,
                metrics=['accuracy'])

model.fit(X_train, y_train) # Causes error

但是它会引发错误:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).行上的model.fit(X_train, y_train)。为什么会发生这种情况的任何想法?

我看过这篇文章Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float),但是解决方案对我不起作用,即像这样np.asarray(X).astype(np.float32)转换火车并进行测试(这会引起另一个错误ValueError: setting an array element with a sequence.)< / p>

由于该错误抱怨无法将np.array转换为张量,因此我尝试使用tf.convert_to_tensor()函数,但这会导致另一个错误:ValueError: Can't convert non-rectangular Python sequence to Tensor.

有人知道这里到底发生了什么吗?

1 个答案:

答案 0 :(得分:1)

好,我知道了。

首先,一张尺寸为(1088,1920)的图像太宽泛了。为了进行测试,我使用cv2.resize()将其重塑为(68,120)(摆脱了Reshape()层)。这样就解决了我的尺寸问题。例如,X_train不再是(1846,)而是(1846,68,120,3)。

因为我摆脱了Reshape()层,所以我指定了第一个Conv2D层,其input_size为(68,120,3),现在它可以工作了!