ValueError:`decode_predictions`需要一批预测(即2D形状阵列(样本,1000))。找到形状有阵列:(1,7)

时间:2018-03-13 14:57:37

标签: python machine-learning keras conv-neural-network pre-trained-model

我正在使用带有keras的VGG16进行传输学习(我的新模型中有7个类),因此我想使用内置的decode_predictions方法输出模型的预测。但是,使用以下代码:

preds = model.predict(img)

decode_predictions(preds, top=3)[0]

我收到以下错误消息:

  

ValueError:decode_predictions需要一批预测(即2D形状阵列(样本,1000))。找到形状为:(1,7)

的数组

现在我想知道为什么当我在再培训模型中只有7个班级时,为什么会有1000个。

我在stackoverflow上找到的类似问题(Keras: ValueError: decode_predictions expects a batch of predictions )建议在模型定义中包含'inlcude_top = True'来解决这个问题:

model = VGG16(weights='imagenet', include_top=True)

我试过这个,但是它仍然没有用 - 给我和以前一样的错误。任何关于如何解决这个问题的提示或建议都受到高度赞赏。

2 个答案:

答案 0 :(得分:6)

i suspect you are using some pre-trained model, let's say for instance resnet50 and you are importing decode_predictions like this:

from keras.applications.resnet50 import decode_predictions

decode_predictions transform an array of (num_samples, 1000) probabilities to class name of original imagenet classes.

if you want to transer learning and classify between 7 different classes you need to do it like this:

base_model = resnet50 (weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 7 classes
predictions = Dense(7, activation='softmax')(x) 
model = Model(inputs=base_model.input, outputs=predictions)
...

after fitting the model and calculate predictions you have to manually assign the class name to output number without using imported decode_predictions

答案 1 :(得分:0)

'decode_predictions'函数的重载。注释掉原始函数的1000个类约束:

CLASS_INDEX = None
@keras_modules_injection
def test_my_decode_predictions(*args, **kwargs):
    return my_decode_predictions(*args, **kwargs)


def my_decode_predictions(preds, top=5, **kwargs):
    global CLASS_INDEX

    backend, _, _, keras_utils = get_submodules_from_kwargs(kwargs)

    # if len(preds.shape) != 2 or preds.shape[1] != 1000:
    #     raise ValueError('`decode_predictions` expects '
    #                      'a batch of predictions '
    #                      '(i.e. a 2D array of shape (samples, 1000)). '
    #                      'Found array with shape: ' + str(preds.shape))
    if CLASS_INDEX is None:
        fpath = keras_utils.get_file(
            'imagenet_class_index.json',
            CLASS_INDEX_PATH,
            cache_subdir='models',
            file_hash='c2c37ea517e94d9795004a39431a14cb')
        with open(fpath) as f:
            CLASS_INDEX = json.load(f)
    results = []
    for pred in preds:
        top_indices = pred.argsort()[-top:][::-1]
        result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices]
        result.sort(key=lambda x: x[2], reverse=True)
        results.append(result)
    return results


print('Predicted: ', test_my_decode_predictions(pred, top=10))