keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 913 forks source link

For what reason I need to use a specific input shape when loading imagenet weights, even though any shape works fine when transferring? #122

Closed DableUTeeF closed 5 years ago

DableUTeeF commented 5 years ago

I just want to train MobileNet with larger images but I have to put an Input tensor to the function otherwise it will stuck in this line.

https://github.com/keras-team/keras-applications/blob/master/keras_applications/imagenet_utils.py#L292

I believe this line is really unneeded.

Is this against the rule or should I use Pull request instead?

taehoonlee commented 5 years ago

@DableUTeeF, You must designate the input_shape as (rows, cols, channels), thus the line is necessary.

DableUTeeF commented 5 years ago

Why is it necessary? Of cource you need to specific the shape but it's not has to be (224, 224, 3) right? Even (416, 416, 3) works just fine but that line make this errer!!

On Fri, Jul 5, 2019, 12:07 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF, You must designate the input_shape as (rows, cols, channels), thus the line is necessary.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R763QTS6FEUTE27W2YGDP53JIDA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZIRMUY#issuecomment-508630611, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R763O2QQFGJEVSOFNXJDP53JIDANCNFSM4H4WXDLQ .

taehoonlee commented 5 years ago

@DableUTeeF, the following example works fine without an error.

import numpy as np
from keras.applications.mobilenet import MobileNet
model = MobileNet(include_top=False, input_shape=(416, 416, 3))
print(model.predict(np.random.random((1, 416, 416, 3))).shape)
model2 = MobileNet(weights=None, input_shape=(416, 416, 3))
print(model2.predict(np.random.random((1, 416, 416, 3))).shape)
DableUTeeF commented 5 years ago

Indeed But for some reasons this raise error in Colab.

model = tf.keras.applications.nasnet.NASNetLarge(
      include_top=False,
      weights='imagenet',
      input_tensor=None,
      input_shape=(224, 224, 3),
      pooling=None)

On Fri, Jul 5, 2019, 12:50 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF, the following example works fine without an error.

import numpy as npfrom keras.applications.mobilenet import MobileNet model = MobileNet(include_top=False, input_shape=(416, 416, 3))print(model.predict(np.random.random((1, 416, 416, 3))).shape) model2 = MobileNet(weights=None, input_shape=(416, 416, 3))print(model2.predict(np.random.random((1, 416, 416, 3))).shape)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R767RRA2RLUZC53IBRBDP53OLNA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZITGSI#issuecomment-508638025, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R76YCYHNIOVHTQBQT7P3P53OLNANCNFSM4H4WXDLQ .

DableUTeeF commented 5 years ago

Well, I tried it again and the MoblieNet works. But NasNet raise this

/usr/local/lib/python3.6/dist-packages/keras_applications/imagenet_utils.py in _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights)
    290                                  'and loading `imagenet` weights, '
    291                                  '`input_shape` should be ' +
--> 292                                  str(default_shape) + '.')
    293         return default_shape
    294     if input_shape:

ValueError: When setting `include_top=True` and loading `imagenet` weights, `input_shape` should be (331, 331, 3).
taehoonlee commented 5 years ago

@DableUTeeF, the ValueError of NASNet is mandatory in order to reproduce the original results. If you want to train from scratch, you can put weights=None as an argument.

DableUTeeF commented 5 years ago

Of cource not!! I don't mean to train it from scratch so that's why I put the weights='imagenet' no? The results are far better when transferring even if you change the size, that's why I've said this should not raise error.

On Fri, Jul 5, 2019, 13:57 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF, the ValueError of NASNet is mandatory in order to reproduce the original results. If you want to train from scratch, you can put weights=None as an argument.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R76Y5I2KBANBTYSZCFG3P53WGXA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZIWYUQ#issuecomment-508652626, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R762ORE4ZWAMASV24MY3P53WGXANCNFSM4H4WXDLQ .

DableUTeeF commented 5 years ago

Well, suppose the MobileNet does work but the NASNet doesn't, it might be something from the NASNet code rather than the utils.

On Fri, Jul 5, 2019, 14:01 ณัฐพร หงษ์เจริญ palm22180@gmail.com wrote:

Of cource not!! I don't mean to train it from scratch so that's why I put the weights='imagenet' no? The results are far better when transferring even if you change the size, that's why I've said this should not raise error.

On Fri, Jul 5, 2019, 13:57 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF, the ValueError of NASNet is mandatory in order to reproduce the original results. If you want to train from scratch, you can put weights=None as an argument.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R76Y5I2KBANBTYSZCFG3P53WGXA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZIWYUQ#issuecomment-508652626, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R762ORE4ZWAMASV24MY3P53WGXANCNFSM4H4WXDLQ .

taehoonlee commented 5 years ago

