File size: 10,163 Bytes
4526133 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | import os
import numpy as np
import torch
import random
import json
import h5py
import cv2
from PIL import Image
from torchvision import transforms
json_data = '{\n "items": [\n {"name": "background", "color_hex_code": "#c00000", "class_group": 0},\n {"name": "Martian Soil", "color_hex_code": "#800000", "class_group": 1},\n {"name": "Sands", "color_hex_code": "#008000", "class_group": 2},\n {"name": "Gravel", "color_hex_code": "#808000", "class_group": 3},\n {"name": "Bedrock", "color_hex_code": "#000080", "class_group": 4},\n {"name": "Rocks", "color_hex_code": "#800080", "class_group": 5},\n {"name": "Tracks", "color_hex_code": "#008080", "class_group": 6},\n {"name": "Shadows", "color_hex_code": "#808080", "class_group": 7},\n {"name": "unknown", "color_hex_code": "#400000", "class_group": 8}\n ]\n}'
data = json.loads(json_data)
color_to_class = {}
for item in data['items']:
hex_color = item['color_hex_code']
rgb_color = tuple((int(hex_color[i:i + 2], 16) for i in (1, 3, 5)))
color_to_class[rgb_color] = item['name']
color_to_label = {}
for (idx, item) in enumerate(data['items']):
hex_color = item['color_hex_code']
rgb_color = tuple((int(hex_color[i:i + 2], 16) for i in (1, 3, 5)))
color_to_label[rgb_color] = item['class_group']
class MarsDataset(object):
def __init__(self, root_dir, input_shape, train):
self.root_dir = root_dir
self.input_shape = input_shape
self.train = train
img_dir = os.path.join(root_dir, 'img')
hsi_dir = os.path.join(root_dir, 'hsi')
label_dir = os.path.join(root_dir, 'label')
def _list_valid_files(folder):
return sorted([f for f in os.listdir(folder) if not f.startswith('.') and os.path.isfile(os.path.join(folder, f))])
img_files = _list_valid_files(img_dir)
hsi_files = _list_valid_files(hsi_dir)
label_files = _list_valid_files(label_dir)
img_map = {os.path.splitext(f)[0]: f for f in img_files}
hsi_map = {os.path.splitext(f)[0]: f for f in hsi_files}
label_map = {os.path.splitext(f)[0]: f for f in label_files}
common_stems = sorted(set(img_map.keys()) & set(hsi_map.keys()) & set(label_map.keys()))
if len(common_stems) == 0:
raise RuntimeError(f'No aligned samples found under {root_dir}. Please check file names in img/hsi/label.')
self.imgs = [img_map[s] for s in common_stems]
self.hsi = [hsi_map[s] for s in common_stems]
self.label = [label_map[s] for s in common_stems]
def __getitem__(self, idx):
rgb_mean = (0.0, 0.0, 0.0)
rgb_std = (255.0, 255.0, 255.0)
(h, w) = self.input_shape
img_path = os.path.join(self.root_dir, 'img', self.imgs[idx])
hsi_path = os.path.join(self.root_dir, 'hsi', self.hsi[idx])
label_path = os.path.join(self.root_dir, 'label', self.label[idx])
if self.train:
with h5py.File(hsi_path, 'r') as mat_data:
cube = np.array(mat_data['cube'])
intensity = 1
lam31 = np.array([400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700])
hsi_one_channel_list = []
resized_data = np.empty((31, h, w), dtype=np.float32)
for i in range(cube.shape[0]):
img_nb = i
img_slice = cube[img_nb, :, :]
img_adjusted = img_slice * intensity
img_adjusted = np.clip(img_adjusted, 0, 1)
img_adjusted = np.transpose(img_adjusted, (1, 0))
img_uint8 = (img_adjusted * 255).astype(np.uint8)
band_img = Image.fromarray(img_uint8)
hsi_one_channel_list.append(band_img)
rgb = Image.open(img_path).convert('RGB')
label_image = cv2.imread(label_path)
label_image = cv2.cvtColor(label_image, cv2.COLOR_BGR2RGB)
(height, width, _) = label_image.shape
label = np.zeros((height, width), dtype=np.uint8)
for (rgb_color, labels) in color_to_label.items():
mask = np.all(label_image == np.array(rgb_color), axis=-1)
label[mask] = labels
label_pil = Image.fromarray(label)
(hsi_one_channel_list, rgb, label_pil) = self.random_flip_LR(hsi_one_channel_list, rgb, label_pil)
(hsi_one_channel_list, rgb, label_pil) = self.random_flip_UD(hsi_one_channel_list, rgb, label_pil)
(hsi_one_channel_list, rgb, label_pil) = self.Random_Crop(hsi_one_channel_list, rgb, label_pil)
for index in range(len(hsi_one_channel_list)):
hsi_one_channel_list[index] = hsi_one_channel_list[index].resize((w, h), Image.BILINEAR)
rgb = rgb.resize((w, h), Image.BILINEAR)
label_pil = label_pil.resize((w, h), Image.NEAREST)
for i in range(31):
resized_data[i, :, :] = np.array(hsi_one_channel_list[i], dtype=np.float32)
hsi = resized_data
rgb = np.array(rgb, dtype=np.float32)
label = np.array(label_pil, dtype=np.uint8)
label[label >= 9] = 255
label = torch.tensor(label, dtype=torch.uint8)
mask = torch.where(label == 7, torch.tensor(1), torch.tensor(0))
transform_rgb = transforms.Compose([transforms.ToTensor(), transforms.Normalize(rgb_mean, rgb_std)])
rgb = transform_rgb(rgb)
return (hsi, rgb, label, mask)
else:
img_path = os.path.join(self.root_dir, 'img', self.imgs[idx])
hsi_path = os.path.join(self.root_dir, 'hsi', self.hsi[idx])
label_path = os.path.join(self.root_dir, 'label', self.label[idx])
with h5py.File(hsi_path, 'r') as mat_data:
cube = np.array(mat_data['cube'])
intensity = 1
lam31 = np.array([400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700])
hsi_one_channel_list = []
resized_data = np.empty((31, h, w), dtype=np.float32)
for i in range(cube.shape[0]):
img_nb = i
img_slice = cube[img_nb, :, :]
img_adjusted = img_slice * intensity
img_adjusted = np.clip(img_adjusted, 0, 1)
img_adjusted = np.transpose(img_adjusted, (1, 0))
img_uint8 = (img_adjusted * 255).astype(np.uint8)
band_img = Image.fromarray(img_uint8)
hsi_one_channel_list.append(band_img)
rgb = Image.open(img_path).convert('RGB')
label_image = cv2.imread(label_path)
label_image = cv2.cvtColor(label_image, cv2.COLOR_BGR2RGB)
(height, width, _) = label_image.shape
label = np.zeros((height, width), dtype=np.uint8)
for (rgb_color, labels) in color_to_label.items():
mask = np.all(label_image == np.array(rgb_color), axis=-1)
label[mask] = labels
label_pil = Image.fromarray(label)
for index in range(len(hsi_one_channel_list)):
hsi_one_channel_list[index] = hsi_one_channel_list[index].resize((w, h), Image.BILINEAR)
rgb = rgb.resize((w, h), Image.BILINEAR)
label_pil = label_pil.resize((w, h), Image.NEAREST)
for i in range(31):
resized_data[i, :, :] = np.array(hsi_one_channel_list[i], dtype=np.float32)
hsi = resized_data
rgb = np.array(rgb, dtype=np.float32)
label = np.array(label_pil, dtype=np.uint8)
label[label >= 9] = 255
label = torch.tensor(label, dtype=torch.uint8)
mask = torch.where(label == 7, torch.tensor(1), torch.tensor(0))
transform_rgb = transforms.Compose([transforms.ToTensor(), transforms.Normalize(rgb_mean, rgb_std)])
rgb = transform_rgb(rgb)
return (hsi, rgb, label, mask)
def __len__(self):
return len(self.imgs)
def random_flip_LR(self, hsi_one_channel_list, rgb, label):
if random.random() > 0.5:
for index in range(len(hsi_one_channel_list)):
hsi_one_channel_list[index] = hsi_one_channel_list[index].transpose(Image.FLIP_LEFT_RIGHT)
rgb = rgb.transpose(Image.FLIP_LEFT_RIGHT)
label = label.transpose(Image.FLIP_LEFT_RIGHT)
return (hsi_one_channel_list, rgb, label)
def random_flip_UD(self, hsi_one_channel_list, rgb, label):
if random.random() > 0.5:
for index in range(len(hsi_one_channel_list)):
hsi_one_channel_list[index] = hsi_one_channel_list[index].transpose(Image.FLIP_TOP_BOTTOM)
rgb = rgb.transpose(Image.FLIP_TOP_BOTTOM)
label = label.transpose(Image.FLIP_TOP_BOTTOM)
return (hsi_one_channel_list, rgb, label)
def get_params(self, low):
(self.h, self.w) = low.size
self.crop_height = min(128, self.h)
self.crop_width = min(128, self.w)
i = random.randint(0, max(self.h - self.crop_height, 0))
j = random.randint(0, max(self.w - self.crop_width, 0))
return (i, j)
def Random_Crop(self, hsi_one_channel_list, rgb, label):
(self.i, self.j) = self.get_params(rgb)
if random.random() > 0.5:
rgb = rgb.crop((self.j, self.i, self.j + self.crop_width, self.i + self.crop_height))
label = label.crop((self.j, self.i, self.j + self.crop_width, self.i + self.crop_height))
for index in range(len(hsi_one_channel_list)):
hsi_one_channel_list[index] = hsi_one_channel_list[index].crop((self.j, self.i, self.j + self.crop_width, self.i + self.crop_height))
return (hsi_one_channel_list, rgb, label)
|