Sunday, June 30, 2013

Elisp Function to Add License File at Beginning of Buffer

I wrote a small function that allows for reading a file and adding it at the beginning of the buffer. This comes in handy when you need to add license terms to all your source files.
The function can be found in my .emacs on github. There is also a fake license containing just one line for trying that function.

The call syntax is
(add-license-handling license_dir license_type)

license_dir is the directory containing the license file (defaulting to ~/.emacs.d/license) and license_type is the license that you want to use stored in the file <license_dir>/<license_type>. You will also need the functions get_default_license_dir and get_default_license_type from my .emacs file to get this to work.
If you want to set different default values, add this to your .emacs:

(setq default_license_dir "~")
(setq default_license_type "FAKE")

Monday, June 10, 2013

Polymorphic Allocators and Concepts in C++14

With the release of the first feature complete C++11 compiler gcc 4.8.1, you might consider taking a peak at the upcoming standard C++14.
Two - in my opionion - very interesting aspects are:

  1. Polymorphic Allocators - The object type should be independent of the container. This is especially important when you consider two containers whose data have been allocated with different allocators. In previous standards they are incompatible, but with C++14 they will not depend on the allocator anymore.
  2. Concepts are finally introduced in C++. Concepts are very helpful for template programming and restricting the type of the template parameters. This will make interfaces much clearer and also reduce cryptic template error messages, very helpful both for developers and users. Examples for concepts are Iterators, Containers, Numerical, etc.
Those are only two features of many to come in C++14. If you want to check out more propositions and ideas, there is a blog that dedicated four rather long posts on papers regarding C++14:
1 2 3 4

Sunday, June 9, 2013

Improving an Object Detector and Extracting Regions using Superpixels (CVPR 2013)

As usual there will be shortpapers in our journal club on Thursday. The one I picked titled "Improving an Object Detector and Extracting Regions using Superpixels" is from the upcoming CVPR and the authors are Guang Shu, Afshin Dehghan, Mubarak Shah from the Computer Vision Lab at the University of Central Florida. A pdf can be obtained here.
Their goal is to improve offline-trained object detectors such as Deformable Parts Model (DPM), that are trained generally on training data that do not neccessarily represent future test data. Variant illuminations, background and camera viewpoints will degrade detector performance. The authors' approach can be subsumed in four steps:

Initial Detection

Using a DPM detector with a low threshold \(t_d\) (design parameter) they obtain a large number of true detections and also many false alarms. Given the detector's confidence scores they classify each target positive or hard. Negative examples are obtained from background (without overlap).

Superpixels and Appearance Method

Using SLIC Superpixels each target is segmented into a given number of superpixels \(N_{sp}\)(design parameter, chosen such that each superpixel is roughly uniform in color and preserves object boundaries). Each superpixel is described by a five-dimensional feature vector containg the average CIELAB colorspace value and the average location of all contained pixels.
Using K-Means an \(M\)-word (design parameter) vocabulary is created and superpixels are aggregated into an \(M\)-bin L2-normalized histogram for each target (representation in a Bag-of-Word (BoW)-fashion).

Classification

A support vector machine (SVM) is trained for classification of hard examples, based on positive and negative examples. Hard examples with high scores will get the label positive, low scores will be negative. The SVM is retrained until all example labels stay unchanged.

Region Extraction

To get en estimate of the actual shape of the target rather than a bounding box a confidence map for each superpixel belonging to the target is calculated. First all superpixels of the negative samples are clustered into \(M_n\) (design parameter) clusters using CIELAB color features. For every superpixel of a positive example the similarity to each of the negative cluster centers is measured by
\[ W_{i,j} = exp(||Sp(i) - clst(j)||\times prior(j))\]
where \(W_{i,j}\) is the respective entry in the similarity matrix, \(Sp(i)\) is the i-th superpixel of a positive example, \(clst(j)\) is the j-th negative cluster center, \(prior(j)\) is the prior probability that a cluster belongs to the background, defined by the number of superpixels in that cluster.
The similarity matrix can be used to calculated the confidence \(Q_i\) of superpixel i belonging to the target
\[Q(i) = 1 - max_jW_{i,j}\]
Using this confidence map to form unary potentials \(\Psi(c_i|s_i)\) and pairwise edge potentials (to account for smoothness) \(\Phi(c_i,c_j|s_i,s_j)\), the energy in the sense of a Conditional Random Field (CRF) needs to be minimized, to achieve the most probable solution.
\[E=\sum_{s_i\in Sp}\Psi(c_i|s_i)+\omega\sum_{s_i,s_j\in Edge}\Phi(c_i, c_j|s_i, s_j)\]
\(\Phi\) is specified in the paper. \(c_i\) is the label for each superpixel \(s_i\). The weight between unary and 
binary terms \(\omega\) is another design parameter.

