模型可视化
import tensorflow as tf
from tensorflow import keras
from keras import models
from keras.preprocessing import image
import numpy as np
import os
img_path=r"C:\Users\bxzyz\Desktop\doc\ocr\data\orc-data\annotations\charactor-008"
img_name=r"charactor-008_00223.bmp"
img_path=r"C:\Users\bxzyz\Desktop\doc\ocr\data\orc-data\annotations\charactor-009"
img_name=r"charactor-009_00054.bmp"
imgpath=os.path.join(img_path,img_name)
model = keras.models.load_model('D:\mydoc\ML\ocr2_IC_model_all.h5')
model.summary()#输出网络结构
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_3 (Conv2D) (None, 73, 33, 32) 896
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 36, 16, 32) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 34, 14, 64) 18496
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 17, 7, 64) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 16, 6, 128) 32896
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 8, 3, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 3072) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 786688
_________________________________________________________________
dense_2 (Dense) (None, 62) 15934
=================================================================
Total params: 854,910
Trainable params: 854,910
Non-trainable params: 0
_________________________________________________________________
from tensorflow.keras.utils import plot_model
plot_model(model,to_file="E:\imagedata\model.png")#to_file后面是保存网络结构图的路径和文件名
output_3_0.png
img=image.load_img(imgpath,target_size=(75,45))#使用keras的读图函数读取图像
img_tensor=image.img_to_array(img)
img_tensor=np.expand_dims(img_tensor,axis=0)
print(img_tensor.shape)
(1, 75, 45, 3)
import matplotlib.pyplot as plt
plt.imshow(np.array(img_tensor[0],np.uint8))
plt.show()#显示原图
output_5_0.png
#提取网络前四层
from keras import models
layer_outputs=[layer.output for layer in model.layers[:4]]#提取前4层网络
activation_model=tf.keras.models.Model(inputs=model.input,outputs=layer_outputs)
activations=activation_model.predict(img_tensor)
WARNING:tensorflow:AutoGraph could not transform <function Model.make_predict_function.<locals>.predict_function at 0x000001EC40D603A8> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING: AutoGraph could not transform <function Model.make_predict_function.<locals>.predict_function at 0x000001EC40D603A8> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING:tensorflow:Model was constructed with shape (None, 75, 35, 3) for input Tensor("input_4_2:0", shape=(None, 75, 35, 3), dtype=float32), but it was called on an input with incompatible shape (None, 75, 45, 3).
WARNING:tensorflow:5 out of the last 5 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001EC40D603A8> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.
model.input
<tf.Tensor 'input_4_2:0' shape=(None, 75, 35, 3) dtype=float32>
#显示第一层激活输出的图像
first_layer_activation=activations[0]
plt.figure(figsize=(20, 20))
for i in range(32):
plt.subplot(6,6,i+1)
plt.imshow(first_layer_activation[0,:,:,i]/255)#i表示特征通道,conv2D(32,。。。),32就是特征通道数
plt.show()
output_8_0.png
#显示各层激活输出的图像
num=0
for layer_output in activations:
num+=1
plt.figure(num)
plt.figure(figsize=(20, 20))
for i in range(layer_output.shape[-1]):
#layer_output的形状是(1, 73, 43, 32),最后一个表示特征通道数,feature maps
plt.subplot(8,8,i+1)
plt.imshow(layer_output[0,:,:,i])
plt.show()
<Figure size 432x288 with 0 Axes>
output_9_1.png
<Figure size 432x288 with 0 Axes>
output_9_3.png
<Figure size 432x288 with 0 Axes>
output_9_5.png
<Figure size 432x288 with 0 Axes>
output_9_7.png
activations[0].shape
(1, 73, 43, 32)
网友评论