-
Notifications
You must be signed in to change notification settings - Fork 84
[RNE Rewrite] feat: add semantic segmentation task #1275
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
12 commits
Select commit
Hold shift + click to select a range
d226206
feat: add semantic segmentation task and native image operations
barhanc 73ed34c
refactor(cv): optimize applyColormap and document layout requirements…
barhanc e6db298
refactor(cv): validate colormap input parameters and document layout …
barhanc 22388e9
feat: add semantic segmentation screen
barhanc b4cbf07
Apply suggestion from @msluszniak
barhanc bc1e9ad
refactor(cv): extract RGBA channel count to constant in applyColormap
barhanc ef90fc6
docs(math): add comment to prevent argmax loop optimization
barhanc 2e624cf
feat(cv): add DeepLab V3 and FCN segmentation models and configure re…
barhanc 3bca7c2
docs(cv): document colormap behavior and logit modes in createSemanti…
barhanc fc9bbf2
fix(cv): validate colormap channel range to prevent UB/wrap in applyC…
barhanc 35427ee
fix(vision): add null check for Skia.Image.MakeImage in segmentation …
barhanc 7702d77
fix(cv-apps): prevent screen jumping & overlap with android system bar
barhanc 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import React, { useState, useRef } from 'react'; | ||
| import { View, Text, ScrollView } from 'react-native'; | ||
| import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
| import { commonStyles, theme } from '../../theme'; | ||
| import { | ||
| Skia, | ||
| ColorType, | ||
| AlphaType, | ||
| useImage, | ||
| type SkImage as SkiaImageType, | ||
| } from '@shopify/react-native-skia'; | ||
| import { useSemanticSegmenter, models } from 'react-native-executorch'; | ||
| import ScreenWrapper from '../../components/ScreenWrapper'; | ||
| import { ModelPicker, type ModelOption } from '../../components/ModelPicker'; | ||
| import { getImage } from '../../utils'; | ||
| import { ImageViewport } from '../../components/ImageViewport'; | ||
| import { ModelStatus } from '../../components/ModelStatus'; | ||
| import { LatencyIndicator } from '../../components/LatencyIndicator'; | ||
| import { Button } from '../../components/Button'; | ||
|
|
||
| const SEGMENTATION_OPTIONS: ModelOption[] = [ | ||
| { | ||
| label: 'Selfie Seg (FP32)', | ||
| value: models.semanticSegmentation.SELFIE_SEGMENTATION.XNNPACK_FP32, | ||
| }, | ||
| { | ||
| label: 'LRASPP MobileNet V3 (INT8)', | ||
| value: models.semanticSegmentation.LRASPP_MOBILENET_V3_LARGE.XNNPACK_INT8, | ||
| }, | ||
| { | ||
| label: 'DeepLab V3 ResNet50 (INT8)', | ||
| value: models.semanticSegmentation.DEEPLAB_V3_RESNET50.XNNPACK_INT8, | ||
| }, | ||
| { | ||
| label: 'DeepLab V3 ResNet101 (INT8)', | ||
| value: models.semanticSegmentation.DEEPLAB_V3_RESNET101.XNNPACK_INT8, | ||
| }, | ||
| { | ||
| label: 'DeepLab V3 MobileNet V3 (INT8)', | ||
| value: models.semanticSegmentation.DEEPLAB_V3_MOBILENET_V3_LARGE.XNNPACK_INT8, | ||
| }, | ||
| { | ||
| label: 'FCN ResNet50 (INT8)', | ||
| value: models.semanticSegmentation.FCN_RESNET50.XNNPACK_INT8, | ||
| }, | ||
| { | ||
| label: 'FCN ResNet101 (INT8)', | ||
| value: models.semanticSegmentation.FCN_RESNET101.XNNPACK_INT8, | ||
| }, | ||
| ]; | ||
|
|
||
| function SegmentationContent() { | ||
| const insets = useSafeAreaInsets(); | ||
| const [selectedModel, setSelectedModel] = useState<any>(SEGMENTATION_OPTIONS[0].value); | ||
| const [imageUri, setImageUri] = useState<string | null>(null); | ||
| const [isProcessing, setIsProcessing] = useState(false); | ||
| const [latency, setLatency] = useState<number | null>(null); | ||
| const [segmentationImage, setSegmentationImage] = useState<SkiaImageType | null>(null); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| const isProcessingRef = useRef(false); | ||
|
|
||
| const skiaImage = useImage(imageUri, (err) => setError(err.message || String(err))); | ||
|
|
||
| const { | ||
| isReady, | ||
| downloadProgress, | ||
| error: loadError, | ||
| segment, | ||
| segmentWorklet, | ||
| } = useSemanticSegmenter(selectedModel); | ||
|
|
||
| const handlePickImage = async (useCamera: boolean) => { | ||
| setError(null); | ||
| try { | ||
| const uri = await getImage(useCamera); | ||
| if (uri) { | ||
| setImageUri(uri); | ||
| setLatency(null); | ||
| setSegmentationImage(null); | ||
| } | ||
| } catch (e: any) { | ||
| setError(e.message || String(e)); | ||
| } | ||
| }; | ||
|
|
||
| const runSegmentation = async (sync: boolean) => { | ||
| if (isProcessingRef.current) return; | ||
| if (!skiaImage || !segment || !segmentWorklet) return; | ||
|
|
||
| isProcessingRef.current = true; | ||
| setIsProcessing(true); | ||
| setError(null); | ||
|
|
||
| try { | ||
| const pixels = skiaImage.readPixels(); | ||
| if (!pixels) { | ||
| throw new Error('Failed to read pixels from image'); | ||
| } | ||
| if (!(pixels instanceof Uint8Array)) { | ||
| throw new Error('Expected Uint8Array from readPixels'); | ||
| } | ||
| const buffer = { | ||
| data: pixels, | ||
| width: skiaImage.width(), | ||
| height: skiaImage.height(), | ||
| format: 'rgba' as const, | ||
| layout: 'hwc' as const, | ||
| }; | ||
|
|
||
| const start = Date.now(); | ||
| const { buffer: outBuffer } = sync ? segmentWorklet(buffer) : await segment(buffer); | ||
| setLatency(Date.now() - start); | ||
|
|
||
| const outData = Skia.Data.fromBytes(outBuffer.data); | ||
| const info = { | ||
| width: buffer.width, | ||
| height: buffer.height, | ||
| colorType: ColorType.RGBA_8888, | ||
| alphaType: AlphaType.Premul, | ||
| }; | ||
| const nextImage = Skia.Image.MakeImage(info, outData, buffer.width * 4); | ||
| if (!nextImage) { | ||
| throw new Error('Failed to create overlay image from output data'); | ||
| } | ||
| setSegmentationImage(nextImage); | ||
| } catch (e: any) { | ||
| setError(e.message || String(e)); | ||
| } finally { | ||
| isProcessingRef.current = false; | ||
| setIsProcessing(false); | ||
| } | ||
| }; | ||
|
|
||
| const activeError = loadError ? String(loadError) : error; | ||
|
|
||
| return ( | ||
| <ScrollView | ||
| style={commonStyles.container} | ||
| contentContainerStyle={[ | ||
| commonStyles.contentContainer, | ||
| { paddingBottom: insets.bottom + theme.spacing.large }, | ||
| ]} | ||
| > | ||
| <Text style={commonStyles.description}> | ||
| Upload or capture an image to partition it into multiple segments using semantic | ||
| segmentation. | ||
| </Text> | ||
|
|
||
| <ModelPicker | ||
| label="Model" | ||
| options={SEGMENTATION_OPTIONS} | ||
| selectedValue={selectedModel} | ||
| onValueChange={(model) => { | ||
| setSelectedModel(model); | ||
| setLatency(null); | ||
| setError(null); | ||
| setSegmentationImage(null); | ||
| }} | ||
| /> | ||
|
|
||
| <ModelStatus | ||
| isReady={isReady} | ||
| downloadProgress={downloadProgress} | ||
| error={activeError} | ||
| modelTypeLabel="segmentation model" | ||
| /> | ||
|
|
||
| <ImageViewport | ||
| skiaImage={skiaImage} | ||
| overlayImage={segmentationImage} | ||
| onPressPlaceholder={() => handlePickImage(false)} | ||
| /> | ||
|
|
||
| <View style={commonStyles.buttonRow}> | ||
| <Button title="Gallery" onPress={() => handlePickImage(false)} variant="secondary" /> | ||
| <Button title="Camera" onPress={() => handlePickImage(true)} variant="secondary" /> | ||
| </View> | ||
|
|
||
| <View style={commonStyles.buttonRow}> | ||
| <Button | ||
| title="Run Async" | ||
| onPress={() => runSegmentation(false)} | ||
| disabled={!skiaImage || !isReady || isProcessing} | ||
| loading={isProcessing} | ||
| /> | ||
| <Button | ||
| title="Run Sync" | ||
| onPress={() => runSegmentation(true)} | ||
| disabled={!skiaImage || !isReady || isProcessing} | ||
| variant="accent" | ||
| /> | ||
| </View> | ||
|
|
||
| <LatencyIndicator latency={latency} /> | ||
| </ScrollView> | ||
| ); | ||
| } | ||
|
|
||
| export default function SegmentationScreen() { | ||
| return ( | ||
| <ScreenWrapper> | ||
| <SegmentationContent /> | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
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
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
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.