gpt-4o-mini-2024-07-18· openai
from WildChat-4.8M
class ImageMaskDataset(Dataset): def init(self, imagetxtfile, masktxtfile,…
- User
class ImageMaskDataset(Dataset): def init(self, image_txt_file, mask_txt_file, transform=None): self.image_mask_pairs = self.read_txt_files(image_txt_file, mask_txt_file) self.transform = transform
codedef read_txt_files(self, image_txt_file, mask_txt_file): image_mask_pairs = [] # 读取图像路径 with open(image_txt_file, 'r') as img_file, open(mask_txt_file, 'r') as msk_file: for img_line, msk_line in zip(img_file, msk_file): image_path = img_line.strip() mask_path = msk_line.strip() image_mask_pairs.append((image_path, mask_path)) return image_mask_pairs def __len__(self): return len(self.image_mask_pairs) def __getitem__(self, idx): image_path, mask_path = self.image_mask_pairs[idx] try: image = Image.open(image_path).convert('RGB') mask = Image.open(mask_path).convert('L') except Exception as e: print(f"Error loading image or mask: {e}") return self.__getitem__((idx + 1) % len(self.image_mask_pairs)) # 跳过当前文件,尝试加载下一个文件 if self.transform: image = self.transform(image) mask = self.transform(mask) # 下采样和上采样mask downsample_transform = transforms.Resize((mask.shape[1] // 2, mask.shape[2] // 2), interpolation=Image.NEAREST) upsample_transform = transforms.Resize((mask.shape[1], mask.shape[2]), interpolation=Image.NEAREST) mask_downsampled = downsample_transform(mask) mask_upsampled = upsample_transform(mask_downsampled) # 可视化输入 mask_upsampled_input = mask_upsampled.cpu().detach().numpy() cv2.imwrite("/home/notebook/data/group/dzb/mask_patch/mask_upsampled_input.png", mask_upsampled_input[0] *255 ) # 将image和upsampled mask合并为4通道输入 combined = torch.cat((image, mask_upsampled), dim=0) return combined, mask - Assistant
- User
刚刚说错了,depth就是mask
- Assistant