Search code examples
pythonarraysnumpytensorflowtflearn

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


I am trying to classify images using Convolutional Neural Network using tflearn. The input images are of different sizes and I am resizing all of them to 32x32x3 (i am using all RGB channels). Here's my code.

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')

I am getting the error for the last line of the code that is for the function

model.fit(....)

The error says that "value error:setting an array element with a sequence". I checked with the tflearn documentation. It says that the input(in my case, the X,Y parameters in the function model.fit) and the validation(test_x,test_y parameters in the function) both require the data type of an array. So, I've converted all of them into numpy arrays but still I am getting this error. Plus, I ran this code on another machine which has a virtualenv of tensorflow installed. The code ran on that machine perfectly. I have searched many forums for this but none of them had an answer.

One more thing, I have used image preprocessing and image augmentation of tflearn. Shouldn't they technically increase the data set size by adding more images (rotated images of 15 degrees)? The data set size is also not getting increased. Is there any error while using those functions?

Thanks in advance!


Solution

  • Change this line:

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

    to

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

    also you don't need to reshape again below:

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