0

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!

2
  • All elements in X (and Y) should have the same dimension. There must be a problem with your preprocessing messing around with the dimensions. Commented Jan 13, 2018 at 18:22
  • @sladomic I have the shape of X as (2327, 32, 32, 3) and the shape of Y as (2327,). Y should only contain the one hot encoded labels of the respective data so where exactly do you think the problem is? Are the shapes of X and Y both wrong? I have 2327 images for training and 200 for testing. Commented Jan 13, 2018 at 18:37

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Also I think cv2.imread() returns the channels in the last column. So it should rather be float_img.reshape([32,32,3])
I tried implementing these changes in the code. When i remove the reshape, tensorflow gives me another error saying ValueError: Cannot feed value of shape (100, 1, 32, 32, 3) for Tensor 'InputData/X:0', which has shape '(?, 32, 32, 3)'. But anyway, even if i change just the float_img line, the error is still there.
Hmm, for me both solutions seem to work, but I would prefer the one without reshaping, since after the resize the shapes are already correct (32,32,3). Have you checked, that you are retrieving the raw images instead of an old pickle file?
Yep I've checked for the old pickle file. The code just doesn't seem to run for me. It is giving me the same error :(
Got it, they was some problem during processing the data! I took a look at the dataset and sorted it out. :P Thanks though for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.