2017-06-19 147 views
0

我遵循評估MNIST tutorial並想調整它以使用我自己的數據集。使用初始模型,我使用build_image_data.py將圖像轉換爲張量並加載它們。然後我嘗試使用它們作爲模型的輸入,但執行直到model.fit()函數停止。之後沒有任何CPU使用率和輸出。Tensorflow沒有開始適合的訓練

下面是相關代碼:

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

import numpy as np 
import tensorflow as tf 

from tensorflow.contrib import learn 
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib 

import image_processing 
import dataset 

tf.logging.set_verbosity(tf.logging.INFO) 

height = 200 
width = 200 

def cnn_model_fn(features, labels, mode): 
    input_layer = tf.reshape(features, [-1, width, height, 1]) 

    con 
    v1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) 
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) 
    conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) 
    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) 
    pool2_flat = tf.reshape(pool2, [-1, (width/4) * (width/4) * 64]) 
    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) 
    dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == learn.ModeKeys.TRAIN) 
    logits = tf.layers.dense(inputs=dropout, units=2) 

    loss = None 
    train_op = None 

    if mode != learn.ModeKeys.INFER: 
     onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=2) 
     loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) 

    if mode == learn.ModeKeys.TRAIN: 
     train_op = tf.contrib.layers.optimize_loss(loss=loss, global_step=tf.contrib.framework.get_global_step(), learning_rate=0.001, optimizer="SGD") 

    predictions = { 
      "classes": tf.argmax(input=logits, axis=1), 
      "probabilities": tf.nn.softmax(logits, name="softmax_tensor") 
    } 

    return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions, loss=loss, train_op=train_op) 

def main(unused_argv): 
    training_data = dataset.Dataset("train-00000-of-00001", "train") 
    validation_data = dataset.Dataset("validation-00000-of-00001", "validation") 
    images, labels = image_processing.inputs(training_data) 
    vimages, vlabels = image_processing.inputs(validation_data) 

    sess = tf.InteractiveSession() 
    feature_classifier = learn.SKCompat(learn.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/feature_model")) 
    tensors_to_log = {"probabilities": "softmax_tensor"} 
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10) 
    feature_classifier.fit(x=images.eval(), y=labels.eval(), batch_size=100, steps=200000, monitors=[logging_hook]) 
    metrics = { 
      "accuracy": 
        learn.MetricSpec(metric_fn=tf.metrics.accuracy, prediction_key="classes"), 
    } 
    # Evaluate the model and print results 
    eval_results = feature_classifier.evaluate(x=vimages.eval(), y=vlabels.eval(), metrics=metrics) 
    print(eval_results) 

if __name__ == "__main__": 
    tf.app.run() 

它給劈頭唯一的輸出是:

信息:tensorflow:使用默認配置。 INFO:tensorflow:使用配置:{ '_save_checkpoints_steps':無, '_tf_config':gpu_options { per_process_gpu_memory_fraction:1 } '_tf_random_seed':無, '_keep_checkpoint_max':5 '_num_ps_replicas':0, '_master' :'','_is_chief':True,'_keep_checkpoint_every_n_hours':10000,'_task_id':0,'_save_summary_steps':100,'_task_type':無,'_num_worker_replicas':0,'_save_checkpoints_secs':600,'_evaluation_master': '', '_cluster_spec': '_environment': '本地', '_model_dir':無}

我的數據集是約31 MB + 6 MB用於輸入和驗證集。

回答

1

您需要啓動隊列跑步者。下面的代碼更改應該工作:

sess = tf.InteractiveSession() 

sess.run(tf.global_variables_initializer()) 
coordinator = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(sess=sess,coord=coordinator) 

feature_classifier = learn.SKCompat(learn.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/feature_model")) 
... 

print(eval_results) 
coordinator.request_stop() 
coordinator.join(threads) 

另一個推薦的方法是進行以下更改使用更新的估算「input_fn」的方法:

sess = tf.InteractiveSession() 

feature_classifier = learn.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/feature_model") 
tensors_to_log = {"probabilities": "softmax_tensor"} 
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10) 
feature_classifier.fit(input_fn=lambda:image_processing.inputs(training_data), train=True), steps=200000, monitors=[logging_hook]) 
metrics = { 
     "accuracy": 
       learn.MetricSpec(metric_fn=tf.metrics.accuracy, prediction_key="classes"), 
} 
+0

嗨,取出後「火車= TRUE)」模型開始學習我的CPU。現在我只需要修復它在我的GPU上的內存不足。謝謝。 – Eejin

+0

要修復GPU內存問題,請使image_processing模塊在CPU上運行。檢查tensoflow的最佳實踐性能指南:https://www.tensorflow.org/performance/performance_guide –

+0

我從初始模型中使用它:https://github.com/tensorflow/models/blob/master/inception/inception /image_processing.py這似乎做到了這一點。但是如果我將神經元的數量從1024減少到512,它就會訓練我的2GB gpu。 – Eejin