I can't use the executor to forward for the second time!


#1

I try to use simblebind to finish forward propogation, but when I bind the net, the input tensor will not change, here comes the code,thanks for your help!

#include
#include
#include
#include
#include
#include <opencv2/opencv.hpp>
#include “mxnet-cpp/MxNetCpp.h”
using namespace cv;
using namespace std;
using namespace mxnet::cpp;
Context global_ctx = Context::cpu();
//Context global_ctx(kGPU, 0);
//NDArray img_tensor;
map<string, NDArray> args_map;
map<string, NDArray> aux_map;
Symbol net;

void GetFeatureSymbol() {
net = Symbol::Load("…/test-symbol.json");
auto arg_names = net.ListArguments();
for (size_t i = 0; i < arg_names.size(); ++i) {
if (arg_names[i] == “data”){
cout<<i;
}
}
}

void LoadParameters() {
map<string, NDArray> paramters;
NDArray::Load("…/test-0000.params", 0, &paramters);
for (const auto &k : paramters) {
if (k.first.substr(0, 4) == “aux:”) {
auto name = k.first.substr(4, k.first.size() - 4);
aux_map[name] = k.second.Copy(global_ctx);
}
if (k.first.substr(0, 4) == “arg:”) {
auto name = k.first.substr(4, k.first.size() - 4);
args_map[name] = k.second.Copy(global_ctx);
}
}
args_map[“data”] = NDArray(Shape(1, 3, 1000, 1000), global_ctx);
//NDArray::WaitAll();

}

void extractAndShowOneImage(Executor *executor, Mat& imgOrigResized, NDArray data) {
executor->Forward(false);
/print out the features/
auto label_array = executor->outputs[0].Copy(Context(kCPU, 0));
auto score_array = executor->outputs[1].Copy(Context(kCPU, 0));
auto bboxes_array = executor->outputs[2].Copy(Context(kCPU, 0));
NDArray::WaitAll();
for (int i = 0;i < 5; ++i) {
int cls_id = label_array.At(0,i);
float score = score_array.At(0,i);
if(score >= 0){
int min_x = static_cast(bboxes_array.At(0,i * 4));
int min_y = static_cast(bboxes_array.At(0,i * 4 + 1));
int max_x = static_cast(bboxes_array.At(0,i * 4 + 2));
int max_y = static_cast(bboxes_array.At(0,i * 4 + 3));
cout<<min_x<<","<<min_y<<","<<max_x - min_x<<","<<max_y - min_y<<endl;
//rectangle(imgOrigResized, Point(min_x, min_y), Point(max_x, max_y), Scalar(0, 23, 255), 8, 4, 0 );
rectangle(imgOrigResized, Rect(min_x, min_y, max_x - min_x, max_y - min_y), Scalar(0, 23, 255), 3, 3, 0 );
}
}
imshow(“gaga”, imgOrigResized);
waitKey(0);
}

float resizeByLongShort(Mat &imgOrig, int longSide, int shortSide){
int imgOrigMinSide = min(imgOrig.cols, imgOrig.rows);
int imgOrigMaxSide = max(imgOrig.cols, imgOrig.rows);
float imgResizedScale = float(shortSide) / float(imgOrigMinSide);
if (ceil(imgResizedScale * imgOrigMaxSide) > longSide){
imgResizedScale = float(longSide) / float(imgOrigMaxSide);
}
resize(imgOrig, imgOrig, Size(imgOrig.cols * imgResizedScale, imgOrig.rows * imgResizedScale), 0, 0, INTER_LINEAR);
return imgResizedScale;
}

NDArray transformImage(Mat imgResized, float* mean, float* std, Context ctx){
Mat imgResizedTmp;
cvtColor(imgResized, imgResizedTmp, CV_BGR2RGB);
int image_size = imgResized.rows * imgResized.cols * imgResized.channels();
vector<mx_float> image_data = std::vector<mx_float>(image_size);
NDArray img_tensor(Shape(1, 3, imgResized.rows, imgResized.cols), ctx);
for(int i = 0; i < imgResized.rows; ++i){
uchar* data = imgResized.ptr(i);
for(int j = 0; j< imgResized.cols; ++j){
image_data.data()[2imgResized.rowsimgResized.cols+iimgResized.cols+j] = (static_cast<mx_float>(data++) / 255 - static_cast<mx_float>(mean[0])) / static_cast<mx_float>(std[0]);
image_data.data()[imgResized.rows
imgResized.cols+i
imgResized.cols+j] = (static_cast<mx_float>(data++) / 255 - static_cast<mx_float>(mean[1])) / static_cast<mx_float>(std[1]);
image_data.data()[i*imgResized.cols+j] = (static_cast<mx_float>(data++) / 255 - static_cast<mx_float>(mean[2])) / static_cast<mx_float>(std[2]);
}
}
img_tensor.SyncCopyFromCPU(image_data.data(), image_size);
//NDArray::WaitAll();
return img_tensor;
}
NDArray loadImageTensor(Mat &imgOrig, int longSide, int shortSide, float
mean, float
std, Context ctx) {
float imgResizedScale = resizeByLongShort(imgOrig, shortSide, shortSide);
NDArray image_tensor = transformImage(imgOrig, mean, std, ctx);
return image_tensor;
}

int main() {
Mat imgOrig = imread("…/2.jpg");
float mean[3] = {0.485, 0.456, 0.406};
float std[3] = {0.229, 0.224, 0.225};
GetFeatureSymbol();
LoadParameters();
NDArray img_tensor = loadImageTensor(imgOrig, 1000, 512, mean, std, global_ctx);
/bind the executor/
//args_map[“data”] = img_tensor;
//net.InferArgsMap(global_ctx, &args_map, args_map);
auto *executor = net.SimpleBind(global_ctx, args_map,map<string, NDArray>(),
map<string, OpReqType>(), aux_map);
//args_map = executor->arg_dict();

NDArray img_tensor3 = loadImageTensor(imgOrig, 1000, 512, mean, std, global_ctx);
img_tensor3.CopyTo(&args_map["data"]);
NDArray::WaitAll();
//NDArray* nd = &executor->arg_arrays[0];
//img_tensor.CopyTo(nd);
//args_map["data"] = img_tensor;
extractAndShowOneImage(executor, imgOrig, img_tensor3);
imgOrig = imread("../3.jpg");
NDArray img_tensor4 = loadImageTensor(imgOrig, 1000, 512, mean, std, global_ctx);
img_tensor4.CopyTo(&args_map["data"]);
NDArray::WaitAll();
//executor->arg_arrays[0] = img_tensor;
extractAndShowOneImage(executor, imgOrig, img_tensor4);
//img_tensor = loadImageTensor(imgOrig, 1000, 512, mean, std, global_ctx);
extractAndShowOneImage(executor, imgOrig, img_tensor4);
return 0;

}