Skip to content

Commit

Permalink
remove boost from kmeans.h
Browse files Browse the repository at this point in the history
Now boost went from our code
  • Loading branch information
zddhub committed Apr 10, 2017
1 parent f017ac2 commit c46dd30
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ install:
- sudo add-apt-repository --yes ppa:lttng/daily
- sudo add-apt-repository ppa:amarburg/opencv3 -y # opencv3 ppa
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
- sudo add-apt-repository ppa:boost-latest -y
- sudo apt-get update -qq
- sudo apt-get install -qq libopencv3-dev
- sudo apt-get install -qq libboost-all-dev
- sudo apt-get install g++-4.8 -y
- export CXX="g++-4.8"
script:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include_directories(..)
include_directories(${PROJECT_SOURCE_DIR}/sse)

link_directories(/usr/local/lib)
set(REQUIRED_LIB opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui opencv_features2d opencv_ml boost_thread boost_system)
set(REQUIRED_LIB opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui opencv_features2d opencv_ml)

add_subdirectory(sse)

Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ MAINTAINER zdd <zddhub@gmail.com>

RUN apt-get update \
&& apt-get install g++ -y \
&& apt-get install cmake -y \
&& apt-get install libboost-all-dev -y
&& apt-get install cmake -y

RUN mkdir opensse
COPY . opensse
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Recognize input sketch image, you can try it via [web version](http://online.ope
How to install
==============

OpenSSE library uses [OpenCV 3.0.0+](http://opencv.org/) and [boost 1.55.0](http://www.boost.org/), it's a cross-platform library.
OpenSSE core library only depends on [OpenCV 3.0.0+](http://opencv.org/), it's a cross-platform library.

You can compile and install it like below:

Expand All @@ -53,8 +53,8 @@ More detail info, go to [OpenSSE Wiki](https://github.com/zddhub/opensse/wiki)
Todo List
=========
- [x] Docker support
- [x] Remove boost for core library, done
- [ ] Speed up train steps
- [ ] Remove boost for core library, 83% done
- [ ] Recognize sketch
- [ ] Support big image set

Expand Down
2 changes: 1 addition & 1 deletion opensse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#include "sse/io/json_parser.h"
#include "sse/quantize/quantizer.h"
#include "sse/vocabulary/kmeans_init.h"
// #include "sse/vocabulary/kmeans.h"
#include "sse/vocabulary/kmeans.h"

#endif
3 changes: 1 addition & 2 deletions opensse.pri
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ INCLUDEPATH += $$PWD \
/usr/local/include

LIBS += -L/usr/local/lib/ \
-lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lopencv_features2d -lopencv_ml \
-lboost_thread -lboost_system
-lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lopencv_features2d -lopencv_ml
}

macx: {
Expand Down
2 changes: 1 addition & 1 deletion sse/opensse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#include "opensse/io/json_parser.h"
#include "opensse/quantize/quantizer.h"
#include "opensse/vocabulary/kmeans_init.h"
// #include "opensse/vocabulary/kmeans.h"
#include "opensse/vocabulary/kmeans.h"

#endif
45 changes: 23 additions & 22 deletions sse/vocabulary/kmeans.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
#include <algorithm>
#include <iostream>
#include <set>
#include <thread>
#include <mutex>

#include "../common/distance.h"
#include "../common/types.h"

#include <boost/random.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>

#include "kmeans_init.h"

namespace sse {
Expand All @@ -40,8 +37,8 @@ namespace sse {
template <class collection_t, class dist_fn>
class Kmeans
{
typedef boost::mutex mutex_t;
typedef boost::lock_guard<mutex_t> locker_t;
typedef std::mutex mutex_t;
typedef std::lock_guard<mutex_t> locker_t;

typedef typename collection_t::value_type sample_t;

Expand Down Expand Up @@ -83,8 +80,6 @@ class Kmeans
*/
void run(std::size_t maxiteration, double minchangesfraction)
{
using namespace boost;

// main iteration
std::size_t iteration = 0;
for (;;)
Expand All @@ -94,14 +89,20 @@ class Kmeans
std::size_t changes = 0;

// distribute items on clusters in parallel
thread_group pool;
// thread_group pool;
std::vector<std::thread> pools(std::thread::hardware_concurrency());
// pools.emplace_back(functor); // pass in the argument of std::thread()

std::size_t idx = 0;
mutex_t mtx;
for (std::size_t i = 0; i < thread::hardware_concurrency(); i++)
for (std::size_t i = 0; i < std::thread::hardware_concurrency(); i++)
{
pools[i] = std::thread(&Kmeans::distribute_samples, this, std::ref(idx), std::ref(changes), std::ref(mtx));
}
for (std::size_t i = 0; i < std::thread::hardware_concurrency(); i++)
{
pool.create_thread(bind(&Kmeans::distribute_samples, this, boost::ref(idx), boost::ref(changes), boost::ref(mtx)));
pools[i].join();
}
pool.join_all();

iteration++;

Expand Down Expand Up @@ -135,14 +136,13 @@ class Kmeans
{
invalid.push_back(i);
}
//std::cout << "clustersize " << i << ": " << clustersize[i] << std::endl;
}

// fix invalid centers, i.e. those with no members
while (!invalid.empty() && !valid.empty())
{
std::size_t current = invalid.back();
std::cout << "handle invalid clusters: " << invalid.size() << std::endl;
std::cout << "handle invalid clusters: " << invalid.size() << std::endl;
// compute for each valid cluster the variance
// of distances to all members and get the
// most distant member
Expand Down Expand Up @@ -176,16 +176,16 @@ std::cout << "handle invalid clusters: " << invalid.size() << std::endl;
_centers[current] = _collection[farthest[c]];
_clusters[farthest[c]] = current;

std::cout << "reassign " << current << " to sample " << farthest[c] << " of cluster " << c << std::endl;
std::cout << "reassign " << current << " to sample " << farthest[c] << " of cluster " << c << std::endl;

valid.pop_back();
invalid.pop_back();
}

std::cout << "iteration " << iteration << std::endl;
std::cout << "iteration " << iteration << std::endl;
}

std::cout << "kmeans iterations: " << iteration << std::endl;
std::cout << "kmeans iterations: " << iteration << std::endl;
}


Expand Down Expand Up @@ -231,7 +231,7 @@ std::cout << "kmeans iterations: " << iteration << std::endl;
for (std::size_t i = 0; i < lhs.size(); i++) lhs[i] /= rhs;
}

private:
private:

void distribute_samples(std::size_t& index, std::size_t& changes, mutex_t& mutex)
{
Expand All @@ -249,7 +249,9 @@ std::cout << "kmeans iterations: " << iteration << std::endl;
}

// compute distance of current point to every center
std::transform(_centers.begin(), _centers.end(), dists.begin(), boost::bind(_distfn, boost::ref(_collection[i]), boost::arg<1>()));
// std::cout <<endl << "collection[" << i << "]=" << _collection[i].size() << " " << _centers.size() <<endl;
std::vector<Vec_f32_t> collections(_centers.size(), _collection[i]);
std::transform(_centers.begin(), _centers.end(), collections.begin(), dists.begin(), _distfn);

// find the minimum distance, i.e. the nearest center
std::size_t c = std::distance(dists.begin(), std::min_element(dists.begin(), dists.end()));
Expand All @@ -263,7 +265,6 @@ std::cout << "kmeans iterations: " << iteration << std::endl;
_clusters[i] = c;
currentchanges++;
}
//if (i % 1000 == 0) std::cout << "kmeans distribute: " << i << std::endl;
}
}

Expand All @@ -279,7 +280,7 @@ std::cout << "kmeans iterations: " << iteration << std::endl;
std::vector<sample_t> _centers;
std::vector<std::size_t> _clusters;

boost::mutex _mutex;
mutex_t _mutex;
};

} //namespace sse
Expand Down
1 change: 0 additions & 1 deletion tools/vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using namespace std;

#include "opensse/opensse.h"
#include "vocabulary/kmeans.h"

using namespace sse;

Expand Down

0 comments on commit c46dd30

Please sign in to comment.