首先加载相关模块
import requests
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.utils import img_to_array, get_file
from tensorflow.keras.preprocessing.image import load_img
from skimage.transform import resize
加载标签
LABELS_PATH = './labels.txt'
with open(LABELS_PATH, 'rt') as f:
labels = [label[:-1] for label in f.readlines()]
print('Num of Labels: %s' % len(labels))
print('Labels Sample: ', labels[:5])
Num of Labels: 1000
Labels Sample: ['tench, Tinca tinca', 'goldfish, Carassius auratus', 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias', 'tiger shark, Galeocerdo cuvieri', 'hammerhead, hammerhead shark']
预处理
INPUT_SHAPE = (224, 224, 3)
def load_and_preprocess(image_path):
img = load_img(image_path)
img = img_to_array(img)
img = resize(img, INPUT_SHAPE)
img = preprocess_input(img)
return img
加载训练好的模型
vgg = VGG16(input_shape=INPUT_SHAPE, weights='imagenet', include_top=True)
vgg.summary()
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 224, 224, 3)] 0
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
flatten (Flatten) (None, 25088) 0
fc1 (Dense) (None, 4096) 102764544
fc2 (Dense) (None, 4096) 16781312
predictions (Dense) (None, 1000) 4097000
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
定义相关可视化函数
SELECT_LAYERS = [1, 3, 5, 7, 8, 10]
def predict(x):
X = np.expand_dims(x, axis=0)
y_preds = vgg.predict(X, verbose=False)
idx = np.argmax(y_preds[0])
return labels[idx]
def get_layers_output(img):
label = predict(img)
print('Image predicted to be: %s' % label)
input_layer = vgg.layers[0]
x = input_layer(np.expand_dims(img, axis=0))
outputs = [x[0]]
for layer in vgg.layers[1:]:
x = layer(x)
outputs.append(x[0])
outputs = np.array(outputs)
return outputs[SELECT_LAYERS]
def plot_layer_output(feature_maps, layer_idx, first=3):
fig = plt.figure(figsize=(first * 3, 4))
fig.suptitle('Layer %s' % layer_idx)
for idx in range(first):
sub_img = feature_maps[:, : , idx]
ax = plt.subplot(1, first, idx + 1)
ax.imshow(sub_img)
ax.set_title(feature_maps.shape)
ax.axis('off')
plt.show()
def plot_feature_maps(layers_output):
for idx, output in zip(SELECT_LAYERS, layers_output):
plot_layer_output(output, idx, first=10)
def plot_original_img(path):
img = load_img(path)
img = img_to_array(img)
plt.figure(figsize=(6, 9))
plt.title(path.split('.')[-2])
plt.imshow(img / 255.0)
plt.axis('off')
plt.show()
在已知类别上测试VGG
def get_inside(path):
img = load_and_preprocess(path)
plot_original_img(path)
# Get feature maps
outputs = get_layers_output(img)
plot_feature_maps(outputs)
get_inside('kite.jpg')
mbd.pub/o/GeBENHAGEN
擅长现代信号处理(改进小波分析系列,改进变分模态分解,改进经验小波变换,改进辛几何模态分解等等),改进机器学习,改进深度学习,机械故障诊断,改进时间序列分析(金融信号,心电信号,振动信号等)