File size: 9,191 Bytes
adc9971 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | // Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "flip_dataset.h"
#include <dlib/data_io.h>
#include <dlib/dir_nav.h>
#include <string>
#include "common.h"
#include <dlib/image_transforms.h>
#include <dlib/optimization.h>
#include <dlib/image_processing.h>
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
std::vector<long> align_points(
const std::vector<dpoint>& from,
const std::vector<dpoint>& to,
double min_angle = -90*pi/180.0,
double max_angle = 90*pi/180.0,
long num_angles = 181
)
/*!
ensures
- Figures out how to align the points in from with the points in to. Returns an
assignment array A that indicates that from[i] matches with to[A[i]].
We use the Hungarian algorithm with a search over reasonable angles. This method
works because we just need to account for a translation and a mild rotation and
nothing else. If there is any other more complex mapping then you probably don't
have landmarks that make sense to flip.
!*/
{
DLIB_CASSERT(from.size() == to.size());
std::vector<long> best_assignment;
double best_assignment_cost = std::numeric_limits<double>::infinity();
matrix<double> dists(from.size(), to.size());
matrix<long long> idists;
for (auto angle : linspace(min_angle, max_angle, num_angles))
{
auto rot = rotation_matrix(angle);
for (long r = 0; r < dists.nr(); ++r)
{
for (long c = 0; c < dists.nc(); ++c)
{
dists(r,c) = length_squared(rot*from[r]-to[c]);
}
}
idists = matrix_cast<long long>(-round(std::numeric_limits<long long>::max()*(dists/max(dists))));
auto assignment = max_cost_assignment(idists);
auto cost = assignment_cost(dists, assignment);
if (cost < best_assignment_cost)
{
best_assignment_cost = cost;
best_assignment = std::move(assignment);
}
}
// Now compute the alignment error in terms of average distance moved by each part. We
// do this so we can give the user a warning if it's impossible to make a good
// alignment.
running_stats<double> rs;
std::vector<dpoint> tmp(to.size());
for (size_t i = 0; i < to.size(); ++i)
tmp[best_assignment[i]] = to[i];
auto tform = find_similarity_transform(from, tmp);
for (size_t i = 0; i < from.size(); ++i)
rs.add(length(tform(from[i])-tmp[i]));
if (rs.mean() > 0.05)
{
cout << "WARNING, your dataset has object part annotations and you asked imglab to " << endl;
cout << "flip the data. Imglab tried to adjust the part labels so that the average" << endl;
cout << "part layout in the flipped dataset is the same as the source dataset. " << endl;
cout << "However, the part annotation scheme doesn't seem to be left-right symmetric." << endl;
cout << "You should manually review the output to make sure the part annotations are " << endl;
cout << "labeled as you expect." << endl;
}
return best_assignment;
}
// ----------------------------------------------------------------------------------------
std::map<string,dpoint> normalized_parts (
const image_dataset_metadata::box& b
)
{
auto tform = dlib::impl::normalizing_tform(b.rect);
std::map<string,dpoint> temp;
for (auto& p : b.parts)
temp[p.first] = tform(p.second);
return temp;
}
// ----------------------------------------------------------------------------------------
std::map<string,dpoint> average_parts (
const image_dataset_metadata::dataset& data
)
/*!
ensures
- returns the average part layout over all objects in data. This is done by
centering the parts inside their rects and then averaging all the objects.
!*/
{
std::map<string,dpoint> psum;
std::map<string,double> pcnt;
for (auto& image : data.images)
{
for (auto& box : image.boxes)
{
for (auto& p : normalized_parts(box))
{
psum[p.first] += p.second;
pcnt[p.first] += 1;
}
}
}
// make into an average
for (auto& p : psum)
p.second /= pcnt[p.first];
return psum;
}
// ----------------------------------------------------------------------------------------
void make_part_labeling_match_target_dataset (
const image_dataset_metadata::dataset& target,
image_dataset_metadata::dataset& data
)
/*!
This function tries to adjust the part labels in data so that the average part layout
in data is the same as target, according to the string labels. Therefore, it doesn't
adjust part positions, instead it changes the string labels on the parts to achieve
this. This really only makes sense when you flipped a dataset that contains left-right
symmetric objects and you want to remap the part labels of the flipped data so that
they match the unflipped data's annotation scheme.
!*/
{
auto target_parts = average_parts(target);
auto data_parts = average_parts(data);
// Convert to a form align_points() understands. We also need to keep track of the
// labels for later.
std::vector<dpoint> from, to;
std::vector<string> from_labels, to_labels;
for (auto& p : target_parts)
{
from_labels.emplace_back(p.first);
from.emplace_back(p.second);
}
for (auto& p : data_parts)
{
to_labels.emplace_back(p.first);
to.emplace_back(p.second);
}
auto assignment = align_points(from, to);
// so now we know that from_labels[i] should replace to_labels[assignment[i]]
std::map<string,string> label_mapping;
for (size_t i = 0; i < assignment.size(); ++i)
label_mapping[to_labels[assignment[i]]] = from_labels[i];
// now apply the label mapping to the dataset
for (auto& image : data.images)
{
for (auto& box : image.boxes)
{
std::map<string,point> temp;
for (auto& p : box.parts)
temp[label_mapping[p.first]] = p.second;
box.parts = std::move(temp);
}
}
}
// ----------------------------------------------------------------------------------------
void flip_dataset(const command_line_parser& parser)
{
image_dataset_metadata::dataset metadata, orig_metadata;
string datasource;
if (parser.option("flip"))
datasource = parser.option("flip").argument();
else
datasource = parser.option("flip-basic").argument();
load_image_dataset_metadata(metadata,datasource);
orig_metadata = metadata;
const string metadata_filename = get_parent_directory(file(datasource)).full_name() +
directory::get_separator() + "flipped_" + file(datasource).name();
// Set the current directory to be the one that contains the
// metadata file. We do this because the file might contain
// file paths which are relative to this folder.
set_current_dir(get_parent_directory(file(datasource)));
array2d<rgb_pixel> img, temp;
for (unsigned long i = 0; i < metadata.images.size(); ++i)
{
file f(metadata.images[i].filename);
string filename = get_parent_directory(f).full_name() + directory::get_separator() + "flipped_" + to_png_name(f.name());
load_image(img, metadata.images[i].filename);
flip_image_left_right(img, temp);
if (parser.option("jpg"))
{
filename = to_jpg_name(filename);
save_jpeg(temp, filename,JPEG_QUALITY);
}
#ifdef DLIB_JXL_SUPPORT
else if (parser.option("jxl"))
{
filename = to_jxl_name(filename);
const float jxl_quality = std::stof(parser.option("jxl").argument());
save_webp(temp, filename, jxl_quality);
}
#endif
#ifdef DLIB_WEBP_SUPPORT
else if (parser.option("webp"))
{
filename = to_webp_name(filename);
const float webp_quality = std::stof(parser.option("webp").argument());
save_webp(temp, filename, webp_quality);
}
#endif
else
{
save_png(temp, filename);
}
for (unsigned long j = 0; j < metadata.images[i].boxes.size(); ++j)
{
metadata.images[i].boxes[j].rect = impl::flip_rect_left_right(metadata.images[i].boxes[j].rect, get_rect(img));
// flip all the object parts
for (auto& part : metadata.images[i].boxes[j].parts)
{
part.second = impl::flip_rect_left_right(rectangle(part.second,part.second), get_rect(img)).tl_corner();
}
}
metadata.images[i].filename = filename;
}
if (!parser.option("flip-basic"))
make_part_labeling_match_target_dataset(orig_metadata, metadata);
save_image_dataset_metadata(metadata, metadata_filename);
}
// ----------------------------------------------------------------------------------------
|