ultrasound-nerve-segmentation icon indicating copy to clipboard operation
ultrasound-nerve-segmentation copied to clipboard

Help to fix this error

Open Guru-prasad opened this issue 7 years ago • 3 comments

Hi, thanks for replying regarding train.py i changed from python 3.5 to python 2.7 now i am getting following error:

   71     conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
   72 

---> 73 up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=3) 74 conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7) 75 conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)

ValueError: Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 0, 104, 256), (None, 0, 105, 128)]

i have used theano backend.

the code is : `from future import print_function

import os os.environ['KERAS_BACKEND']='theano' import keras from skimage.transform import rotate, resize, rescale #from skimage.io import imsave from skimage.io import imsave, imread import numpy as np from keras.models import Model from keras.layers import Input, concatenate, Conv2D, MaxPooling2D, UpSampling2D from keras.optimizers import Adam from keras.callbacks import ModelCheckpoint from keras import backend as K K.image_dim_ordering = 'th'

#from data import load_train_data, load_test_data

K.set_image_data_format('channels_last') # TF dimension ordering in this code

img_rows = 420 img_cols = 580

smooth = 1.

def load_train_data(): imgs_train = np.load('imgs_train.npy') imgs_mask_train = np.load('imgs_mask_train.npy') return imgs_train, imgs_mask_train

def load_test_data(): imgs_test = np.load('imgs_test.npy') imgs_id = np.load('imgs_id_test.npy') return imgs_test, imgs_id

def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)

def dice_coef_loss(y_true, y_pred): return -dice_coef(y_true, y_pred)

def get_unet(): inputs = Input((1, img_rows, img_cols)) conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs) conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)

up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)

up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)

up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)

up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)

conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)

model = Model(inputs=[inputs], outputs=[conv10])

model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])

return model`

thanks in advance!!

Guru-prasad avatar May 16 '17 16:05 Guru-prasad

If you take a look at your input image size:

img_rows = 420
img_cols = 580

Both of your width and height has to be divisible by 2^4=16 (because there are 4 MaxPooling2D layers). For example for height (img_rows), size of the feature maps changes through the network as: 420 -> 210 -> 105 -> 52 (rounded from 52.5) -> 26 -> 52 -> 104 -> 208 -> 416 If you take a look at the bolded ones, you try to concatenate them and it makes perfect sense this is not possible, because feature maps are of different size.

If you are using custom data - make sure your input image width and height are divisible by 16!

jocicmarko avatar May 19 '17 11:05 jocicmarko

So, how we can customize our image width and height !! i am also using image size as rows, cols = 327, 532 Please help

pratikgorji avatar Jun 22 '17 10:06 pratikgorji

Please read my comment just above yours, it answers your question.

jocicmarko avatar Jun 22 '17 10:06 jocicmarko