dp01
dp01

Reputation: 41

Tflearn training batch error says " object of type 'Tensor' has no len()"

I am new to tensorflow. I am using Tflearn to train my images to classify eye state. For initial period, right now, i have 400 training images and 200 validating images. I am using image_preloader to take custom image input in my script. I think it loads image successfully shows:

tflearn.data_utils.ImagePreloader object at 0x7fa28f3a5310

but it's causing problem while dividing and getting batches while training,

giving a Traceback error as

Traceback (most recent call last):
  File "tflearn_custom.py", line 181, in <module>
    model.fit(x,y,validation_set=({'input':test_x},{'targets':test_y}),n_epoch=10,batch_size=10)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.py", line 215, in fit
    callbacks=callbacks)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 285, in fit
    self.summ_writer, self.coord)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 709, in initialize_fit
    self.n_train_samples = len(get_dict_first_element(feed_dict))
TypeError: object of type 'Tensor' has no len()

This is my code:

import tflearn 
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 
import tensorflow as tf
from tflearn.data_utils import image_preloader

test_filename='/path_to_validating_set/'
train_filename='/path_to_training_set/'


x, y = image_preloader(train_filename, image_shape=(128, 128),  mode='folder',  grayscale=True, categorical_labels=True,   normalize=True)
test_x, test_y = image_preloader(test_filename, image_shape=(128, 128),   mode='folder', grayscale=True, categorical_labels=True,   normalize=True)

convnet =input_data(shape=[None, 128,128,1], name='input')  
convnet = conv_2d(convnet, 32, 2, activation='relu') 

convnet = max_pool_2d(convnet,2)

convnet = conv_2d(convnet, 64, 2, activation='relu')

convnet = max_pool_2d(convnet,2)

convnet = fully_connected(convnet, 1024, activation='relu')  

convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')



convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')

model= tflearn.DNN(convnet)


model.fit(x,y,validation_set=({'input':test_x},{'targets':test_y}),n_epoch=5,batch_size=5)

I am using tensorflow 1.0. I already tried searching similar problems but nothing could resolved it.

Upvotes: 4

Views: 6799

Answers (1)

aaon22357
aaon22357

Reputation: 51

I think the question is feed_dict is a tensor, and object of type Tensor has no length. You can use feed_dict.shape[0] to get it's length.

Upvotes: 5

Related Questions