【问题标题】:ValueError: Error when checking input: expected input_2 to have shape (224, 224, 3) but got array with shape (224, 224, 4)ValueError:检查输入时出错:预期 input_2 的形状为 (224, 224, 3) 但得到的数组的形状为 (224, 224, 4)
【发布时间】:2020-01-11 23:43:45
【问题描述】:

我已从文件夹中获取输入,然后根据模型 VGG16-places365 对其进行相应的重新调整。它仍然显示相同的错误,并查看了问题的 Keras 文档 (https://keras.io/applications/#vgg16),但错误仍然存​​在。

if __name__ == '__main__':
    #from urllib.request import urlopen
    import numpy as np
    from PIL import Image
    from cv2 import resize
    pred_array = np.empty((0,6),dtype=float)
    TEST_PATH = '/home/guest/Downloads/content/image/thumb'
    for img in os.listdir(TEST_PATH):
        image = Image.open(os.path.join(TEST_PATH, img))
        image = np.array(image, dtype=np.uint8)
        image = resize(image, (224, 224))
        image = np.expand_dims(image, 0)

    model = VGG16_Places365(weights='places')
    predictions_to_return = 5
    preds = model.predict(image)[0]
    top_preds = np.argsort(preds)[::-1][0:predictions_to_return]

    # load the class label
    file_name = 'categories_places365.txt'
    if not os.access(file_name, os.W_OK):
        synset_url = 'https://raw.githubusercontent.com/csailvision/places365/master/categories_places365.txt'
        os.system('wget ' + synset_url)
    classes = list()
    with open(file_name) as class_file:
        for line in class_file:
            classes.append(line.strip().split(' ')[0][3:])
    classes = tuple(classes)
    temprow = np.hstack((np.array([img]),top_preds))
    np.append(pred_array,temprow.reshape(-1,pred_array.shape[1]),axis=0)
df = pd.DataFrame(data=pred_array,columns=['File_name','Tag_1','Tag_2','Tag_3','Tag_4','Tag_5'])    
print(df)

【问题讨论】:

    标签: python deep-learning computer-vision vgg-net imagenet


    【解决方案1】:

    您可能正在加载具有 Alpha 通道 (RGBA) 的图像,但 VGG16 神经网络需要一个没有 Alpha 通道 (RGB) 的图像。

    要将图像从 RGBA 转换为 RGB,您可以使用

    image = image.convert("RGB")
    

    在 PIL Image 对象上,即直接在Image.open 之后,或者在调用np.array 之后,在numpy 数组对象上使用numpy 数组切片来切断前三个颜色通道:

    image = image[:, :, :3]
    

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 2019-11-21
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 2022-06-15
      相关资源
      最近更新 更多