# 0. 패키기 ----------------------
library(tidymodels)
library(tidyverse)
library(palmerpenguins)
library(themis)
library(kknn)
# 1. 데이터 ----------------------
set.seed(999)
<- penguins %>%
penguins_tbl
drop_na()
# 2. 훈련/시험 데이터 분할 ----------------------
<- initial_split(penguins_tbl, strata = species )
penguin_split
<- training(penguin_split)
penguin_train
<- testing(penguin_split)
penguin_test
# 3. Feature Engineering ----------------------
## 3.1. 훈련 데이터
<- recipe(species ~., data = penguin_train) %>%
penguin_rec # class unbalance
::step_downsample(species) %>%
themis# 정규화
step_normalize(all_numeric()) %>%
# 비법을 데이터에 적용
prep()
<- juice(penguin_rec) # 데이터만 추출
tbl_train
## 3.2. 시험 데이터
<- bake(penguin_rec, new_data = penguin_test)
tbl_test
# 4. 학습 ------------------------
<- nearest_neighbor() %>%
knn_spec set_engine("kknn") %>%
set_mode("classification")
<- knn_spec %>%
knn_fit fit(species ~., data = tbl_train)
knn_fit
parsnip model object
Call:
kknn::train.kknn(formula = species ~ ., data = data, ks = min_rows(5, data, 5))
Type of response variable: nominal
Minimal misclassification: 0.01960784
Best kernel: optimal
Best k: 5
# 5. 평가 ------------------------
%>%
knn_fit predict(tbl_test) %>%
bind_cols(tbl_test) %>%
metrics(truth = species, estimate = .pred_class)
# A tibble: 2 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy multiclass 0.988
2 kap multiclass 0.981
%>%
knn_fit predict(tbl_test) %>%
bind_cols(tbl_test) %>%
conf_mat(truth = species, estimate = .pred_class)
Truth
Prediction Adelie Chinstrap Gentoo
Adelie 36 0 0
Chinstrap 1 17 0
Gentoo 0 0 30
%>%
knn_fit # 펭귄 종 예측 확률
predict(tbl_test, type = "prob") %>%
bind_cols(tbl_test) %>%
# 시각화
# gain_curve(species, .pred_Adelie:.pred_Gentoo) %>%
roc_curve(species, .pred_Adelie:.pred_Gentoo) %>%
autoplot() +
theme_light()
<- tbl_test %>%
new_penguin slice_sample(n = 1)
%>%
knn_fit predict(new_penguin) %>%
bind_cols(knn_fit %>% predict(new_penguin, type = "prob") )
# A tibble: 1 × 4
.pred_class .pred_Adelie .pred_Chinstrap .pred_Gentoo
<fct> <dbl> <dbl> <dbl>
1 Gentoo 0 0 1
기계학습(Machine Learning)
Viola-Jones object detection based on Haar features
SIFT (Scale-invariant feature transform)
HOG (Histogram of oriented gradients)
딥러닝(Deep Learning)
R-CNN
You Only Look Once(YOLO)
Single Shot MultiBox Detector (SSD)
## YOLO 모형 다운로드 설치
= "mycomputer/myfolder/yolo.h5"
modelpath
from imageai import Detection
= Detection.ObjectDetection()
yolo
yolo.setModelTypeAsYOLOv3()
yolo.setModelPath(modelpath)
yolo.loadModel()
## 비디오 실행
import cv2
= cv2.VideoCapture(0) #0=front-cam, 1=back-cam
cam set(cv2.CAP_PROP_FRAME_WIDTH, 1300)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1500)
cam.
while True:
## read frames
= cam.read()
ret, img ## predict yolo
= yolo.detectCustomObjectsFromImage(input_image=img,
img, preds =None, input_type="array",
custom_objects="array",
output_type=70,
minimum_percentage_probability=False,
display_percentage_probability=True)
display_object_name## display predictions
"", img)
cv2.imshow(## press q or Esc to quit
if (cv2.waitKey(1) & 0xFF == ord("q")) or (cv2.waitKey(1)==27):
break
## close camera
cam.release() cv2.destroyAllWindows()
## Transformers 버전이 낮은 경우 YolosFeatureExtractor 가 없음
import transformers
print(transformers.__version__)
4.20.1
from transformers import YolosFeatureExtractor, YolosForObjectDetection
<frozen importlib._bootstrap>:219: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject
from PIL import Image
import requests
import io
import torch
import matplotlib.pyplot as plt
= 'http://images.cocodataset.org/val2017/000000039769.jpg'
url_input = Image.open(requests.get(url_input, stream=True).raw)
image
# https://huggingface.co/spaces/imkaushalpatel/YOLOv5/blame/754ef7b10ecd5c4c2db55fcb9240361e82066c6a/app.py
= YolosFeatureExtractor.from_pretrained('hustvl/yolos-tiny')
feature_extractor = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
yolo_model
def fig2img(fig):
= io.BytesIO()
buf
fig.savefig(buf)0)
buf.seek(= Image.open(buf)
img return img
# colors for visualization
= [
COLORS 0.000, 0.447, 0.741],
[0.850, 0.325, 0.098],
[0.929, 0.694, 0.125],
[0.494, 0.184, 0.556],
[0.466, 0.674, 0.188],
[0.301, 0.745, 0.933]
[
]
def make_prediction(img, feature_extractor, model):
= feature_extractor(img, return_tensors="pt")
inputs = model(**inputs)
outputs = torch.tensor([tuple(reversed(img.size))])
img_size = feature_extractor.post_process(outputs, img_size)
processed_outputs return processed_outputs[0]
def visualize_prediction(pil_img, output_dict, threshold=0.7, id2label=None):
= output_dict["scores"] > threshold
keep = output_dict["boxes"][keep].tolist()
boxes = output_dict["scores"][keep].tolist()
scores = output_dict["labels"][keep].tolist()
labels if id2label is not None:
= [id2label[x] for x in labels]
labels
=(16, 10))
plt.figure(figsize
plt.imshow(pil_img)= plt.gca()
ax = COLORS * 100
colors for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
- xmin, ymax - ymin, fill=False, color=color, linewidth=3))
ax.add_patch(plt.Rectangle((xmin, ymin), xmax f"{label}: {score:0.2f}", fontsize=15, bbox=dict(facecolor="yellow", alpha=0.5))
ax.text(xmin, ymin, "off")
plt.axis(return fig2img(plt.gcf())
= make_prediction(image, feature_extractor, yolo_model)
processed_outputs = 0.7
threshold
= visualize_prediction(image, processed_outputs, threshold, yolo_model.config.id2label)
viz_img
plt.show()