반응형
05-14 05:47
Today
Total
«   2024/05   »
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
관리 메뉴

개발하는 고라니

[Tensorflow.js] 선행 학습된 Model 본문

Programming/Tensorflow.js

[Tensorflow.js] 선행 학습된 Model

조용한고라니 2021. 1. 9. 13:41
반응형

 최근에 '생활코딩'에서 21년 첫 수업으로 머신러닝을 업로드하셨다. 몇 년전에 머신러닝, 딥러닝에 관심이 있어서 텐서플로를 사용해보고자 했는데... 너무 어렵고 이해도 어려울 뿐 더러 일개 학생이 배울 수 있는 곳이 마땅치 않았다. 그러나 항상 초심자의 눈높이에서 쉽게 강의해주시는 생활코딩 덕분에 입문이라도 해보고자 한다. 앞으로 업로드 될 내용들은 강의 내용 + 나의 생각 + 스스로 좀 더 연습한 것을 정리해서 올려놓을 예정이다.


www.tensorflow.org/js/models?hl=ko

 

TensorFlow.js 모델

모든 프로젝트에서 즉시 사용할 수 있는 선행 학습된 TensorFlow.js 모델을 탐색하세요.

www.tensorflow.org

처음 텐서플로를 접할 당시에는 Python으로만 사용이 가능했는데 자바스크립트로도 나왔다. 이곳에 가면 어떤 능력있는 사람들이 특정 주제에 대해 미리 학습시켜놓은 모델들을 사용해볼 수 있다.

 

# 이미지 분류(MobileNet)

* ImageNet 데이터베이스의 라벨로 이미지 분류

 

MobileNet의 사용법은 굉장히 간단하다.

 

1) <script> 태그를 통해 사용

<!-- 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>

<!-- 모델에게 보여주고싶은 이미지 설정 -->
<img id="img" src="dog.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'); //img 태그를 찾아서

  // 모델을 로딩한다. 로딩이 끝나면 모델을 인자로 넘겨 분류를 하고,
  // 가능성이 높은 3개를 배열로 반환한다.
  mobilenet.load().then(model => {
    // Classify the image.
    model.classify(img).then(predictions => {
      console.log('Predictions: ');
      console.log(predictions);
    });
  });
</script>

 

2) NPM을 이용해 Node.js에서 사용

// 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);

 

1) 을 사용해 HTML에서 동작하게 해보았다. 강아지 사진을 올렸을 때 신기하게도 다음과 같이 결과가 나왔다.

  1. 0: {className: "golden retriever", probability: 0.6340572834014893}
  2. 1: {className: "cocker spaniel, English cocker spaniel, cocker", probability: 0.081318698823452}
  3. 2: {className: "Saluki, gazelle hound", probability: 0.05207628011703491}

 

<참고>

opentutorials.org/course/4628/29770

반응형

'Programming > Tensorflow.js' 카테고리의 다른 글

[Tensorflow.js] 모델 Save & Load  (0) 2021.01.09
[Tensorflow.js] 모델 만들기  (0) 2021.01.09
Comments