{"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-07-29T06:37:40.317399Z","iopub.execute_input":"2021-07-29T06:37:40.317758Z","iopub.status.idle":"2021-07-29T06:37:40.328101Z","shell.execute_reply.started":"2021-07-29T06:37:40.317724Z","shell.execute_reply":"2021-07-29T06:37:40.326917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and Check Data","metadata":{}},{"cell_type":"code","source":"import os\nimport warnings\n# filter warnings\nwarnings.filterwarnings('ignore')\nfrom zipfile import ZipFile\nprint(os.listdir(\"../input/dogs-vs-cats\"))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:40.329814Z","iopub.execute_input":"2021-07-29T06:37:40.330197Z","iopub.status.idle":"2021-07-29T06:37:40.339069Z","shell.execute_reply.started":"2021-07-29T06:37:40.330163Z","shell.execute_reply":"2021-07-29T06:37:40.338068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\").extractall()\n\nZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", \"r\").extractall()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:40.340667Z","iopub.execute_input":"2021-07-29T06:37:40.341187Z","iopub.status.idle":"2021-07-29T06:37:54.310776Z","shell.execute_reply.started":"2021-07-29T06:37:40.34114Z","shell.execute_reply":"2021-07-29T06:37:54.309628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"./train\"\ntest_path = \"./test1\"","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:54.312548Z","iopub.execute_input":"2021-07-29T06:37:54.312982Z","iopub.status.idle":"2021-07-29T06:37:54.317604Z","shell.execute_reply.started":"2021-07-29T06:37:54.312921Z","shell.execute_reply":"2021-07-29T06:37:54.316723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Total number of images in train and test datasets\nprint('Total images in Train and Test Data Set:')\nprint(len(os.listdir(train_path)))\nprint(len(os.listdir(test_path)))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:54.319204Z","iopub.execute_input":"2021-07-29T06:37:54.319703Z","iopub.status.idle":"2021-07-29T06:37:54.355569Z","shell.execute_reply.started":"2021-07-29T06:37:54.319668Z","shell.execute_reply":"2021-07-29T06:37:54.354534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(train_path)\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    'category': categories\n})\n#df train\n#if pic is dog pic category=1 else it is cat pic category=0","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:54.357091Z","iopub.execute_input":"2021-07-29T06:37:54.35737Z","iopub.status.idle":"2021-07-29T06:37:54.393298Z","shell.execute_reply.started":"2021-07-29T06:37:54.357342Z","shell.execute_reply":"2021-07-29T06:37:54.39229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:54.394559Z","iopub.execute_input":"2021-07-29T06:37:54.394853Z","iopub.status.idle":"2021-07-29T06:37:54.406326Z","shell.execute_reply.started":"2021-07-29T06:37:54.394824Z","shell.execute_reply":"2021-07-29T06:37:54.404736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:54.408178Z","iopub.execute_input":"2021-07-29T06:37:54.408747Z","iopub.status.idle":"2021-07-29T06:37:54.425167Z","shell.execute_reply.started":"2021-07-29T06:37:54.408583Z","shell.execute_reply":"2021-07-29T06:37:54.423854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install numpy","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:37:54.426723Z","iopub.execute_input":"2021-07-29T06:37:54.427142Z","iopub.status.idle":"2021-07-29T06:38:00.724014Z","shell.execute_reply.started":"2021-07-29T06:37:54.427097Z","shell.execute_reply":"2021-07-29T06:38:00.722706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install Pillow","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:00.727769Z","iopub.execute_input":"2021-07-29T06:38:00.728433Z","iopub.status.idle":"2021-07-29T06:38:07.007859Z","shell.execute_reply.started":"2021-07-29T06:38:00.728373Z","shell.execute_reply":"2021-07-29T06:38:07.006541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\n  \nprint('Installed Pillow Version:', PIL.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:07.01081Z","iopub.execute_input":"2021-07-29T06:38:07.011151Z","iopub.status.idle":"2021-07-29T06:38:07.017579Z","shell.execute_reply.started":"2021-07-29T06:38:07.011115Z","shell.execute_reply":"2021-07-29T06:38:07.016055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.iloc[:10000]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:07.019015Z","iopub.execute_input":"2021-07-29T06:38:07.01954Z","iopub.status.idle":"2021-07-29T06:38:07.032092Z","shell.execute_reply.started":"2021-07-29T06:38:07.019503Z","shell.execute_reply":"2021-07-29T06:38:07.031068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:07.033264Z","iopub.execute_input":"2021-07-29T06:38:07.033539Z","iopub.status.idle":"2021-07-29T06:38:07.053432Z","shell.execute_reply.started":"2021-07-29T06:38:07.033512Z","shell.execute_reply":"2021-07-29T06:38:07.052362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom numpy import asarray  \nimport cv2\nfrom PIL import Image, ImageOps\nimport matplotlib.image as mpimg\nfrom PIL import Image, ImageOps\n\ndef rgb2gray(rgb):\n    return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:07.054628Z","iopub.execute_input":"2021-07-29T06:38:07.055085Z","iopub.status.idle":"2021-07-29T06:38:07.066842Z","shell.execute_reply.started":"2021-07-29T06:38:07.055036Z","shell.execute_reply":"2021-07-29T06:38:07.065651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom numpy import asarray  \ndata=[]\n# sample.png is the name of the image\n# file and assuming that it is uploaded\n\n# in the current directory or we need\n# to give the path\nfor i in df.filename:\n    img=Image.open(\"./train/\"+i)\n    img=ImageOps.fit(img,(128,128),Image.ANTIALIAS,0,(0.5,0.5))\n    img = ImageOps.grayscale(img)\n    #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    numpydata = asarray(img)\n    numpydata=numpydata.flatten()\n    data.append(numpydata)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:07.068434Z","iopub.execute_input":"2021-07-29T06:38:07.068844Z","iopub.status.idle":"2021-07-29T06:38:56.711191Z","shell.execute_reply.started":"2021-07-29T06:38:07.068799Z","shell.execute_reply":"2021-07-29T06:38:56.710226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:56.712736Z","iopub.execute_input":"2021-07-29T06:38:56.713053Z","iopub.status.idle":"2021-07-29T06:38:56.7172Z","shell.execute_reply.started":"2021-07-29T06:38:56.713022Z","shell.execute_reply":"2021-07-29T06:38:56.716062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:56.718474Z","iopub.execute_input":"2021-07-29T06:38:56.718926Z","iopub.status.idle":"2021-07-29T06:38:56.742262Z","shell.execute_reply.started":"2021-07-29T06:38:56.718894Z","shell.execute_reply":"2021-07-29T06:38:56.740958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=pd.DataFrame(data)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:38:56.744072Z","iopub.execute_input":"2021-07-29T06:38:56.744497Z","iopub.status.idle":"2021-07-29T06:41:26.094684Z","shell.execute_reply.started":"2021-07-29T06:38:56.744441Z","shell.execute_reply":"2021-07-29T06:41:26.093895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.097183Z","iopub.execute_input":"2021-07-29T06:41:26.097841Z","iopub.status.idle":"2021-07-29T06:41:26.117993Z","shell.execute_reply.started":"2021-07-29T06:41:26.097793Z","shell.execute_reply":"2021-07-29T06:41:26.116915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"x_train=x_train/255","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.119147Z","iopub.execute_input":"2021-07-29T06:41:26.11942Z","iopub.status.idle":"2021-07-29T06:41:26.445909Z","shell.execute_reply.started":"2021-07-29T06:41:26.119391Z","shell.execute_reply":"2021-07-29T06:41:26.444126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.447425Z","iopub.execute_input":"2021-07-29T06:41:26.447884Z","iopub.status.idle":"2021-07-29T06:41:26.483636Z","shell.execute_reply.started":"2021-07-29T06:41:26.447835Z","shell.execute_reply":"2021-07-29T06:41:26.482627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.485077Z","iopub.execute_input":"2021-07-29T06:41:26.485393Z","iopub.status.idle":"2021-07-29T06:41:26.492983Z","shell.execute_reply.started":"2021-07-29T06:41:26.485361Z","shell.execute_reply":"2021-07-29T06:41:26.491893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=df[\"category\"]\ny_train","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.494266Z","iopub.execute_input":"2021-07-29T06:41:26.494552Z","iopub.status.idle":"2021-07-29T06:41:26.510141Z","shell.execute_reply.started":"2021-07-29T06:41:26.494523Z","shell.execute_reply":"2021-07-29T06:41:26.508756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\ny_train = to_categorical(y_train, num_classes = 2)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.511165Z","iopub.execute_input":"2021-07-29T06:41:26.511444Z","iopub.status.idle":"2021-07-29T06:41:26.523821Z","shell.execute_reply.started":"2021-07-29T06:41:26.511405Z","shell.execute_reply":"2021-07-29T06:41:26.523034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.526563Z","iopub.execute_input":"2021-07-29T06:41:26.527022Z","iopub.status.idle":"2021-07-29T06:41:26.539821Z","shell.execute_reply.started":"2021-07-29T06:41:26.526979Z","shell.execute_reply":"2021-07-29T06:41:26.539093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.541186Z","iopub.execute_input":"2021-07-29T06:41:26.541599Z","iopub.status.idle":"2021-07-29T06:41:26.554564Z","shell.execute_reply.started":"2021-07-29T06:41:26.541558Z","shell.execute_reply":"2021-07-29T06:41:26.553428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train.values.reshape(-1,128,128,1)\n\n\nprint(\"x_train shape: \",x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.556349Z","iopub.execute_input":"2021-07-29T06:41:26.556771Z","iopub.status.idle":"2021-07-29T06:41:26.568172Z","shell.execute_reply.started":"2021-07-29T06:41:26.556713Z","shell.execute_reply":"2021-07-29T06:41:26.567048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nfrom sklearn.model_selection import train_test_split\nx_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=2)\nprint(\"x_train shape\",x_train.shape)\nprint(\"x_val shape\",x_val.shape)\nprint(\"y_train shape\",y_train.shape)\nprint(\"y_val shape\",y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:26.569706Z","iopub.execute_input":"2021-07-29T06:41:26.570134Z","iopub.status.idle":"2021-07-29T06:41:29.922032Z","shell.execute_reply.started":"2021-07-29T06:41:26.570086Z","shell.execute_reply":"2021-07-29T06:41:29.920795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x_train=x_train.values.reshape(-1,128,128,1)\n\n\n#print(\"x_train shape: \",x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:41:29.923218Z","iopub.execute_input":"2021-07-29T06:41:29.923502Z","iopub.status.idle":"2021-07-29T06:41:29.927572Z","shell.execute_reply.started":"2021-07-29T06:41:29.923474Z","shell.execute_reply":"2021-07-29T06:41:29.926442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\nmodel = Sequential()\n#\nmodel.add(Conv2D(filters = 16, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (128,128,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n#\nmodel.add(Conv2D(filters = 8, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n# fully connected\nmodel.add(Flatten())\nmodel.add(Dense(100, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation = \"sigmoid\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:48:57.311996Z","iopub.execute_input":"2021-07-29T06:48:57.312411Z","iopub.status.idle":"2021-07-29T06:48:57.399164Z","shell.execute_reply.started":"2021-07-29T06:48:57.312374Z","shell.execute_reply":"2021-07-29T06:48:57.398103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:49:00.182995Z","iopub.execute_input":"2021-07-29T06:49:00.183396Z","iopub.status.idle":"2021-07-29T06:49:00.188115Z","shell.execute_reply.started":"2021-07-29T06:49:00.183358Z","shell.execute_reply":"2021-07-29T06:49:00.187124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\n\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:49:01.625587Z","iopub.execute_input":"2021-07-29T06:49:01.625916Z","iopub.status.idle":"2021-07-29T06:49:01.63703Z","shell.execute_reply.started":"2021-07-29T06:49:01.625887Z","shell.execute_reply":"2021-07-29T06:49:01.636119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10  # for better result increase the epochs\nbatch_size = 100","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:49:06.553812Z","iopub.execute_input":"2021-07-29T06:49:06.554182Z","iopub.status.idle":"2021-07-29T06:49:06.558862Z","shell.execute_reply.started":"2021-07-29T06:49:06.55414Z","shell.execute_reply":"2021-07-29T06:49:06.557751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data augmentation\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=5,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.1, # Randomly zoom image 10%\n        width_shift_range=0.1,  # randomly shift images horizontally 10%\n        height_shift_range=0.1,  # randomly shift images vertically 10%\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:49:08.106511Z","iopub.execute_input":"2021-07-29T06:49:08.106866Z","iopub.status.idle":"2021-07-29T06:49:08.898169Z","shell.execute_reply.started":"2021-07-29T06:49:08.106837Z","shell.execute_reply":"2021-07-29T06:49:08.896962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (x_val,y_val), steps_per_epoch=x_train.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T06:49:10.260858Z","iopub.execute_input":"2021-07-29T06:49:10.261255Z","iopub.status.idle":"2021-07-29T06:59:56.560189Z","shell.execute_reply.started":"2021-07-29T06:49:10.261217Z","shell.execute_reply":"2021-07-29T06:59:56.559082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the loss and accuracy curves for training and validation \nimport matplotlib.pyplot as plt\nplt.plot(history.history['val_loss'], color='b', label=\"validation loss\")\nplt.title(\"Test Loss\")\nplt.xlabel(\"Number of Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:01:03.32629Z","iopub.execute_input":"2021-07-29T07:01:03.326774Z","iopub.status.idle":"2021-07-29T07:01:03.510337Z","shell.execute_reply.started":"2021-07-29T07:01:03.32674Z","shell.execute_reply":"2021-07-29T07:01:03.509267Z"},"trusted":true},"execution_count":null,"outputs":[]}]}