在Android中使用BI LSTM CTC Tensorflow模型

2024-01-12

TL;DR,我想知道如何在 Android 应用程序中使用 bi-lstm-ctc 张量流模型。

我已经成功训练了我的 bi-lstm-ctc 张量流模型,现在我想将它用于我的手写识别 Android 应用程序。这是定义我使用的图表的代码部分:

self.inputs = tf.placeholder(tf.float32, [None, None, network_config.num_features], name="input")
self.labels = tf.sparse_placeholder(tf.int32, name="label")
self.seq_len = tf.placeholder(tf.int32, [None], name="seq_len_input")

logits = self._bidirectional_lstm_layers(
   network_config.num_hidden_units,
   network_config.num_layers,
   network_config.num_classes
)

self.global_step = tf.Variable(0, trainable=False)
self.loss = tf.nn.ctc_loss(labels=self.labels, inputs=logits, sequence_length=self.seq_len)
self.cost = tf.reduce_mean(self.loss)

self.optimizer = tf.train.AdamOptimizer(network_config.learning_rate).minimize(self.cost)
self.decoded, self.log_prob = tf.nn.ctc_beam_search_decoder(inputs=logits, sequence_length=self.seq_len, merge_repeated=False)
self.dense_decoded = tf.sparse_tensor_to_dense(self.decoded[0], default_value=-1, name="output")

我还成功地在冻结和优化图形代码之后冻结和优化了图形tutorial https://omid.al/posts/2017-02-20-Tutorial-Build-Your-First-Tensorflow-Android-App.html。这是应该运行模型的代码部分:

bitmap = Bitmap.createScaledBitmap(bitmap, 1024, 128, true);
int[] intValues = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
float[] floatValues = new float[bitmap.getWidth() * bitmap.getHeight()];
for (int i = 0; i < intValues.length; ++i) {
    final int val = intValues[i];
    floatValues[i] = (((val >> 16) & 0xFF));
}
float[] result = new float[80];
long[] INPUT_SIZE = new long[]{1, bitmap.getHeight(), bitmap.getWidth()};
inferenceInterface.feed(config.getInputName(), floatValues, INPUT_SIZE);
inferenceInterface.feed("seq_len_input", new int[]{bitmap.getWidth()}, 1);
inferenceInterface.run(config.getOutputNames());
inferenceInterface.fetch(config.getOutputNames()[0], result);

return result.toString();

但是,根据我使用的型号,我会遇到这些问题。如果我使用冻结图,我会遇到以下错误:

Caused by: java.lang.IllegalArgumentException: No OpKernel was registered to support
Op 'SparseToDense' with these attrs.  Registered devices: [CPU], Registered kernels:
device='CPU'; T in [DT_STRING]; Tindices in [DT_INT64]
device='CPU'; T in [DT_STRING]; Tindices in [DT_INT32]
device='CPU'; T in [DT_BOOL]; Tindices in [DT_INT64]
device='CPU'; T in [DT_BOOL]; Tindices in [DT_INT32]
device='CPU'; T in [DT_FLOAT]; Tindices in [DT_INT64]
device='CPU'; T in [DT_FLOAT]; Tindices in [DT_INT32]
device='CPU'; T in [DT_INT32]; Tindices in [DT_INT64]
device='CPU'; T in [DT_INT32]; Tindices in [DT_INT32]

[[Node: output = SparseToDense[T=DT_INT64, Tindices=DT_INT64, validate_indices=true](CTCBeamSearchDecoder, CTCBeamSearchDecoder:2, CTCBeamSearchDecoder:1, output/default_value)]]

如果我使用优化的冻结图,我会遇到以下错误:

java.io.IOException: Not a valid TensorFlow Graph serialization: NodeDef expected inputs '' do not match 1 inputs 
specified; Op<name=Const; signature= -> output:dtype; attr=value:tensor; attr=dtype:type>; 
NodeDef: stack_bidirectional_rnn/cell_0/bidirectional_rnn/bw/bw/while/add/y = Const[dtype=DT_INT32, 
value=Tensor<type: int32 shape: [] values: 1>](stack_bidirectional_rnn/cell_0/bidirectional_rnn/bw/bw/while/Switch:1)

除了解决这些错误的方法之外,我还有其他问题/澄清:

