Last active
April 11, 2018 15:42
-
-
Save nhannguyen95/b1edad19bf9942f86211e9ac1b7b7733 to your computer and use it in GitHub Desktop.
Custom VGG Keras pre-trained, ref: https://stackoverflow.com/a/45389215/3852032
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Model | |
from keras.applications.vgg16 import VGG16 | |
from keras.layers import GlobalAveragePooling2D | |
# Wait for downloading for the 1st time, | |
# the weights is saved in ~/.keras/models | |
vgg16 = VGG16(include_top=False, # don't include 3 fully connected layers | |
weights='imagenet', # use pre-trained weights, not random initialization | |
pooling=None) # don't apply any pooling at the last convolutional layer | |
# Continue building your own model | |
cst = nn.get_layer('block2_pool').output | |
cst = GlobalAveragePooling2D()(cst) | |
custom_vgg = Model(inputs=vgg16.input, outputs=cst) | |
# Want to infer some image? | |
# x = read_some_image, RGB, [0, 255], uint8 | |
x = x.astype('float') | |
x = x.reshape(1, x.shape[0], x.shape[1], x.shape[2]) # to 4D tensor | |
x = preprocess_input(x) | |
y = custom_vgg.predict(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment