{"id":1997,"date":"2017-02-24T09:48:34","date_gmt":"2017-02-24T09:48:34","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=1997"},"modified":"2017-02-26T09:21:29","modified_gmt":"2017-02-26T09:21:29","slug":"cnn-and-tensorflow-for-recognizing-cats-and-dogs","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/24\/cnn-and-tensorflow-for-recognizing-cats-and-dogs\/","title":{"rendered":"CNN and TensorFlow For Recognizing Dogs and Cats"},"content":{"rendered":"<p>Reference: <a href=\"https:\/\/blog.keras.io\/building-powerful-image-classification-models-using-very-little-data.html\">https:\/\/blog.keras.io\/building-powerful-image-classification-models-using-very-little-data.html<br \/>\n<\/a><a href=\"https:\/\/github.com\/abnera\/image-classifier\">https:\/\/github.com\/abnera\/image-classifier<br \/>\n<\/a><a href=\"https:\/\/github.com\/abnera\/image-classifier\/blob\/master\/code\/fine_tune.py\">https:\/\/github.com\/abnera\/image-classifier\/blob\/master\/code\/fine_tune.py<\/a><br \/>\n<a href=\"http:\/\/www.subsubroutine.com\/sub-subroutine\/2016\/9\/30\/cats-and-dogs-and-convolutional-neural-networks\">http:\/\/www.subsubroutine.com\/sub-subroutine\/2016\/9\/30\/cats-and-dogs-and-convolutional-neural-networks<\/a><\/p>\n<p>NOTE: THIS MAY BE MISLEADING BECAUSE I CAN&#8217;T USE TENSORFLOW AS HERE BUT THEANO ALTHOUGH I INTEND TO USE TENSORFLOW. PLS READ THE REST WHY.<\/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\/\">Make sure keras and tensorflow already installed<\/a>.<\/li>\n<li>Download the data needed from <a href=\"https:\/\/www.kaggle.com\/c\/dogs-vs-cats\">Kaggle&#8217;s Dogs vs Cats<\/a>. Download train.zip dan test1.zip<\/li>\n<li>Create a new directory &#8216;dogs-vs-cats-tf&#8217;<br \/>\nThen in the\u00a0&#8216;dogs-vs-cats-tf&#8217; directory create a new directory &#8216;data&#8217;<br \/>\nthen in &#8216;data&#8217; directory, create a few new dirs &#8216;train\/dogs&#8217;, &#8216;train\/cats&#8217;, &#8216;validation\/dogs&#8217; and &#8216;validation\/cats&#8217;.<\/li>\n<li>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&#8217;d put them in their respective directories (data\/train\/dogs and data\/train\/cats).<br \/>\nThen we use additional 400 samples for each class as validation data, to evaluate our models. We&#8217;d put them in their respective directories (data\/validation\/dogs and data\/validation\/cats).<br \/>\nHere are the clear instruction:<br \/>\n&#8211; cat pictures index 0-999 in data\/train\/cats<br \/>\n&#8211; cat pictures index 1000-1399 in data\/validation\/cats<br \/>\n&#8211; dogs pictures index 0-999 in data\/train\/dogs<br \/>\n&#8211; dog pictures index 1000-1399 in data\/validation\/dogs<\/p>\n<pre class=\"lang:default decode:true \">Example: Dogs vs Cats (Directory Structure)\r\ndata\/\r\n    train\/\r\n        dogs\/\r\n            dog.0.jpg\r\n            dog.1.jpg\r\n            ...\r\n            dog.999.jpg\r\n        cats\/\r\n            cat.0.jpg\r\n            cat.1.jpg\r\n            ...\r\n            cat.999.jpg\r\n    validation\/\r\n        dogs\/\r\n            dog.1000.jpg\r\n            dog.1001.jpg\r\n            ...\r\n            dog.1399.jpg\r\n        cats\/\r\n            cat.1000.jpg\r\n            cat.1001.jpg\r\n            ...\r\n            cat.1399.jpg<\/pre>\n<p>&nbsp;<\/li>\n<li>in &#8216;dogs-vs-cats-tf&#8217; directory, create a new python file &#8216;dogs-vs-cats1.py&#8217;. Here is the content (ref: <a href=\"https:\/\/gist.github.com\/fchollet\/0830affa1f7f19fd47b06d4cf89ed44d\">https:\/\/gist.github.com\/fchollet\/0830affa1f7f19fd47b06d4cf89ed44d<\/a>).\n<pre class=\"lang:default decode:true\">'''This script goes along the blog post\r\n\"Building powerful image classification models using very little data\"\r\nfrom blog.keras.io.\r\n\r\nIt uses data that can be downloaded at:\r\nhttps:\/\/www.kaggle.com\/c\/dogs-vs-cats\/data\r\n\r\nIn our setup, we:\r\n- created a data\/ folder\r\n- created train\/ and validation\/ subfolders inside data\/\r\n- created cats\/ and dogs\/ subfolders inside train\/ and validation\/\r\n- put the cat pictures index 0-999 in data\/train\/cats\r\n- put the cat pictures index 1000-1399 in data\/validation\/cats\r\n- put the dogs pictures index 0-999 in data\/train\/dogs\r\n- put the dog pictures index 1000-1399 in data\/validation\/dogs\r\n\r\nSo that we have 1000 training examples for each class, and 400 validation examples for each class.\r\n\r\nIn summary, this is our directory structure:\r\n```\r\ndata\/\r\n    train\/\r\n        dogs\/\r\n            dog.0.jpg\r\n            dog.1.jpg\r\n            ...\r\n            dog.999.jpg\r\n        cats\/\r\n            cat.0.jpg\r\n            cat.1.jpg\r\n            ...\r\n            cat.999.jpg\r\n    validation\/\r\n        dogs\/\r\n            dog.1000.jpg\r\n            dog.1001.jpg\r\n            ...\r\n            dog.1399.jpg\r\n        cats\/\r\n            cat.1000.jpg\r\n            cat.1001.jpg\r\n            ...\r\n            cat.1399.jpg\r\n```\r\n'''\r\n\r\nfrom keras.preprocessing.image import ImageDataGenerator\r\nfrom keras.models import Sequential\r\nfrom keras.layers import Convolution2D, MaxPooling2D\r\nfrom keras.layers import Activation, Dropout, Flatten, Dense\r\n\r\n\r\n# dimensions of our images.\r\nimg_width, img_height = 150, 150\r\n\r\ntrain_data_dir = 'data\/train'\r\nvalidation_data_dir = 'data\/validation'\r\nnb_train_samples = 2000\r\nnb_validation_samples = 800\r\nnb_epoch = 50\r\n\r\n\r\nmodel = Sequential()\r\nmodel.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height)))\r\nmodel.add(Activation('relu'))\r\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\r\n\r\nmodel.add(Convolution2D(32, 3, 3))\r\nmodel.add(Activation('relu'))\r\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\r\n\r\nmodel.add(Convolution2D(64, 3, 3))\r\nmodel.add(Activation('relu'))\r\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\r\n\r\nmodel.add(Flatten())\r\nmodel.add(Dense(64))\r\nmodel.add(Activation('relu'))\r\nmodel.add(Dropout(0.5))\r\nmodel.add(Dense(1))\r\nmodel.add(Activation('sigmoid'))\r\n\r\nmodel.compile(loss='binary_crossentropy',\r\n              optimizer='rmsprop',\r\n              metrics=['accuracy'])\r\n\r\n# this is the augmentation configuration we will use for training\r\ntrain_datagen = ImageDataGenerator(\r\n        rescale=1.\/255,\r\n        shear_range=0.2,\r\n        zoom_range=0.2,\r\n        horizontal_flip=True)\r\n\r\n# this is the augmentation configuration we will use for testing:\r\n# only rescaling\r\ntest_datagen = ImageDataGenerator(rescale=1.\/255)\r\n\r\ntrain_generator = train_datagen.flow_from_directory(\r\n        train_data_dir,\r\n        target_size=(img_width, img_height),\r\n        batch_size=32,\r\n        class_mode='binary')\r\n\r\nvalidation_generator = test_datagen.flow_from_directory(\r\n        validation_data_dir,\r\n        target_size=(img_width, img_height),\r\n        batch_size=32,\r\n        class_mode='binary')\r\n\r\nmodel.fit_generator(\r\n        train_generator,\r\n        samples_per_epoch=nb_train_samples,\r\n        nb_epoch=nb_epoch,\r\n        validation_data=validation_generator,\r\n        nb_val_samples=nb_validation_samples)\r\n\r\n#model.load_weights('first_try.h5')\r\nmodel.save_weights('first_try.h5')<\/pre>\n<p>&nbsp;<\/li>\n<li>Run the python file.<br \/>\nNOTE: In <a href=\"http:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/22\/installation-of-opencv-keras-and-tensorflow-on-ubuntu-14-04\/\">http:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/22\/installation-of-opencv-keras-and-tensorflow-on-ubuntu-14-04\/<\/a>, I use virtualenv &#8216;opencv_keras_tf&#8217; 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<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy-K43SJ:~\/Documents\/python\/dogs-vs-cats-tf$ python dogs-vs-cats1.py \r\nTraceback (most recent call last):\r\n  File \"dogs-vs-cats1.py\", line 43, in &lt;module&gt;\r\n    from keras.preprocessing.image import ImageDataGenerator\r\nImportError: No module named keras.preprocessing.image<\/pre>\n<p>The error &#8216;ImportError: No module named keras.preprocessing.image&#8217; because I ONLY install keras in virtualenv &#8216;opencv_keras_tf&#8217; NOT GLOBALLY!<br \/>\nOK. Use virtualenv &#8216;opencv_keras_tf&#8217; then run the python file.<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy-K43SJ:~\/Documents\/python\/dogs-vs-cats-tf$ workon opencv_keras_tf<\/pre>\n<pre class=\"lang:default decode:true \">(opencv_keras_tf) teddy@teddy-K43SJ:~\/Documents\/python\/dogs-vs-cats-tf$ python dogs-vs-cats1.py \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\nTraceback (most recent call last):\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/framework\/common_shapes.py\", line 594, in call_cpp_shape_fn\r\n    status)\r\n  File \"\/usr\/lib\/python3.4\/contextlib.py\", line 66, in __exit__\r\n    next(self.gen)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/framework\/errors.py\", line 463, in raise_exception_on_not_ok_status\r\n    pywrap_tensorflow.TF_GetCode(status))\r\ntensorflow.python.framework.errors.InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n  File \"dogs-vs-cats1.py\", line 62, in &lt;module&gt;\r\n    model.add(MaxPooling2D(pool_size=(2, 2)))\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/models.py\", line 332, in add\r\n    output_tensor = layer(self.outputs[0])\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/engine\/topology.py\", line 572, in __call__\r\n    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/engine\/topology.py\", line 635, in add_inbound_node\r\n    Node.create_node(self, inbound_layers, node_indices, tensor_indices)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/engine\/topology.py\", line 166, in create_node\r\n    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/layers\/pooling.py\", line 160, in call\r\n    dim_ordering=self.dim_ordering)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/layers\/pooling.py\", line 210, in _pooling_function\r\n    pool_mode='max')\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/backend\/tensorflow_backend.py\", line 2866, in pool2d\r\n    x = tf.nn.max_pool(x, pool_size, strides, padding=padding)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/ops\/nn_ops.py\", line 850, in max_pool\r\n    name=name)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/ops\/gen_nn_ops.py\", line 1440, in _max_pool\r\n    data_format=data_format, name=name)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/framework\/op_def_library.py\", line 749, in apply_op\r\n    op_def=op_def)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/framework\/ops.py\", line 2382, in create_op\r\n    set_shapes_for_outputs(ret)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/framework\/ops.py\", line 1783, in set_shapes_for_outputs\r\n    shapes = shape_func(op)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/tensorflow\/python\/framework\/common_shapes.py\", line 596, in call_cpp_shape_fn\r\n    raise ValueError(err.message)\r\nValueError: Negative dimension size caused by subtracting 2 from 1<\/pre>\n<p>I GOT AN ERROR: &#8216;ValueError: Negative dimension size caused by subtracting 2 from 1&#8217;<br \/>\nSOLUTION: Modify keras.json and replace &#8216;tf&#8217; in &#8216;image_dim_ordering&#8217; to &#8216;th&#8217; (WHY &#8216;tf&#8217; DIDN&#8217;T WORK???)<\/p>\n<pre class=\"lang:default decode:true\">(opencv_keras_tf) teddy@teddy-K43SJ:~\/Documents\/python\/dogs-vs-cats-tf$ gedit ~\/.keras\/keras.json<\/pre>\n<p>SO HERE IS THE CONTENT OF ~\/.keras\/keras.json<\/p>\n<pre class=\"lang:default decode:true \">{\r\n    \"backend\": \"tensorflow\",\r\n    \"image_dim_ordering\": \"th\",\r\n    \"epsilon\": 1e-07,\r\n    \"floatx\": \"float32\"\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>RUN AGAIN (IT&#8217;S WORKING NOW):<br \/>\nSTART AT 2017-02-24 10:08PM<\/p>\n<pre class=\"lang:default decode:true\">(opencv_keras_tf) teddy@teddy-K43SJ:~\/Documents\/python\/dogs-vs-cats-tf$ python dogs-vs-cats1.py \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\nFound 2000 images belonging to 2 classes.\r\nFound 800 images belonging to 2 classes.\r\nEpoch 1\/50\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: 758.38MiB\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  32\/2000 [..............................] - ETA: 536s - loss: 0.6856 - acc: 0.6  \r\n...\r\n2000\/2000 [==============================] - 589s - loss: 0.7349 - acc: 0.5115 - val_loss: 0.6872 - val_acc: 0.5563\r\nEpoch 2\/50\r\n  32\/2000 [..............................] - ETA: 451s - loss: 0.6873 - acc: 0.5  \r\n...\r\n2000\/2000 [==============================] - 630s - loss: 0.6964 - acc: 0.5695 - val_loss: 0.6541 - val_acc: 0.6663\r\nEpoch 3\/50\r\n 32\/2000 [..............................] - ETA: 499s - loss: 0.6306 - acc: 0.7 \r\n...\r\n2000\/2000 [==============================] - 599s - loss: 0.6610 - acc: 0.6070 - val_loss: 0.6264 - val_acc: 0.6412<\/pre>\n<p>NOTE: Read this about CUDA compute capability : <a href=\"http:\/\/stackoverflow.com\/questions\/10961476\/what-are-the-differences-between-cuda-compute-capabilities\">http:\/\/stackoverflow.com\/questions\/10961476\/what-are-the-differences-between-cuda-compute-capabilities<\/a><br \/>\nIT SEEMS DEPEND ON THE HARDWARE! NOTHING CAN DO TO IMPROVE THE COMPUTE CAPABILITY!<br \/>\nAFTER READING THIS <a href=\"https:\/\/www.tensorflow.org\/versions\/r0.10\/get_started\/os_setup#installing_from_sources\">https:\/\/www.tensorflow.org\/versions\/r0.10\/get_started\/os_setup#installing_from_sources<\/a> AND <a href=\"http:\/\/stackoverflow.com\/questions\/38542763\/how-can-i-make-tensorflow-run-on-a-gpu-with-capability-2-0\">http:\/\/stackoverflow.com\/questions\/38542763\/how-can-i-make-tensorflow-run-on-a-gpu-with-capability-2-0<\/a>, I GOT SAD NEWS THAT<br \/>\n&#8220;<span style=\"color: #ffff00;\">In order to build or run TensorFlow with GPU support, both NVIDIA&#8217;s Cuda Toolkit (&gt;= 7.0) and cuDNN (&gt;= v3) need to be installed. <\/span><br \/>\n<span style=\"color: #ffff00;\">TensorFlow GPU support requires having a GPU card with NVidia Compute Capability &gt;= 3.0.<\/span>&#8221;<br \/>\n<span style=\"background-color: #ff0000;\">SO MY GPU GeForce GT 520M THAT ONLY HAS CUDA COMPUTE CAPABILITY 2.1(REF: <span style=\"color: #ccffff;\"><a style=\"color: #ccffff;\" href=\"https:\/\/en.wikipedia.org\/wiki\/CUDA\">https:\/\/en.wikipedia.org\/wiki\/CUDA<\/a><\/span>) CAN&#8217;T BE USED FOR TENSORFLOW!!!I ONLY CAN USE CPU!!!<\/span><br \/>\nI STOPPED AFTER THE 3rd EPOCH (2017-02-14 10:39) BECAUSE I HAVE GOT ANOTHER THING TO DO. HOPEFULLY CAN RESUME THIS TOMORROW.<br \/>\nI DID IT AGAIN!<\/p>\n<pre class=\"lang:default decode:true \">epoch 1\/50 start at 2017-02-25 08:45PM\r\n2000\/2000 [==============================] - 500s - loss: 0.6997 - acc: 0.5270 - val_loss: 0.6800 - val_acc: 0.5025\r\nEpoch 2\/50\r\n2000\/2000 [==============================] - 533s - loss: 0.6652 - acc: 0.6100 - val_loss: 0.6326 - val_acc: 0.6613\r\nEpoch 3\/50\r\n2000\/2000 [==============================] - 539s - loss: 0.6291 - acc: 0.6540 - val_loss: 0.6102 - val_acc: 0.6700\r\nEpoch 4\/50\r\n2000\/2000 [==============================] - 559s - loss: 0.6141 - acc: 0.6875 - val_loss: 0.5782 - val_acc: 0.6863\r\nEpoch 5\/50\r\n2000\/2000 [==============================] - 528s - loss: 0.5825 - acc: 0.6920 - val_loss: 0.6414 - val_acc: 0.6112\r\nEpoch 6\/50\r\n2000\/2000 [==============================] - 526s - loss: 0.5719 - acc: 0.7085 - val_loss: 0.5651 - val_acc: 0.6750\r\nEpoch 7\/50\r\n2000\/2000 [==============================] - 513s - loss: 0.5550 - acc: 0.7240 - val_loss: 0.5450 - val_acc: 0.7037\r\nEpoch 8\/50\r\n...\r\n2000\/2000 [==============================] - 495s - loss: 0.4445 - acc: 0.7960 - val_loss: 0.4813 - val_acc: 0.7788\r\nEpoch 17\/50\r\n2000\/2000 [==============================] - 495s - loss: 0.4424 - acc: 0.7930 - val_loss: 0.4919 - val_acc: 0.7638\r\nEpoch 18\/50\r\n2000\/2000 [==============================] - 495s - loss: 0.4343 - acc: 0.8065 - val_loss: 0.4890 - val_acc: 0.7625\r\nEpoch 19\/50\r\n2000\/2000 [==============================] - 494s - loss: 0.4202 - acc: 0.8085 - val_loss: 0.5072 - val_acc: 0.7538\r\nEpoch 20\/50\r\n2000\/2000 [==============================] - 493s - loss: 0.4146 - acc: 0.8230 - val_loss: 0.4561 - val_acc: 0.7900\r\nEpoch 21\/50\r\n2000\/2000 [==============================] - 492s - loss: 0.4210 - acc: 0.8125 - val_loss: 0.5270 - val_acc: 0.7450\r\nEpoch 22\/50\r\n2000\/2000 [==============================] - 493s - loss: 0.4179 - acc: 0.8065 - val_loss: 0.4678 - val_acc: 0.7925\r\nEpoch 23\/50\r\n2000\/2000 [==============================] - 494s - loss: 0.3911 - acc: 0.8245 - val_loss: 0.4943 - val_acc: 0.7488\r\nEpoch 24\/50\r\n2000\/2000 [==============================] - 493s - loss: 0.3944 - acc: 0.8230 - val_loss: 0.5489 - val_acc: 0.7612\r\nEpoch 25\/50\r\n2000\/2000 [==============================] - 494s - loss: 0.3874 - acc: 0.8280 - val_loss: 0.5076 - val_acc: 0.7762\r\nEpoch 26\/50\r\n2000\/2000 [==============================] - 493s - loss: 0.3863 - acc: 0.8320 - val_loss: 0.5106 - val_acc: 0.7550\r\nEpoch 27\/50\r\n2000\/2000 [==============================] - 494s - loss: 0.3838 - acc: 0.8370 - val_loss: 0.5342 - val_acc: 0.7588\r\nEpoch 28\/50\r\n2000\/2000 [==============================] - 491s - loss: 0.3684 - acc: 0.8400 - val_loss: 0.7603 - val_acc: 0.7400\r\nEpoch 29\/50\r\n2000\/2000 [==============================] - 495s - loss: 0.3615 - acc: 0.8335 - val_loss: 0.5512 - val_acc: 0.7837\r\nEpoch 30\/50\r\n2000\/2000 [==============================] - 490s - loss: 0.3677 - acc: 0.8370 - val_loss: 0.4612 - val_acc: 0.7950\r\nEpoch 31\/50\r\n2000\/2000 [==============================] - 493s - loss: 0.3570 - acc: 0.8395 - val_loss: 0.4771 - val_acc: 0.7775\r\nEpoch 32\/50\r\n2000\/2000 [==============================] - 494s - loss: 0.3334 - acc: 0.8625 - val_loss: 0.5000 - val_acc: 0.7963\r\nEpoch 33\/50\r\n2000\/2000 [==============================] - 490s - loss: 0.3448 - acc: 0.8475 - val_loss: 0.4768 - val_acc: 0.7750\r\nEpoch 34\/50\r\n2000\/2000 [==============================] - 493s - loss: 0.3495 - acc: 0.8535 - val_loss: 0.6233 - val_acc: 0.7412\r\nEpoch 35\/50\r\n2000\/2000 [==============================] - 492s - loss: 0.3237 - acc: 0.8505 - val_loss: 0.5072 - val_acc: 0.8125\r\nEpoch 36\/50\r\n2000\/2000 [==============================] - 490s - loss: 0.3285 - acc: 0.8565 - val_loss: 0.5616 - val_acc: 0.7875\r\nEpoch 37\/50\r\n2000\/2000 [==============================] - 490s - loss: 0.3200 - acc: 0.8690 - val_loss: 0.7266 - val_acc: 0.7412\r\nEpoch 38\/50\r\n2000\/2000 [==============================] - 491s - loss: 0.3317 - acc: 0.8595 - val_loss: 0.9790 - val_acc: 0.7113\r\nEpoch 39\/50\r\n2000\/2000 [==============================] - 489s - loss: 0.3331 - acc: 0.8615 - val_loss: 0.5178 - val_acc: 0.7725\r\nEpoch 40\/50\r\n2000\/2000 [==============================] - 489s - loss: 0.3212 - acc: 0.8655 - val_loss: 0.4486 - val_acc: 0.8087\r\nEpoch 41\/50\r\n2000\/2000 [==============================] - 491s - loss: 0.3147 - acc: 0.8725 - val_loss: 0.5545 - val_acc: 0.7887\r\nEpoch 42\/50\r\n2000\/2000 [==============================] - 488s - loss: 0.3199 - acc: 0.8650 - val_loss: 0.5090 - val_acc: 0.8087\r\nEpoch 43\/50\r\n2000\/2000 [==============================] - 491s - loss: 0.3182 - acc: 0.8605 - val_loss: 0.5351 - val_acc: 0.7950\r\nEpoch 44\/50\r\n2000\/2000 [==============================] - 490s - loss: 0.3030 - acc: 0.8665 - val_loss: 0.4939 - val_acc: 0.7963\r\nEpoch 45\/50\r\n2000\/2000 [==============================] - 490s - loss: 0.3026 - acc: 0.8705 - val_loss: 0.6770 - val_acc: 0.7762\r\nEpoch 46\/50\r\n2000\/2000 [==============================] - 488s - loss: 0.3124 - acc: 0.8625 - val_loss: 0.5910 - val_acc: 0.7850\r\nEpoch 47\/50\r\n2000\/2000 [==============================] - 518s - loss: 0.3069 - acc: 0.8725 - val_loss: 0.5056 - val_acc: 0.8063\r\nEpoch 48\/50   START AT 2017-02-26 03:14AM\r\n2000\/2000 [==============================] - 526s - loss: 0.3038 - acc: 0.8790 - val_loss: 0.5111 - val_acc: 0.7937\r\nEpoch 49\/50\r\n2000\/2000 [==============================] - 501s - loss: 0.2811 - acc: 0.8870 - val_loss: 0.4624 - val_acc: 0.8013\r\nEpoch 50\/50\r\n2000\/2000 [==============================] - 497s - loss: 0.2817 - acc: 0.8815 - val_loss: 0.7820 - val_acc: 0.7662\r\nEND AT 2017-02-26 03:45AM<\/pre>\n<p>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%???<br \/>\nAND AT THE END I GOT THIS ERROR<\/p>\n<pre class=\"lang:default decode:true\">Traceback (most recent call last):\r\n  File \"dogs-vs-cats1.py\", line 117, in &lt;module&gt;\r\n    model.load_weights('first_try.h5')\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/keras\/engine\/topology.py\", line 2702, in load_weights\r\n    f = h5py.File(filepath, mode='r')\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/h5py\/_hl\/files.py\", line 272, in __init__\r\n    fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)\r\n  File \"\/home\/teddy\/.virtualenvs\/opencv_keras_tf\/lib\/python3.4\/site-packages\/h5py\/_hl\/files.py\", line 92, in make_fid\r\n    fid = h5f.open(name, flags, fapl=fapl)\r\n  File \"h5py\/_objects.pyx\", line 54, in h5py._objects.with_phil.wrapper (\/tmp\/pip-eeirwumi-build\/h5py\/_objects.c:2684)\r\n  File \"h5py\/_objects.pyx\", line 55, in h5py._objects.with_phil.wrapper (\/tmp\/pip-eeirwumi-build\/h5py\/_objects.c:2642)\r\n  File \"h5py\/h5f.pyx\", line 76, in h5py.h5f.open (\/tmp\/pip-eeirwumi-build\/h5py\/h5f.c:1930)\r\nOSError: Unable to open file (Unable to open file: name = 'first_try.h5', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0)\r\nException ignored in: &lt;bound method Session.__del__ of &lt;tensorflow.python.client.session.Session object at 0x7f2363381d30&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>ALSO I DONT FIND &#8216;first_try.h5&#8217; FILE!!!! &#8211;&gt; SOLUTION: In the file &#8216;dogs-vs-cats1.py&#8217;, replace &#8216;model.load_weights(&#8216;first_try.h5&#8242;)&#8217; with &#8216;model.save_weights(&#8216;first_try.h5&#8242;)&#8217; OR LIKE THIS:<\/p>\n<pre class=\"lang:default decode:true \">...\r\n#model.load_weights('first_try.h5')\r\nmodel.save_weights('first_try.h5')<\/pre>\n<p>NOW I GOT THE TRAINING FILE &#8216;first_try.h5&#8217; IN &#8216;dogs-vs-cats-tf&#8217; DIRECTORY!<br \/>\nNOTE: I ONLY SET &#8216;nb_epoch = 5&#8217; 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<\/p>\n<pre class=\"lang:default decode:true \">2000\/2000 [==============================] - 558s - loss: 0.5839 - acc: 0.7015 - val_loss: 0.5949 - val_acc: 0.6813<\/pre>\n<p>OK. NOW HOW TO USE THE TRAINING FILE FOR TESTING!!!<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;T USE TENSORFLOW AS HERE BUT THEANO ALTHOUGH I INTEND TO USE TENSORFLOW. PLS READ THE REST WHY. Make sure keras and tensorflow already installed. Download the data needed from Kaggle&#8217;s Dogs vs Cats. Download train.zip dan test1.zip Create a new directory &#8216;dogs-vs-cats-tf&#8217; &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2017\/02\/24\/cnn-and-tensorflow-for-recognizing-cats-and-dogs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;CNN and TensorFlow For Recognizing Dogs and Cats&#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-1997","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\/1997","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=1997"}],"version-history":[{"count":16,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1997\/revisions"}],"predecessor-version":[{"id":2019,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1997\/revisions\/2019"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=1997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=1997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=1997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}