keras-team / keras-applications

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

Pretrained VGG has multi input sizes? #142

Open cuongth95 opened 4 years ago

cuongth95 commented 4 years ago
pretrain_vgg = tf.keras.applications.vgg16.VGG16(include_top=False,
                                            weights='imagenet',
                                            input_tensor=None,
                                            input_shape=(224,224,3),#<---attention
                                            pooling=None,
                                            classes=6)
#create image
test_img = tf.random.uniform((112,112,3))#112,112,3
fake_batch = tf.expand_dims(test_img, 0)#1,112,112,3

#create dummy model
#declare input with different shape
test_inp = tf.keras.Input(shape=(112,112,3),name="input_1")

#build new model
x = pretrain_vgg(test_inp) 
fake_model = tf.keras.models.Model(inputs=test_inp,outputs=x)
fake_model.summary()
fake_model.predict(fake_batch) # no error

#multi input shapes?
for layer in pretrain_vgg.layers:
    print(layer.get_input_shape_at(0)) 

Hi everyone, I am looking for the pre-trained VGG16 and try some experiments. Some mistakes in the input shapes were found, but the model still runs unexpectedly. Can you guys explain more how VGG still works in the above case? In my opinion, there are several Input layers in pre-trained VGG16 model, but, after convolutions, how they can fit perfectly without any error in the last layers???

Kind regards, Huy