Object Classification on aYahoo image dataset

Disclaimer:
1. You need to have Caffe installed on your system to run the below tasks. If not follow the tutorial here to get Caffe up and running on your system.
2. You need to download aYahoo image dataset here.
3. You need to get all the pre-trained models: alexnet.caffemodel, coffenet.caffemodel and googlenet.caffemodel to get started on extracting features from images.

We will start by getting the paths to all the downloaded images into a text file. Run this line in terminal with correct path to images’ folder and path to text file in which we need to append all the paths to images.

find `pwd`/examples/images/ayahoo -type f -exec echo {} \; > examples/tempFeaExtr/temp.txt

Now we need to label the images. I am assigning labels starting from 0. It is recommended to sort all the names in text file, so we can divide the whole dataset into train and test datasets with reasonable number of images of each class in both datasets rather than randomly the whole data.

#to_label
outfile= open('zz_temp.txt', 'w')
infile= open('z_temp.txt', 'r')
myset= []

for line in infile:
 a= line[(len(line)-(line[::-1].index('/'))):-1]
 a= a[:a.index('_')]
 myset.append(a)

 if a== "bag":
 b= "0"
 elif a== "building":
 b= "1"
 elif a== "carriage":
 b= "2"
 elif a== "centaur":
 b= "3"
 elif a== "donkey":
 b= "4"
 elif a== "goat":
 b= "5"
 elif a== "jetski":
 b= "6"
 elif a== "monkey":
 b= "7"
 elif a== "mug":
 b= "8"
 elif a== "statue":
 b= "9"
 elif a== "wolf":
 b= "10"
 elif a== "zebra":
 b= "11"

 outfile.write(line[:-1]+ " "+ b+ "\n")
print(set(myset))

infile.close()
outfile.close()

#to_sort
infile= open('zz_temp.txt', 'r')
outfile= open('z_temp.txt', 'w')
myset= []

for line in infile:
 myset.append(line[:-1])

myset.sort()
for a in myset:
 outfile.write(a+"\n")

infile.close()
outfile.close()

As we have labeled and sorted the whole database, now, we divide it into two sets namely train_data.txt and test_data.txt. Try to keep the ratio of “no. of train images/ no. of test images” constant for all the classes. Now, we have train_data and test_data to proceed on to next step.

I am going to extract features of every image using the pre-trained models. After extracting I am gonna train SVM classifiers to predict the classes of test_data.

Link to the code to extract features and to train SVMs id here on GitHub.

Happy classifying images.
Adios

Advertisements

Install Caffe on Ubuntu 14.04

Disclaimer:
1. As my machine has no GPU hardware, I am going to install Caffe without CUDA support.
2. I am going to install only Python wrapper for Caffe.

As the installation page on Caffe has no detailed instructions to install all the dependencies required and get your system ready to run CNNs , I am writing this small tutorial to set Caffe Up and Running on your machine.

Firstly, we will get stared by downloading latest release of Caffe from github. (You need to have Git installed on your machine)

git clone https://github.com/BVLC/caffe.git
#add these following lines in the end of your .bashrc file
export PYTHONPATH=$PYTHONPATH:$HOME/caffe/python
export PYTHONPATH=$PYTHONPATH:$HOME/caffe/python/caffe

It is advised to use Anaconda Python distribution as it installs most of the requirements for python wrapper around Caffe (though it install a lot of packages we have no use). But, installing Anaconda is up to you.
Download Anaconda and run shell script. Add the path of bin to #PATH in file ~/.bashrc.

bash Anaconda-2.1.0-Linux-x86_64.sh
#add the following line at the end to your .bashrc
export PATH=/home/suryateja/anaconda/bin:$PATH

Get gflags and install on your system.

wget https://github.com/schuhschuh/gflags/archive/master.zip
unzip master.zip
cd gflags-master
mkdir build && cd build
export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1</pre>
<pre>make
sudo make install

Download google-glog and install. You can also install through apt-get. And install snappy.

tar -xvzf google-glog_0.3.3.orig.tar.gz
cd glog-0.3.3/
./configure
make
sudo make install
agi libgoogle-glog-dev
agi libsnappy-dev