我该如何解决这些错误?


我已经成功了。解决方案也可以在这个中找到github问题 https://github.com/tensorflow/tensorflow/issues/13651.

显然,问题在于所使用的类型。我正在传递 int64 ,其中只接受 int32 。

self.dense_decoded = tf.sparse_tensor_to_dense(self.decoded[0], default_value=-1, name="output")

为了解决这个问题,我将稀疏张量元素转换为 int32:

self.dense_decoded = tf.sparse_to_dense(tf.to_int32(self.decoded[0].indices),
               tf.to_int32(self.decoded[0].dense_shape),
               tf.to_int32(self.decoded[0].values),
               name="output")

之后运行应用程序给了我这个错误:

java.lang.IllegalArgumentException: Matrix size-incompatible: In[0]: [1,1056], In[1]: [160,128]
[[Node:stack_bidirectional_rnn/cell_0/bidirectional_rnn/bw/bw/while/bw/basic_lstm_cell/basic_lstm_cell/

MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"]

(stack_bidirectional_rnn/cell_0/bidirectional_rnn/bw/bw/while/bw/basic_lstm_cell/basic_lstm_cell/concat, 
stack_bidirectional_rnn/cell_0/bidirectional_rnn/bw/bw/while/bw/basic_lstm_cell/basic_lstm_cell/MatMul/Enter)]]

由于某些奇怪的原因,在 java 代码中将图像宽度从 1024 更改为 128 修复了该错误。再次运行该应用程序给了我这个错误:

java.lang.IllegalArgumentException: cannot use java.nio.FloatArrayBuffer with Tensor of type INT32

获取输出时出现问题。这样,我知道模型运行成功,但应用程序无法获取结果。

inferenceInterface.run(outputs);
inferenceInterface.fetch(outputs[0], result); //where the error happens

愚蠢的我忘记了输出是一个整数数组,而不是一个浮点数组。因此,我将结果数组的类型更改为 int 数组:

//float[] result = new float[80];
int[] result = new int[80];

从而使应用程序正常工作。模型的准确性不好,因为它没有经过适当的训练。我只是想让它在应用程序中工作。是时候进行一些认真的训练了!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Android中使用BI LSTM CTC Tensorflow模型 的相关文章

