Tensorflow 和 tflearn:使用序列设置数组元素时出现值错误

Tensorflow and tflearn : value error setting an array element with a sequence

我正在尝试使用 tflearn 使用卷积神经网络对图像进行分类。输入图像大小不同,我正在将它们全部调整为 32x32x3(我正在使用所有 RGB 通道)。这是我的代码。

import os
import numpy as np
from random import shuffle
from tqdm import tqdm
import tflearn
import cv2
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import tensorflow as tf
import matplotlib.pyplot as plt
import pickle

TRAIN_DIR = 'some_dir'

def label_img(img):
    word_label = img.split('.')[0]
    if 'metal' in word_label: 
        return np.array([1,0,0,0])
    elif 'plastic' in word_label: 
        return np.array([0,1,0,0])
    elif 'paper' in word_label: 
        return np.array([0,0,1,0])
    elif 'glass' in word_label: 
        return np.array([0,0,0,1])

def create_train_data():
    training_data = []
    for img in tqdm(os.listdir(TRAIN_DIR)):
        label = label_img(img)
        path = os.path.join(TRAIN_DIR,img)
        img = cv2.imread(path,1)
        img = cv2.resize(img, (32,32), interpolation = cv2.INTER_AREA)
        float_img = img.astype(float)
        float_img = float_img.reshape([-1,3,32,32])
        float_img = float_img.transpose([0,2,3,1])
        training_data.append([float_img, label])

    shuffle(training_data)
    with open('training_data.pickle','wb') as f:
        pickle.dump(training_data,f)

    return training_data

if os.path.exists('training_data.pickle'):
    pickle_in = open('training_data.pickle','rb')
    train_data = pickle.load(pickle_in)
else:
    train_data = create_train_data()


img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=15.)

network = input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep, data_augmentation=img_aug)
network = conv_2d(network, 32, 3, strides=1, padding='same', activation='relu', bias=True, bias_init='zeros', weights_init='uniform_scaling')
network = max_pool_2d(network, 2 , strides=None, padding='same')
network = conv_2d(network, 64, 3, strides=1, padding='same', activation='relu', bias=True, bias_init='zeros', weights_init='uniform_scaling')
network = conv_2d(network, 64, 3 , strides=1, padding='same', activation='relu', bias=True, bias_init='zeros', weights_init='uniform_scaling')
network = max_pool_2d(network, 2 , strides=None, padding='same')
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 4, activation='softmax')
network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001)

train = train_data[:-200]
test = train_data[-200:]

X = np.array([i[0] for i in train]).reshape(-1, 32, 32, 3)
Y = np.array([i[1] for i in train])

test_x = np.array([i[0] for i in test]).reshape(-1, 32, 32, 3)
test_y = np.array([i[1] for i in test])

model = tflearn.DNN(network, tensorboard_dir='log')
model.fit(X, Y, n_epoch=50, shuffle=True, validation_set= (test_x, test_y) , show_metric=True, batch_size=100 , run_id='aa2')

我收到函数代码最后一行的错误

model.fit(....)

错误表示“值 error:setting 具有序列 的数组元素”。 我查看了 tflearn 文档。它说输入(在我的例子中,函数 model.fit 中的 X,Y 参数)和验证([= 函数)中的22=]test_x,test_y参数都需要数组的数据类型。因此,我已将它们全部转换为 numpy 数组,但仍然出现此错误。 另外,我 运行 在另一台安装了 tensorflow virtualenv 的机器上运行这段代码。那台机器上的代码运行完美。我为此搜索了很多论坛,但其中 none 个有答案。

还有一点,我用过tflearn的图像预处理和图像增强。他们不应该通过添加更多图像(旋转 15 度的图像)在技术上增加数据集的大小吗?数据集大小也没有增加。使用这些函数有没有错误?

提前致谢!

更改此行:

float_img = float_img.reshape([-1,3,32,32])

float_img = float_img.reshape([3,32,32])

下面也不需要再reshape:

X = np.array([i[0] for i in train])<s>.reshape(-1, 32, 32, 3)</s>