Lena – the origin about the de facto test image

(Warning: this post might contain nudity which is inappropriate for underage audience) 

Many of you probably have seen or worked with this image sometime in your experience working with image and signal processing.

lena

It is a widely used images in the academic world and of course, has been cited or referenced countless times in all kinds of video/blog tutorials. But have you ever asked, who is this young lady in this picture and why everyone is using this picture, and even, why this image even came into the dull and male-dominated circleūüôā

1. Who is her

This lady is named Lena Söderberg who showed up in the playboy magazine back in the 1970s. That photo was taken almost 40 ~ 50 years ago!  It is actually not that hard to find some images out in the public internet of the raw magazine photo.

top-supermodels-of-computer-graphics-28-728

2. How and Why her?

In a nutshell, it was a gentlemen named Alex Sawchuck who randomly found a copy of magazine and cropped the shoulder above piece to be scanned and then used in a paper later on.

“The Original Sin

Alexander Sawchuk estimates that it was in June or July of 1973 when he, then an assistant professor of electrical engineering at the USC Signal and Image Processing Institute (SIPI), along with a graduate student and the SIPI lab manager, was hurriedly searching the lab for a good image to scan for a colleague’s conference paper. They had tired of their stock of usual test images, dull stuff dating back to television standards work in the early 1960s. They wanted something glossy to ensure good output dynamic range, and they wanted a human face. Just then, somebody happened to walk in with a recent issue of Playboy.

The engineers tore away the top third of the centerfold so they could wrap it around the drum of their Muirhead wirephoto scanner, which they had outfitted with analog-to-digital converters (one each for the red, green, and blue channels) and a Hewlett Packard 2100 minicomputer. The Muirhead had a fixed resolution of 100 lines per inch and the engineers wanted a 512 ‚úē 512 image, so they limited the scan to the top 5.12 inches of the picture, effectively cropping it at the subject‚Äôs shoulders.” – CMU

There is also a very good presentation on slideshare where you can find the origins of all the interesting stories behind some of the popular or even famous images.

Anyway, next time when you saw that image, you might infuse your research with a big more imagination after knowing the story behind it.

References:

[1] Lena Story

[2] Professional Communication Society Newsletter

[3] Lenna Wikipedia

[4] Top Model in Computer Graphics

A few Numpy Functions

Today I was taking the deep learning course from Udacity, they use numpy very often in that class and here are some notes about a few handy numpy functions that I learned.

np.arange

arange is not a typo, it is simply a function very much like built-in range function but return a ndarray instead of list, that is probably why it is called a(rray)range. Here is the source code for arange in case you are interested.

In [33]: np.arange(-1, 1, 0.5)
Out[33]: array([-1. , -0.5,  0. ,  0.5])
In [51]: np.arange(12).reshape((3,4))

Out[51]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

np.vstack

v(ertically)stack all the passed ndarray/list into a new array.

In [37]: np.vstack([[1,2], [3,4], [5,6], [7,8]])

Out[37]: 
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

At the same time, you have other sibling utility functions like hstack, concatenate ..etc. that have a very similar usage.

In [38]: np.hstack([[1,2], [3,4], [5,6]])
Out[38]: array([1, 2, 3, 4, 5, 6])

np.sum

the sum is pretty straight-forward, summing up all the numbers, however, there is one pitfall that I fell over is did not pay attention to the argument ‘axis‘. 0 means column wise and 1 means row wise.

In [53]: x

Out[53]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [54]: x.sum(0)

Out[54]: array([12, 15, 18, 21])

In [55]: x.sum(1)

Out[55]: array([ 6, 22, 38])

 

WAV – Deepdive into file format

WAV (WAVE, waveform audio file format) is one of the most popular audio file formats out there. Understanding how the format is structured, how the data is stored is the key to understanding audio. This blog post will be my study note of the file format and a deep dive into a WAV file by looking into the hex code.

Here is the wiki page of what WAV is in general, and here is another tutorial from topherlee that really mapped out the file structure of a wav file. You can download a free sample wav from a site called wavsource for later analysis. (I downloaded the about_time.wav from here).

1.View Binary File

First, we need to figure out a way to view binary files, there are so many tools out there but I found a really cool way from stackoverflow. If you open the file in VIM, it will look like this:

