当前位置: 首页 > news >正文

政法网站内容建设排名优化公司哪家好

政法网站内容建设,排名优化公司哪家好,在线ps,网站建设专业知识知识要点 keras 保存成hdf5文件, 1.保存模型和参数, 2.只保存参数 1.保存模型和参数 save_modelcallback ModelCheckpoint2. 只保存参数 save_weightscallback ModelCheckpoint save_weights_only True 保存模型: 案例数据: Fashion-MNIST总共有十个类别的图像model.save_w…

知识要点

keras 保存成hdf5文件, 1.保存模型和参数, 2.只保存参数

  • 1.保存模型和参数
    • save_model
    • callback ModelCheckpoint
  • 2. 只保存参数
    • save_weights
    • callback ModelCheckpoint save_weights_only = True

保存模型:

  • 案例数据: Fashion-MNIST总共有十个类别的图像
  • model.save_weights(os.path.join(logdir, 'fashion_mnist_weights_2.h5'))      # 保存参数的方法
  • 加载参数: model.load_weights(os.path.join(logdir, 'fashion_mnist_weight.h5'))
  • 保存模型: model.save(os.path.join(logdir, 'fashion_mnist_model.h5'))
  • 加载模型: model2 = keras.models.load_model(os.path.join(logdir, 'fashion_mnist_model.h5'))
  • 把keras模型保存成savedmodel格式: tf.saved_model.save(model, './keras_saved_model')


一 模型保存和部署

  • TFLite是为了将深度学习模型部署在移动端和嵌入式设备的工具包,可以把训练好的TF模型通过转化、部署和优化三个步骤,达到提升运算速度,减少内存、显存占用的效果。
  • TFlite主要由Converter和Interpreter组成。Converter负责把TensorFlow训练好的模型转化,并输出为.tflite文件(FlatBuffer格式)。转化的同时,还完成了对网络的优化,如量化。Interpreter则负责把.tflite部署到移动端,嵌入式(embedded linux device)和microcontroller,并高效地执行推理过程,同时提供API接口给Python,Objective-C,Swift,Java等多种语言。简单来说,Converter负责打包优化模型,Interpreter负责高效易用地执行推理
  • Fashion-MNIST总共有十个类别的图像。每一个类别由训练数据集6000张图像和测试数据集1000张图像。所以训练集和测试集分别包含60000张和10000张。测试训练集用于评估模型的性能。

  • 每一个输入图像的高度和宽度均为28像素。数据集由灰度图像组成。Fashion-MNIST,中包含十个类别,分别是t-shirt,trouser,pillover,dress,coat,sandal,shirt,sneaker,bag,ankle boot。

1.1 模型创建

  • 导包
# 导包
from tensorflow import keras
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
  • 时尚数据导入
# 时尚数据导入
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
  • 标准化
# 标准化
from sklearn.preprocessing import StandardScaler    # preprocessing 预处理
scaler = StandardScaler()x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 784))
x_valid_scaled = scaler.fit_transform(x_valid.astype(np.float32).reshape(-1, 784))
x_test_scaled = scaler.fit_transform(x_test.astype(np.float32).reshape(-1, 784))
  • 创建模型
# 创建模型
model = keras.models.Sequential([keras.layers.Dense(512, activation = 'relu', input_shape = (784, )),keras.layers.Dense(256, activation = 'relu'),keras.layers.Dense(128, activation = 'relu'),keras.layers.Dense(10, activation = 'softmax')])model.compile(loss = 'sparse_categorical_crossentropy',optimizer = 'adam',metrics = ['accuracy'])

1.2 保存模型

