1

我目前按照教程再培训盗梦空间的图像分类更改标签名称: https://cloud.google.com/blog/big-data/2016/12/how-to-train-and-classify-images-using-google-cloud-machine-learning-and-cloud-dataflow在谷歌云再培训盗当ML

然而,当我提出与API我只得到我的课的指数预测一个标签。不过,我想,该API实际上是给了我一个字符串返回的实际类名称,如代替

​predictions: 
- key: '0' 
    prediction: 4 
    scores: 
    - 8.11998e-09 
    - 2.64907e-08 
    - 1.10307e-06 

我想获得:

​predictions: 
- key: '0' 
    prediction: ROSES 
    scores: 
    - 8.11998e-09 
    - 2.64907e-08 
    - 1.10307e-06 

查看的是谷歌API的参考它应该是可能的: https://cloud.google.com/ml-engine/reference/rest/v1/projects/predict

我已经尝试过在model.py更改以下到

outputs = { 
    'key': keys.name, 
    'prediction': tensors.predictions[0].name, 
    'scores': tensors.predictions[1].name 
} 
tf.add_to_collection('outputs', json.dumps(outputs)) 

if tensors.predictions[0].name == 0: 
    pred_name ='roses' 
elif tensors.predictions[0].name == 1: 
    pred_name ='tulips' 


outputs = { 
    'key': keys.name, 
    'prediction': pred_name, 
    'scores': tensors.predictions[1].name 
} 
tf.add_to_collection('outputs', json.dumps(outputs)) 

但这不起作用。

我的下一个想法是改变这一部分在preprocess.py文件。所以,而不是得到我想要使用字符串标签的索引。

def process(self, row, all_labels): 
    try: 
     row = row.element 
    except AttributeError: 
     pass 
    if not self.label_to_id_map: 
     for i, label in enumerate(all_labels): 
     label = label.strip() 
     if label: 
      self.label_to_id_map[label] = label #i 

label_ids = [] 
for label in row[1:]: 
    try: 
    label_ids.append(label.strip()) 
    #label_ids.append(self.label_to_id_map[label.strip()]) 
    except KeyError: 
    unknown_label.inc() 

但是这给了错误:

TypeError: 'roses' has type <type 'str'>, but expected one of: (<type 'int'>, <type 'long'>) [while running 'Embed and make TFExample'] 

所以我想我应该在这里preprocess.py改变的东西,为了让字符串:

example = tf.train.Example(features=tf.train.Features(feature={ 
     'image_uri': _bytes_feature([uri]), 
     'embedding': _float_feature(embedding.ravel().tolist()), 
    })) 

if label_ids: 
    label_ids.sort() 
    example.features.feature['label'].int64_list.value.extend(label_ids) 

但我不硝酸钾如何适当地改变它,因为我找不到像str_list那样的东西。任何人都可以请帮我在这里?

回答

0

在线预测当然允许这一点,模型本身需要进行更新,以从int到字符串做转换。

请记住,Python代码只是建筑,描述了计算在你的模型做一个图形 - 你不发送Python代码网上预报名,你发送你建立的图表。您还没有任何输入或预测,所以你将不能够检查它们的值 - 因为你所做的更改是在Python

这区别是非常重要的。您需要做的是将等同的查找添加到要导出的图形中。

您可以修改,像这样的代码:

labels = tf.constant(['cars', 'trucks', 'suvs']) 
predicted_indices = tf.argmax(softmax, 1) 
prediction = tf.gather(labels, predicted_indices) 

而离开输入/输出不变,从原来的代码