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

deduplicate annotations in pipeline [WIP] #438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions src/pytorch_ie/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
TaskModule,
TaskOutput,
)
from pytorch_ie.utils.document import deduplicate_annotations

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -243,18 +244,22 @@ def postprocess(
self,
model_inputs: Sequence[TaskEncoding],
model_outputs: Sequence[TaskOutput],
deduplicate_annotations: bool = False,
**postprocess_parameters,
) -> Sequence[Document]:
"""
Postprocess will receive the model inputs and (unbatched) model outputs and reformat them into
something more friendly. Generally it will output a list of documents.
"""
# This creates annotations from the model outputs and attaches them to the correct documents.
return self.taskmodule.decode(
result = self.taskmodule.decode(
task_encodings=model_inputs,
task_outputs=model_outputs,
**postprocess_parameters,
)
if deduplicate_annotations:
result = [document.deduplicate_annotations() for document in result]
return result

def get_inference_context(self):
inference_context = (
Expand Down Expand Up @@ -308,12 +313,6 @@ def __call__(
postprocess_params,
) = self._sanitize_parameters(**kwargs)

in_place: bool = postprocess_params.get("inplace", True)
if in_place and not isinstance(documents, (MutableSequence, Document)):
raise InplaceNotSupportedException(
"Immutable sequences of Documents (such as Datasets) can't be modified in place. Please set inplace=False."
)

if "TOKENIZERS_PARALLELISM" not in os.environ:
logger.info(
"Disabling tokenizer parallelism, we're using DataLoader multithreading already"
Expand All @@ -326,6 +325,16 @@ def __call__(
forward_params = {**self._forward_params, **forward_params}
postprocess_params = {**self._postprocess_params, **postprocess_params}

in_place: bool = postprocess_params.get("inplace", True)
if in_place and not isinstance(documents, (MutableSequence, Document)):
raise InplaceNotSupportedException(
"Immutable sequences of Documents (such as Datasets) can't be modified in place. Please set inplace=False."
)
if postprocess_params.get("deduplicate_annotations", False) and in_place:
raise ValueError(
"Deduplicating annotations requires inplace=False. Please set inplace=False."
)

single_document = False
if isinstance(documents, Document):
single_document = True
Expand Down
Loading