Matlab comes built in with a profiler. It is much akin to the profiler in valgrind, only that in true matlab fashion it generates a html report of the profiler.
Profiling can be used to look at specfic runs of functions and the call tree, it generates statistics like the number of times a function was called and how much time a function takes to execute.
This kind of information can provide key insight into blockages, especially on the IO front. For example, in C++ it is better to use "\n" than endl to mark end of lines.endl flushes the buffer and adds consdierable overhead. In matlab, you don't figure out why some functions are slow, you can use profiling to pinpoint the exact line causing the bottleneck. This can be crucial especially if you deal with big datasets and/or heavy computation.
So, if you now have code which you want to profile and say save the stats and measure, simply run the following commands and it will be done.
ruminations of the many wonders of programming languages (to be read as limitations, pitfalls and hindrances), mixed with the allure of data analysis, applied heavily to data extracted in many forms
Friday, June 29, 2012
Saturday, March 31, 2012
Reverse engineering the ising model
I was trying to solve this problem in image segmentation. The key idea in segmentation is to group pixels which seem spatially similar owing to properties of color, region and body. There is no one single segmentation which is correct, it is a matter of perception. Perception of images varies by individual; what I perceive in an image, you may not. Its like looking at monet or chagall, my opinions of masterpiece may be lost on you.
So, starting with this how does end up with the ising model. One starts by applying a markov random field to the image and running a max-flow on the image graph to get the segmentation. Standard fare combinatorial optimization problems some say.
My introduction to markov process is from a NLP standpoint, pretty informally again (understanding the process, and not reading a whole bunch of literature). I understood the conditional inference scheme depended only on the previous state(s) and not all the past states. With this understanding I embarked to solve this problem (maybe I needed better accoutrements, but this too shall pass).
How does one group anything? A general consensus is to put all things similar in a heap and call it a group. The question then arises, "how do you define similarity in images?". This led me down the path of wave particle duality, although we only see images as composed of particles (aka pixels). It took me a little while to understand what I was solving was not a computer science question but a physics question. I imagined there should be a force applied on the pixels to conform them to some shape, in essence changing their basic property (read color intensity value). If you think of a child grouping his toys into jedi and dark forces for a epic battle, he pushes all the dark ones into a group. The act of "pushing" is a force, which is the same concept for the image. Aha, but there is catch, you cannot move the pixels spatially like the little boy. A little confusing I agree.
So then, the story thus far looks good but we still don't have a solution. Now I can use the markov knowledge from a previous class. Imagine a bunch of patches or markers hanging in the air above the ground, and you image is painted for you by monet . For simplicity let's consider 2 patches, white and black. If we have a background and foreground in an image, lets say the white patches are attracted to the background and black patches to the foreground. Now if we can define an attraction force between the pixel and the patch we are home scott free. Herein comes the spatial arrangement problem, pixels close to each other should have the same patches. (I agree with this notion in a general sense, but not completely, occlusion and a few other factors like spatial incoherence, which can be seen in illusions make me a little uncomfortable with this generalization).
Steaming ahead with this idea, we now have 2 forces, one force between the patch (in the air) and the pixel in the image on the ground and another force between the pixels on the ground making them stick close together (I also think of a force between the patches and regions, making up a complete field but that too shall pass for now).
With this scheme of things, we can define a function to to either minimize or maximize over all the variables we have. The norm is to choose a energy function here and the general idea is to have a energy minimization or maximization,
E(x) = Ed + Es.
Here Ed is the data term and Es the smoothing term. The data term makes makes up some energy but only the pixel data and no neighborhood function. The smoothness term uses the neighborhood function and ensures we get the same labeling for pixels within the same region.
You can read more about this scheme here - http://www.cis.upenn.edu/~jshi/GraphTutorial/
So now that we have that down, we need equate the force to energy. If we assume a gravitational force, it will be attraction and we will have higher force for correct labelling and we can maximize the energy function to get a stable labeling of the pixels. On the other hand, if we consider a magnetic field, where like particles repel and unlike don't. We need to minimize the function to get a correct labeling of the images, which is the accepted norm in vision.
The concepts here are grossly oversimplified to help me understand the workings and make a intuitive sense of the process underlying the markov field. As with everything, the disclaimer, I do not know everything and I can get things wrong.
So, starting with this how does end up with the ising model. One starts by applying a markov random field to the image and running a max-flow on the image graph to get the segmentation. Standard fare combinatorial optimization problems some say.
My introduction to markov process is from a NLP standpoint, pretty informally again (understanding the process, and not reading a whole bunch of literature). I understood the conditional inference scheme depended only on the previous state(s) and not all the past states. With this understanding I embarked to solve this problem (maybe I needed better accoutrements, but this too shall pass).
How does one group anything? A general consensus is to put all things similar in a heap and call it a group. The question then arises, "how do you define similarity in images?". This led me down the path of wave particle duality, although we only see images as composed of particles (aka pixels). It took me a little while to understand what I was solving was not a computer science question but a physics question. I imagined there should be a force applied on the pixels to conform them to some shape, in essence changing their basic property (read color intensity value). If you think of a child grouping his toys into jedi and dark forces for a epic battle, he pushes all the dark ones into a group. The act of "pushing" is a force, which is the same concept for the image. Aha, but there is catch, you cannot move the pixels spatially like the little boy. A little confusing I agree.
So then, the story thus far looks good but we still don't have a solution. Now I can use the markov knowledge from a previous class. Imagine a bunch of patches or markers hanging in the air above the ground, and you image is painted for you by monet . For simplicity let's consider 2 patches, white and black. If we have a background and foreground in an image, lets say the white patches are attracted to the background and black patches to the foreground. Now if we can define an attraction force between the pixel and the patch we are home scott free. Herein comes the spatial arrangement problem, pixels close to each other should have the same patches. (I agree with this notion in a general sense, but not completely, occlusion and a few other factors like spatial incoherence, which can be seen in illusions make me a little uncomfortable with this generalization).
Steaming ahead with this idea, we now have 2 forces, one force between the patch (in the air) and the pixel in the image on the ground and another force between the pixels on the ground making them stick close together (I also think of a force between the patches and regions, making up a complete field but that too shall pass for now).
With this scheme of things, we can define a function to to either minimize or maximize over all the variables we have. The norm is to choose a energy function here and the general idea is to have a energy minimization or maximization,
E(x) = Ed + Es.
Here Ed is the data term and Es the smoothing term. The data term makes makes up some energy but only the pixel data and no neighborhood function. The smoothness term uses the neighborhood function and ensures we get the same labeling for pixels within the same region.
You can read more about this scheme here - http://www.cis.upenn.edu/~jshi/GraphTutorial/
So now that we have that down, we need equate the force to energy. If we assume a gravitational force, it will be attraction and we will have higher force for correct labelling and we can maximize the energy function to get a stable labeling of the pixels. On the other hand, if we consider a magnetic field, where like particles repel and unlike don't. We need to minimize the function to get a correct labeling of the images, which is the accepted norm in vision.
The concepts here are grossly oversimplified to help me understand the workings and make a intuitive sense of the process underlying the markov field. As with everything, the disclaimer, I do not know everything and I can get things wrong.
Sunday, February 5, 2012
Intro to vision - Images and representation
I am dabbling in computer vision and it helps if I can gather my thoughts. (This field is not as simple as I imagined it to be.) There are a lot of background and information which goes missing , and I hate reading the unabridged versions of text. So, here goes:
Vision is primarily concerned with images, video can be treated as a series of images at a certain rate or frequency. There is the 2-D and 3-D representation of images. I shall not talk about 3-D since my knowledge on that is limited at best.
What does 2-D representation mean? Backtracking a little, every image is represented by a series of pixels, these pixels store some information. In a 2-D representation, the pixels are spread across 2 dimensions, lets say X and Y (for convenience). This is generally represented in matrix form, where the top-left pixel of the image is the top-left value in the matrix.
What are these pixel thingies? These "pixels" can hold different types of information. One common type in vision is the RGB space, where we represent the color space of the image with the composites of red, blue and green. Each of these are called the channels of the color space. Ideally, we use 8 bits for each color, thereby 2^8 = 255 possible values for each component. You can also represent the image using 18-bit color, 32-bit color and 48-bit color. These various color spaces correspond to different color depths. TrueColor is the 24-bit color, with each channel RGB getting 8 bits.
There are also CMYK, sRGB, scRGB color spaces which are used in HDR (photo enthusiasts alert), but vision folks generally deal with RGB or BGR(reverse RGB representation) color spaces.
For more on the color representation look here and dig up more - http://en.wikipedia.org/wiki/Color_depth
Although this color representation is great it does not give us an intuitive idea of color. If for example I want orange, its R=255,G=142,B=13. If I want a darker orange its R=210,G=65,B=0. So, it becomes hard to play with these colors and we use the HSI (Hue, Saturation and Intensity model). This is seen in displays and color pickers across the digital image applications. We can easily get a darker or lighter color by varying the saturation making our lives much easier.
It is possible to convert from one space to another, and most libraries have api's for easy conversion.
So, if we see a image, we can imagine it as a matrix containing 24bits in each matrix cell to represent a pixel on the image.
Vision is primarily concerned with images, video can be treated as a series of images at a certain rate or frequency. There is the 2-D and 3-D representation of images. I shall not talk about 3-D since my knowledge on that is limited at best.
What does 2-D representation mean? Backtracking a little, every image is represented by a series of pixels, these pixels store some information. In a 2-D representation, the pixels are spread across 2 dimensions, lets say X and Y (for convenience). This is generally represented in matrix form, where the top-left pixel of the image is the top-left value in the matrix.
What are these pixel thingies? These "pixels" can hold different types of information. One common type in vision is the RGB space, where we represent the color space of the image with the composites of red, blue and green. Each of these are called the channels of the color space. Ideally, we use 8 bits for each color, thereby 2^8 = 255 possible values for each component. You can also represent the image using 18-bit color, 32-bit color and 48-bit color. These various color spaces correspond to different color depths. TrueColor is the 24-bit color, with each channel RGB getting 8 bits.
There are also CMYK, sRGB, scRGB color spaces which are used in HDR (photo enthusiasts alert), but vision folks generally deal with RGB or BGR(reverse RGB representation) color spaces.
For more on the color representation look here and dig up more - http://en.wikipedia.org/wiki/Color_depth
Although this color representation is great it does not give us an intuitive idea of color. If for example I want orange, its R=255,G=142,B=13. If I want a darker orange its R=210,G=65,B=0. So, it becomes hard to play with these colors and we use the HSI (Hue, Saturation and Intensity model). This is seen in displays and color pickers across the digital image applications. We can easily get a darker or lighter color by varying the saturation making our lives much easier.
It is possible to convert from one space to another, and most libraries have api's for easy conversion.
So, if we see a image, we can imagine it as a matrix containing 24bits in each matrix cell to represent a pixel on the image.
Saturday, January 21, 2012
Mobius Strip
The mobius strip is a wonderful geometric conundrum. It perplexes you since it is a closed 3-D object with only one edge (lo and behold!). It is really easy to make one of these, take a piece of paper and twist it clockwise on one edge, counter clockwise on the other edge join the two edges and you have this quizzical geometry.
This image describes it correctly:
Why am I putting this here? Well, I have this annoying habit of setting wires and straps on bags straight. I recently ran into this mobius strip on a bag, seeing that I could not make it straight made me wince. I could not come up with a name to attach and put my frustration on. Now I can. :)
This image describes it correctly:
Why am I putting this here? Well, I have this annoying habit of setting wires and straps on bags straight. I recently ran into this mobius strip on a bag, seeing that I could not make it straight made me wince. I could not come up with a name to attach and put my frustration on. Now I can. :)
Monday, September 26, 2011
matplotlib thou art not matlab
I am a little taken aback by the matplotlib apis, their examples don't seem that clear on data visualization. Being a former matlab user, I switched to matplotlib as it gives me access to both nltk, opencv, numpy/scipy. But in hindsight, I could just use the insane matlab toolboxes for these purposes and be done with it.
Saturday, June 25, 2011
Shogun toolkit
A fellow researcher (read arun) in semantic parsing and sentiment analysis directed me towards the shogun toolbox. Amazing features; it implements a range of weighted kernels, SVM and HMMs. It has interfaces to python, matlab and R (in my order of preference). It also supports ascii, Json and xml data formats. The best part is its got about 600 examples (albeit not in pydoc and for dna data!!!); a few I read took me about 5 mins to understand. (5 mins, now to get someone more brilliant to explain everything else I need, R2-D2, scooby-doo)
So now if I am rapid prototyping some algorithm I can do a instant analysis for many SVM methods and also HMM classification in very little time. Now maybe I can prove my algorithms are seriously inefficient and need lots of work or they can kick ass with the best of methods. (I am rooting for the latter)
I know PyML is slow and trying to catch up, but if only they just used everything form shogun and wrote new wrappers in PyML form and their fantastic doc. (I do wish a lot!!)
Time to fire the infinite improb drive and head to my bed.
So now if I am rapid prototyping some algorithm I can do a instant analysis for many SVM methods and also HMM classification in very little time. Now maybe I can prove my algorithms are seriously inefficient and need lots of work or they can kick ass with the best of methods. (I am rooting for the latter)
I know PyML is slow and trying to catch up, but if only they just used everything form shogun and wrote new wrappers in PyML form and their fantastic doc. (I do wish a lot!!)
Time to fire the infinite improb drive and head to my bed.
Tuesday, June 14, 2011
Woes in data representation
I have been dabbling with numpy for a while, creating serialized objects and memory mapped files. The numpy package is great because it give s you the flexibility of using python, it is definitely not as fast as implementing the same in C, but you so have the alternative to use Cython (well if you do care).
I have written a converter for the date object, I was trying to look at time series analysis, but turns out if I am using a converter I need to return in float or string and cannot store in date object format. What a bummer!!
I don't have an idea how to use serialized objects or the parallel in matlab, the next quest learn the magical techniques in matlab.
I have written a converter for the date object, I was trying to look at time series analysis, but turns out if I am using a converter I need to return in float or string and cannot store in date object format. What a bummer!!
I don't have an idea how to use serialized objects or the parallel in matlab, the next quest learn the magical techniques in matlab.
Subscribe to:
Posts (Atom)