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!!!

Leave a Reply

Your email address will not be published. Required fields are marked *