Calculate the per-pixel accuracy on a dataset whose file names are supplied as a parameter.
| 93 | |
| 94 | // Calculate the per-pixel accuracy on a dataset whose file names are supplied as a parameter. |
| 95 | double calculate_accuracy(anet_type& anet, const std::vector<image_info>& dataset) |
| 96 | { |
| 97 | int num_right = 0; |
| 98 | int num_wrong = 0; |
| 99 | |
| 100 | matrix<rgb_pixel> input_image; |
| 101 | matrix<rgb_pixel> rgb_label_image; |
| 102 | matrix<uint16_t> index_label_image; |
| 103 | matrix<uint16_t> net_output; |
| 104 | |
| 105 | for (const auto& image_info : dataset) |
| 106 | { |
| 107 | // Load the input image. |
| 108 | load_image(input_image, image_info.image_filename); |
| 109 | |
| 110 | // Load the ground-truth (RGB) labels. |
| 111 | load_image(rgb_label_image, image_info.class_label_filename); |
| 112 | |
| 113 | // Create predictions for each pixel. At this point, the type of each prediction |
| 114 | // is an index (a value between 0 and 20). Note that the net may return an image |
| 115 | // that is not exactly the same size as the input. |
| 116 | const matrix<uint16_t> temp = anet(input_image); |
| 117 | |
| 118 | // Convert the RGB values to indexes. |
| 119 | rgb_label_image_to_index_label_image(rgb_label_image, index_label_image); |
| 120 | |
| 121 | // Crop the net output to be exactly the same size as the input. |
| 122 | const chip_details chip_details( |
| 123 | centered_rect(temp.nc() / 2, temp.nr() / 2, input_image.nc(), input_image.nr()), |
| 124 | chip_dims(input_image.nr(), input_image.nc()) |
| 125 | ); |
| 126 | extract_image_chip(temp, chip_details, net_output, interpolate_nearest_neighbor()); |
| 127 | |
| 128 | const long nr = index_label_image.nr(); |
| 129 | const long nc = index_label_image.nc(); |
| 130 | |
| 131 | // Compare the predicted values to the ground-truth values. |
| 132 | for (long r = 0; r < nr; ++r) |
| 133 | { |
| 134 | for (long c = 0; c < nc; ++c) |
| 135 | { |
| 136 | const uint16_t truth = index_label_image(r, c); |
| 137 | if (truth != dlib::loss_multiclass_log_per_pixel_::label_to_ignore) |
| 138 | { |
| 139 | const uint16_t prediction = net_output(r, c); |
| 140 | if (prediction == truth) |
| 141 | { |
| 142 | ++num_right; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | ++num_wrong; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 |
no test coverage detected