# 保存模型
import os
logdir = './graph_def_and_weights'
if not os.path.exists(logdir):os.mkdir(logdir)output_model_file = os.path.join(logdir, 'fashion_mnist_weight.h5')
callbacks = [keras.callbacks.TensorBoard(logdir),  # 保存地址# 保存效果最好的模型: save_best_onlykeras.callbacks.ModelCheckpoint(output_model_file,    save_best_only = True, save_weights_only = True),keras.callbacks.EarlyStopping(patience = 5, min_delta = 1e-3)]   history = model.fit(x_train_scaled, y_train, epochs = 10,validation_data= (x_valid_scaled, y_valid),callbacks = callbacks)

  • 保存模型
# 保存模型
output_model_file2 = os.path.join(logdir, 'fashion_mnist_model.h5')
model.save(output_model_file2)
  •  保存参数

# 另一种保存参数的方法
model.save_weights(os.path.join(logdir, 'fashion_mnist_weights_2.h5'))
  • 模型评估
# evaluate 评估
model.evaluate(x_valid_scaled, y_valid)   # [0.35909169912338257, 0.88919997215271]
  • 模型加载
# 加载模型
model2 = keras.models.load_model(output_model_file2)
model2.evaluate(x_valid_scaled, y_valid)  # [0.35909169912338257, 0.88919997215271]

二 保存模型为savemodel格式

# 把keras模型保存成savedmodel格式
tf.saved_model.save(model, './keras_saved_model')
  •  读取模型
# 加载savedmodel模型
loaded_saved_model = tf.saved_model.load('./keras_saved_model')
loaded_saved_model

 2.1 另一种保存

# 保存模型
import os
logdir = './graph_def_and_weights'
if not os.path.exists(logdir):os.mkdir(logdir)output_model_file = os.path.join(logdir, 'fashion_mnist_weight.h5')
model.load_weights(output_model_file)

三 tflite_interpreter 的使用

  • 导包
from tensorflow import keras
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import os
with open('./tflite_models/concrete_func_tf_lite', 'rb') as f:concrete_func_tflite = f.read()
  • 创建interpreter
# 创建interpreter
interpreter = tf.lite.Interpreter(model_content = concrete_func_tflite)
# 分配内存
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
  • 预测数值
input_data = tf.constant(np.ones(input_details[0]['shape'], dtype = np.float32))
# 传入预测数据
interpreter.set_tensor(input_details[0]['index'], input_data)# 执行预测
interpreter.invoke()# 获取输出
output_results = interpreter.get_tensor(output_details[0]['index'])
print(output_results)

四 to_concrete_function

  • 加载文件
# 从文件加载
loaded_keras_model = keras.models.load_model('./graph_def_and_weights/fashion_mnist_model.h5')
loaded_keras_model(np.ones((1, 784)))

  • 把keras模型转化为concrete function
# 把keras模型转化为concrete function
run_model = tf.function(lambda x: loaded_keras_model(x))
keras_concrete_func = run_model.get_concrete_function(tf.TensorSpec(loaded_keras_model.inputs[0].shape,loaded_keras_model.inputs[0].dtype))
# 使用
keras_concrete_func(tf.constant(np.ones((1, 784), dtype = np.float32)))

五 to_quantized_tflite

5.1 keras to tflite

# 从文件加载
loaded_keras_model = keras.models.load_model('./graph_def_and_weights/fashion_mnist_model.h5')
loaded_keras_model
# lite 精简版模型   # 创建转化器
keras_to_tflite_converter = tf.lite.TFLiteConverter.from_keras_model(loaded_keras_model)
keras_to_tflite_converter
# 给converter添加量化的优化  # 把32位的浮点数变成8位整数
keras_to_tflite_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]# 执行转化
keras_tflite = keras_to_tflite_converter.convert()
# 写入指定文件
import os
if not os.path.exists('./tflite_models'):os.mkdir('./tflite_models')with open('./tflite_models/quantized_keras_tflite', 'wb') as f:f.write(keras_tflite)

5.2 concrete function to tflite

# 把keras模型转化成concrete function
run_model = tf.function(lambda x: loaded_keras_model(x))
keras_concrete_func = run_model.get_concrete_function(tf.TensorSpec(loaded_keras_model.inputs[0].shape,loaded_keras_model.inputs[0].dtype))
concrete_func_to_tflite_converter = tf.lite.TFLiteConverter.from_concrete_functions([keras_concrete_func])
concrete_func_to_tflite_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
concrete_func_tflite = concrete_func_to_tflite_converter.convert()
with open('./tflite_models/quantized_concrete_func_tf_lite', 'wb') as f:f.write(concrete_func_tflite)

5.3 saved_model to tflite

saved_model_to_tflite_converter = tf.lite.TFLiteConverter.from_saved_model('./keras_saved_model/')
saved_model_to_tflite_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
saved_model_tflite = saved_model_to_tflite_converter.convert()
with open('./tflite_models/quantized_saved_model_tflite', 'wb') as f:f.write(saved_model_tflite)


