semantic-segmentation-pytorch icon indicating copy to clipboard operation
semantic-segmentation-pytorch copied to clipboard

how to output the gray image without random.randint?

Open zhou-rui1 opened this issue 4 years ago • 5 comments

Hi, I want to output the gray image for the binary classfication, how should I edit it?

I try to edit this part but failed: `random.seed(0) class_colors = [(random.randint(0, 255), random.randint( 0, 255), random.randint(0, 255)) for _ in range(5000)]

def convert_seg_gray_to_color(input, n_classes, output_path=None, colors=class_colors): if isinstance(input, six.string_types): seg = cv2.imread(input, flags=cv2.IMREAD_GRAYSCALE) elif type(input) is np.ndarray: assert len(input.shape) == 2, "Input should be h,w " seg = input

height = seg.shape[0]
width = seg.shape[1]

seg_img = np.zeros((height, width, 3))

for c in range(n_classes):
	seg_arr = seg[:, :] == c
	seg_img[:, :, 0] += ((seg_arr) * colors[c][0]).astype('uint8')
	seg_img[:, :, 1] += ((seg_arr) * colors[c][1]).astype('uint8')
	seg_img[:, :, 2] += ((seg_arr) * colors[c][2]).astype('uint8')

if output_path:
	cv2.imwrite(output_path, seg_img)
else:
	return seg_img`

Can you give me some guidance? Thanks!

zhou-rui1 avatar Feb 06 '21 11:02 zhou-rui1

Hi rui,

You can try to change one code line like as bellow.

predict method in predict.py

seg_img = convert_seg_gray_to_color(lbl_pred, n_classes, colors=colors)

to

seg_img = lbl_pred

Thanks

IanTaehoonYoo avatar Feb 09 '21 00:02 IanTaehoonYoo

Thanks for your guidance!

Did you split the train and test manual in advance, or can it implement train_img_ids, val_img_ids = train_test_split(img_ids, test_size=0.2, random_state=41) function like this ?

With regards rui

zhou-rui1 avatar Feb 12 '21 02:02 zhou-rui1

Hi, when I input different size of images it feedback an inconsistent tensor,

also I am still looking in split the dataset automatically...is there any way?

Best regards, rui

zhou-rui1 avatar Feb 18 '21 11:02 zhou-rui1

Hi, I am wondering why for binary segmentation, I have to set the n_classes as 256 to train, other than 2, is this according to the pixel values?

zhou-rui1 avatar Aug 07 '21 02:08 zhou-rui1

I think it may need to add a pixel value normalization process likeimg = img.astype('float32') / 255,

but where should I put it...could you give me more guidance ?

With great appreciation

zhou-rui1 avatar Aug 07 '21 08:08 zhou-rui1