Init commit
This commit is contained in:
109
vs_linux/src/prior_boxes.cpp
Normal file
109
vs_linux/src/prior_boxes.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Prior boxes for RetinaNet
|
||||
* Tomas Goldmann,2023
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include "prior_boxes.hpp"
|
||||
|
||||
PriorBox::PriorBox(std::vector<int> image_size, std::string phase)
|
||||
{
|
||||
|
||||
std::map<std::string, std::vector<std::vector<float>>> cfg;
|
||||
this->min_sizes = {{16.0f, 32.0f}, {64.0f, 128.0f}, {256.0f, 512.0f}};
|
||||
this->image_size = {image_size[1], image_size[0]};
|
||||
this->steps = {8, 16, 32};
|
||||
|
||||
for (const float step : steps)
|
||||
{
|
||||
this->feature_maps.push_back({(int)std::ceil(image_size[0] / step), (int)std::ceil(image_size[1] / step)});
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<float>> PriorBox::forward()
|
||||
{
|
||||
|
||||
std::vector<float> anchors;
|
||||
for (size_t k = 0; k < this->feature_maps.size(); k++)
|
||||
{
|
||||
const auto &f = this->feature_maps[k];
|
||||
const auto &min_sizes = this->min_sizes[k];
|
||||
|
||||
for (int i = 0; i < f[1]; i++)
|
||||
{
|
||||
for (int j = 0; j < f[0]; j++)
|
||||
{
|
||||
for (const auto &min_size : min_sizes)
|
||||
{
|
||||
float s_kx = min_size / this->image_size[1];
|
||||
float s_ky = min_size / this->image_size[0];
|
||||
std::vector<float> dense_cx = {static_cast<float>(j + 0.5) * this->steps[k] / this->image_size[1]};
|
||||
std::vector<float> dense_cy = {static_cast<float>(i + 0.5) * this->steps[k] / this->image_size[0]};
|
||||
|
||||
for (const auto &cy : dense_cy)
|
||||
{
|
||||
for (const auto &cx : dense_cx)
|
||||
{
|
||||
|
||||
anchors.push_back(cx);
|
||||
anchors.push_back(cy);
|
||||
anchors.push_back(s_kx);
|
||||
anchors.push_back(s_ky);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<float>> output;
|
||||
|
||||
size_t num_anchors = anchors.size() / 4;
|
||||
for (size_t i = 0; i < num_anchors; i++)
|
||||
{
|
||||
for (size_t j = 0; j < 4; j++)
|
||||
{
|
||||
anchors[i * 4 + j] = std::min(std::max(anchors[i * 4 + j], 0.0f), 1.0f);
|
||||
}
|
||||
output.push_back({anchors[i * 4], anchors[i * 4 + 1], anchors[i * 4 + 2], anchors[i * 4 + 3]});
|
||||
}
|
||||
//std::cout << "len " << output.size() << std::endl;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<std::vector<float>> decode(const std::vector<std::vector<float>> &loc, const std::vector<std::vector<float>> &priors, const std::vector<float> &variances)
|
||||
{
|
||||
std::vector<std::vector<float>> boxes;
|
||||
for (size_t i = 0; i < loc.size(); i++)
|
||||
{
|
||||
const auto &prior = priors[i];
|
||||
const auto &loc_pred = loc[i];
|
||||
float prior_x = prior[0];
|
||||
float prior_y = prior[1];
|
||||
float prior_w = prior[2];
|
||||
float prior_h = prior[3];
|
||||
float var_x = variances[0];
|
||||
float var_y = variances[1];
|
||||
float var_w = variances[2];
|
||||
float var_h = variances[3];
|
||||
|
||||
float decoded_x = prior_x + loc_pred[0] * var_x * prior_w;
|
||||
float decoded_y = prior_y + loc_pred[1] * var_y * prior_h;
|
||||
float decoded_w = prior_w * std::exp(loc_pred[2] * var_w);
|
||||
float decoded_h = prior_h * std::exp(loc_pred[3] * var_h);
|
||||
|
||||
float decoded_xmin = decoded_x - decoded_w / 2;
|
||||
float decoded_ymin = decoded_y - decoded_h / 2;
|
||||
float decoded_xmax = decoded_x + decoded_w / 2;
|
||||
float decoded_ymax = decoded_y + decoded_h / 2;
|
||||
|
||||
boxes.push_back({decoded_xmin, decoded_ymin, decoded_xmax, decoded_ymax});
|
||||
}
|
||||
|
||||
return boxes;
|
||||
}
|
61
vs_linux/src/reader.cpp
Normal file
61
vs_linux/src/reader.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Function to read floats from txt
|
||||
* Tomas Goldmann,2023
|
||||
*/
|
||||
|
||||
|
||||
#include "reader.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<vector<float>> splitFloats(const vector<float>& floats, int index) {
|
||||
|
||||
vector<float> first_half;
|
||||
vector<float> second_half;
|
||||
|
||||
// Ensure the original vector has enough elements for splitting
|
||||
if (floats.size() < 6*12600) {
|
||||
cerr << "Error: The input must containt 6*12600 floats" << endl;
|
||||
return {first_half, second_half}; // Return empty vectors
|
||||
}
|
||||
|
||||
// Split the original vector into two separate vectors
|
||||
first_half.insert(first_half.end(), floats.begin(), floats.begin() + index);
|
||||
second_half.insert(second_half.end(), floats.begin() + index, floats.end() );
|
||||
|
||||
// Return a vector containing the two split vectors
|
||||
return {first_half, second_half};
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<float> readFloatsFromFile(const string& filename) {
|
||||
vector<float> floats;
|
||||
ifstream file(filename);
|
||||
|
||||
// Check if the file is opened successfully
|
||||
if (!file.is_open()) {
|
||||
cerr << "Error opening the file" << endl;
|
||||
return floats; // Return an empty vector if the file cannot be opened
|
||||
}
|
||||
|
||||
string line;
|
||||
|
||||
// Read each line from the file
|
||||
while (getline(file, line)) {
|
||||
// Create a string stream from the line
|
||||
stringstream ss(line);
|
||||
string token;
|
||||
|
||||
// Split the line by commas and read each float
|
||||
while (getline(ss, token, ',')) {
|
||||
// Convert the string to a float and store it in the vector
|
||||
floats.push_back(stof(token));
|
||||
}
|
||||
}
|
||||
|
||||
// Close the file
|
||||
file.close();
|
||||
|
||||
return floats;
|
||||
}
|
64
vs_linux/src/utils.cpp
Normal file
64
vs_linux/src/utils.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#include "utils.hpp"
|
||||
|
||||
|
||||
bool CompareBBox(const std::vector<float> & a, const std::vector<float> & b)
|
||||
{
|
||||
return a[4] > b[4];
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::vector<std::vector<float>> nms(std::vector<std::vector<float>>& bboxes, float threshold)
|
||||
{
|
||||
std::vector<std::vector<float>> bboxes_nms;
|
||||
std::sort(bboxes.begin(), bboxes.end(), CompareBBox);
|
||||
|
||||
int32_t select_idx = 0;
|
||||
int32_t num_bbox = static_cast<int32_t>(bboxes.size());
|
||||
std::vector<int32_t> mask_merged(num_bbox, 0);
|
||||
bool all_merged = false;
|
||||
|
||||
while (!all_merged) {
|
||||
while (select_idx < num_bbox && mask_merged[select_idx] == 1)
|
||||
select_idx++;
|
||||
if (select_idx == num_bbox) {
|
||||
all_merged = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
bboxes_nms.push_back(bboxes[select_idx]);
|
||||
mask_merged[select_idx] = 1;
|
||||
|
||||
std::vector<float> select_bbox = bboxes[select_idx];
|
||||
float area1 = static_cast<float>((select_bbox[2] - select_bbox[0] + 1) * (select_bbox[3] - select_bbox[1] + 1));
|
||||
float x1 = static_cast<float>(select_bbox[0]);
|
||||
float y1 = static_cast<float>(select_bbox[1]);
|
||||
float x2 = static_cast<float>(select_bbox[2]);
|
||||
float y2 = static_cast<float>(select_bbox[3]);
|
||||
|
||||
select_idx++;
|
||||
for (int32_t i = select_idx; i < num_bbox; i++) {
|
||||
if (mask_merged[i] == 1)
|
||||
continue;
|
||||
|
||||
std::vector<float>& bbox_i = bboxes[i];
|
||||
float x = std::max<float>(x1, static_cast<float>(bbox_i[0]));
|
||||
float y = std::max<float>(y1, static_cast<float>(bbox_i[1]));
|
||||
float w = std::min<float>(x2, static_cast<float>(bbox_i[2])) - x + 1;
|
||||
float h = std::min<float>(y2, static_cast<float>(bbox_i[3])) - y + 1;
|
||||
|
||||
if (w <= 0 || h <= 0)
|
||||
continue;
|
||||
|
||||
float area2 = static_cast<float>((bbox_i[2] - bbox_i[0] + 1) * (bbox_i[3] - bbox_i[1] + 1));
|
||||
float area_intersect = w * h;
|
||||
|
||||
|
||||
if (static_cast<float>(area_intersect) / (area1 + area2 - area_intersect) > threshold) {
|
||||
mask_merged[i] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bboxes_nms;
|
||||
}
|
Reference in New Issue
Block a user