{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nimport random\nimport os,shutil\n\nsrc_path=\"../input\"\n\nprint(os.listdir(src_path))\n\n#constant value\nVALID_SPIT=0.2\nIMAGE_SIZE=64\nBATCH_SIZE=128\nCHANNEL_SIZE=1\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc928b5c9d681fa550231204eb05ad6402ec106a"},"cell_type":"code","source":"label=[]\ndata=[]\ncounter=0\npath=\"../input/train/train\"\nfor file in os.listdir(path):\n    image_data=cv2.imread(os.path.join(path,file), cv2.IMREAD_GRAYSCALE)\n    image_data=cv2.resize(image_data,(IMAGE_SIZE,IMAGE_SIZE))\n    if file.startswith(\"cat\"):\n        label.append(0)\n    elif file.startswith(\"dog\"):\n        label.append(1)\n    try:\n        data.append(image_data/255)\n    except:\n        label=label[:len(label)-1]\n    counter+=1\n    if counter%1000==0:\n        print (counter,\" image data retreived\")\n\ndata=np.array(data)\ndata=data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1)\nlabel=np.array(label)\nprint (data.shape)\nprint (label.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5aaa1607e0757c68ebc6489e216c24359fef4865"},"cell_type":"code","source":"sns.countplot(label)\npd.Series(label).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50433529f32b5c409deaf96721819fe45df10e1e"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_data, valid_data, train_label, valid_label = train_test_split(\n    data, label, test_size=0.2, random_state=42)\nprint(train_data.shape)\nprint(train_label.shape)\nprint(valid_data.shape)\nprint(valid_label.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caf768afb49730f1d0d5de90d7352ad32fb5935a"},"cell_type":"code","source":"sns.countplot(train_label)\npd.Series(train_label).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c68cb2b34a2e7dfcd93fbe0603dc89966fc0af1a"},"cell_type":"code","source":"sns.countplot(valid_label)\npd.Series(valid_label).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d12f7adacb8c02ad6be51f3f45dcbfbe19264205"},"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import *\nimport keras.optimizers as optimizers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import *\nimport keras.backend as K","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06be5cfc85cf580b6f21ca2c7e7ad5464b2b7951"},"cell_type":"code","source":"#custom net\nmodel=Sequential()\n\nmodel.add(Flatten(input_shape=(IMAGE_SIZE, IMAGE_SIZE, CHANNEL_SIZE)))\n\nmodel.add(Dense(1024,activation=\"relu\"))\n\nmodel.add(Dense(128,activation=\"relu\"))\n\nmodel.add(Dense(1,activation=\"sigmoid\"))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a3be3c49cf57fa01140530665da92292fadc49e"},"cell_type":"code","source":"#custom cnn net \nmodel=Sequential()\nmodel.add(Conv2D(8, (3, 3), input_shape=(IMAGE_SIZE, IMAGE_SIZE, CHANNEL_SIZE), activation='relu', padding='same'))\nmodel.add(MaxPooling2D())\n\nmodel.add(Conv2D(16, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D())\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D())\n\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D())\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(100,activation=\"relu\"))\nmodel.add(Dense(1,activation=\"sigmoid\"))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad30690020baf1cf5d9847a80cbf3f7c14d148fe"},"cell_type":"code","source":"# training\nmodel.compile(optimizer='adam',loss=\"binary_crossentropy\",metrics=[\"accuracy\"])\n\ncallack_saver = ModelCheckpoint(\n            \"model.h5\"\n            , monitor='val_loss'\n            , verbose=0\n            , save_weights_only=True\n            , mode='auto'\n            , save_best_only=True\n        )\n\ntrain_history=model.fit(train_data,train_label,validation_data=(valid_data,valid_label),epochs=15,batch_size=BATCH_SIZE, callbacks=[callack_saver])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4c49438d53a4ac0650054bd77e6b9cf87b79d47"},"cell_type":"code","source":"def show_train_history(train_history, train, validation):\n    plt.plot(train_history.history[train])\n    plt.plot(train_history.history[validation])\n    plt.title('Train History')\n    plt.ylabel(train)\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"275ee24fffaa7152e4ddb9f66b8bcbedd40a8617","_kg_hide-output":true},"cell_type":"code","source":"show_train_history(train_history, 'loss', 'val_loss')\nshow_train_history(train_history, 'acc', 'val_acc')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f8a52c06c9081023bdd8c3ee5ae23d664d7f618"},"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n    \n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7145704147ed2d53c7305deab7a416d1aee3a7ce"},"cell_type":"code","source":"# Predict the values from the validation dataset\nY_pred = model.predict(valid_data)\npredicted_label=np.round(Y_pred,decimals=2)\npredicted_label=[1 if value>0.5 else 0 for value in predicted_label]\nconfusion_mtx = confusion_matrix(valid_label, predicted_label) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(2)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3454dc2c8b11a242df6803788f8eb103c85dd84a","scrolled":true},"cell_type":"code","source":"image_list=[]\ntest_data=[]\ncount = 0\nfor file in os.listdir(\"../input/test1/test1\"):\n    image_data=cv2.imread(os.path.join(\"../input/test1/test1\",file))\n    image_list.append(image_data)\n    \n    image_data=cv2.imread(os.path.join(\"../input/test1/test1\",file), cv2.IMREAD_GRAYSCALE)\n    image_data=cv2.resize(image_data,(IMAGE_SIZE,IMAGE_SIZE))\n    test_data.append(image_data/255)\n    count +=1\n    if count == 1:\n        break\n        \nfig, ax = plt.subplots(1,2,figsize=(10,5))\nax[0].imshow(image_list[0])\nax[1].imshow(test_data[0])   \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb0a278a74e37af1ea4b43eb28d91380ef0f3fb6"},"cell_type":"code","source":"test_data=np.array(test_data)\ntest_data=test_data.reshape((test_data.shape)[0],(test_data.shape)[1],(test_data.shape)[2],1)\nprint(test_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75a431543fbcf6d70ee852d65cb5f5ea1e250457"},"cell_type":"code","source":"predicted_labels=model.predict(test_data)\npredicted_labels=np.round(predicted_labels,decimals=2)\nlabels=[1 if value>0.5 else 0 for value in predicted_labels]\nprint(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6367f55ffdbcca3b44c7fac8fe3612d82ecfdbe8"},"cell_type":"code","source":"layer_1 = K.function([model.layers[0].input], [model.layers[1].output])\nf1 = layer_1([test_data])[0]\nprint(f1.shape)\n#第一层卷积后的特征图展示，输出是（1,32,32,8）\nfor _ in range(8):\n        show_img = f1[:, :, :, _]\n        show_img.shape = [32, 32]\n        plt.subplot(1, 8, _ + 1)\n        plt.imshow(show_img, cmap='gray')\n        plt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47f134d5b2aeea54385c579cf79516cae66b60d0"},"cell_type":"code","source":"layer_3 = K.function([model.layers[0].input], [model.layers[3].output])\nf1 = layer_3([test_data])[0]#只修改inpu_image\nprint(f1.shape)\nfor _ in range(16):\n        show_img = f1[:, :, :, _]\n        show_img.shape = [16, 16]\n        plt.subplot(2, 8, _ + 1)\n        plt.imshow(show_img, cmap='gray')\n        plt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"846a0fb19154ac96ca92b7fd198784c936608dee"},"cell_type":"code","source":"layer_5 = K.function([model.layers[0].input], [model.layers[5].output])\nf1 = layer_5([test_data])[0]#只修改inpu_image\nprint(f1.shape)\nfor _ in range(32):\n        show_img = f1[:, :, :, _]\n        show_img.shape = [8, 8]\n        plt.subplot(4, 8, _ + 1)\n        plt.imshow(show_img, cmap='gray')\n        plt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1e43a75827b851f338e7967e576cc18d8898e87"},"cell_type":"code","source":"layer_7 = K.function([model.layers[0].input], [model.layers[7].output])\nf1 = layer_7([test_data])[0]#只修改inpu_image\nprint(f1.shape)\nfor _ in range(64):\n        show_img = f1[:, :, :, _]\n        show_img.shape = [4, 4]\n        plt.subplot(8, 8, _ + 1)\n        plt.imshow(show_img, cmap='gray')\n        plt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96215efad4ac864d7c61e065596dcf40a54cdf83"},"cell_type":"code","source":"test_data=[]\nid=[]\ncounter=0\nfor file in os.listdir(\"../input/test1/test1\"):\n    image_data=cv2.imread(os.path.join(\"../input/test1/test1\",file), cv2.IMREAD_GRAYSCALE)\n    try:\n        image_data=cv2.resize(image_data,(IMAGE_SIZE,IMAGE_SIZE))\n        test_data.append(image_data/255)\n        id.append((file.split(\".\"))[0])\n    except:\n        print (\"ek gaya\")\n    counter+=1\n    if counter%1000==0:\n        print (counter,\" image data retreived\")\n\ntest_data=np.array(test_data)\nprint (test_data.shape)\ntest_data=test_data.reshape((test_data.shape)[0],(test_data.shape)[1],(test_data.shape)[2],1)\ndataframe_output=pd.DataFrame({\"id\":id})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d1e188744e90d0cdad53469322375153e75e040"},"cell_type":"code","source":"predicted_labels=model.predict(test_data)\npredicted_labels=np.round(predicted_labels,decimals=2)\nlabels=[1 if value>0.5 else 0 for value in predicted_labels]\n\n#print(len(labels))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d072c5357840cca7b4846dd2786da2f751766b85"},"cell_type":"code","source":"dataframe_output[\"label\"]=labels\nprint(dataframe_output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5be17382af31459070c165811e1e54f87675d18f"},"cell_type":"code","source":"dataframe_output.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}