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

Leave a Reply

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