4

When we do transfer learning in Keras2., the Arguments require "input_shape" and "input_tensor". But I use only input_tensor and haven never used input_shape. I think only input_tensor is enough, and I don't know when to use input_shape. How should I use them separately?

I used input_tensor and input_shape simultaneously with separate value, and only value of input_tensor was adopted and input_shape was ignored.

vgg16_model = VGG16(include_top=False, weights='imagenet', 
                    input_tensor = Input(shape=(150, 150, 3)), 
                    input_shape=(224,224,3))

top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dense(1, activation='sigmoid'))
model = Model(input=vgg16_model.input, output=top_model(vgg16_model.output))

model.summary()
Layer (type)                 Output Shape              Param #   
================================================================
input_6 (InputLayer)         (None, 150, 150, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 150, 150, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 150, 150, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 75, 75, 64)        0         
_________________________________________________________________
block2_conv......

I expected I get some errors in this code, but there was no error, and this model could accept the shape of (150, 150, 3). Input_shape=(224,224,3) was ignored.

Can you maybe give me a little help ? Thanks.

today
  • 32,602
  • 8
  • 95
  • 115
Kai.K
  • 53
  • 1
  • 4
  • Both are optional arguments when the fully connected classification layers aren't used. These models can accept variable sized inputs, but will then deliver variable sized outputs – JimmyOnThePage Jul 05 '19 at 07:16
  • Thanks! but I still have a question. I added details of code. When must I use input_shape instead of input_tensor? – Kai.K Jul 05 '19 at 07:59

2 Answers2

6

The VGG16 code probably simply forgot to check for the two arguments.

It doesn't make sense to have both, of course.

  • You use input_shape when you want the model to create its own input layer automatically with that size.
  • You use input_tensor when you have a tensor that you want to be the input.

You can use any tensor in input_tensor, this is meant to use the outputs of other models/layers as the input of the VGG16. Of course that you can pass a dummy input tensor as you did, there is no reason for the code to complain, it received a tensor, ok.

The only thing there is that the coder forgot to verify "if both arguments exist, thrown an error".

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • 1
    Thank you so much!, and I found the way to connect other models/layers. https://github.com/keras-team/keras/pull/8999 Keras has some bugs about it. – Kai.K Jul 09 '19 at 01:18
2

Actually, when you set the input_tensor argument, the given tensor (assuming it is a Keras tensor) will be used for the input and therefore the input_shape argument would be ignored. Here is the relevant section in keras-applications source code:

if input_tensor is None:
    img_input = layers.Input(shape=input_shape)
else:
    if not backend.is_keras_tensor(input_tensor):
        img_input = layers.Input(tensor=input_tensor, shape=input_shape)
    else:
        img_input = input_tensor

As you can see, in the last line the given input_tensor will be used for the input tensor without considering the input_shape.

today
  • 32,602
  • 8
  • 95
  • 115
  • For the fits if statement of the else, I'm not sure what the logic means. If the input_tensor isn't a keras_tensor, then we want the placeholder of the tensor to be equal to the input tensor? Why is this the case? Thanks. – IntegrateThis Aug 14 '20 at 20:55
  • 2
    @IntegrateThis If it's a Keras tensor, then it means it is connected to a layer/model and from there it can take values; therefore, using it as it is would be fine. However, if it's not a Keras tensor it should be wrapped in a `Input` layer so that it could be fed (i.e. take values) using a `placeholder` (which is the underlying structure of `Input` layer). – today Aug 15 '20 at 05:59