-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathindex.tsx
More file actions
191 lines (179 loc) · 5.28 KB
/
index.tsx
File metadata and controls
191 lines (179 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import Spinner from '../../components/Spinner';
import { getImage } from '../../utils';
import {
useClassification,
EFFICIENTNET_V2_S,
EFFICIENTNET_V2_S_QUANTIZED,
ClassificationModelSources,
} from 'react-native-executorch';
import { ModelPicker, ModelOption } from '../../components/ModelPicker';
const MODELS: ModelOption<ClassificationModelSources>[] = [
{ label: 'EfficientNet V2 S Quantized', value: EFFICIENTNET_V2_S_QUANTIZED },
{ label: 'EfficientNet V2 S', value: EFFICIENTNET_V2_S },
];
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
import { BottomBar } from '../../components/BottomBar';
import React, { useContext, useEffect, useState } from 'react';
import { GeneratingContext } from '../../context';
import ScreenWrapper from '../../ScreenWrapper';
import { StatsBar } from '../../components/StatsBar';
import ErrorBanner from '../../components/ErrorBanner';
export default function ClassificationScreen() {
const [selectedModel, setSelectedModel] =
useState<ClassificationModelSources>(EFFICIENTNET_V2_S_QUANTIZED);
const [results, setResults] = useState<{ label: string; score: number }[]>(
[]
);
const [imageUri, setImageUri] = useState('');
const [inferenceTime, setInferenceTime] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
const model = useClassification({ model: selectedModel });
const { setGlobalGenerating } = useContext(GeneratingContext);
useEffect(() => {
setGlobalGenerating(model.isGenerating);
}, [model.isGenerating, setGlobalGenerating]);
useEffect(() => {
if (model.error) setError(String(model.error));
}, [model.error]);
const handleCameraPress = async (isCamera: boolean) => {
const image = await getImage(isCamera);
const uri = image?.uri;
if (typeof uri === 'string') {
setImageUri(uri as string);
setResults([]);
setInferenceTime(null);
}
};
const runForward = async () => {
if (imageUri) {
try {
const start = Date.now();
const output = await model.forward(imageUri);
setInferenceTime(Date.now() - start);
const top10 = Object.entries(output)
.sort(([, a], [, b]) => (b as number) - (a as number))
.slice(0, 10)
.map(([label, score]) => ({ label, score: score as number }));
setResults(top10);
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
}
}
};
if (!model.isReady && !model.error) {
return (
<Spinner
visible={true}
textContent={`Loading the model ${(model.downloadProgress * 100).toFixed(0)} %`}
/>
);
}
return (
<ScreenWrapper>
<ErrorBanner message={error} onDismiss={() => setError(null)} />
<View style={styles.imageContainer}>
<Image
style={styles.image}
resizeMode="contain"
source={
imageUri
? { uri: imageUri }
: require('../../assets/icons/executorch_logo.png')
}
/>
{!imageUri && (
<View style={styles.infoContainer}>
<Text style={styles.infoTitle}>Image Classification</Text>
<Text style={styles.infoText}>
This model analyzes an image and returns the top 10 most likely
labels with confidence scores. Use the gallery or camera icons
below to pick an image, then tap the button to run the model.
</Text>
</View>
)}
{results.length > 0 && (
<View style={styles.results}>
<Text style={styles.resultHeader}>Results Top 10</Text>
<ScrollView style={styles.resultsList}>
{results.map(({ label, score }) => (
<View key={label} style={styles.resultRecord}>
<Text style={styles.resultLabel}>{label}</Text>
<Text>{score.toFixed(3)}</Text>
</View>
))}
</ScrollView>
</View>
)}
</View>
<ModelPicker
models={MODELS}
selectedModel={selectedModel}
disabled={model.isGenerating}
onSelect={(m) => {
setSelectedModel(m);
setResults([]);
}}
/>
<StatsBar inferenceTime={inferenceTime} />
<BottomBar
handleCameraPress={handleCameraPress}
runForward={runForward}
hasImage={!!imageUri}
isGenerating={model.isGenerating}
/>
</ScreenWrapper>
);
}
const styles = StyleSheet.create({
imageContainer: {
flex: 6,
width: '100%',
padding: 16,
},
image: {
flex: 2,
borderRadius: 8,
width: '100%',
},
results: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
gap: 4,
padding: 4,
},
resultHeader: {
fontSize: 18,
color: 'navy',
},
resultsList: {
flex: 1,
},
resultRecord: {
flexDirection: 'row',
width: '100%',
justifyContent: 'space-between',
padding: 8,
borderBottomWidth: 1,
},
resultLabel: {
flex: 1,
marginRight: 4,
},
infoContainer: {
alignItems: 'center',
padding: 16,
gap: 8,
},
infoTitle: {
fontSize: 18,
fontWeight: '600',
color: 'navy',
},
infoText: {
fontSize: 14,
color: '#555',
textAlign: 'center',
lineHeight: 20,
},
});