Skip to content

Instantly share code, notes, and snippets.

@estebanuri
estebanuri / mobilenet_conversion_sanity_check2.py
Created June 18, 2020 18:58
FaceNet TF -> Keras -> TF Lite conversion
def pre_process(face, required_size=(160, 160)):
ret = cv2.resize(face, required_size)
#ret = cv2.cvtColor(ret, cv2.COLOR_BGR2RGB)
ret = ret.astype('float32')
# standardize pixel values across channels (global)
mean, std = ret.mean(), ret.std()
ret = (ret - mean) / std
return ret
@estebanuri
estebanuri / facenet_conversion_sanity_check.py
Created June 18, 2020 18:54
FaceNet TF -> Keras -> TF Lite conversion
import cv2
import matplotlib.pyplot as plt
from mtcnn.mtcnn import MTCNN
def read_image(file):
img = cv2.imread(file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img
def crop_bb(image, detection, margin):
@estebanuri
estebanuri / TFLiteObjectDetectionAPIModel.java
Last active June 17, 2020 09:34
Changes made to original model implementation for supporting face recognition
// changes made to the model implementation
public class TFLiteObjectDetectionAPIModel
implements SimilarityClassifier {
...
// the output size is 192
private static final int OUTPUT_SIZE = 192;
@estebanuri
estebanuri / SimilarityClassifier.java
Last active June 17, 2020 09:26
Changes made to original model for supporting face recognition
// Changes made to the model interface
// This interface was renamed from Classifier to SimilarityClassifier
public interface SimilarityClassifier {
// added method for registering samples into the dataset
void register(String name, Recognition recognition);
...
public class Recognition {
@estebanuri
estebanuri / mobilenet_sandberg_conversion.py
Created June 16, 2020 03:57
Converting Keras Sandberg's model to TensorFlow Lite
import tensorflow.lite as lite
input_file = "models/sandberg/facenet_keras.h5"
output_file = "models/sandberg/facenet.tflite"
# Converts the Keras model to TensorFlow Lite
converter = lite.TocoConverter.from_keras_model_file(input_file)
converter.post_training_quantize = True
tflite_model = converter.convert()
open(output_file, "wb").write(tflite_model)
@estebanuri
estebanuri / hirokis_norm.py
Created June 15, 2020 19:17
The norm of the embeedings from the Hirokis Keras trained model is not 1.0!
import numpy as np
from tensorflow.keras import models
# Loads Hiroki's FaceNet model
model_file = 'models/hiroki/facenet_keras.h5'
hiroki_model = models.load_model(model_file)
# Creates a random image
img = np.random.randint(0, 255, (160, 160, 3), dtype='uint8')
@estebanuri
estebanuri / keras_2_tflite.py
Last active June 15, 2020 17:28
How to convert from Keras to TensorFlow Lite post-quantized
import tensorflow.lite as lite
input_file = "models/hiroki/facenet_keras.h5"
output_file = "models/hiroki/facenet.tflite"
# Converts the Keras model to TensorFlow Lite
converter = lite.TocoConverter.from_keras_model_file(input_file)
converter.post_training_quantize = True
tflite_model = converter.convert()
open(output_file, "wb").write(tflite_model)
@estebanuri
estebanuri / TFLiteObjectDetectionAPIModel.java
Created June 8, 2020 04:21
Modifying to fit face mask detector
@Override
public List<Recognition> recognizeImage(final Bitmap bitmap) {
...
Object[] inputArray = {imgData};
// Here outputMap is changed to fit the Face Mask detector
Map<Integer, Object> outputMap = new HashMap<>();
output = new float[1][2];
public class DetectorActivity
extends CameraActivity
implements OnImageAvailableListener {
...
// Face Mask
private static final int TF_OD_API_INPUT_SIZE = 224;
private static final boolean TF_OD_API_IS_QUANTIZED = false;
private static final String TF_OD_API_MODEL_FILE = "mask_detector.tflite";
private void onFacesDetected(long currTimestamp, List<Face> faces) {
final List<Classifier.Recognition> mappedRecognitions =
new LinkedList<Classifier.Recognition>();
...
final Canvas cvFace = new Canvas(faceBmp);