{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-04T16:21:13.511394Z","iopub.execute_input":"2021-06-04T16:21:13.511917Z","iopub.status.idle":"2021-06-04T16:21:13.524198Z","shell.execute_reply.started":"2021-06-04T16:21:13.511834Z","shell.execute_reply":"2021-06-04T16:21:13.523208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:21:13.525844Z","iopub.execute_input":"2021-06-04T16:21:13.526145Z","iopub.status.idle":"2021-06-04T16:21:13.535044Z","shell.execute_reply.started":"2021-06-04T16:21:13.526116Z","shell.execute_reply":"2021-06-04T16:21:13.534202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_ref = zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r')\nzip_ref.extractall('/tmp/train')#extracting data\nzip_ref.close()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:21:13.536112Z","iopub.execute_input":"2021-06-04T16:21:13.536484Z","iopub.status.idle":"2021-06-04T16:21:26.899195Z","shell.execute_reply.started":"2021-06-04T16:21:13.536457Z","shell.execute_reply":"2021-06-04T16:21:26.898173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"/tmp/train/train\")\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'label': categories\n})\n","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:21:26.900392Z","iopub.execute_input":"2021-06-04T16:21:26.900701Z","iopub.status.idle":"2021-06-04T16:21:26.954212Z","shell.execute_reply.started":"2021-06-04T16:21:26.900673Z","shell.execute_reply":"2021-06-04T16:21:26.952143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:21:26.956619Z","iopub.execute_input":"2021-06-04T16:21:26.956909Z","iopub.status.idle":"2021-06-04T16:21:27.064001Z","shell.execute_reply.started":"2021-06-04T16:21:26.956875Z","shell.execute_reply":"2021-06-04T16:21:27.063066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, Activation, Input\nimport pandas as pd\nimport os\nimport numpy as np\nfrom keras.models import load_model\nimport tensorflow as tf\nimport keras.backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import img_to_array\nfrom keras.utils import to_categorical\nimport matplotlib.pyplot as plt\nimport random\nimport cv2\nimport time\nimport h5py\n","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:21:27.065443Z","iopub.execute_input":"2021-06-04T16:21:27.065749Z","iopub.status.idle":"2021-06-04T16:21:33.690096Z","shell.execute_reply.started":"2021-06-04T16:21:27.065719Z","shell.execute_reply":"2021-06-04T16:21:33.68906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"[INFO] loading images...\")\ndata = []\nlabels = []\n\n# shuffling images \n\npath=\"/tmp/train/train/\"\n\n\n#image augmentation \ntemp = []\nfor img_name in df.filename:\n    image_path = path+img_name\n    img = cv2.imread(image_path, 0)\n    img = cv2.resize(img, (128, 128))#resizing for augmentation\n    img = img.reshape(1, 128 * 128)#reshaping for augmentation\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# This last bit is to have the images coverted from the default BGR from cv2 to RGB to correctly visualize the dataset (it has no effect over the training)\n    img = img.astype('float32')\n    temp.append(img)\n\n\ntrain_x = np.stack(temp)\n\ntrain_x /= 255.0\n\ntrain_x = train_x.reshape(len(df), 128,128,1).astype('float32')\ntrain_y = to_categorical(df.label.values,num_classes=2)\n\nprint(\"no_prob\")\ntrain_x=np.array(train_x)\ntrain_y=np.array(train_y)\n\nx_train,x_test,y_train,y_test=train_test_split(train_x,train_y,test_size=0.2,random_state=4)\n\naug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,\n    height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,\n    horizontal_flip=True, fill_mode=\"nearest\")\n\n\naug.fit(train_x)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:21:33.69144Z","iopub.execute_input":"2021-06-04T16:21:33.691747Z","iopub.status.idle":"2021-06-04T16:22:04.490531Z","shell.execute_reply.started":"2021-06-04T16:21:33.69172Z","shell.execute_reply":"2021-06-04T16:22:04.489479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNNmodel:\n    @staticmethod\n    def build(width, height, depth, classes):\n        # initializing the model\n        model = Sequential()\n        inputShape = (height, width, depth)\n \n        # as i am using \"channels first\", i have updated the input shape\n    \n        if K.image_data_format() == \"channels_first\":\n            inputShape = (depth, height, width)\n\n        # first set of CONV => RELU => POOL layers\n        #convolve 5*5 kernel over the image \n        model.add(Conv2D(20, (5, 5), padding=\"same\",\n            input_shape=inputShape))\n        model.add(Activation(\"relu\"))\n        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))\n\n        # second set of CONV => RELU => POOL layers\n        #activation used is the rectifier, RELU for immediate threshold change\n        model.add(Conv2D(50, (5, 5), padding=\"same\"))\n        model.add(Activation(\"relu\"))\n        #now, the result from 1 conv 1 RELU and 1 more conv 2d layer is maxpooled using a 2*2 pooling size kernel\n        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))\n\n        # first (and only) set of FC => RELU layers\n        model.add(Flatten())\n        model.add(Dense(500))\n        model.add(Activation(\"relu\"))\n \n        # softmax classifier\n        model.add(Dense(classes))\n        model.add(Activation(\"sigmoid\"))\n \n        # return the constructed network architecture\n        return model","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:22:04.491768Z","iopub.execute_input":"2021-06-04T16:22:04.492055Z","iopub.status.idle":"2021-06-04T16:22:04.502648Z","shell.execute_reply.started":"2021-06-04T16:22:04.492018Z","shell.execute_reply":"2021-06-04T16:22:04.501439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 100\nINIT_LR = 1e-3\nBS = 32","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:22:04.50392Z","iopub.execute_input":"2021-06-04T16:22:04.504297Z","iopub.status.idle":"2021-06-04T16:22:04.516148Z","shell.execute_reply.started":"2021-06-04T16:22:04.504254Z","shell.execute_reply":"2021-06-04T16:22:04.514992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"[INFO] compiling model...\")\nmodel = CNNmodel.build(width=128, height=128, depth=1, classes=2)\nopt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=opt,\n    metrics=[\"accuracy\"])\n\n# training the network\nprint(\"[INFO] training network...\")\n#fitting the model on data\nH = model.fit_generator(aug.flow(x_train, y_train, batch_size=BS),validation_data=(x_test, y_test),steps_per_epoch=len(x_train) // BS,epochs=EPOCHS, verbose=1)\n\n# saving the the model to disk\nprint(\"[INFO] serializing network...\")\nmodel.save('model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:22:04.517719Z","iopub.execute_input":"2021-06-04T16:22:04.518488Z","iopub.status.idle":"2021-06-04T16:36:57.222383Z","shell.execute_reply.started":"2021-06-04T16:22:04.518443Z","shell.execute_reply":"2021-06-04T16:36:57.221256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_graph(H,EPOCHS,INIT_LR,BS):\n\n    plt.style.use(\"ggplot\")\n    plt.figure()\n    N = EPOCHS\n    plt.plot(np.arange(0, N), H.history[\"loss\"], label=\"train_loss\")\n    plt.plot(np.arange(0, N), H.history[\"val_loss\"], label=\"val_loss\")\n    plt.plot(np.arange(0, N), H.history[\"accuracy\"], label=\"train_acc\")\n    plt.plot(np.arange(0, N), H.history[\"val_accuracy\"], label=\"val_acc\")\n    plt.title(\"Training Loss and Accuracy on my system\")\n    plt.xlabel(\"Epoch no.\")\n    plt.ylabel(\"Loss/Accuracy\")\n    plt.legend(loc=\"lower left\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:36:57.224339Z","iopub.execute_input":"2021-06-04T16:36:57.224761Z","iopub.status.idle":"2021-06-04T16:36:57.232297Z","shell.execute_reply.started":"2021-06-04T16:36:57.224717Z","shell.execute_reply":"2021-06-04T16:36:57.231611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_graph(H,EPOCHS,INIT_LR,BS)","metadata":{"execution":{"iopub.status.busy":"2021-06-04T16:36:57.234421Z","iopub.execute_input":"2021-06-04T16:36:57.234842Z","iopub.status.idle":"2021-06-04T16:36:57.494156Z","shell.execute_reply.started":"2021-06-04T16:36:57.234813Z","shell.execute_reply":"2021-06-04T16:36:57.492847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_ref = zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r')\nzip_ref.extractall('/test1')\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2021-06-04T17:30:52.585099Z","iopub.execute_input":"2021-06-04T17:30:52.585751Z","iopub.status.idle":"2021-06-04T17:30:57.245619Z","shell.execute_reply.started":"2021-06-04T17:30:52.585668Z","shell.execute_reply":"2021-06-04T17:30:57.244635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"/tmp/test1/test1\"","metadata":{"execution":{"iopub.status.busy":"2021-06-04T17:14:14.888647Z","iopub.execute_input":"2021-06-04T17:14:14.889091Z","iopub.status.idle":"2021-06-04T17:14:14.892937Z","shell.execute_reply.started":"2021-06-04T17:14:14.889052Z","shell.execute_reply":"2021-06-04T17:14:14.891996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = 1\nsubmission = pd.DataFrame(columns = ['id', 'label'])\nfor img in os.listdir(\"/test1/test1\"):\n    image = cv2.imread(\"/test1/test1/\" + img,0)\n    # orig = image.copy()\n\n    # pre-process the image for classification\n    image = cv2.resize(image, (128, 128))\n    image = image.astype(\"float32\") / 255.0\n    image = img_to_array(image)\n    image = np.expand_dims(image, axis=0)\n    predictions=model.predict(image)\n    val = np.argmax(np.squeeze(predictions))\n    submission = submission.append({'id': c,'label': val},ignore_index=True)\n\nsubmission.to_csv('submission.csv' ,index=False)\nprint(submission.head())","metadata":{"execution":{"iopub.status.busy":"2021-06-04T17:31:04.577172Z","iopub.execute_input":"2021-06-04T17:31:04.577559Z","iopub.status.idle":"2021-06-04T17:43:48.714264Z","shell.execute_reply.started":"2021-06-04T17:31:04.57751Z","shell.execute_reply":"2021-06-04T17:43:48.713218Z"},"trusted":true},"execution_count":null,"outputs":[]}]}