-
Notifications
You must be signed in to change notification settings - Fork 62
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
Open
benITo47
wants to merge
56
commits into
main
Choose a base branch
from
@bo/instanceSegmentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,827
−811
Open
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
7c358b9
chore: Rename ImageSegmentation to SemanticSegmentation
benITo47 2888282
Update docs
benITo47 a7567a1
Upadte claude skills
benITo47 8b8dc32
Initial instance segmetntation
benITo47 5869154
Rename to BaseInstanceSegmentation
benITo47 fb870cb
Update instance segmentaiton demo app
benITo47 701e6fd
Update api reference docs
benITo47 957849d
Add docs and fix some typechecks
benITo47 a5984d4
Add method unloading
benITo47 eb80bb1
Update gitignore
benITo47 090de10
Remove type field from postprocessor config
benITo47 f7bd1c2
Add ImageWithMasks component
benITo47 52e981b
Change BaseInstanceSegmentation to accept and return string labels
benITo47 ed18734
Remove redundant postprocessor config type
benITo47 cef8007
Change typedoc links
benITo47 e568069
Add docs
benITo47 907793d
Speed up mask processing
benITo47 1abc8da
Return class index, tweak mask drawing performance
benITo47 1b78a00
Add yolo specific coco labels
benITo47 755348d
Fix yarn stuff
benITo47 f65f732
Add tests
benITo47 2fdbed7
Add RFDetr-segmentation model, change preprocessorConfig types
benITo47 8da7405
Update docs/docs/02-benchmarks/inference-time.md
benITo47 504c9d9
Update docs/docs/02-benchmarks/memory-usage.md
benITo47 9e2bea4
Restore docs
benITo47 4d16716
Review fixes for InstanceSegmentationModule
benITo47 56a6523
Change typing in useInstanceSegmentation
benITo47 ce9cd72
Split instance segmentation postprocessing, introduce CV utils
benITo47 d03e6d4
Migrate ObjectDetection to common CV utils
benITo47 ef46c0e
Fix links
benITo47 48aa04b
Break up postprocessing helpers in Instance Segmentation
benITo47 cd340b3
Streamline postprocessing
benITo47 f45c022
Change inheritance to VisionModel
benITo47 d86bae2
Change inheritance to VisionModel
benITo47 9c9618b
Add live instance segmentation
benITo47 abda932
Checkout upstream app
benITo47 94e1bd7
Fix normalizing in InstanceSegmentation
benITo47 2e9ce55
Add missing brackets
benITo47 7b6d6f1
Strengthen typing
benITo47 d8e4795
Remove instanceId field
benITo47 98a8343
Apply review suggestions
benITo47 71312bc
Update docs
benITo47 013ac1c
Migrate vector<uint8> to OwningArrayBuffer
benITo47 3ceadb3
Apply revies suggestions
benITo47 7ebdbba
Change rfdetr naming, apply suggestions
benITo47 139536c
Apply suggestions
benITo47 fa85d56
Apply suggestions
benITo47 2b69c66
Apply suggestions
benITo47 0e90871
Update docs
benITo47 2b4ef62
Improve tests
benITo47 3dd47a5
Update tests
benITo47 8e4d43f
Readd accidentaly removed dep
benITo47 e4d7a61
Revert formatting in run_test.sh
benITo47 b3a9b89
Remove OD_live button
benITo47 ab4c33c
Add Mateusz's patch
benITo47 bac2b69
Remove console.logs, change error type
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.