XiaoPanX / FastLatticeSuperpixels

MIT License
3 stars 0 forks source link

How to set up the environment to run the code? #1

Open Haojia521 opened 1 year ago

Haojia521 commented 1 year ago

I'm not familiar with caffe and I would greatly appreciate it if you could provide some guidance on how to configure the environment to run your code.

XiaoPanX commented 1 year ago

Please refer to our updataed ReadMe file.

Haojia521 commented 1 year ago

Ok, thank you very much for updating the codes. However, I'm confused the following codes to calculate number superpixels in row or colum direction: https://github.com/XiaoPanX/FastLatticeSuperpixels/blob/72429dc6975c30b5df4b96cef78648214a6e8e97/PytorchVersion/Example.py#L30-L31 the operater ^ means XOR in python. Should there be ** operator?

XiaoPanX commented 1 year ago

Yes, you are right. We made a mistake. And there is also an error on the defination of the input_transform function. Please see the updated example.py file

Haojia521 commented 1 year ago

Thanks for your reply. I have paid some efforts to run some test codes of pytroch version model. Following the "ReadMe", I compiled and installed the three cuda extensions and successfully got the outputs. However, the superpixel segmentation results seem to be incorrect.

the result by using input image of lab color: example-lab using lab input image

The segmentation is a mess.

the result by using input image of rgb color: example-rgb using rgb input image

The segmentation looks better but the edges of superpixels not adhere object boundary.

Hers is my test code. Is there any error leading to the incorrect segmentation results?

import os
import os.path as osp
import torch
from torchvision import transforms
import numpy as np
from tqdm import tqdm

from skimage.color import rgb2lab
from skimage.util import img_as_float
from skimage import io
from skimage.segmentation import mark_boundaries

from modelsA.Spixel_trans_feature import SpixelTransNet
from pixelfeaturecuda.pixelfeature import PixelFeatureFunction

class ArrayToTensor(object):
    """Converts a numpy.ndarray (H x W x C) to a torch.FloatTensor of shape (C x H x W)."""

    def __call__(self, array):
        assert(isinstance(array, np.ndarray))

        # put it from HWC to CHW format
        array = np.transpose(array, (2, 0, 1))
        # handle numpy array
        tensor = torch.from_numpy(array)

        return tensor.float()

def to_boundary_image(img, np_sp_map, output_file_path):
    bdy_image = mark_boundaries(img, np_sp_map.astype(np.int32), color=(0, 1, 1), background_label=-1)
    io.imsave(output_file_path, (bdy_image*255).astype(np.uint8))

@torch.no_grad()
def process(model, img, s_h, s_w, s_l, p_scale, color_scale):
    img_width = img.shape[3]
    img_height = img.shape[2]
    num_spixels_w = int(np.floor(img_width /(s_w*2**(s_l-1))))
    num_spixels_h = int(np.floor(img_height / (s_h*2**(s_l-1))))
    pos_scale_w = (1.0 * num_spixels_w) / (float(p_scale) * img_width)
    pos_scale_h = (1.0 * num_spixels_h) / (float(p_scale) * img_height)
    pos_scale = np.max([pos_scale_h, pos_scale_w])

    img_trans = PixelFeatureFunction.apply(img, pos_scale, color_scale)
    sp_map = model(img_trans, s_h, s_w, s_l)

    return sp_map.squeeze()

def run(img_dir, out_dir, s_h, s_w, s_l, p_scale, color_scale):
    os.makedirs(out_dir, exist_ok=True)

    # data transform
    input_transform = transforms.Compose([
        ArrayToTensor(),
    ])

    # load model
    modelA = SpixelTransNet(batchNorm=True)
    modelA.load_weights_from_pkl('BSDweight.pkl')
    modelA = modelA.cuda().eval()

    # get name list of images
    name_list = os.listdir(img_dir)

    # process every image
    bar = tqdm(name_list, total=len(name_list))
    for img_name in bar:
        bar.set_description(img_name)
        nid = img_name[:-4]

        img_rgb = img_as_float(io.imread(osp.join(img_dir, img_name)))

        # use the lab color space
        # img_lab = rgb2lab(img_rgb)
        # img = input_transform(img_lab).unsqueeze(0).cuda()

        # use the rgb color space
        img = input_transform(img_rgb).unsqueeze(0).cuda()

        sp_map = process(modelA, img, s_h, s_w, s_l, p_scale, color_scale)

        to_boundary_image(img_rgb, sp_map.cpu().numpy(), osp.join(out_dir, f'{nid}.png'))

if __name__ == '__main__':
    run(r'/mnt/d/TempFiles/FLSP/img', r'/mnt/d/TempFiles/FLSP', 2, 2, 4, 0.4, 0.26)