Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Last active December 8, 2021 20:32
Show Gist options
  • Save hsleonis/84a73414c67f53472c5be59e5700ba95 to your computer and use it in GitHub Desktop.
Save hsleonis/84a73414c67f53472c5be59e5700ba95 to your computer and use it in GitHub Desktop.
VGG Neural Network
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
import numpy as np
# image
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# Extract features with VGG16
model = VGG16(weights='imagenet', include_top=False)
features = model.predict(x)
# Extract features from an arbitrary intermediate layer with VGG19
base_model = VGG19(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment