removed --extract-chips

This commit is contained in:
Davis King 2016-10-16 16:19:24 -04:00
parent 968322bcf8
commit f8fcfcd0b4
1 changed files with 1 additions and 183 deletions

View File

@ -404,174 +404,6 @@ void rotate_dataset(const command_line_parser& parser)
// ----------------------------------------------------------------------------------------
int extract_chips (const command_line_parser& parser)
{
if (parser.number_of_arguments() != 1)
{
cerr << "The --extract-chips option requires you to give one XML file on the command line." << endl;
return EXIT_FAILURE;
}
const size_t obj_size = get_option(parser,"extract-chips",100*100);
dlib::image_dataset_metadata::dataset data;
load_image_dataset_metadata(data, parser[0]);
// figure out the average box size so we can make all the chips have the same exact
// dimensions
running_stats<double> rs;
for (auto&& img : data.images)
{
for (auto&& box : img.boxes)
{
if (box.rect.height() != 0)
rs.add(box.rect.width()/(double)box.rect.height());
}
}
if (rs.current_n() == 0)
{
cerr << "Dataset doesn't contain any non-empty and non-ignored boxes!" << endl;
return EXIT_FAILURE;
}
const double aspect_ratio = rs.mean();
const double dobj_nr = std::sqrt(obj_size/aspect_ratio);
const double dobj_nc = obj_size/dobj_nr;
const chip_dims cdims(std::round(dobj_nr), std::round(dobj_nc));
locally_change_current_dir chdir(get_parent_directory(file(parser[0])));
cout << "Writing image chips to image_chips.dat. It is a file containing serialized images" << endl;
cout << "Written like this: " << endl;
cout << " ofstream fout(\"image_chips.dat\", ios::bianry); " << endl;
cout << " bool is_not_background; " << endl;
cout << " array2d<rgb_pixel> the_image_chip; " << endl;
cout << " while(more images) { " << endl;
cout << " ... load chip ... " << endl;
cout << " serialize(is_not_background, fout);" << endl;
cout << " serialize(the_image_chip, fout);" << endl;
cout << " }" << endl;
cout << endl;
ofstream fout("image_chips.dat", ios::binary);
dlib::rand rnd;
unsigned long count = 0;
console_progress_indicator pbar(data.images.size());
for (unsigned long i = 0; i < data.images.size(); ++i)
{
// don't even bother loading images that don't have objects.
if (data.images[i].boxes.size() == 0)
continue;
pbar.print_status(i);
array2d<rgb_pixel> img, chip;
load_image(img, data.images[i].filename);
std::vector<chip_details> chips;
std::vector<rectangle> used_rects;
for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
{
const rectangle rect = set_aspect_ratio(data.images[i].boxes[j].rect, aspect_ratio);
used_rects.push_back(rect);
if (data.images[i].boxes[j].ignore)
continue;
chips.push_back(chip_details(rect, cdims));
chips.push_back(chip_details(rect, cdims, 25*pi/180));
chips.push_back(chip_details(rect, cdims, -25*pi/180));
}
const auto num_good_chps = chips.size();
// now grab overlapping boxes that are just off enough to be negatives
for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
{
if (data.images[i].boxes[j].ignore)
continue;
const rectangle rect = set_aspect_ratio(data.images[i].boxes[j].rect, aspect_ratio);
rectangle r1 = centered_rect(rect, ceil(rect.width()*sqrt_2), ceil(rect.height()*sqrt_2));
rectangle r2 = centered_rect(rect, rect.width()/sqrt_2, rect.height()/sqrt_2);
// Corner rectangles that are inside the box.
rectangle r3 = rectangle(rect.tl_corner(), rect.tl_corner() + point(r2.width(),r2.height()));
rectangle r4 = rectangle(rect.tr_corner(), rect.tr_corner() + point(-(long)r2.width(),r2.height()));
rectangle r5 = rectangle(rect.bl_corner(), rect.bl_corner() + point(r2.width(),-(long)r2.height()));
rectangle r6 = rectangle(rect.br_corner(), rect.br_corner() + point(-(long)r2.width(),-(long)r2.height()));
// Corner rectangles that are outside the box.
rectangle r7 = rectangle(rect.tl_corner(), rect.tl_corner() + point(r1.width(),r1.height()));
rectangle r8 = rectangle(rect.tr_corner(), rect.tr_corner() + point(-(long)r1.width(),r1.height()));
rectangle r9 = rectangle(rect.bl_corner(), rect.bl_corner() + point(r1.width(),-(long)r1.height()));
rectangle r10 = rectangle(rect.br_corner(), rect.br_corner() + point(-(long)r1.width(),-(long)r1.height()));
used_rects.push_back(r1); chips.push_back(chip_details(r1, cdims));
used_rects.push_back(r2); chips.push_back(chip_details(r2, cdims));
used_rects.push_back(r3); chips.push_back(chip_details(r3, cdims));
used_rects.push_back(r4); chips.push_back(chip_details(r4, cdims));
used_rects.push_back(r5); chips.push_back(chip_details(r5, cdims));
used_rects.push_back(r6); chips.push_back(chip_details(r6, cdims));
used_rects.push_back(r7); chips.push_back(chip_details(r7, cdims));
used_rects.push_back(r8); chips.push_back(chip_details(r8, cdims));
used_rects.push_back(r9); chips.push_back(chip_details(r9, cdims));
used_rects.push_back(r10); chips.push_back(chip_details(r10, cdims));
}
// Now grab some bad chips, being careful not to grab things that overlap with
// annotated boxes in the dataset.
for (unsigned long j = 0; j < num_good_chps*6; ++j)
{
// pick two random points that make a box of the correct aspect ratio
// pick a point so that our rectangle will fit within the
point p1(rnd.get_random_32bit_number()%img.nc(), rnd.get_random_32bit_number()%img.nr());
// make the random box between 0.5 and 1.5 times the size of the truth boxes.
double box_size = rnd.get_random_double() + 0.5;
point p2 = p1 + point(dobj_nc*box_size, dobj_nr*box_size);
rectangle rect(p1,p2);
if (overlaps_any_box(used_rects, rect) || !get_rect(img).contains(rect))
continue;
used_rects.push_back(rect);
if (rnd.get_random_double() > 0.5)
{
chips.push_back(chip_details(rect, cdims));
}
else
{
double angle = (rnd.get_random_double()*2-1) * 25*pi/180;
chips.push_back(chip_details(rect, cdims, angle));
}
}
// now save these chips to disk.
dlib::array<array2d<rgb_pixel>> image_chips;
extract_image_chips(img, chips, image_chips);
bool is_not_background = true;
unsigned long j;
for (j = 0; j < num_good_chps; ++j)
{
serialize(is_not_background, fout);
serialize(image_chips[j], fout);
}
is_not_background = false;
for (; j < image_chips.size(); ++j)
{
serialize(is_not_background, fout);
serialize(image_chips[j], fout);
}
count += image_chips.size();
}
cout << "\nSaved " << count << " chips." << endl;
return EXIT_SUCCESS;
}
// ----------------------------------------------------------------------------------------
int resample_dataset(const command_line_parser& parser)
{
if (parser.number_of_arguments() != 1)
@ -803,15 +635,13 @@ int main(int argc, char** argv)
parser.add_option("min-object-size", "When doing --resample, skip objects that have fewer than <arg> pixels in them (default 1).",1);
parser.add_option("crop-size", "When doing --resample, the entire cropped image will be <arg> times wider than the object (default 2.5).",1);
parser.add_option("one-object-per-image", "When doing --resample, only include one non-ignored object per image (i.e. the central object).");
parser.add_option("extract-chips", "Crops out images with tight bounding boxes around each object. Also crops out "
"many background chips. All these image chips are serialized into one big data file. The chips will contain <arg> pixels each.",1);
parser.parse(argc, argv);
const char* singles[] = {"h","c","r","l","files","convert","parts","rmdiff", "rmtrunc", "rmdupes", "seed", "shuffle", "split", "add",
"flip", "rotate", "tile", "size", "cluster", "resample", "extract-chips", "min-object-size", "rmempty",
"flip", "rotate", "tile", "size", "cluster", "resample", "min-object-size", "rmempty",
"crop-size", "cropped-object-size", "rmlabel", "rm-if-overlaps", "sort-num-objects", "one-object-per-image", "jpg"};
parser.check_one_time_options(singles);
const char* c_sub_ops[] = {"r", "convert"};
@ -838,7 +668,6 @@ int main(int argc, char** argv)
parser.check_incompatible_options("c", "tile");
parser.check_incompatible_options("c", "cluster");
parser.check_incompatible_options("c", "resample");
parser.check_incompatible_options("c", "extract-chips");
parser.check_incompatible_options("l", "rename");
parser.check_incompatible_options("l", "ignore");
parser.check_incompatible_options("l", "add");
@ -858,16 +687,12 @@ int main(int argc, char** argv)
parser.check_incompatible_options("rotate", "tile");
parser.check_incompatible_options("cluster", "tile");
parser.check_incompatible_options("resample", "tile");
parser.check_incompatible_options("extract-chips", "tile");
parser.check_incompatible_options("flip", "cluster");
parser.check_incompatible_options("rotate", "cluster");
parser.check_incompatible_options("add", "cluster");
parser.check_incompatible_options("flip", "resample");
parser.check_incompatible_options("rotate", "resample");
parser.check_incompatible_options("add", "resample");
parser.check_incompatible_options("flip", "extract-chips");
parser.check_incompatible_options("rotate", "extract-chips");
parser.check_incompatible_options("add", "extract-chips");
parser.check_incompatible_options("shuffle", "tile");
parser.check_incompatible_options("sort-num-objects", "tile");
parser.check_incompatible_options("convert", "l");
@ -877,7 +702,6 @@ int main(int argc, char** argv)
parser.check_incompatible_options("convert", "parts");
parser.check_incompatible_options("convert", "cluster");
parser.check_incompatible_options("convert", "resample");
parser.check_incompatible_options("convert", "extract-chips");
parser.check_incompatible_options("rmdiff", "rename");
parser.check_incompatible_options("rmdiff", "ignore");
parser.check_incompatible_options("rmempty", "ignore");
@ -895,7 +719,6 @@ int main(int argc, char** argv)
parser.check_option_arg_range("cluster", 2, 999);
parser.check_option_arg_range("rotate", -360, 360);
parser.check_option_arg_range("size", 10*10, 1000*1000);
parser.check_option_arg_range("extract-chips", 4, 1000*1000);
parser.check_option_arg_range("min-object-size", 1, 10000*10000);
parser.check_option_arg_range("cropped-object-size", 4, 10000*10000);
parser.check_option_arg_range("crop-size", 1.0, 100.0);
@ -950,11 +773,6 @@ int main(int argc, char** argv)
return resample_dataset(parser);
}
if (parser.option("extract-chips"))
{
return extract_chips(parser);
}
if (parser.option("c"))
{
if (parser.option("convert"))