@DableUTeeF,

  1. When include_top=True, both the following models must raise ValueError.

    MobileNet(input_shape=(416, 416, 3))
    NASNetLarge(input_shape=(416, 416, 3))
  2. When include_top=False, only the NASNet must raise ValueError. It is intended behavior because this line causes structural changes. In other words, the NASNet with include_top=any, input_shape=(None, None, 3) may differ from the intended structure of the authors.

    NASNetLarge(include_top=False, input_shape=(416, 416, 3))
DableUTeeF commented 5 years ago

I know that!! Please look at my answer above again (I don't have a PC around), I clearly specific include_top=False in the NASNetLarge(). That's should means the Nasnet code sends a True param even though I put it False.

On Sat, Jul 6, 2019, 13:06 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF,

  1. When include_top=True, both the following models must raise ValueError.

MobileNet(input_shape=(416, 416, 3)) NASNetLarge(input_shape=(416, 416, 3))

  1. When include_top=False, only the NASNet must raise ValueError. It is intended behavior because this line https://github.com/keras-team/keras-applications/blob/master/keras_applications/nasnet.py#L513 causes structural changes. In other words, the NASNet with include_top=any, input_shape=(None, None, 3) may differ from the intended structure of the authors.

NASNetLarge(include_top=False, input_shape=(416, 416, 3))

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R762XNNR5PAPPQZKHQGDP6AY6LA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZKTH5I#issuecomment-508900341, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R765FQ3XGNVRDPIBCZEDP6AY6LANCNFSM4H4WXDLQ .

taehoonlee commented 5 years ago

@DableUTeeF, the ValueError is intended. And the error message can be misleading. Do you want to revise the error message?

DableUTeeF commented 5 years ago

It's not about the message. I mean, it's raise error when it should not. 1.) I put a specific include_top=False but the error said include_top is True, this is a bug right?. And if include_top is False then whatever input size I use shouldn't be any problem right? In fact, this does work in MobileNet. Well, my question at first misleading this. But this same params raise Valueerror when I change it to NasNetLarge. 2.) Despite the 1.) error, if I change the weights to None and then manually load the weight in .keras, it works!! Which means this is difinitely a bug in NasNet code.

On Sat, Jul 6, 2019, 13:16 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF, the ValueError is intended. And the error message can be misleading. Do you want to revise the error message?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R764ML3QVRDZCXWOOK3DP6A2FPA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZKTLZY#issuecomment-508900839, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R763FDSIIHLUWZO22PDDP6A2FPANCNFSM4H4WXDLQ .

DableUTeeF commented 5 years ago

The main problem now is "Why it's only error when I use NASNet.

On Sat, Jul 6, 2019, 13:26 ณัฐพร หงษ์เจริญ palm22180@gmail.com wrote:

It's not about the message. I mean, it's raise error when it should not. 1.) I put a specific include_top=False but the error said include_top is True, this is a bug right?. And if include_top is False then whatever input size I use shouldn't be any problem right? In fact, this does work in MobileNet. Well, my question at first misleading this. But this same params raise Valueerror when I change it to NasNetLarge. 2.) Despite the 1.) error, if I change the weights to None and then manually load the weight in .keras, it works!! Which means this is difinitely a bug in NasNet code.

On Sat, Jul 6, 2019, 13:16 Taehoon Lee notifications@github.com wrote:

@DableUTeeF https://github.com/DableUTeeF, the ValueError is intended. And the error message can be misleading. Do you want to revise the error message?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras-applications/issues/122?email_source=notifications&email_token=AE4R764ML3QVRDZCXWOOK3DP6A2FPA5CNFSM4H4WXDL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZKTLZY#issuecomment-508900839, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4R763FDSIIHLUWZO22PDDP6A2FPANCNFSM4H4WXDLQ .

taehoonlee commented 5 years ago

@DableUTeeF, That is not a bug. It is intended behavior because this line causes structural changes. In other words, the NASNet with include_top=any, input_shape=(None, None, 3) may differ from the intended structure of the authors.

DableUTeeF commented 5 years ago

@taehoonlee As I said, I can just create model with weight=None and load the "official Imagenet weights" without any issue. This code works in colab.

import tensorflow as tf
model = tf.keras.applications.nasnet.NASNetLarge(
      include_top=False,
      weights=None,
      input_tensor=None,
      input_shape=(416, 416, 3),
      pooling=None)
model.load_weights('/root/.keras/models/nasnet_large_no_top.h5')

And as expected, the result is much better than train everything from scratch. So even the structural is changed, it doesn't cause any problem right?

taehoonlee commented 5 years ago

@DableUTeeF, I figured out that the structural changes have disappeared since the 1.0.6 of keras-applications. Thus, now we can enable NASNet(include_top=False) with arbitrary input shapes. Finally you made the point clear :) Thank you

hoangphucITJP commented 4 years ago

I want to train EfficientNet on the grayscale images. But the 3 channels restriction makes me have to convert grayscale to RGB which is redundant.

Is there any way to train on 1-channel images with imagenet pretrained?