-
Notifications
You must be signed in to change notification settings - Fork 68
feat: Instance Segmentation API #893
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
Merged
Merged
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
a03edb8
chore: Rename ImageSegmentation to SemanticSegmentation
benITo47 d210b2c
Update docs
benITo47 fa31fc3
Upadte claude skills
benITo47 c7877aa
Initial instance segmetntation
benITo47 544f539
Rename to BaseInstanceSegmentation
benITo47 93f5ae3
Update instance segmentaiton demo app
benITo47 eaa2abb
Update api reference docs
benITo47 8d01aa8
Add docs and fix some typechecks
benITo47 8353377
Add method unloading
benITo47 40e9ccf
Update gitignore
benITo47 ec227f9
Remove type field from postprocessor config
benITo47 13c0f95
Add ImageWithMasks component
benITo47 489bfb6
Change BaseInstanceSegmentation to accept and return string labels
benITo47 4c62b97
Remove redundant postprocessor config type
benITo47 f1a31fc
Change typedoc links
benITo47 85f0e71
Add docs
benITo47 f6c4207
Speed up mask processing
benITo47 f81cfc7
Return class index, tweak mask drawing performance
benITo47 67e804f
Add yolo specific coco labels
benITo47 27353d2
Fix yarn stuff
benITo47 b72dae4
Add tests
benITo47 5da2857
Add RFDetr-segmentation model, change preprocessorConfig types
benITo47 5dbbd4d
Update docs/docs/02-benchmarks/inference-time.md
benITo47 3178c6d
Update docs/docs/02-benchmarks/memory-usage.md
benITo47 a3e3522
Restore docs
benITo47 8e29394
Review fixes for InstanceSegmentationModule
benITo47 572fb60
Change typing in useInstanceSegmentation
benITo47 b4ed649
Split instance segmentation postprocessing, introduce CV utils
benITo47 1e647e4
Migrate ObjectDetection to common CV utils
benITo47 8bb3899
Fix links
benITo47 42e71e4
Break up postprocessing helpers in Instance Segmentation
benITo47 6d12f4b
Streamline postprocessing
benITo47 5bd7929
Change inheritance to VisionModel
benITo47 a84c610
Change inheritance to VisionModel
benITo47 e4f3ff0
Add live instance segmentation
benITo47 3453dde
Checkout upstream app
benITo47 9c2707d
Fix normalizing in InstanceSegmentation
benITo47 f4e07ec
Add missing brackets
benITo47 6f986f2
Strengthen typing
benITo47 0a71e41
Remove instanceId field
benITo47 9f74e90
Apply review suggestions
benITo47 b9438f7
Update docs
benITo47 5d1ed9f
Migrate vector<uint8> to OwningArrayBuffer
benITo47 39f3489
Apply revies suggestions
benITo47 2c9b69c
Change rfdetr naming, apply suggestions
benITo47 2d0e616
Apply suggestions
benITo47 406271b
Apply suggestions
benITo47 7b876df
Apply suggestions
benITo47 b71c041
Update docs
benITo47 058381a
Improve tests
benITo47 e7fa1a3
Update tests
benITo47 377728f
Readd accidentaly removed dep
benITo47 e157b4b
Revert formatting in run_test.sh
benITo47 fc681bf
Remove OD_live button
benITo47 d6a8637
Add Mateusz's patch
benITo47 e07cdab
Remove console.logs, change error type
benITo47 6255cf0
Fix docs and class filter
benITo47 ab997d8
Add benchmarks for RFDETR
benITo47 1600daf
Fix yarn issues
benITo47 800d3b0
Fix live task bug
benITo47 ab3c5cd
fix: type declaration order
benITo47 be20450
Fix declaration order
benITo47 b50b4fb
Stylish improvements
benITo47 18599af
Fix declaration order
benITo47 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,3 +102,4 @@ packages/react-native-executorch/common/rnexecutorch/tests/integration/assets/mo | |
| *.tgz | ||
| Makefile | ||
| *.pte | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
277 changes: 277 additions & 0 deletions
277
apps/computer-vision/app/instance_segmentation/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,277 @@ | ||
| import Spinner from '../../components/Spinner'; | ||
| import { BottomBar } from '../../components/BottomBar'; | ||
| import { getImage } from '../../utils'; | ||
| import { useInstanceSegmentation, YOLO26N_SEG } from 'react-native-executorch'; | ||
| import { | ||
| View, | ||
| StyleSheet, | ||
| ScrollView, | ||
| Text, | ||
| TouchableOpacity, | ||
| } from 'react-native'; | ||
| import React, { useContext, useEffect, useState } from 'react'; | ||
| import { GeneratingContext } from '../../context'; | ||
| import ScreenWrapper from '../../ScreenWrapper'; | ||
| import ImageWithMasks, { | ||
| buildDisplayInstances, | ||
| DisplayInstance, | ||
| } from '../../components/ImageWithMasks'; | ||
|
|
||
| export default function InstanceSegmentationScreen() { | ||
| const { setGlobalGenerating } = useContext(GeneratingContext); | ||
|
|
||
| const { | ||
| isReady, | ||
| isGenerating, | ||
| downloadProgress, | ||
| forward, | ||
| error, | ||
| getAvailableInputSizes, | ||
| } = useInstanceSegmentation({ | ||
| model: YOLO26N_SEG, | ||
| }); | ||
|
|
||
| const [imageUri, setImageUri] = useState(''); | ||
| const [imageSize, setImageSize] = useState({ width: 0, height: 0 }); | ||
| const [instances, setInstances] = useState<DisplayInstance[]>([]); | ||
| const [selectedInputSize, setSelectedInputSize] = useState<number | null>( | ||
| null | ||
| ); | ||
|
|
||
| const availableInputSizes = getAvailableInputSizes(); | ||
|
|
||
| useEffect(() => { | ||
| setGlobalGenerating(isGenerating); | ||
| }, [isGenerating, setGlobalGenerating]); | ||
|
|
||
| // Set default input size when model is ready | ||
| useEffect(() => { | ||
| if (isReady && availableInputSizes && availableInputSizes.length > 0) { | ||
| setSelectedInputSize(availableInputSizes[0]); | ||
| } | ||
| }, [isReady, availableInputSizes]); | ||
|
|
||
| const handleCameraPress = async (isCamera: boolean) => { | ||
| const image = await getImage(isCamera); | ||
| if (!image?.uri) return; | ||
| setImageUri(image.uri); | ||
| setImageSize({ | ||
| width: image.width ?? 0, | ||
| height: image.height ?? 0, | ||
| }); | ||
| setInstances([]); | ||
| }; | ||
|
|
||
| const runForward = async () => { | ||
| if (!imageUri || imageSize.width === 0 || imageSize.height === 0) return; | ||
|
|
||
| try { | ||
| const output = await forward(imageUri, { | ||
| confidenceThreshold: 0.5, | ||
| iouThreshold: 0.55, | ||
| maxInstances: 20, | ||
| returnMaskAtOriginalResolution: true, | ||
| inputSize: selectedInputSize ?? undefined, | ||
| }); | ||
|
|
||
| // Convert raw masks → small Skia images immediately. | ||
| // Raw Uint8Array mask buffers (backed by native OwningArrayBuffer) | ||
| // go out of scope here and become eligible for GC right away. | ||
| setInstances(buildDisplayInstances(output)); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| }; | ||
|
|
||
| if (!isReady && error) { | ||
| return ( | ||
| <ScreenWrapper> | ||
| <View style={styles.errorContainer}> | ||
| <Text style={styles.errorTitle}>Error Loading Model</Text> | ||
| <Text style={styles.errorText}> | ||
| {error?.message || 'Unknown error occurred'} | ||
| </Text> | ||
| <Text style={styles.errorCode}>Code: {error?.code || 'N/A'}</Text> | ||
| </View> | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
|
|
||
| if (!isReady) { | ||
| return ( | ||
| <Spinner | ||
| visible={!isReady} | ||
| textContent={`Loading the model ${(downloadProgress * 100).toFixed(0)} %`} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <ScreenWrapper> | ||
| <View style={styles.container}> | ||
| <View style={styles.imageContainer}> | ||
| <ImageWithMasks | ||
| imageUri={imageUri} | ||
| instances={instances} | ||
| imageWidth={imageSize.width} | ||
| imageHeight={imageSize.height} | ||
| /> | ||
| </View> | ||
|
|
||
| {imageUri && availableInputSizes && availableInputSizes.length > 0 && ( | ||
| <View style={styles.inputSizeContainer}> | ||
| <Text style={styles.inputSizeLabel}>Input Size:</Text> | ||
| <ScrollView | ||
| horizontal | ||
| showsHorizontalScrollIndicator={false} | ||
| style={styles.inputSizeScroll} | ||
| > | ||
| {availableInputSizes.map((size) => ( | ||
| <TouchableOpacity | ||
| key={size} | ||
| style={[ | ||
| styles.sizeButton, | ||
| selectedInputSize === size && styles.sizeButtonActive, | ||
| ]} | ||
| onPress={() => setSelectedInputSize(size)} | ||
| > | ||
| <Text | ||
| style={[ | ||
| styles.sizeButtonText, | ||
| selectedInputSize === size && styles.sizeButtonTextActive, | ||
| ]} | ||
| > | ||
| {size} | ||
| </Text> | ||
| </TouchableOpacity> | ||
| ))} | ||
| </ScrollView> | ||
| </View> | ||
| )} | ||
|
|
||
| {instances.length > 0 && ( | ||
| <View style={styles.resultsContainer}> | ||
| <Text style={styles.resultsHeader}> | ||
| Detected {instances.length} instance(s) | ||
| </Text> | ||
| <ScrollView style={styles.resultsList}> | ||
| {instances.map((instance, idx) => ( | ||
| <View key={idx} style={styles.resultRow}> | ||
| <Text style={styles.resultText}> | ||
| {instance.label || 'Unknown'} ( | ||
| {(instance.score * 100).toFixed(1)}%) | ||
| </Text> | ||
| </View> | ||
| ))} | ||
| </ScrollView> | ||
| </View> | ||
| )} | ||
| </View> | ||
|
|
||
| <BottomBar | ||
| handleCameraPress={handleCameraPress} | ||
| runForward={runForward} | ||
| /> | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 6, | ||
| width: '100%', | ||
| }, | ||
| imageContainer: { | ||
| flex: 1, | ||
| width: '100%', | ||
| padding: 16, | ||
| }, | ||
| inputSizeContainer: { | ||
| paddingHorizontal: 16, | ||
| paddingVertical: 12, | ||
| backgroundColor: '#fff', | ||
| borderTopWidth: 1, | ||
| borderTopColor: '#e0e0e0', | ||
| }, | ||
| inputSizeLabel: { | ||
| fontSize: 14, | ||
| fontWeight: '600', | ||
| color: '#333', | ||
| marginBottom: 8, | ||
| }, | ||
| inputSizeScroll: { | ||
| flexDirection: 'row', | ||
| }, | ||
| sizeButton: { | ||
| paddingHorizontal: 16, | ||
| paddingVertical: 8, | ||
| marginRight: 8, | ||
| borderRadius: 6, | ||
| backgroundColor: '#f0f0f0', | ||
| }, | ||
| sizeButtonActive: { | ||
| backgroundColor: '#007AFF', | ||
| }, | ||
| sizeButtonText: { | ||
| fontSize: 14, | ||
| fontWeight: '600', | ||
| color: '#666', | ||
| }, | ||
| sizeButtonTextActive: { | ||
| color: '#fff', | ||
| }, | ||
| resultsContainer: { | ||
| maxHeight: 200, | ||
| paddingHorizontal: 16, | ||
| paddingVertical: 12, | ||
| backgroundColor: '#fff', | ||
| borderTopWidth: 1, | ||
| borderTopColor: '#e0e0e0', | ||
| }, | ||
| resultsHeader: { | ||
| fontSize: 16, | ||
| fontWeight: '600', | ||
| marginBottom: 8, | ||
| color: '#333', | ||
| }, | ||
| resultsList: { | ||
| flex: 1, | ||
| }, | ||
| resultRow: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| paddingVertical: 6, | ||
| paddingHorizontal: 8, | ||
| marginBottom: 4, | ||
| backgroundColor: '#f9f9f9', | ||
| borderRadius: 6, | ||
| }, | ||
| resultText: { | ||
| fontSize: 14, | ||
| fontWeight: '500', | ||
| color: '#333', | ||
| }, | ||
| errorContainer: { | ||
| flex: 1, | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| padding: 32, | ||
| }, | ||
| errorTitle: { | ||
| fontSize: 20, | ||
| fontWeight: '700', | ||
| color: '#e74c3c', | ||
| marginBottom: 12, | ||
| }, | ||
| errorText: { | ||
| fontSize: 14, | ||
| color: '#555', | ||
| textAlign: 'center', | ||
| marginBottom: 8, | ||
| }, | ||
| errorCode: { | ||
| fontSize: 12, | ||
| color: '#999', | ||
| fontFamily: 'Courier', | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.