我已经按照evalutating MNIST教程进行了调整,并希望对其进行调整以使用我自己的数据集。使用inception model我已使用build_image_data.py将我的图像转换为张量并加载它们。现在我不能直接将这些数据输入到.fit函数中,如下所示:
输入不能是张量。请提供input_fn。
以下是相关代码:
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()
我将代码更改为注释中的更新,现在它将挂起而在feature_classifier.fit()处没有输出。它在一开始就给出的唯一输出是:
INFO:tensorflow:使用默认配置。 INFO:tensorflow:使用config:{'_ save_checkpoints_steps':无,'_ tf_config':gpu_options { per_process_gpu_memory_fraction:1 } ,'_ tf_random_seed':无,'_ kep_checkpoint_max':5,'_ num_ps_replicas':0,'_ master':'','_ is_chief':是的,'_ 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_pec':','_ environment':'local','_ model_dir':无}
修改: 将代码示例更改为程序的完整源代码。我现在使用Google初始模型中的数据集和image_processing,而不是使用自定义函数将数据集转换为图像。没有错误,但是当调用feature_classifier.fit()时程序就会冻结。没有CPU使用,没有任何输出。