コード例 #1
0
def model(features, labels, mode, params):
  """CNN classifier model."""
  images = features["image"]
  labels = labels["label"]

  training = mode == tf.estimator.ModeKeys.TRAIN

  features = ops.conv_layers(
    images, [16], [3], linear_top_layer=True)

  features = ops.resnet_blocks(
    features, [16, 32, 64], [1, 2, 2], 5, training)

  features = ops.batch_normalization(features, training=training)
  features = tf.nn.relu(features)

  features = tf.reduce_mean(features, axis=[1, 2])
  logits = ops.dense_layers(
    features, [params.num_classes], linear_top_layer=False)

  predictions = tf.argmax(logits, axis=-1)

  loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

  tf.summary.image("images", images)

  eval_metrics = {
    "accuracy": tf.metrics.accuracy(labels, predictions),
    "top_1_error": tf.metrics.mean(metrics.top_k_error(labels, logits, 1)),
  }

  return {"predictions": predictions}, loss, eval_metrics
コード例 #2
0
def model(features, labels, mode, params):
    """CNN classifier model."""
    images = features["image"]
    labels = labels["label"]

    training = mode == tf.estimator.ModeKeys.TRAIN
    drop_rate = params.drop_rate if training else 0.0

    features = ops.conv_layers(images,
                               filters=[64, 128, 256],
                               kernels=[3, 3, 3],
                               pool_sizes=[2, 2, 2])

    features = tf.contrib.layers.flatten(features)

    logits = ops.dense_layers(features, [512, params.num_classes],
                              drop_rates=drop_rate,
                              linear_top_layer=True)

    predictions = tf.argmax(logits, axis=-1)

    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

    tf.summary.image("images", images)

    eval_metrics = {
        "accuracy": tf.metrics.accuracy(labels, predictions),
        "top_1_error": tf.metrics.mean(metrics.top_k_error(labels, logits, 1)),
    }

    tf.add_to_collection("batch_logging", tf.identity(labels, name="labels"))
    tf.add_to_collection("batch_logging",
                         tf.identity(predictions, name="predictions"))

    return {"predictions": predictions}, loss, eval_metrics
コード例 #3
0
def model_fn(features, labels, mode, params):
  """CNN classifier model."""
  images = features['image']
  labels = labels['label']

  drop_rate = params.drop_rate if mode == tf.estimator.ModeKeys.TRAIN else 0.0

  features = ops.conv_layers(
    images,
    filters=[32, 64, 128],
    kernels=[3, 3, 3],
    pools=[2, 2, 2])

  features = tf.contrib.layers.flatten(features)

  logits = ops.dense_layers(
    features, [512, params.num_classes],
    drop_rate=drop_rate,
    linear_top_layer=True)

  predictions = tf.argmax(logits, axis=1)

  loss = tf.losses.sparse_softmax_cross_entropy(
    labels=labels, logits=logits)

  summary.labeled_image('images', images, predictions)

  return {'predictions': predictions}, loss