-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: style transfer with openCV(ios) #48
Conversation
## 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
## 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. -->
src/StyleTransfer.ts
Outdated
setIsModelReady(false); | ||
await StyleTransfer.loadModule(path); | ||
} catch (e) { | ||
setError(getError(e)); | ||
} finally { | ||
setIsModelLoading(false); | ||
setIsModelReady(true); |
There was a problem hiding this comment.
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
src/StyleTransfer.ts
Outdated
if (error) { | ||
throw new Error(error); | ||
} | ||
|
There was a problem hiding this comment.
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.
cv::Mat encodedData(1, [data length], CV_8UC1, (void *)data.bytes); | ||
inputImage = cv::imdecode(encodedData, cv::IMREAD_COLOR); | ||
} | ||
if([[url scheme] isEqualToString: @"file"]){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if([[url scheme] isEqualToString: @"file"]){ | |
else if([[url scheme] isEqualToString: @"file"]){ |
[array addObject:number]; | ||
} | ||
cv::Size modelImageSize = [self getModelImageSize]; | ||
cv::resize(input, input, modelImageSize); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
?
src/StyleTransfer.ts
Outdated
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const [isModelReady, setIsModelReady] = useState(true); | |
const [isModelReady, setIsModelReady] = useState(false); |
@@ -30,33 +30,34 @@ export const useStyleTransfer = ({ | |||
} | |||
|
|||
try { | |||
setIsModelLoading(true); | |||
await StyleTransfer.loadModule(path); |
There was a problem hiding this comment.
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
## 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. -->
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
Tested on
Testing instructions
Screenshots
Related issues
Checklist
Additional notes