文章目录
Visualkeras介绍
Visualkeras是一个Python包,用于帮助可视化Keras(独立或包含在tensorflow中)神经网络架构。它允许简单的造型来满足大多数需求。该模块支持分层风格的架构生成,这对CNN(卷积神经网络)非常有用。
下载安装
Visualkeras源代码链接:https://github.com/paulgavrikov/visualkeras
使用清华源安装Visualkeras
python
pip install visualkeras -i https://pypi.tuna.tsinghua.edu.cn/simple
代码示例
使用CNN经典网络VGG16作为示例,可视化神经网络结构。
1、导入必要的库
python
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D, InputLayer, ZeroPadding2D
from collections import defaultdict
import visualkeras
from PIL import ImageFont
2、创建VGG16神经网络模型
python
# create VGG16
image_size = 224
model = Sequential()
model.add(InputLayer(input_shape=(image_size, image_size, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(MaxPooling2D())
model.add(visualkeras.SpacingDummyLayer())
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'))
3、可视化神经网络结构
python
# Now visualize the model!
color_map = defaultdict(dict)
color_map[Conv2D]['fill'] = 'orange'
color_map[ZeroPadding2D]['fill'] = 'gray'
color_map[Dropout]['fill'] = 'pink'
color_map[MaxPooling2D]['fill'] = 'red'
color_map[Dense]['fill'] = 'green'
color_map[Flatten]['fill'] = 'teal'
font = ImageFont.truetype("./Arial.ttf", 32)
visualkeras.layered_view(model, to_file='./figures/vgg16.png', type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_legend.png', type_ignore=[visualkeras.SpacingDummyLayer],
legend=True, font=font)
visualkeras.layered_view(model, to_file='./figures/vgg16_spacing_layers.png', spacing=0)
visualkeras.layered_view(model, to_file='./figures/vgg16_type_ignore.png',
type_ignore=[ZeroPadding2D, Dropout, Flatten, visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_color_map.png',
color_map=color_map, type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_flat.png',
draw_volume=False, type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_scaling.png',
scale_xy=1, scale_z=1, max_z=1000, type_ignore=[visualkeras.SpacingDummyLayer])
4、完整代码
python
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D, InputLayer, ZeroPadding2D
from collections import defaultdict
import visualkeras
from PIL import ImageFont
# create VGG16
image_size = 224
model = Sequential()
model.add(InputLayer(input_shape=(image_size, image_size, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(visualkeras.SpacingDummyLayer())
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, activation='relu', kernel_size=(3, 3)))
model.add(MaxPooling2D())
model.add(visualkeras.SpacingDummyLayer())
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'))
# Now visualize the model!
color_map = defaultdict(dict)
color_map[Conv2D]['fill'] = 'orange'
color_map[ZeroPadding2D]['fill'] = 'gray'
color_map[Dropout]['fill'] = 'pink'
color_map[MaxPooling2D]['fill'] = 'red'
color_map[Dense]['fill'] = 'green'
color_map[Flatten]['fill'] = 'teal'
font = ImageFont.truetype("./Arial.ttf", 32)
visualkeras.layered_view(model, to_file='./figures/vgg16.png', type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_legend.png', type_ignore=[visualkeras.SpacingDummyLayer],
legend=True, font=font)
visualkeras.layered_view(model, to_file='./figures/vgg16_spacing_layers.png', spacing=0)
visualkeras.layered_view(model, to_file='./figures/vgg16_type_ignore.png',
type_ignore=[ZeroPadding2D, Dropout, Flatten, visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_color_map.png',
color_map=color_map, type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_flat.png',
draw_volume=False, type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='./figures/vgg16_scaling.png',
scale_xy=1, scale_z=1, max_z=1000, type_ignore=[visualkeras.SpacingDummyLayer])
5、使用教程
- 创建一个项目文件夹(例如:Project)
- 在创建的项目文件夹Project 中新建一个文件夹(文件夹名为 figures )
- 通过链接(https://ultralytics.com/assets/Arial.ttf)下载 Arial.ttf 字体文件
- 将下载的 Arial.ttf 字体文件 放在 项目文件夹Project 下
- 在 项目文件夹Project 下新建一个py文件(如:examples.py)
- 将上述的完整代码复制到 examples.py 中
- 运行examples.py
- 在 figures文件夹中查看生成的可视化图
- vgg16.png
- vgg16_legend.png
可视化自己创建的神经网络结构
1、导入要的库
python
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models,layers
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Flatten, Dense
from tensorflow.keras.callbacks import Callback, ModelCheckpoint
import visualkeras
2、创建自己的神经网络模型
将以下代码替换为自己的Keras / TensorFlow 神经网络结构。
python
model = models.Sequential()
# 第一层卷积层
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1))) # 假设输入图像大小为48x48,1为灰度图
model.add(layers.MaxPooling2D((2, 2)))
# 第二层卷积层
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
# 展平层
model.add(layers.Flatten())
# 全连接层
model.add(layers.Dense(64, activation='relu'))
# 输出层,假设分类任务有7个类别
model.add(layers.Dense(7, activation='softmax'))
3、可视化神经网络结构图
显示层风格图
python
visualkeras.layered_view(model).show() # 只显示图
# visualkeras.layered_view(model, to_file='output.png').show() # 保存和显示图
显示带有标签的层风格图
python
from PIL import ImageFont
font = ImageFont.truetype("./Arial.ttf", 32)
visualkeras.layered_view(model, legend=True, font=font).show() # 只显示图
# visualkeras.layered_view(model, to_file='output_legend.png', legend=True, font=font).show() # 保存和显示图
4、完整代码
python
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models,layers
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Flatten, Dense
from tensorflow.keras.callbacks import Callback, ModelCheckpoint
import visualkeras
# 可以将下面这部分创建模型的代码更换你自己的神经网络结构
model = models.Sequential()
# 第一层卷积层
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1))) # 假设输入图像大小为48x48,1为灰度图
model.add(layers.MaxPooling2D((2, 2)))
# 第二层卷积层
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
# 展平层
model.add(layers.Flatten())
# 全连接层
model.add(layers.Dense(64, activation='relu'))
# 输出层,假设分类任务有7个类别
model.add(layers.Dense(7, activation='softmax'))
visualkeras.layered_view(model).show() # 只显示图
# visualkeras.layered_view(model, to_file='output.png').show() # 保存和显示图
from PIL import ImageFont
font = ImageFont.truetype("./Arial.ttf", 32)
visualkeras.layered_view(model, legend=True, font=font).show() # 只显示图
# visualkeras.layered_view(model, to_file='output_legend.png', legend=True, font=font).show() # 保存和显示图