2017-03-23 9 views
0

InceptionV1の花のデータセットを訓練するのに、次のコードを使用しています。このコードは、HereInceptionV4とV2がInceptionV1よりも「花データセット」の精度が低い

import os 

from datasets import flowers 
from nets import inception 
from preprocessing import inception_preprocessing 

slim = tf.contrib.slim 
image_size = inception.inception_v1.default_image_size 


def get_init_fn(): 
    """Returns a function run by the chief worker to warm-start the training.""" 
    checkpoint_exclude_scopes=["InceptionV1/Logits", "InceptionV1/AuxLogits"] 

exclusions = [scope.strip() for scope in checkpoint_exclude_scopes] 

variables_to_restore = [] 
for var in slim.get_model_variables(): 
    excluded = False 
    for exclusion in exclusions: 
     if var.op.name.startswith(exclusion): 
      excluded = True 
      break 
    if not excluded: 
     variables_to_restore.append(var) 

return slim.assign_from_checkpoint_fn(
    os.path.join(checkpoints_dir, 'inception_v1.ckpt'), 
    variables_to_restore) 


train_dir = '/tmp/inception_finetuned/' 

with tf.Graph().as_default(): 
    tf.logging.set_verbosity(tf.logging.INFO) 

dataset = flowers.get_split('train', flowers_data_dir) 
images, _, labels = load_batch(dataset, height=image_size, width=image_size) 

# Create the model, use the default arg scope to configure the batch norm parameters. 
with slim.arg_scope(inception.inception_v1_arg_scope()): 
    logits, _ = inception.inception_v1(images, num_classes=dataset.num_classes, is_training=True) 

# Specify the loss function: 
one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) 
slim.losses.softmax_cross_entropy(logits, one_hot_labels) 
total_loss = slim.losses.get_total_loss() 

# Create some summaries to visualize the training process: 
tf.scalar_summary('losses/Total Loss', total_loss) 

# Specify the optimizer and create the train op: 
optimizer = tf.train.AdamOptimizer(learning_rate=0.01) 
train_op = slim.learning.create_train_op(total_loss, optimizer) 

# Run the training: 
final_loss = slim.learning.train(
    train_op, 
    logdir=train_dir, 
    init_fn=get_init_fn(), 
    number_of_steps=2) 



print('Finished training. Last batch loss %f' % final_loss) 

が設けられている。私は、次のコードを使用してモデルを評価し、チェックポイントやトレーニングディレクトリを設定するから58.34パーセント別に

import numpy as np 
import tensorflow as tf 
from datasets import flowers 
from nets import inception 

slim = tf.contrib.slim 

image_size = inception.inception_v1.default_image_size 
batch_size = 3 

with tf.Graph().as_default(): 
    tf.logging.set_verbosity(tf.logging.INFO) 

    dataset = flowers.get_split('train', flowers_data_dir) 
    images, images_raw, labels = load_batch(dataset, height=image_size, width=image_size) 

# Create the model, use the default arg scope to configure the batch norm parameters. 
with slim.arg_scope(inception.inception_v1_arg_scope()): 
    logits, _ = inception.inception_v1(images, num_classes=dataset.num_classes, is_training=True) 
    predictions = tf.argmax(logits, 1) 


checkpoint_path = tf.train.latest_checkpoint(train_dir) 
init_fn = slim.assign_from_checkpoint_fn(
    checkpoint_path, 
    slim.get_variables_to_restore()) 

names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 
    'eval/Accuracy': slim.metrics.streaming_accuracy(predictions, labels), 
    'eval/[email protected]': slim.metrics.streaming_recall_at_k(logits, labels, 5), 
}) 

# Define the streaming summaries to write: 
for metric_name, metric_value in names_to_values.items(): 
    tf.summary.scalar(metric_name, metric_value) 

print('Running evaluation Loop...') 
# Load the most recent checkpoint of variables saved 
checkpoint_path = tf.train.latest_checkpoint(train_dir) 
# Evaluates the model at the given checkpoint path 
metric_values = slim.evaluation.evaluate_once(
    master='', 
    checkpoint_path=checkpoint_path, 
    logdir=train_dir, 
    num_evals=100, 
    eval_op=list(names_to_updates.values()), 
    final_op=list(names_to_values.values()), 
    summary_op=tf.summary.merge_all()) 

names_to_values = dict(zip(names_to_values.keys(), metric_values)) 
for name in names_to_values: 
    print('%s: %f' % (name, names_to_values[name])) 

の精度を持って、私はのみとコードに「V1」を置き換えます"V2"と "V4"とモデルを訓練しました。

第1に、すべての100回の反復で、「V2」と「V4」の両方で4%前後のトレーニング損失が一定しています。 第2に、「V2」と「V4」の評価精度が約25%になる

私はTFには新しいですから、間違っているのは間違っていますか?

+0

フラワー写真のデータセットにV3の事前習得開始V3を使ったトランスファーラーニングをやったことがあり、ピーク精度は約94%に達することがあります。 –

+1

@ Jie.Zhouあなたのコードを共有していただけますか?なぜ私は何が起こっているのか、私は何かが欠けているに違いない – Asad

答えて

1

Inception V3のようなかなり大きな畳み込みネットワークを微調整するときに間違っていることがたくさんあります。あなたは上記の投稿したトレーニング・コードがtf.GraphにロードされてからInceptionV1/LogitsInceptionV1/AuxLogitsを除く

  • :ここでは、あなたのモデルを改善するために見ることができるいくつかのポインタです。これらのテンソルは、畳み込みベースの上に完全に接続されたレイヤーです。本質的に、これはあなた自身のInceptionV1/LogitsInceptionV1/AuxLogitsを訓練することを可能にします。しかし、このコードは畳み込みベースを「フリーズ」せず、畳み込みフィルタが訓練可能であることを意味します。これは、ランダムに初期化された完全に接続されたレイヤーから流れる大きな勾配が畳み込みベースで学習されたウェイトを壊す可能性があるため、悪い考えです。これは、より大きなネットワークでより壊滅的な影響をもたらし、V2とV4がV1よりも悪化した理由を説明するかもしれません。微調整ネットワークhereについて詳しく読むことができます。
  • 0.01の学習率は、ネットワークの微調整のために非常に高いようです。通常、事前に訓練されたモデルは、ラインやエッジの検出などの低レベルのフィルタを学習していたため、ウェイトを大幅に変更する必要はありません。学習率は< = 0.001で十分です。
  • しかし、説明したことから、100回の反復で0.04に固定されているため、モデルは収束していないように見えます。これは学習率を高めることを示唆しています。私はまだこれについて確信しています。おそらく、コードは単なる例であり、他のモデルに適応させることは想定されていません。

Tensorflowには、異なるモデルの微調整に関する詳細が記載されていますhere。また、ユーザーフレンドリーで簡潔なTensorflowのラッパーであるslimも使用しています。おそらくあなたはそれを試すことができます。がんばろう。

関連する問題