Get leveldb onto your machine and lmdb with its dependencies.

git clone https://code.google.com/p/leveldb/
cd leveldb
make
cp --preserve=links libleveldb.* /usr/local/lib
sudo cp --preserve=links libleveldb.* /usr/local/lib
sudo cp -r include/leveldb /usr/local/include/
ldconfig
sudo ldconfig
sudo apt-get install libffi-dev python-dev build-essential
sudo apt-get install liblmdb-dev

Download Atlas and Lapack (directly downloads when you click on link!). But before installing, you need to modify your CPU frequency/scaling. You you get errors saving the file, try to use different editor. I have tried sublime, gedit. Finally with I can modify with emacs. Change the scaling for all of your CPU cores to replace the single word in the file with ‘performance’.

gksu emacs /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
gksu emacs /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
gksu emacs /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
gksu emacs /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor

bunzip2 -c atlas3.10.2.tar.bz2 | tar xfm -
mv ATLAS ATLAS3.10.2
cd ATLAS3.10.2
mkdir Linux_C2D64SSE3
cd Linux_C2D64SSE3
../configure -b 64 -D c -DPentiumCPS=2400 --prefix=/home/suryateja/lib/atlas --with-netlib-lapack-tarfile=/home/suryateja/Downloads/lapack-3.5.0.tgz

make build
make check
make ptcheck
make time
sudo make install

Install dependencies for python wrapper if you didn’t choose to install Anaconda Python distrubution. The dependencies file is in caffe/python. And install hdf5 via apt-get.

cd python/
for req in $(cat requirements.txt); do sudo pip install $req; done
sudo apt-get install libhdf5-serial-dev

Download latest 3.0.0-beta version (file automatically downloads if you click) of OpenCV and build it. But before install all the dependencies of OpenCV.

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

cd Downloads
cd opencv-3.0.0-beta/
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install

Now, we are almost done with getting all the dependencies of Caffe. So, lets dive into getting Caffe up and running on our machines. Modify the makefile as your needs as shown in installation page.

cd caffe
cp Makefile.config.example Makefile.config
make all
make test
make runtest

If you have errors while installing even followed this tutorial, you can search for any known issues on their issues page. One of the errors I faced is this. And I am able to solve it as suggested in the comments section of issue page.

At last, now, you have Caffe installed and running on your machine.
Happy training Deep Neural Networks. 😀

Download friends’ profile pictures on facebook!

Hello guys, before going into the topic, I wanna say you one thing. This method may not fetch all of your facebook friends’ profile pictures. But, this is the efficient way*(considering the number of pictures you get) you can find on whole internet(exaggerating 😛 ).
*depends on your popularity on FB.
Let me make sure you have:(Prerequisites, and don’t proceed without understanding these topics well.)

  • Considerable knowledge of facebook GraphAPI. Nice video explaining GraphAPI.
  • Installed third-party facebook-sdk for python. As I am going to use python.

So, with a simple google search for getting friends’ pics, you might land on a hell lot of pages with most of them using a call to friends (/friends) with a user node(me/friends).

Okay. So, why isn’t it efficient?

Here’s the catch. The permissions required to make this call from this page. permissions The ‘second point’ states it will only return those who have use the app making the request. I don’t know what’s your case, but, for me it returned only 14 friends of 970 in my list. Very disappointing. Try it for yourself and let me know how many nodes( 😛 ) it returned in comments.

import facebook

graph= facebook.GraphAPI()
friends= graph.get_connections(&quot;me&quot;, &quot;friends&quot;)
print(len(friends["data"]))
It returned only 15 friends in my list.
It returned only 15 friends in my list.

I think our aim is to fetch profile pics, not getting friends list!

If you are thinking like that, just take a minute to re-think. To get pics through GraphAPI we can call me/picture which returns present profile picture of user. We can give a username or user id in the place of ‘me‘ while we are calling for pictures. That’s the reason we are obsessed to know our friends’ list to get user id or username. But, here’s another catch. The ids you got are not the real ids. 😦 See this…

You can see that for yourself. It returned some other value in the place of userid.
You can see that for yourself. It returned some other value in the place of user id.

