简体   繁体   中英

RGB color codes in Semantic Segmentation

I am using the Semantic Segmentation network (SegNet). I am trying to reduce the number of classes and thus rearranging the network.

Therefore, I am also changing the color-coding of the predictions as well. My problem is I don't get the intended colors in the output image.

For eg

pascal_palette = np.array([(0, 0, 0),
                           (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                           (0, 0, 128), (0, 128, 0), (0, 0, 0), (0, 0, 0), (128, 0, 0),
                           (0, 0, 0), (0, 0, 0)
                        ], dtype=np.uint8) 

The above line gives perfect results for the three classes as the pixels are only in 1 channel.

The output is as below:

给出预期的结果

However, if I modify the line and add values to different channels it gives weird output. The output is attached below:

pascal_palette = np.array([(0, 0, 0),
                           (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                           (0, 0, 128), (124, 252, 0), (0, 0, 0), (0, 0, 0), (128, 0, 0),
                           (0, 0, 0), (0, 0, 0)
                            ], dtype=np.uint8)

Changed the color code to (124, 252, 0). The code should be for lawn green color. I also checked it on a website like RBG codes

都变红了

What am I missing here? Any explanation will be helpful.

Prediciton code:

 prob = model.predict(net_in)[0]

    # Reshape to 2d here since the networks outputs a flat array per channel
    prob_edge = np.sqrt(prob.shape[0]).astype(np.int)
    prob = prob.reshape((prob_edge, prob_edge, 13))

    # Upsample
    if args.zoom > 1:
        prob = interp_map(prob, args.zoom, image_size[1], image_size[0])

    # Recover the most likely prediction (actual segment class)
    prediction = np.argmax(prob, axis=2)

    # Apply the color palette to the segmented image
    color_image = np.array(pascal_palette)[prediction.ravel()].reshape(
        prediction.shape + (3,))

    print('Saving results to: ', args.output_path)
    with open(args.output_path, 'wb') as out_file:
        Image.fromarray(np.multiply(color_image,255)).save(out_file)

PS. I have used same model for predictions in both case

The problem is very probably in np.multiply(color_image,255) .

As you created a pallete already with values from 0 to 255 and you're simply gathering values from this pallete, you don't need to multiply it by 255.

Use simply Image.fromarray(color_image).save(out_file) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM