EdwardTyantov / ultrasound-nerve-segmentation

Kaggle Ultrasound Nerve Segmentation competition [Keras]
181 stars 61 forks source link

Regarding img_rows and img_cols in preprocess(imgs) function #6

Closed huaiyanggongzi closed 7 years ago

huaiyanggongzi commented 7 years ago

Hi Edward,

While running your code, I have a question regarding preprocess(imgs).

def preprocess(imgs, to_rows=None, to_cols=None):
    if to_rows is None or to_cols is None:
        to_rows = img_rows
        to_cols = img_cols
    imgs_p = np.ndarray((imgs.shape[0], imgs.shape[1], to_rows, to_cols), dtype=np.uint8)
    for i in xrange(imgs.shape[0]):
        imgs_p[i, 0] = cv2.resize(imgs[i, 0], (to_cols, to_rows), interpolation=cv2.INTER_CUBIC)
    return imgs_p

At first, you define imgs_p with the shape of (batchsize, channel, to_rows,to_cols). However, in the resize function, you resize the original image to (to_cols, to_rows). It seems to me that it should resize to (to_rows,to_cols).

In fact, if I use the original shape, imgs_p[i, 0] = resize(imgs[i, 0], (to_cols, to_rows)) I got the following error message

Traceback (most recent call last): File "train.py", line 153, in <module> train_and_predict() File "train.py", line 92, in train_and_predict imgs_train = preprocess(imgs_train) File "train.py", line 81, in preprocess imgs_p[i, 0] = resize(imgs[i, 0], (to_cols, to_rows)) ValueError: could not broadcast input array from shape (80,64) into shape (64,80)

Would you like to take a look at this issue? Thank you very much.

EdwardTyantov commented 7 years ago

Hi, do you use tensorflow as a backend ? With theano the code works fine. It seems strange, and i can't remember the reason why the code looks like this. Maybe the reason - opencv, different versions.

huaiyanggongzi commented 7 years ago

Hi Edward,

I am using tensorflow as a backend.

As a temporary walkaround, I just set cols=rows. to bypass this issue. However, I still have difficulties to get the code run. Generally, the thing I changed is to ensure the image is of shape (batch, width, height, 1).

In your code, I did not find other places which are Theano dependent. Do you have any suggestions for which part of the code I may need to modify to make it Tensorflow runnable. Thanks.

Huaiyang

EdwardTyantov commented 7 years ago

augmentation.py contains theano-dep code (CustomImageDataGenerator) is case you run train_generator.py

EdwardTyantov commented 7 years ago

It's odd, I used tf backend without a problem (

huaiyanggongzi commented 7 years ago

Hi Edward,

Originally, I changed the image shape to (nb_sample, height, width, 1) to comply with TF. It gave me the error message as mentioned above. I just changed it back to (nb_sample, 1, height, width), and still use Tensorflow as backend. Now it can run. But frankly speaking, I am not sure why that can solve the problem. Does that mean we should always use Theano way to represent the image shape regardless the backend.

Thanks a lot.

Huaiyang

EdwardTyantov commented 7 years ago

Hi. I don't know every aspect of the keras switching between backends. But almost every keras module has a dim_ordering parameter, which can easily be modified in the keras config. For example, in my environment I've created a file - ~/.keras/keras.json with the following content: { "image_dim_ordering": "th", "epsilon": 1e-07, "floatx": "float32", "backend": "theano" } You can substitute backend to "tensorflow" and keep image_dim_ordering as "th".

More information here

huaiyanggongzi commented 7 years ago

Thank you very much for the help! Happy New Year!