如何用非序贯模型得到类标签?

2024-10-04 09:31:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Keras训练了一个9类的图像识别(带边界框)模型。验证精度为0.85

我试过使用predict\u类,但是因为模型不是连续的,所以它不起作用。此外,我还尝试将预测数组与按字母顺序排序的标签列表相匹配。但都没用

预测:


kategoriler = ['category0', 'category1', 'category2', 'category3', 'category4', 'category5', 'category6', 'category7', 'category8']


def load_image(img_path, show=False):
    img = image.load_img(img_path, target_size=(200, 200))
    img_tensor = image.img_to_array(img)   
    img_tensor = np.expand_dims(img_tensor, axis=0)    
    img_tensor /= 255.    

    return img_tensor

def predict_labels (event,context):
    img_url = event['imageUrl']
    img_id = event['id']
    human_id = event['humanId']


    img_path = '/tmp/img'
    os.system('curl -o ' + img_path + ' ' + img_url)

    new_image = load_image(img_path)

    # check prediction
    pred = model.predict(new_image)
    sonuc = kategoriler[np.argmax(pred[0])]

    response = {
        'id' : img_id,
        'humanId' : human_id,
        'clothes' : {
            'category' : sonuc,
            'subCategory' : '-'
        },
        'pred': str(pred)
    }

    return json.dumps(response)

我期望输出category6,但实际输出是0.05920969


Tags: path模型imageeventidimgreturndef