Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

python - VGG16 with other input shape and Imagenet weights

I'm new to models like VGG16. I've been searching information about this model and I still have doubts about it. I have 10000 images of different sizes to train the model (2 classes), so I decided to use an image size of 86x86 because of computational limitations and it's near average of every image size. So I did that:

base_model16 = VGG16(weights='imagenet', include_top=False, input_shape=(86,86,3)) 

And for generators:

datagen = ImageDataGenerator(preprocessing_function=preprocess_vgg16) 

train_generator = datagen.flow_from_directory(path_train,
                                                    target_size=(86,86),
                                                    color_mode='rgb',
                                                    batch_size = 128,
                                                    class_mode='categorical',
                                                    shuffle=True) 

I read that VGG16 was trained with 224x224 and I understood that we can use other size, but can someone confirm if i am doing it right? Because i am using imagenet weights and preprocess_vgg16 and it was with 224x224. Sorry if anyone has already asked this question before but i need help understanding it please.

Thank you.

question from:https://stackoverflow.com/questions/65851403/vgg16-with-other-input-shape-and-imagenet-weights

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have to modify the Vgg model because it is designed to classify 1000 images. setting include_top=False removes the top layer of the model which had 1000 neurons. Now we need to include a layer which will have 2 neurons in it. The code below will accomplish that. Note in the parameters of the VGG model I have set pooling='max'. This results in the output of the Vgg model to be a vector that can be used as input to a dense layer.

base_model=tf.keras.applications.VGG16( include_top=False, input_shape=(86,86,3), 
                                        pooling='max', weights='imagenet' ) 
x=base_model.output
output=Dense(2, activation='softmax')(x)
model=Model(inputs=base_model.input, outputs=output)
model.compile(Adam(lr=.001), loss='categorical_crossentropy', metrics=['accuracy') 

As an aside I do not like to use VGG16. It has about 40 million traninable parameters so it is computationally expense resulting in long training time. I prefer to use the MobileNet model which only has about 4 million trainable parameters and is about as accurate. To use the MobileNet model just use this line of code instead of the code for the Vgg model. Note I set the image_shape to (128,128,3) because there is a version of the mobilenet weights trained on imagenet with 128 X 128 images that will download automatically and help the model converge faster. But you can use 86 X86 if you choose. So in your train_generator set target_size=(128,128). Also in the ImageDataGenerator the code preprocessing_function=preprocess_vgg16 should still work for the Mobilenet model because I think it is the same as keras.applications.mobilenet.preprocess_input. I believe both of them just rescale the pixels to be between -1 and +1.

base_model=tf.keras.applications.mobilenet.MobileNet( include_top=False, 
           input_shape=(128,128,3), pooling='max', weights='imagenet',dropout=.4)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...