Skip to content

Commit f38e125

Browse files
wangtzMarkDaoust
authored andcommitted
Fix some doc
PiperOrigin-RevId: 419717366
1 parent 6a5c4df commit f38e125

File tree

45 files changed

+120
-113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+120
-113
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

lite/examples/image_classification/raspberry_pi/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on a Raspberry Pi to perform real-time image classification using images
55
streamed from the camera.
66

77
At the end of this page, there are extra steps to accelerate the example using
8-
the Coral USB Accelerator, which increases the inference speed by ~10x.
8+
the Coral USB Accelerator to increase inference speed.
99

1010

1111
## Set up your hardware

lite/examples/image_classification/raspberry_pi/image_classifier.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
"""A wrapper for TensorFlow Lite image classification models."""
1515

1616
import dataclasses
17+
import json
1718
import platform
1819
from typing import List
19-
import zipfile
2020

2121
import cv2
2222
import numpy as np
23+
from tflite_support import metadata
2324

2425
# pylint: disable=g-import-not-at-top
2526
try:
@@ -76,11 +77,6 @@ def edgetpu_lib_name():
7677
class ImageClassifier(object):
7778
"""A wrapper class for a TFLite image classification model."""
7879

79-
_mean = 127
80-
"""Default mean normalization parameter for float model."""
81-
_std = 128
82-
"""Default std normalization parameter for float model."""
83-
8480
def __init__(
8581
self,
8682
model_path: str,
@@ -96,21 +92,27 @@ def __init__(
9692
ValueError: If the TFLite model is invalid.
9793
OSError: If the current OS isn't supported by EdgeTPU.
9894
"""
95+
# Load metadata from model.
96+
displayer = metadata.MetadataDisplayer.with_model_file(model_path)
97+
98+
# Save model metadata for preprocessing later.
99+
model_metadata = json.loads(displayer.get_metadata_json())
100+
process_units = model_metadata['subgraph_metadata'][0][
101+
'input_tensor_metadata'][0]['process_units']
102+
mean = 127.5
103+
std = 127.5
104+
for option in process_units:
105+
if option['options_type'] == 'NormalizationOptions':
106+
mean = option['options']['mean'][0]
107+
std = option['options']['std'][0]
108+
self._mean = mean
109+
self._std = std
110+
99111
# Load label list from metadata.
100-
try:
101-
with zipfile.ZipFile(model_path) as model_with_metadata:
102-
if not model_with_metadata.namelist():
103-
raise ValueError('Invalid TFLite model: no label file found.')
104-
105-
file_name = model_with_metadata.namelist()[0]
106-
with model_with_metadata.open(file_name) as label_file:
107-
label_list = label_file.read().splitlines()
108-
self._labels_list = [label.decode('ascii') for label in label_list]
109-
except zipfile.BadZipFile:
110-
print(
111-
'ERROR: Please use models trained with Model Maker or downloaded from TensorFlow Hub.'
112-
)
113-
raise ValueError('Invalid TFLite model: no metadata found.')
112+
file_name = displayer.get_packed_associated_file_list()[0]
113+
label_map_file = displayer.get_associated_file_buffer(file_name).decode()
114+
label_list = list(filter(len, label_map_file.splitlines()))
115+
self._label_list = label_list
114116

115117
# Initialize TFLite model.
116118
if options.enable_edgetpu:
@@ -191,7 +193,7 @@ def _postprocess(self, output_tensor: np.ndarray) -> List[Category]:
191193
range(len(output_tensor)), key=lambda k: output_tensor[k], reverse=True)
192194

193195
categories = [
194-
Category(label=self._labels_list[idx], score=output_tensor[idx])
196+
Category(label=self._label_list[idx], score=output_tensor[idx])
195197
for idx in prob_descending
196198
]
197199

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
argparse
22
numpy>=1.20.0 # To ensure compatibility with OpenCV on Raspberry Pi.
33
opencv-python~=4.5.3.56
4+
tflite-runtime>=2.7.0
5+
tflite-support>=0.3.1

lite/examples/image_classification/raspberry_pi/requirements_pypi.txt

-3
This file was deleted.

lite/examples/image_classification/raspberry_pi/requirements_tflite.txt

-2
This file was deleted.

lite/examples/image_classification/raspberry_pi/setup.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ else
77
fi
88

99
# Install Python dependencies.
10-
python3 -m pip install -r requirements_pypi.txt
11-
python3 -m pip install -r requirements_tflite.txt
10+
python3 -m pip install pip --upgrade
11+
python3 -m pip install -r requirements.txt
1212

1313
# Download TF Lite model with metadata.
1414
FILE=${DATA_DIR}/efficientnet_lite0.tflite
@@ -24,4 +24,4 @@ if [ ! -f "$FILE" ]; then
2424
-L 'https://storage.googleapis.com/download.tensorflow.org/models/tflite/edgetpu/efficientnet-edgetpu-M_quant_edgetpu.tflite' \
2525
-o ${FILE}
2626
fi
27-
echo -e "Downloaded files are in ${DATA_DIR}"
27+
echo -e "Downloaded files are in ${DATA_DIR}"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

lite/examples/image_segmentation/raspberry_pi/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ a Raspberry Pi to perform real-time image segmentation using images streamed
55
from the camera.
66

77
At the end of this page, there are extra steps to accelerate the example using
8-
the Coral USB Accelerator, which increases the inference speed by ~10x.
8+
the Coral USB Accelerator to increase inference speed.
99

1010
## Set up your hardware
1111

lite/examples/image_segmentation/raspberry_pi/image_segmenter.py

+23-20
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
import dataclasses
1717
import enum
18+
import json
1819
import platform
1920
from typing import List, Tuple
20-
import zipfile
2121

2222
import cv2
2323
import numpy as np
24+
from tflite_support import metadata
2425

2526
# pylint: disable=g-import-not-at-top
2627
try:
@@ -388,11 +389,6 @@ def _label_to_color_image(label: np.ndarray) -> np.ndarray:
388389
class ImageSegmenter(object):
389390
"""A wrapper class for a TFLite image segmentation model."""
390391

391-
_mean = 127.5
392-
"""Default mean normalization parameter for float model."""
393-
_std = 127.5
394-
"""Default std normalization parameter for float model."""
395-
396392
def __init__(
397393
self,
398394
model_path: str,
@@ -408,21 +404,28 @@ def __init__(
408404
ValueError: If the TFLite model is invalid.
409405
OSError: If the current OS isn't supported by EdgeTPU.
410406
"""
407+
# Load metadata from model.
408+
displayer = metadata.MetadataDisplayer.with_model_file(model_path)
409+
410+
# Save model metadata for preprocessing later.
411+
model_metadata = json.loads(displayer.get_metadata_json())
412+
process_units = model_metadata['subgraph_metadata'][0][
413+
'input_tensor_metadata'][0]['process_units']
414+
415+
mean = 127.5
416+
std = 127.5
417+
for option in process_units:
418+
if option['options_type'] == 'NormalizationOptions':
419+
mean = option['options']['mean'][0]
420+
std = option['options']['std'][0]
421+
self._mean = mean
422+
self._std = std
423+
411424
# Load label list from metadata.
412-
try:
413-
with zipfile.ZipFile(model_path) as model_with_metadata:
414-
if not model_with_metadata.namelist():
415-
raise ValueError('Invalid TFLite model: no label file found.')
416-
417-
file_name = model_with_metadata.namelist()[0]
418-
with model_with_metadata.open(file_name) as label_file:
419-
label_list = label_file.read().splitlines()
420-
self._label_list = [label.decode('ascii') for label in label_list]
421-
except zipfile.BadZipFile:
422-
print(
423-
'ERROR: Please use models trained with Model Maker or downloaded from TensorFlow Hub.'
424-
)
425-
raise ValueError('Invalid TFLite model: no metadata found.')
425+
file_name = displayer.get_packed_associated_file_list()[0]
426+
label_map_file = displayer.get_associated_file_buffer(file_name).decode()
427+
label_list = list(filter(len, label_map_file.splitlines()))
428+
self._label_list = label_list
426429

427430
# Initialize TFLite model.
428431
if options.enable_edgetpu:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
argparse
2+
numpy>=1.20.0 # To ensure compatibility with OpenCV on Raspberry Pi.
3+
opencv-python~=4.5.3.56
4+
tflite-runtime>=2.7.0
5+
tflite-support>=0.3.1

lite/examples/image_segmentation/raspberry_pi/requirements_pypi.txt

-3
This file was deleted.

lite/examples/image_segmentation/raspberry_pi/requirements_tflite.txt

-2
This file was deleted.

lite/examples/image_segmentation/raspberry_pi/setup.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ else
77
fi
88

99
# Install Python dependencies
10-
python3 -m pip install -r requirements_pypi.txt
11-
python3 -m pip install -r requirements_tflite.txt
10+
python3 -m pip install pip --upgrade
11+
python3 -m pip install -r requirements.txt
1212

1313
# Download TF Lite models with metadata.
1414
FILE=${DATA_DIR}/deeplabv3.tflite
@@ -25,4 +25,4 @@ if [ ! -f "$FILE" ]; then
2525
-o ${FILE}
2626
fi
2727

28-
echo -e "Downloaded files are in ${DATA_DIR}"
28+
echo -e "Downloaded files are in ${DATA_DIR}"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

lite/examples/object_detection/raspberry_pi/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ the Pi Camera. It draws a bounding box around each detected object in the camera
66
preview (when the object score is above a given threshold).
77

88
At the end of this page, there are extra steps to accelerate the example using
9-
the Coral USB Accelerator, which increases the inference speed by ~10x.
9+
the Coral USB Accelerator to increase inference speed.
1010

1111
## Set up your hardware
1212

lite/examples/object_detection/raspberry_pi/object_detector.py

+22-20
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# limitations under the License.
1414
"""A module to run object detection with a TensorFlow Lite model."""
1515

16+
import json
1617
import platform
1718
from typing import List, NamedTuple
18-
import zipfile
1919

2020
import cv2
2121
import numpy as np
22+
from tflite_support import metadata
2223

2324
# pylint: disable=g-import-not-at-top
2425
try:
@@ -90,11 +91,6 @@ def edgetpu_lib_name():
9091
class ObjectDetector:
9192
"""A wrapper class for a TFLite object detection model."""
9293

93-
_mean = 127.5
94-
"""Default mean normalization parameter for float model."""
95-
_std = 127.5
96-
"""Default std normalization parameter for float model."""
97-
9894
_OUTPUT_LOCATION_NAME = 'location'
9995
_OUTPUT_CATEGORY_NAME = 'category'
10096
_OUTPUT_SCORE_NAME = 'score'
@@ -116,21 +112,27 @@ def __init__(
116112
OSError: If the current OS isn't supported by EdgeTPU.
117113
"""
118114

115+
# Load metadata from model.
116+
displayer = metadata.MetadataDisplayer.with_model_file(model_path)
117+
118+
# Save model metadata for preprocessing later.
119+
model_metadata = json.loads(displayer.get_metadata_json())
120+
process_units = model_metadata['subgraph_metadata'][0][
121+
'input_tensor_metadata'][0]['process_units']
122+
mean = 127.5
123+
std = 127.5
124+
for option in process_units:
125+
if option['options_type'] == 'NormalizationOptions':
126+
mean = option['options']['mean'][0]
127+
std = option['options']['std'][0]
128+
self._mean = mean
129+
self._std = std
130+
119131
# Load label list from metadata.
120-
try:
121-
with zipfile.ZipFile(model_path) as model_with_metadata:
122-
if not model_with_metadata.namelist():
123-
raise ValueError('Invalid TFLite model: no label file found.')
124-
125-
file_name = model_with_metadata.namelist()[0]
126-
with model_with_metadata.open(file_name) as label_file:
127-
label_list = label_file.read().splitlines()
128-
self._label_list = [label.decode('ascii') for label in label_list]
129-
except zipfile.BadZipFile:
130-
print(
131-
'ERROR: Please use models trained with Model Maker or downloaded from TensorFlow Hub.'
132-
)
133-
raise ValueError('Invalid TFLite model: no metadata found.')
132+
file_name = displayer.get_packed_associated_file_list()[0]
133+
label_map_file = displayer.get_associated_file_buffer(file_name).decode()
134+
label_list = list(filter(len, label_map_file.splitlines()))
135+
self._label_list = label_list
134136

135137
# Initialize TFLite model.
136138
if options.enable_edgetpu:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
argparse
2+
numpy>=1.20.0 # To ensure compatibility with OpenCV on Raspberry Pi.
3+
opencv-python~=4.5.3.56
4+
tflite-runtime>=2.7.0
5+
tflite-support>=0.3.1

lite/examples/object_detection/raspberry_pi/requirements_tflite.txt

-2
This file was deleted.

0 commit comments

Comments
 (0)