dnn_instance_segmentation_ex.cpp 7.34 KB
Newer Older
Juha Reunanen's avatar
Juha Reunanen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
    This example shows how to do instance segmentation on an image using net pretrained
    on the PASCAL VOC2012 dataset.  For an introduction to what instance segmentation is,
    see the accompanying header file dnn_instance_segmentation_ex.h.

    Instructions how to run the example:
    1. Download the PASCAL VOC2012 data, and untar it somewhere.
       http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
    2. Build the dnn_instance_segmentation_train_ex example program.
    3. Run:
       ./dnn_instance_segmentation_train_ex /path/to/VOC2012
    4. Wait while the network is being trained.
    5. Build the dnn_instance_segmentation_ex example program.
    6. Run:
       ./dnn_instance_segmentation_ex /path/to/VOC2012-or-other-images

    An alternative to steps 2-4 above is to download a pre-trained network
19
    from here: http://dlib.net/files/instance_segmentation_voc2012net_v2.dnn
Juha Reunanen's avatar
Juha Reunanen committed
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

    It would be a good idea to become familiar with dlib's DNN tooling before reading this
    example.  So you should read dnn_introduction_ex.cpp and dnn_introduction2_ex.cpp
    before reading this example program.
*/

#include "dnn_instance_segmentation_ex.h"
#include "pascal_voc_2012.h"

#include <iostream>
#include <dlib/data_io.h>
#include <dlib/gui_widgets.h>

using namespace std;
using namespace dlib;
 
// ----------------------------------------------------------------------------------------

int main(int argc, char** argv) try
{
    if (argc != 2)
    {
        cout << "You call this program like this: " << endl;
        cout << "./dnn_instance_segmentation_train_ex /path/to/images" << endl;
        cout << endl;
        cout << "You will also need a trained '" << instance_segmentation_net_filename << "' file." << endl;
        cout << "You can either train it yourself (see example program" << endl;
        cout << "dnn_instance_segmentation_train_ex), or download a" << endl;
        cout << "copy from here: http://dlib.net/files/" << instance_segmentation_net_filename << endl;
        return 1;
    }

    // Read the file containing the trained networks from the working directory.
    det_anet_type det_net;
    std::map<std::string, seg_bnet_type> seg_nets_by_class;
    deserialize(instance_segmentation_net_filename) >> det_net >> seg_nets_by_class;

    // Show inference results in a window.
    image_window win;

    matrix<rgb_pixel> input_image;

    // Find supported image files.
    const std::vector<file> files = dlib::get_files_in_directory_tree(argv[1],
Adrià Arrufat's avatar
Adrià Arrufat committed
64
        dlib::match_endings(".jpeg .jpg .png .webp"));
Juha Reunanen's avatar
Juha Reunanen committed
65
66
67
68
69
70
71
72
73

    dlib::rand rnd;

    cout << "Found " << files.size() << " images, processing..." << endl;

    for (const file& file : files)
    {
        // Load the input image.
        load_image(input_image, file.full_name());
74
        
Juha Reunanen's avatar
Juha Reunanen committed
75
        // Find instances in the input image
76
        const auto instances = det_net(input_image);
Juha Reunanen's avatar
Juha Reunanen committed
77
78

        matrix<rgb_pixel> rgb_label_image;
79
80
        matrix<float> label_image_confidence;

Juha Reunanen's avatar
Juha Reunanen committed
81
82
83
84
85
        matrix<rgb_pixel> input_chip;

        rgb_label_image.set_size(input_image.nr(), input_image.nc());
        rgb_label_image = rgb_pixel(0, 0, 0);

86
87
88
        label_image_confidence.set_size(input_image.nr(), input_image.nc());
        label_image_confidence = 0.0;

Juha Reunanen's avatar
Juha Reunanen committed
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
        bool found_something = false;

        for (const auto& instance : instances)
        {
            if (!found_something)
            {
                cout << "Found ";
                found_something = true;
            }
            else
            {
                cout << ", ";
            }
            cout << instance.label;

            const auto cropping_rect = get_cropping_rect(instance.rect);
            const chip_details chip_details(cropping_rect, chip_dims(seg_dim, seg_dim));
            extract_image_chip(input_image, chip_details, input_chip, interpolate_bilinear());

            const auto i = seg_nets_by_class.find(instance.label);
            if (i == seg_nets_by_class.end())
            {
                // per-class segmentation net not found, so we must be using the same net for all classes
                // (see bool separate_seg_net_for_each_class in dnn_instance_segmentation_train_ex.cpp)
                DLIB_CASSERT(seg_nets_by_class.size() == 1);
                DLIB_CASSERT(seg_nets_by_class.begin()->first == "");
            }

            auto& seg_net = i != seg_nets_by_class.end()
                ? i->second // use the segmentation net trained for this class
                : seg_nets_by_class.begin()->second; // use the same segmentation net for all classes

            const auto mask = seg_net(input_chip);

            const rgb_pixel random_color(
                rnd.get_random_8bit_number(),
                rnd.get_random_8bit_number(),
                rnd.get_random_8bit_number()
            );

129
            dlib::matrix<float> resized_mask(
Juha Reunanen's avatar
Juha Reunanen committed
130
131
132
133
134
135
136
137
138
139
                static_cast<int>(chip_details.rect.height()),
                static_cast<int>(chip_details.rect.width())
            );

            dlib::resize_image(mask, resized_mask);

            for (int r = 0; r < resized_mask.nr(); ++r)
            {
                for (int c = 0; c < resized_mask.nc(); ++c)
                {
140
141
                    const auto new_confidence = resized_mask(r, c);
                    if (new_confidence > 0)
Juha Reunanen's avatar
Juha Reunanen committed
142
143
144
145
                    {
                        const auto y = chip_details.rect.top() + r;
                        const auto x = chip_details.rect.left() + c;
                        if (y >= 0 && y < rgb_label_image.nr() && x >= 0 && x < rgb_label_image.nc())
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                        {
                            auto& current_confidence = label_image_confidence(y, x);
                            if (new_confidence > current_confidence)
                            {
                                auto rgb_label = random_color;
                                const auto baseline_confidence = 5;
                                if (new_confidence < baseline_confidence)
                                {
                                    // Scale label intensity if confidence isn't high
                                    rgb_label.red   *= new_confidence / baseline_confidence;
                                    rgb_label.green *= new_confidence / baseline_confidence;
                                    rgb_label.blue  *= new_confidence / baseline_confidence;
                                }
                                rgb_label_image(y, x) = rgb_label;
                                current_confidence = new_confidence;
                            }
                        }
Juha Reunanen's avatar
Juha Reunanen committed
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
                    }
                }
            }

            const Voc2012class& voc2012_class = find_voc2012_class(
                [&instance](const Voc2012class& candidate) {
                    return candidate.classlabel == instance.label;
                }
            );

            dlib::draw_rectangle(rgb_label_image, instance.rect, voc2012_class.rgb_label, 1);
        }

        // Show the input image on the left, and the predicted RGB labels on the right.
        win.set_image(join_rows(input_image, rgb_label_image));

        if (!instances.empty())
        {
            cout << " in " << file.name() << " - hit enter to process the next image";
            cin.get();
        }
    }
}
catch(std::exception& e)
{
    cout << e.what() << endl;
}