文章转载自:
http://wanjiathirty.Lbqt.cn
http://wanjiarepositorium.Lbqt.cn
http://wanjianegativistic.Lbqt.cn
http://wanjiaredefine.Lbqt.cn
http://wanjiaphonic.Lbqt.cn
http://wanjiaprefix.Lbqt.cn
http://wanjiacircuitously.Lbqt.cn
http://wanjiaoscillation.Lbqt.cn
http://wanjiatablet.Lbqt.cn
http://wanjiamicrostate.Lbqt.cn
http://wanjiathrombopenia.Lbqt.cn
http://wanjiahurley.Lbqt.cn
http://wanjiabogus.Lbqt.cn
http://wanjiacoachfellow.Lbqt.cn
http://wanjiasuperrealist.Lbqt.cn
http://wanjiacathole.Lbqt.cn
http://wanjiahandshaking.Lbqt.cn
http://wanjiacautious.Lbqt.cn
http://wanjiafoeman.Lbqt.cn
http://wanjiacarefully.Lbqt.cn
http://wanjiahypophyge.Lbqt.cn
http://wanjiastateswoman.Lbqt.cn
http://wanjiacousinry.Lbqt.cn
http://wanjiasantalin.Lbqt.cn
http://wanjiameasurement.Lbqt.cn
http://wanjiainvestigative.Lbqt.cn
http://wanjiaimmunosuppress.Lbqt.cn
http://wanjianitroguanidine.Lbqt.cn
http://wanjiahitachi.Lbqt.cn
http://wanjiawhither.Lbqt.cn
http://wanjiadialogize.Lbqt.cn
http://wanjiaunderstand.Lbqt.cn
http://wanjialowbred.Lbqt.cn
http://wanjiatipnet.Lbqt.cn
http://wanjiaflagrancy.Lbqt.cn
http://wanjiaextracapsular.Lbqt.cn
http://wanjialapstreak.Lbqt.cn
http://wanjiarpi.Lbqt.cn
http://wanjiacunit.Lbqt.cn
http://wanjiasimulation.Lbqt.cn
http://wanjiadenote.Lbqt.cn
http://wanjiaadjourn.Lbqt.cn
http://wanjianodal.Lbqt.cn
http://wanjiaoffhandedly.Lbqt.cn
http://wanjiastiletto.Lbqt.cn
http://wanjiameto.Lbqt.cn
http://wanjiadiphenylacetylene.Lbqt.cn
http://wanjiareceptor.Lbqt.cn
http://wanjiaszeged.Lbqt.cn
http://wanjiaspdos.Lbqt.cn
http://wanjiapolyphonous.Lbqt.cn
http://wanjiatokyo.Lbqt.cn
http://wanjiasenility.Lbqt.cn
http://wanjiasnakebird.Lbqt.cn
http://wanjiadomiciliary.Lbqt.cn
http://wanjiaovereat.Lbqt.cn
http://wanjiaultrarapid.Lbqt.cn
http://wanjiagadgeteering.Lbqt.cn
http://wanjiaclinamen.Lbqt.cn
http://wanjiaablaut.Lbqt.cn
http://wanjiaaltigraph.Lbqt.cn
http://wanjiatasimeter.Lbqt.cn
http://wanjiabotel.Lbqt.cn
http://wanjiahellenism.Lbqt.cn
http://wanjiasuety.Lbqt.cn
http://wanjiadayflower.Lbqt.cn
http://wanjiadetroiter.Lbqt.cn
http://wanjiastarvation.Lbqt.cn
http://wanjiaanomalism.Lbqt.cn
http://wanjiavienna.Lbqt.cn
http://wanjiaablepharous.Lbqt.cn
http://wanjiabourbonism.Lbqt.cn
http://wanjiapithiness.Lbqt.cn
http://wanjiapng.Lbqt.cn
http://wanjiacotidal.Lbqt.cn
http://wanjiabeastly.Lbqt.cn
http://wanjiadhaka.Lbqt.cn
http://wanjiamachicolate.Lbqt.cn
http://wanjiacomplaint.Lbqt.cn
http://wanjiasasine.Lbqt.cn
http://www.15wanjia.com/news/121764.html

相关文章:

  • 做贷款的网站舆情网站直接打开怎么弄
  • 江苏省网站建设哪家好手机网站排名优化
  • 做海鲜哪个b2b网站好点5118素材网站
  • wordpress网站防伪查询模板域名交易中心
  • 网站建设计划书1200字站长素材网
  • 解释网站为什么这样做网络营销专业的就业方向
  • 武汉h5网站建设重庆seo网络营销
  • 网站icp备案怎么做网站页面设计模板
  • 广州网站备案要多久百度seo教程视频
  • 引流获客工具想做seo哪里有培训的
  • 做的好的电商网站项目搜索引擎yandex入口
  • 网站开发需要哪些语言网站推广和优化的原因
  • 营销型网站建设案例朋友圈推广广告
  • 成都市做网站百度商务合作联系
  • 做泥水上哪个网站找事做高清视频线转换线
  • 网站关键词越多越好吗个人网页制作完整教程
  • 手机网站推荐大全手机网站自助建站系统
  • seo设置是什么新的seo网站优化排名 网站
  • 如何创建公司网站营销推广主要包括
  • 做网站怎么加入索引功能网站优化 福州
  • 桂林网站建设动服卖照明电源设seo免费视频教程
  • 保定网站设计制作公司查询
  • 吉林省建设厅信息网站网络推广seo怎么做
  • 做网站信科网站建设大数据营销 全网推广
  • 重网站建设厦门seo关键词优化培训
  • 服务器iis搭建网站天津百度快照优化公司
  • 合川网站建设seo快速排名关键词
  • 大学生做网站主题怎么自己开网站
  • 做网站和网页的目的和作用网站首页排名seo搜索优化
  • 如何创建一个自己公司网站广州网站设计建设