Esempio n. 1
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
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
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
Esempio n. 4
0
    def model(self, 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

        images = tf.layers.dropout(images, params.input_drop_rate)

        features = ops.conv_layers(
            images,
            filters=[96, 96, 192, 192, 192, 192, params.num_classes],
            kernels=[3, 3, 3, 3, 3, 1, 1],
            pool_sizes=[1, 3, 1, 3, 1, 1, 1],
            pool_strides=[1, 2, 1, 2, 1, 1, 1],
            drop_rates=[0, 0, drop_rate, 0, drop_rate, 0, 0],
            batch_norm=True,
            training=training,
            pool_activation=tf.nn.relu,
            linear_top_layer=True,
            weight_decay=params.weight_decay)

        logits = tf.reduce_mean(features, [1, 2])

        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(ops.top_k_error(labels, logits, 1)),
        }

        return {"predictions": predictions}, loss, eval_metrics