Season 1 아카이브/프로그래밍
                
              서포터 벡터 머신과 이미지 인식 (2) - 이미지 픽셀을 사용한 학습
                문장전달자
                 2016. 11. 6. 16:40
              
              
                    
        728x90
    
    
  서포터 벡터 머신과 이미지 인식 (2) 
접근법 1 - 학습 알고리즘에 대해 속성으로 이미지 픽셀을 사용하도록 하는 작업
SVM은 매우 커다란 차원 공간을 가지는 이미지 인식에 적용할 수 있다(이미지 각 픽셀의 값을 속성으로 고려한다). 사람의 얼굴을 고려해 그 사람이 속한 리스트를 예측하는 것이다. 여기서 학습 데이터는 레이블된 사람 얼굴 이미지인 인스턴스 그룹이고, 보지 못한 새로운 인스턴스의 레이블을 예측하는 모델을 학습시킨다.
<학습 알고리즘에 대해 속성으로 이미지 픽셀 사용>
- 픽셀값 → 학습속성
 - 개별 레이블 → 목적 범주
 
데이터셋을 가져와서 출력해보자. 아래는 해당 데이터셋에 대한 설명 출력이다. 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27  | >>> import sklearn as sk >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from sklearn.datasets import fetch_olivetti_faces >>> faces = fetch_olivetti_faces() >>> print faces.DESCR Modified Olivetti faces dataset. The original database was available from     http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html The version retrieved here comes in MATLAB format from the personal web page of Sam Roweis:     http://www.cs.nyu.edu/~roweis/ There are ten different images of each of 40 distinct subjects. For some subjects, the images were taken at different times, varying the lighting, facial expressions (open / closed eyes, smiling / not smiling) and facial details (glasses / no glasses). All the images were taken against a dark homogeneous background with the subjects in an upright, frontal position (with tolerance for some side movement). The original dataset consisted of 92 x 112, while the Roweis version consists of 64x64 images.  | cs | 
이미지, 데이터, 타겟의 배열은 다음과 같다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122  | {'images': array([[[ 0.30991736,  0.36776859,  0.41735536, ...,  0.37190083,           0.33057851,  0.30578512],         [ 0.3429752 ,  0.40495867,  0.43801653, ...,  0.37190083,           0.33884299,  0.3140496 ],         [ 0.3429752 ,  0.41735536,  0.45041323, ...,  0.38016528,           0.33884299,  0.29752067],         ...,          [ 0.21487603,  0.20661157,  0.22314049, ...,  0.15289256,           0.16528925,  0.17355372],         [ 0.20247933,  0.2107438 ,  0.2107438 , ...,  0.14876033,           0.16115703,  0.16528925],         [ 0.20247933,  0.20661157,  0.20247933, ...,  0.15289256,           0.16115703,  0.1570248 ]],        [[ 0.45454547,  0.47107437,  0.51239669, ...,  0.19008264,           0.18595041,  0.18595041],         [ 0.44628099,  0.48347107,  0.52066118, ...,  0.21487603,           0.2107438 ,  0.2107438 ],         [ 0.49586776,  0.5165289 ,  0.53305787, ...,  0.20247933,           0.20661157,  0.20661157],         ...,          [ 0.77272725,  0.78099173,  0.79338843, ...,  0.14462809,           0.14462809,  0.14462809],         [ 0.77272725,  0.77685952,  0.78925622, ...,  0.13636364,           0.13636364,  0.13636364],         [ 0.76446283,  0.78925622,  0.78099173, ...,  0.15289256,           0.15289256,  0.15289256]],        [[ 0.31818181,  0.40082645,  0.49173555, ...,  0.40082645,           0.35537189,  0.30991736],         [ 0.30991736,  0.39669421,  0.47933885, ...,  0.40495867,           0.37603307,  0.30165288],         [ 0.26859504,  0.34710744,  0.45454547, ...,  0.39669421,           0.37190083,  0.30991736],         ...,          [ 0.1322314 ,  0.09917355,  0.08264463, ...,  0.13636364,           0.14876033,  0.15289256],         [ 0.11570248,  0.09504132,  0.0785124 , ...,  0.14462809,           0.14462809,  0.1570248 ],         [ 0.11157025,  0.09090909,  0.0785124 , ...,  0.14049587,           0.14876033,  0.15289256]],        ...,         [[ 0.5       ,  0.53305787,  0.60743803, ...,  0.28512397,           0.23966943,  0.21487603],         [ 0.49173555,  0.54132229,  0.60330576, ...,  0.29752067,           0.20247933,  0.20661157],         [ 0.46694216,  0.55785125,  0.61983472, ...,  0.29752067,           0.17768595,  0.18595041],         ...,          [ 0.03305785,  0.46280992,  0.5289256 , ...,  0.17355372,           0.17355372,  0.16942149],         [ 0.1570248 ,  0.52479339,  0.53305787, ...,  0.16528925,           0.1570248 ,  0.18595041],         [ 0.45454547,  0.52066118,  0.53305787, ...,  0.17768595,           0.14876033,  0.19008264]],        [[ 0.21487603,  0.21900827,  0.21900827, ...,  0.71487606,           0.71487606,  0.69421488],         [ 0.20247933,  0.20661157,  0.20661157, ...,  0.71074378,           0.70661157,  0.69421488],         [ 0.2107438 ,  0.20661157,  0.20661157, ...,  0.6859504 ,           0.69008267,  0.69421488],         ...,          [ 0.2644628 ,  0.25619835,  0.26033059, ...,  0.54132229,           0.57438016,  0.59090906],         [ 0.26859504,  0.2644628 ,  0.26859504, ...,  0.56198347,           0.58264464,  0.59504133],         [ 0.27272728,  0.26859504,  0.27272728, ...,  0.57438016,           0.59090906,  0.60330576]],        [[ 0.5165289 ,  0.46280992,  0.28099173, ...,  0.57851237,           0.54132229,  0.60330576],         [ 0.5165289 ,  0.45041323,  0.29338843, ...,  0.58264464,           0.55371898,  0.57851237],         [ 0.5165289 ,  0.44214877,  0.29338843, ...,  0.59917355,           0.57851237,  0.54545456],         ...,          [ 0.39256197,  0.41322315,  0.38842976, ...,  0.33471075,           0.37190083,  0.39669421],         [ 0.39256197,  0.38429752,  0.40495867, ...,  0.33057851,           0.35950413,  0.37603307],         [ 0.36776859,  0.40495867,  0.39669421, ...,  0.35950413,           0.35537189,  0.38429752]]], dtype=float32),  'data': array([[ 0.30991736,  0.36776859,  0.41735536, ...,  0.15289256,          0.16115703,  0.1570248 ],        [ 0.45454547,  0.47107437,  0.51239669, ...,  0.15289256,          0.15289256,  0.15289256],        [ 0.31818181,  0.40082645,  0.49173555, ...,  0.14049587,          0.14876033,  0.15289256],        ...,         [ 0.5       ,  0.53305787,  0.60743803, ...,  0.17768595,          0.14876033,  0.19008264],        [ 0.21487603,  0.21900827,  0.21900827, ...,  0.57438016,          0.59090906,  0.60330576],        [ 0.5165289 ,  0.46280992,  0.28099173, ...,  0.35950413,          0.35537189,  0.38429752]], dtype=float32), 'target': array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,         1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,         3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  5,         5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,         6,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,         8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10,        10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,        11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13,        13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15,        15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,        18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20,        20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,        22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,        23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,        25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27,        27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28,        28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30,        30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32,        32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,        34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35,        35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37,        37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39,        39, 39, 39, 39, 39, 39, 39, 39, 39]) }  | cs | 
실제 이미지를 간략히 보면 다음과 같다.
The Database of Faces at a glance (http://www.cl.cam.ac.uk/research/dtg/attarchive/facesataglance.html)
데이터셋은 40명의 (서로 다른 조명조건과 얼굴 표정으로 찍혀있는) 총 400개의 얼굴이미지로, face 오브젝트의 주요 내용은 다음과 같다. 데이터는 4,096(64*64) 픽셀로, 총 400개의 이미지가 있으며, 목적범주는 0 에서 39까지의 배열이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  | >>> import sklearn as sk >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from sklearn.datasets import fetch_olivetti_faces >>> faces = fetch_olivetti_faces() >>> print faces.keys() ['images', 'data', 'target', 'DESCR'] >>> print faces.images.shape (400, 64, 64) >>> print faces.data.shape (400, 4096) >>> print faces.target.shape (400,)  | cs | 
이미지 정규화 여부를 확인한다. 아래를 통해 이미지가 0 과 1 사이의 값임을 확인할 수 있다.
1 2  | >>> print np.max(faces.data), np.min(faces.data), np.mean(faces.data) 1.0 0.0 0.547043  | cs | 
이미 이미지는 정규화되어 있으므로, 본격적인 학습 전에 일부 얼굴을 출력해보자.
1 2 3 4 5 6 7 8 9 10 11 12  | >>> %matplotlib inline >>>  >>> def print_faces(images, target, top_n): >>>     fig = plt.figure(figsize = (12,12)) >>>     fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05) >>>     for i in range(top_n): >>>         p = fig.add_subplot(20, 20, i+1, xticks=[], yticks=[]) >>>         p.imshow(images[i], cmap=plt.cm.bone) >>>         p.text(0, 14, str(target[i])) >>>         p.text(0, 60, str(i)) >>>          >>> print_faces(faces.images, faces.target, 20)  | cs | 
※ matplotlib.figure http://matplotlib.org/api/figure_api.html
728x90