Tensorflow.jsの画像認識って

2024.03.11

Logging

おはようございます、Tensorflow.jsの画像認識ってドキュメント通り書いて上手く画像認識できますか?自分が試してみたら、どうも下記のエラーがでて上手く動作してくれなかったのでもしかしたらと思いバージョンをアップしたら動作してくれました。

Uncaught (in promise) Error: Tensorflow Op is not supported: _FusedConv2D
<!-- Load TensorFlow.js. This is required to use MobileNet. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.17.0"> </script>
<!-- Load the MobileNet model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@2.1.1"> </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>

因みに自分は画像投稿系のサイトで使用するために今回のTensorflow.jsを使用するのですが、よくよく調べているとファインチューニングが出来るようです。ファインチューニングとは一度学習したものに再学習を埋め込む手法といえば良いのかな?要するにカスタマイズしてある分類に特化させる手法のことを指します。今のところ学習済みのモデルで全然判定されるのでOKだと思うのですが、ユーザーさんから認識できないという不満の声が上がれば対応しないといけなくなりそうです。

明日へ続く。

著者名  @taoka_toshiaki

Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki

タグ

'src', below, const img, Error, getElementById, gt, img, img&quot, Load TensorFlow.js, lt, MobileNet, mobilenet.load, model, predictions, quot, statement, then, Uncaught, ファインチューニング,

Photo by Pixabay on Pexels.com

TensorFlow(テンソルフロー)で画像分類させたら車も人の顔って😇

2022.08.07

Logging

こんにちは、今日もまだ呟くこともしないで日が暮れるかもです。

今日は機械学習で画像分類させることを昨日からゴニョゴニョとしていて、やっとこさ自作のモデルから判定することが出来たのですが、あまり精度が良くないので正直な所、残念です。もっと精度の良いものを作れれば良いのになって思いますが、今の力量ではココらへんですね。

因みにココから画像判別の精度を上げるためにはコードをある程度、作り込まないといけないです。あとはデータ量ももう少し多くのデータが必要です。今回作っていてPythonもなかなか面白いなってね感じました。そして結構、書きやすいなとも思ったのですが、まだまだゴリゴリとコードをPythonで書けるわけではないので、もっと勉強しないとなって事です。

Python言語は結構人気だし、機械学習は花形なんですよ。そういう言語を自在に使えるようになりたいなって思います、そしてPHPやJSなどやフレームワークもゴニョゴニョと絵の具のように思い通り使いたいなって未だに思います。知れば知るほど未だまだ勉強で、おそらくコード書きは引退しても学び続けるだろうなって思います。

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import PIL.Image
tf.get_logger().setLevel("ERROR")
def preprocess_image(image_path):
    image = PIL.Image.open(image_path).convert("RGB").resize((150,150))
    image = np.array(image) / 255
    image = np.expand_dims(image, 0)

    return image

def test_model(imgurl):
    image_path = imgurl
    model_file_name='human_or.h5'
    labels = ["human","dogs"]
    model = tf.keras.models.load_model(model_file_name, custom_objects={"KerasLayer": hub.KerasLayer})
    predictions = model.predict(preprocess_image(image_path))
    print("検証 %s 人の顔である確率 %3d%%" %(image_path,int(predictions[0][0]*100)) )

test_model("ai_image_test\\test1.jpg")
test_model("ai_image_test\\test2.jpg")
test_model("ai_image_test\\test3.jpg")
test_model("ai_image_test\\test4.jpg")
test_model("ai_image_test\\test5.jpg")
test_model("ai_image_test\\test6.jpg")

https://taoka-toshiaki.com/ML/human_or.zip ←モデル

著者名  @taoka_toshiaki

Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki

タグ

array, convert, custom_objects, expand_dims, get_logger, hub.KerasLayer, image, imgurl, int, labels, model, predictions, print, quot, resize, setLevel, フレームワーク, 力量, 絵の具, 花形,