vimwave.png

Then you can run the command :% ! xxd, you will have a view like this:

xxd

This is not something magical about VIM, it is because there is a command in Linux called xxd¬†that most people don’t know, it is out of the scope of this post to dive into xxd, however, that online is all you need, to quit the hex view, you need to run `:% ! xxd -r` and you are good to go.

2.Understanding format header

Like most of the file format, the first few bytes are about the metadata header about the file, I found this really helpful website soundfile.sapp.org that explained the structure of the wav format. The following image shows the canonical wave file format in a simple way.

waveformat

Attached is an image that I madeūüôā that explained every byte in the WAV file.

wavdata.png

  1. 10 00 00 00 (little endian) ~ 00000010 (hex) ~ 16 (dec): subchunk 1 size
  2. 0100 (little endian) ~ 0001(hex) ~ 1: audio format PCM
  3. 0100 (little endian) ~ 0001(hex) ~ 1: channel number:
  4. 0100 (little endian) ~ 1 (dec): sample rate
  5. 0800 (little endian) ~ 8(dec): bits per sample
  6. 80 70 00 00 (little endian) ~ 28,800 (dec): subchunk 2 size

A quick summary of the all the metadatas

  • channel: 1
  • file size: 29Kbytes
  • 8 bits per sample
  • PCM audio format
  • sample rate: 11, 025 (samples per second)
  • byte rate: 11,025

A few interesting math equation to help you understand those terminologies better:

  • Bytes Rate = (Sample Rate * BitsPerSample * Channels) / 8

3. Understanding data format

First we need to refresh our freshman memory, what is a “signed short”, short is simply a two bytes representation of a number, not as a long as four bytes integer, but good enough to represent our audio data, 16bit! that is all we need. For signed short, a positive integer will be the way it is, however, for negative numbers, it will be its two’s compliment. So what is a “two compliment”? it is simply to subtract the absolute value of the negative number from the range, in this case is 2^16, i.e the signed short of -N ~ (2^16 – N).

For example, -32640, signed short will be 2^16-(32640) =32896 (decimal) = 1000,0000,1000,0000 (binary) = 8 0 8 0 (hex).

One more example, 32639, signed short will be 32639 (decimal) = 0111,1111,0111,1111 (binary) = 7 f 7 f (hex).

Now after understanding how to map a number between (-32,767, 32,768) to byte codes on the disk/memory. We can start looking into the data.

Here are some Python code to read in the WAV file and extract the data part out.

wavdatapythonLooking into the data, we can see the first 14 samples are all -32640 and then the 15th sample changed to 32639. So based on the examples above, we know the bytes representation of -32640 is 8080(hex) and 32639 is 7f7f(hex). Now we might need to revisit our VIM xxd representation of the raw WAV file. We have covered the metadata part of WAV file, and starting from the data chunk, we can clearly see ‘8080’ repeated by 14 times and then 7f7f, so on and so forth.

haha, now we understand all the nitty-gritty details about WAV, let’s take a look a data part and have a more intuitive understanding of “about_time”. The length of the data is 14400, which means 14400 samples and 16 bits each sample. In section one, we know the sample rate is about 11025 samples/second, theoretically, the total length of the audio file should be around 14400/11025=1.3 seconds.

TODO: the actual length of about 3 seconds, something is wrong here.

plotdata500.png

Here I am plotting the first 500 samples and you can easily see the sound, a few tips to interpret the lines, really high and really low means loud, really “dense” means high frequency, ie, high pitch. Of course, this is not a single sound, but a mixture.

wavdatahist

Here is a histogram of all the sample values distribution. I don’t quite understand what exactly does the value mean, the magnitude of the sound? If that is the case, does that mean most of the values are too loud? and too quiet? I assume in this case, it is simply the vocal of a male reading some words, mostly it will either be the guys voice and the quiet moment…

TODO: need to look into what does (-32,767, 32,768) mean?

In the end, here is a spectrum graph where I borrowed the source code mostly from here.

malespectrum

well, this is what I have done and need to do more research and have a better intuitive understanding of what everything is.

 

Python – Profiling cProfile

