NeurIPS-CellSeg
NeurIPS-CellSeg copied to clipboard
using compute_metrics.py for segment anything
I want to evaluate the results of segment anything on cells. The issue is that I am struggling to convert the masks generated by segment anything to the format required by compute_metrics.py. I followed the examples:
def generate(self, image: np.ndarray) -> List[Dict[str, Any]]:
"""
Generates masks for the given image.
Arguments:
image (np.ndarray): The image to generate masks for, in HWC uint8 format.
Returns:
list(dict(str, any)): A list over records for masks. Each record is
a dict containing the following keys:
segmentation (dict(str, any) or np.ndarray): The mask. If
output_mode='binary_mask', is an array of shape HW. Otherwise,
is a dictionary containing the RLE.`
This is the definition of "segmentation" in the generate function of segment anything.
for name in names:
# 1. read image: img_data = imread(os.path.join(input_path, name))
# 2. infer the image with your model (the output should be instance mask)
inst_mask = model(img_data)
# relabel the mask
inst_mask, _, _ = segmentaiton.relabel_sequential(inst_mask)
# save results; Please compress the mask by setting the `compression`
save_name = name.split('.')[0] + '_label.tiff' # the suffix of the segmentation results should be '_label.tiff'
tifffile.imwrite(join(output_path, save_name), np.int16(inst_mask), compression='zlib')
I checked the example code for how to handle the data in the dataset but still failed to make it work.
Hi @XiangyuLijoey ,
Here is a demo to save the SAM auto-seg results to the instance label map.
masks = mask_generator.generate(image)
mask_np = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint16)
for idx, mask in enumerate(masks, start=1):
mask_np[mask['segmentation']] = idx
mask_np, _, _ = segmentation.relabel_sequential(mask_np)
# remove small regions
tif.imwrite(join(save_path, name.replace('.png', '.tiff')), mask_np.astype(np.uint16))