{"id":1934,"date":"2017-02-22T20:51:47","date_gmt":"2017-02-22T20:51:47","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=1934"},"modified":"2017-02-26T07:02:40","modified_gmt":"2017-02-26T07:02:40","slug":"machine-learning-with-opencv-keras-imagenet","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/22\/machine-learning-with-opencv-keras-imagenet\/","title":{"rendered":"Machine Learning with OpenCV, Keras &#038; ImageNet"},"content":{"rendered":"<p>Reference: <a href=\"http:\/\/www.pyimagesearch.com\/2016\/08\/10\/imagenet-classification-with-python-and-keras\/\">http:\/\/www.pyimagesearch.com\/2016\/08\/10\/imagenet-classification-with-python-and-keras\/<br \/>\n<\/a><a href=\"https:\/\/github.com\/fchollet\">Fran\u00e7ois Chollet<\/a> pushed three Keras models (VGG16, VGG19, and ResNet50) online \u2014 these networks are pre-trained on the ImageNet dataset, meaning that they can recognize 1,000 common object classes out-of-the-box.<br \/>\nwe can now easily apply VGG16, VGG19, and ResNet50 using Keras and Python to our own applications.<br \/>\n<a href=\"http:\/\/image-net.org\/index\">ImageNet<\/a> 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.<\/p>\n<ol>\n<li><a href=\"http:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/22\/installation-of-opencv-keras-and-tensorflow-on-ubuntu-14-04\/\">Install keras and tensorflow backend<br \/>\n<\/a>list the package needed<\/p>\n<pre class=\"lang:default decode:true \">(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip freeze\r\nappdirs==1.4.0\r\nh5py==2.6.0\r\nKeras==1.2.2\r\nnumpy==1.12.0\r\nolefile==0.44\r\npackaging==16.8\r\nPillow==4.0.0\r\nprotobuf==3.0.0\r\npyparsing==2.1.10\r\nPyYAML==3.12\r\nscikit-learn==0.18.1\r\nscipy==0.18.1\r\nsix==1.10.0\r\ntensorflow==0.11.0\r\nTheano==0.8.2<\/pre>\n<\/li>\n<li>Clone the <a href=\"https:\/\/github.com\/fchollet\/deep-learning-models\" target=\"_blank\">deep-learning-models repository<br \/>\n<\/a>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:<\/p>\n<pre class=\"lang:default decode:true\">(opencv_keras_tf) teddy@teddy-K43SJ:~$ git clone https:\/\/github.com\/fchollet\/deep-learning-models\r\nCloning into 'deep-learning-models'...\r\nremote: Counting objects: 47, done.\r\nremote: Total 47 (delta 0), reused 0 (delta 0), pack-reused 47\r\nUnpacking objects: 100% (47\/47), done.\r\nChecking connectivity... done.<\/pre>\n<p>Then go into the dir &#8216;deep-learning-models&#8217;<\/p>\n<pre class=\"lang:default decode:true \">(opencv_keras_tf) teddy@teddy-K43SJ:~$ cd deep-learning-models\/\r\n(opencv_keras_tf) teddy@teddy-K43SJ:~\/deep-learning-models$<\/pre>\n<p>Notice how we have four Python files. The resnet50.py\u00a0, vgg16.py\u00a0, and vgg19.py\u00a0 files correspond to their respective network architecture definitions.<br \/>\nThe imagenet_utils\u00a0 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.<\/li>\n<li>Write some Python code to classify image contents utilizing Convolutional Neural Networks (CNNs) pre-trained on the ImageNet dataset.<br \/>\nTo start, open up a new file, name it &#8216;test_imagenet.py&#8217; , and insert the following code:<\/p>\n<pre class=\"lang:default decode:true \"># import the necessary packages\r\nfrom keras.preprocessing import image as image_utils\r\nfrom imagenet_utils import decode_predictions\r\nfrom imagenet_utils import preprocess_input\r\nfrom vgg16 import VGG16\r\nimport numpy as np\r\nimport argparse\r\nimport cv2\r\n \r\n# construct the argument parse and parse the arguments\r\nap = argparse.ArgumentParser()\r\nap.add_argument(\"-i\", \"--image\", required=True,\r\n    help=\"path to the input image\")\r\nargs = vars(ap.parse_args())\r\n \r\n# load the original image via OpenCV so we can draw on it and display\r\n# it to our screen later\r\norig = cv2.imread(args[\"image\"])\r\n\r\n# load the input image using the Keras helper utility while ensuring\r\n# that the image is resized to 224x224 pxiels, the required input\r\n# dimensions for the network -- then convert the PIL image to a\r\n# NumPy array\r\nprint(\"[INFO] loading and preprocessing image...\")\r\nimage = image_utils.load_img(args[\"image\"], target_size=(224, 224))\r\nimage = image_utils.img_to_array(image)\r\n\r\n# our image is now represented by a NumPy array of shape (3, 224, 224),\r\n# but we need to expand the dimensions to be (1, 3, 224, 224) so we can\r\n# pass it through the network -- we'll also preprocess the image by\r\n# subtracting the mean RGB pixel intensity from the ImageNet dataset\r\nimage = np.expand_dims(image, axis=0)\r\nimage = preprocess_input(image)\r\n\r\n# load the VGG16 network\r\nprint(\"[INFO] loading network...\")\r\nmodel = VGG16(weights=\"imagenet\")\r\n \r\n# classify the image\r\nprint(\"[INFO] classifying image...\")\r\npreds = model.predict(image)\r\n(inID, label) = decode_predictions(preds)[0]\r\n \r\n# display the predictions to our screen\r\nprint(\"ImageNet ID: {}, Label: {}\".format(inID, label))\r\ncv2.putText(orig, \"Label: {}\".format(label), (10, 30),\r\n    cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)\r\ncv2.imshow(\"Classification\", orig)\r\ncv2.waitKey(0)<\/pre>\n<p>&nbsp;<\/li>\n<li>Run the python script &#8216;test_imagenet.py&#8217;.<br \/>\nNOTE: FOR THE FIRST TIME IT&#8217;S VERY SLOW BECAUSE IT DOWNLOADED VGG16 WEIGHT FROM INTERNET (~500MB)<\/p>\n<pre class=\"lang:default decode:true\">(opencv_keras_tf) teddy@teddy-K43SJ:~\/deep-learning-models$ python test_imagenet.py --image images\/dog_beagle.png\r\nUsing TensorFlow backend.\r\nI tensorflow\/stream_executor\/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally\r\nI tensorflow\/stream_executor\/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally\r\nI tensorflow\/stream_executor\/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally\r\nI tensorflow\/stream_executor\/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally\r\nI tensorflow\/stream_executor\/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally\r\n[INFO] loading and preprocessing image...\r\n[INFO] loading network...\r\nK.image_dim_ordering: tf\r\nDownloading data from https:\/\/github.com\/fchollet\/deep-learning-models\/releases\/download\/v0.1\/vgg16_weights_tf_dim_ordering_tf_kernels.h5\r\nI tensorflow\/stream_executor\/cuda\/cuda_gpu_executor.cc:925] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:951] Found device 0 with properties: \r\nname: GeForce GT 520M\r\nmajor: 2 minor: 1 memoryClockRate (GHz) 1.48\r\npciBusID 0000:01:00.0\r\nTotal memory: 963.62MiB\r\nFree memory: 780.62MiB\r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:972] DMA: 0 \r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:982] 0:   Y \r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:1014] Ignoring visible gpu device (device: 0, name: GeForce GT 520M, pci bus id: 0000:01:00.0) with Cuda compute capability 2.1. The minimum required Cuda capability is 3.0.\r\n[INFO] classifying image...\r\nDownloading data from https:\/\/s3.amazonaws.com\/deep-learning-models\/image-models\/imagenet_class_index.json\r\nTraceback (most recent call last):\r\n  File \"test_imagenet.py\", line 42, in &lt;module&gt;\r\n    (inID, label) = decode_predictions(preds)[0]\r\nValueError: too many values to unpack (expected 2)<\/pre>\n<p>I GOT AN ERROR AT &#8216;(inID, label) = decode_predictions(preds)[0]&#8217;<br \/>\nSOLUTION: COMMENT LINE #42 in &#8216;test_imagenet.py&#8217; AND ADD TWO NEW LINES LIKE THIS:<\/p>\n<pre class=\"lang:default decode:true \">...\r\npreds = model.predict(image)\r\n#(inID, label) = decode_predictions(preds)[0]\r\nP = decode_predictions(preds)\r\n(inID, label, prob) = P[0][0]\r\n...<\/pre>\n<p>RUN AGAIN<\/p>\n<pre class=\"lang:default decode:true\">(opencv_keras_tf) teddy@teddy-K43SJ:~\/deep-learning-models$ python test_imagenet.py --image images\/dog_beagle.png\r\nUsing TensorFlow backend.\r\n...\r\n[INFO] loading and preprocessing image...\r\n[INFO] loading network...\r\nK.image_dim_ordering: tf\r\nI tensorflow\/stream_executor\/cuda\/cuda_gpu_executor.cc:925] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:951] Found device 0 with properties: \r\nname: GeForce GT 520M\r\nmajor: 2 minor: 1 memoryClockRate (GHz) 1.48\r\npciBusID 0000:01:00.0\r\nTotal memory: 963.62MiB\r\nFree memory: 794.12MiB\r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:972] DMA: 0 \r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:982] 0:   Y \r\nI tensorflow\/core\/common_runtime\/gpu\/gpu_device.cc:1014] Ignoring visible gpu device (device: 0, name: GeForce GT 520M, pci bus id: 0000:01:00.0) with Cuda compute capability 2.1. The minimum required Cuda capability is 3.0.\r\n[INFO] classifying image...\r\nImageNet ID: n02088364, Label: beagle\r\nException ignored in: &lt;bound method Session.__del__ of &lt;tensorflow.python.client.session.Session object at 0x7f3732e2c400&gt;&gt;\r\nTraceback (most recent call last):\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/client\/session.py\", line 532, in __del__\r\nAttributeError: 'NoneType' object has no attribute 'TF_DeleteStatus'<\/pre>\n<p>SUCCESS! IT CAN RECOGNIZED A DOG &#8216;BEAGLE&#8217;. HERE IS THE RESULT<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/beagle.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1938\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/beagle.jpg\" alt=\"\" width=\"376\" height=\"531\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/beagle.jpg 376w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/beagle-212x300.jpg 212w\" sizes=\"auto, (max-width: 376px) 85vw, 376px\" \/><\/a>NOTE: The last error &#8216;NoneType&#8217; not related with keras. TAKE CARE OF IT LATER (SOLUTION READ: <a href=\"http:\/\/www.pyimagesearch.com\/2016\/12\/26\/opencv-resolving-nonetype-errors\/\">http:\/\/www.pyimagesearch.com\/2016\/12\/26\/opencv-resolving-nonetype-errors\/<\/a>)<br \/>\nTRY ANOTHER EXAMPLE:<\/p>\n<pre class=\"lang:default decode:true \">(opencv_keras_tf) teddy@teddy-K43SJ:~\/deep-learning-models$ python test_imagenet.py --image images\/space_shuttle.png<\/pre>\n<p><a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/space-shuttle.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1940\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/space-shuttle.jpg\" alt=\"\" width=\"600\" height=\"509\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/space-shuttle.jpg 600w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/space-shuttle-300x255.jpg 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a>NOTE: The downloaded dataset would be stored in \/.keras\/models\/ directory. the dataset filename is &#8216;vgg16_weights_tf_dim_ordering_tf_kernels.h5&#8217; and the labeled index filename is &#8216;imagenet_class_index.json&#8217;.<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/vgg16-dataset.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2009\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/vgg16-dataset.jpg\" alt=\"\" width=\"527\" height=\"235\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/vgg16-dataset.jpg 527w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/vgg16-dataset-300x134.jpg 300w\" sizes=\"auto, (max-width: 527px) 85vw, 527px\" \/><\/a><\/li>\n<li>MORE TEST<br \/>\nI have some test for recognizing fish on some pictures. I put three samples in &#8216;images&#8217; directory (\/deep-learning-models\/images).<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1942\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1.jpg\" alt=\"\" width=\"500\" height=\"346\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1.jpg 500w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1-300x208.jpg 300w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a> <a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1943\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2.jpg\" alt=\"\" width=\"380\" height=\"254\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2.jpg 380w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2-300x201.jpg 300w\" sizes=\"auto, (max-width: 380px) 85vw, 380px\" \/><\/a> <a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1944\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3.jpg\" alt=\"\" width=\"451\" height=\"500\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3.jpg 451w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3-271x300.jpg 271w\" sizes=\"auto, (max-width: 451px) 85vw, 451px\" \/><\/a>BUT NONE OF THEM SHOW THE CORRECT RESULT. HERE ARE THE RESULT<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1_tench.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1945\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1_tench.jpg\" alt=\"\" width=\"495\" height=\"372\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1_tench.jpg 495w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish1_tench-300x225.jpg 300w\" sizes=\"auto, (max-width: 495px) 85vw, 495px\" \/><\/a>RECOGNIZED AS &#8216;tench&#8217; ???<br \/>\n<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2_electric_ray.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1946\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2_electric_ray.jpg\" alt=\"\" width=\"377\" height=\"279\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2_electric_ray.jpg 377w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish2_electric_ray-300x222.jpg 300w\" sizes=\"auto, (max-width: 377px) 85vw, 377px\" \/><\/a>RECOGNIZED AS &#8216;electric_ray&#8217; ???<br \/>\n<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3_hermit_crab.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1947\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3_hermit_crab.jpg\" alt=\"\" width=\"439\" height=\"502\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3_hermit_crab.jpg 439w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/02\/fish3_hermit_crab-262x300.jpg 262w\" sizes=\"auto, (max-width: 439px) 85vw, 439px\" \/><\/a>RECOGNIZED AS &#8216;hermit_crab&#8217; ???<br \/>\nI THINK I NEED TO TRAIN THESE SAMPLES FIRST BEFORE IT READY TO BE RECOGNIZED<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Reference: http:\/\/www.pyimagesearch.com\/2016\/08\/10\/imagenet-classification-with-python-and-keras\/ Fran\u00e7ois Chollet pushed three Keras models (VGG16, VGG19, and ResNet50) online \u2014 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 &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/22\/machine-learning-with-opencv-keras-imagenet\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Machine Learning with OpenCV, Keras &#038; ImageNet&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,19],"tags":[],"class_list":["post-1934","post","type-post","status-publish","format-standard","hentry","category-opencv","category-python"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=1934"}],"version-history":[{"count":8,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1934\/revisions"}],"predecessor-version":[{"id":2010,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1934\/revisions\/2010"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=1934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=1934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=1934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}