Recently I have a project that I have lots of raw data (time series data), however, the output need to some higher level statistics which requires some aggregation of the raw data. So here are the choices that I have, either for every request, pull the raw data and calculate it on the fly, or preprocess all the raw data and store the stats somewhere else, so when use needs the data, then it is simply a look up.

Since the data is pretty big and updated at a daily level, batch preprocessing all the data is like boiling the ocean, and what is even worse, we need to reboil the ocean every day. There is also a possibility that the user who requested this service won’t even be able to use all that much data that frequent, which will result in a huge waste of computing power. On the other hand, calculating on the fly faces some other challenges, you need to ensure your logic is so well written and generic and will succeed for all the parts. Mostly important, the performance need to be fast enough to serve as a service. In the history, I wrote my python code in a style that “hmm, it is fast, hmm.. it is taking a long time”. Nothing more than a linux “time python script.py”. Now I face the challenge of turning whatever calculation into something that will be fast at a service level (<100ms). Have a quantitive understanding of how much time each step takes, where the bottle neck is and then we can strategically to improve certain parts without switching to other programming languages (C, Java)..

Then I learned that this type of analysis is called profiling:

“A profile is a set of statistics that describes how often and for how long various parts of the program executed.” – Python Documentation

The cProfile is the de-facto profiling tool to benchmark Python code. It is not the mostly user friendly tool but once you spent some time on it, getting familiar with its syntax, then you will have a tool like linux top command but for your python code.

import cProfile 
cProfile.run('range(10)')

Either the python documentation or pyMOTW can help you get started quickly. Then I came across a blog post from Julien Danjou РProfiling Python using cProfile: a concrete case which introduced me to KCacheGrind.

If you are a Mac user like me, brew install everything following this instruction.

pip install pyprof2calltree, and then you will be good to go.

pyprof2calltree -k -i file_profile

In the end, you will have a beautiful visualized way of how long each step takes.

Screen Shot 2016-07-23 at 6.59.56 PM.png

 

Shell – Exclamation Mark !

The exclamation mark will definitely speed up your history look up. Usually people look up history by hitting the up arrow to go through the history commands. I literally have seen someone who hit the arrow sign more than 20 times and still wouldn’t be able to locate the exact command he was looking for.

Then there are some users there who uses the ‘history’ command to lookup history. One can either by copy paste the command, or you can find the command line number and use “!<number>” to execute the command.

Also you have people use command like ‘history | grep <keyword>’. However, if you happen to know the command you are searching for start with certain prefix or even contains certain keyword. You can use “!<prefix>” or “!?<substring>?” to quickly pull the last executed command that starts or contains the specified keyword.

(note: !xxx show in the history, the command it represents does)

 

$ echo 'hello'
hello
$ cd ~
$ !ec
echo 'hello'
hello
$ !hell
-bash: !hell: event not found
$ !?hell?
echo 'hello'
hello

Here is an answer on stackexchange that contains a more detailed explanation of use cases for exclamation!

Spring-boot: actuator default endpoints

Actuator is a sub-project of Spring-boot, which provides production ready features for spring-boot applications. It provides a number of additional features to monitor and manage the application when it pushes to production.

You can try it out by clone the spring-boot github repo, and navigate to the spring-boot-samples directory which contains plenty of built-in samples, you can find one that is called spring-boot-sample-actuator-log4j2, and run the command `mvn spring-boot:run` to bring up the spring boot application. 

Screen Shot 2016-07-03 at 10.41.36 AM

As you can see, there is not really any code in the project where defines this autoconfig endpoint. This “autoconfig”one might not be the most interesting or straightforward one, but it is actually a really important and sophisticated one which displays the auto-configuration report of all the auto configuration report, also which one is applied or not and why.

You can refer to spring-boot-actuator documentation for a complete list of the available endpoints. Here are a few ones that I tried out myself along with some description and screenshots to help you understand how that works in real life:

1.configprops – configuration properties

Screen Shot 2016-07-03 at 10.53.20 AM.png

2.health – heath status

application health information

Screen Shot 2016-07-03 at 11.05.00 AM

3. metrics – metrics of current application

If you are in a production environment, i think you should care every number in the response.

Screen Shot 2016-07-03 at 11.08.48 AM.png

4. mappings – display a collated list of all paths

