【发布时间】:2018-11-14 01:24:58
【问题描述】:
我正在构建一个简单的图像分割网络,基于 TensorFlow 中的编码器/解码器架构,目前该模型似乎可以运行,但它没有更新权重。输入是我自己的数据集:图像转换为形状为 [-1,200,200,1] 的 numpy 数组,带有形状为 [-1,40000,3] 的像素-bu-像素掩码标签。
我的模型函数的代码在这里
def model(features, labels, mode):
inpt = tf.reshape(features["x"], [-1,200,200,1])
#encoder
x = tf.layers.conv2d(
inputs = inpt,
filters = 16,
kernel_size = [2,2],
padding = 'same',
activation = tf.nn.relu)
x = tf.layers.max_pooling2d(inputs = x, pool_size = [2,2], strides = 2)
x = tf.layers.conv2d(
inputs = x,
filters = 32,
kernel_size = [2,2],
padding = 'same',
activation = tf.nn.relu)
x = tf.layers.max_pooling2d(inputs = x, pool_size = [2,2], strides = 2)
x = tf.layers.conv2d(
inputs = x,
filters = 64,
kernel_size = [2,2],
padding = 'same',
activation = tf.nn.relu)
x = tf.layers.max_pooling2d(inputs = x, pool_size = [2,2], strides = 2)
#decoder
x = tf.keras.layers.UpSampling2D(size = (2,2))(x)
x = tf.layers.conv2d(
inputs = x,
filters = 64,
kernel_size = [2,2],
padding = 'same',
activation = tf.nn.relu)
x = tf.keras.layers.UpSampling2D(size = (2,2))(x)
x = tf.layers.conv2d(
inputs = x,
filters = 32,
kernel_size = [2,2],
padding = 'same',
activation = tf.nn.relu)
x = tf.keras.layers.UpSampling2D(size = (2,2))(x)
x = tf.layers.conv2d(
inputs = x,
filters = 16,
kernel_size = [2,2],
padding = 'same',
activation = tf.nn.relu)
x = tf.layers.conv2d(
inputs = x,
filters = 3,
kernel_size = [1,1],
padding = 'valid',
activation = tf.nn.relu)
logits = tf.reshape(x, [-1,3])
predictions = {
"classes": tf.argmax(input = logits, axis = 1),
"probabilities": tf.nn.softmax(logits, name = "softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode = mode, predictions = predictions)
labels = tf.reshape(labels, [-1,3])
loss = tf.losses.softmax_cross_entropy(onehot_labels = labels, logits = logits)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.0001)
train_op = optimizer.minimize(
loss = loss,
global_step = tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode = mode, loss = loss, train_op = train_op)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(
labels = tf.argmax(labels, axis = 1), predictions = predictions["classes"])
}
return tf.estimator.EstimatorSpec(
mode = mode, loss = loss, eval_metric_ops = eval_metric_ops)
然后我按如下方式调用模型:
classifier = tf.estimator.Estimator(model_fn = model, model_dir = '.')
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors = tensors_to_log, every_n_iter = 50)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x = {"x": train},
y = train_label,
batch_size = 1,
num_epochs = 5,
shuffle = True)
classifier.train(
input_fn = train_input_fn,
steps = None)
模型正在运行,但准确度得分保持在 35% 左右,无法训练。如果有人有任何建议,他们将不胜感激。
【问题讨论】:
-
我会使用
tf.layers.conv2d_transpose而不是tf.keras.layers.UpSampling2D,以免混合 Keras 和 Tensorflow 层。 -
我的理解是它们的表现不同。我想使用最近邻上采样,我相信 Keras 会使用它,因为转置卷积层会在上采样图像中添加伪影,从而混淆输出。
标签: python tensorflow machine-learning computer-vision