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

石家庄晋州网站建设阿里云能做网站么

石家庄晋州网站建设,阿里云能做网站么,简述一个网站设计的主要步骤,天津网站推广一、说明 在这篇文章中#xff0c;我们将展示如何构建计算机视觉模型并准备将其部署在移动和嵌入式设备上。有了这些知识#xff0c;您就可以真正将脚本部署到日常使用或移动应用程序中。 教程概述#xff1a; 介绍在 TensorFlow 中构建模型将模型转换为 TensorFlow Lite训练… 一、说明         在这篇文章中我们将展示如何构建计算机视觉模型并准备将其部署在移动和嵌入式设备上。有了这些知识您就可以真正将脚本部署到日常使用或移动应用程序中。         教程概述 介绍在 TensorFlow 中构建模型将模型转换为 TensorFlow Lite训练后量化 二、前提知识       上次我们展示了如何使用迁移学习来提高模型性能。但是当我们可以在智能手机或其他嵌入式设备上使用我们的模型时为什么我们只使用我们的模型来预测计算机上猫或狗的图像呢         TensorFlow Lite 是 TensorFlow 针对移动和嵌入式设备的轻量级解决方案。它使我们能够在移动设备上以低延迟快速运行机器学习模型而无需访问服务器。         它可以通过 C API 在 Android 和 iOS 设备上使用也可以与 Android 开发人员的 Java 包装器一起使用。 三、 在 TensorFlow 中构建模型         在开始使用 TensorFlow Lite 之前我们需要训练一个模型。我们将使用台式计算机或云平台等更强大的机器从一组数据训练模型。然后可以导出该模型并在移动设备上使用。         让我们首先准备训练数据集。我们将使用 wget .download 命令下载数据集。之后我们需要解压缩它并合并训练和测试部分的路径。 import os import wget import zipfile wget.download(https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip)100% [........................................................................] 68606236 / 68606236 Out[2]: cats_and_dogs_filtered.zip with zipfile.ZipFile(cats_and_dogs_filtered.zip,r) as zip_ref:zip_ref.extractall()base_dir cats_and_dogs_filteredtrain_dir os.path.join(base_dir, train) validation_dir os.path.join(base_dir, validation)                  接下来让我们导入所有必需的库并构建我们的模型。我们将使用一个名为MobileNetV2的预训练网络 该网络是在 ImageNet 数据集上进行训练的。之后我们需要冻结预训练层并添加一个名为GlobalAveragePooling2D 的新层然后是具有 sigmoid 激活函数的密集层。我们将使用图像数据生成器来迭代图像。 base_model MobileNetV2(input_shape(224, 224, 3),include_topFalse,weightsimagenet)base_model.trainable False model Sequential([base_model,GlobalAveragePooling2D(),Dense(1, activationsigmoid)]) model.compile(lossbinary_crossentropy,optimizerRMSprop(lr0.001),metrics[accuracy])train_datagen ImageDataGenerator(rescale1./255) val_datagen ImageDataGenerator(rescale1./255)train_generator train_datagen.flow_from_directory(train_dir,target_size(224, 224),batch_size32,class_modebinary)validation_generator val_datagen.flow_from_directory(validation_dir,target_size(224, 224),batch_size32,class_modebinary)Found 2000 images belonging to 2 classes. Found 1000 images belonging to 2 classes. history model.fit(train_generator,epochs6,validation_datavalidation_generator,verbose2) Train for 63 steps, validate for 32 steps Epoch 1/6 63/63 - 470s - loss: 0.3610 - accuracy: 0.8515 - val_loss: 0.1664 - val_accuracy: 0.9460 Epoch 2/6 63/63 - 139s - loss: 0.2055 - accuracy: 0.9210 - val_loss: 0.2065 - val_accuracy: 0.9150 Epoch 3/6 63/63 - 89s - loss: 0.1507 - accuracy: 0.9470 - val_loss: 0.1294 - val_accuracy: 0.9560 Epoch 4/6 63/63 - 81s - loss: 0.1342 - accuracy: 0.9510 - val_loss: 0.1733 - val_accuracy: 0.9350 Epoch 5/6 63/63 - 80s - loss: 0.1205 - accuracy: 0.9555 - val_loss: 0.1684 - val_accuracy: 0.9390 Epoch 6/6 63/63 - 82s - loss: 0.1079 - accuracy: 0.9620 - val_loss: 0.1418 - val_accuracy: 0.9450 四、 将模型转换为 TensorFlow Lite         在 TensorFlow 中训练深度神经网络后我们可以将其转换为 TensorFlow Lite。         这里的想法是使用提供的 TensorFlow Lite 转换器来转换模型并生成 TensorFlow Lite FlatBuffer文件。此类文件的扩展名是.tflite。         可以直接从经过训练的 tf.keras 模型、保存的模型或具体的 TensorFlow 函数进行转换这也是可能的。 converter tf.lite.TFLiteConverter.from_keras_model(model) tflite_model converter.convert()open(converted_model.tflite, wb).write(tflite_model)         转换后FlatBuffer 文件可以部署到客户端设备。在客户端设备上它可以使用 TensorFlow Lite 解释器在本地运行。 https://www.tensorflow.org/lite/convert         让我们看看如何使用TensorFlow Lite Interpreter直接从 Python 运行该模型。 interpreter tf.lite.Interpreter(model_contenttflite_model) interpreter.allocate_tensors()         我们可以通过运行get_input_details和get_output_details函数来获取模型的输入和输出详细信息。我们还可以调整输入和输出的大小这样我们就可以对整批图像进行预测。 input_details interpreter.get_input_details() output_details interpreter.get_output_details()print( Input details ) print(shape:, input_details[0][shape]) print(type:, input_details[0][dtype]) print(\n Output details ) print(shape:, output_details[0][shape]) print(type:, output_details[0][dtype]) Input details shape: [ 1 224 224 3] type: class numpy.float32 Output details shape: [1 1] type: class numpy.float32 interpreter.resize_tensor_input(input_details[0][index], (32, 224, 224, 3)) interpreter.resize_tensor_input(output_details[0][index], (32, 5)) interpreter.allocate_tensors()input_details interpreter.get_input_details() output_details interpreter.get_output_details()print( Input details ) print(shape:, input_details[0][shape]) print(type:, input_details[0][dtype]) print(\n Output details ) print(shape:, output_details[0][shape]) print(type:, output_details[0][dtype]) Input details shape: [ 32 224 224 3] type: class numpy.float32 Output details shape: [32 1] type: class numpy.float32         接下来我们可以生成一批图像在我们的例子中为 32 个因为这是之前图像数据生成器中使用的数量。         使用 TensorFlow Lite 进行预测的第一步是设置模型的输入在我们的示例中是一批 32 张图像。设置输入张量后我们可以调用invoke 命令来调用解释器。输出预测将通过调用get_tensor命令获得。         我们来对比一下 TensorFlow Lite 模型和 TensorFlow 模型的结果。它们应该是相同的。我们将使用np.testing.assert_almost_equal函数来完成此操作。如果结果在一定的小数位数内不相同则会引发错误。 mage_batch, label_batch next(iter(validation_generator)) interpreter.set_tensor(input_details[0][index], image_batch) interpreter.invoke() tflite_results interpreter.get_tensor(output_details[0][index])tf_results model.predict(image_batch)for tf_result, tflite_result in zip(tf_results, tflite_results):np.testing.assert_almost_equal(tf_result, tflite_result, decimal5)         到目前为止没有错误所以它向我们表明此转换已成功完成。但这是有阶级概率的。让我们编写一个函数将其转换为类。该函数将执行与 TensorFlow 中内置函数相同的操作。此外我们还将展示这些预测。红色将显示与主 TensorFlow 模型不同的预测蓝色图像将显示两个模型做出相同的预测。作为标签我们将显示 TensorFlow Lite 模型的预测。 def predict_classes(tf_model, tf_lite_interpreter, x):tf_lite_interpreter.set_tensor(input_details[0][index], x)tf_lite_interpreter.invoke()tflite_results tf_lite_interpreter.get_tensor(output_details[0][index])tf_results model.predict_classes(x)if tflite_results.shape[-1] 1:tflite_results tflite_results.argmax(axis-1)else:tflite_results (tflite_results 0.5).astype(int32)plt.figure(figsize(12,10))for n in range(32):plt.subplot(6,6,n1)plt.subplots_adjust(hspace 0.3)plt.imshow(image_batch[n])color blue if tf_results[n] tflite_results[n] else redplt.title(tflite_results[n], colorcolor)plt.axis(off)_ plt.suptitle(Model predictions (blue: same, red: different))predict_classes(model, interpreter, image_batch) 五、训练后量化         这非常简单但某些模型并未针对在低功耗设备上运行进行优化。因此为了解决这个问题我们需要进行量化。         训练后量化是一种转换技术可以减小模型大小同时改善 CPU 和硬件加速器延迟而模型精度几乎没有下降。         下表显示了三种技术及其优点以及可以运行该技术的硬件。 技术好处硬件权重量化缩小 4 倍加速 2-3 倍准确度提高中央处理器全整数量化缩小 4 倍加速 3 倍以上CPU、边缘TPU等Float16 量化体积缩小 2 倍具有潜在的 GPU 加速能力中央处理器/图形处理器         让我们看看它是如何进行权重量化的这是默认的。 converter tf.lite.TFLiteConverter.from_keras_model(model) converter.optimizations [tf.lite.Optimize.DEFAULT] tflite_quant_model converter.convert()open(converted_model.tflite, wb).write(tflite_quant_model)interpreter tf.lite.Interpreter(model_contenttflite_quant_model) interpreter.allocate_tensors() input_details interpreter.get_input_details() output_details interpreter.get_output_details()interpreter.resize_tensor_input(input_details[0][index], (32, 224, 224, 3)) interpreter.resize_tensor_input(output_details[0][index], (32, 5)) interpreter.allocate_tensors()input_details interpreter.get_input_details() output_details interpreter.get_output_details() print( Input details ) print(shape:, input_details[0][shape]) print(type:, input_details[0][dtype]) print(\n Output details ) print(shape:, output_details[0][shape]) print(type:, output_details[0][dtype]) Input details shape: [ 32 224 224 3] type: class numpy.float32 Output details shape: [32 1] type: class numpy.float32 以下决策树可以帮助确定哪种训练后量化方法最适合您的用例。         在我们的例子中我们使用名为 MobileNetV2 的模型该模型已经针对低功耗设备进行了优化。所以本例中的权重量化会降低模型精度。         和之前一样让我们​​看看这次模型的表现如何。红色将显示与主 TensorFlow 模型不同的预测蓝色图像将显示两个模型做出相同的预测。作为标签我们将显示 TensorFlow Lite 模型的预测。 predict_classes(model, interpreter, image_batch) 六、总结         总而言之在这篇文章中我们讨论了从 TensorFlow 到 TensorFlow Lite 的转换以及低功耗设备的优化。在下一篇文章中我们将展示如何在 TensorFlow 2.0 中实现 LeNet-5。
http://www.hkea.cn/news/14296802/

