赞
踩
keras中的模型加载
from keras import backend as K from keras.models import load_model from PIL import Image import numpy as np source='/path/' def get_layer_output(model, x, index=-1): layer = K.function([model.input], [model.layers[index].output]) return layer([x])[0] #加载模型 model = load_model(source + 'h5/文件名.h5') #循环输出网络各层的参数 for i in range(len(model.layers)): print("i,model.layers[index].output:",i,model.layers[i].output) names = [weight.name for layer in model.layers for weight in layer.weights] #获取权重 weights = model.get_weights() #输出各层权重值的尺寸 for name, weight in zip(names, weights): print(name, weight.shape) #得到指定层的权重和偏置值 weight_dense_1,bias_dense_1 = model.get_layer('dense_1').get_weights() #输出权重和偏置值 print(weight_dense_1) print(bias_dense_1) #存储权重值到本地 np.save(source+"result/weight_dense_1.npy", weight_dense_1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。