{"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 \n%pip install imutils\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom imutils import paths\nfrom glob import glob\nimport cv2 \nimport os\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"dataset_train = \"../input/2019-fall-pr-project/train/train/\"\n\ndef feature_vector(image, size=(32, 32)):   #이미지 resize 함수\n    return cv2.resize(image, size).flatten()\n\nprint(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset_train))\n\n\nprint(len(imagePaths))\nprint(imagePaths[0])\n\nrawImages = []\nlabels = []\n\nfor (i, imagePath) in enumerate(imagePaths):\n  # 이미지를 로드하고 클래스 레이블을 추출\n\n    image = cv2.imread(imagePath)\n    label = imagePath.split(os.path.sep)[-1].split(\".\")[0] #라벨 추출\n \n    pixels = feature_vector(image) #이미지 resize\n    \n    rawImages.append(pixels)\n    labels.append(label)\n \n  # 이미지 1000개당 업데이트\n    if i > 0 and i % 1000 == 0:\n        print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))\n  \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(trainImage, testImage, trainLabel, testLabel) = train_test_split(\n    rawImages, labels, test_size=0.25, random_state=42) #이미지와 라벨 서브 샘플링\n\nrawImages_subset = rawImages[:2000]\nlabels_subset= labels[:2000]\n\n(subtrainImage, subtestImage, subtrainLabel, subtestLabel) = train_test_split(\n    rawImages_subset, labels_subset, test_size=0.25, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"[INFO] evaluating raw pixel accuracy...\")\n\nclf = SVC(kernel = 'rbf',gamma='auto', C = 10)\n\nclf.fit(subtrainImage, subtrainLabel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_test = \"../input/2019-fall-pr-project/test1/test1/\"\n\nimagePaths = list(paths.list_images(dataset_test))\n\nrawImages = []\nlabels = []\n\nfor (i, imagePath) in enumerate(imagePaths):\n    # 이미지를 로드하고 클래스 레이블을 추출\n    # 위의 트레인 데이터와 동일하게 작성\n    image = cv2.imread(imagePath)\n \n    pixels = feature_vector(image) #이미지 resize\n    label = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n\n    rawImages.append(pixels)\n    labels.append(label)\n \n  # 이미지 1000개당 업데이트\n    if i > 0 and i % 1000 == 0:\n        print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(len(testImage),len(testLabel))\nacc = clf.score(testImage, testLabel)\nlabels=np.array(labels)\nprint(labels.shape)\nprint(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# numpy 를 Pandas 이용하여 결과 파일로 저장\n\nimport pandas as pd\n\ntestLabel=np.array(testLabel)\nprint(testLabel.shape)\n\ndf = pd.DataFrame([testLabel])\ndf = df.replace('dog',1)\ndf = df.replace('cat',0)\n\ndf.T.to_csv('results-yk-v2.csv',index=True, header=False)","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}