Переделал рекурсию на цикл.
Some checks are pending
Build/Publish Develop Docs / deploy (push) Waiting to run
Some checks are pending
Build/Publish Develop Docs / deploy (push) Waiting to run
This commit is contained in:
@@ -116,42 +116,57 @@ class SimpleDataSet(Dataset):
|
|||||||
ext_data.append(data)
|
ext_data.append(data)
|
||||||
return ext_data
|
return ext_data
|
||||||
|
|
||||||
|
def create_dummy_sample(self):
|
||||||
|
"""Создает пустой образец для предотвращения сбоев"""
|
||||||
|
dummy_img = np.zeros((32, 320, 3), dtype=np.uint8)
|
||||||
|
dummy_img = cv2.imencode('.jpg', dummy_img)[1].tobytes()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"image": dummy_img,
|
||||||
|
"label": "",
|
||||||
|
"img_path": "dummy.jpg",
|
||||||
|
"ext_data": self.get_ext_data(),
|
||||||
|
"filename": "dummy.jpg"
|
||||||
|
}
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
def __getitem__(self, idx):
|
||||||
file_idx = self.data_idx_order_list[idx]
|
max_attempts = 10
|
||||||
data_line = self.data_lines[file_idx]
|
for attempt in range(max_attempts):
|
||||||
try:
|
try:
|
||||||
data_line = data_line.decode("utf-8")
|
file_idx = self.data_idx_order_list[idx]
|
||||||
substr = data_line.strip("\n").split(self.delimiter)
|
data_line = self.data_lines[file_idx]
|
||||||
file_name = substr[0]
|
data_line = data_line.decode("utf-8")
|
||||||
file_name = self._try_parse_filename_list(file_name)
|
substr = data_line.strip("\n").split(self.delimiter)
|
||||||
label = substr[1]
|
file_name = substr[0]
|
||||||
img_path = os.path.join(self.data_dir, file_name)
|
file_name = self._try_parse_filename_list(file_name)
|
||||||
data = {"img_path": img_path, "label": label}
|
label = substr[1]
|
||||||
if not os.path.exists(img_path):
|
img_path = os.path.join(self.data_dir, file_name)
|
||||||
raise Exception("{} does not exist!".format(img_path))
|
data = {"img_path": img_path, "label": label}
|
||||||
with open(data["img_path"], "rb") as f:
|
if not os.path.exists(img_path):
|
||||||
img = f.read()
|
raise Exception("{} does not exist!".format(img_path))
|
||||||
data["image"] = img
|
with open(data["img_path"], "rb") as f:
|
||||||
data["ext_data"] = self.get_ext_data()
|
img = f.read()
|
||||||
data["filename"] = data["img_path"]
|
data["image"] = img
|
||||||
outs = transform(data, self.ops)
|
data["ext_data"] = self.get_ext_data()
|
||||||
except Exception as exc:
|
data["filename"] = data["img_path"]
|
||||||
print("HERE", str(exc))
|
outs = transform(data, self.ops)
|
||||||
self.logger.error(
|
return outs
|
||||||
"When parsing line {}, error happened with msg: {}".format(
|
|
||||||
data_line, traceback.format_exc()
|
except Exception as exc:
|
||||||
|
if attempt < max_attempts - 1:
|
||||||
|
idx = (
|
||||||
|
np.random.randint(self.__len__())
|
||||||
|
if self.mode == "train"
|
||||||
|
else (idx + 1) % self.__len__()
|
||||||
|
)
|
||||||
|
|
||||||
|
self.logger.error(
|
||||||
|
"Failed after {} attempts: {}".format(
|
||||||
|
max_attempts, str(exc)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
outs = None
|
return self.create_dummy_sample()
|
||||||
if outs is None:
|
|
||||||
# during evaluation, we should fix the idx to get same results for many times of evaluation.
|
|
||||||
rnd_idx = (
|
|
||||||
np.random.randint(self.__len__())
|
|
||||||
if self.mode == "train"
|
|
||||||
else (idx + 1) % self.__len__()
|
|
||||||
)
|
|
||||||
return self.__getitem__(rnd_idx)
|
|
||||||
return outs
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.data_idx_order_list)
|
return len(self.data_idx_order_list)
|
||||||
|
|||||||
Reference in New Issue
Block a user