20190310のTensorFlowに関する記事は3件です。

tfliteファイルの入力、出力フォーマットを確認

tflite形式をMobileアプリ等に組み込む際にInput、Outputのフォーマットを確認する方法

tfliteFormatCheck.py
import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path="my_model.tflite")
interpreter.allocate_tensors()

# Print input shape and type
print(interpreter.get_input_details()[0]['shape'])
print(interpreter.get_input_details()[0]['dtype'])

# Print output shape and type
print(interpreter.get_output_details()[0]['shape'])
print(interpreter.get_output_details()[0]['dtype'])

参考文献
https://firebase.google.com/docs/ml-kit/ios/use-custom-models?hl=ja

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

tfliteファイルの入力、出力フォーマット確認方法

tflite形式をMobileアプリ等に組み込む際にInput、Outputのフォーマットを確認する方法

tfliteFormatCheck.py
import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path="my_model.tflite")
interpreter.allocate_tensors()

# Print input shape and type
print(interpreter.get_input_details()[0]['shape'])
print(interpreter.get_input_details()[0]['dtype'])

# Print output shape and type
print(interpreter.get_output_details()[0]['shape'])
print(interpreter.get_output_details()[0]['dtype'])

参考文献
https://firebase.google.com/docs/ml-kit/ios/use-custom-models?hl=ja

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

TensorboardのEmbeddingによる高次元データの可視化

Tensorboard: Embedding

TensorbordのEmbeddingを利用することで高次元データを2/3次元での可視化が可能です。
embedding.png
公式ドキュメント等詳しく書かれていますが、備忘録を兼ねてコードを紹介したいと思います。

TensorFlow Embedding: https://www.tensorflow.org/guide/embedding
Embedding デモ: https://projector.tensorflow.org/

次元削減

次元の圧縮方法としてはPCAまたはt-SNEが利用可能です。Tensorboard実行時にはPCAによる削減がデフォルトで行われます。
注意点として、t-SNEによる次元削減では入力が50次元を超える場合、50次元まで落とした後にt-SNEによる削減を行っている(githubのissueで見かけたがリンクを忘れてしまった)。このため、sklearn等によるt-SNEの結果と一致しないようです。

Python用コード

手順としては
1. projectorの設定ファイルを書き出す
2. checkpointファイルを書き出す
これだけです。

環境

  • Python: 3.6
  • tensorflow: 1.10.0
  • pandas: 0.24.1

コード

embedding.py
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector
from pathlib import Path
import pandas as pd

def embedding(vectors, save_dir, labels=None):
    with tf.Session() as sess:
        save_dir = Path(save_dir)
        if not save_dir.exists():
            save_dir.mkdir(parents=True)

        vectors_tensor = tf.Variable(vectors)
        sess.run(tf.global_variables_initializer())

        save = tf.train.Saver()
        save.save(sess, save_dir.joinpath('embedding.ckpt').as_posix())

        config = projector.ProjectorConfig()

        embedding = config.embeddings.add()
        embedding.tensor_name = vectors_tensor.name
        if labels is not None:
            embedding.metadata_path = 'label.tsv'
            label_df = pd.DataFrame({'Label': labels})
            label_df.to_csv(save_dir.joinpath('label.tsv'), sep='\t', index=False, header=False)

        summary_writer = tf.summary.FileWriter(save_dir.as_posix())
        projector.visualize_embeddings(summary_writer, config)

可視化したいサイズ(データ数, 次元数)のベクトルデータと保存先を与えることで指定ディレクトリにcheckpointファイルを書き出してくれる。データ数に一致するラベルを与えることでデータにラベルを付加することも可能です。
なお、10,000点を超えるデータは実行時に10,000点がランダムに選択されて可視化されます。

実行

実行はいつも通りtensorboardを実行後、ブラウザでアクセスするだけです。

$ tensorboard --logdir path/to/logdir
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む