2

I am going to use Keras pretrained Inception V3 model. After preprocessing the image shape is 224 x 224 x 3. But the input to the Keras Inception V3 model is (?, 3 , ?, ?), that is after batch size comes the channel. So I did array reshape. But this makes the whole thing super slow and eats up memory I am not sure why.

Note: When the image shape was 224, 224, 3 it works fine on a simple CNN. But 3, 224, 224 fed to the simple CNN made things super slow and memory overflow.

This is my code:

def get_image_preprocessed(image_name):
    im = Image.open(image_name)
    im = np.asarray(im)
    im = im/float(255)
    im = im.reshape(3,224,224) #this changes 224,224,3 to 3,224,224
    return im

This is the input tensor shape

tf.Tensor 'input_1:0' shape=(?, 3, ?, ?) dtype=float32

More Information:

Model-

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3,224, 224), padding='same', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))

Generator Function-

def generator(save_dir_path, encoding_list, batch_size, image_size):
    # Create empty arrays to contain batch of features and labels#
    batch_features = np.zeros((batch_size, 3, image_size, image_size))
    batch_labels = np.zeros((batch_size,len(encoding_list)))
    image_list= [file for file in os.listdir(save_dir_path) if (file.endswith('.jpeg') or file.endswith('.-'))]
    while True:
        for i in range(batch_size):
            # choose random index in features
            image_name= random.choice(image_list)
            batch_features[i] = get_image_preprocessed(save_dir_path, image_name)
            batch_labels[i] = np.asarray(get_encoding(encoding_list, image_name.split('_')[0]))
        yield batch_features, batch_labels
sjishan
  • 3,392
  • 9
  • 29
  • 53
  • This looks like a `transpose` problem, not a `reshape` one. The `3` should continue to represent the channels, and the `224,224` the image shape. – hpaulj Dec 30 '17 at 18:08
  • i did, the problem did not resolved. i have added more information here. – sjishan Dec 30 '17 at 18:22
  • The problem is the backend. You need to read the documentation for the correct use of this model. – enterML Dec 30 '17 at 19:24

2 Answers2

3

You can use .transpose for this with:

im = im.transpose(2,0,1)

So from now on, the old third index (2) is the first index, the old first index (0) is the second index, and the old second index (1) is the third index.

So if you access im[i,j,k] is is like you had accessed im[j,k,i] before the transpose.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I just did transpose, the problem is still there. when using 224,224,3 it is taking around 300MB but when 3,224,224 it is taking 4.4GB. This is crazy. – sjishan Dec 30 '17 at 18:19
  • @sjishan: that is close to impossible, since `transpose` does *not* create a new array, it creates a *view*. Usually that would take ~100 bytes . – Willem Van Onsem Dec 30 '17 at 18:24
  • Hi Yes. I figured it out, this is not the issue with `transpose` or `reshape`. 224,224,3 creates 1,910,851 parameters. 3,224,224 creating 205,533,091 parameters. So that's overflowing the memory. – sjishan Dec 30 '17 at 18:29
0

Other than reshape and transpose, another similar solution: swapaxes in the Numpy library. The following line swaps the first axis with the 3rd one in an array im.

im.swapaxes(0,2)

if a is an ndarray, then a view of a is returned; otherwise a new array is created. --Quoted from numpy-1.13.0 Docs

Reference

How does numpy.swapaxes work?

Tai
  • 7,684
  • 3
  • 29
  • 49