MCPcopy Create free account
hub / github.com/InternRobotics/G2VLM / __iter__

Method __iter__

data/vlm_dataset.py:92–214  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

90 return elements
91
92 def __iter__(self):
93 data_paths_per_worker, worker_id = self.get_data_paths_per_worker()
94 if self.data_status is not None:
95 row_start_id = self.data_status[worker_id] + 1
96 else:
97 row_start_id = 0
98 transform_stride = self.vit_transform.stride
99
100 print(
101 f"rank-{self.local_rank} worker-{worker_id} dataset-{self.dataset_name}: "
102 f"resuming data at row#{row_start_id}"
103 )
104
105 while True:
106 data_paths_per_worker_ = data_paths_per_worker[row_start_id:]
107 for row_idx, (data, image_dir) in enumerate(data_paths_per_worker_, start=row_start_id):
108 num_tokens = 0
109 image_tensor_list = []
110 text_ids_list = []
111 sequence_plan = []
112 image_grid_thw_list = []
113
114 try:
115 data_item = json.loads(data)
116 raw_images = None
117 if 'image' in data_item:
118 if type(data_item['image']) == list:
119 raw_images = [
120 pil_img2rgb(Image.open(os.path.join(image_dir, image)))
121 for image in data_item['image']
122 ]
123 special_tokens = '<vit_image>' * len(raw_images)
124 for item in data_item['conversations']:
125 if item['from'] == "human":
126 if "<video>" in item['value']:
127 item['value'] = item['value'].replace("<video>", special_tokens).strip()
128 else:
129 item['value'] = special_tokens + " " + item['value']
130 break
131
132 else:
133 raw_images = [
134 pil_img2rgb(Image.open(os.path.join(image_dir, data_item['image'])))
135 ]
136 special_tokens = '<vit_image>' * len(raw_images)
137 for item in data_item['conversations']:
138 if item['from'] == "human":
139 if "<video>" in item['value']:
140 item['value'] = item['value'].replace("<video>", special_tokens).strip()
141 else:
142 item['value'] = special_tokens + item['value']
143 break
144 elif 'video' in data_item:
145 raw_images = self.frame_sampler(os.path.join(image_dir, data_item['video']))
146 special_tokens = '<vit_image>' * len(raw_images)
147 for item in data_item['conversations']:
148 if '<video>' in item['value']:
149 item['value'] = item['value'].replace('<video>', special_tokens)

Callers

nothing calls this directly

Calls 5

pil_img2rgbFunction · 0.85
apply_template_qwenvl2Function · 0.85
openMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected