代码之家  ›  专栏  ›  技术社区  ›  wl2776

复制纸质结果时Keras VGG16的准确性较差

  •  0
  • wl2776  · 技术社区  · 8 年前

    我正试图重现 paper, describing Grad-CAM method ,将Keras与Tensorflow GPU后端一起使用,并获取完全不正确的标签。

    这是我的形象:

    snapshot

    Keras manuals

    import imageio
    from matplotlib import pyplot as plt
    from skimage.transform import resize
    
    from keras import activations
    from keras.applications import VGG16
    from keras.applications.vgg16 import preprocess_input, decode_predictions
    
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    
    %matplotlib inline
    
    dog_img = imageio.imread(r"F:\tmp\Opera Snapshot_2018-09-24_133452_arxiv.org.png")
    dog_img = dog_img[:, :, 0:3]   # Opera has added alpha channel
    dog_img = resize(dog_img, (224, 224, 3))
    
    x = np.expand_dims(dog_img, axis=0)
    x = preprocess_input(x, mode='tf')
    
    pred = model.predict(x)
    decode_predictions(pred)
    

    输出:

    [[('n03788365', 'mosquito_net', 0.017053505),
      ('n03291819', 'envelope', 0.015034639),
      ('n15075141', 'toilet_tissue', 0.012603286),
      ('n01737021', 'water_snake', 0.010620943),
      ('n04209239', 'shower_curtain', 0.009625845)]]
    

    然而,当我把同一张图片提交给论文作者运行的在线服务时, http://gradcam.cloudcv.org/classification

    这是他们称之为“终端”的东西的输出:

    Completed the Classification Task
    
    "Time taken for inference in torch: 9.0"
    "Total time taken: 9.12565684319"
    {"classify_gcam": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gcam_243.png", "execution_time": 9.0, "label": 243.0, "classify_gb_gcam": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gb_gcam_243.png", "classify_gcam_raw": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gcam_raw_243.png", "input_image": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/Opera Snapshot_2018-09-24_133452_arxiv.org.png", "pred_label": 243.0, "classify_gb": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gb_243.png"}
    Completed the Classification Task
    
    "Time taken for inference in torch: 9.0"
    "Total time taken: 9.05940508842"
    {"classify_gcam": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gcam_243.png", "execution_time": 9.0, "label": 243.0, "classify_gb_gcam": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gb_gcam_243.png", "classify_gcam_raw": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gcam_raw_243.png", "input_image": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/Opera Snapshot_2018-09-24_133452_arxiv.org.png", "pred_label": 243.0, "classify_gb": "./media/grad_cam/classification/86560f84-bfe5-11e8-a657-22000b4a9274/classify_gb_243.png"}
    Job published successfully
    Publishing job to Classification Queue
    Starting classification job on VGG_ILSVRC_16_layers.caffemodel
    Job published successfully
    Publishing job to Classification Queue
    Starting classification job on VGG_ILSVRC_16_layers.caffemodel
    

    keras                     2.2.2                         0
    keras-applications        1.0.4                    py36_1
    keras-base                2.2.2                    py36_0
    keras-preprocessing       1.0.2                    py36_1
    tensorflow                1.10.0          eigen_py36h849fbd8_0
    tensorflow-base           1.10.0          eigen_py36h45df0d8_0
    

    我做错什么了?我怎样才能得到拳击手的标签?

    2 回复  |  直到 8 年前
        1
  •  1
  •   Atul Shanbhag    8 年前

    显然你不能做下面这行

    dog_img = dog_img[:, :, 0:3]   # Opera has added alpha channel
    

    所以我用Keras中的一个实用程序加载了这个图像 load_img ,它不添加alpha通道。

    import imageio
    from matplotlib import pyplot as plt
    from skimage.transform import resize
    import numpy as np
    from keras import activations
    from keras.applications import VGG16
    from keras.preprocessing import image
    from keras.applications.vgg16 import preprocess_input, decode_predictions
    
    # Build the VGG16 network with ImageNet weights
    model = VGG16(weights='imagenet', include_top=True)
    dog_img = image.img_to_array(image.load_img(r"F:\tmp\Opera Snapshot_2018-09-24_133452_arxiv.org.png", target_size=(224, 224)))
    
    x = np.expand_dims(dog_img, axis=0)
    x = preprocess_input(x)
    
    pred = model.predict(x)
    print(decode_predictions(pred))
    
    [[('n02108089', 'boxer', 0.29122102), ('n02108422', 'bull_mastiff', 0.199128), ('n02129604', 'tiger', 0.10050287), ('n02123159', 'tiger_cat', 0.09733449), ('n02109047', 'Great_Dane', 0.056869864)]]   
    
        2
  •  1
  •   dportman    8 年前

    考虑到所有的输出概率都很低,并且或多或少地平均分布在0.01左右,我的猜测是您对图像进行了错误的预处理,并将某种看起来像噪声的加扰图像传递给了 model.predict() imshow 你面前的图像 predict()