Showing posts with label Computer Vision. Show all posts
Showing posts with label Computer Vision. Show all posts

Sunday, December 25, 2016

Verifying Convolution Theorem on 2D Images (MATLAB Code)


The objective of this post is to verify the convolution theorem on 2D images. I will follow a practical verification based on experiments.

In mathematics, the convolution theorem states that under suitable conditions the Fourier transform of a convolution is the pointwise product of Fourier transforms. In other words, convolution in one domain (e.g., time domain) equals point-wise multiplication in the other domain (e.g., frequency domain).
Figure 1. Convolution Theorem

Fourier transform (FT) calculates the frequency domain representation of a spacial domain signal, while inverse Fourier transform (IFT) does the opposite; given the frequency domain representation of a signal, it calculates the spacial domain representation of it.

Figure 2. Fourier Transform and Inverse Fourier Transfrom

Convolution theorem is not only valid for 1D signals, but also 2D signals. This makes it very useful for many image processing and computer vision applications. Figure 3 shows two example images in spacial domain ( we are used to see images in that domain :) ) and the corresponding representation in frequency domain.
Figure 3. Examples for 2 images in spacial and frequency domains

In our verification experiment, we will apply it to three different images with different level of details: high, medium, and low details. The reason will be clear at the end of this post.
Firstly, let's create a black image W of the same size of I with a small white rectangle in the middle. 
For the image I, firstly, we will get the result of multiplying the frequency domain of I by W:
  1. Apply Fourier transform on I, let the result be F
  2. Calculate the point-wise multiplication of F and W, let the result be F×W
  3. Get the inverse Fourier transform of F×W, let this result be R1
Now, let's do the inverse process, for the same image I, we will get the result of the convolution of the spacial domain of I and the inverse Fourier transform of W.
  1. Apply inverse Fourier transform on W, let the result be M
  2. Calculate the convolution of I and M, let the result be R2
As described earlier, the convolution theorem establish that the two processes described above to get R1 and R2 are equivalent; so R1 and R2 should be the same images at the end. If we see that, we verify the convolution theorem on 2D images.
In the first process, we point-wise multiply the input image frequency domain representation by a black image with a small white rectangle in the middle. We eliminate high frequencies and keep low frequencies. So, the spacial domain of the resulted image should be a blurred version image of the input image, because high frequencies in that input image are eliminated.
The smaller the white rectangle in the middle of W is, the more frequencies we remove, and hence, the more blurred image we get. The following figures 4 and 5 show R1 and R2 results for an input image of high detail. (It's form Microsoft PhD Summer School 2015 that I was invited for in Cambridge University.). As it's clear from the figures R1 and R2 are equivalent, which verifies the convolution theorem in 2D. In both results, the white rectangle of W dimensions are 30% of image dimensions.

Figure 4. R1 for a high detail image, the white rectangle of W dimensions are 30% of image sizes

Figure 5. R2 for a high detail image, the white rectangle of W dimensions are 30% of image sizes

For the same high detail image, if we visualize R1 and R2 but when the white rectangle of W is smaller to have dimensions of 10% of image dimensions instead of 30%. The resulted image becomes more blurred as expected as shown in figures 6 and 7. When the size of the white rectangle W is larger, the level of detail preserved in R1 and R2 increases, i.e., more high frequency components of I are preserved. 

Figure 6. R1 for a high detail image, the white rectangle of W dimensions are 10% of image dimensions

Figure 7. R2 for a high detail image, the white rectangle of W dimensions are 10% of image dimensions

For the white rectangle of W dimensions are 30% of image dimensions, let's use an input image I of low detail already instead of the high detail image we used above. The result is shown in Figure 8.

Figure 8. R1 for a low detail image, the white rectangle of W dimensions are 30% of image dimensions

As we can see, using the same W, the result in figure 8 looks less blurred than the result in figure 4. Because in figure 8 experiment, the input image contains less details already. For the same white rectangle W size, the image of high detail losses more information than the low detail image (i.e., looks more blurred). The high frequency components of a highly detailed image contain more information than it in a less detailed image; this is why the highly detailed image loses more information (and looks more blurred) when the same pass filter is applied to both images.

Please note that the obtained spacial domain of W represents the known image low pass filter everyone uses. Also, my MATLAB code in MathWorks FileExchange website conducts more experiments than I've presented here. Figure 9 shows last example for the result using a moderate level of detail image as input.

Figure 9. R2 for a medium detail image, the white rectangle of W dimensions are 10% of image dimensions

Sunday, August 28, 2016

Ranked top 5% percent in Kaggle Distracted Driver Competition

Kaggle State Farm Distracted Driver Detection competition has just ended, and I ranked within top 5% (64th out of 1450 participating teams, winner's got $65,000).My approach is mainly based on Deep Learning (trained 20 very deep models) but still applies Computer Vision strategies to reduce neural network distraction.A brief description about the system is in the image below:
References:[1] Rajen Bhatt, Abhinav Dhall, 'Skin Segmentation Dataset', UCI Machine Learning Repository.
[2] X. Zhu, D. Ramanan. "Face detection, pose estimation and landmark localization in the wild" Computer Vision and Pattern Recognition (CVPR) Providence, Rhode Island, June 2012.
[3] Simonyan, Karen, and Andrew Zisserman. "Very deep convolutional networks for large-scale image recognition." arXiv preprint arXiv:1409.1556 (2014).

Notes:
  • The competition was very challenging, we did not do some costly annotations nor used test data in any form of learning (even semi-supervised) nor annotation.
    We treat images independently, while some participants learn from test data and take advantage of the fact that test images are originally sampled from recorded videos. So, they do some sort of test videos reconstruction and hence image classification makes use of temporal context.
    Also, some participants annotate their training data and some crowdsources the annotation. Which is either too much work or needs money.
    Our system is more general than such systems, even that they are doing better than ours in leaderboard; they are indirectly over-fitting the competition test data.
  • The average loss metric used in competition leaderboard ranking doesn't directly reflect the system accuracy. I believe all top 100 systems classification accuracies are higher than 99%, but the loss metric reflects how were you confident in your classification, which is harder. Only a single misclassification with high confidence, will give a very bad average loss.
  • Face detection for such problem is hard, the well-known Haar Cascades surely fail. The example in the image above is easy, but normally driver face is seen from side.
  • I've tried lots of strategies for upper body Human Pose Estimation (Calvin) and scene/driver segmentation (this and this) but didn't achieve good results.
  • I used data augmentation. Because the training data size is not large.
  • The camera is not calibrated, and changes orientation. The system should be intelligent enough to handle this.
  • I know many interesting details and results (like the following image visualizing the most important area that affected network decision) are missing here, but I will be happy to answer any of your questions about any details.