File size: 12,050 Bytes
0453c63 | 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | import glob
import json
import os
import random
import cv2
import numpy as np
import torch
import torch.nn.functional as F
from transformers import CLIPImageProcessor
from model.llava import conversation as conversation_lib
from model.segment_anything.utils.transforms import ResizeLongestSide
from .data_processing import get_mask_from_json
from .utils import (ANSWER_LIST, DEFAULT_IMAGE_TOKEN,
EXPLANATORY_QUESTION_LIST, LONG_QUESTION_LIST,
SHORT_QUESTION_LIST)
from PIL import Image
import pickle
AFFORDANCE_QUESTION_LIST = [
DEFAULT_IMAGE_TOKEN + "\n" + "Can you segment the affordance map of {class_name} in this image?",
DEFAULT_IMAGE_TOKEN + "\n" + "Please segment the affordance map of {class_name} in this image.",
DEFAULT_IMAGE_TOKEN
+ "\n"
+ "What is the affordance map of {class_name} in this image? Please respond with segmentation mask.",
DEFAULT_IMAGE_TOKEN
+ "\n"
+ "What is the affordance map of {class_name} in this image? Please output segmentation mask.",
]
class ReasonAffDataset(torch.utils.data.Dataset):
pixel_mean = torch.Tensor([123.675, 116.28, 103.53]).view(-1, 1, 1)
pixel_std = torch.Tensor([58.395, 57.12, 57.375]).view(-1, 1, 1)
img_size = 1024
ignore_label = 255
def __init__(
self,
base_image_dir,
tokenizer,
vision_tower,
samples_per_epoch=500 * 8 * 2 * 10,
precision: str = "fp32",
image_size: int = 224,
num_classes_per_sample: int = 3,
exclude_val=False,
reason_aff_data="handal_hard_reasoning",
reason_aff_sample_ratio=[1],
explanatory=0.1,
):
self.exclude_val = exclude_val
self.reason_aff_data = reason_aff_data
reason_aff_sample_ratio = np.array(reason_aff_sample_ratio)
self.reason_aff_sample_ratio = reason_aff_sample_ratio / reason_aff_sample_ratio.sum()
self.samples_per_epoch = samples_per_epoch
self.explanatory = explanatory
self.num_classes_per_sample = num_classes_per_sample
self.base_image_dir = base_image_dir
self.image_size = image_size
self.tokenizer = tokenizer
self.precision = precision
self.transform = ResizeLongestSide(image_size)
self.clip_image_processor = CLIPImageProcessor.from_pretrained(vision_tower)
self.short_question_list = SHORT_QUESTION_LIST
self.affordance_question_list = AFFORDANCE_QUESTION_LIST
self.long_question_list = LONG_QUESTION_LIST
self.answer_list = ANSWER_LIST
reason_aff_datas = reason_aff_data.split("||")
self.data2list = {}
self.object_ids = {}
for ds in reason_aff_datas:
if ds == "handal_hard_reasoning" or ds == "egoobjects_easy_reasoning" or ds == "egoobjects_hard_reasoning":
pkl_path = os.path.join(base_image_dir, f'{ds}_train.pkl')
images = {}
labels = {}
questions = {}
answers = {}
with open(pkl_path, 'rb') as f:
aff_datas = pickle.load(f)
for aff_data in aff_datas:
if aff_data['task_object_class'] not in images:
images[aff_data['task_object_class']] = []
labels[aff_data['task_object_class']] = []
questions[aff_data['task_object_class']] = []
answers[aff_data['task_object_class']] = []
images[aff_data['task_object_class']].append(aff_data['frame_path'])
labels[aff_data['task_object_class']].append(aff_data['mask_path'])
questions[aff_data['task_object_class']].append(aff_data['question'])
answers[aff_data['task_object_class']].append(aff_data['answer'])
# keep same numbers of samples for each class
for k in images.keys():
assert len(images[k]) == len(labels[k])
self.data2list[ds] = (images, labels, questions, answers)
print(f"categories of {ds}: ", images.keys())
print(f"number of {ds} samples: ", len(aff_datas))
else:
raise ValueError(f"Unsupported affordance segmentation dataset: {ds}")
def __len__(self):
return self.samples_per_epoch
def preprocess(self, x: torch.Tensor) -> torch.Tensor:
"""Normalize pixel values and pad to a square input."""
# Normalize colors
x = (x - self.pixel_mean) / self.pixel_std
# Pad
h, w = x.shape[-2:]
padh = self.img_size - h
padw = self.img_size - w
x = F.pad(x, (0, padw, 0, padh))
return x
def __getitem__(self, idx):
ds = np.random.choice(list(self.data2list.keys()), p=self.reason_aff_sample_ratio)
images, labels, my_questions, my_answers = self.data2list[ds]
class_name = random.choice(list(images.keys()))
idx = random.randint(0, len(images[class_name]) - 1)
image_path = images[class_name][idx]
label_path = labels[class_name][idx]
my_question = my_questions[class_name][idx]
my_answer = my_answers[class_name][idx]
# load image and prepare input for clip and sam
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
ori_size = image.shape[:2]
# preprocess image for clip
image_clip = self.clip_image_processor.preprocess(image, return_tensors="pt")[
"pixel_values"
][0]
image = self.transform.apply_image(image) # preprocess image for sam
resize = image.shape[:2]
image = self.preprocess(torch.from_numpy(image).permute(2, 0, 1).contiguous())
# load class names
sampled_classes = [class_name]
# load label
label = Image.open(label_path)
label = np.array(label)
label = torch.from_numpy(label).long()
masks = []
if ds == 'graspnet':
object_id = self.object_ids[ds][class_name][idx]
# if data is from graspnet and object_id exists, use the mask of the object_id
if object_id is None:
for _ in range(len(sampled_classes)):
masks.append(label > 0)
else:
for _ in range(len(sampled_classes)):
masks.append(label == object_id)
else:
for _ in range(len(sampled_classes)):
masks.append(label > 0)
masks = torch.stack(masks, dim=0)
questions = []
answers = []
for sampled_cls in sampled_classes:
text = sampled_cls
# assert len(text.split("||")) == 1
# question_template = random.choice(self.affordance_question_list)
# questions.append(question_template.format(class_name=text.lower()))
#
# answers.append(random.choice(self.answer_list))
questions.append(DEFAULT_IMAGE_TOKEN + "\n" + "You are an embodied robot. " + my_question)
# answers.append(my_answer + " [SEG].")
answers.append(my_answer + " [AFF].")
conversations = []
conv = conversation_lib.default_conversation.copy()
i = 0
while i < len(questions):
conv.messages = []
conv.append_message(conv.roles[0], questions[i])
conv.append_message(conv.roles[1], answers[i])
conversations.append(conv.get_prompt())
i += 1
return (
image_path,
image,
image_clip,
conversations,
masks,
label,
resize,
questions,
sampled_classes,
)
class ReasonAffValDataset(torch.utils.data.Dataset):
pixel_mean = torch.Tensor([123.675, 116.28, 103.53]).view(-1, 1, 1)
pixel_std = torch.Tensor([58.395, 57.12, 57.375]).view(-1, 1, 1)
img_size = 1024
ignore_label = 255
def __init__(
self,
base_image_dir,
tokenizer,
vision_tower,
val_dataset,
image_size=1024,
):
self.base_image_dir = base_image_dir.replace("/lisa_data", "")
# splits = val_dataset.split("|")
# ds, split = splits
ds = val_dataset
self.images = []
self.labels = []
self.questions = []
self.answers = []
self.class_ids = []
self.class_names = []
pkl_path = os.path.join(self.base_image_dir, f'{ds}_val.pkl')
with open(pkl_path, 'rb') as f:
reason_datas = pickle.load(f)
for reason_data in reason_datas:
# one image is broken in 3doi_easy_reasoning_val.pkl, so skip it
if 'EK_frame_0000040462.jpg' in reason_data['frame_path']:
continue
self.images.append(reason_data['frame_path'])
self.labels.append(reason_data['mask_path'])
self.questions.append(reason_data['question'])
self.answers.append(reason_data['answer'])
self.class_ids.append(None)
self.class_names.append(reason_data['task_object_class'])
self.ds = ds
self.image_size = image_size
self.tokenizer = tokenizer
self.transform = ResizeLongestSide(image_size)
self.clip_image_processor = CLIPImageProcessor.from_pretrained(vision_tower)
def __len__(self):
return len(self.images)
def preprocess(self, x: torch.Tensor) -> torch.Tensor:
"""Normalize pixel values and pad to a square input."""
# Normalize colors
x = (x - self.pixel_mean) / self.pixel_std
# Pad
h, w = x.shape[-2:]
padh = self.img_size - h
padw = self.img_size - w
x = F.pad(x, (0, padw, 0, padh))
return x
def __getitem__(self, idx):
# load image
image_path = self.images[idx]
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# preprocess image for clip
image_clip = self.clip_image_processor.preprocess(image, return_tensors="pt")[
"pixel_values"
][0]
# preprocess image for sam
image = self.transform.apply_image(image)
resize = image.shape[:2]
image = self.preprocess(torch.from_numpy(image).permute(2, 0, 1).contiguous())
# load class names
sampled_sents = [self.class_names[idx]]
# load label
label_path = self.labels[idx]
label = Image.open(label_path)
label = np.array(label)
label = torch.from_numpy(label).long()
masks = []
class_id = self.class_ids[idx]
# if data object_id exists, use the mask of the object_id
if class_id is None:
for _ in range(len(sampled_sents)):
masks.append(label > 0)
else:
for _ in range(len(sampled_sents)):
masks.append(label == class_id)
masks = torch.stack(masks, dim=0)
# load question and answer
my_question = self.questions[idx]
my_answer = self.answers[idx]
conversations = []
conv = conversation_lib.default_conversation.copy()
i = 0
while i < len(sampled_sents):
conv.messages = []
text = sampled_sents[i].strip()
conv.append_message(
conv.roles[0],
DEFAULT_IMAGE_TOKEN + "\n" + "You are an embodied robot. " + "{}".format(my_question),
)
conv.append_message(conv.roles[1], my_answer + " [AFF].")
conversations.append(conv.get_prompt())
i += 1
inference = True
return (
image_path,
image,
image_clip,
conversations,
masks,
label,
resize,
None,
None,
inference,
) |