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

PIL图像Caffe分割的整形输出阵列

  •  1
  • Michael  · 技术社区  · 6 年前

    希望每个人白天(或晚上)都过得很好。

    我一直在玩我遇到的一个Caffe模型,在使用输出阵列时遇到了一些问题。我以前没有使用过分割,因此这可能是一个简单的修复方法,适合于对该主题更了解的人。

    该模型基于本文 Deep Joint Task Learning for Generic Object Extraction 。我已将模型转换为CoreML格式。

    我的问题是:

    当试图从输出中创建PIL图像时,我得到了看起来像随机噪声的东西,我认为这只是一个简单的问题,即numpy数组的形状不正确,或者像素的顺序错误。 输出阵列的形状为(2500,1),应该是50x50黑白图像

    代码如下所示:

    image = Image.open('./1.jpg')
    image = image.resize((55, 55), Image.ANTIALIAS)
    
    predictions = model.predict({'data_55': image} , useCPUOnly = False)
    predictions = predictions['fc8_seg']
    
    reshape_array = numpy.reshape(predictions, (50,50))
    output_image = Image.fromarray(reshape_array, '1')
    

    我在numpy整形上尝试了F和C顺序,除了噪音之外,似乎没有其他效果 。我使用的是原始回购协议中提供的一个测试图像,所以应该不会有问题。另请注意,数组中的值如下所示:

    [[  4.55798066e-08   5.40980977e-07   2.13476710e-06 ...,   6.66990445e-08
    6.81615759e-08   3.21255470e-07]
    [  2.69358861e-05   1.94866928e-07   4.71876803e-07 ...,   1.25911642e-10
    3.14572794e-08   1.61371077e-08]
    

    如果您有任何想法或答案,我们将不胜感激并给予帮助。提前感谢!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Michael    6 年前

    看来我能解决这个问题。这不是数组顺序的问题,而是值和数据类型的问题。下面是我为从输出中获得正确图像而编写的代码。

    predictions = model.predict({'data_55': image} , useCPUOnly = True) # Run the prediction
    
    map_final = predictions['fc8_seg'][0,0,:,:] # fc8_seg is the output of the neural network
    map_final = map_final.reshape((50,50)) # Reshape the output from shape (2500) to (50, 50)
    map_final = numpy.flip(map_final, 1) # Flip axis 1 to unmirror the image
    
    # Scale the values in the array to a range between 0 and 255
    map_final -= map_final.min() 
    map_final /= map_final.max()
    map_final = numpy.ceil(map_final*255)
    
    map_final_unint8 = map_final.astype(numpy.uint8) # Convert the data type to an uint8
    pil_image = Image.fromarray(map_final_unint8, mode = 'L') # Create the PIL image
    

    和输出:

    And the output

    一切看起来都正常!