2017-11-14 26 views
0

私はWindows 7を使用しています。 私は公式の再トレーニングの例を使ってmobilenetを訓練しています。Tensorflow:テンソルフローのlabel_image.pyの "input_mean"と "input_std"は何ですか?

python ../tensorflow-master/tensorflow/examples/image_retraining/retrain.py --image_dir test/ --learning_rate=0.0001 --testing_percentage=20 --validation_percentage=20 --train_batch_size=32 --validation_batch_size=-1 --flip_left_right True --random_scale=30 --random_brightness=30 --eval_step_interval=100 --how_many_training_steps=2000 --architecture mobilenet_0.25_128 

をそして私が手に訓練グラフとラベルファイル "output_graph.pb" と "output_labels.txt":

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/image_retraining

私はこのようなコマンドを実行しています。

ここでは、上記のグラフを使って分類を行いたいので、tensorflow githubで提供されているlabel_image.pyを使用しました。

from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 

import argparse 
import sys 

import numpy as np 
import tensorflow as tf 

def load_graph(model_file): 
    graph = tf.Graph() 
    graph_def = tf.GraphDef() 

    with open(model_file, "rb") as f: 
    graph_def.ParseFromString(f.read()) 
    with graph.as_default(): 
    tf.import_graph_def(graph_def) 

    return graph 

def read_tensor_from_image_file(file_name, input_height=299, input_width=299, 
       input_mean=0, input_std=255): 
    input_name = "file_reader" 
    output_name = "normalized" 
    file_reader = tf.read_file(file_name, input_name) 
    if file_name.endswith(".png"): 
    image_reader = tf.image.decode_png(file_reader, channels = 3, 
             name='png_reader') 
    elif file_name.endswith(".gif"): 
    image_reader = tf.squeeze(tf.image.decode_gif(file_reader, 
                name='gif_reader')) 
    elif file_name.endswith(".bmp"): 
    image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader') 
    else: 
    image_reader = tf.image.decode_jpeg(file_reader, channels = 3, 
             name='jpeg_reader') 
    float_caster = tf.cast(image_reader, tf.float32) 
    dims_expander = tf.expand_dims(float_caster, 0); 
    resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width]) 
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std]) 
    sess = tf.Session() 
    result = sess.run(normalized) 

    return result 

def load_labels(label_file): 
    label = [] 
    proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines() 
    for l in proto_as_ascii_lines: 
    label.append(l.rstrip()) 
    return label 

if __name__ == "__main__": 
    file_name = "tensorflow/examples/label_image/data/grace_hopper.jpg" 
    model_file = \ 
    "tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb" 
    label_file = "tensorflow/examples/label_image/data/imagenet_slim_labels.txt" 
    input_height = 299 
    input_width = 299 
    input_mean = 0 
    input_std = 255 
    input_layer = "input" 
    output_layer = "InceptionV3/Predictions/Reshape_1" 

    parser = argparse.ArgumentParser() 
    parser.add_argument("--image", help="image to be processed") 
    parser.add_argument("--graph", help="graph/model to be executed") 
    parser.add_argument("--labels", help="name of file containing labels") 
    parser.add_argument("--input_height", type=int, help="input height") 
    parser.add_argument("--input_width", type=int, help="input width") 
    parser.add_argument("--input_mean", type=int, help="input mean") 
    parser.add_argument("--input_std", type=int, help="input std") 
    parser.add_argument("--input_layer", help="name of input layer") 
    parser.add_argument("--output_layer", help="name of output layer") 
    args = parser.parse_args() 

    if args.graph: 
    model_file = args.graph 
    if args.image: 
    file_name = args.image 
    if args.labels: 
    label_file = args.labels 
    if args.input_height: 
    input_height = args.input_height 
    if args.input_width: 
    input_width = args.input_width 
    if args.input_mean: 
    input_mean = args.input_mean 
    if args.input_std: 
    input_std = args.input_std 
    if args.input_layer: 
    input_layer = args.input_layer 
    if args.output_layer: 
    output_layer = args.output_layer 

    graph = load_graph(model_file) 
    t = read_tensor_from_image_file(file_name, 
            input_height=input_height, 
            input_width=input_width, 
            input_mean=input_mean, 
            input_std=input_std) 

    input_name = "import/" + input_layer 
    output_name = "import/" + output_layer 
    input_operation = graph.get_operation_by_name(input_name); 
    output_operation = graph.get_operation_by_name(output_name); 

    with tf.Session(graph=graph) as sess: 
    results = sess.run(output_operation.outputs[0], 
         {input_operation.outputs[0]: t}) 
    results = np.squeeze(results) 

    top_k = results.argsort()[-5:][::-1] 
    labels = load_labels(label_file) 
    for i in top_k: 
    print(labels[i], results[i]) 

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/label_imageそして、私は、次のコマンドを実行します。

