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

Train and inference with different image data types #2535

Merged
merged 6 commits into from
Dec 9, 2022

Conversation

suzhoum
Copy link
Contributor

@suzhoum suzhoum commented Dec 8, 2022

Issue #, if available:

Description of changes:
This PR combines current different image modalities (image_path and image_bytearray) into single image modality. This supports image training and inference with different feature types.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@suzhoum suzhoum force-pushed the single_image_modality branch 3 times, most recently from 2716445 to a182803 Compare December 8, 2022 16:19
@@ -131,6 +131,33 @@ def is_rois_column(data: pd.Series) -> bool:
return is_rois_input(data[idx])


def is_image_path(feature: Any):
is_path = True
image_paths = str(feature).split(";")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering why we need to split by ";"? Is there any upstream logics introducing it or some assumptions made?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there is an existing upstream logic for image_path that we use ";" to separate a list of images.

Comment on lines 234 to 257
def _is_image_path(self, feature: Any):
is_path = True
image_paths = str(feature).split(";")
for img_path in image_paths:
try:
with PIL.Image.open(img_path) as img:
pass
break
except:
is_path = False
return is_path

def _is_image_bytearray(self, feature: Any):
is_bytearray = True
if not isinstance(feature, list):
feature = [feature]
for img_bytearray in feature:
try:
with PIL.Image.open(BytesIO(img_bytearray)) as img:
pass
break
except:
is_bytearray = False
return is_bytearray
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 functions seems to be duplicate of what defined in infer_types.py. Better to consolidate and reuse

Copy link
Contributor Author

@suzhoum suzhoum Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the intention was to add these utility function inside./multimodal/data/utils.py or ./multimodal/utils/data.py, but they both depend on MultiModalFeaturePreprocessor which creates circular dependency. Unless we want to create a new utility function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we let infer_types.py use the function defined here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would require us to initialize a MultiModalFeaturePreprocessor with variables that are not readily available, and potentially introduce unstable dependencies since there are files that import both infer_types and MultiModalFeaturePreprocessor, e.g.

from .data.infer_types import (
infer_column_types,
infer_label_column_type_by_problem_type,
infer_problem_type_output_shape,
infer_rois_column_type,
)
from .data.preprocess_dataframe import MultiModalFeaturePreprocessor

@suzhoum suzhoum force-pushed the single_image_modality branch 3 times, most recently from 809eae3 to acf4464 Compare December 8, 2022 21:10
@suzhoum suzhoum marked this pull request as ready for review December 8, 2022 21:20
data = data.tolist()
image_type = get_image_feature_type(data.iloc[0])
if image_type == IMAGE_PATH:
data = data.apply(lambda ele: str(ele).split(";")).tolist()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use vectorized form: data.str.split(';').tolist()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to distinguish image_path and image_bytearray in the column types. These image sub-types would be used in df preprocessor and image processor. The current modification breaks the original logic.

The problem is that there is a difference of usage between training and inference time. We can train using images on disk and then use bytearrays during the inference. Saving files to a file system during the inference time creates additional security overhead.

I agree that we need to support using image_path in training and image_bytearray in inference. To do so, we can infer the image column types during inference and see whether the subtype changes. If we detect subtype changes, we can modify the _column_types in df preprocessor to reflect them. There is no need to change the internal logic among infer_types, df preprocessor, and processor.

Thanks for the suggestion. I'm working on a quick POC for this idea, and hopefully we can still catch the release.

Copy link
Contributor

@zhiqiangdon zhiqiangdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to distinguish image_path and image_bytearray in the column types. These image sub-types would be used in df preprocessor and image processor. The current modification breaks the original logic.

@gradientsky
Copy link
Contributor

gradientsky commented Dec 8, 2022

We need to distinguish image_path and image_bytearray in the column types. These image sub-types would be used in df preprocessor and image processor. The current modification breaks the original logic.

The problem is that there is a difference of usage between training and inference time. We can train using images on disk and then use bytearrays during the inference. Saving files to a file system during the inference time creates additional security overhead.

@github-actions
Copy link

github-actions bot commented Dec 8, 2022

Job PR-2535-b8a52d8 is done.
Docs are uploaded to http://autogluon-staging.s3-website-us-west-2.amazonaws.com/PR-2535/b8a52d8/index.html

@zhiqiangdon
Copy link
Contributor

We need to distinguish image_path and image_bytearray in the column types. These image sub-types would be used in df preprocessor and image processor. The current modification breaks the original logic.

The problem is that there is a difference of usage between training and inference time. We can train using images on disk and then use bytearrays during the inference. Saving files to a file system during the inference time creates additional security overhead.

I agree that we need to support using image_path in training and image_bytearray in inference. To do so, we can infer the image column types during inference and see whether the subtype changes. If we detect subtype changes, we can modify the _column_types in df preprocessor to reflect them. There is no need to change the internal logic among infer_types, df preprocessor, and processor.

@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Job PR-2535-b16626f is done.
Docs are uploaded to http://autogluon-staging.s3-website-us-west-2.amazonaws.com/PR-2535/b16626f/index.html

@suzhoum suzhoum force-pushed the single_image_modality branch 2 times, most recently from 441ce57 to b240ce3 Compare December 9, 2022 02:55
@suzhoum suzhoum changed the title Single image modality Train and inference with different image data types Dec 9, 2022
@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Job PR-2535-b240ce3 is done.
Docs are uploaded to http://autogluon-staging.s3-website-us-west-2.amazonaws.com/PR-2535/b240ce3/index.html

@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Job PR-2535-441ce57 is done.
Docs are uploaded to http://autogluon-staging.s3-website-us-west-2.amazonaws.com/PR-2535/441ce57/index.html

Copy link
Contributor

@zhiqiangdon zhiqiangdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Consider using the updated column_types to explicitly replace the df_preprocessor._column_types to make logic more clear.

@tonyhoo
Copy link
Collaborator

tonyhoo commented Dec 9, 2022

LGTM! Consider using the updated column_types to explicitly replace the df_preprocessor._column_types to make logic more clear.

Wondering what the current behavior would be if the df_preprocessor._column_types not in sync

@suzhoum suzhoum force-pushed the single_image_modality branch 2 times, most recently from 437530d to a67ff50 Compare December 9, 2022 07:55
@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Job PR-2535-8cb0dda is done.
Docs are uploaded to http://autogluon-staging.s3-website-us-west-2.amazonaws.com/PR-2535/8cb0dda/index.html

@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Job PR-2535-725f115 is done.
Docs are uploaded to http://autogluon-staging.s3-website-us-west-2.amazonaws.com/PR-2535/725f115/index.html

@suzhoum suzhoum merged commit 96917d1 into autogluon:master Dec 9, 2022
@suzhoum suzhoum added this to the 0.7 Release milestone Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants