<jupyter_start><jupyter_text>1. Design the model<jupyter_code>#all_cnn_net
# https://github.com/AnuragGorkar/Image_Recognition_using_AllCNN_architechture/blob/master/Image_Recognition_using_AllCNN_architechture.ipynb
model = Sequential()

model.add(Conv2D(96, (3, 3), padding = "same", input_shape = (32, 32, 3))) #----> 1 #n,h,w,c
model.add(Activation('relu')) #----> 1

    # INTERMEDIATE MODEL LAYERS
model.add(Conv2D(96, (3, 3), padding = "same")) #----> 2
model.add(Activation('relu')) #----> 2

model.add(Conv2D(96, (3, 3), padding = "same", strides = (2,2))) #----> 3
model.add(Dropout(0.5))

model.add(Conv2D(192, (3,3), padding = "same")) #----> 4
model.add(Activation('relu')) #----> 4

model.add(Conv2D(192, (3,3), padding = "same")) #----> 5
model.add(Activation('relu')) #----> 5

model.add(Conv2D(192, (3,3), padding = "same", strides = (2,2))) #----> 6
model.add(Dropout(0.5)) #----> 6

model.add(Conv2D(192, (3,3),padding = "same")) #----> 7
model.add(Activation('relu')) #----> 7

model.add(Conv2D(192, (1,1),padding = "valid")) #----> 8
model.add(Activation('relu')) #----> 8

model.add(Conv2D(10, (1,1),padding = "valid")) #----> 9
model.add(Activation('relu')) #----> 9

# ADD GLOBAL AVERAGE POOLING LAYER WITH SOFTMAX
model.add(GlobalAveragePooling2D())
model.add(Activation('softmax'))
#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

print (model.summary())
model_select = Sequential()

model_select.add(Conv2D(96, (3, 3), padding = "same", input_shape = (32, 32, 3))) #----> 1 #n,h,w,c
model_select.add(Activation('relu')) #----> 1

print (model_select.summary())<jupyter_output>Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_10 (Conv2D)           (None, 32, 32, 96)        2688      
_________________________________________________________________
activation_9 (Activation)    (None, 32, 32, 96)        0         
=================================================================
Total params: 2,688
Trainable params: 2,688
Non-trainable params: 0
_________________________________________________________________
None<jupyter_text>2. Train Model (skip now)<jupyter_code>#(x_train, y_train),(x_test, y_test) = cifar10.load_data()
#model_select.save(MODEL_TF)
model.save(MODEL_TF)<jupyter_output>INFO:tensorflow:Assets written to: models/model/assets<jupyter_text>3. Generate a TensorFlow Lite Model  1. Generate Models with or without QuantizationWe now have an acceptably accurate model. We'll use the TensorFlow Lite Converter to convert the model into a special, space-efficient format for use on memory-constrained devices.Since this model is going to be deployed on a microcontroller, we want it to be as tiny as possible! One technique for reducing the size of a model is called quantization. It reduces the precision of the model's weights, and possibly the activations (output of each layer) as well, which saves memory, often without much impact on accuracy. Quantized models also run faster, since the calculations required are simpler.In the following cell, we'll convert the model twice: once with quantization, once without<jupyter_code>np.random.seed(0)
# 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 _ in range(100):
      data = np.random.rand(1, 32, 32, 3)
      yield [data.astype(np.float32)]
# 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)<jupyter_output><empty_output><jupyter_text>Generate a TensorFlow Lite for Microcontrollers ModelConvert the TensorFlow Lite quantized model into a C source file that can be loaded by TensorFlow Lite for Microcontrollers.<jupyter_code># Install xxd if it is not available
#!apt-get update && apt-get -qq install xxd
# Convert to a C source file, i.e, a TensorFlow Lite for Microcontrollers model
!xxd -i {MODEL_TFLITE} > {MODEL_TFLITE_MICRO}
# Update variable names
REPLACE_TEXT = MODEL_TFLITE.replace('/', '_').replace('.', '_')
!sed -i 's/'{REPLACE_TEXT}'/g_model/g' {MODEL_TFLITE_MICRO}
# Reference: https://www.tensorflow.org/lite/performance/post_training_quantization
interpreter = tf.lite.Interpreter(model_path=str(MODEL_TFLITE))
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()[0]
print(input_details)
output_details = interpreter.get_output_details()[0]

input_data = np.random.rand(1, 32, 32, 3)
#print(input_data)
# Check if the input type is quantized, then rescale input data to uint8
if input_details['dtype'] == np.int8:
    input_scale, input_zero_point = input_details["quantization"]
    input_data = input_data / input_scale + input_zero_point
    input_data = input_data.astype(np.int8)

interpreter.set_tensor(input_details["index"], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details["index"])[0]
# save input and expected output
np.save(INPUT_DATA_NPY, input_data)
np.save(OUTPUT_DATA_NPY, output_data)
!xxd -i {INPUT_DATA_NPY} > {MODEL_INPUT}
!xxd -i {OUTPUT_DATA_NPY} > {MODEL_OUTPUT}
print(input_data[0,0,0,:])
print(output_data.shape)
print(input_details["quantization"])
print(output_data)<jupyter_output>[   0   -6 -100]
(10,)
(0.003921564668416977, -128)
[-102 -102 -102 -102 -103 -103 -103 -103 -103 -102]
{'fraction_non_alphanumeric': 0.15355866103347415, 'fraction_numerical': 0.03688657783555411, 'mean_word_length': 5.575854700854701, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 36, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '339445', 'license': 'Apache-2.0', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2042, 'n_words': 506, 'path': 'tensorflow/lite/micro/examples/network_tester/train/train_network_tester_model.ipynb', 'repo_name': 'pyjhzwh/tflite-micro', 'size': '6154'}