{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport os\nimport cv2\nimport zipfile\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras import backend as K\nimport random\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nfrom keras.models import Sequential\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D\nfrom keras.layers.core import Dense, Flatten, Dropout, Activation\nfrom keras.layers import BatchNormalization\nfrom keras.optimizers import Adam\nfrom keras.utils import np_utils\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.models import save_model, load_model\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom mlxtend.plotting import plot_confusion_matrix\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip -d train\n!unzip ../input/dogs-vs-cats-redux-kernels-edition/test.zip -d test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_data = '../working/train/train'\ntesting_data = '../working/test/test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input/'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = []\nRESIZE = 100\nX = []\ny = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_training_data():\n    for img in tqdm(os.listdir(training_data)):\n        try:\n            img_array = cv2.imread(os.path.join(training_data,img), cv2.IMREAD_GRAYSCALE)            \n            img2 = cv2.resize(img_array, (RESIZE,RESIZE))\n            img2 = (img2 - img2.mean())/img2.std()\n            if img[:3] == 'dog':\n                class_num = 0\n            else:\n                class_num = 1\n            X.append(img2)\n            y.append(class_num)\n        except Exception as e:\n            pass\n        \ncreate_training_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = np.array(X).reshape(-1, RESIZE, RESIZE, 1)\ny = np.asarray(y)\n(X_train, X_val, y_train, y_val) = train_test_split(X, y, test_size=0.3, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug_train = ImageDataGenerator(\n    rotation_range=20,\n    zoom_range=0.15,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.15,\n    horizontal_flip=True,\n    fill_mode=\"nearest\")\n\ngenerator_val = ImageDataGenerator()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug_train.fit(X_train)\n\ngenerator_val.fit(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"K.set_image_data_format('channels_last')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, (3, 3), input_shape=(RESIZE, RESIZE, 1)))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(2, activation='softmax'))\n\nmodel.compile(loss='sparse_categorical_crossentropy', optimizer= Adam(), metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# earlystop = EarlyStopping(patience=5)\n\nhistory = model.fit_generator(aug_train.flow(X_train, y_train, batch_size=32), \n                    validation_data=generator_val.flow(X_val, y_val, batch_size=32), \n                    epochs=100)\n# callbacks=[earlystop]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nLABELS = [\"DOG\", \"CAT\"]\ntest_data = []\nRESIZE = 100\nX_test = []\nX_id = []\n\ndef create_test_data():\n    for img in tqdm(os.listdir(testing_data)):\n        try:\n            img_array = cv2.imread(os.path.join(testing_data,img), cv2.IMREAD_GRAYSCALE)            \n            img2 = cv2.resize(img_array, (RESIZE,RESIZE))\n            X_test.append(img2)\n            img_num = img.split('.')[0]\n            X_id.append(np.array(img_num))\n            \n        except Exception as e:\n            pass\n        \ncreate_test_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = np.array(X_test).reshape(-1, RESIZE, RESIZE, 1)\n\narr_test = model.predict(X_test.astype(float))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'id':X_id,'label':arr_test[:,0]})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filename = 'Prediction1.csv'\n\nsubmission.to_csv(filename,index=False)\n\nprint('Saved file: ' + filename)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predicted_label = np.argmax(arr_test, axis=1)\nfig=plt.figure(figsize=(20,20))\n\nfor counter, img in enumerate(X_test[:40]):\n    ax = fig.add_subplot(10,4,counter+1)\n    ax.imshow(X_test[counter,:,:,0], cmap='gray')\n    plt.title(LABELS[test_predicted_label[counter]])\n    ax.axes.get_xaxis().set_visible(False)\n    ax.axes.get_yaxis().set_visible(False)\n    \nplt.tight_layout()\nplt.show()","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":1}