Ubuntu下面的Keras可视化+权重维度获取-Netron的安装使用
环境:
Ubuntu |
19.10 |
Python
|
3.6.10 |
Keras | 2.3.1 |
#---------------------------------------------------------------------------------------------------------------------------------------
操作过程如下:
$ snap install netron
$ netron
Serving at http://localhost:8080
然后在浏览器打开http://localhost:8080
在Open Model上传自己的.h5模型即可。
#---------------------------------------------------------------------------------------------------------------------------------------
准备好数据集:
tsocks wget https://s3.amazonaws.com/img-datasets/mnist.npz
mv mnist.npz ~/.keras/datasets
#---------------------------------------------------------------------------------------------------------------------------------------
运行如下代码(来自[1]):
from keras.models import Model
from keras.layers import Input, Dense
from keras.datasets import mnist
from keras.utils import np_utils
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train=x_train.reshape(x_train.shape[0],-1)/255.0
x_test=x_test.reshape(x_test.shape[0],-1)/255.0
y_train=np_utils.to_categorical(y_train,num_classes=10)
y_test=np_utils.to_categorical(y_test,num_classes=10)
inputs = Input(shape=(784, ))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
y = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=y)
model.save('m1.h5')
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=10)
#loss,accuracy=model.evaluate(x_test,y_test)
model.save('m2.h5')
model.save_weights('m3.h5')
运行后我们得到m1,m2,m3三个文件
文件名 | 保存的函数 | 保存了图结构 | 保存了模型参数 | 可否用Neutron打开 |
m1.h5 | save() | √ | X | √ |
m2.h5 | save() | √ | √ | √ |
m3.h5 | save_weights() | X | √ | √ |
m1.h5打开结果
m2.h5打开结果
m3.h5打开结果
从上面可以看到权重的维度。
所以如[1]所说,没啥事儿的话,尽量使用save()而不是save_weights()
#---------------------------------------------------------------------------------------------------------------------------------------
Reference:
[1]keras保存模型中的save()和save_weights()
下一篇: MySQL复制表结构和表数据