Tensorflow 在 fit 函数处未开始训练

我按照MNIST 教程进行了学习,并尝试将其调整为使用我自己的数据集。我使用了 Inception 模型,通过 build_image_data.py 将我的图像转换为张量并加载。然后我尝试将它们作为模型的输入,但执行在 model.fit() 函数处就停止了。此后没有任何 CPU 使用率和输出。

以下是相关的代码:

from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport numpy as npimport tensorflow as tffrom tensorflow.contrib import learnfrom tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_libimport image_processingimport datasettf.logging.set_verbosity(tf.logging.INFO)height = 200width = 200def 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"),    }    # 评估模型并打印结果    eval_results = feature_classifier.evaluate(x=vimages.eval(), y=vlabels.eval(), metrics=metrics)    print(eval_results)if __name__ == "__main__":    tf.app.run()

它在最开始时唯一的输出是:

INFO:tensorflow:使用默认配置。 INFO:tensorflow:使用配置: {‘_save_checkpoints_steps’: None, ‘_tf_config’: gpu_options { per_process_gpu_memory_fraction: 1 } , ‘_tf_random_seed’: None, ‘_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’: None, ‘_num_worker_replicas’: 0, ‘_save_checkpoints_secs’: 600, ‘_evaluation_master’: ”, ‘_cluster_spec’: , ‘_environment’: ‘local’, ‘_model_dir’: None}

我的数据集大约有 31 MB + 6 MB 用于输入和验证集。


回答:

你需要启动队列运行器。以下代码更改应该有效:

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)

另一种推荐的方法是使用更新后的 Estimator ‘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"),}

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注