from skimage import data, color, feature
import skimage.data
import matplotlib.pyplot as plt
import numpy as np
image = data.chelsea()
plt.imshow(image)
plt.title('input image')
image = color.rgb2gray(data.chelsea())
hog_vec, hog_vis = feature.hog(image, visualize=True)
fig, ax = plt.subplots(1, 2, figsize=(12, 6),
subplot_kw = dict(xticks=[], yticks=[]))
ax[0].imshow(image, cmap='gray')
ax[0].set_title('input image')
ax[1].imshow(hog_vis)
ax[1].set_title('Visualization of HOG features')
*HOG 참고
# 안면 인식 알고리즘 구현: linear SVM 사용
# 얼굴 데이터 다운로드
from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people()
#1. 긍정 훈련 표본
from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people()
positive_patches = faces.images
fig, axes = plt.subplots(3, 8, figsize=(9,4),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i, ax in enumerate(axes.flat):
ax.imshow(positive_patches[i], cmap='bone')
plt.show()
#2. 부정 훈련 표본
from skimage import data, transform
images_to_use = ['camera', 'text', 'coins', 'moon',
'page', 'clock', 'immunohistochemistry',
'chelsea', 'coffee', 'hubble_deep_field']
images = [getattr(data, name)() for name in images_to_use[:6]]
images = images + [color.rgb2gray(getattr(data, name)())
for name in images_to_use[6:]]
print(len(images))
fig, axes = plt.subplots(2,5, figsize=(9,4),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i, ax in enumerate(axes.flat):
ax.imshow(images[i], cmap='bone')
plt.show()
from sklearn.feature_extraction.image import PatchExtractor
def extract_patches(img, N, scale=1.0, patch_size=positive_patches[0].shape):
extracted_patch_size = \
tuple((scale * np.array(patch_size)).astype(int))
extractor = PatchExtractor(patch_size=extracted_patch_size,
max_patches=N, random_state=0)
patches = extractor.transform(img[np.newaxis])
if scale != 1:
patches = np.array([transform.resize(patch, patch_size)
for patch in patches])
return patches
negative_patches = np.vstack([extract_patches(im, 1000, scale)
for im in images for scale in [0.5, 1.0, 2.0]])
fig, ax = plt.subplots(6, 10)
for i, axi in enumerate(ax.flat):
axi.imshow(negative_patches[500*i], cmap='gray')
axi.axis('off')
# 집합을 결합하고 HOG 특징 추출
from itertools import chain
X_train = np.array([feature.hog(im)
for im in chain(positive_patches, negative_patches)])
y_train = np.zeros(X_train.shape[0])
y_train[:positive_patches.shape[0]] = 1
from sklearn.svm import LinearSVC
from sklearn.model_selection import GridSearchCV
grid = GridSearchCV(LinearSVC(), {'C':[1.0, 2.0, 4.0, 8.0]})
grid.fit(X_train, y_train)
grid.best_score_
model = grid.best_estimator_
model.fit(X_train, y_train)
test_image = skimage.data.astronaut()
plt.imshow(test_image)
test_image = skimage.data.astronaut()
test_image = skimage.color.rgb2gray(test_image)
test_image = skimage.transform.rescale(test_image, 0.5)
test_image = test_image[:160, 40:180]
plt.imshow(test_image, cmap='gray')
plt.axis('off')
def sliding_window(img, patch_size=positive_patches[0].shape,
istep=2, jstep=2, scale=1.0):
Ni, Nj = (int(scale * s) for s in patch_size)
for i in range(0, img.shape[0] - Ni, istep): #Ni: rows
for j in range(0, img.shape[1] - Nj, jstep): #Nj: columns
patch = img[i:i+Ni, j:j+Nj]
if scale != 1:
patch = transformation.resize(patch, patch_size)
yield(i, j), patch
indices, patches = zip(*sliding_window(test_image))
patches_hog = np.array([feature.hog(patch) for patch in patches])
labels = model.predict(patches_hog)
fig, ax = plt.subplots()
ax.imshow(test_image, cmap='gray')
ax.axis('off')
Ni, Nj = positive_patches[0].shape
indices = np.array(indices)
idxList=[]
for i, j in indices[labels==1]:
ax.add_patch(plt.Rectangle((j,i), Nj, Ni, edgecolor='red',
alpha=0.3, lw=2, facecolor='none'))
idxList.append([i, j])
idx = np.array(idxList)
i, j = idx.mean(axis=0).astype(int)
i, j
fig, ax = plt.subplots()
ax.imshow(test_image, cmap='gray')
ax.axis('off')
Ni, Nj = positive_patches[0].shape
ax.add_patch(plt.Rectangle((j,i), Nj, Ni, edgecolor='red',
alpha=0.5, lw=5, facecolor='none'))
plt.show()
plt.imshow(test_image[i:i+Ni, j:j+Nj], cmap='gray')
plt.show()
'Study > AI' 카테고리의 다른 글
[AI][ML]앙상블(Ensemble) (0) | 2022.08.12 |
---|---|
[AI][ML]랜덤 포레스트(Random Forest) (0) | 2022.08.12 |
[AI][ML]결정 트리(Decision Tree) (0) | 2022.08.11 |
[AI][ML]나이브 베이즈(Naive Bayes) (0) | 2022.08.10 |
[AI][ML]SVM(Support Vector Machine) (0) | 2022.08.10 |