Google Teachable Machine 활용 워크샵 Activity 개발 프로젝트(1)

김지원·2022년 11월 10일
1

RasberryPi4

목록 보기
4/4
post-thumbnail

Teachable Machine이란?

Teachable Machine 링크
머신러닝 모델을 누구나 쉽고 빠르게 만들 수 있는 웹 기반 도구이다.
이용하기 위해 사전 머신러닝 지식이 요구되지 않는다.
머신러닝 학습 코드를 작성하지 않아도 이미지, 소리, 또는 동작를 학습시켜 자신만의 모델을 만들 수 있다.

3가지 모델을 학습하여 사용할 수 있다.
(1) MobileNet: 이미지 분류
(2) Speeach Commands: audio snippets 분류
(3) PoseNet: 몸 동작(body pose) 분류

Teachable Machine Github 접속

Teachable Machine Community 깃허브에 들어가면 libraries 와 snippets 섹션이 있다.
1. libraries section:
Teachable Machine에서 사용하는 모든 머신러닝 코드를 볼 수 있다.
머신러닝 모델 학습 및 사용을 위한 라이브러리인 Tensorflow.js를 사용한다.
image, audio, 그리고 pose helper 라이브러리를 위한 API 또한 확인 가능하다.
2. snippets
마크다운 snippets 확인 가능하다. Javascript, Java, Python 사용.

✨MobileNet

# Install하기
npm i @teachablemachine/image

분류(classification), 탐지(detection), embeddings, segmentation 작업 수행을 위해 사용 가능하다.

이 TensorFlow.js 모델은 머신러닝 지식을 필요로하지 않는다.
<img>,<video>, <canvas> element 와 같은 어떤 browser-based 이미지 element도 입력(input)으로 사용 가능하고, 예측 결과(predictions)와 신뢰도(confidences) 를 배열로 return 한다.

MobileNet에 대해 더 알고 싶다면 이 링크에서 참고하기.

🔎 MobileNet 사용 방법

JavaScript 프로젝트에서 아래 두 가지 방법으로 MobileNet 모델을 사용할 수 있다.
(1) script tag를 통해 혹은 (2) NPM로 설치하고 Parcel, WebPack 또는 Rollup과 같은 build tool를 통해 사용 가능하다.

1. Script Tag

<!-- Load TensorFlow.js. This is required to use MobileNet. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.1"> </script>
<!-- Load the MobileNet model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@1.0.0"> </script>

<!-- Replace this with your image. Make sure CORS settings allow reading the image! -->
<img id="img" src="cat.jpg"></img>

<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
  // Notice there is no 'import' statement. 'mobilenet' and 'tf' is
  // available on the index-page because of the script tag above.

  const img = document.getElementById('img');

  // Load the model.
  mobilenet.load().then(model => {
    // Classify the image.
    model.classify(img).then(predictions => {
      console.log('Predictions: ');
      console.log(predictions);
    });
  });
</script>

2. NPM

// Note: you do not need to import @tensorflow/tfjs here.

const mobilenet = require('@tensorflow-models/mobilenet');

const img = document.getElementById('img');

// Load the model.
const model = await mobilenet.load();

// Classify the image.
const predictions = await model.classify(img);

console.log('Predictions: ');
console.log(predictions);

🔎 MobileNet API

🚩 Model 불러오기 (Loading the model)

<script src> method를 사용하면 mobilenet 모듈이 자동으로 포함된다.

mobilenet.load({
    version: 1,
    alpha?: 0.25 | .50 | .75 | 1.0,
    modelUrl?: string
    inputRange?: [number, number]
  }
)

Args:

  • version: MobileNet 버전 (MobileNetV1 또는 MobileNetV2)
  • alpha: network의 width, 성능을 위한 trading accuracy 를 조정한다. 값이 작으면 정확도 감소, 성능 향상한다. 값 0.25sms 버전1(V1) 모델에서만 설정 가능하다. Default 값은 1.0.
  • modelUrl: Optional param, custom model url 또는 tf.io.IOHandler object를 지정할 수 있다. model object를 return 한다.
  • inputRange: Optional param, pixel 값 범위를 지정. [0,1] 또는 [-1,1]

🚩 classification 수행하기

MobileNet.classify (image를 입력값으로 받고, top classes 와 probabilities의 배열을 return함) 없이 classification 할 수 있다.

  • 전이학습(transfer learning) 을 수행하고 싶다면 infer method를 볼 것.
model.classify(
  img: tf.Tensor3D | ImageData | HTMLImageElement |
      HTMLCanvasElement | HTMLVideoElement,
  topk?: number
)

Args:

  • img: classification 수행하기 위한 Tensor 또는 image
  • topk: return 할 top probabilities 개수, Default 값 3

return 형태는 아래와 같다.

[{
  className: "Egyptian cat",
  probability: 0.8380282521247864
}, {
  className: "tabby, tabby cat",
  probability: 0.04644153267145157
}, {
  className: "Siamese cat, Siamese",
  probability: 0.024488523602485657
}]

🔎 Getting Embeddings

전이 학습을 위한 image의 embedding을 얻을 수 있다.
alpha 값에 다라 embedding의 크기가 달라진다.

model.infer(
  img: tf.Tensor3D | ImageData | HTMLImageElement |
      HTMLCanvasElement | HTMLVideoElement,
  embedding = false
)

Args:

  • img: classification 수행하기 위한 Tensor 또는 image
  • embedding: true라면 embedding을 return 함. 아니면 1000-dim unnormalized logits를 return한다.

💡 참고할 만한 프로젝트들

Teachable Machine Node Example도 있다.

Experiments with Google
TensorFlow Lite for Microcontrollers

굉장히 다양한 프로젝트들이 있다. 여기서 아이디어를 얻을 수도 있을 것 같다!!

https://github.com/googlecreativelab/teachablemachine-community/

profile
Make your lives Extraordinary!

0개의 댓글