当前位置: 代码迷 >> python >> 错误:投影表达式[table({{gcloud.ml-engine.local.predict)
  详细解决方案

错误:投影表达式[table({{gcloud.ml-engine.local.predict)

热度:75   发布时间:2023-06-19 09:24:41.0

我正在定义一个多头深度学习模型。 我正在使用tensorflow.estimator建议的包装(python包装),并在我的model_fn函数中定义了输出字典。 这是我的函数的代码:

def model_fn(features, labels, mode, params):
    global LIST_OF_LABELS
    global LEN_OF_LABELS
    get_model_fn = MODEL_FCS.get(params['model_name'])

    if get_model_fn is not None:
        if not LIST_OF_LABELS:
            with open(os.path.join(os.path.split(params['train_data_path'])[0], 'data.json')) as flabel:
                classes_dump = json.load(flabel)
            LIST_OF_LABELS = {'name_tree{}'.format(i): classes_dump['name_tree{}'.format(i)] for i in range(1,6)}
        if not LEN_OF_LABELS:
            LEN_OF_LABELS = {k: len(it) for k, it in LIST_OF_LABELS.items()}

        if isinstance(features, dict):
            features = features["image"]
        tf.logging.info('Features sizing ... features={}'.format(features.get_shape()))
        base_model = get_model_fn(
            weights=None,
            include_top=False,
            input_tensor=features,
            pooling='max'
        )
        tf.logging.info('Loaded {} model'.format(params['model_name']))

        feature_map = base_model.output
        tf.logging.info(feature_map)
        feature_map = tf.keras.layers.Dense(
            params['latent_size'],
            activation='sigmoid',
            kernel_initializer='glorot_uniform',
            name='latent'+str(params['latent_size'])
        )(feature_map)
        feature_map = tf.keras.layers.Dropout(0.25)(feature_map)
        tf.logging.info('Latent layer sizing ... latent_size={}'.format(feature_map.get_shape()))

        logits = []
        predictions = {}
        def get_key(a1, a2):
            return '%s/%s' % (a1, a2)
        for head_name, it in sorted(LIST_OF_LABELS.items()):
            _logit = tf.layers.batch_normalization(
                tf.keras.layers.Dense(
                    units=len(it),
                    activation=None,
                    kernel_initializer='glorot_uniform',
                    name='fc_'+head_name)(feature_map),
                training=(mode == tf.estimator.ModeKeys.TRAIN)
            )
            logits.append(_logit)
            prob_key = get_key("probabilities", head_name)
            id_key = get_key("class_id", head_name)
            str_key = get_key("class_str", head_name)
            predictions[prob_key] = tf.nn.softmax(_logit, name="softmax_tensor_"+head_name)
            # tf.logging.info('predictions[prob_key]={}'.format(predictions[prob_key].get_shape()))
            predictions[id_key] = tf.cast(tf.argmax(predictions[prob_key], 1), tf.int32)
            predictions[str_key] = tf.gather(LIST_OF_LABELS[head_name], tf.cast(predictions[id_key], tf.int32))

        if mode == tf.estimator.ModeKeys.TRAIN or mode == tf.estimator.ModeKeys.EVAL:
            evalmetrics, accuracy = {}, {}
            for i, (head_name, it) in enumerate(sorted(LIST_OF_LABELS.items())):
                accuracy[head_name] = tf.metrics.accuracy(predictions[get_key("class_id", head_name)], labels[head_name])
                evalmetrics['accuracy/'+head_name] =  accuracy[head_name]
                tf.summary.scalar('accuracy/'+head_name, accuracy[head_name][1])
            predictions = None
            export_outputs = None
            loss = 0
            for i, logit in enumerate(logits):
                k = CLASS_NAMES[i]
                loss_i = tf.losses.softmax_cross_entropy(
                    onehot_labels=tf.one_hot(labels[k], LEN_OF_LABELS[k]),
                    label_smoothing=0.1,
                    logits=logit
                )
                loss += loss_i
                tf.summary.scalar('loss/name_tree{}'.format(i+1), loss_i)
            if mode == tf.estimator.ModeKeys.TRAIN:
                def rate_decay(learning_rate, global_step):
                    return tf.train.exponential_decay(learning_rate, global_step,
                        decay_steps=131000, decay_rate=0.94)
                update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
                train_op_fn = tf.contrib.layers.optimize_loss(
                    loss, 
                    tf.train.get_global_step(),
                    learning_rate=params['learning_rate'],
                    optimizer="Adam",
                    update_ops=update_ops,
                    learning_rate_decay_fn=rate_decay
                )
            else:
                train_op_fn = None
        else:
            loss = None
            train_op_fn = None
            evalmetrics = None
            export_outputs = {_DEFAULT_SERVING_KEY:
                tf.estimator.export.PredictOutput(predictions)}

        for var in tf.trainable_variables():
            tf.summary.histogram(var.name.replace(':', '_'), var)
        return tf.estimator.EstimatorSpec(
            mode=mode,
            predictions=predictions,
            loss=loss,
            train_op=train_op_fn,
            eval_metric_ops=evalmetrics,
            export_outputs=export_outputs
        )
    else:
        raise Exception('The model input must be one of this options: %s' % MODEL_FCS)

重要的部分是这一部分:

def get_key(a1, a2):
    return '%s/%s' % (a1, a2)

在运行gcloud ml-engine local predict /正在创建以下错误:

ERROR: (gcloud.ml-engine.local.predict) Expected ) in projection expression [table(
              class_id *HERE* /name_tree1, class_id/name_tree2, class_id/name_tree3, class_id/name_tree4, class_id/name_tree5, class_str/name_tree1, class_str/name_tree2, class_str/name_tree3,class_str/name_tree4, class_str/name_tree5, probabilities/name_tree1, probabilities/name_tree2, probabilities/name_tree3, probabilities/name_tree4, probabilities/name_tree5
          )].

来自此文件和行:

File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_projection_parser.py", line 429, in _ParseKeys
self._lex.Annotate()))

我决定开放此问题,因为不支持创建Issues

我通过改变解决了这个/一个_创建用于键时predictions将被馈送到词典export_outputs = {_DEFAULT_SERVING_KEY tf.estimator.export.PredictOutput(predictions)}export_outputs所述的tf.estimator.EstimatorSpec

因此,最终的密钥创建者功能保持如下:

def get_key(a1, a2):
    return '%s_%s' % (a1, a2)
  相关解决方案