以下代码在jupyter notebook中完成,使用的keras+tensorflow环境。
% matplotlib inline
import matplotlib.pyplot as plt
from keras.models import Model,Sequential
import cv2, numpy as np
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from skimage import io
from keras.optimizers import SGD
io.use_plugin("pil", "imread")
test_im=io.imread('D:/000036.jpg') # define a test image
test_im.shape
(500, 332, 3)
# pre-process the test image
test_im=test_im/255
im = cv2.resize(test_im, (224, 224)).astype(np.float32)
test_im=im.reshape(1,224,224,3)
# define model
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224,3)))
model.add(Conv2D(64,(3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64,(3,3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128,(3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128,(3,3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256,(3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256,(3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256,(3,3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3,3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3,3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3,3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
# load weight
model.load_weights("D:/detection/vgg16_weights.h5",by_name=True)
# compile model
sdg = SGD(lr = 0.01, decay = 0.0 ,momentum = 0.9, nesterov = True)
model.compile(optimizer = sdg, loss = "categorical_crossentropy", metrics=["accuracy"])
# to gain the output of intermediate layer, we can 'create' a new model
# the intermediate_output is the final output you want
layer_index=5 # the index of the layer you want to observe
intermediate_layer_model=Model(inputs=model.input, outputs=model.get_layer(index=layer_index).output)
intermediate_output = intermediate_layer_model.predict(test_im)
intermediate_output.shape
(1, 112, 112, 64)
layer_output = intermediate_output[0]
plt.imshow(layer_output[:,:,0],cmap='jet')
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
zero_padding2d_27 (ZeroPaddi (None, 226, 226, 3) 0
_________________________________________________________________
conv2d_27 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
zero_padding2d_28 (ZeroPaddi (None, 226, 226, 64) 0
_________________________________________________________________
conv2d_28 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
max_pooling2d_11 (MaxPooling (None, 112, 112, 64) 0
_________________________________________________________________
zero_padding2d_29 (ZeroPaddi (None, 114, 114, 64) 0
_________________________________________________________________
conv2d_29 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
zero_padding2d_30 (ZeroPaddi (None, 114, 114, 128) 0
_________________________________________________________________
conv2d_30 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 56, 56, 128) 0
_________________________________________________________________
zero_padding2d_31 (ZeroPaddi (None, 58, 58, 128) 0
_________________________________________________________________
conv2d_31 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
zero_padding2d_32 (ZeroPaddi (None, 58, 58, 256) 0
_________________________________________________________________
conv2d_32 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
zero_padding2d_33 (ZeroPaddi (None, 58, 58, 256) 0
_________________________________________________________________
conv2d_33 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 28, 28, 256) 0
_________________________________________________________________
zero_padding2d_34 (ZeroPaddi (None, 30, 30, 256) 0
_________________________________________________________________
conv2d_34 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
zero_padding2d_35 (ZeroPaddi (None, 30, 30, 512) 0
_________________________________________________________________
conv2d_35 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
zero_padding2d_36 (ZeroPaddi (None, 30, 30, 512) 0
_________________________________________________________________
conv2d_36 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 14, 14, 512) 0
_________________________________________________________________
zero_padding2d_37 (ZeroPaddi (None, 16, 16, 512) 0
_________________________________________________________________
conv2d_37 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
zero_padding2d_38 (ZeroPaddi (None, 16, 16, 512) 0
_________________________________________________________________
conv2d_38 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
zero_padding2d_39 (ZeroPaddi (None, 16, 16, 512) 0
_________________________________________________________________
conv2d_39 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 7, 7, 512) 0
_________________________________________________________________
flatten_3 (Flatten) (None, 25088) 0
_________________________________________________________________
dense_7 (Dense) (None, 4096) 102764544
_________________________________________________________________
dropout_5 (Dropout) (None, 4096) 0
_________________________________________________________________
dense_8 (Dense) (None, 4096) 16781312
_________________________________________________________________
dropout_6 (Dropout) (None, 4096) 0
_________________________________________________________________
dense_9 (Dense) (None, 1000) 4097000
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
intermediate_layer_model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
zero_padding2d_27_input (Inp (None, 224, 224, 3) 0
_________________________________________________________________
zero_padding2d_27 (ZeroPaddi (None, 226, 226, 3) 0
_________________________________________________________________
conv2d_27 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
zero_padding2d_28 (ZeroPaddi (None, 226, 226, 64) 0
_________________________________________________________________
conv2d_28 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
max_pooling2d_11 (MaxPooling (None, 112, 112, 64) 0
=================================================================
Total params: 38,720
Trainable params: 38,720
Non-trainable params: 0
_________________________________________________________________
# plot the intermediate output- feature map
fig = plt.figure(figsize=(8, 8))
fig.subplots_adjust(
left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(64):
ax = fig.add_subplot(8, 8, i + 1, xticks=[], yticks=[])
ax.imshow(layer_output[:,:,i], cmap='jet')
plt.show()
# gain all the weights of the model
c = model.get_weights()
len(c)
32
for i in range(32):
print(c[i].shape)
(3, 3, 3, 64)
(64,)
(3, 3, 64, 64)
(64,)
(3, 3, 64, 128)
(128,)
(3, 3, 128, 128)
(128,)
(3, 3, 128, 256)
(256,)
(3, 3, 256, 256)
(256,)
(3, 3, 256, 256)
(256,)
(3, 3, 256, 512)
(512,)
(3, 3, 512, 512)
(512,)
(3, 3, 512, 512)
(512,)
(3, 3, 512, 512)
(512,)
(3, 3, 512, 512)
(512,)
(3, 3, 512, 512)
(512,)
(25088, 4096)
(4096,)
(4096, 4096)
(4096,)
(4096, 1000)
(1000,)
# plot weights of the first layer
for k in range(3):
fig = plt.figure(figsize=(8, 8))
fig.subplots_adjust(
left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(64):
ax = fig.add_subplot(8,8, i + 1, xticks=[], yticks=[])
ax.imshow(c[0][:,:,k,i], cmap='gray')
plt.show()