So, now, some how, find a way to get user-id of our friends.

Likes! Yes the answer is likes.

You can get your friends’ user-ids (depends on their settings. But, you can access 90% of them.) by passing any of our pics or statuses with most number of likes. Find its id in search bar by clicking on your pic. Now, you can pass <pic-id>/likes which returns a list of friends who liked your pic. Now, here’s the brilliant part. FB provides a link to their profile who all liked your nodes(pics or statuses).

likes= graph.get_connections(&quot;&lt;node-id&gt;&quot;, &quot;likes?fields=link,name&amp;limit=500&quot;)
#returns only 1000 friends maximum.
#returns your friends profile as &quot;https://www.facebook.com/&lt;username&gt;&quot;

Now, we got our friends’ usernames. Lets start downloading pics. 🙂

for i in range (0,len(likes["data"])):
myid= likes["data"][i]["id"]
myname= likes["data"][i]["name"]
mylink= likes["data"][i]["link"]
username= mylink[25:]
print(myname)
pic= graph.get_connections(username, &quot;picture?height=9000&amp;redirect=false&quot;)
urllib.request.urlretrieve(pic["data"]["url"], "fb/likers/"+myname+".jpg")
print(&quot;downloaded the pic&quot;)

Downloading pics in action.
Downloading pics in action.

In this process you may get some errors fetching friends’ username, because of their settings. In that case we check for it and try to ignore those links.

#'https://www.facebook.com/profile.php?id=100005090622885&amp;#8242' It looks similar to this. From which we can’t extract username.
check= (username[:7])
if(check == &quot;profile&quot;):
print(&quot;Can’t fetch username due to their settings.&quot;)
continue

If you are planning to download pictures of friends who liked your nodes from two or more nodes, you may get repeated users i.e. one of your friends may liked both of your nodes. In that case we try not to re-download their pictures.

if (os.path.exists("fb/likers/"+nodename+".jpg")):
print("Already downloaded "+nodename+"’s picture!")
continue

OMG! omg! I got a lot of pics. So, now what to do?

You can do a lot of things. Really a lot of things. You can build a crazy face-recogniser like this one here.
And you can put it in your room, running, recognises the person entered your room. 😛

For entire code you can visit my GitHub page.

Any questions, feedback is always welcome at shine123surya@gmail.com 

Birthday code! (Thanking friends on facebook using GraphAPI)

I wrote this on my birthday,

import facebook

graph = facebook.GraphAPI('Access token you got from facebook developers page')
feed = graph.get_connections(&quot;suryateja.cheedella&quot;, &quot;feed?limit=200&quot;)
for a in range (0,200):
    post = feed[&quot;data&quot;][a]
    fromDetails = (post[&quot;from&quot;])
    fromName = fromDetails[&quot;name&quot;]
    graph.put_like(post[&quot;id&quot;])
    graph.put_object(post[&quot;id&quot;], &quot;comments&quot;, message=&quot;Thank you :d &quot; + fromName)

and then there, it thanked all my friends who wished me on my timeline.

Hello, what’s up guys,

It’s afternoon on 4th and as usually I am browsing on quora and facebook. I stumbled upon ‘what are the best python codes you have written’ question on quora. I saw some top answers which are actually thanking friends on FB on their birthday. Actually it’s the best time to learn or just copy&paste all of that code so, I can make my thanking task easy for tomorrow. But, I wrote ‘Hello world’ program in python just two weeks back. And completed reading like 100 pages of ‘Head first python’. So, I started searching on web and got to know about some thing called GraphAPI. After spending like 5 to 6 hours, I wrote that 10 lines of code (in a perfectly working condition) and tested it.

You can say, dude why don’t you use that already written code found on quora. Basically all those codes are old. I mean those are the codes written before releasing GraphAPI by facebook. With that thing for facebook developers, one can reduce their code to 10 lines (yeah, you look their) from 40 to 50 lines of code which I have found on Quora.

That is not the best way to reach our goal (like commenting on last ‘x’ number of posts on my wall) but, that is what I can learn in that span of time.

Shine.