Skip to content

Commit

Permalink
fix: Handle single image files in pipeline
Browse files Browse the repository at this point in the history
Fix thumbnailing endpoint and model inference wrapper's logic to
correctly process single image files (as well as PDFs). Fixes #18.
Relates to #5.

Co-authored-by: David <40301721+d-v-dlee@users.noreply.github.com>
  • Loading branch information
athewsey and d-v-dlee committed Jul 7, 2022
1 parent 346683d commit de7ac69
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions notebooks/preproc/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ def process_doc_in_worker(inputs: dict):
# MIME type constants:
SINGLE_IMAGE_CONTENT_TYPES = {
"image/jpeg": "JPG",
"image/jpg": "JPG",
"image/png": "PNG",
}
MULTI_IMAGE_CONTENT_TYPES = {
Expand Down
10 changes: 5 additions & 5 deletions notebooks/src/code/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def input_fn(input_bytes, content_type: str):
if "images" in thumbnails:
thumbnails = thumbnails["images"]
elif "image" in thumbnails:
thumbnails = thumbnails["image"]
thumbnails = np.expand_dims(thumbnails["image"], axis=0)
else:
raise ValueError(
"Page thumbnails archive for request did not contain either 'images' or "
Expand All @@ -256,7 +256,7 @@ def input_fn(input_bytes, content_type: str):
with io.BytesIO(b) as imgio:
thmbs.append(PIL.Image.open(imgio).copy())
thumbnails = thmbs
elif len(thumbnails.shape) != 4:
elif thumbnails.ndim != 4:
logger.warning(
"Thumbnails expected either array of PNG bytestrings or 4D images array. "
f"Got shape {thumbnails.shape}"
Expand All @@ -266,7 +266,7 @@ def input_fn(input_bytes, content_type: str):
# Again closing the BytesIOs without breaking PIL.Image:
with io.BytesIO(thumbnails[page_num - 1]) as imgio:
thumbnails = [PIL.Image.open(imgio).copy()]
elif len(thumbnails.shape) != 4:
elif thumbnails.ndim != 4:
logger.warning(
"Thumbnails expected either array of PNG bytestrings or 4D images array. "
f"Got shape {thumbnails.shape}"
Expand Down Expand Up @@ -425,7 +425,7 @@ def predict_fn(input_data: dict, model: dict):
tokenizer_params = set(signature(tokenizer).parameters)
collate_fn = lambda batch: collator(batch)

if processor and not images:
if processor and (images is None):
warns.append(
f"SageMaker model's preprocessor ({type(processor)}) expects page images (as "
".S3Thumbnails.{Bucket, Key} numpy array pointer in the request) but none were "
Expand All @@ -442,7 +442,7 @@ def predict_fn(input_data: dict, model: dict):
"text": words_by_page,
"block-ids": word_block_ids_by_page,
"boxes": boxes_by_page,
**({"images": images} if images and processor else {}),
**({"images": images} if processor and (images is not None) else {}),
},
tokenizer=tokenizer,
max_seq_len=max_seq_len - 2, # (Leave room for CLS and SEP)
Expand Down

0 comments on commit de7ac69

Please sign in to comment.