Closed taotaoyuhust closed 4 years ago
代码如下: ############################################ Net caffe_net("F:/caffe/RefineDet-master/models/models_VGGNet_VOC0712_refinedet_vgg16_320x320/deploy.prototxt", caffe::TEST, 0); caffe_net.CopyTrainedLayersFrom("F:/caffe/RefineDet-master/models/models_VGGNet_VOC0712_refinedet_vgg16_320x320/VOC0712_refinedet_vgg16_320x320_final.caffemodel");
// 针对单张图片的
//获取输入层尺寸 Blob* input_layer = caffe_net.input_blobs()[0];
int numchannels = input_layer->channels(); CHECK(numchannels == 3 || numchannels == 1) << "Input layer should have 1 or 3 channels."; cv::Size inputgeometry = cv::Size(input_layer->width(), input_layer->height());
std::cout << "numchannels." << numchannels; //打开图像 cv::Mat imgorin = cv::imread("F:/caffe/RefineDet-master/examples/images/000456.jpg", -1); CHECK(!imgorin.empty()) << "Unable to decode image " << FLAGS_jpgfile; double t1 = (double)cv::getTickCount();
//将输入图像reshape到输入层大小 int width = input_layer->width(); int height = input_layer->height(); float* input_data = input_layer->mutable_cpu_data(); std::vector input_channels;;
std::cout << "width." << width << "height" << height; for (int i = 0; i < input_layer->channels(); ++i) { cv::Mat channel(height, width, CV_32FC1, input_data); input_channels.push_back(channel); input_data += width * height; }
cv::Mat sample_resized; if (imgorin.size() != inputgeometry) cv::resize(imgorin, sample_resized, inputgeometry); else sample_resized = imgorin; cv::Mat sample_float; if (numchannels == 3) sample_resized.convertTo(sample_float, CV_32FC3); else sample_resized.convertTo(sample_float, CV_32FC1);
cv::Mat sample_normalized; std::vector mean_channels; vector values; values.push_back(104.0); values.push_back(117.0); values.push_back(123.0); for (int i = 0; i < numchannels; ++i) { cv::Mat channel(inputgeometry.height, inputgeometry.width, CV_32FC1, cv::Scalar(values[i])); mean_channels.pushback(channel); } cv::Mat mean; cv::merge(meanchannels, mean); cv::subtract(samplefloat, mean, sample_normalized);
cv::split(sample_normalized, input_channels);
LOG(INFO) << "before Forward.";
//前向传播 caffe_net.Forward();
LOG(INFO) << "after Forward.";
//获取检测结果 std::vector<Blob > outblobvec = caffe_net.output_blobs(); std::vector indexsss = caffe_net.output_blob_indices(); Blob result_blob = caffe_net.output_blobs()[0]; const float result = result_blob->cpu_data(); const int num_det = result_blob->height(); vector<vector > detections; for (int k = 0; k < num_det; ++k) { if (result[0] == -1) { result += 7; continue; } vector detection(result, result + 7); detections.push_back(detection); result += 7; } int t2 = ((double)cv::getTickCount() - t1) 1000 / cv::getTickFrequency(); //获得时间,单位是秒 std::cout << "time is " << t2 << std::endl;;
for (int i = 0; i < detections.size(); ++i) { const vector& d = detections[i]; // Detection format: [image_id, label, score, xmin, ymin, xmax, ymax]. CHECK_EQ(d.size(), 7); const float score = d[2]; if (score >= 0.3) { std::cout << FLAGS_jpgfile << " "; std::cout << static_cast(d[1]) << " "; std::cout << score << " "; std::cout << static_cast(d[3] sample_resized.cols) << " "; std::cout << static_cast(d[4] sample_resized.rows) << " "; std::cout << static_cast(d[5] sample_resized.cols) << " "; std::cout << static_cast(d[6] sample_resized.rows) << std::endl; //show rect cv::Point pt1, pt2; pt1.x = sample_resized.colsd[3]; pt1.y = sample_resized.rowsd[4]; pt2.x = sample_resized.colsd[5]; pt2.y = sample_resized.rowsd[6]; cv::rectangle(sample_resized, pt1, pt2, cvScalar(0, 255, 0), 1, 8, 0); } } std::string file1 = FLAGS_jpgfile + "_result.jpg"; cv::imwrite(file1, sample_resized); cv::imshow("Show", sample_resized); cv::waitKey(0);
################################################## 输出结果见附件 感觉detection_out层没起作用?nms都没做一样? 帮忙看看,非常感谢!
@taotaoyuhust C++的前传请参考这个.
是的,我就是参考这个的。。
@taotaoyuhust 我这边看你贴的代码看不出啥来,麻烦你自己单步调试一下吧。
代码如下: ############################################ Net caffe_net("F:/caffe/RefineDet-master/models/models_VGGNet_VOC0712_refinedet_vgg16_320x320/deploy.prototxt", caffe::TEST, 0);
caffe_net.CopyTrainedLayersFrom("F:/caffe/RefineDet-master/models/models_VGGNet_VOC0712_refinedet_vgg16_320x320/VOC0712_refinedet_vgg16_320x320_final.caffemodel");
// 针对单张图片的
//获取输入层尺寸 Blob* input_layer = caffe_net.input_blobs()[0];
int numchannels = input_layer->channels(); CHECK(numchannels == 3 || numchannels == 1) << "Input layer should have 1 or 3 channels."; cv::Size inputgeometry = cv::Size(input_layer->width(), input_layer->height());
std::cout << "numchannels." << numchannels; //打开图像 cv::Mat imgorin = cv::imread("F:/caffe/RefineDet-master/examples/images/000456.jpg", -1); CHECK(!imgorin.empty()) << "Unable to decode image " << FLAGS_jpgfile; double t1 = (double)cv::getTickCount();
//将输入图像reshape到输入层大小 int width = input_layer->width(); int height = input_layer->height(); float* input_data = input_layer->mutable_cpu_data(); std::vector input_channels;;
std::cout << "width." << width << "height" << height; for (int i = 0; i < input_layer->channels(); ++i) { cv::Mat channel(height, width, CV_32FC1, input_data); input_channels.push_back(channel); input_data += width * height; }
cv::Mat sample_resized; if (imgorin.size() != inputgeometry) cv::resize(imgorin, sample_resized, inputgeometry); else sample_resized = imgorin; cv::Mat sample_float; if (numchannels == 3) sample_resized.convertTo(sample_float, CV_32FC3); else sample_resized.convertTo(sample_float, CV_32FC1);
cv::Mat sample_normalized; std::vector mean_channels;
vector values;
values.push_back(104.0);
values.push_back(117.0);
values.push_back(123.0);
for (int i = 0; i < numchannels; ++i)
{
cv::Mat channel(inputgeometry.height, inputgeometry.width, CV_32FC1, cv::Scalar(values[i]));
mean_channels.pushback(channel);
}
cv::Mat mean;
cv::merge(meanchannels, mean);
cv::subtract(samplefloat, mean, sample_normalized);
cv::split(sample_normalized, input_channels);
LOG(INFO) << "before Forward.";
//前向传播 caffe_net.Forward();
LOG(INFO) << "after Forward.";
//获取检测结果 std::vector<Blob > outblobvec = caffe_net.output_blobs();
std::vector indexsss = caffe_net.output_blob_indices();
Blob result_blob = caffe_net.output_blobs()[0];
const float result = result_blob->cpu_data();
const int num_det = result_blob->height();
vector<vector > detections;
for (int k = 0; k < num_det; ++k)
{
if (result[0] == -1)
{
result += 7;
continue;
}
vector detection(result, result + 7);
detections.push_back(detection);
result += 7;
} 1000 / cv::getTickFrequency(); //获得时间,单位是秒
std::cout << "time is " << t2 << std::endl;;
int t2 = ((double)cv::getTickCount() - t1)
for (int i = 0; i < detections.size(); ++i) { const vector& d = detections[i];
// Detection format: [image_id, label, score, xmin, ymin, xmax, ymax].
CHECK_EQ(d.size(), 7);
const float score = d[2];
if (score >= 0.3)
{
std::cout << FLAGS_jpgfile << " ";
std::cout << static_cast(d[1]) << " ";
std::cout << score << " ";
std::cout << static_cast(d[3] sample_resized.cols) << " ";
std::cout << static_cast(d[4] sample_resized.rows) << " ";
std::cout << static_cast(d[5] sample_resized.cols) << " ";
std::cout << static_cast(d[6] sample_resized.rows) << std::endl;
//show rect
cv::Point pt1, pt2;
pt1.x = sample_resized.colsd[3];
pt1.y = sample_resized.rowsd[4];
pt2.x = sample_resized.colsd[5];
pt2.y = sample_resized.rowsd[6];
cv::rectangle(sample_resized, pt1, pt2, cvScalar(0, 255, 0), 1, 8, 0);
}
}
std::string file1 = FLAGS_jpgfile + "_result.jpg";
cv::imwrite(file1, sample_resized);
cv::imshow("Show", sample_resized);
cv::waitKey(0);
################################################## 输出结果见附件 感觉detection_out层没起作用?nms都没做一样? 帮忙看看,非常感谢!