Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue regarding adding custom model to menu list #456

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# CFU Playground

Want a faster ML processor? Do it yourself!

This project provides a framework that an engineer, intern, or student can use to design and evaluate **enhancements** to an FPGA-based “soft” processor, specifically to increase the performance of machine learning (ML) tasks. The goal is to abstract away most infrastructure details so that the user can get up to speed quickly and focus solely on adding new processor instructions, exploiting them in the computation, and measuring the results.
Expand Down
10 changes: 10 additions & 0 deletions common/src/models/models.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "models/mlcommons_tiny_v01/vww/vww.h"
#include "models/mnv2/mnv2.h"
#include "models/pdti8/pdti8.h"
#include "models/neural_net1/neural_net1.h"
#include "models/neural_net_svhn/neural_net_svhn.h"

inline void no_menu() {}

Expand All @@ -40,6 +42,14 @@ static struct Menu MENU = {
"TfLM Models",
"models",
{
#if defined(INCLUDE_MODEL_NEURAL_NET1) || defined(INCLUDE_ALL_TFLM_EXAMPLES)
MENU_ITEM(AUTO_INC_CHAR, "Neural_net1 model", neural_net1_menu),
#endif

#if defined(INCLUDE_MODEL_NEURAL_NET_SVHN) || defined(INCLUDE_ALL_TFLM_EXAMPLES)
MENU_ITEM(AUTO_INC_CHAR, "Neural_net_svhn model", neural_net_svhn_menu),
#endif

#if defined(INCLUDE_MODEL_PDTI8) || defined(INCLUDE_ALL_TFLM_EXAMPLES)
MENU_ITEM(AUTO_INC_CHAR, "Person Detection int8 model", pdti8_menu),
#endif
Expand Down
34,056 changes: 34,056 additions & 0 deletions common/src/models/neural_net1/model_neural_net1.h

Large diffs are not rendered by default.

Binary file not shown.
114 changes: 114 additions & 0 deletions common/src/models/neural_net1/neural_net1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2021 The CFU-Playground Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "models/neural_net1/neural_net1.h"

#include <stdio.h>

#include "menu.h"
#include "models/neural_net1/model_neural_net1.h"
#include "/home/shivaubuntu/Tensorflow/mnist_test_data/mnist_img_data.cc"
#include "tflite.h"


extern "C" {
#include "fb_util.h"
};


// Initialize everything once
// deallocate tensors when done
static void neural_net1_init(void) {
tflite_load_model(model_neural_net1_tflite, model_neural_net1_tflite_len);
}

// Run classification, after input has been loaded
static int32_t neural_net1_classify() {
printf("Running neural_net1\n");
tflite_classify();

// Process the inference results.
int8_t* output = tflite_get_output();
int8_t max_conf=0;
for(int i=0;i<10;i++)
{
if(output[i]>=output[max_conf])
max_conf = i;
printf("conf lvl for %d : %d\n",i,output[i]);
}
printf("OUT: %d\n",max_conf);
return max_conf;

}


#define NUM_GOLDEN 2
static int32_t golden_results[NUM_GOLDEN] = {0, 2};

static void do_golden_tests() {
int32_t actual[NUM_GOLDEN];


tflite_set_input_zeros();
actual[0] = neural_net1_classify();
printf("output @ all 0s inp: %ld", actual[0]);
tflite_set_input(g_mnist_img_data);
actual[1] = neural_net1_classify();

#ifdef CSR_VIDEO_FRAMEBUFFER_BASE
char msg_buff[256] = { 0 };
fb_clear();
memset(msg_buff, 0x00, sizeof(msg_buff));
snprintf(msg_buff, sizeof(msg_buff), "Result is %ld, Expected is %ld", actual[1], golden_results[1]);
flush_cpu_dcache();
flush_l2_cache();
#endif

if(actual[1]!= golden_results[1])
printf("Failed golden test..check again");



}


static void test() {
printf("hi");

}
static struct Menu MENU = {
"Tests for MNIST neural_net1 model",
"neural_net1",
{
MENU_ITEM('1', "Test- say hi", test),
MENU_ITEM('g', "Run golden tests (check for expected outputs)",
do_golden_tests),
MENU_END,
},
};

// For integration into menu system
void neural_net1_menu() {
neural_net1_init();

#ifdef CSR_VIDEO_FRAMEBUFFER_BASE
fb_init();
flush_cpu_dcache();
flush_l2_cache();
#endif

menu_run(&MENU);
}
31 changes: 31 additions & 0 deletions common/src/models/neural_net1/neural_net1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2021 The CFU-Playground Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _NEURAL_NET1_H
#define _NEURAL_NET1_H

#ifdef __cplusplus
extern "C" {
#endif

// For integration into menu system
void neural_net1_menu();

#ifdef __cplusplus
}
#endif

#endif // _NEURAL_NET1_H
77 changes: 77 additions & 0 deletions common/src/models/neural_net1/neural_net1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import tensorflow as tf
import numpy as np
import tensorflow_datasets as tfds
(ds_train, ds_test), ds_info = tfds.load(
'mnist',
split=['train', 'test'],
shuffle_files=True,
as_supervised=True,
with_info=True,
)
def normalize_img(image, label):
"""Normalize image"""
return tf.cast(image, tf.float32) / 255., label

ds_train = ds_train.map( normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(128)
ds_train = ds_train.prefetch(tf.data.AUTOTUNE)

""" Testing pipeline"""
ds_test = ds_test.map(
normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_test = ds_test.batch(128)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.AUTOTUNE)


model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
run_eagerly = True
)
model.fit(
ds_train,
epochs=6,
validation_data=ds_test,
)

"""Custom Inference test"""
model.summary
count = 0
#for data in ds_train:
# print(model(data[0]))

""" Converting to TFlite"""
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
f.write(tflite_model)

interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
""" Giving random input to model to see if it is computing properly"""
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

print("Evaluate on test data")
results = model.evaluate(ds_test, batch_size=128)
print("test loss, test acc:", results)

Loading