Beispiel #1
0
def model_fn(inputs, mode, **kwargs):
    # In train or eval, id_or_labels represents labels. In predict, id_or_labels represents id.
    images, id_or_labels, angles = inputs
    # Reshape angles from [batch_size] to [batch_size, 1]
    angles = tf.expand_dims(angles, 1)
    # Apply your version of model
    logits = model_v1(images, angles, mode)

    if mode == mox.ModeKeys.PREDICT:
        logits = tf.nn.softmax(logits)
        # clip logits to get lower loss value.
        logits = tf.clip_by_value(logits,
                                  clip_value_min=0.05,
                                  clip_value_max=0.95)
        model_spec = mox.ModelSpec(output_info={
            'id': id_or_labels,
            'logits': logits
        })
    elif mode == mox.ModeKeys.EXPORT:
        predictions = tf.nn.softmax(logits)
        export_spec = mox.ExportSpec(inputs_dict={
            'images': images,
            'angles': angles
        },
                                     outputs_dict={'predictions': predictions},
                                     version='model')
        model_spec = mox.ModelSpec(export_spec=export_spec)
    else:
        labels_one_hot = slim.one_hot_encoding(id_or_labels, 2)
        loss = tf.losses.softmax_cross_entropy(logits=logits,
                                               onehot_labels=labels_one_hot,
                                               label_smoothing=0.0,
                                               weights=1.0)
        model_spec = mox.ModelSpec(loss=loss, log_info={'loss': loss})
    return model_spec
Beispiel #2
0
    def model_fn(inputs, mode, **kwargs):
        images, labels = inputs

        mox_model_fn = mox.get_model_fn(name=flags.model_name,
                                        run_mode=mode,
                                        num_classes=data_meta.num_classes,
                                        weight_decay=0.00004,
                                        data_format='NCHW',
                                        batch_norm_fused=True)

        images = tf.cast(images, tf.float16)
        with mox.var_scope(force_dtype=tf.float32):
            logits, _ = mox_model_fn(images)

        labels_one_hot = slim.one_hot_encoding(labels, data_meta.num_classes)
        loss = tf.losses.softmax_cross_entropy(labels_one_hot, logits=logits)

        regularization_losses = mox.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        regularization_loss = tf.add_n(regularization_losses)
        loss = loss + regularization_loss

        logits_fp32 = tf.cast(logits, tf.float32)
        accuracy = tf.reduce_mean(
            tf.cast(tf.nn.in_top_k(logits_fp32, labels, 1), tf.float32))

        export_spec = mox.ExportSpec(inputs_dict={'images': images},
                                     outputs_dict={'logits': logits_fp32})

        return mox.ModelSpec(loss=loss,
                             log_info={
                                 'loss': loss,
                                 'accuracy': accuracy
                             },
                             export_spec=export_spec)
Beispiel #3
0
 def model_fn(inputs, run_mode, **kwargs):
   x, y_ = inputs
   W = tf.get_variable(name='W', initializer=tf.zeros([784, 10]))
   b = tf.get_variable(name='b', initializer=tf.zeros([10]))
   y = tf.matmul(x, W) + b
   cross_entropy = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
   predictions = tf.argmax(y, 1)
   correct_predictions = tf.equal(predictions, tf.argmax(y_, 1))
   accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
   export_spec = mox.ExportSpec(inputs_dict={'images': x}, outputs_dict={'predictions': predictions}, version='model')
   return mox.ModelSpec(loss=cross_entropy, log_info={'loss': cross_entropy, 'accuracy': accuracy},
                        export_spec=export_spec)
Beispiel #4
0
    def model_fn(inputs, run_mode, **kwargs):
        x, y_ = inputs

        y = tf.keras.layers.Dense(128, activation='relu')(x)
        y = tf.keras.layers.Dense(10)(y)
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        export_spec = mox.ExportSpec(inputs_dict={'images': x},
                                     outputs_dict={'logits': y},
                                     version='model')
        return mox.ModelSpec(loss=cross_entropy,
                             log_info={
                                 'loss': cross_entropy,
                                 'accuracy': accuracy
                             },
                             export_spec=export_spec)