CNN and TensorFlow For Recognizing Dogs and Cats

Reference: https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
https://github.com/abnera/image-classifier
https://github.com/abnera/image-classifier/blob/master/code/fine_tune.py
http://www.subsubroutine.com/sub-subroutine/2016/9/30/cats-and-dogs-and-convolutional-neural-networks

NOTE: THIS MAY BE MISLEADING BECAUSE I CAN’T USE TENSORFLOW AS HERE BUT THEANO ALTHOUGH I INTEND TO USE TENSORFLOW. PLS READ THE REST WHY.

  1. Make sure keras and tensorflow already installed.
  2. Download the data needed from Kaggle’s Dogs vs Cats. Download train.zip dan test1.zip
  3. Create a new directory ‘dogs-vs-cats-tf’
    Then in theĀ ‘dogs-vs-cats-tf’ directory create a new directory ‘data’
    then in ‘data’ directory, create a few new dirs ‘train/dogs’, ‘train/cats’, ‘validation/dogs’ and ‘validation/cats’.
  4. The first time to use keras and tensorflow, I just want to use 1000 samples for each class (1000 samples for dogs and 1000 samples for cats. although the original dataset had 12,500 cats and 12,500 dogs) for the training. We’d put them in their respective directories (data/train/dogs and data/train/cats).
    Then we use additional 400 samples for each class as validation data, to evaluate our models. We’d put them in their respective directories (data/validation/dogs and data/validation/cats).
    Here are the clear instruction:
    – cat pictures index 0-999 in data/train/cats
    – cat pictures index 1000-1399 in data/validation/cats
    – dogs pictures index 0-999 in data/train/dogs
    – dog pictures index 1000-1399 in data/validation/dogs

     
  5. in ‘dogs-vs-cats-tf’ directory, create a new python file ‘dogs-vs-cats1.py’. Here is the content (ref: https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d).

     
  6. Run the python file.
    NOTE: In http://myprojects.advchaweb.com/index.php/2017/02/22/installation-of-opencv-keras-and-tensorflow-on-ubuntu-14-04/, I use virtualenv ‘opencv_keras_tf’ and installed keras and tensorflow there. Use the virtualenv! The first time I forgot to use the virtualenv then I got this error when I tried to run the python file

    The error ‘ImportError: No module named keras.preprocessing.image’ because I ONLY install keras in virtualenv ‘opencv_keras_tf’ NOT GLOBALLY!
    OK. Use virtualenv ‘opencv_keras_tf’ then run the python file.


    I GOT AN ERROR: ‘ValueError: Negative dimension size caused by subtracting 2 from 1’
    SOLUTION: Modify keras.json and replace ‘tf’ in ‘image_dim_ordering’ to ‘th’ (WHY ‘tf’ DIDN’T WORK???)

    SO HERE IS THE CONTENT OF ~/.keras/keras.json

     

    RUN AGAIN (IT’S WORKING NOW):
    START AT 2017-02-24 10:08PM


    NOTE: Read this about CUDA compute capability : http://stackoverflow.com/questions/10961476/what-are-the-differences-between-cuda-compute-capabilities
    IT SEEMS DEPEND ON THE HARDWARE! NOTHING CAN DO TO IMPROVE THE COMPUTE CAPABILITY!
    AFTER READING THIS https://www.tensorflow.org/versions/r0.10/get_started/os_setup#installing_from_sources AND http://stackoverflow.com/questions/38542763/how-can-i-make-tensorflow-run-on-a-gpu-with-capability-2-0, I GOT SAD NEWS THAT
    In order to build or run TensorFlow with GPU support, both NVIDIA’s Cuda Toolkit (>= 7.0) and cuDNN (>= v3) need to be installed.
    TensorFlow GPU support requires having a GPU card with NVidia Compute Capability >= 3.0.
    SO MY GPU GeForce GT 520M THAT ONLY HAS CUDA COMPUTE CAPABILITY 2.1(REF: https://en.wikipedia.org/wiki/CUDA) CAN’T BE USED FOR TENSORFLOW!!!I ONLY CAN USE CPU!!!
    I STOPPED AFTER THE 3rd EPOCH (2017-02-14 10:39) BECAUSE I HAVE GOT ANOTHER THING TO DO. HOPEFULLY CAN RESUME THIS TOMORROW.
    I DID IT AGAIN!

    START AT 2017-02-25 08:45PM AND END AT 2017-02-26 03:45AM. SO THE TRAINING PROCESS RUN FOR 7 HOURS!!! AND THE ACCURACY IS 88.15%???
    AND AT THE END I GOT THIS ERROR

    ALSO I DONT FIND ‘first_try.h5’ FILE!!!! –> SOLUTION: In the file ‘dogs-vs-cats1.py’, replace ‘model.load_weights(‘first_try.h5′)’ with ‘model.save_weights(‘first_try.h5′)’ OR LIKE THIS:

    NOW I GOT THE TRAINING FILE ‘first_try.h5’ IN ‘dogs-vs-cats-tf’ DIRECTORY!
    NOTE: I ONLY SET ‘nb_epoch = 5’ JUST TO PROVE THE .h5 FILE WOULD BE CREATED OR NOT. SO THE ACCURACY IS NOT TOO GOOD (70.15%). HERE IS THE LAST LINE ON MY TERMINAL AT THE EPOCH 5TH

    OK. NOW HOW TO USE THE TRAINING FILE FOR TESTING!!!

Configure CodeBlock With OpenCV

CONFIGURE CODEBLOCK WITH OPENCV
ref:http://jonniedub.blogspot.co.id/2013/01/setting-up-codeblocks-ide-for-use-with.html?showComment=1454444260714#c506464558721086998
http://blog.csdn.net/mummyding/article/details/49804921
1. Create new project:
File -> New -> Project…-> Find ‘OpenCV Project’ -> click ‘Go’ -> Next -> Write Project Title and the Path ->
Compiler: GNU GCC Compiler -> Finish

2. Default opencv template (main.cpp in /Sources/ directory) is created. Here is main.cpp:

3. Its time to configure this project so it’d be linked to opencv library.
From eclipse configuration above, we get the libraries path by using:
pkg-config –cflags opencv
pkg-config –libs opencv
We’ll do the same here:
a. Right click the Project Name ‘OpenCV’ -> Build Options -> Click the Project name (OpenCV) DO NOT the ‘Debug’ ->
Search directories tab -> Compiler tab -> click Add -> fill with: /usr/local/include/opencv
b. then click Linker tab -> click Add -> fill with: /usr/local/lib
c. Then at Linker settings tab -> In Link Libraries, Add this (one by one):
opencv_core
opencv_imgcodecs
opencv_highgui

In Others linker options:, add:
opencv-config --ldstaticflags

d. click OK

4. Test it!
Right click the Project name ‘OpenCV’ -> Build
make sure everything okay
Then click ‘Run’ (the green arrow) or from Build -> Run
SHORTCUT: F9 (Build and Run at ONCE!)

Configure Netbeans C++ With OpenCV

CONFIGURE NETBEANS C++ WITH OPENCV
ref: http://www.technical-recipes.com/2015/configure-netbeans-to-use-opencv-in-linux-environments/
1. Create C++ Application Project
File->New Project->C/C++ ->C/C++ Application->Next->’Give it the Project name and the location’ AND CHOOSE C++11->Finish

2. Configure the compiler:
Right Click The project->Properties->Build->C++ Compiler->’Include Directories’: /usr/local/include/opencv ->Apply

3. Add the opencv libraries:
Right Click The project->Properties->Build->Linker->’Additional Library Directories’: /usr/local/lib ->Apply
Right Click The project->Properties->Build->Linker->’Libraries’: Add -> Click Add Library THEN GO TO /usr/local/lib ->CLICK (SIMULTANEOUSLY) libopencv_core.so, libopencv_highgui.so, libopencv_imgproc.so, libopencv_imgcodecs.so
OK

NOTE: TO SHOW VIDEO CAPTURE, ADD libopencv_videoio.so
4. Test
in main.cpp:

BUILD & RUN:
OPENCV VERSION: 3.1.0

RUN FINISHED;

exit value 0;

real time: 0ms; user: 0ms; system: 0ms
SHOW IMAGE main.cpp:

 

VIDEO CAPTURE main.cpp (add linker libopencv_videoio.so) :

 

Configure Eclipse With C++ And OpenCV

CONFIGURE ECLIPSE WITH C++ AND OPENCV
ref:http://rodrigoberriel.com/2014/10/using-opencv-3-0-0-with-eclipse/
1. Assumed eclipse and cdt (C++ Development tools) already installed. if not, pls follow this:

a. Install C++ Development (CDT):
ref:https://eclipse.org/cdt/downloads.php
Help -> Install New Software -> Add… ->
Add Repository form:
Name: C++ Development Tools
Location: http://download.eclipse.org/tools/cdt/releases/8.8.1 (CDT 8.8.1 FOR ECLIPSE MARS)

b. Check All and Install

2. Creating a New C++ Project on Eclipse
a. File Ā» New Ā» C++ Project;
b. Give a name to your project in Project Name; for example: TestOpenCV
c. Choose Executable Ā» Empty Project in Project Type;
d. Check Linux GCC in Toolchains and press Next;
e. Click on Finish;

3. Linking OpenCV to the newly created project
a. Select the project and go to the menu Project Ā» Properties (or press Alt+ENTER);
b. We’re going to modify somethings on Tool Settings tab of C/C++ Build Ā» Settings;
c. In GCC C++ Compiler Ā» Includes, add “/usr/local/include/opencv” in Include paths (-l). Discover the correct path by typing the following on the terminal: pkg-config –cflags opencv;

d. Go to GCC C++ Linker Ā» Libraries and add “/usr/local/lib” in Library search paths (-L). Discover the correct path by typing the following on the terminal:: pkg-config –libs opencv;

e. Still in GCC C++ Linker Ā» Libraries, add the libraries you’ll need in your project in Libraries (-l). We’re going to need 3 to our project (don’t forget to link opencv_imgcodecs, since imread() has moved to it in OpenCV 3):
opencv_core
opencv_imgcodecs
opencv_highgui

Done!

4. Lets test it!
a. Right-click on the project in Project Explorer and go to New Ā» Folder;
b. Give a name to the folder, for instance: src.
c. Right-click on the src folder and go to New Ā» File;
b. Give a name to the file, for instance: DisplayImage.cpp and type (or copy and paste) the code snippet below. Don’t forget to save the file.

Project Ā» Build All (or Ctrl+B) to build;

5. Run the program
a. Right-click on the project in Project Explorer and go to Run As Ā» Local C/C++ Application
b. The image should be appeared!!!

Compile OpenCV 3.1 And Python 2.7 With CUDA 7.5 On Ubuntu 14.04

COMPILE OPENCV 3.1 AND PYTHON 2.7 WITH CUDA
ref:http://rolflussi.blogspot.co.id/2015/09/opencv-with-cuda-on-ubuntu-1404.html
1. Create a new virtualenv with python parameter point to python2.7.

2. Check python version:

3. Check pip version:

4. Install numpy:

CHECK numpy VERSION:

IMPORTANT: I FOUND AN ERROR ‘The data should normally be NULL! in function allocate’ WHEN TRY TO TEST SIFT Holography Feature Matching (flann.knnMatch). PLS FIX THIS FIRST (SEE SOLUTION IN /media/data/MASTER/opencv/github_list_fish_rec.txt) BEFORE BUILD OPENCV AT STEP #5 BELOW! — OKAY. THIS IS ALREADY FIXED ON THE LAST COMPILE (OPENCV 3.1 WITH PYTHON 2.7)

5. We already cloned opencv 3.1.0 and opencv_contrib 3.1.0. so now, go to the build WITH CUDA and do setup:
<OLD>
THIS IS THE INITIAL CMAKE COMMAND: (DONT USE IT!)

</OLD>

<OLD>
THIS IS THE CMAKE COMMAND WITH CUDA I GOT FROM http://rolflussi.blogspot.co.id/2015/09/opencv-with-cuda-on-ubuntu-1404.html: (DONT USE IT! ‘WITH_QT’ COMMAND GAVE ME AN ERROR! MAY BE I DONT HAVE QT INSTALLED)

THIS IS THE ERROR:

</OLD>

<NEW>
THIS IS THE CMAKE COMMAND WITH CUDA AND REMOVE ‘WITH_QT’ COMMAND. THIS IS WORK!!! BUT MAKE SURE ‘build’ directory EMPTY FIRST!
NOTE: I USE ‘INSTALL_C_EXAMPLES=ON’ INSTEAD OF ‘OFF’ LIKE PREVIOUS BUILD. LET ME KNOW IF EVERYTHING WORKS (ITS CONFIRMED!!!)

</NEW>

NOTE:
PLS SEE THE NVIDIA CUDA PART ON THE ABOVE RESULT IS INCLUDED! BUILD WITH CUDA IS FAST (A FEW MINUTES)!

6. Compile opencv:
NOTE: BEFORE EXECUTING THIS, CONSIDER TO EDIT THIS FILE:

SEE BELOW HOW TO MODIFY IT!!!

NOTE:
AFTER WAITING FOR MORE THAN 1 hour AND 25 MINUTES (91% COMPLETED), THE COMPILATION IS STOPPED WITH THESE ERRORS STACK:

SOLUTION:
ref:https://github.com/Itseez/opencv/issues/5859
I USED A TEMPORARY SOLUTION: EDIT /home/teddy/opencv/build/samples/gpu/CMakeFiles/example_gpu_opengl.dir/link.txt. THEN ADD THIS: -lGL -lGLU AT THE END OF THE LINE (IN SINGLE LINE! DONT ADD AT THE NEW LINE!). SO THE FILE WOULD BE LIKE THIS:

SAVE THE FILE THEN RE-MAKE (BUT I SUGGEST TO REMOVE ‘build’ DIRECTORY, CREATE IT AGAIN LIKE STEP #5. BUT BEFORE EXECUTING STEP #6, EDIT link.txt FILE LIKE THE ABOVE! ). THIS SOLUTION IS WORKING!!!
ACTUALLY THERE IS A PERMANENT SOLUTION IS SUGGESTED IN THE SAME URL, BUT I DONT KNOW HOW TO DO IT!!! LEARN IT

OK. HERE IS THE SUCCESFULL COMPILE (MAKE)

NOTE:
SEE THE MORE COMPLETE COMPILATION (MAKE) PROCESS IN THIS FILE: /media/data/MASTER/opencv/make_opencv_cuda.txt
ITS NOT FULL COMPLETED BECAUSE I MISSED SOME DATA BUT IT WORTHS TO SEE (THERE ARE SOME WARNING!!!)
THIS COMPILATION TOOK MORE THAN 1 hour AND 30 MINUTES. COMPILATION WITH CUDA (MAY BE ALSO WITH OPENGL) MAKE IT SLOWER!!!

WARNINGS:
1.

2.

7. Assuming OpenCV 3.1 compiled without error, you can now install it on your system:
NOTE: BEFORE EXECUTING THIS COMMAND, MODIFY AGAIN THE ABOVE FILE: /home/teddy/opencv/build/samples/gpu/CMakeFiles/example_gpu_opengl.dir/link.txt OR WE’D GET THE SAME ERROR (PLS SEE THE ERROR HERE: /media/data/MASTER/opencv/opencv_cuda_make_install_error.txt ):

THIS TIME WE MUST USE SUDO BECAUSE THE FILE IS LOCKED:

THEN ADD THIS AT THE END OF THE SINGLE LINE LIKE ABOVE: -lGL -lGLU
THEN SAVE AND CLOSE
INSTALL AGAIN:

NOTE:
FOR COMPLETE PROCESS RESULT, SEE THIS FILE: /media/data/MASTER/opencv/opencv_cuda_make_install_success.txt

8.

9. If you’ve reached this step without an error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages
However, our ‘opencv_p27_cuda’ virtual environment is located in our home directory — thus to use OpenCV within our cv environment, we first need to sym-link OpenCV into the site-packages directory of the ‘opencv_p27_cuda’ virtual environment:

10. Check the installation opencv version:

11. EXECUTE SOME OPENCV EXAMPLES in /home/teddy/opencv/build/bin/ DIRECTORY:
a. Run this example (image grain):

b. Test Cuda Codec:

 

Installation of CUDA Toolkit 7.5 On Ubuntu 14.04

CONSIDER USING CUDA???
FROM THIS: https://en.wikipedia.org/wiki/CUDA, MY GPU Nvidia GeForce GT 520M IS SUPPORTED!!! Micro Architecture Fermi (https://en.wikipedia.org/wiki/Fermi_(microarchitecture)) version 2.1
CHECK CUDA FROM TERMINAL:
ref:http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/#axzz44a7lZNcT

INSTALLING CUDA TOOLKIT 7.5 ON UBUNTU 14.04
ref:http://www.r-tutor.com/gpu-computing/cuda-installation/cuda7.5-ubuntu
https://www.quantstart.com/articles/Installing-Nvidia-cuda-on-Ubuntu-14-04-for-Linux-GPU-Computing
https://developer.nvidia.com/cuda-downloads -> Linux -> x86_64 -> Ubuntu -> 14.04 -> deb(local)
http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
(1.9 GB)
1. The first task is to make sure that you have the GNU compiler collection (GCC) tools installed. This is carried out by installing the build-essential package:

2. Download the latest Cuda toolkit:
Manually FROM:
http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
OR:

3. PREPARE THE PACKAGE:

OK

4. Update

5. INSTALL CUDA:

SOLUTION:
ref:http://askubuntu.com/questions/672047/anyone-has-successfully-installed-cuda-7-5-on-ubuntu-14-04-3-lts-x86-64

RUN AGAIN:

6. REBOOT

7. MODIFY ENVIRONMENT VARIABLES:
As part of the CUDA environment, you should add the following in the .bashrc file of your home folder.

ADD THESE LINES AT THE BOTTOM:

THEN SAVE AND CLOSE!

8. Remember to make sure that the terminal has access to these variables:

9. Before proceeding to test the GPU cards we will ensure that the drivers are correctly installed. The following line will provide us with the driver version:

10. Check the version of the Nvidia CUDA compiler:

TRY THE CUDA 7.5 SAMPLES:
1. Copy the samples into /home/teddy/Documents/

2. GO to the samples directory:

3. Go to the deviceQuery directory THEN make:

4. run the deviceQuery script to test if we can communicate with the GPU:

 

5. Go to the bandwidthTest directory THEN make:

 

6. run bandwidthTest:

 

Installation of OpenCV 3.1 (Python 2.7) on Ubuntu 14.04

USE PYTHON 2.7 INSTEAD OF PYTHON 3.4 AND INSTALL OPENCV 3.1
WHY: ref:http://www.pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/
SOME PYTHON PACKAGES STILL USE 2.7 VERSION
http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/
1. Create a new virtualenv with python parameter point to python2.7. ref:http://stackoverflow.com/questions/5506110/is-it-possible-to-install-another-version-of-python-to-virtualenv

2. Check python version:

2.b Just Check python version in another virtualenv (opencv):

OK. PYTHON VERSION IN ANOTHER VIRTUALENV NOT CHANGED (STILL 3.4)

3. Check pip version:

4. Install numpy:

CHECK numpy VERSION:

IMPORTANT: I FOUND AN ERROR ‘The data should normally be NULL! in function allocate’ WHEN TRY TO TEST SIFT Holography Feature Matching (flann.knnMatch). PLS FIX THIS FIRST (SEE SOLUTION IN /media/data/MASTER/opencv/github_list_fish_rec.txt) BEFORE BUILD OPENCV AT STEP #5 BELOW!

5. We already cloned opencv 3.1.0 and opencv_contrib 3.1.0. so now, go to the build and do setup:

6. Compile opencv:

7. Install:

8. If you’ve reached this step without an error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages
However, our ‘opencv_p27’ virtual environment is located in our home directory — thus to use OpenCV within our cv environment, we first need to sym-link OpenCV into the site-packages directory of the ‘opencv_p27’ virtual environment:

9. Check the installation opencv version:

 

Installation of OpenCV 3.1 (Python 3.4) on Ubuntu 14.04

Installation:

  1. lets create a virtual environment with name ‘opencv’

    A FEW VIRTUALENV COMMANDS:
    a. Leave/exit virtualenv opencv, use ‘deactivate opencv’

    b. Go to or get into virtualenv opencv, use ‘workon opencv’

    c. Remove/delete virtualenv pls use rmvirtualenv <virtualenv name>. BUT PLS DEACTIVATE FIRST BEFORE REMOVING
    for example:

     
  2. COMPILE AND INSTALL OPENCV 3 LIB FOR PYTHON 3
    ref:http://www.pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/
    http://askubuntu.com/questions/94102/what-is-the-difference-between-apt-get-update-and-upgrade
    http://milq.github.io/install-opencv-ubuntu-debian/
    1. Install prerequisites

    2. Install developer tools used to compile OpenCV 3.0:

    3. Install libraries and packages used to read various image formats from disk:

    4. Install a few libraries used to read video formats from disk: (GOT ERROR)

    SKIP???
    OK HERE IS THE SOLUTION:
    (ref:http://askubuntu.com/questions/691109/how-do-i-install-ffmpeg-and-codecs)
    Install ffmpeg and the codecs:

    Re-run ‘sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev’. SHOULD WORKS NOW!!!

    e. Install GUI libs: (ref:http://milq.github.io/install-opencv-ubuntu-debian/)

    SOLUTION: Install Lib GTK so we can use OpenCV’s GUI features:

    Re-run ‘sudo apt-get install qt5-default libvtk6-dev’

    f. Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:

    g. Install documentation (doxigen):

    h. Install Parallelism and linear algebra libraries:

     

  3. We’re halfway done setting up Python. But in order to compile OpenCV 3.0 with Python 3.4+ bindings, we’ll need to install the Python 3.4+ headers and development files:
    a. sudo apt-get install python3.4-dev

    b. go into our virtualenv ‘opencv’

    c. OpenCV represents images as NumPy arrays, so we need to install NumPy into our ‘opencv’ virtual environment:

    CHECK numpy VERSION: (ITS OKAY JUST USE python IN VIRTUALENV CREATED FOR PYTHON3)

     

  4. Build and install OpenCV 3.0 with Python 3.4+ bindings
    a. deactivate opencv first (NO. DON’T DO THIS. KEEP ON THE opencv virtualenv BECAUSE WE’LL USE numpy!)

    b. Alright, our system is all setup now! Let’s pull down OpenCV from GitHub and checkout the 3.1.0 version:

    c. We’ll also need to grab the opencv_contrib repo as well (for more information as to why we need opencv_contrib, take a look at my previous OpenCV 3.0 Ubuntu install post: http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/):

    IMPORTANT: I FOUND AN ERROR ‘The data should normally be NULL! in function allocate’ WHEN TRY TO TEST SIFT Holography Feature Matching (flann.knnMatch). PLS FIX THIS FIRST (SEE SOLUTION IN /media/data/MASTER/opencv/github_list_fish_rec.txt) BEFORE BUILD OPENCV AT STEP #5 BELOW!
    d.a Time to setup the build (USE virtualenv opencv BETTER BECAUSE WE NEED numpy!!!):–> RENAMED DIR TO ‘build0’. I THINK DONT USE THIS. JUST USE d.b.

    d.b Time to setup the build (USE virtualenv opencv BETTER BECAUSE WE NEED numpy!!!):

    e. compile! (TAKE LONG TIME AND I GOT 5 GB DATA)
    make -j4
    Where the 4 can be replaced with the number of available cores on your processor to speedup the compilation time.
    To find out how many core your computer has, pls do:
    ref:http://www.howtogeek.com/howto/ubuntu/display-number-of-processors-on-linux/

    f. Assuming OpenCV 3.1 compiled without error, you can now install it on your system:

    g.

     

  5. Sym-link OpenCV 3.1
    If you’ve reached this step, OpenCV 3.1 should now be installed in /usr/local/lib/python3.4/site-packages/

    Here, our OpenCV bindings are stored under the name cv2.cpython-34m.so

    Be sure to take note of this filename, you’ll need it in just a few seconds!

    However, in order to use OpenCV 3.1 within our opencv virtual environment, we first need to sym-link OpenCV into the site-packages directory of the cv environment, like this:

    Notice how I am changing the name from cv2.cpython-34m.so to cv2.so — this is so Python can import our OpenCV bindings using the name cv2 .

    So now when you list the contents of the opencv virtual environment’s site-packages directory, you’ll see our OpenCV 3.1 bindings (the cv2.so file):

    Again, this is a very important step, so be sure that you have the cv2.so file in your virtual environment, otherwise you will not be able to import OpenCV in your Python scripts!

    5. Test out the OpenCV 3.0 and Python 3.4+ install

    As you can see, OpenCV 3.1 with Python 3.4+ bindings has been successfully installed on my Ubuntu system!

    6. EXECUTE SOME OPENCV EXAMPLES (ref:http://milq.github.io/install-opencv-ubuntu-debian/)
    a. Run this example (edge detect):

    Would open an image (fruit) with edge detect!

    b. run python samples (video capture from my laptop camera):



    7. CREATE AN EXAMPLE DEMO
    There is a sample code in /opencv/samples/cpp/example_cmake/
    To compile example.cpp successfully, we need CMakeLists.txt and Makefile
    run:

    RESULT:

    TO EXECUTE THE FILE:

    FOR MORE OPENCV TUTORIAL FOR PYTHON, PLS SEE /media/data/MASTER/opencv/opencv_python_tutorial.txt

Installation of Caffe and PyCaffe On Ubuntu 14.04

Reference:

Pre-requisites: Make sure you already installed CUDA 8, cuDNN v5.1, OpenCV 3.1, Python 3.4. Don’t forget to use python virtualenv.

Installation:

  1. Use python virtual environment ‘opencv_caffe’

    Check python version:

     

Machine Learning with OpenCV, Keras & ImageNet

Reference: http://www.pyimagesearch.com/2016/08/10/imagenet-classification-with-python-and-keras/
FranƧois Chollet pushed three Keras models (VGG16, VGG19, and ResNet50) online — these networks are pre-trained on the ImageNet dataset, meaning that they can recognize 1,000 common object classes out-of-the-box.
we can now easily apply VGG16, VGG19, and ResNet50 using Keras and Python to our own applications.
ImageNet is actually a project aimed at labeling and categorizing images into almost 22,000 categories based on a defined set of words and phrases. At the time of this writing, there are over 14 million images in the ImageNet project.

  1. Install keras and tensorflow backend
    list the package needed
  2. Clone the deep-learning-models repository
    Then, to gain access to VGG16, VGG19, and the ResNet50 architectures and pre-trained weights, you need to clone the deep-learning-models repository from GitHub:

    Then go into the dir ‘deep-learning-models’

    Notice how we have four Python files. The resnet50.pyĀ , vgg16.pyĀ , and vgg19.pyĀ  files correspond to their respective network architecture definitions.
    The imagenet_utilsĀ  file, as the name suggests, contains a couple helper functions that allow us to prepare images for classification as well as obtain the final class label predictions from the network.
  3. Write some Python code to classify image contents utilizing Convolutional Neural Networks (CNNs) pre-trained on the ImageNet dataset.
    To start, open up a new file, name it ‘test_imagenet.py’ , and insert the following code:

     
  4. Run the python script ‘test_imagenet.py’.
    NOTE: FOR THE FIRST TIME IT’S VERY SLOW BECAUSE IT DOWNLOADED VGG16 WEIGHT FROM INTERNET (~500MB)

    I GOT AN ERROR AT ‘(inID, label) = decode_predictions(preds)[0]’
    SOLUTION: COMMENT LINE #42 in ‘test_imagenet.py’ AND ADD TWO NEW LINES LIKE THIS:

    RUN AGAIN

    SUCCESS! IT CAN RECOGNIZED A DOG ‘BEAGLE’. HERE IS THE RESULTNOTE: The last error ‘NoneType’ not related with keras. TAKE CARE OF IT LATER (SOLUTION READ: http://www.pyimagesearch.com/2016/12/26/opencv-resolving-nonetype-errors/)
    TRY ANOTHER EXAMPLE:

    NOTE: The downloaded dataset would be stored in /.keras/models/ directory. the dataset filename is ‘vgg16_weights_tf_dim_ordering_tf_kernels.h5’ and the labeled index filename is ‘imagenet_class_index.json’.
  5. MORE TEST
    I have some test for recognizing fish on some pictures. I put three samples in ‘images’ directory (/deep-learning-models/images). BUT NONE OF THEM SHOW THE CORRECT RESULT. HERE ARE THE RESULTRECOGNIZED AS ‘tench’ ???
    RECOGNIZED AS ‘electric_ray’ ???
    RECOGNIZED AS ‘hermit_crab’ ???
    I THINK I NEED TO TRAIN THESE SAMPLES FIRST BEFORE IT READY TO BE RECOGNIZED