Screen Shot 2016-07-03 at 11.14.37 AM

I pasted the response to a site called jsonlint to put it in a better format for human to read.

Screen Shot 2016-07-03 at 11.14.17 AM

5. shutdown – make a post to the server to shutdown

This is quite a dangerous endpoint that a post request to the server will shut it down.

Screen Shot 2016-07-03 at 11.19.02 AM.png

Well… here they are, enjoy the awesome work done and appreciate it.

Serialization and Deserialization

I am curious really how a class or serialized at the byte level. I borrow the example from tutorialspoint, modified it a bit and here is what I have right at this moment.

Here is my Employee class and here is my main function.

serial_employeeserial_main

Clearly, the main function will write a file to my desktop and you can use your favorite editor to take a look at the seralized, i.e. binary file. There is a tool called hexedit which might come handy. Here is a screenshot of how the binary file looks like in the text editor.

hexedit

As you can tell, the binary file is a bit messy but most of the contents are almost in a human readable format, say for example, we have 4 attributes and all the string fields are so easy to tell. However, the goal of this post is to 100% decode every byte there and understand how Java really serialized a object.

This really has nothing to do with intelligence but to read the protocol of Java serialization. Here is where the protocol is and of course, it is the only source I have to decipher this binary file.

By the time that I am writing this post, I have not fully decipher every character yet, but I will say I am almost 80% there and here is my progress.

# raw value
aced 0005 7372 0019 636f 6d2e 6461 7461
6669 7265 6261 6c6c 2e45 6d70 6c6f 7965
65da 231e 1f8f 8a0e 4402 0003 4900 066e
756d 6265 724c 0007 6164 6472 6573 7374
0012 4c6a 6176 612f 6c61 6e67 2f53 7472
696e 673b 4c00 046e 616d 6571 007e 0001
7870 0001 0932 7400 0864 697a 6869 e590
8d74 0006 6d69 6e67 7a69 
------
# decipher
aced: Ôľąstream magicÔľČ 
0005: (stream version)
73: (object)
72: (class description) 
0019: 
636f 6d2e 6461 7461 6669 7265 6261 6c6c 2e45 6d70 6c6f 7965 65: com.data.fireball.Employee
da 231e 1f8f 8a0e 4402 0003 
49: (I)
00 06: (6 bytes)
6e 756d 6265 72: number
4c: (L)
0007: (7 bytes) 
6164 6472 6573 73: address
74: (string marker)
0012: (18 bytes) 
4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b: Ljava/lang/String; 
4c: (L)
00 04: (4 bytes)
6e 616d 65: name
71 007e 0001 7870 
0001 0932: 67890
74: (string marker)
00 08: (8 bytes)
64 697a 6869: dizhi 
e590 8d: Śźć
74: (string marker)
0006: (6 bytes) 
6d69 6e67 7a69: mingzi

 

 

 

ToStringHelper(guava) – SimpleResponse

I came across a handy Java class that is from Google Commons named ToStringHelper. This is where I found the usage of ToStringHelper by reading openscoring source code.

Villu was wrote this tiny SimpleResponse class in openscoring.common which is serializable and only one attribute, three methods, the getter, setter and the toString method, which is implemented using the MoreObjects.toStringHelper class.

First, we need to cover something basic about “toString“. ¬†It is a method that comes with class Object, basically means every class in Java is kind of a object and it should always inherit this default toString method unless overwritten. However, who need a hashcode, right?!. User tend to need something that is more informative and concise, like you might want the first name and last name out of a person object? a title, author in the textual format of a book class so on and so forth. You can overwrite toString in whatever way you prefer, but using the toStringHelper really made this part easy and consistent.

toStringHelper

using tostringhelper

toStringHelper_default

default tostring

Keep this little trick in your toolbox and hopefully it is helpful sometime.

 

Restful Java with Jax-rs 2.0 – How to run Shop App

I am reading this Oreilly book RESTful Java with JAX-RS2.0 from Bill Bourke. I am trying to following the example in Chapter three of how to to deploy a na√≠ve web API that can create/update and get customer information. The book comes with some sample code where you can find from this Github repo. (If you cannot locate the right project, the project should reside in “restful_java_jax-rs_2_0-master/resteasy-jaxrs-3.0.5.Final/examples/oreilly-jaxrs-2.0-workbook/ex03_1″). I tried to run the code in several ways like command line, Eclipse and also tested it out using Junit and tools like postman, I want to list my experience here so others can save some time and get it up and running fast.

