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

Improves TranslatorExpansions with pre-processing and post-processing #2213

Merged
merged 1 commit into from
Dec 8, 2022

Conversation

zachgk
Copy link
Contributor

@zachgk zachgk commented Dec 7, 2022

This provides an improvement to the TranslatorExpansions model. Now, it includes three types of expansions: pre-processing expansions, post-processing expansions, and full translator expansions. Before, it only had the full translator expansions.

The key improvement is that any combination of pre-processing expansion and post-processing expansion can be used. So, if there are 5 pre-processing and 3 post-processing then it will be 15 expansions. It also follows a standard tendency to handle the pre-processing and post-processing separately as seen with classes like the BaseImageTranslatorFactory.

Let me talk about the model for the expansion a bit. The translator can be
thought of as the composition of 5 different functions: pre-processing ->
batchify -> model -> unbatchify -> post-process.

With an expansion, it increases this to 7: pre-processing expansion ->
pre-processing -> batchify -> model -> unbatchify -> post-process -> post-processing expansion.

The weakness of this implementation is that it also limits it to 7. For example, we can use a Translator<InputStream -> Classifications> and expand the pre-processing to form the Translator<Url -> Classifications>. With this model, there is no built-in way to do this. That being said, this manifests mostly in increased work on the side of writing expansions rather than a loss in functionality. You would just have to use the InputStreamImagePreProcessor as part of the UrlImagePreProcessor or otherwise write it from scratch. In exchange for increased simplicity over a more complete model, I think it is a worthwhile tradeoff.

Following this creation, there are a few other changes which I will list here for ease of reading:

  1. It creates a BasicTranslator which simply composes a separate PreProcessor and PostProcessor. It is a useful utility in it's own right
  2. Moves the FileTranslator, UrlTranslator, and InputStreamTranslator into just a PreProcessor
  3. Uses the new ExpansionFactory to update the BaseImageTranslatorFactory and apply it where possible

This provides an improvement to the TranslatorExpansions model. Now, it includes
three types of expansions: pre-processing expansions, post-processing
expansions, and full translator expansions. Before, it only had the full
translator expansions.

The key improvement is that any combination of pre-processing expansion and
post-processing expansion can be used. So, if there are 5 pre-processing and 3
post-processing then it will be 15 expansions. It also follows a standard
tendency to handle the pre-processing and post-processing separately as seen
with classes like the BaseImageTranslatorFactory.

Let me talk about the model for the expansion a bit. The translator can be
thought of as the composition of 5 different functions: pre-processing ->
batchify -> model -> unbatchify -> post-process.

With an expansion, it increases this to 7: pre-processing expansion ->
pre-processing -> batchify -> model -> unbatchify -> post-process ->
post-processing expansion.

The weakness of this implementation is that it also limits it to 7. For example,
we can use a Translator<InputStream -> Classifications> and expand the
pre-processing to form the Translator<Url -> Classifications>. With this model,
there is no built-in way to do this. That being said, this manifests mostly in
increased work on the side of writing expansions rather than a loss in
functionality. You would just have to use the InputStreamImagePreProcessor as
part of the UrlImagePreProcessor or otherwise write it from scratch. In exchange
for increased simplicity over a more complete model, I think it is a worthwhile
tradeoff.

Following this creation, there are a few other changes which I will list here
for ease of reading:
1. It creates a BasicTranslator which simply composes a
separate PreProcessor and PostProcessor. It is a useful utility in it's own
right
2. Moves the FileTranslator, UrlTranslator, and InputStreamTranslator into just
a PreProcessor
3. Uses the new ExpansionFactory to update the BaseImageTranslatorFactory and
apply it where possible
@zachgk zachgk requested a review from frankfliu as a code owner December 7, 2022 00:19
@codecov-commenter
Copy link

Codecov Report

Base: 72.08% // Head: 71.62% // Decreases project coverage by -0.46% ⚠️

Coverage data is based on head (dcdd882) compared to base (bb5073f).
Patch coverage: 72.99% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2213      +/-   ##
============================================
- Coverage     72.08%   71.62%   -0.47%     
- Complexity     5126     6329    +1203     
============================================
  Files           473      631     +158     
  Lines         21970    27972    +6002     
  Branches       2351     2980     +629     
============================================
+ Hits          15838    20034    +4196     
- Misses         4925     6479    +1554     
- Partials       1207     1459     +252     
Impacted Files Coverage Δ
api/src/main/java/ai/djl/modality/cv/Image.java 69.23% <ø> (-4.11%) ⬇️
...rc/main/java/ai/djl/modality/cv/MultiBoxPrior.java 76.00% <ø> (ø)
...rc/main/java/ai/djl/modality/cv/output/Joints.java 71.42% <ø> (ø)
.../main/java/ai/djl/modality/cv/output/Landmark.java 100.00% <ø> (ø)
...main/java/ai/djl/modality/cv/output/Rectangle.java 72.41% <0.00%> (ø)
...i/djl/modality/cv/translator/BigGANTranslator.java 21.42% <0.00%> (-5.24%) ⬇️
.../modality/cv/translator/ImageFeatureExtractor.java 0.00% <0.00%> (ø)
.../ai/djl/modality/cv/translator/YoloTranslator.java 27.77% <0.00%> (+18.95%) ⬆️
...ain/java/ai/djl/modality/cv/util/NDImageUtils.java 59.21% <0.00%> (ø)
api/src/main/java/ai/djl/modality/nlp/Decoder.java 63.63% <ø> (ø)
... and 562 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@zachgk zachgk merged commit b6b7d68 into deepjavalibrary:master Dec 8, 2022
@zachgk zachgk deleted the expansions branch December 8, 2022 18:49
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.

3 participants