{"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 \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=80\nBATCH_SIZE=20","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"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_COLOR)\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],3)\nlabel=np.array(label)\nprint (data.shape)\nprint (label.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae5ff8693305fe80631ffcea4515870b3846d180"},"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":"88e17e6c01e00c8a7bb3cbdcba8007430f6eab7c"},"cell_type":"code","source":"from keras import applications\nvgg_model = applications.VGG16(weights='imagenet',\n                               include_top=False,\n                               input_shape=(80, 80, 3))\n\nvgg_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05c5ba06f303410e5b551714335a0557d494d82f"},"cell_type":"code","source":"from keras import layers\nfrom keras import models\nmodel = models.Sequential()\nmodel.add(vgg_model)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation  = 'relu'))\nmodel.add(layers.Dense(1, activation = 'sigmoid'))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"31cdcb10a6c9f84c011a82f3e4dc4b457920dcac"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee4e08f0c4a850db586dd69bff1cdbd724993733"},"cell_type":"code","source":"from keras import backend as K\nK.set_image_dim_ordering('th')\nK.set_image_data_format('channels_last')\nfrom keras import layers\nfrom keras import models\nfrom keras import optimizers\nfrom keras.layers import GlobalAveragePooling2D\n\nmodel.compile(loss='binary_crossentropy',optimizer=optimizers.adam(lr=1e-4),metrics=['acc'])\n# vgg_model.summary()\n# train_history=vgg_model.fit(train_data,train_label,validation_data=(valid_data,valid_label),epochs=40,batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01917df352392c805999c9a288246ef98136fdc0","scrolled":false},"cell_type":"code","source":"train_history=model.fit(train_data,train_label,validation_data=(valid_data,valid_label),epochs=20,batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e04d972b494ae8e481a656c403dd24ab44b9788"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc = train_history.history['acc']\nval_acc = train_history.history['val_acc']\nloss = train_history.history['loss']\nval_loss = train_history.history['val_loss']\nepochs = range(1, len(acc) + 1)\nplt.plot(epochs, acc, 'blue', label='Training acc')\nplt.plot(epochs, val_acc, 'red', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs, loss, 'blue', label='Training loss')\nplt.plot(epochs, val_loss, 'red', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ff8b625bd9a665ad2b783f17ac942950e09a8c83"},"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\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_datagen.flow_from_directory(\"../input/test1\",target_size=(80, 80),batch_size=32,class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c213ba48d2991c06670a3f59ebccbaaf650a86d"},"cell_type":"code","source":"from tensorflow.python.keras.models import Sequential\nfrom keras.models import load_model\n\nprint(\"-- Evaluate --\")\n\nscores = model.evaluate_generator(\n            test_generator, \n            steps = 100)\n\nprint(\"%s: %.2f%%\" %(model.metrics_names[1], scores[1]*100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bd259e6d073c767ddec3849374f8ded595328e6"},"cell_type":"code","source":"Y_pred = model.predict(valid_data)\npredicted_label=np.round(Y_pred,decimals=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b141a3cf77700698f1779e666ee61582986c5ef"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nfrom mlxtend.plotting import plot_confusion_matrix\n\n# Get the confusion matrix\n\nCM = confusion_matrix(valid_label, Y_pred.round())\nfig, ax = plot_confusion_matrix(conf_mat=CM ,  figsize=(12, 12))\nplt.xticks(range(2), ['Cat', 'Dog'], fontsize=16)\nplt.yticks(range(2), ['Cat', 'Dog'], fontsize=16)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"506ba5ce4dd006ca086efaf738495cb694d25f18"},"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_COLOR)\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\n\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],3)\ndataframe_output=pd.DataFrame({\"id\":id})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1f93c264677d8a0bf7b87eb48788b372dcee06c"},"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]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fd5ef5274cfdfc3db0a961b265d5916b2ab7ab9"},"cell_type":"code","source":"dataframe_output[\"label\"]=labels\nprint(dataframe_output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cba83d33bc64f2938e2edca7130282c02603132b"},"cell_type":"code","source":"#0이 고양이 1이 강아지\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfor i in range(40):\n    plt.figure(figsize=(20, 6))\n    plt.subplot(1, 2, 2)\n    plt.imshow(test_data[i])\n#     plt.xlabel('label')\n    plt.xlabel(dataframe_output[\"label\"][i],fontsize=30)\n    plt.title('0-cat,1-dog',fontsize=40)\n    plt.show()\n\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4772c8d06ffd2e2043f4cc77ea5c121cb4ad2b8c"},"cell_type":"code","source":"correct_indices = []\nincorrect_indices = []\n\nanswer=[1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,0,0,1,1,0,1,1,1,1,0,1,0,0,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a30177b609865b5db1f3e4c35a7977632b22fa07"},"cell_type":"code","source":"len(answer)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7491a4c132cfc0fba62bf0b3e6dfa219422f49f0"},"cell_type":"code","source":"correct_indices=[]\nincorrect_indices=[]\n\nfor i in range(len(answer)):\n  if dataframe_output[\"label\"][i] == answer[i]:\n    correct_indices.append(test_data[i])\n  else:\n    incorrect_indices.append(test_data[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86409ac26df134ad53fc6afbb5998aae12b971bb"},"cell_type":"code","source":"print(len(correct_indices),len(incorrect_indices))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1dc32394b4d24b4df2143efede3465ac59b0d0d4"},"cell_type":"code","source":"for i in range(len(correct_indices)):\n    plt.figure(figsize=(20, 6))\n    plt.subplot(1,2,1)\n    plt.title('correct',fontsize=40)\n    plt.imshow(correct_indices[i])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a02a59675ff1ce088a87cc61645ceb36854dfd43"},"cell_type":"code","source":"for i in range(len(incorrect_indices)):\n    plt.figure(figsize=(20, 6))\n    plt.subplot(1,2,1)\n    plt.title('correct',fontsize=40)\n    plt.imshow(incorrect_indices[i])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb4c6fc462c76907eb42c725d438a094e1e62caa"},"cell_type":"code","source":"","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}