There is a README file under the project directory telling you “mvn clean install” is the way to go. Clearly, the author put lots of thoughts into the pom.xml and it will build the war file and deploy the war using jetty maven plugin and run a few unit tests as the client to ensure you can do all the CRUD operations as it supposed to. Here is how the final junit test look like:

ex3_1junit¬†As you can see, it “looks like” the application is working fine, however, after the maven build, it will not keep a server running and you really cannot have a hands-on experience playing around with the app. In that case, we need to deploy the application and keep it running as long as we want. And potentially test it using your own tool set instead that Junit test.

1. Jetty-runner.jar

A few words about jetty if you are new to the whole Java web app thing, like me. Jetty is a web server and servlet container in one sentences, was first created in 1995 and open sourced and has been in sourceforge, codehaus, eclipse and now Github. Instead of diving too much into the installation, integration of how to get a jetty server working, they have a easy version of jetty-runner that packages everything you need into a jar file and you can use it to run java web applications. Here is a more detailed tutorial of how you use jetty-runner. I first went to the target folder of that shop app where there is a ex03_1.war file got generated after the mvn clean install in the previous section. I downloaded the jetty-runner to the target folder so the jar and war file are at the same directory. Then you simply need to run command

java -jar jetty-runner*.jar ex03_1.war

Screen Shot 2016-06-12 at 10.35.14 AM

And you should have the web application running on your localhost at port 8080. Now lets test out without using the Junit test.

2. POSTMAN

In the idea scenario, I want to use a browser to show you this, however, to make the post command in vanilla browser, you cannot do it AFAIK, however, there are tons of browser extensions you can use and the one that I am going to use today is called postman. First, lets take a quick look at how that junit test created a user.

ex3_1juniteclipse

The logic is pretty straightforward, first create a customer in the xml format, and then make a post request to the highlighted URL and you should get the 201 status for successfully creating a user. Now, lets try to do it in our postman.

postman_createuser.png

As you can see, when you run jettyrunner, the default behavior is pretty good and the postman also 100% reenforced the fact that our API creation is running properly, here is another screenshot of GET the newly created user.

postman_get

Jetty runner has also some extra arguments for you to customize, here are two screenshots of how I changed the port, the default root path and I am even running it against the folder instead of the war file.

jettyextrafeaturesnewpostman

3. Eclipse

IDE like Eclipse or IntellJ is always good to have. It not only gives you a heavy duty text editor, but also provides you with all the development features that a plain text editor lacks. Also, all the features that we described above using Jetty to deploy could be configured as one button click, this makes a few difference when you need to do the same thing 100 times!

In this case, I am planning to learn more about Tomcat, so I download Tomcat and uncompressed it to a folder. Since all the examples in the Github repo are maven projects, you can easily import projects as existing maven projects. Then you simply need to right click the jaxrs-2.0-workbook-ex03_1 projects and “run on server”. Find the right Tomcat version and point to the installation folder and you are good to go.

eclipse_tomcat_ex03_01.png

There is only one thing I did not fully understand is why the URL root turned out to be the project folder name, which is “ex03_01”, if you happen to know the answer, please leave a comment below.

Kerberos – Create New User

I followed this tutorial and managed to install Kerberos on an AWS ubuntu box. I did not notice anything extra but until I need to create users.

In the origital Ubuntu environment, creating new users is nothing but two lines of code, useradd and passwd, this will create a new user and change the password if you have root access or the sudoer. However, in a Kerberized environment, you not only need to create a user at the Linux system level, but also need to create a principle in the Kerberos database and set up the password there.

Here is a screenshot of how to create a new user and the extra steps are simply login to the kerberos admin server and add the principals there. For some people, you might be wondering what those extra steps bring us, you are right, if you are talking about one machine, this will add zero benefit. However, consider you have a network of hosts/servers, when you change your password, do you really need to go to every machine and change them one by one? Having a centralized third party authentication software like Kerberos will totally save the time.

keberosadduser