随机推荐

  • Oracle 触发器创建自动编号

    我以前从未在 Oracle 中创建过触发器 所以我正在寻找一些方向 我想创建一个触发器 如果 ID 不在插入语句中 则将 ID 加一 ID 应从 10000 开始 插入记录时下一个 ID 应为 10001 如果插入语句包含 ID 则应覆盖自
  • 从用户应用程序访问 Ring 0 模式(以及 Borland 允许这样做的原因)

    随着学期截止日期的临近 我决定开始在我的大学开展操作系统课程的项目 项目作业的问题在于它要求学生开发一个用户应用程序 exe 将作为一个简单的内核执行 基本进程和线程管理 我首先想到的是 我到底该如何在用户应用程序中执行特权代码 在咨询了其
  • 如何在同一个 Visual Studio 项目中混合使用 Node.js 和 Typescript?

    Visual Studio 有一个打字稿用于使用 Typescript 语言开发应用程序的插件 而且还有Node jsVS 工具 可以用来创建和调试 Node js 应用程序 我尝试创建一个 Node js 项目 但是无法向其中添加 Typ
  • 从单元测试运行时,CATextLayer 不会出现在 AVMutableComposition 中

    编辑 最奇怪的事情 似乎当从完整的应用程序运行此代码时 一切正常 但我总是从单元测试中运行电影的创建 只有在那里它不起作用 试图弄清楚为什么会这样 我正在尝试使用 AVMutableComposition 组合视频 音频 文本并将其导出到新
  • 如何为 Django 启用 WSGIPassAuthorization?

    我正在测试 Django API 端点 但我需要启用 WSGIPassAuthorization 才能接收 Authorization 标头 我应该在哪里启用它 PS 我使用的是 macOS 但任何答案都可能有用 如果您正在使用mod ws
  • Mac 事件点击只是延迟丢弃的事件

    我正在尝试编写一些代码 在 Mac OSX 10 6 上启用时丢弃所有键盘和鼠标事件 我的代码以 root 用户身份运行 我采取的方法是创建一个事件水龙头 丢弃传递给它的所有事件 启用时 事件点击回调函数如下所示 CGEventRef My
  • div 内的点击位置

    我试图获取 div 内单击的位置 以便当我在鼠标拖动移动窗口时定位窗口时 鼠标光标将恰好位于初始单击发生的位置 相对于移动窗口 这是窗口 div class Popup div img class xOut src images xOut
  • 叮叮当当“你好,世界!” Windows 中的链接错误

    我刚刚下载了 CLang 源代码 使用 CMake 创建了 Visual C 10 IDE 工作区 并从 Visual C 10 0 express 构建了所有内容 现在我在 hello world 上收到一堆链接器错误 d dev tes
  • 如何在golang中编写bson形式的mongo查询?

    我可以使用命令行查询查询我的 mongodb 集合 以根据 nfType 和最小距离获取 ipv4Addresses db nfinstancesdb aggregate match nfType AMF unwind ipv4Addres
  • 如何解密MySQL密码

    创建我公司使用的平台的开发人员不再为我们工作 我不知道如何从自定义 PHP 应用程序检索密码 当我查看 PHPmyAdmin 时 密码已加密 例如 2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 我如何更改
  • 如何禁用布局中的所有按钮?

    应用程序的工作方式如下 应用程序向用户提示 30 个按钮 用户可以通过点击猜出正确的按钮 当用户点击某个按钮时 所有按钮 例如包含这些按钮的视图 都应该被锁定 同时播放相应的 正确或错误的猜测 动画 点击的按钮本身应该被禁用 直到下一轮 动
  • 停止 Chrome 标签页睡眠/休眠

    如何阻止 Chrome 的选项卡睡眠 休眠 我正在运行 左轮手枪 扩展 我希望在循环之前重新加载选项卡 但是 chrome 的选项卡似乎处于休眠状态 因此在选项卡处于 活动 状态之前不会重新加载 并且显示 有解决方法吗 来自 wOxxOm
  • ember-data DS.RESTAdapter 导致 TypeError

    我正在尝试使用 Ember js 和 ember data 并且定义了以下应用程序 window App Ember Application create App store DS Store create revision 4 adapt
  • Django - {% csrf_token %} 在模板中使用,但上下文未提供值

    我是 Django 的新手 我仍在尝试掌握它的功能 我创建了非常简单的项目姜戈 1 4 2它有简单形式的索引页面 您可以在其中输入内容 还有结果页面 您的输入在提交后显示 代码如下 提交后 我收到错误 403 和以下消息 模板中使用了 cs
  • shell: /bin/bash -e {0} 在 github Action Worker 的 bash shell 输出中意味着什么?

    因为当没有要提交的更改时 git 会使用非零代码提交退出 这会导致 github 操作失败 为了克服这个问题 我尝试在提交之前检查是否有任何更改 如下所示 if git diff index quiet HEAD then echo cha
  • C++读取wav文件,subchunk1size = 18

    通常 wav 文件的 subchunk1size 是 16 但是 我有一些 subchunk1size 18 的 wav 文件 我有 C 代码来读取 subchunk1size 16 的 wav 文件 现在我想读取 subchunk1siz
  • iOS 6 旋转问题 - 呈现的模态视图控制器没有旋转

    我有一个 MainViewController 它有一个按钮 可以通过水平翻转推送新视图 InfoViewController 像这样 controller modalTransitionStyle UIModalTransitionSty
  • 构建动态 where 子句,Linq To Sql

    我使用的是 EF Code First 4 2 当需要动态构建 where 子句时 您提出什么样的解决方案 然而 非常需要包含功能 var results db Set
  • 使用 rsync 仅同步修改过的文件

    我正在尝试同步两个文件夹 developer and shared 在Ubuntu中 当我修改文件时 shared 我希望能够将文件复制到 developer folder I tried rsync r shared developer
  • 在Android中使用BI LSTM CTC Tensorflow模型

    TL DR 我想知道如何在 Android 应用程序中使用 bi lstm ctc 张量流模型 我已经成功训练了我的 bi lstm ctc 张量流模型 现在我想将它用于我的手写识别 Android 应用程序 这是定义我使用的图表的代码部分