56 / 66 · 09 Mobile & Cross-Platform Testing · On-Device ML Model Testing← prev⊞ allnext →☰ Read as one page
10.4Testing Model Accuracy on Device
The same model may produce slightly different results on different hardware due to floating-point precision differences between GPU, CPU, and NPU inference.
def test_model_accuracy_matches_server(driver):
"""On-device predictions must agree with server-side predictions."""
validation_set = load_validation_set("test_data/validation/")
agreements = 0
total = len(validation_set)
for image_path, expected_label in validation_set:
# Get on-device prediction
driver.push_file("/sdcard/test_images/current.jpg", source_path=image_path)
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "classify-image").click()
result = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "classification-result")
device_prediction = result.text
# Compare with server-side prediction
if device_prediction.lower() == expected_label.lower():
agreements += 1
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "clear-result").click()
accuracy = agreements / total
assert accuracy >= 0.99, \
f"On-device accuracy {accuracy:.2%} is below 99% threshold"
def test_model_handles_edge_case_inputs(driver):
"""Model should handle adversarial and edge case inputs gracefully."""
edge_cases = [
("test_data/blank_white.jpg", "no_object_detected"),
("test_data/blank_black.jpg", "no_object_detected"),
("test_data/tiny_1x1.jpg", "invalid_input"),
("test_data/very_large.jpg", "cat"), # Should still classify correctly
("test_data/rotated_90.jpg", "cat"), # Rotation invariance
("test_data/low_quality.jpg", "cat"), # JPEG quality = 10
]
for image_path, expected_category in edge_cases:
driver.push_file("/sdcard/test_images/edge.jpg", source_path=image_path)
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "classify-image").click()
# Should not crash regardless of input
result = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "classification-result")
assert result.is_displayed(), f"Model crashed on {image_path}"
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "clear-result").click()