{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"%%writefile hdf5datasetwriter.py\nimport h5py\nimport os\n\nclass HDF5DatasetWriter:\n    def __init__(self, dims, outputPath, dataKey=\"images\",\n    bufSize=500):\n        # check to see if the output path exists, and if so, raise\n        # an exception\n        if os.path.exists(outputPath):\n            raise ValueError(\"The supplied ‘outputPath‘ already \"\n            \"exists and cannot be overwritten.Manually delete \"\n            \"the file before continuing.\", outputPath)\n        self.db = h5py.File(outputPath, \"w\")\n        self.data = self.db.create_dataset(dataKey, dims,\n                                           dtype=\"float\",compression='gzip',compression_opts=9)\n        self.labels = self.db.create_dataset(\"labels\", (dims[0],),\n                                             dtype=\"int\",compression='gzip',compression_opts=9)\n        self.bufSize = bufSize\n        self.buffer = {\"data\": [], \"labels\": []}\n        self.idx = 0\n\n    def add(self, rows, labels):\n\n        # add the rows and labels to the buffer\n        self.buffer[\"data\"].extend(rows)\n        self.buffer[\"labels\"].extend(labels)\n        if len(self.buffer[\"data\"]) >= self.bufSize:\n            self.flush()\n\n    def flush(self):\n\n        # write the buffers to disk then reset the buffer\n        i = self.idx + len(self.buffer[\"data\"])\n        self.data[self.idx:i] = self.buffer[\"data\"]\n        self.labels[self.idx:i] = self.buffer[\"labels\"]\n        self.idx = i\n        self.buffer = {\"data\": [], \"labels\": []}\n\n    def storeClassLabels(self, classLabels):\n\n        # create a dataset to store the actual class label names,\n        # then store the class labels\n        dt = h5py.special_dtype(vlen=str)\n        labelSet = self.db.create_dataset(\"label_names\",\n                                          (len(classLabels),), dtype=dt)\n        labelSet[:] = classLabels\n\n    def close(self):\n\n        # check to see if there are any other entries in the buffer\n        # that need to be flushed to disk\n        if len(self.buffer[\"data\"]) > 0:\n            self.flush()\n\n        # close the dataset\n        self.db.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install imutils","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications import VGG16\nfrom keras.applications import imagenet_utils\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import load_img\nfrom sklearn.preprocessing import LabelEncoder\nfrom hdf5datasetwriter import HDF5DatasetWriter\nfrom imutils import paths\nimport numpy as np\nimport progressbar\nimport random\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from zipfile import ZipFile\nwith ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zipObj:\n   # Extract all the contents of zip file in current directory\n   zipObj.extractall()\n    \nwith ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zipObj:\n   # Extract all the contents of zip file in current directory\n   zipObj.extractall()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bs=32\nprint(\"[INFO] loading images...\")\nimagePaths = list(paths.list_images('/kaggle/working/train/'))\nrandom.shuffle(imagePaths)\nlabels = [p.split(os.path.sep)[-1] for p in imagePaths]\nlabels =[ label.split('.')[-3] for label in labels]\n\nle = LabelEncoder()\nlabels = le.fit_transform(labels)\nprint(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"[INFO] loading network...\")\nBase_model = VGG16(weights=\"imagenet\", include_top=False,input_shape=(224,224,3))\nBase_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = HDF5DatasetWriter((len(imagePaths), 512 * 7 * 7),\n'CatdogFeatures2.hdf5', dataKey=\"features\", bufSize=100)\ndataset.storeClassLabels(le.classes_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"widgets = [\"Extracting Features: \", progressbar.Percentage(), \" \",\nprogressbar.Bar(), \" \", progressbar.ETA()]\npbar = progressbar.ProgressBar(maxval=len(imagePaths),\nwidgets=widgets).start()\n\nfor i in np.arange(0, len(imagePaths), bs):\n    # extract the batch of images and labels, then initialize the\n    # list of actual images that will be passed through the network\n    # for feature extraction\n    batchPaths = imagePaths[i:i + bs]\n    batchLabels = labels[i:i + bs]\n    batchImages = []\n    \n        # loop over the images and labels in the current batch\n    for (j, imagePath) in enumerate(batchPaths):\n        # load the input image using the Keras helper utility\n        # while ensuring the image is resized to 224x224 pixels\n        image = load_img(imagePath, target_size=(224, 224))\n        image = img_to_array(image)\n        # preprocess the image by (1) expanding the dimensions and\n        # (2) subtracting the mean RGB pixel intensity from the\n        # ImageNet dataset\n        image = np.expand_dims(image, axis=0)\n        image = imagenet_utils.preprocess_input(image)\n        # add the image to the batch\n        batchImages.append(image)\n    batchImages = np.vstack(batchImages)\n    features = Base_model.predict(batchImages, batch_size=bs)\n    features = features.reshape((features.shape[0], 512 * 7 * 7))\n    # add the features and labels to our HDF5 dataset\n    dataset.add(features, batchLabels)\n    pbar.update(i)\n# close the dataset\ndataset.close()\npbar.finish()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import h5py\ndb=h5py.File('/kaggle/working/CatdogFeatures2.hdf5')\nlist(db.keys())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(db['features'].shape)\nprint(db['labels'].shape)\nprint(db['label_names'].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"db = h5py.File('/kaggle/working/CatdogFeatures2.hdf5', \"r\")\ni = int(db[\"labels\"].shape[0] * 0.75)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Flatten,Dense,GlobalAveragePooling2D\n\nmodel=Sequential()\nmodel.add(GlobalAveragePooling2D(input_shape=(7,7,512)))\nmodel.add(Dense(2,activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=[\"acc\"])\nmodel.fit(db[\"features\"][:i].reshape(-1,7,7,512), db[\"labels\"][:i],epochs=5,validation_data=(db[\"features\"][i:].reshape(-1,7,7,512),db[\"labels\"][i:]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"db.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('catdogclassfyingmodel.hdf5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batchImages = []\nimagePaths = list(paths.list_images('/kaggle/working/test1/'))[10:25]\nfor image in imagePaths:\n    img=load_img(image ,target_size=(224, 224,3))\n    img=img_to_array(img)\n    img=np.expand_dims(img,axis=0)\n    img=imagenet_utils.preprocess_input(img)\n    batchImages.append(img)\nbatchImages=np.vstack(batchImages)\n    \nfeatures=Base_model.predict(batchImages)\nfeatures=features.reshape(features.shape[0],7*7*512)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nprediction=model.predict(features.reshape(-1,7,7,512)).argmax(axis=1)\n\n\n# except:\n#     prediction=model.predict(features.reshape(1,7*7*512)).argmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nclassLabels=[\"cat\",\"dog\"]\nfig=plt.figure(figsize=(50, 50))\ncolumns = 3\nrows = 5\nfor (i, imagePath) in enumerate(imagePaths):\n    # load the example image, draw the prediction, and display it\n    # to our screen\n    image = cv2.imread(imagePath)\n    image= cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n    cv2.putText(image, \"Label: {}\".format(classLabels[prediction[i]]),\n    (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)\n    fig.add_subplot(rows, columns,i+1)\n    plt.imshow( image)\nplt.show()\n    #cv2.imwrite(\"{}.jpg\".format(str(i)),image)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}