相关文章:

  • 如何查询网站备案号网站开发人员趋势
  • 群晖可以做网站吗招商网站建设全包
  • 南京手机网站制作公司做游戏音频下载网站
  • 专门做各种产品测评的网站asp网站源码安装流程
  • 合肥网站推广优化公司个人网站logo
  • 视频网站直播怎么做的企业手机网站建设信息
  • 佛山市研发网站建设哪家好网站开发时自适应
  • 下列关于网站开发中网页上传和武夷山网站定制
  • 无锡网站制作公司哪家好廊坊seo整站优化软件
  • 网站建设售后回访话术东莞市广建建设工程有限公司
  • 网站建设年度报告注册google账号
  • 住房和城市建设厅网站2016企业网站建设方案
  • 河南专业网站建设公司排名中小企业建站是什么
  • 临海钢结构设计网站wordpress安装不了主题
  • 做网站智能工具中国建设银行网址是什么
  • 烟台网站建设4038gzs培训教育机构
  • 自动注册wordpress账号软件google企业网站seo
  • 苏州知名高端网站建设企业大庆今天最新公告
  • 河北省城乡和建设厅网站首页阀门公司网站建设
  • 阿瓦提网站建设项目投资
  • 织梦做的网站有哪些小程序制作软件下载
  • 免费网站建设福州工作室网站建设的意义
  • wordpress建站腾讯云现在pc网站的标准一般是做多大
  • 网站开速度 流失网站开发之前前后端不分离
  • 可以做系统同步时间的网站制作商城公司
  • 基础型网站价格网站设计中怎么做二级页面
  • 企业做网站平台的好处设计师个人主页
  • 深圳住房和建设局网站官网打不开综合门户网站什么意思
  • 在哪找专业做淘宝网站wap网站解析
  • 怎样营销网站建设江门seo推广公司