{
  "id": 117040,
  "title": "[HW] image_classification-sungbin",
  "url": "/competitions/2019-fall-pr-project/discussion/117040",
  "author_name": "sung_bin",
  "post_date": "2019-11-13T03:38:56.361000",
  "votes": 1,
  "comment_count": 0,
  "views": 0,
  "content": "<h1>벡터 변환 함수 사용</h1>\n\n<p>def image_to_feature_vector(image, size=(32, 32)):\n    return cv2.resize(image, size).flatten()</p>\n\n<p>dataset_train = \"/content/input2/train/\"</p>\n\n<p>print(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset_train))</p>\n\n<p>print(len(imagePaths))</p>\n\n<h1>매트릭스 초기화</h1>\n\n<h1>and labels list</h1>\n\n<p>rawImages = []\nlabels = []\nfor (i, imagePath) in enumerate(imagePaths):\n    # load the image and extract the class label (assuming that our\n    # path as the format: /path/to/dataset/{class}.{image_num}.jpg</p>\n\n<pre><code>image = cv2.imread(imagePath)\nlabel = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n</code></pre>\n\n<h1>feature 추출</h1>\n\n<pre><code># histogram to characterize the color distribution of the pixels\n# in the image\n\npixels = image_to_feature_vector(image)\n</code></pre>\n\n<h1>매트릭스 업로드</h1>\n\n<pre><code># respectively\nrawImages.append(pixels)\nlabels.append(label)\n\n\nif i &gt; 0 and i % 1000 == 0:\n    print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))\n</code></pre>\n\n<h1>데이터 셋 서브 샘플링 &amp; Train-Validation 나누기</h1>\n\n<p>rawImages = np.array(rawImages)\nlabels = np.array(labels)</p>\n\n<p>print(\"[INFO] pixels matrix: {:.2f}MB\".format(\nrawImages.nbytes / (1024 * 1000.0)))\nbUseCompleteDataset = False\nif bUseCompleteDataset:\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages, labels, test_size=0.25, random_state=42)\nelse:\n    rawImages_subset = rawImages[:2000]\n    labels_subset = labels[:2000]\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages_subset, labels_subset, test_size=0.25, random_state=42)</p>\n\n<h1>학습 기반 분류기 설계</h1>\n\n<p>print(\"[INFO] evaluating raw pixel accuracy...\")\nneighbors = [1, 3, 5, 7, 13]</p>\n\n<p>for k in neighbors:\n    model = KNeighborsClassifier(n_neighbors= 5)\n    a=model.fit(trainRI, trainRL)\n    acc = model.score(testRI, testRL)\n    print(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))</p>\n\n<h1>테스트 데이터 준비</h1>\n\n<p>dataset_test = \"/content/input2/test1/\"</p>\n\n<p>imagePaths_test=list(paths.list_images(dataset_test))</p>\n\n<p>print(len(imagePaths_test))\nrawImages_test=[]\nlabels_test=[]</p>\n\n<p>for(i,imagePath_test) in enumerate(imagePaths_test):\n    image=cv2.imread(imagePath_test)\n    label=imagePath_test.split(os.path.sep)[-1].split(\".\")[0]</p>\n\n<pre><code>pixels=image_to_feature_vector(image)\n\nrawImages_test.append(pixels)\nlabels_test.append(label)\n\n\nif i&gt;0 and i%1000==0:\n  print(\"[INFO] processed {}/{}\".format(i,len(imagePaths_test)))\n</code></pre>\n\n<h1>numpy 를 Pandas 이용하여 결과 파일로 저장</h1>\n\n<p>import pandas as pd</p>\n\n<p>result=np.reshape(result,(-1,1))</p>\n\n<p>print(result.shape)\ndf = pd.DataFrame(result, columns=[\"label\"])\ndf.index.name = 'id'\ndf.index += 1\ndf = df.replace('dog',1)\ndf = df.replace('cat',0)\ndf = df.rename({'1':'id','0':'label'})\ndf.to_csv('sungbin-v2.csv',index=True, header=True)</p>",
  "messages": [
    {
      "id": 671685,
      "postDate": "2019-11-13T03:38:56.363Z",
      "content": "<h1>벡터 변환 함수 사용</h1>\n\n<p>def image_to_feature_vector(image, size=(32, 32)):\n    return cv2.resize(image, size).flatten()</p>\n\n<p>dataset_train = \"/content/input2/train/\"</p>\n\n<p>print(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset_train))</p>\n\n<p>print(len(imagePaths))</p>\n\n<h1>매트릭스 초기화</h1>\n\n<h1>and labels list</h1>\n\n<p>rawImages = []\nlabels = []\nfor (i, imagePath) in enumerate(imagePaths):\n    # load the image and extract the class label (assuming that our\n    # path as the format: /path/to/dataset/{class}.{image_num}.jpg</p>\n\n<pre><code>image = cv2.imread(imagePath)\nlabel = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n</code></pre>\n\n<h1>feature 추출</h1>\n\n<pre><code># histogram to characterize the color distribution of the pixels\n# in the image\n\npixels = image_to_feature_vector(image)\n</code></pre>\n\n<h1>매트릭스 업로드</h1>\n\n<pre><code># respectively\nrawImages.append(pixels)\nlabels.append(label)\n\n\nif i &gt; 0 and i % 1000 == 0:\n    print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))\n</code></pre>\n\n<h1>데이터 셋 서브 샘플링 &amp; Train-Validation 나누기</h1>\n\n<p>rawImages = np.array(rawImages)\nlabels = np.array(labels)</p>\n\n<p>print(\"[INFO] pixels matrix: {:.2f}MB\".format(\nrawImages.nbytes / (1024 * 1000.0)))\nbUseCompleteDataset = False\nif bUseCompleteDataset:\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages, labels, test_size=0.25, random_state=42)\nelse:\n    rawImages_subset = rawImages[:2000]\n    labels_subset = labels[:2000]\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages_subset, labels_subset, test_size=0.25, random_state=42)</p>\n\n<h1>학습 기반 분류기 설계</h1>\n\n<p>print(\"[INFO] evaluating raw pixel accuracy...\")\nneighbors = [1, 3, 5, 7, 13]</p>\n\n<p>for k in neighbors:\n    model = KNeighborsClassifier(n_neighbors= 5)\n    a=model.fit(trainRI, trainRL)\n    acc = model.score(testRI, testRL)\n    print(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))</p>\n\n<h1>테스트 데이터 준비</h1>\n\n<p>dataset_test = \"/content/input2/test1/\"</p>\n\n<p>imagePaths_test=list(paths.list_images(dataset_test))</p>\n\n<p>print(len(imagePaths_test))\nrawImages_test=[]\nlabels_test=[]</p>\n\n<p>for(i,imagePath_test) in enumerate(imagePaths_test):\n    image=cv2.imread(imagePath_test)\n    label=imagePath_test.split(os.path.sep)[-1].split(\".\")[0]</p>\n\n<pre><code>pixels=image_to_feature_vector(image)\n\nrawImages_test.append(pixels)\nlabels_test.append(label)\n\n\nif i&gt;0 and i%1000==0:\n  print(\"[INFO] processed {}/{}\".format(i,len(imagePaths_test)))\n</code></pre>\n\n<h1>numpy 를 Pandas 이용하여 결과 파일로 저장</h1>\n\n<p>import pandas as pd</p>\n\n<p>result=np.reshape(result,(-1,1))</p>\n\n<p>print(result.shape)\ndf = pd.DataFrame(result, columns=[\"label\"])\ndf.index.name = 'id'\ndf.index += 1\ndf = df.replace('dog',1)\ndf = df.replace('cat',0)\ndf = df.rename({'1':'id','0':'label'})\ndf.to_csv('sungbin-v2.csv',index=True, header=True)</p>",
      "rawMarkdown": "#벡터 변환 함수 사용\ndef image_to_feature_vector(image, size=(32, 32)):\n    return cv2.resize(image, size).flatten()\n\ndataset_train = \"/content/input2/train/\"\n\nprint(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset_train))\n\nprint(len(imagePaths))\n# 매트릭스 초기화\n# and labels list\nrawImages = []\nlabels = []\nfor (i, imagePath) in enumerate(imagePaths):\n\t# load the image and extract the class label (assuming that our\n\t# path as the format: /path/to/dataset/{class}.{image_num}.jpg\n\n    image = cv2.imread(imagePath)\n    label = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n \n# feature 추출\n\t# histogram to characterize the color distribution of the pixels\n\t# in the image\n\n    pixels = image_to_feature_vector(image)\n    \n \n# 매트릭스 업로드\n\t# respectively\n    rawImages.append(pixels)\n    labels.append(label)\n \n\n    if i &gt; 0 and i % 1000 == 0:\n        print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))\n\n\n\n\n#데이터 셋 서브 샘플링 &amp; Train-Validation 나누기\nrawImages = np.array(rawImages)\nlabels = np.array(labels)\n\nprint(\"[INFO] pixels matrix: {:.2f}MB\".format(\nrawImages.nbytes / (1024 * 1000.0)))\nbUseCompleteDataset = False\nif bUseCompleteDataset:\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages, labels, test_size=0.25, random_state=42)\nelse:\n    rawImages_subset = rawImages[:2000]\n    labels_subset = labels[:2000]\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages_subset, labels_subset, test_size=0.25, random_state=42)\n\n\n\n#학습 기반 분류기 설계\n\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nneighbors = [1, 3, 5, 7, 13]\n\nfor k in neighbors:\n    model = KNeighborsClassifier(n_neighbors= 5)\n    a=model.fit(trainRI, trainRL)\n    acc = model.score(testRI, testRL)\n    print(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))\n\n\n\n#테스트 데이터 준비\n\n\ndataset_test = \"/content/input2/test1/\"\n\nimagePaths_test=list(paths.list_images(dataset_test))\n\nprint(len(imagePaths_test))\nrawImages_test=[]\nlabels_test=[]\n\nfor(i,imagePath_test) in enumerate(imagePaths_test):\n    image=cv2.imread(imagePath_test)\n    label=imagePath_test.split(os.path.sep)[-1].split(\".\")[0]\n\n    pixels=image_to_feature_vector(image)\n\n    rawImages_test.append(pixels)\n    labels_test.append(label)\n\n\n    if i&gt;0 and i%1000==0:\n      print(\"[INFO] processed {}/{}\".format(i,len(imagePaths_test)))\n\n\n\n\n\n\n#numpy 를 Pandas 이용하여 결과 파일로 저장\nimport pandas as pd\n\n\nresult=np.reshape(result,(-1,1))\n\nprint(result.shape)\ndf = pd.DataFrame(result, columns=[\"label\"])\ndf.index.name = 'id'\ndf.index += 1\ndf = df.replace('dog',1)\ndf = df.replace('cat',0)\ndf = df.rename({'1':'id','0':'label'})\ndf.to_csv('sungbin-v2.csv',index=True, header=True)",
      "votes": 1
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "671685": "#벡터 변환 함수 사용\ndef image_to_feature_vector(image, size=(32, 32)):\n    return cv2.resize(image, size).flatten()\n\ndataset_train = \"/content/input2/train/\"\n\nprint(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset_train))\n\nprint(len(imagePaths))\n# 매트릭스 초기화\n# and labels list\nrawImages = []\nlabels = []\nfor (i, imagePath) in enumerate(imagePaths):\n\t# load the image and extract the class label (assuming that our\n\t# path as the format: /path/to/dataset/{class}.{image_num}.jpg\n\n    image = cv2.imread(imagePath)\n    label = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n \n# feature 추출\n\t# histogram to characterize the color distribution of the pixels\n\t# in the image\n\n    pixels = image_to_feature_vector(image)\n    \n \n# 매트릭스 업로드\n\t# respectively\n    rawImages.append(pixels)\n    labels.append(label)\n \n\n    if i &gt; 0 and i % 1000 == 0:\n        print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))\n\n\n\n\n#데이터 셋 서브 샘플링 &amp; Train-Validation 나누기\nrawImages = np.array(rawImages)\nlabels = np.array(labels)\n\nprint(\"[INFO] pixels matrix: {:.2f}MB\".format(\nrawImages.nbytes / (1024 * 1000.0)))\nbUseCompleteDataset = False\nif bUseCompleteDataset:\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages, labels, test_size=0.25, random_state=42)\nelse:\n    rawImages_subset = rawImages[:2000]\n    labels_subset = labels[:2000]\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages_subset, labels_subset, test_size=0.25, random_state=42)\n\n\n\n#학습 기반 분류기 설계\n\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nneighbors = [1, 3, 5, 7, 13]\n\nfor k in neighbors:\n    model = KNeighborsClassifier(n_neighbors= 5)\n    a=model.fit(trainRI, trainRL)\n    acc = model.score(testRI, testRL)\n    print(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))\n\n\n\n#테스트 데이터 준비\n\n\ndataset_test = \"/content/input2/test1/\"\n\nimagePaths_test=list(paths.list_images(dataset_test))\n\nprint(len(imagePaths_test))\nrawImages_test=[]\nlabels_test=[]\n\nfor(i,imagePath_test) in enumerate(imagePaths_test):\n    image=cv2.imread(imagePath_test)\n    label=imagePath_test.split(os.path.sep)[-1].split(\".\")[0]\n\n    pixels=image_to_feature_vector(image)\n\n    rawImages_test.append(pixels)\n    labels_test.append(label)\n\n\n    if i&gt;0 and i%1000==0:\n      print(\"[INFO] processed {}/{}\".format(i,len(imagePaths_test)))\n\n\n\n\n\n\n#numpy 를 Pandas 이용하여 결과 파일로 저장\nimport pandas as pd\n\n\nresult=np.reshape(result,(-1,1))\n\nprint(result.shape)\ndf = pd.DataFrame(result, columns=[\"label\"])\ndf.index.name = 'id'\ndf.index += 1\ndf = df.replace('dog',1)\ndf = df.replace('cat',0)\ndf = df.rename({'1':'id','0':'label'})\ndf.to_csv('sungbin-v2.csv',index=True, header=True)"
  }
}