Search code examples
pythontensorflowconv-neural-networktflearn

Can't input images in the right shape for TFlearn


I have my target folder set up correctly I believe as below

Cell_Images ---> Cell_1 --> imgD1.png, imgD2.png...etc

                 Cell_2 --> imgT1.png, imgT2.png...etc

And here is my code that I am using

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
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_utils import image_preloader

dataset_file = 'C:/Users/Lenovo/dir2/CNN/Cell_Images'

X, Y = image_preloader(dataset_file, image_shape=(128, 128), mode='folder', grayscale= True, categorical_labels=True, normalize=True)


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

convnet = input_data(shape=[None,128,128,1], data_preprocessing=img_prep, 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, n_epoch=10, snapshot_step=500, show_metric=True, run_id='cells')

model.save('cellcnn.model')

But I am getting the following error relating to the shape of my input and I can't figure out how to reshape it properly. Any ideas? Thanks.

curses is not supported on this machine (please install/reinstall curses for an optimal experience)
---------------------------------
Run id: cells
Log directory: /tmp/tflearn_logs/
---------------------------------
Preprocessing... Calculating mean over all dataset (this may take long)...
Mean: 0.270792861708 (To avoid repetitive computation, add it to argument 'mean' of `add_featurewise_zero_center`)
---------------------------------
Preprocessing... Calculating std over all dataset (this may take long)...
STD: 0.283742975044 (To avoid repetitive computation, add it to argument 'std' of `add_featurewise_stdnorm`)
INFO:tensorflow:Summary name Accuracy/ (raw) is illegal; using Accuracy/__raw_ instead.
---------------------------------
Training samples: 156
Validation samples: 0
--
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-2db08f4fee1c> in <module>()
     31 model = tflearn.DNN(convnet)
     32 
---> 33 model.fit(X,Y, n_epoch=10, snapshot_step=500, show_metric=True, run_id='cells')
     34 
     35 model.save('cellcnn.model')

C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\models\dnn.py in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, excl_trainops, validation_batch_size, run_id, callbacks)
    213                          excl_trainops=excl_trainops,
    214                          run_id=run_id,
--> 215                          callbacks=callbacks)
    216 
    217     def predict(self, X):

C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\helpers\trainer.py in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, dprep_dict, daug_dict, excl_trainops, run_id, callbacks)
    331                                                        (bool(self.best_checkpoint_path) | snapshot_epoch),
    332                                                        snapshot_step,
--> 333                                                        show_metric)
    334 
    335                             # Update training state

C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\helpers\trainer.py in _train(self, training_step, snapshot_epoch, snapshot_step, show_metric)
    772         tflearn.is_training(True, session=self.session)
    773         _, train_summ_str = self.session.run([self.train, self.summ_op],
--> 774                                              feed_batch)
    775 
    776         # Retrieve loss value from summary string

C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    942                 'Cannot feed value of shape %r for Tensor %r, '
    943                 'which has shape %r'
--> 944                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    945           if not self.graph.is_feedable(subfeed_t):
    946             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (64, 128, 128) for Tensor 'input/X:0', which has shape '(?, 128, 128, 1)'

EDIT:

Tried reshpaing with Numpy as follows:

X = np.reshape(X, (64,128,128,1))

But got error:

ValueError: cannot reshape array of size 2555904 into shape (64,128,128,1)

Also tried reshaping with tf.reshape as follows:

X = tf.reshape(X, (64,128,128,1))

But got error:

ValueError: Argument must be a dense tensor: <tflearn.data_utils.ImagePreloader object at 0x00000176F774FEB8> - got shape [156, 128, 128], but wanted [].

Seems like the image preloader is doing something odd, is it due to the fact that the images are loaded on the fly and not stored? Or is the preloader just not returning the correct type of objects for further reshaping?


Solution

  • I got it to work by doing the following reshape:

    X = np.reshape(X, (-1, 128, 128,1))
    

    Seems you can't give it a specific batch size such as 64, so -1 is needed.