Experiments

The proposed method is compared to the original DPM approach and outperforms it on all datasets. The used values of the design parameters are specified (except for \(\omega\), but the authors do not state how they obtained them.
\[t_d = -2, N_{sp} = 100, M = 400, M_n = 200, \omega=?\]
It would be very interesting to know how problem specific or general those parameters are.

Conclusion

The paper is quite straight forward, well written and easy to understand. Few typos and misaligned images indicate that the paper's been finished just before the deadline.
When I first read the title and abstract, I was hoping for a method that did not need segmentation candidates and would not introduce many design parameters. Both assumptions were wrong (first segmentation using DPM, five design parameters), but the approach might still be interesting to the cell tracking project I am working on.

Saturday, June 8, 2013

img2term v0.6 - create awesome text output from image input

This is the day of the first release of img2term 0.6. It contains a lib for image to ascii/color escape sequences conversion upon which a command line program is based. Requirements so far are  vigra and boost::program options. The current version lacks unit tests and might need some improvement on color matching, which uses predefined terminal colors from konsole. An adaptive approach would make the output much nicer for any other terminal as the colors might vary.

I am open to new ideas, but please take notice that this is a one-man project done in free time, effort strongly depending on motivation and other occupation. Even if I really like your idea, I cannot guarantee it will be included within a day or months.

Future work

  • adaptive terminal color matching (no predefined terminal colors)
  • more functinality
  • better documentation, if neccessary
  • unit tests
  • VERY MAYBE video support

For a first start you should run img2term --help.
To give you an impression what it looks like (using konsole):



Kudos who knows why the current version is 0.6. Also kudos to those who have seen this awesome movie.

You can obtain version 0.6 on my github. For a more up-to-date version checkout the master branch.

Project Euler Problem 32

After quite a long while I did another Project Euler Problem: 32 - Pandigital products.
Quoting projecteuler:


We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39  186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

The problem is quite straight forward: For all permutations of 123456789 check if you can form a pandigital product. If so, add the product to a unique set. The first five digits of each permutation are reserved to form the two factors, the last four for the resulting product.
The STL offers all the neccessary tools: std::next_permutation and std::set. Obviously, the implementation did not aim for good design.


bool check_product(int x1, int x2, int res) {
  return (x1*x2) == res;
}

template <typename t>
int get_int_from_array(T* begin, T* end) {
  --end;
  --begin;
  int res = 0;
  for (int n = 1; end != begin; --end, n *= 10) {
    res += n*(*end);
  }
  return res;
}


void get_set_of_pandigital_products(int* begin, int* end, std::set<int>& prods) {
  int n_digits = end-begin;
  int n_max = 5;
  int x1, x2, res;
  do {
    for (int i = 0; i < n_max-1; ++ i) {
      x1 = get_int_from_array(begin, begin+i);
      x2 = get_int_from_array(begin+i, begin+n_max);
      res = get_int_from_array(begin+n_max, begin+n_digits);
      if (check_product(x1, x2, res)) {
        prods.insert(res);
      }
    }
  } while (std::next_permutation(begin, end));
}

As usual, the code can be found on github.

Bayesian Reasoning and Machine Learning

If you are interested in Machine Learning and are looking for literature, I do recommend having a look at "Bayesian Reasoning and Machine Learning" by David Barber. You can order a hardcover version from a vendor of your choice or get a free copy online at the book's website. This also includes a Machine Learning toolbox for Matlab.

Sunday, June 2, 2013

How to specify filepaths when using latex \include or \input

Just as a reminder for myself:

  • \include cannot be nested. Use \input in child documents!
  • Always specify paths relative to the root document, not relative to the child document you are using \input from!