def model_fn(inputs, run_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, run_mode)
     if run_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
         })
     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
Esempio n. 2
0
    def model_fn(inputs, mode):
        images, labels = inputs

        # 获取一个resnet50的模型,输入images,输入logits和end_points,这里不关心end_points,仅取logits
        logits, _ = mox.get_model_fn(name='resnet_v1_50',
                                     run_mode=mode,
                                     num_classes=data_meta.num_classes,
                                     weight_decay=0.00004)(images)

        # 计算交叉熵损失值
        labels_one_hot = slim.one_hot_encoding(labels, data_meta.num_classes)
        loss = tf.losses.softmax_cross_entropy(logits=logits,
                                               onehot_labels=labels_one_hot)

        # 获取正则项损失值,并加到loss上,这里必须要用mox.get_collection代替tf.get_collection
        regularization_losses = mox.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        regularization_loss = tf.add_n(regularization_losses)
        loss = loss + regularization_loss

        # 计算分类正确率
        accuracy = tf.reduce_mean(
            tf.cast(tf.nn.in_top_k(logits, labels, 1), tf.float32))

        # 返回MoXing-TensorFlow用于定义模型的类ModelSpec
        return mox.ModelSpec(loss=loss,
                             log_info={
                                 'loss': loss,
                                 'accuracy': accuracy
                             })
Esempio n. 3
0
def model_fn(inputs, run_mode, **kwargs):
    x, y_ = inputs
    x_image = tf.reshape(x, [-1, 28, 28, 1])  # 转换输入数据shape,以便于用于网络中
    W_conv1 = weight_variable('w1', [5, 5, 1, 32])
    b_conv1 = bias_variable('b1', [32])
    # dcp export only support BiasAdd
    # h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_conv1 = tf.nn.relu(tf.nn.bias_add(conv2d(x_image, W_conv1),
                                        b_conv1))  # 第一个卷积层
    h_pool1 = max_pool(h_conv1)  # 第一个池化层

    W_conv2 = weight_variable('w2', [5, 5, 32, 64])
    b_conv2 = bias_variable('b2', [64])
    h_conv2 = tf.nn.relu(tf.nn.bias_add(conv2d(h_pool1, W_conv2),
                                        b_conv2))  # 第二个卷积层
    h_pool2 = max_pool(h_conv2)  # 第二个池化层
    W_fc1 = weight_variable('w3', [7 * 7 * 64, 1024])
    b_fc1 = bias_variable('b3', [1024])
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])  # reshape成向量
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)  # 第一个全连接层
    W_fc2 = weight_variable('w4', [1024, 10])
    b_fc2 = bias_variable('b4', [10])
    y = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)  # softmax层
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
    from efficient_ai.config import CompressorSpec
    compressor_spec = CompressorSpec(logits=y)
    return mox.ModelSpec(loss=cross_entropy,
                         compressor_spec=compressor_spec,
                         log_info={'loss': cross_entropy})
Esempio n. 4
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)
Esempio n. 5
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
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    return mox.ModelSpec(log_info={'accuracy': accuracy})
Esempio n. 6
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))
    return mox.ModelSpec(loss=cross_entropy, log_info={'loss': cross_entropy})
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
0
    def model_fn(inputs, mode, **kwargs):
        if not flags.gpu_synthetic:
            if flags.split_to_device:
                images, labels = inputs.get_input(0)
            else:
                images, labels = inputs
        else:
            import numpy as np
            images = tf.constant(
                np.random.randint(low=0,
                                  high=255,
                                  size=[flags.batch_size, 128, 128, 3],
                                  dtype=np.uint8))
            labels = tf.constant(
                np.random.randint(low=0,
                                  high=999,
                                  size=[flags.batch_size],
                                  dtype=np.int64))

        if flags.fp16:
            images = tf.cast(images, tf.float16)

        def preprocess_fn(images, run_mode, *args):
            images = images / 255.0
            channels = tf.split(axis=3, num_or_size_splits=3, value=images)
            for i in range(3):
                channels[i] = (channels[i] - mean[i]) / std[i]
            images = tf.concat(axis=3, values=channels)
            if flags.data_format == 'NCHW':
                images = tf.transpose(images, perm=(0, 3, 1, 2))
            return images

        model_kwargs = {}
        if flags.model_name == 'resnet_v1_50_8k':
            if flags.official_stride:
                model_kwargs['official'] = True
            if flags.fastai_initializer:
                model_kwargs['weights_initializer_params'] = {
                    'factor': 2.0 / 1.3,
                    'mode': 'FAN_OUT'
                }

        mox_model_fn = mox.get_model_fn(name=flags.model_name,
                                        run_mode=mode,
                                        num_classes=1000,
                                        preprocess_fn=preprocess_fn,
                                        weight_decay=flags.weight_decay,
                                        data_format=flags.data_format,
                                        batch_norm_fused=True,
                                        batch_renorm=False,
                                        **model_kwargs)

        logits, end_points = mox_model_fn(images)

        labels_one_hot = slim.one_hot_encoding(labels, 1000)
        loss = tf.losses.softmax_cross_entropy(logits=logits,
                                               onehot_labels=labels_one_hot,
                                               label_smoothing=0.0,
                                               weights=1.0)

        logits_fp32 = tf.cast(logits, tf.float32)
        accuracy_top_1 = tf.reduce_mean(
            tf.cast(tf.nn.in_top_k(logits_fp32, labels, 1), tf.float32))
        accuracy_top_5 = tf.reduce_mean(
            tf.cast(tf.nn.in_top_k(logits_fp32, labels, 5), tf.float32))

        log_info = {
            'ent_loss': loss,
            'top-1': accuracy_top_1,
            'top-5': accuracy_top_5
        }

        regularization_losses = mox.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        if len(regularization_losses
               ) > 0 and flags.use_optimizer != 'dymomentumw':
            regularization_loss = tf.add_n(regularization_losses)
            log_info['reg_loss'] = regularization_loss
            loss = loss + regularization_loss
            log_info['total_loss'] = loss

        return mox.ModelSpec(loss=loss, log_info=log_info)