forked from violetljj/blind-assist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_detector_ab_device_benchmark.ps1
More file actions
240 lines (224 loc) · 11.3 KB
/
Copy pathrun_detector_ab_device_benchmark.ps1
File metadata and controls
240 lines (224 loc) · 11.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
param(
[ValidateSet("Coco100", "BlindAssistEvalSet")]
[string]$DatasetKind = "Coco100",
[ValidateSet("DetectorAb", "DepthFusion", "DepthFusionSweep", "SanpoTraversabilityOracle", "SegmentationCandidate", "InformationCeilingThreeArm")]
[string]$ComparisonMode = "DetectorAb",
[string]$DatasetRoot,
[int]$ImageLimit = 100,
[int]$PureWarmup = 10,
[int]$PureRuns = 100,
[int]$AppRunsPerImage = 3,
[double]$MatchIouThreshold = 0.5,
[ValidateSet("current", "center_near_sensitive", "center_near_strict", "critical_sensitive", "side_near_sensitive")]
[string]$RiskConfig = "current",
[switch]$RiskSweep,
[int]$DefaultRegressionSeconds = 90,
[switch]$SkipDefaultRegression,
[string]$SegmentationModelPath = "artifacts.local\downloads\traversability-lab\exports\mobilenetv3_lraspp_int8_256.tflite",
[string]$SegmentationModelAssetName = "mobilenetv3_lraspp_int8_256.tflite",
[string]$AdbPath,
[string]$PythonPath = "E:\codex-tools\bin\blindassist-python.cmd",
[string]$GradleUserHome = "E:\codex-tools\projects\blindassist\state\gradle"
)
$ErrorActionPreference = "Stop"
function Resolve-RepoPath([string]$Path) {
if ([System.IO.Path]::IsPathRooted($Path)) {
return $Path
}
return (Join-Path $repoRoot $Path)
}
function Resolve-Adb([string]$RequestedPath) {
if ($RequestedPath) {
if (-not (Test-Path -LiteralPath $RequestedPath)) {
throw "ADB not found at $RequestedPath"
}
return (Resolve-Path -LiteralPath $RequestedPath).Path
}
$localAdb = Join-Path $repoRoot ".android-sdk\platform-tools\adb.exe"
if (Test-Path -LiteralPath $localAdb) {
return (Resolve-Path -LiteralPath $localAdb).Path
}
$command = Get-Command adb -ErrorAction SilentlyContinue
if ($command) {
return $command.Source
}
throw "ADB not found. Pass -AdbPath or install platform-tools."
}
function Invoke-Native {
param(
[string]$FilePath,
[string[]]$Arguments,
[string]$LogPath
)
$previousPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try {
$output = & $FilePath @Arguments 2>&1
$code = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previousPreference
}
if ($LogPath) {
$output | Tee-Object -FilePath $LogPath
}
if ($code -ne 0) {
throw "$FilePath $($Arguments -join ' ') failed with exit code $code"
}
return $output
}
function Get-SingleDevice([string]$Adb) {
$output = Invoke-Native $Adb @("devices") $null
$devices = @(
$output |
Where-Object { $_ -match "^\S+\s+device$" } |
ForEach-Object { ($_ -split "\s+")[0] }
)
if ($devices.Count -ne 1) {
throw "Expected exactly one online device, found $($devices.Count). Raw adb devices output: $($output -join ' | ')"
}
return $devices[0]
}
$repoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "..")).Path
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$artifactRoot = Join-Path (Join-Path $repoRoot "artifacts.local\evidence\detector-ab-device-benchmark") $timestamp
New-Item -ItemType Directory -Force -Path $artifactRoot | Out-Null
$python = Resolve-RepoPath $PythonPath
$yolo11n = Resolve-RepoPath "app\src\main\assets\yolo11n_fp16_320.tflite"
$yolo26n = Resolve-RepoPath "artifacts.local\downloads\detector-lab\exports\yolo26n_fp16_320.tflite"
$manifest = Resolve-RepoPath "artifacts.local\downloads\detector-lab\datasets\coco100\coco100_manifest.json"
$annotations = Resolve-RepoPath "artifacts.local\downloads\detector-lab\datasets\coco100\coco100_annotations.json"
$defaultBlindAssistEvalSet = "artifacts.local\evidence\datasets\blindassist-evalset-20260527-impl"
$requestedBlindAssistEvalSet = if ($DatasetRoot) { $DatasetRoot } else { $defaultBlindAssistEvalSet }
$blindAssistEvalSet = Resolve-RepoPath $requestedBlindAssistEvalSet
$segmentationModel = Resolve-RepoPath $SegmentationModelPath
$resolvedGradleUserHome = Resolve-RepoPath $GradleUserHome
$apk = Resolve-RepoPath "app\build\outputs\apk\debug\app-debug.apk"
$aapt = Resolve-RepoPath ".android-sdk\build-tools\35.0.0\aapt.exe"
$adb = Resolve-Adb $AdbPath
$device = Get-SingleDevice $adb
if (-not (Test-Path -LiteralPath $python)) {
throw "Python runtime not found: $python"
}
if (-not (Test-Path -LiteralPath $resolvedGradleUserHome -PathType Container)) {
throw "Gradle user home not found: $resolvedGradleUserHome"
}
if (-not (Test-Path -LiteralPath $yolo11n)) {
throw "yolo11n TFLite asset not found: $yolo11n"
}
if ($ComparisonMode -eq "DetectorAb" -and -not (Test-Path -LiteralPath $yolo26n)) {
throw "yolo26n TFLite candidate not found: $yolo26n"
}
if ($ComparisonMode -eq "SegmentationCandidate" -and -not (Test-Path -LiteralPath $segmentationModel)) {
throw "Benchmark-only segmentation INT8 model not found: $segmentationModel"
}
if ($DatasetKind -eq "BlindAssistEvalSet") {
if (-not (Test-Path -LiteralPath (Join-Path $blindAssistEvalSet "manifest.jsonl"))) {
throw "BlindAssist evalset manifest not found: $(Join-Path $blindAssistEvalSet 'manifest.jsonl')"
}
if (-not (Test-Path -LiteralPath (Join-Path $blindAssistEvalSet "images\test"))) {
throw "BlindAssist evalset images/test directory not found: $(Join-Path $blindAssistEvalSet 'images\test')"
}
}
Push-Location $repoRoot
try {
$env:JAVA_HOME = (Resolve-Path ".\.jdk\jdk17.0.19_10").Path
$env:PATH = "$env:JAVA_HOME\bin;$((Resolve-Path '.\.android-sdk\platform-tools').Path);$env:PATH"
$env:GRADLE_USER_HOME = (Resolve-Path -LiteralPath $resolvedGradleUserHome).Path
New-Item -ItemType Directory -Force -Path ".\.android-home", ".\.kotlin-home" | Out-Null
$env:ANDROID_USER_HOME = (Resolve-Path ".\.android-home").Path
$env:KOTLIN_HOME = (Resolve-Path ".\.kotlin-home").Path
$env:GRADLE_OPTS = "-Dkotlin.compiler.execution.strategy=in-process"
Invoke-Native $python @("scripts\inspect_tflite.py") (Join-Path $artifactRoot "inspect-yolo11n.txt") | Out-Null
if ($ComparisonMode -eq "DetectorAb") {
Invoke-Native $python @("scripts\inspect_tflite.py", "--allow-any-shape", "artifacts.local\downloads\detector-lab\exports\yolo26n_fp16_320.tflite") (Join-Path $artifactRoot "inspect-yolo26n.txt") | Out-Null
}
if ($DatasetKind -eq "Coco100") {
if (-not (Test-Path -LiteralPath $manifest) -or -not (Test-Path -LiteralPath $annotations)) {
Invoke-Native $python @("scripts\prepare_coco100.py", "--sample-count", "$ImageLimit") (Join-Path $artifactRoot "prepare-coco100.txt") | Out-Null
} else {
"manifest_exists=$manifest`nannotations_exists=$annotations" |
Tee-Object -FilePath (Join-Path $artifactRoot "prepare-coco100.txt") | Out-Null
}
}
Invoke-Native ".\gradlew.bat" @(
":app:assembleDebug",
":device-benchmark:assembleDebug",
"-PblindAssistEvalSetDir=$blindAssistEvalSet",
"-PsegmentationBenchmarkModelPath=$segmentationModel",
"-PsegmentationBenchmarkModelAssetName=$SegmentationModelAssetName",
"--no-daemon",
"--console=plain"
) (Join-Path $artifactRoot "gradle-assemble.txt") | Out-Null
if (-not (Test-Path -LiteralPath $apk)) {
throw "Debug APK not found after build: $apk"
}
Invoke-Native $aapt @("list", $apk) (Join-Path $artifactRoot "main-apk-assets.txt") | Out-Null
$mainAssets = Get-Content -Path (Join-Path $artifactRoot "main-apk-assets.txt")
if ($mainAssets -notcontains "assets/yolo11n_fp16_320.tflite") {
throw "Default yolo11n asset is missing from the main debug APK."
}
if ($mainAssets -contains "assets/yolo26n_fp16_320.tflite") {
throw "yolo26n unexpectedly entered the main debug APK assets."
}
if ($mainAssets -contains "assets/$SegmentationModelAssetName") {
throw "Benchmark-only segmentation model unexpectedly entered the main debug APK assets."
}
$riskConfigs = if ($RiskSweep) {
@("current", "center_near_sensitive", "center_near_strict", "critical_sensitive", "side_near_sensitive")
} else {
@($RiskConfig)
}
foreach ($currentRiskConfig in $riskConfigs) {
Invoke-Native ".\gradlew.bat" @(
":device-benchmark:connectedDebugAndroidTest",
"-PblindAssistEvalSetDir=$blindAssistEvalSet",
"-PsegmentationBenchmarkModelPath=$segmentationModel",
"-PsegmentationBenchmarkModelAssetName=$SegmentationModelAssetName",
"-Pandroid.testInstrumentationRunnerArguments.class=com.linnan.blindassist.benchmark.DetectorAbDeviceBenchmarkTest",
"-Pandroid.testInstrumentationRunnerArguments.datasetKind=$DatasetKind",
"-Pandroid.testInstrumentationRunnerArguments.comparisonMode=$ComparisonMode",
"-Pandroid.testInstrumentationRunnerArguments.segmentationModelAsset=$SegmentationModelAssetName",
"-Pandroid.testInstrumentationRunnerArguments.riskConfig=$currentRiskConfig",
"-Pandroid.testInstrumentationRunnerArguments.imageLimit=$ImageLimit",
"-Pandroid.testInstrumentationRunnerArguments.pureWarmup=$PureWarmup",
"-Pandroid.testInstrumentationRunnerArguments.pureRuns=$PureRuns",
"-Pandroid.testInstrumentationRunnerArguments.appRunsPerImage=$AppRunsPerImage",
"-Pandroid.testInstrumentationRunnerArguments.matchIouThreshold=$MatchIouThreshold",
"-Pandroid.injected.androidTest.leaveApksInstalledAfterRun=true",
"--no-daemon",
"--console=plain"
) (Join-Path $artifactRoot "connected-detector-ab-benchmark-$currentRiskConfig.txt") | Out-Null
}
Invoke-Native $adb @("-s", $device, "logcat", "-d", "-s", "DetectorAbBenchmark", "BlindAssistPerf") (Join-Path $artifactRoot "logcat-detector-ab.txt") | Out-Null
$deviceArtifactRoot = "/sdcard/Android/data/com.linnan.blindassist/files/detector-ab-benchmark"
Invoke-Native $adb @("-s", $device, "pull", $deviceArtifactRoot, (Join-Path $artifactRoot "device-detector-ab-benchmark")) (Join-Path $artifactRoot "adb-pull-detector-ab.txt") | Out-Null
if (-not $SkipDefaultRegression) {
powershell -ExecutionPolicy Bypass -File .\scripts\run_device_regression.ps1 -SampleSeconds $DefaultRegressionSeconds 2>&1 |
Tee-Object -FilePath (Join-Path $artifactRoot "default-device-regression.txt")
if ($LASTEXITCODE -ne 0) {
throw "Default model device regression failed with exit code $LASTEXITCODE"
}
}
$summary = [ordered]@{
status = "passed"
timestamp = $timestamp
device = $device
artifactRoot = $artifactRoot
yolo11n = $yolo11n
yolo26n = $yolo26n
segmentationModel = if ($ComparisonMode -eq "SegmentationCandidate") { $segmentationModel } else { $null }
datasetKind = $DatasetKind
comparisonMode = $ComparisonMode
blindAssistEvalSet = $blindAssistEvalSet
coco100Manifest = $manifest
coco100Annotations = $annotations
matchIouThreshold = $MatchIouThreshold
appRunsPerImage = $AppRunsPerImage
riskConfigs = $riskConfigs
defaultRegression = -not [bool]$SkipDefaultRegression
}
$summary | ConvertTo-Json -Depth 5 | Out-File -FilePath (Join-Path $artifactRoot "summary.json") -Encoding utf8
Write-Host "Detector A/B device benchmark artifacts: $artifactRoot"
} finally {
Pop-Location
}