What is the right way to reshape a tensor?

Question:

The code below is for lungs segmentation(2D) from Chest X-ray. It is supposed to generate lung masks from Chest X-rays using the trained model ‘trained_model.hdf5’. On giving a chest x-ray as input, it should be able to identify which are the lungs and create a separate mask of the lungs accordingly. The trained_model.hdf5 contains model trained on JSRT data sets.

#from load_data import loadDataJSRT, loadDataMontgomery

import numpy as np
import pandas as pd
from keras.models import load_model
from skimage import morphology, io, color, exposure, img_as_float, transform
from keras.preprocessing.image import ImageDataGenerator

def loadDataGeneral(df, path, im_shape):
    X = []
    for i, item in df.iterrows():
        img = img_as_float(io.imread(path + str(item[0])))
        #mask = io.imread(path + item[1])
        img = transform.resize(img, im_shape)
        img = exposure.equalize_hist(img)
        img = np.expand_dims(img, -1)
        #mask = transform.resize(mask, im_shape)
        #mask = np.expand_dims(mask, -1)
        X.append(img)
        #y.append(mask)
    X = np.array(X)
    #y = np.array(y)
    X -= X.mean()
    X /= X.std()

    print( '### Dataset loaded')
    print( 't{}'.format(path))
    #print( 't{}t{}'.format(X.shape, y.shape))
    #print( 'tX:{:.1f}-{:.1f}ty:{:.1f}-{:.1f}n'.format(X.min(), X.max(), y.min(), y.max()))
    print( 'tX.mean = {}, X.std = {}'.format(X.mean(), X.std()))
    return X



if __name__ == '__main__':

    # Path to csv-file. File should contain X-ray filenames as first column,
    # mask filenames as second column.
    csv_path = 'idx.csv'
    # Path to the folder with images. Images will be read from path + path_from_csv
    path = 'Data/'

    df = pd.read_csv(csv_path)

    # Load test data
    im_shape = (256, 256)
    X = loadDataGeneral(df, path, im_shape)
    #print('***X= ',X)
    n_test = X.shape[0]
    inp_shape = X[0].shape

    # Load model
    model_name = 'trained_model.hdf5'
    UNet = load_model(model_name)

    # For inference standard keras ImageGenerator is used.
    test_gen = ImageDataGenerator(rescale=1.)

    ious = np.zeros(n_test)
    dices = np.zeros(n_test)

    i = 0

    print("TEST_GEN ",test_gen)

    print(len(X))
    for xx in test_gen.flow(X, batch_size=1):
        xx = xx[0:4]
        img = exposure.rescale_intensity(np.squeeze(xx), out_range=(0,1))
        pred = UNet.predict(xx)[..., 0].reshape(inp_shape[:2])
        #mask = yy[..., 0].reshape(inp_shape[:2])

        # Binarize masks
        #gt = mask > 0.5
        pr = pred > 0.5

        # Remove regions smaller than 2% of the image
        pr = remove_small_regions(pr, 0.02 * np.prod(im_shape))

        io.imsave('results/{}'.format(df.iloc[i][0]), masked(img, pr, 1))

        #ious[i] = IoU(gt, pr)
        #dices[i] = Dice(gt, pr)
        #print(df.iloc[i][0], ious[i], dices[i])

        i += 1
        if i == n_test:
            break

However I get this error:

### Dataset loaded
        Data/
        X.mean = -1.042684457293793e-15, X.std = 1.0000000000000002
2018-09-28 09:45:55.598419: W C:tf_jenkinshomeworkspacerel-winMwindowsPY36tensorflowcoreplatformcpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-09-28 09:45:55.605772: W C:tf_jenkinshomeworkspacerel-winMwindowsPY36tensorflowcoreplatformcpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
C:Anaconda3envstensorflowlibsite-packageskerasenginesaving.py:304: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
  warnings.warn('Error in loading the saved optimizer '
TEST_GEN  <keras_preprocessing.image.ImageDataGenerator object at 0x00000159CC91AEB8>
5
Traceback (most recent call last):
  File "inference_1.py", line 67, in <module>
    for xx in test_gen.flow(X, batch_size=1):
  File "C:Anaconda3envstensorflowlibsite-packageskeras_preprocessingimage.py", line 867, in flow
    subset=subset)
  File "C:Anaconda3envstensorflowlibsite-packageskeras_preprocessingimage.py", line 1427, in __init__
    'with shape', self.x.shape)
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (5, 256, 256, 3, 1))

How do I reshape the tensor ? What am I doing wrong ?

Asked By: ArnabC

||

Answers:

ImageDataGenerator expects the shape of input to be (samples, height, width, channels) but in your case, there’s an extra dimension. But the shape of your input X is (samples, height, width, channels, 1) so you need to drop that last dimension first.

To answer your question about reshaping a tensor, there are multiple ways to do this.
Try

X = X[:, :, :, 0]

OR

X = X[:, :, :, -1]

OR

X = tf.reshape(X, [5, 256, 256, 3])

OR

X = tf.squeeze(X)
Answered By: betelgeuse