void MainWindow::createActions() { openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); openAct->setShortcuts(QKeySequence::Open); openAct->setStatusTip(tr("Open an existing file")); connect(openAct, SIGNAL(triggered()), this, SLOT(open())); exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setStatusTip(tr("Exit the application")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); edgeAct = new QAction(tr("edge"),this); edgeAct->setStatusTip(tr("edge")); connect(edgeAct, SIGNAL(triggered()), this, SLOT(edge())); mhiAct = new QAction(tr("mhi"),this); mhiAct->setStatusTip(tr("mhi")); connect(mhiAct, SIGNAL(triggered()), this, SLOT(mhi())); codebookAct = new QAction(tr("codebook"),this); codebookAct->setStatusTip(tr("codebook")); connect(codebookAct, SIGNAL(triggered()), this, SLOT(codebook())); aboutAct = new QAction(tr("&About"), this); aboutAct->setStatusTip(tr("Show the application's About box")); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); aboutQtAct = new QAction(tr("About &Qt"), this); aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); }
mat kmeans(Array<vec> &DB, int SIZE, int NOITER, bool VERBOSE) { int DIM = DB(0).length(), T = DB.length(); mat codebook(DIM, SIZE); int n, i, j; double D, Dold; ivec ind(SIZE); for (i = 0;i < SIZE;i++) { ind(i) = randi(0, T - 1); j = 0; while (j < i) { if (ind(j) == ind(i)) { ind(i) = randi(0, T - 1); j = 0; } j++; } codebook.set_col(i, DB(ind(i))); } if (VERBOSE) std::cout << "Training VQ..." << std::endl ; D = 1E20; for (n = 0;n < NOITER;n++) { Dold = D; D = kmeansiter(DB, codebook); if (VERBOSE) std::cout << n << ": " << D / T << " "; if (std::abs((D - Dold) / D) < 1e-4) break; } return codebook; }
mat vqtrain(Array<vec> &DB, int SIZE, int NOITER, double STARTSTEP, bool VERBOSE) { int DIM = DB(0).length(); vec x; vec codebook(DIM*SIZE); int n, MinIndex, i, j; double MinS, S, D, step, *xp, *cp; for (i = 0;i < SIZE;i++) { codebook.replace_mid(i*DIM, DB(randi(0, DB.length() - 1))); } if (VERBOSE) std::cout << "Training VQ..." << std::endl ; res: D = 0; for (n = 0;n < NOITER;n++) { step = STARTSTEP * (1.0 - double(n) / NOITER); if (step < 0) step = 0; x = DB(randi(0, DB.length() - 1)); // seems unnecessary! Check it up. xp = x._data(); MinS = 1E20; MinIndex = 0; for (i = 0;i < SIZE;i++) { cp = &codebook(i * DIM); S = sqr(xp[0] - cp[0]); for (j = 1;j < DIM;j++) { S += sqr(xp[j] - cp[j]); if (S >= MinS) goto sune; } MinS = S; MinIndex = i; sune: i = i; } D += MinS; cp = &codebook(MinIndex * DIM); for (j = 0;j < DIM;j++) { cp[j] += step * (xp[j] - cp[j]); } if ((n % 20000 == 0) && (n > 1)) { if (VERBOSE) std::cout << n << ": " << D / 20000 << " "; D = 0; } } // checking training result vec dist(SIZE), num(SIZE); dist.clear(); num.clear(); for (n = 0;n < DB.length();n++) { x = DB(n); xp = x._data(); MinS = 1E20; MinIndex = 0; for (i = 0;i < SIZE;i++) { cp = &codebook(i * DIM); S = sqr(xp[0] - cp[0]); for (j = 1;j < DIM;j++) { S += sqr(xp[j] - cp[j]); if (S >= MinS) goto sune2; } MinS = S; MinIndex = i; sune2: i = i; } dist(MinIndex) += MinS; num(MinIndex) += 1; } dist = 10 * log10(dist * dist.length() / sum(dist)); if (VERBOSE) std::cout << std::endl << "Distortion contribution: " << dist << std::endl ; if (VERBOSE) std::cout << "Num spread: " << num / DB.length()*100 << " %" << std::endl << std::endl ; if (min(dist) < -30) { std::cout << "Points without entries! Retraining" << std::endl ; j = min_index(dist); i = max_index(num); codebook.replace_mid(j*DIM, codebook.mid(i*DIM, DIM)); goto res; } mat cb(DIM, SIZE); for (i = 0;i < SIZE;i++) { cb.set_col(i, codebook.mid(i*DIM, DIM)); } return cb; }
int main() { // load codebook cout << "load codebook ... ..." << endl; string cbpath = "data/cluster/clst.npy"; CvMLData mlData; mlData.read_csv(cbpath.c_str()); Mat codebook(mlData.get_values()); // load flann cout << "build flann ... ... " << endl; string flannpath = "data/cache/flann.index"; cv::flann::IndexParams *indexParams; cv::flann::Index *flannIndex; if (exists(flannpath)) { indexParams = new cv::flann::SavedIndexParams(flannpath); flannIndex = new cv::flann::Index(codebook, *indexParams); } else { indexParams = new cv::flann::AutotunedIndexParams(); flannIndex = new cv::flann::Index(codebook, *indexParams); flannIndex->save(flannpath); } // create the inverted index vector<string> siftpaths = readlines("data/featlist"); string savepath = "data/cache/ivindex.txt"; if (!exists(savepath)) { cerr << "index file doesn't exist ... ..." << endl; exit(-1); } IvIndex ivindex(savepath, codebook, siftpaths.size()); // prepare the context and sockets zmq::context_t context(1); zmq::socket_t socket(context, ZMQ_REP); socket.bind("tcp://*:5555"); cout << "start to receive request ... ..." << endl; // query while (true) { // Wait for next request from client string recvStr = s_recv(socket); // ???? transform recvStr ukbench00000.th.jpg to data/Images/ukbench00000.jpg.sift char path[128]; sprintf(path, "data/Images/ukbench%s.jpg.sift", recvStr.substr(7, 5).c_str()); cout << "process request: " << recvStr << endl; vector<size_t> ret = ivindex.score(path, 15, flannIndex); sleep(1); // Send reply back to client string reply; for (auto x: ret) { reply += to_string(x) + " "; } s_send(socket, reply); } delete indexParams; indexParams = nullptr; delete flannIndex; flannIndex = nullptr; return 0; }