1

I am trying to generate masks for the image segmentation task using dinov2 model. I have generated some using patch embeddings but when I try to save these mask images, the saved mask images are all black. How can i resolve this error?

import numpy as np
import torch.nn.functional as F
ans=[]
masks=[]
for idx,(image,_) in enumerate(trainloader):
    seg=segmentor(1)
    x=seg.forward(layerpatches)
    print(f"mask image shape:{x.shape}")
    for i in range(x.shape[0]):
        image_array = x[i].detach().squeeze().numpy()
        mask_image = image_array
        image=image.permute(0,2,3,1).squeeze()
        fig, ax = plt.subplots(1, 2, figsize=(10, 5))
        ax[0].imshow(mask_image)
        ax[0].set_title('Mask')
        ax[0].axis('off')
        ax[1].imshow(image)
        ax[1].set_title('Corresponding Image')
        ax[1].axis('off')
        plt.show()
        mask_name = f"{img_name[idx]}_mask.jpg"
        mask_np = np.clip(mask_rgb, 0, 255).astype(np.uint8)
        # # Save the adjusted mask as PNG
        mask_path = os.path.join(mask_dir, mask_name)
        cv2.imwrite(mask_path,mask_image)
    break

The mask and corresponding image visualized on kaggle Same mask when saved in either png or jpg format

I have tried all the techniques to save the image as well as converting mask to RGB but saved images are all black.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
user436524
  • 11
  • 1

0 Answers0