Layers in Keras
Layers in Keras.
- VGG16
- Loop over layers, layer name
- Get first 4 layers
- Grab layer after name
- Build a model based on VGG16
import tensorflow as tf
from tensorflow import keras
import numpy as np
vgg16 = tf.keras.applications.VGG16(include_top = False, weights = "imagenet",\
input_tensor = tf.keras.layers.Input(shape = (224,224,3)))
for layer in vgg16.layers:
print (layer.name, ": ", layer)
for idx in range(len(vgg16.layers)):
print(vgg16.get_layer(index = idx).name)
vgg16 = tf.keras.applications.VGG16(include_top = False, weights = "imagenet",\
input_tensor = tf.keras.layers.Input(shape = (224,224,3)))
print(vgg16.layers[1].input_shape)
# print output shape
print(vgg16.layers[1].output_shape)
# weight matrix
print(vgg16.layers[1].get_weights)
# layer name
print(vgg16.layers[1].name)
# input tensor
print(vgg16.layers[1].input)
# output tensor
print(vgg16.layers[1].output)
vgg16 = tf.keras.applications.VGG16(include_top = False, weights = "imagenet",\
input_tensor = tf.keras.layers.Input(shape = (224,224,3)))
vgg16.get_layer('block3_conv1').output
vgg16 = tf.keras.applications.VGG16(include_top = False, weights = "imagenet",\
input_tensor = tf.keras.layers.Input(shape = (224,224,3)))
model_output = vgg16.get_layer("block3_conv1").output
new_model = tf.keras.models.Model(inputs=vgg16.input, outputs=model_output)