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

feat: style transfer with openCV(ios) #48

Merged

Conversation

NorbertKlockiewicz
Copy link
Contributor

@NorbertKlockiewicz NorbertKlockiewicz commented Dec 10, 2024

Description

This PR introduces implementation for style transfer module which is using opencv under the hood, it allows to load images from external sources, device storage and base64.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (improves or adds clarity to existing documentation)

Tested on

  • iOS
  • Android

Testing instructions

Screenshots

Related issues

Checklist

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional notes

## Description
This pull request introduces example app for computer vision task, which
is similar to llama example, for now it only has one screen
implemented(Style Transfer)

### Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Documentation update (improves or adds clarity to existing
documentation)

### Tested on
- [x] iOS
- [x] Android

### Testing instructions
<!-- Provide step-by-step instructions on how to test your changes.
Include setup details if necessary. -->

### Screenshots
<!-- Add screenshots here, if applicable -->

### Related issues
<!-- Link related issues here using #issue-number -->

### Checklist
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have updated the documentation accordingly
- [x] My changes generate no new warnings

### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
…d image resource from base64, storage and external sources
@NorbertKlockiewicz NorbertKlockiewicz changed the title Style Transfer Implementation with opencv feat: style transfer with openCV(ios) Dec 12, 2024
@NorbertKlockiewicz NorbertKlockiewicz marked this pull request as ready for review December 12, 2024 10:45
## Description
This pull request introduces example app for computer vision task, which
is similar to llama example, for now it only has one screen
implemented(Style Transfer)

### Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Documentation update (improves or adds clarity to existing
documentation)

### Tested on
- [x] iOS
- [x] Android

### Testing instructions
<!-- Provide step-by-step instructions on how to test your changes.
Include setup details if necessary. -->

### Screenshots
<!-- Add screenshots here, if applicable -->

### Related issues
<!-- Link related issues here using #issue-number -->

### Checklist
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have updated the documentation accordingly
- [x] My changes generate no new warnings

### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
## Description
Turbo module which was handling running of llama models was named
RnExecutorchModule which wasn't really clear and conflicted with name of
library, it's now changed to LLM, matching the style of StyleTransfer,
ObjectDetection etc.

### Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Documentation update (improves or adds clarity to existing
documentation)

### Tested on
- [x] iOS
- [x] Android

### Testing instructions
<!-- Provide step-by-step instructions on how to test your changes.
Include setup details if necessary. -->

### Screenshots
<!-- Add screenshots here, if applicable -->

### Related issues
<!-- Link related issues here using #issue-number -->

### Checklist
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have updated the documentation accordingly
- [ ] My changes generate no new warnings

### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
Comment on lines 33 to 38
setIsModelReady(false);
await StyleTransfer.loadModule(path);
} catch (e) {
setError(getError(e));
} finally {
setIsModelLoading(false);
setIsModelReady(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks a bit off to me, why do we set isModelReady to true even if an error occurs? I think it is more intuitive to treat isModelReady like a superset of isModelLoadedSuccessfully.
Also on this subject, change default value for isModelReady to false and only set it to true after loading succeeds.
Also perhaps we should move setIsModelReady(false) outside of the async block to avoid any possible weird race conditions when forward is called before isModelReady is set to false

Comment on lines 50 to 53
if (error) {
throw new Error(error);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be necessary if we keep invariant that: isModelReady is true only if error is null.

src/StyleTransfer.ts Show resolved Hide resolved
src/StyleTransfer.ts Show resolved Hide resolved
cv::Mat encodedData(1, [data length], CV_8UC1, (void *)data.bytes);
inputImage = cv::imdecode(encodedData, cv::IMREAD_COLOR);
}
if([[url scheme] isEqualToString: @"file"]){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if([[url scheme] isEqualToString: @"file"]){
else if([[url scheme] isEqualToString: @"file"]){

[array addObject:number];
}
cv::Size modelImageSize = [self getModelImageSize];
cv::resize(input, input, modelImageSize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems wrong, what if the input is smaller than modelImageSize?

Copy link
Collaborator

Choose a reason for hiding this comment

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

never mind this, I thought initially this is an array pointer and not a cv::Mat object. Anyway since this is not performed 'inplace' due to the above reason I would consider creating another cv::Mat object output for better readability

- (cv::Mat)postprocess:(NSArray *)output {
cv::Size modelImageSize = [self getModelImageSize];
cv::Mat processedImage = [ImageProcessor arrayToMat: output width:modelImageSize.width height:modelImageSize.height];
cv::resize(processedImage, processedImage, originalSize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, what if the originalSize is larger than modelImageSize?

@@ -18,7 +18,7 @@ export const useStyleTransfer = ({
modulePath,
}: Props): StyleTransferModule => {
const [error, setError] = useState<null | string>(null);
const [isModelLoading, setIsModelLoading] = useState(true);
const [isModelReady, setIsModelReady] = useState(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const [isModelReady, setIsModelReady] = useState(true);
const [isModelReady, setIsModelReady] = useState(false);

@@ -30,33 +30,34 @@ export const useStyleTransfer = ({
}

try {
setIsModelLoading(true);
await StyleTransfer.loadModule(path);
Copy link
Collaborator

Choose a reason for hiding this comment

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

add setIsModelReady(false); here, in case modulePath prop changes

@NorbertKlockiewicz NorbertKlockiewicz merged commit f556b8a into main Dec 16, 2024
2 checks passed
@NorbertKlockiewicz NorbertKlockiewicz deleted the @norbertklockiewicz/style-transfer-implementation branch December 16, 2024 12:55
@mkopcins mkopcins restored the @norbertklockiewicz/style-transfer-implementation branch December 16, 2024 13:22
@mkopcins mkopcins deleted the @norbertklockiewicz/style-transfer-implementation branch December 16, 2024 13:35
chmjkb pushed a commit that referenced this pull request Dec 17, 2024
## Description
This PR introduces implementation for style transfer module which is
using opencv under the hood, it allows to load images from external
sources, device storage and base64.

### Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Documentation update (improves or adds clarity to existing
documentation)

### Tested on
- [x] iOS
- [ ] Android

### Testing instructions
<!-- Provide step-by-step instructions on how to test your changes.
Include setup details if necessary. -->

### Screenshots
<!-- Add screenshots here, if applicable -->

### Related issues
<!-- Link related issues here using #issue-number -->

### Checklist
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have updated the documentation accordingly
- [x] My changes generate no new warnings

### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
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.

2 participants