std::vector<float> Classifier::Predict(const cv::Mat& img) { Blob<float>* input_layer = net_->input_blobs()[0]; input_layer->Reshape(1, num_channels_, input_geometry_.height, input_geometry_.width); /* Forward dimension change to all layers. */ net_->Reshape(); std::vector<cv::Mat> input_channels; WrapInputLayer(&input_channels); Preprocess(img, &input_channels); net_->ForwardPrefilled(); /* Copy the output layer to a std::vector */ Blob<float>* output_layer = net_->output_blobs()[0]; const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); return std::vector<float>(begin, end); }
void ENetSegmenter::Predict(const cv::Mat& in_image_mat, cv::Mat& out_segmented) { caffe::Blob<float>* input_layer = net_->input_blobs()[0]; input_layer->Reshape(1, num_channels_, input_geometry_.height, input_geometry_.width); /* Forward dimension change to all layers. */ net_->Reshape(); std::vector<cv::Mat> input_channels; WrapInputLayer(&input_channels); Preprocess(in_image_mat, &input_channels); net_->Forward(); /* Copy the output layer to a std::vector */ caffe::Blob<float>* output_layer = net_->output_blobs()[0]; int width = output_layer->width(); int height = output_layer->height(); int channels = output_layer->channels(); // compute argmax cv::Mat class_each_row(channels, width * height, CV_32FC1, const_cast<float *>(output_layer->cpu_data())); class_each_row = class_each_row.t(); // transpose to make each row with all probabilities cv::Point maxId; // point [x,y] values for index of max double maxValue; // the holy max value itself cv::Mat prediction_map(height, width, CV_8UC1); for (int i = 0; i < class_each_row.rows; i++) { minMaxLoc(class_each_row.row(i), 0, &maxValue, 0, &maxId); prediction_map.at<uchar>(i) = maxId.x; } out_segmented = Visualization(prediction_map, lookuptable_file_); cv::resize(out_segmented, out_segmented, cv::Size(in_image_mat.cols, in_image_mat.rows)); }
std::vector<vector<float> > _SSD::detect(Frame* pFrame) { vector<vector<float> > detections; if(pFrame==NULL)return detections; if(pFrame->empty())return detections; Blob<float>* input_layer = net_->input_blobs()[0]; input_layer->Reshape(1, num_channels_, input_geometry_.height, input_geometry_.width); /* Forward dimension change to all layers. */ net_->Reshape(); std::vector<cv::cuda::GpuMat> input_channels; WrapInputLayer(&input_channels); Preprocess(*pFrame->getGMat(), &input_channels); // std::vector<cv::Mat> input_channels; // WrapInputLayer(&input_channels); // Preprocess(*pFrame->getCMat(), &input_channels); net_->Forward(); /* Copy the output layer to a std::vector */ Blob<float>* result_blob = net_->output_blobs()[0]; const float* result = result_blob->cpu_data(); const int num_det = result_blob->height(); for (int k = 0; k < num_det; ++k) { if (result[0] == -1) { // Skip invalid detection. result += 7; continue; } vector<float> detection(result, result + 7); detections.push_back(detection); result += 7; } return detections; }
vector<float> CaffeMobile::Forward(cv::Mat img) { Blob<float> *input_layer = net_->input_blobs()[0]; input_layer->Reshape(1, num_channels_, input_geometry_.height, input_geometry_.width); /* Forward dimension change to all layers. */ net_->Reshape(); vector<cv::Mat> input_channels; WrapInputLayer(&input_channels); Preprocess(img, &input_channels); clock_t t_start = clock(); net_->ForwardPrefilled(); clock_t t_end = clock(); //VLOG(1) << "Forwarding time: " << 1000.0 * (t_end - t_start) / CLOCKS_PER_SEC<< " ms."; /* Copy the output layer to a std::vector */ Blob<float> *output_layer = net_->output_blobs()[0]; const float *begin = output_layer->cpu_data(); const float *end = begin + output_layer->channels(); return vector<float>(begin, end); }
std::vector<float> Classifier::Predict(const cv::Mat& img) { // clock_t t1 = clock(); caffe::Blob<float>* input_layer = net_->input_blobs()[0]; input_layer->Reshape(1, num_channels_, input_geometry_.height, input_geometry_.width); /* Forward dimension change to all layers. */ net_->Reshape(); std::vector<cv::Mat> input_channels; WrapInputLayer(&input_channels); Preprocess(img, &input_channels); // clock_t t2 = clock(); net_->ForwardPrefilled(); // clock_t t3 = clock(); // std::cout << "(" << (t2-t1)*1.0/CLOCKS_PER_SEC << ")" << "(" << (t3-t2)*1.0/CLOCKS_PER_SEC << ")" << std::endl; /* Copy the output layer to a std::vector */ caffe::Blob<float>* output_layer = net_->output_blobs()[0]; const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); return std::vector<float>(begin, end); }