我使用估算器API来训练对形状图像进行分类的CNN。
我能够使用从tfrecord文件训练的自定义input_fn()成功训练CNN。然后,我能够使用model.predict(predict_input_fn)进行预测。几个时期之后的准确度> 80%,然后当我在某些测试数据上使用model.predict()时。我也得到了> 80%。所以这似乎工作正常。
我想保存模型,然后加载模型并预测使用它,因为这就是我的目标。所以基本上推断。当我这样做并预测我的测试数据时,我得到了糟糕的结果。我从input_fn()中删除了所有预处理并重新训练。因此,当我预测时,我实际上是在传递原始数据。问题依然存在。我想知道为什么会这样或者我做错了什么。感谢您的任何见解。
我将链接相关代码 我的model_fn
def model_fn(features, labels, mode, params):
x = features['image_raw']
net = tf.reshape(x, [-1, 824, 463, num_channels])
net = tf.layers.conv2d(inputs=net, name='layer_conv1',
filters=32, kernel_size=11, strides=4,
padding='same', activation=tf.nn.relu)
net = tf.layers.conv2d(inputs=net, name='layer_conv2',
filters=32, kernel_size=11, strides=4,
padding='same', activation=tf.nn.relu)
net = tf.layers.conv2d(inputs=net, name='layer_conv3',
filters=32, kernel_size=5, strides=2,
padding='same', activation=tf.nn.relu)
net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2,padding='SAME')
net = tf.layers.conv2d(inputs=net, name='layer_conv4',
filters=32, kernel_size=3,
padding='same', activation=tf.nn.relu)
net = tf.contrib.layers.flatten(net)
net = tf.layers.dense(inputs=net, name='layer_fc1',
units=256, activation=tf.nn.relu)
net = tf.nn.dropout(net, 0.5)
net = tf.layers.dense(inputs=net, name='layer_fc_2',
units=num_classes)
logits = net
y_pred = tf.nn.softmax(logits=logits)
y_pred_cls = tf.argmax(y_pred, axis=1)
if mode == tf.estimator.ModeKeys.PREDICT:
export_outputs = {'classes': tf.estimator.export.PredictOutput({"classes": y_pred_cls})}
spec = tf.estimator.EstimatorSpec(mode=mode,predictions=y_pred_cls,export_outputs=export_outputs)
else:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels,logits=logits)
loss = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001,beta1=0.9,beta2=0.999,epsilon=1e-8,name="Adam")
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
metrics = {"accuracy": tf.metrics.accuracy(labels, y_pred_cls)}
# Wrap all of this in an EstimatorSpec.
spec = tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,
eval_metric_ops=metrics
)
return spec
我的服务功能:
def serving_input_receiver_fn():
inputs = {"image_raw": tf.placeholder(shape=[824, 463], dtype=tf.float32)}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
我如何保存训练有素的模型:
export_dir = model.export_savedmodel(
export_dir_base="./saved_model/",
serving_input_receiver_fn=serving_input_receiver_fn,
as_text=True)
我如何从保存的模型中预测:
from tensorflow.contrib import predictor
predict_fn = predictor.from_saved_model('./saved_model/1518601120/')
a = np.ones(shape=(824,463),dtype=np.float32)
image = Image.open((os.path.join(prediction_dir,subdir,file)))
image = np.array(image)
image=image.swapaxes(0,1)
a[:,:]=image[:,:,0] #The input is an RGBa PNG. only 1 channel is populated #with data from our shape.
prediction = predict_fn({"image_raw": a})
predictions.append((prediction['classes'][0]))
答案 0 :(得分:1)
事实证明我将预测函数传递给高度宽度交换的张量。这很好,因为我的占位符是相同的形状。但是一旦张量进入我的model_fn(),它就会被重新塑造成一个大小宽度的高度。导致图像被压扁"在通过模型之前。这导致我遇到的预测结果不好。