python ../tensorflow-master/tensorflow/examples/label_image/label_image.py --graph=output_graph.pb --labels=output_labels.txt --image=test.jpg --input_layer=input --output_layer=final_result --input_mean=128 --input_std=128 --input_width=128 --input_height=128 

そして画像 "test.jpg" を分類することができます。

しかし、 "input_mean"と "input_std"に異なる値を使用すると、結果が変わります。

"input_mean"と "input_std"は何に使用されますか?そして、これらの2つのパラメータの正しい値を得るにはどうすればよいですか?

+0

使用しているコードを入力してください。 – Lescurel

+0

@Lescurel私が使用した.pyファイルを含む関連リンクを追加しました – Season

+0

リンクが将来変更される可能性があるので、あなたの質問にコードを埋め込む必要があります。 – Lescurel

答えて

0

これらの2つの変数は、データセットの正規化に使用されます。その結果、データセットの改善が観察されます。
input_meanは、データセットのチャネルごとの平均であり、input_stdは、関連する標準偏差です。あなたのネットワークに渡すデータセットをコントロールするとき、平均と標準を計算できるはずです。

正規化は、2つのことを意味します

  • ()我々の場合には0を中心に(センタリング)(スケーリング)の点を中心にデータをバランシング
  • 同じ規模でのデータを置くことを

なぜそれを行うのかについては、このCrossvalidated答えで読むことができます:https://stats.stackexchange.com/a/220970

"Suデータセット平均を取ることは、データを「センタリング」するのに役立つ。 さらに、各フィーチャー値 をzスコアに正規化する場合は、その フィーチャーまたはピクセルのsttdevで除算したいと思うのが理想的です。そのアクティベーション を起こすために(偏見)これらの初期の入力に加えること

我々はそれらのものの両方を行う理由は、私たちのネットワークを訓練 の過程で、我々は(重み)を掛けることになるだろうしているためと 我々はその後、モデルを訓練するためにグラジエントをバックプロパゲーションする。私たちの勾配が制御不能に行かない(と私たちは一つだけ グローバル学習レート乗算器を必要とすること)

私たちは、同様の範囲を持っている各機能のために、この過程でみたい

そう 。「

EDIT:3つのピクセルの本当にシンプルなイメージがあなたのイメージを想像し 例をされて与えること:

img = [[[1,2,3],[4,5,6],[7,8,9]]] 

あなたは簡単にチャンネル

mean = [4,5,6] 

と同じように簡単当たりの平均を計算することができますチャネルごとの標準も:

std = [2.45,2.45,2.45] 

テンソルフローは要素ごとの減算を行い、次に要素ごとの除算を行うため、イメージの各チャネルを個別に正規化します。

コードでは、tf.image関数に準拠するディメンションが追加されているようです。最初の次元はバッチ1であるため、配列に0を付加してください。[0,4,5,6]

+0

つまり、私はこのケースのすべてのチャンネルの画像の色の平均と標準を計算する必要があるのですか? – Season

+0

チャネルごとの平均オプションを使用すると、最良の結果が得られます。 intの代わりに、平均と標準の配列を渡します。私は例を提供するために私の答えを更新します。 – Lescurel

関連する問題