Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(SDK): fix model ppl unexpected index error #1553

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions client/starwhale/api/_impl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,37 +232,44 @@ def _starwhale_internal_run_ppl(self) -> None:
for _idx, _data, _annotations in loader:
cnt += 1
_start = time.time()
result: t.Any = b""
exception = None
_unique_id = f"{_uri.object}_{_idx}"
_result: t.Any = b""
_exception = None
_idx_with_ds = f"{_uri.object}_#@#_{_idx}"
try:
# TODO: inspect profiling
result = self.ppl(_data, annotations=_annotations, index=_unique_id)
_result = self.ppl(
_data,
annotations=_annotations,
index=_idx,
index_with_dataset=_idx_with_ds,
)
except Exception as e:
exception = e
self._sw_logger.exception(f"[{_unique_id}] data handle -> failed")
_exception = e
self._sw_logger.exception(f"[{_idx_with_ds}] data handle -> failed")
if not self.ignore_error:
self._update_status(STATUS.FAILED)
raise
else:
exception = None
_exception = None

self._sw_logger.debug(
f"[{_unique_id}] use {time.time() - _start:.3f}s, session-id:{self.context.version} @{self.context.step}-{self.context.index}"
f"[{_idx_with_ds}] use {time.time() - _start:.3f}s, session-id:{self.context.version} @{self.context.step}-{self.context.index}"
)

self._timeline_writer.write(
{
"time": now_str(),
"status": exception is None,
"exception": str(exception),
"index": f"{_unique_id}",
"status": _exception is None,
"exception": str(_exception),
"index": _idx,
"index_with_dataset": _idx_with_ds,
}
)

result_storage.save(
data_id=f"{_unique_id}",
result=result,
data_id=_idx_with_ds,
index=_idx,
result=_result,
annotations={} if self.ignore_annotations else _annotations,
)

Expand Down
12 changes: 9 additions & 3 deletions example/PennFudanPed/pfp/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ def __init__(self) -> None:
super().__init__()

@torch.no_grad()
def ppl(self, img: Image, index: str, **kw):
index = index.split("_")[-1]
def ppl(self, img: Image, index: t.Union[int, str], **kw):
if isinstance(index, str) and "_" in index and index.startswith("dataset-"):
tianweidut marked this conversation as resolved.
Show resolved Hide resolved
# v0.3.2 SDK index contains dataset name-version prefix, such as: 'dataset-pfp-small-d2zbajpbvotc7g7qwbev7lhqwvvu4k33qj5pehkf_PNGImages/FudanPed00001.png'
# other versions index is the origin data row index
index = index.split("_")[-1]

_img = PILImage.open(io.BytesIO(img.to_bytes())).convert("RGB")
_tensor = functional.to_tensor(_img).to(self.device)
output = self.model(torch.stack([_tensor]))
return index, self._post(index, output[0])

def _post(self, index: str, pred: t.Dict[str, torch.Tensor]) -> t.Dict[str, t.Any]:
def _post(
self, index: t.Union[int, str], pred: t.Dict[str, torch.Tensor]
) -> t.Dict[str, t.Any]:
output = {}
pred = {k: v.cpu() for k, v in pred.items()}
for typ in self.iou_types:
Expand Down