Skip to content

Conversation

yanii
Copy link

@yanii yanii commented Feb 14, 2015

Hi all,

I'm trying to add the various augmentation tricks to the Caffe code which are sorely missed at the moment, and this is my first patch towards that. Later much more involved patch for full-image cropping/scale augmentation to come.

This attempts to implement the relighting augmentation as described in the Krizhevsky supervision paper. Towards this, there is a utility to calculate both the pixelwise mean (of floating point precision) and pixelwise principal components of a given data set (e.g. LMDB).

There are some design decisions to think about - most notably how to store/load the eigenvectors/eigenvalues. I took the approach of loading these directly from the prototxt parameters, just as pixelwise mean is done, since they are not very large and this is a bit more transparent. I could also see the view that they should be stored as a separate proto file however, as the image-wise mean is done.

I really don't like the code duplication that exists in data_transformer at the moment (i.e. for the mean/croppping), etc, but I followed the present format and repeated the relighting code for each of the various data formats, but I personally think there should be some work into refactoring this in future. I recognize that due to differences in the formats this isn't that simple though.

Yani

@yanii yanii closed this Feb 14, 2015
@yanii yanii reopened this Feb 14, 2015
@yanii
Copy link
Author

yanii commented Feb 14, 2015

Travis CI build for "WITH_CUDA=false WITH_CMAKE=false" failed on BlobMathTest, which as far as I can see has no relation to my patch. Running same test on my machine with CPU only and not using CMake this test passes. Looks like numerical error:

[----------] 3 tests from BlobMathTest/0, where TypeParam = caffe::FloatCPU
[ RUN      ] BlobMathTest/0.TestSumOfSquares
src/caffe/test/test_blob.cpp:117: Failure
Value of: this->blob_->sumsq_diff()
  Actual: 16266.95
Expected: expected_sumsq * kDiffScaleFactor * kDiffScaleFactor
Which is: 16266.945
[  FAILED  ] BlobMathTest/0.TestSumOfSquares, where TypeParam = caffe::FloatCPU (1 ms)

EDIT: re-running Travis CI build passed with no changes - this test needs to be fixed.

@yanii
Copy link
Author

yanii commented Feb 15, 2015

Just discovered there is an overflow issue running the compute_image_pca util on very large datasets (i.e. imagenet training set) since it is summing over all pixels, had only used it on smaller datasets so far. Will re-implement using a numerically stable algorithm ASAP.

@shelhamer
Copy link
Member

Thanks for the augmentation!

how to store/load the eigenvectors/eigenvalues. I took the approach of loading these directly from the prototxt parameters, just as pixelwise mean is done

Good choice. It's best to bundle these in the definition for clarity.

I really don't like the code duplication that exists in data_transformer at the moment
there should be some work into refactoring this in future

No argument. The data transformer was at least a step to reduce duplication across data layers, but the data pipeline deserves attention to make it simpler and more general.

@shelhamer
Copy link
Member

Re: #1865 (comment) @jeffdonahue could you relax the EXPECT_FLOAT_EQ usage in the blob math tests? They're apparently not totally reliable on cheap / free hardware.

@yanii
Copy link
Author

yanii commented Feb 16, 2015

Wow, doing the covariance calculations on Imagenet is a numerical nightmare! I've implemented an online method for calculating mean/co-variance that works well though - the side-benefit is it only needs one pass of the dataset now, and it seems to work nicely for Imagenet. I get the following eigenvectors/values, which are around what I got when I ran the naive implementation on a 1% subset of Imagenet:

Processed 1281167 files.
Mean channel values: [103.8597331040042, 116.7835176771538, 123.6662784959614]
Channel Covariance: [5244.208369419691, 4492.485855678318, 3891.093127656954;
  4492.485855678318, 4770.908271347861, 4356.913884984975;
  3891.093127656954, 4356.913884984975, 5044.990455324214]
mean_value: 103.86
mean_value: 116.784
mean_value: 123.666
eigen_value: 13515.7
eigen_value: 1252.4
eigen_value: 292.057
eigen_vector_component: 0.582858
eigen_vector_component: 0.581974
eigen_vector_component: 0.567083
eigen_vector_component: -0.691932
eigen_vector_component: -0.0104332
eigen_vector_component: 0.721888
eigen_vector_component: -0.426036
eigen_vector_component: 0.813141
eigen_vector_component: -0.396605

There doesn't seem to be any published numbers to compare to unfortunately.

Future work might be to vectorize this code by calculating the covariance for a block of samples at a time, and combining with the online covariance according to the rule (see end of covariance section of http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Covariance) . Tried doing this already but it's tricky to get it numerically correct for an image-sized block, and didn't speed things up as much as I'd hoped. Might make more sense to just run on a smaller subset of the data however if speed is critical.

…incipal components specified in TransformParameter.

Also adds utility to calculate pixelwise principle components (eigenvectors/values) for a given dataset. Use online algorithm for calculating the mean and covariance matrix to avoid numerical issues on large datasets.
@daodaofr
Copy link

daodaofr commented Mar 8, 2015

Do you have a demo about how to use the compute_image_pca.cpp ?

@yanii
Copy link
Author

yanii commented Mar 9, 2015

Simply run it like you would the compute_image_mean util, i.e. give it the lmdb/leveldb as a parameter - that's all.

It prints out usage when you call it with no params, although it looks like it's slightly wrong at the moment, in that there is no output required (or created). The mean and PCA are simply printed to the screen.

@shelhamer shelhamer added the ES label Mar 10, 2015
@vimalthilak
Copy link

@yanii What type of accuracy improvements did you see after adding this augmentation step?

@yanii
Copy link
Author

yanii commented Apr 30, 2015

@vimalthilak sorry for the late response! Was working like crazy towards the ICCV deadline.

I haven't run with with a standard network I'm afraid - I will try to do so when I have a spare GPU and report it though. Please feel free to try it out and let me know though.

@shelhamer
Copy link
Member

Closing since the dev branch is deprecated. Please send PRs to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants