{"cells":[{"metadata":{"_uuid":"c0a7460d-0668-4a00-92ce-40b729929533","_cell_guid":"e8e09cf1-90fc-435b-af0f-453c68e83f7f","trusted":true,"id":"JadR869D2K7L","colab_type":"code","colab":{}},"cell_type":"code","source":"! pip install imutils\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split\nfrom imutils import paths\nimport numpy as np\nimport imutils \nimport cv2 \nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"465b4546-b7a1-4e0d-9eee-d53ac3c46a61","_cell_guid":"98bec787-df3e-4ec7-a195-3d54c773e066","trusted":true,"id":"y-JkqXus-Awq","colab_type":"code","outputId":"36ac388f-7358-4935-943b-1adf26d6b110","colab":{"base_uri":"https://localhost:8080/","height":53}},"cell_type":"code","source":"\ndataset_train = \"../input/2019-fall-pr-project/train/train\"\n\nX = []\nY = []\n\nfor i, img in enumerate(paths.list_images(dataset_train)):\n  \n  # dog: 1\n  Y.append(1 if 'dog' in img else 0)  \n  img = cv2.imread(img)\n#  print(img.shape)\n  img = cv2.resize(img, (32, 32), interpolation=cv2.INTER_AREA)\n#  print(img.shape)\n  img = img.flatten()\n#  print(img.shape)\n  X.append(img)\n  \n  print(\"loading... \", str(i/200)+\"%\") if i%2000==0 else None\n  \n  if i==1000:\n    break\n\nX = np.array(X)\nY = np.array(Y)\nprint(X.shape, Y.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3d61ff44-2d34-4fd6-8879-dfe77b636d25","_cell_guid":"86bd49ae-10da-40e8-93e7-fe1fd458162f","trusted":true,"id":"7vF_sa0r-Fca","colab_type":"code","colab":{}},"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(\n    X, Y, test_size=0.25, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c0b44782-593a-4f6e-9ce8-11aa7a26659e","_cell_guid":"21c84b15-45dd-4126-828f-348b94c09b73","trusted":true,"id":"K9QhM25L-GYa","colab_type":"code","colab":{}},"cell_type":"code","source":"%matplotlib inline\n#import numpy as np\nimport matplotlib.pyplot as plt\n#from scipy import stats\n\n# use seaborn plotting defaults\n#import seaborn as sns; sns.set()\n\n#import time\n\nfrom sklearn.svm import SVC\n#from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bc0e4e34-ff17-4e3f-8b08-216cd8a6fc35","_cell_guid":"693ac1a8-1592-4061-9499-cfbca475a1e0","trusted":true,"id":"-D36wQF5frKx","colab_type":"code","outputId":"0d197f98-686b-439c-8ec7-6adc2afd5968","colab":{"base_uri":"https://localhost:8080/","height":145}},"cell_type":"code","source":"svc = SVC(kernel='poly')\nsvc.fit(X_train, Y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d867cabe-be10-4f3d-b6d2-9555e56abf0e","_cell_guid":"39c8c60e-5547-4085-a1d4-d1a35cd01ea7","trusted":true,"id":"kgCQeDXZ42ZR","colab_type":"code","outputId":"6cb477dc-edbb-4699-ed7c-e275173571d4","colab":{"base_uri":"https://localhost:8080/","height":179}},"cell_type":"code","source":"predict = svc.predict(X_val)\nprint(classification_report(Y_val, predict))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0f168d61-4ce1-44c7-a485-e9a2a089bc02","_cell_guid":"26407da4-9c58-4a31-9ac7-030e921f0e36","trusted":true,"id":"NknMfw3G-HkC","colab_type":"code","outputId":"e14be41c-0222-480d-df1f-0a2f6bd4da2f","colab":{"base_uri":"https://localhost:8080/","height":233}},"cell_type":"code","source":"\ndataset_test = \"../input/2019-fall-pr-project/test1/test1\"\n\nX_test = []\n#Y_test = []\n\nids = []\n\nfor i, img in enumerate(paths.list_images(dataset_test)):\n  \n  ids.append(os.path.basename(img)[:-4])\n  \n  # dog: 1\n  #Y_test.append(1 if 'dog' in img else 0)  \n  img = cv2.imread(img)\n#  print(img.shape)\n  img = cv2.resize(img, (32, 32), interpolation=cv2.INTER_AREA)\n#  print(img.shape)\n  img = img.flatten()\n#  print(img.shape)\n  X_test.append(img)\n  \n  print(\"loading... \", str(i/50+10)+\"%\") if i%500==0 else None\n  \nids = np.array(ids)\nX_test = np.array(X_test)\n#Y_test = np.array(Y_test)\nprint(ids.shape)\nprint(X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"adf7d01c-3ca5-4244-8367-b6c2c8f5cbd1","_cell_guid":"fc0e4cdf-10e0-4c51-a211-3b6a575fd8b6","trusted":true,"id":"YC0SwWT2IL3Q","colab_type":"code","colab":{}},"cell_type":"code","source":"result = svc.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f1cd09cd-158e-4089-93d8-3ffe8918ae3a","_cell_guid":"61672b15-223d-4329-a1c5-eede8732368c","trusted":true,"id":"uGkTzr1tIzNB","colab_type":"code","outputId":"ccb43ed5-67b8-47dc-e099-8f95dec6b30b","colab":{"base_uri":"https://localhost:8080/","height":35}},"cell_type":"code","source":"\n# numpy 를 Pandas 이용하여 결과 파일로 저장\n\n\nimport pandas as pd\n\nprint(result.shape)\ndf = pd.DataFrame(result, columns=['label'])\n#df = df.replace('dog',1)\n#df = df.replace('cat',0)\ndf.index += 1\ndf.index.name = 'id'\ndf.to_csv('result_namhun_kim-2320.csv',index=True, header=True)","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"PR_Midterm_P4(17011832).ipynb","provenance":[],"collapsed_sections":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"nbformat":4,"nbformat_minor":1}