在Web Worker中导入张量流时,Angular TypeScript TypeCheck问题 [英] Angular typescript typecheck issues when importing tensorflow in web worker

查看:127
本文介绍了在Web Worker中导入张量流时,Angular TypeScript TypeCheck问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个有角度的项目的网络工作者中使用tensorflow/tfjs(TF).

I am trying to use tensorflow/tfjs (TF) in a web-worker in an angular project.

使用 ng generate worker 命令创建网络工作者就可以了.

Creating a web-worker using the ng generate worker command works just fine.

在组件中导入TF也是可以的.

Importing TF in a component is fine too.

但是在worker中导入TF,即:

However importing TF in the worker i.e. :

import * as tf from '@tensorflow/tfjs'

通过 ng build 命令进行构建时,会生成大量缺少定义的错误.缺少的类型通常是与DOM相关的类型,例如 error TS2304:找不到名称ImageData |错误.HTMLImageElement |HTMLCanvasElement |HTMLVideoElement .在TF中的某些定义中使用了这些类型,据我所知,网络工作者无法访问这些类型,因为

Generates a bunch of missing definition errors when building through the ng build command. Missing types are typically DOM-related types such as error TS2304: Cannot find name ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement. Those types are used in some definitions in TF and, as I understand, those types are not accessible with web-workers because DOM manipulation cannot be done from workers. I am perfectly fine with that, my use of TF doesn't rely on those types. But I still need to find a way to build my worker.

因此,我尝试修改 tsconfig.worker.json 文件.我的第一个尝试是通过添加"dom"图标来模仿其他tsconfig *文件.在 compilerOptions.lib 数组中:

I therefore tried to tinker with the tsconfig.worker.json file. My first attempt was to mimic the other tsconfig* files by adding "dom" in the compilerOptions.lib array :

["es2018", "webworker"] 

替换为

["es2018", "webworker", "dom"]

这会导致类型定义冲突

error TS6200: Definitions of the following identifiers conflict with those in another file 

webworker dom 库对于相同类型具有不同的定义,但是我当然不能删除webworker lib参考.

the webworker and dom libs have different definitions for the same types, but I can of course not remove the webworker lib reference.

我的第二次尝试是在 tsconfig.worker.json 文件中添加 skipTypeCheck 编译器选项:那很好,我在我的网络工作者中运行了TF,并输出了结果.

My 2nd attempt was to add the skipTypeCheck compiler option in the tsconfig.worker.json file : That works just fine, I got TF running in my web worker and outputting results.

但是...

跳过类型检查就像撕裂了使用打字稿的整个想法.所以我的问题是:

Skipping type checking feels like ripping of the whole idea of using typescript. So my question is :

在保留类型检查的同时,有没有一种更清洁的方法可以在webworker中以角度使用TF?

感谢您的支持.请让我知道是否需要提供更多配置详细信息.

Thanks for your anwsers. Please let me know if I should provide more configuration details.

推荐答案

来自TensorFlow官方教程: https://www.tensorflow.org/js/tutorials/setup

From the official TensorFlow tutorial: https://www.tensorflow.org/js/tutorials/setup

使用TypeScript时,如果您的项目使用严格的null检查,否则您可能需要在tsconfig.json文件中设置skipLibCheck:true.

When using TypeScript you may need to set skipLibCheck: true in your tsconfig.json file if your project makes use of strict null checking or you will run into errors during compilation.

这比 skipTypeCheck 更好,因为仍然可以检查代码中的类型.这样,代码可以编译,但是您仍然在控制台中收到错误:获取WebGL上下文时出错:错误:无法在此上下文中创建画布.

This is better than skipTypeCheck as types in your code are still checked. This way the code compiles but you still get an error in console: Error when getting WebGL context: Error: Cannot create a canvas in this context.

但是到目前为止,这不是问题.

But that is not a problem so far.

这篇关于在Web Worker中导入张量流时,Angular TypeScript TypeCheck问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