本文介绍了如何在C ++中为Tensorflow-Lite设置图像输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试将Tensoflow模型从Python + Keras版本转移到嵌入式平台上具有C ++的Tensorflow Lite。 似乎我不知道如何 输入形状应为(1、224、224、3)。 作为输入,我正在使用openCV拍摄图像,并将其转换为CV_BGR2RGB。 std :: unique_ptr< tflite :: FlatBufferModel> model_stage1 = tflite :: FlatBufferModel :: BuildFromFile( model1.tflite); TFLITE_MINIMAL_CHECK(model_stage1!= nullptr); //构建解释器 tflite :: ops :: builtin :: BuiltinOpResolver resolver_stage1; std :: unique_ptr< Interpreter> interpreter_stage1; tflite :: InterpreterBuilder(* model_stage1,resolver_stage1)(& interpreter_stage1); TFLITE_MINIMAL_CHECK(interpreter_stage1!= nullptr); cv :: Mat cvimg = cv :: imread(imagefile); if(cvimg.data == NULL){ printf( ===图片读取错误=== \n); 返回0; } cv :: cvtColor(cvimg,cvimg,CV_BGR2RGB); uchar * input_1 = interpreter_stage1-> typed_input_tensor< uchar>(0); memcpy(...); 对于此uchar类型,正确设置memcpy存在问题。 当我这样做时,我在工作中遇到段故障: memcpy (input_1,cvimg.data,cvimg.total()* cvimg.elemSize()); 在这种情况下应如何正确填写输入内容?解决方案要将我的评论转换为答案: Memcpy在这里可能不是正确的方法。 OpenCV将图像保存为每像素RGB顺序(或BGR或另一种颜色组合)颜色值的一维数组。可以通过以下方式遍历这些RGB块: for(const auto& rgb: cvimg){ //现在rgb [0]为红色,rgb [1]为绿色,rgb [2]为蓝色。 } 将值写入Tensorflow-Lite typed_input_tensor应该是这样的;其中i是索引(迭代器),x是赋值: 解释器-> typed_input_tensor< ; uchar>(0)[i] = x; 所以循环看起来像这样: for(size_t i = 0; size_t< cvimg.size(); ++ i){ const auto& rgb = cvimg [i]; 解释器-> typed_input_tensor< uchar>(0)[3 * i + 0] = rgb [0]; 解释器-> typed_input_tensor< uchar>(0)[3 * i +1] = rgb [1]; 解释器-> typed_input_tensor< uchar>(0)[3 * i + 2] = rgb [2]; } I am trying to move our Tensoflow model from Python+Keras version to Tensorflow Lite with C++ on an embedded platform.It looks like I don't know how set properly input for interpreter.Input shape should be (1, 224, 224, 3).As an input I am taking image with openCV, converting this to CV_BGR2RGB.std::unique_ptr<tflite::FlatBufferModel> model_stage1 =tflite::FlatBufferModel::BuildFromFile("model1.tflite"); TFLITE_MINIMAL_CHECK(model_stage1 != nullptr); // Build the interpreter tflite::ops::builtin::BuiltinOpResolver resolver_stage1; std::unique_ptr<Interpreter> interpreter_stage1; tflite::InterpreterBuilder(*model_stage1, resolver_stage1)(&interpreter_stage1);TFLITE_MINIMAL_CHECK(interpreter_stage1 != nullptr); cv::Mat cvimg = cv::imread(imagefile); if(cvimg.data == NULL) { printf("=== IMAGE READ ERROR ===\n"); return 0; } cv::cvtColor(cvimg, cvimg, CV_BGR2RGB); uchar* input_1 = interpreter_stage1->typed_input_tensor<uchar>(0); memcpy( ... );I have issue with proper setup of memcpy for this uchar type.When I am doing like this, I have seg fault during working:memcpy(input_1, cvimg.data, cvimg.total() * cvimg.elemSize());How should I properly fill input in this case? 解决方案 To convert my comments into an answer:Memcpy might not be the right approach here. OpenCV saves images as 1-dimensional arrays of RGB-ordered (or BGR or yet another color combination) color values per pixel. It is possible to iterate over these RGB-chunks via:for (const auto& rgb : cvimg) { // now rgb[0] is the red value, rgb[1] green and rgb[2] blue.}And writing values to a Tensorflow-Lite typed_input_tensor should be done like this; where i is the index (iterator) and x the assigned value:interpreter->typed_input_tensor<uchar>(0)[i] = x;So the loop could look like this:for (size_t i = 0; size_t < cvimg.size(); ++i) { const auto& rgb = cvimg[i]; interpreter->typed_input_tensor<uchar>(0)[3*i + 0] = rgb[0]; interpreter->typed_input_tensor<uchar>(0)[3*i + 1] = rgb[1]; interpreter->typed_input_tensor<uchar>(0)[3*i + 2] = rgb[2];} 这篇关于如何在C ++中为Tensorflow-Lite设置图像输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 22:50