WebAssembly 在 AI 推理中的作用
你可以启动一个虚拟机服务器,并使用 TensorFlow Server 等工具运行 AI 模型。这种 DIY 方法需要对人工智能和操作系统都有深入的操作知识,而且通常相当昂贵,因为你需要为闲置资源付费。 你也可以使用公有云的 AI SaaS 服务上传自己的模型,然后使用 web UI 或 API 上传数据进行推理。这很容易,但不太灵活。会受到 SaaS 所支持的模型、配置和数据预处理/后处理的种类的限制。
国际访问 GitHub 页面: https://Github.com/second-state/tencent-TensorFlow-scf/
国内访问 Gitee 页面: https://Gitee.com/secondstate/tencent-TensorFlow-scf/
$ sls deploy ... ... website: https://sls-website-ap-hongkong-kfdilz-1302315972.cos-website.ap-hongkong.myqcloud.com vendorMessage: null 63s › tencent-TensorFlow-scf › 'deploy' ran for 3 apps successfully.
fn main() { //1.加载训练好的 TensorFlow lite 模型。 let model_data: &[u8] = include_bytes!('lite-model_aiy_vision_classifier_food_V1_1.tflite'); //2. 加载与模型相对应的分类标签文件。 //注:模型输出是一系列数字。标签文件会将这些编号(即行号)映射至食物分类的实际文字描述。 let labels = include_str!('aiy_food_V1_labelmap.txt'); //3. 上传图像的格式是 base64 编码,并通过腾讯云 API 网关封装在 JSON 对象中。 let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).expect('Error reading from STDIN'); let obj: FaasInput = serde_json::from_str(&buffer).unwrap(); let img_buf = base64::decode_config(&(obj.body), base64::STANDARD).unwrap(); //4.加载上传图像并将其调整为192x192,这是这个 MobileNet 模型所需的尺寸。 let flat_img = ssvm_TensorFlow_interface::load_jpg_image_to_rgb8(&img_buf, 192, 192); //5. 用图像作为输入张量运行模型,并获取模型输出张量。 //5.1 初始化模型,并将模型类型指定为 TensorFlow Lite。 let mut session = ssvm_TensorFlow_interface::Session::new(&model_data, ssvm_TensorFlow_interface::ModelType::TensorFlowLite); //5.2 指定模型的输入张量名称、数据和形状,以及输出张量名称。支持多个输入和输出张量。运行该模型。 session.add_input('input', &flat_img, &[1, 192, 192, 3]) .run(); let res_vec: Vec<u8> = session.get_output('MobilenetV1/Predictions/Softmax'); //6. res_vec 向量中的每个数字对应标签文件中每一行标签的概率。 //6.1 找出最高的概率... //6.2 把概率翻译成文本... //6.3 查找相应的标签文本... //7. 文本标签和概率通过 STDOUT 返回给函数的调用者。 let class_name = label_lines.next().unwrap().to_string(); println!('上传的图片里面{} <a href='https://www.google.com/search?q={}'>{}</a>', confidence.to_string(), class_name, class_name);}function callServerlessFunction () { var reader = new FileReader(); reader.readAsDataURL(document.querySelector('#select_file').files[0]); reader.onloadend = function () { $.ajax({ url: window.env.API_URL, type: 'post', data : reader.result.split('base64,')[1], dataType: 'text', success: function (data) { document.querySelector('#msg').innerHTML = data; }, error: function(jqXHR, exception){ document.querySelector('#msg').innerHTML = '服务繁忙,请稍后重试'; } }); }; return false;}$ ssvmup build --enable-aot$ cp pkg/scf.so scf/$ sls deployTensorFlow 函数demo: http://secondstate.info/tencent/ 腾讯云 Serverless 上的 AI 推理模板: https://Github.com/second-state/tencent-TensorFlow-scf/ 国内镜像: https://Gitee.com/secondstate/tencent-TensorFlow-scf/ 腾讯云 Serverless: https://cloud.tencent.com/product/scf
赞 (0)
