Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
208 lines (196 loc) · 8.27 KB
/
Copy pathmain.cpp
File metadata and controls
208 lines (196 loc) · 8.27 KB
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
// Run every signal on images.
// Build: cmake -B build && cmake --build build Usage: build/photokeeper [-j WORKERS] [--eval [MANIFEST]] photo.jpg dir ...
#include <atomic>
#include <chrono>
#include <cmath>
#include <condition_variable>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <functional>
#include <map>
#include <mutex>
#include <optional>
#include <queue>
#include <sstream>
#include <string>
#include <thread>
#include <tuple>
#include <vector>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include "pipeline_c/pipeline.h"
namespace fs = std::filesystem;
// Blocking queue that pops the smallest (image, stage) first, like Python's queue.PriorityQueue.
template <class T>
struct PriorityQueue {
std::priority_queue<T, std::vector<T>, std::greater<T>> q;
std::mutex m;
std::condition_variable cv;
void push(T v) {
{
std::lock_guard<std::mutex> lock(m);
q.push(std::move(v));
}
cv.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock(m);
cv.wait(lock, [&] { return !q.empty(); });
T v = q.top();
q.pop();
return v;
}
};
struct Task {
size_t image;
int stage; // 0 decodes paths[image]; k >= 1 runs SIGNALS[k - 1] on the decoded thumbnail below
cv::Mat bgr, gray;
bool operator>(const Task& o) const { return std::tie(image, stage) > std::tie(o.image, o.stage); }
};
struct Msg {
size_t image;
int stage; // 0 means the decode failed, the only message that image will send
double value;
bool operator>(const Msg& o) const { return std::tie(image, stage) > std::tie(o.image, o.stage); }
};
// Call on_result(index, features) as images finish, in any order. features is empty if decoding failed,
// otherwise NaN marks a signal that threw.
//
// One priority queue of (image index, stage) tasks. Loads are queued up front; an image's signal
// tasks are queued when its decode finishes and sort ahead of later loads, so decoded thumbnails
// in flight stay near the worker count no matter how many paths are given.
static void analyze_all(const std::vector<std::string>& paths, int workers,
const std::function<void(size_t, std::optional<pk::Features>)>& on_result) {
PriorityQueue<Task> tasks;
PriorityQueue<Msg> out;
std::atomic<bool> stop{false};
for (size_t i = 0; i < paths.size(); ++i) tasks.push({i, 0, {}});
auto work = [&] {
while (true) {
Task t = tasks.pop();
if (stop) return;
if (t.stage == 0) {
cv::Mat bgr, gray;
try {
bgr = pk::load(paths[t.image]);
} catch (const cv::Exception&) { // undecodable
}
if (bgr.empty()) {
out.push({t.image, 0, NAN});
continue;
}
cv::cvtColor(bgr, gray, cv::COLOR_BGR2GRAY);
for (int k = 1; k <= pk::N; ++k) tasks.push({t.image, k, bgr, gray});
} else {
double value;
try {
value = pk::SIGNALS[t.stage - 1](t.bgr, t.gray);
} catch (const cv::Exception&) { // OpenCV rejected the pixels
value = NAN;
}
out.push({t.image, t.stage, value});
}
}
};
std::vector<std::thread> threads;
for (int w = 0; w < workers; ++w) threads.emplace_back(work);
std::map<size_t, std::pair<int, pk::Features>> results; // signals received so far, values
for (size_t finished = 0; finished < paths.size();) {
Msg m = out.pop();
if (m.stage == 0) {
++finished;
on_result(m.image, std::nullopt);
continue;
}
auto& [count, features] = results[m.image];
features[m.stage - 1] = m.value;
if (++count == pk::N) {
++finished;
on_result(m.image, features);
results.erase(m.image);
}
}
stop = true; // one wake-up item per worker, each exits on stop
for (size_t w = 0; w < threads.size(); ++w) tasks.push({SIZE_MAX, 0, {}});
for (auto& t : threads) t.join();
}
// Classify every image in the manifest; print the confusion matrix plus per-source accuracy.
// Undecodable images count as 'delete'.
static void run_eval(const std::string& manifest, int workers) {
fs::path root = fs::path(manifest).parent_path();
std::ifstream in(manifest);
std::string line;
std::getline(in, line); // header: path,label,source,location
std::vector<std::array<std::string, 3>> rows; // path, label, source
while (std::getline(in, line)) {
std::stringstream ss(line);
std::array<std::string, 3> r;
for (auto& field : r) std::getline(ss, field, ','); // shortcut: no quoted fields, enough for dataset/manifest.csv
rows.push_back(r);
}
std::vector<std::string> paths;
for (const auto& r : rows) paths.push_back((root / r[0]).string());
std::map<std::pair<std::string, std::string>, int> matrix;
std::map<std::string, std::pair<int, int>> by_source; // hits, total
analyze_all(paths, workers, [&](size_t i, std::optional<pk::Features> f) {
const auto& [path, label, source] = rows[i];
std::string predicted = f && pk::keep(*f) ? "keep" : "delete";
matrix[{label, predicted}]++;
by_source[source].first += label == predicted;
by_source[source].second++;
});
std::printf("%20s %6s %7s\n", "actual \\ predicted", "keep", "delete");
for (const char* actual : {"keep", "delete"})
std::printf("%20s %6d %7d\n", actual, matrix[{actual, "keep"}], matrix[{actual, "delete"}]);
std::printf("accuracy %.3f\n", (matrix[{"keep", "keep"}] + matrix[{"delete", "delete"}]) / double(rows.size()));
for (const auto& [source, hits] : by_source) std::printf(" %-36s %.2f\n", source.c_str(), hits.first / double(hits.second));
}
static const char* USAGE =
"usage: photokeeper [-h] [-j WORKERS] [--eval [MANIFEST]] [paths ...]\n\n"
" paths image files or directories (searched recursively)\n"
" -j, --workers N worker threads (default: all cores)\n"
" --eval [CSV] run the eval instead (default: dataset/manifest.csv)\n";
int main(int argc, char** argv) {
int workers = std::thread::hardware_concurrency() ? std::thread::hardware_concurrency() : 4;
std::string manifest;
std::vector<std::string> paths;
for (int i = 1; i < argc; ++i) {
std::string a = argv[i];
if (a == "-h" || a == "--help") {
std::fputs(USAGE, stdout);
return 0;
} else if ((a == "-j" || a == "--workers") && i + 1 < argc) {
workers = std::stoi(argv[++i]);
} else if (a == "--eval") {
manifest = i + 1 < argc && argv[i + 1][0] != '-' ? argv[++i] : "dataset/manifest.csv";
} else if (a[0] == '-') {
std::fprintf(stderr, "unknown option %s\n%s", a.c_str(), USAGE);
return 2;
} else if (fs::is_directory(a)) {
std::vector<std::string> found;
for (const auto& e : fs::recursive_directory_iterator(a))
if (e.is_regular_file() && cv::haveImageReader(e.path().string())) found.push_back(e.path().string());
std::sort(found.begin(), found.end());
paths.insert(paths.end(), found.begin(), found.end());
} else {
paths.push_back(a);
}
}
if (!manifest.empty()) {
run_eval(manifest, workers);
return 0;
}
auto t0 = std::chrono::steady_clock::now();
analyze_all(paths, workers, [&](size_t i, std::optional<pk::Features> f) {
if (!f) {
std::printf("error\t%s\tcannot decode\n", paths[i].c_str());
return;
}
std::string line = (pk::keep(*f) ? "keep\t" : "delete\t") + paths[i];
for (int k = 0; k < pk::N; ++k) line += (k ? " " : "\t") + std::string(pk::SIGNAL_NAMES[k]) + "=" + std::to_string((*f)[k]);
std::puts(line.c_str());
});
double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
std::printf("%zu images, %d workers: %.0fms total, %.1fms/img\n", paths.size(), workers, ms, ms / std::max<size_t>(paths.size(), 1));
}