<reponame>nickbild/doc_insight_v2
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Flatten, Dense
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math


MODELS_DIR = 'models/'
if not os.path.exists(MODELS_DIR):
    os.mkdir(MODELS_DIR)
MODEL_TF = MODELS_DIR + 'model'
MODEL_NO_QUANT_TFLITE = MODELS_DIR + 'model_no_quant.tflite'
MODEL_TFLITE = MODELS_DIR + 'model.tflite'
MODEL_TFLITE_MICRO = MODELS_DIR + 'model.cc'

# Set seed for experiment reproducibility.
seed = 1
np.random.seed(seed)
tf.random.set_seed(seed)

# Load data.
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "data/",
    color_mode="grayscale",
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(96, 96),
    batch_size=32)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "data/",
    color_mode="grayscale",
    validation_split=0.2,
    subset="validation",
    seed=123,
    image_size=(96, 96),
    batch_size=32)

# Build model.
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(6, 3, activation='relu'),
    tf.keras.layers.AveragePooling2D(),
    tf.keras.layers.Conv2D(6, 3, activation='relu'),
    tf.keras.layers.AveragePooling2D(),
    tf.keras.layers.Conv2D(6, 3, activation='relu'),
    tf.keras.layers.AveragePooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(12, activation='relu'),
    tf.keras.layers.Dense(2)
])

model.compile(
    optimizer='adam',
    loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'])

# Train model.
model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=3
)

# Save model.
model.save(MODEL_TF)

# Convert the model to the TensorFlow Lite format without quantization
converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_TF)
model_no_quant_tflite = converter.convert()

# Save the model to disk
open(MODEL_NO_QUANT_TFLITE, "wb").write(model_no_quant_tflite)

# Convert the model to the TensorFlow Lite format with quantization
def representative_dataset():
  for image_batch, labels_batch in train_ds:
    yield [image_batch]
# Set the optimization flag.
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# Enforce integer only quantization
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
# Provide a representative dataset to ensure we quantize correctly.
converter.representative_dataset = representative_dataset
model_tflite = converter.convert()

# Save the model to disk
open(MODEL_TFLITE, "wb").write(model_tflite)

# Convert model for microcontroller.
os.system("xxd -i {0} > {1}".format(MODEL_TFLITE, MODEL_TFLITE_MICRO))
# Update variable names
REPLACE_TEXT = MODEL_TFLITE_MICRO.replace('/', '_').replace('.', '_')
os.system("sed -i 's/'{0}'/g_model/g' {1}".format(REPLACE_TEXT, MODEL_TFLITE_MICRO))

import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Flatten, Dense
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math


MODELS_DIR = 'models/'
if not os.path.exists(MODELS_DIR):
    os.mkdir(MODELS_DIR)
MODEL_TF = MODELS_DIR + 'model'
MODEL_NO_QUANT_TFLITE = MODELS_DIR + 'model_no_quant.tflite'
MODEL_TFLITE = MODELS_DIR + 'model.tflite'
MODEL_TFLITE_MICRO = MODELS_DIR + 'model.cc'

# Set seed for experiment reproducibility.
seed = 1
np.random.seed(seed)
tf.random.set_seed(seed)

# Load data.
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "data/",
    color_mode="grayscale",
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(96, 96),
    batch_size=32)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "data/",
    color_mode="grayscale",
    validation_split=0.2,
    subset="validation",
    seed=123,
    image_size=(96, 96),
    batch_size=32)

# Build model.
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(6, 3, activation='relu'),
    tf.keras.layers.AveragePooling2D(),
    tf.keras.layers.Conv2D(6, 3, activation='relu'),
    tf.keras.layers.AveragePooling2D(),
    tf.keras.layers.Conv2D(6, 3, activation='relu'),
    tf.keras.layers.AveragePooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(12, activation='relu'),
    tf.keras.layers.Dense(2)
])

model.compile(
    optimizer='adam',
    loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'])

# Train model.
model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=3
)

# Save model.
model.save(MODEL_TF)

# Convert the model to the TensorFlow Lite format without quantization
converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_TF)
model_no_quant_tflite = converter.convert()

# Save the model to disk
open(MODEL_NO_QUANT_TFLITE, "wb").write(model_no_quant_tflite)

# Convert the model to the TensorFlow Lite format with quantization
def representative_dataset():
  for image_batch, labels_batch in train_ds:
    yield [image_batch]
# Set the optimization flag.
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# Enforce integer only quantization
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
# Provide a representative dataset to ensure we quantize correctly.
converter.representative_dataset = representative_dataset
model_tflite = converter.convert()

# Save the model to disk
open(MODEL_TFLITE, "wb").write(model_tflite)

# Convert model for microcontroller.
os.system("xxd -i {0} > {1}".format(MODEL_TFLITE, MODEL_TFLITE_MICRO))
# Update variable names
REPLACE_TEXT = MODEL_TFLITE_MICRO.replace('/', '_').replace('.', '_')
os.system("sed -i 's/'{0}'/g_model/g' {1}".format(REPLACE_TEXT, MODEL_TFLITE_MICRO))
# Set seed for experiment reproducibility. # Load data. # Build model. # Train model. # Save model. # Convert the model to the TensorFlow Lite format without quantization # Save the model to disk # Convert the model to the TensorFlow Lite format with quantization # Set the optimization flag. # Enforce integer only quantization # Provide a representative dataset to ensure we quantize correctly. # Save the model to disk # Convert model for microcontroller. # Update variable names