{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import 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\nin_path=\"../input\"\n\nprint(os.listdir(in_path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd1969e752f9d766bc47efe6ce3c7e17b1adad31","scrolled":false},"cell_type":"code","source":"\nIMAGE_SIZE=64\nBATCH_SIZE=128\n\n\nlabel=[]\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)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"423bf69b7ae05fd3310c552226b03fc4762d0bab"},"cell_type":"code","source":"sns.countplot(label)\n# 1이 dog 0이 cat","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aee866e1859ceffdd47d9ad42c5860e65e52fbd0"},"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":"efa64e74b63442f2da4891337e23cb2cd72dcc8a"},"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\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"63e9978183ee9f41940c8a608b283a8813f2f29d"},"cell_type":"code","source":"from tensorflow.python.keras.applications import ResNet50\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D\n\n\n\n\nmy_new_model = Sequential()\nmy_new_model.add(ResNet50(include_top=False, pooling='avg', weights='imagenet'))\nmy_new_model.add(Dense(1, activation='sigmoid'))\n\n# Say not to train first layer (ResNet) model. It is already trained\nmy_new_model.layers[0].trainable = False\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"3fa86aa95c3a815de34a488b7ad0679ddb2068db"},"cell_type":"code","source":"ResNet50().summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53f281be972084691a2b6c73dfd2617133525900"},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255, \n                                   width_shift_range=0.1,\n                                   height_shift_range=0.1,\n                                   rotation_range=10,\n                                   zoom_range=0.1,)\n\nval_datagen = ImageDataGenerator(rescale=1./255, \n                                 width_shift_range=0.1,\n                                 height_shift_range=0.1,\n                                 rotation_range=10,\n                                 zoom_range=0.1,)\n\n\n\ntrain_generator = train_datagen.flow(np.array(train_data), np.array(train_label), batch_size = 128)\nvalidation_generator = val_datagen.flow(np.array(valid_data), np.array(valid_label), batch_size = 128)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ede4c86dc0ef1f05bbda2a87f70a4d4b428220a6"},"cell_type":"code","source":"my_new_model.compile(optimizer='adam',loss=\"binary_crossentropy\",metrics=[\"accuracy\"])\n\ncallack_saver = ModelCheckpoint(\n            \"my_new_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=my_new_model.fit(train_data,train_label,validation_data=(valid_data,valid_label),epochs=40,batch_size=128, callbacks=[callack_saver])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da2d200de7b285fad50d6f4336394544e9c1004c","scrolled":true},"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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4ca4727783dfcdea58ea3784eceb7fe915c1f17"},"cell_type":"code","source":"\nIMAGE_SIZE=64\nBATCH_SIZE=128\n\n\nlabel=[]\ndata=[]\ncounter=0\npath=\"../input/test1/test1\"\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)\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72ff8bee5840dc6e32bf8fc4085a9ab4c6f98596"},"cell_type":"code","source":"Y_pred = my_new_model.predict(data)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c3cbbeee850d9401282175f7f32645980cf7002"},"cell_type":"code","source":"print(Y_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ef2163e7bd405449ab86db2a056174fb0db92f5"},"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = test_datagen.flow_from_directory(\"../input/test1\",target_size=(128, 128),batch_size=32,class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66c4750a136a0f4582c65133f61c41a9ed3f05f4"},"cell_type":"code","source":"\nfrom tensorflow.python.keras.models import Sequential\nfrom keras.models import load_model\n\nprint(\"-- Evaluate --\")\n\nscores = my_new_model.evaluate_generator(\n            test_generator, \n            steps = 100)\n\nprint(\"%s: %.2f%%\" %(my_new_model.metrics_names[1], scores[1]*100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1767fd52bab953ae5e8eb9b6f633d355d286acc9"},"cell_type":"code","source":"steps = 12500 / 20\nimport numpy as np\n# 모델 예측하기\nprint(\"-- Predict --\")\n\noutput = my_new_model.predict_generator(test_generator,steps)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8b6d1c263ba3e6091e15d9453adcc49fef1a982"},"cell_type":"code","source":"print(output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83fb5819687d93668585e12df8916208f686a55e"},"cell_type":"code","source":"print(len(output))\n\npred = np.array(output) \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"253fa369744513924fe42f0f4ced5ac0189c9fe3"},"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(test_generator.classes, 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":"fa1794ecd4639325d154810fcbc6611383de1765"},"cell_type":"code","source":"Y_pred = my_new_model.predict(valid_data)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7f4c58f88923dbf3c552e3d3234ac08a686d0b3"},"cell_type":"code","source":"# predicted_label=np.round(Y_pred,decimals=2)\n# predicted_label=[1 if value>0.5 else 0 for value in predicted_label]\n# confusion_mtx = confusion_matrix(valid_label, predicted_label) \n# # plot the confusion matrix\n# plot_confusion_matrix(confusion_mtx, classes = range(2)) \n\n\nimport 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":"a96b02a76ca7665b319dd52bc4373a9ac621a828"},"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    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],3)\ndataframe_output=pd.DataFrame({\"id\":id})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d579bf925518eaf70649fb4b1c8e7b519bd4fd8c"},"cell_type":"code","source":"predicted_labels=my_new_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":"381f8f94acc0c0d3d818d296ab341357646328fb"},"cell_type":"code","source":"dataframe_output[\"label\"]=labels\nprint(dataframe_output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b61d67cb7b4fbe82b7c3c7d0621ce44e8a2dca4"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nfor i in range(20):\n    plt.figure()\n    plt.imshow(test_data[i])\n#     plt.xlabel('label')\n    plt.xlabel(dataframe_output[\"label\"][i])\n\n    plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e92791ec794b9f66a2d4420c43b469ce8ed98f59"},"cell_type":"code","source":"0 이 cat 1 강아지","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}