Update README.md
Browse files
README.md
CHANGED
|
@@ -32,7 +32,9 @@ Converted to TFLite Float32 & Float16 formats by Youssef Boulaouane.
|
|
| 32 |
## Model Usage
|
| 33 |
### Image Classification in Python
|
| 34 |
```python
|
|
|
|
| 35 |
import tensorflow as tf
|
|
|
|
| 36 |
|
| 37 |
# Load label file
|
| 38 |
with open('imagenet_classes.txt', 'r') as file:
|
|
@@ -47,16 +49,15 @@ input_details = tfl_model.get_input_details()
|
|
| 47 |
output_details = tfl_model.get_output_details()
|
| 48 |
|
| 49 |
# Load and preprocess the image
|
| 50 |
-
|
| 51 |
-
std = [0.229, 0.224, 0.225]
|
| 52 |
|
| 53 |
-
image =
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
image =
|
| 59 |
-
image = (image
|
| 60 |
|
| 61 |
# Inference and postprocessing
|
| 62 |
input = input_details[0]
|
|
@@ -68,6 +69,13 @@ tfl_output_tensor = tf.convert_to_tensor(tfl_output)
|
|
| 68 |
tfl_softmax_output = tf.nn.softmax(tfl_output_tensor, axis=1)
|
| 69 |
|
| 70 |
tfl_top5_probs, tfl_top5_indices = tf.math.top_k(tfl_softmax_output, k=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
```
|
| 72 |
|
| 73 |
### Deployment on Mobile
|
|
|
|
| 32 |
## Model Usage
|
| 33 |
### Image Classification in Python
|
| 34 |
```python
|
| 35 |
+
import numpy as np
|
| 36 |
import tensorflow as tf
|
| 37 |
+
from PIL import Image
|
| 38 |
|
| 39 |
# Load label file
|
| 40 |
with open('imagenet_classes.txt', 'r') as file:
|
|
|
|
| 49 |
output_details = tfl_model.get_output_details()
|
| 50 |
|
| 51 |
# Load and preprocess the image
|
| 52 |
+
image = Image.open(image_path).resize((224, 224), Image.BICUBIC)
|
|
|
|
| 53 |
|
| 54 |
+
image = np.array(image, dtype=np.float32)
|
| 55 |
+
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
| 56 |
+
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
| 57 |
+
image = (image / 255.0 - mean) / std
|
| 58 |
+
|
| 59 |
+
image = np.expand_dims(image, axis=-1)
|
| 60 |
+
image = np.rollaxis(image, 3)
|
| 61 |
|
| 62 |
# Inference and postprocessing
|
| 63 |
input = input_details[0]
|
|
|
|
| 69 |
tfl_softmax_output = tf.nn.softmax(tfl_output_tensor, axis=1)
|
| 70 |
|
| 71 |
tfl_top5_probs, tfl_top5_indices = tf.math.top_k(tfl_softmax_output, k=5)
|
| 72 |
+
|
| 73 |
+
# Get the top5 class labels and probabilities
|
| 74 |
+
tfl_probs_list = tfl_top5_probs[0].numpy().tolist()
|
| 75 |
+
tfl_index_list = tfl_top5_indices[0].numpy().tolist()
|
| 76 |
+
|
| 77 |
+
for index, prob in zip(tfl_index_list, tfl_probs_list):
|
| 78 |
+
print(f"{index_to_label[index]}: {round(prob*100, 2)}%")
|
| 79 |
```
|
| 80 |
|
| 81 |
### Deployment on Mobile
|