x and y can be no greater than 2-D, but have shapes (1,) and (1, 224, 224, 3)

给你一囗甜甜゛ 提交于 2021-01-28 11:11:44

问题


I am trying to visualize the prediction of VGG-16 for the cat image, Compute the top-5 scores (the 5 classes with the maximum probability), For each of these 5 scores, print the corresponding label and the corresponding probability.

    from keras.applications.vgg16 import preprocess_input
    from keras.preprocessing import image

    # load the image from cat class and resize it
    img = image.load_img('cat.jpg', target_size=(224, 224))

    # convert to numpy array of (224, 224, 3)
    x = image.img_to_array(img)

    # add empty dimention for tensor flow (1,224,224,3)
    x = np.expand_dims(x, axis = 0)

    # perform mean removal as in the original VGG16 network
    x = preprocess_input(x)

    # make the prediction using VGG16
    output = model.predict(x)
    print('model prediction output', output)

    # plot the prediction
    plt.plot(output[0], '-')
    plt.show()

    # decode the prediction
    from keras.applications.vgg16 import decode_predictions
    top5 = decode_predictions(output)
    for _, label, proba in top5[0]:
    print(label, 'with probability', proba)

I am getting this error any help will be appreciated

File "C:\Users\mwaqa\Desktop\Spyder\E8 Q2,3,4,5.py", line 75, in plt.plot(output, '-')

File "c:\users\mwaqa\miniconda3\lib\site-packages\matplotlib\pyplot.py", line 2789, in plot is not None else {}), **kwargs)

File "c:\users\mwaqa\miniconda3\lib\site-packages\matplotlib\axes_axes.py", line 1665, in plot lines = [*self._get_lines(*args, data=data, **kwargs)]

File "c:\users\mwaqa\miniconda3\lib\site-packages\matplotlib\axes_base.py", line 225, in __call__ yield from self._plot_args(this, kwargs)

File "c:\users\mwaqa\miniconda3\lib\site-packages\matplotlib\axes_base.py", line 391, in _plot_args x, y = self._xy_from_xy(x, y)

File "c:\users\mwaqa\miniconda3\lib\site-packages\matplotlib\axes_base.py", line 273, in _xy_from_xy "shapes {} and {}".format(x.shape, y.shape))

ValueError: x and y can be no greater than 2-D, but have shapes (1,) and (1, 224, 224, 3)

来源:https://stackoverflow.com/questions/59879337/x-and-y-can-be-no-greater-than-2-d-but-have-shapes-1-and-1-224-224-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!