GSOC Log jiyeon's log

[Week 4] implement an estimator which uses simple regression

1. Seperate all the code and Test it on the device

Successfully I can get the result what we want, the benchmark, latency time. But even though I seperate the code, but it still looks complicated. 😂

Now I’m getting the benchmarks of 19200 samples (still running).

run_bazel.py

import os
import subprocess
import sqlite3
import numpy as np
import pandas as pd

def run_bazel(model_name):
    proc = subprocess.Popen(
        ['sh', 'gsoc_proj/run.sh', './gsoc_proj/MODELS/{}'.format(model_name)],
        stdout = subprocess.PIPE
    )
    out, err = proc.communicate()
    latency_time = [float(i.replace('ms', '')) for i in out.decode('utf-8').split('\n')[-11:-1]] # only gpu inference latency time..
    return sum(latency_time) / len(latency_time)

def test(model_name):
    proc = subprocess.Popen(
        ['sh', 'gsoc_proj/run.sh', './gsoc_proj/MODELS/{}'.format(model_name)],
        stdout = subprocess.PIPE
    )
    out, err = proc.communicate()
    print(out)
    print("\n", err)
    return out

if __name__ == "__main__":
    #sqlite3 connect
    conn = sqlite3.connect('gsoc.db')
    curr = conn.cursor()

    # read model and run 
    path = os.path.dirname(os.path.abspath('__file__'))
    path = os.path.join(path, 'gsoc_proj', 'MODELS')
    model_list = os.listdir(path)
    for model_name in model_list:
        print(model_name, " bazel run start!")
        latency = run_bazel(model_name)
        print(latency)
        sql = f"UPDATE gsoc SET latency_time={latency} WHERE tflite_name={model_name}"
        curr.execute(sql)

generate_model.py

import os
import subprocess
import sqlite3
from itertools import product
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras

kernel_list = [2*i+1 for i in range(10)]
filter_list = [2**i for i in range(4, 8)]
input_img_list = [(i, i, 3) for i in range(32, 512)]

def init():
    conn = sqlite3.connect('gsoc.db', isolation_level=None)
    cursor = conn.cursor()
    cursor.execute(
        """CREATE TABLE gsoc(
            tflite_name varchar,
            kernel int,
            filter int,
            input_shape varchar,
            latency_time varchar
        )"""
    )
    conn.close()


def generate_model(kernel_list=kernel_list, filter_list=filter_list, input_img_list=input_img_list):
    comb = list(product(kernel_list, filter_list, input_img_list))
    conn = sqlite3.connect('gsoc.db', isolation_level=None)
    curr = conn.cursor()

    for com in comb:
        _kernel, _filter, _input_shape = com
        model = keras.Sequential(
            keras.layers.Conv2D(filters=_filter, kernel_size=_kernel, input_shape=_input_shape,
            padding='same', activation='relu')
        )
        converter = tf.lite.TFLiteConverter.from_keras_model(model)
        tflite_model = converter.convert()

        model_name = "kernel_{}_filter_{}_input_shape_{}.tflite".format(_kernel, _filter, _input_shape)
        with open("./MODELS/" + model_name, 'wb') as f:
            f.write(tflite_model)
        
        sql = f'INSERT INTO gsoc VALUES(tflite_name={model_name}, {_kernel}, {_filter}, {_input_shape}, 0)'.replace('"', '')
        try:
            curr.execute('''CREATE TABLE gsoc(
                    tflite_name varchar(100),
                    kernel int,
                    filter int,
                    input_shape varchar(100),
                    latency_time varchar(100))
                ''')
        except:
            pass
        finally:
            try:
                curr.execute(sql)
            except:
                print(sql)
                break
    conn.close()

if __name__ == "__main__":
    generate_model()

2. How to get Conv2D Params

I checked the visualization.py from our slack channel.

import re

def CamelCaseToSnakeCase(camel_case_input):
  """Converts an identifier in CamelCase to snake_case."""
  s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", camel_case_input)
  return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()


def FlatbufferToDict(fb, preserve_as_numpy):
  """Converts a hierarchy of FB objects into a nested dict.
  We avoid transforming big parts of the flat buffer into python arrays. This
  speeds conversion from ten minutes to a few seconds on big graphs.
  Args:
    fb: a flat buffer structure. (i.e. ModelT)
    preserve_as_numpy: true if all downstream np.arrays should be preserved.
      false if all downstream np.array should become python arrays
  Returns:
    A dictionary representing the flatbuffer rather than a flatbuffer object.
  """
  if isinstance(fb, int) or isinstance(fb, float) or isinstance(fb, str):
    return fb
  elif hasattr(fb, "__dict__"):
    result = {}
    for attribute_name in dir(fb):
      attribute = fb.__getattribute__(attribute_name)
      if not callable(attribute) and attribute_name[0] != "_":
        snake_name = CamelCaseToSnakeCase(attribute_name)
        preserve = True if attribute_name == "buffers" else preserve_as_numpy
        result[snake_name] = FlatbufferToDict(attribute, preserve)
    return result
  elif isinstance(fb, np.ndarray):
    return fb if preserve_as_numpy else fb.tolist()
  elif hasattr(fb, "__len__"):
    return [FlatbufferToDict(entry, preserve_as_numpy) for entry in fb]
  else:
    return fb


def CreateDictFromFlatbuffer(buffer_data):
  model_obj = schema_fb.Model.GetRootAsModel(buffer_data, 0)
  model = schema_fb.ModelT.InitFromObj(model_obj)
  return FlatbufferToDict(model, preserve_as_numpy=False)


data = CreateDictFromFlatbuffer(file_data)
print(data)

And below is the result.

{'buffers': [{'data': None},
  {'data': array([154, 224, 234, 190, 186, 156, 181,  62, 165,  62,  10,  64,  67,
           95, 193, 191, 132,   0, 133, 190,  96,  33,  18,  63, 241, 171,
           57,  64, 119,  46,  77,  64, 170, 189, 109, 191, 212, 110,  50,
           64,  59, 158,  35,  64,  81, 135, 219,  61,  76, 231,  72,  64,
           81, 186,  66,  64, 238, 177,  23,  64,  84, 240, 179,  60, 197,
           50,  56, 192, 168, 143,  29,  64,  29, 151,  81,  64,  70,  95,
           86,  64,  18, 153,  31, 191, 162,  34, 202, 191,   1,  96, 233,
           63, 124,  27,  16,  64,  91,   9, 127,  63, 220, 173,  67,  64,
           32, 192,  17,  64, 201,  95,   8,  64,  67, 240,  89,  64,  80,
          206,  30,  64, 187, 245, 232,  63, 140, 255, 134,  63], dtype=uint8)},
  {'data': None},
  {'data': array([ 72, 153,  63, ..., 232,  41,  59], dtype=uint8)},
  {'data': None}],
 'description': [84,
  79,
  67,
  79,
  32,
  67,
  111,
  110,
  118,
  101,
  114,
  116,
  101,
  100,
  46],
 'metadata': None,
 'metadata_buffer': None,
 'operator_codes': [{'builtin_code': 0,
   'custom_code': None,
   'deprecated_builtin_code': 3,
   'version': 1}],
 'signature_defs': None,
 'subgraphs': [{'inputs': [3],
   'name': None,
   'operators': [{'builtin_options': {'dilation_h_factor': 1,
      'dilation_w_factor': 1,
      'fused_activation_function': 3,
      'padding': 0,
      'stride_h': 2,
      'stride_w': 2},
     'builtin_options_type': 1,
     'custom_options': None,
     'custom_options_format': 0,
     'inputs': [3, 0, 2],
     'intermediates': None,
     'mutating_variable_inputs': None,
     'opcode_index': 0,
     'outputs': [1]}],
   'outputs': [1],
   'tensors': [{'buffer': 3,
     'is_variable': False,
     'name': [77,
      111,
      98,
      105,
      108,
      101,
      110,
      101,
      116,
      86,
      49,
      47,
      67,
      111,
      110,
      118,
      50,
      100,
      95,
      48,
      47,
      119,
      101,
      105,
      103,
      104,
      116,
      115],
     'quantization': {'details': None,
      'details_type': 0,
      'max': None,
      'min': None,
      'quantized_dimension': 0,
      'scale': None,
      'zero_point': None},
     'shape': [32, 3, 3, 3],
     'shape_signature': None,
     'sparsity': None,
     'type': 0},
    {'buffer': 2,
     'is_variable': False,
     'name': [77,
      111,
      98,
      105,
      108,
      101,
      110,
      101,
      116,
      86,
      49,
      47,
      77,
      111,
      98,
      105,
      108,
      101,
      110,
      101,
      116,
      86,
      49,
      47,
      67,
      111,
      110,
      118,
      50,
      100,
      95,
      48,
      47,
      82,
      101,
      108,
      117,
      54],
     'quantization': {'details': None,
      'details_type': 0,
      'max': None,
      'min': None,
      'quantized_dimension': 0,
      'scale': None,
      'zero_point': None},
     'shape': [1, 112, 112, 32],
     'shape_signature': None,
     'sparsity': None,
     'type': 0},
    {'buffer': 1,
     'is_variable': False,
     'name': [77,
      111,
      98,
      105,
      108,
      101,
      110,
      101,
      116,
      86,
      49,
      47,
      77,
      111,
      98,
      105,
      108,
      101,
      110,
      101,
      116,
      86,
      49,
      47,
      67,
      111,
      110,
      118,
      50,
      100,
      95,
      48,
      47,
      99,
      111,
      110,
      118,
      111,
      108,
      117,
      116,
      105,
      111,
      110,
      95,
      98,
      105,
      97,
      115],
     'quantization': {'details': None,
      'details_type': 0,
      'max': None,
      'min': None,
      'quantized_dimension': 0,
      'scale': None,
      'zero_point': None},
     'shape': [32],
     'shape_signature': None,
     'sparsity': None,
     'type': 0},
    {'buffer': 4,
     'is_variable': False,
     'name': [105, 110, 112, 117, 116],
     'quantization': {'details': None,
      'details_type': 0,
      'max': [255.0],
      'min': [0.0],
      'quantized_dimension': 0,
      'scale': None,
      'zero_point': None},
     'shape': [1, 224, 224, 3],
     'shape_signature': None,
     'sparsity': None,
     'type': 0}]}],
 'version': 3}

I couldn’t go through all the codes in detail, but maybe I can get the conv2d params like stride, padding, etc.

Thinks to check

  • SQLite3 Syntax Error
import sqlite3

conn = sqlite3.connect('gsoc.db')
curr = conn.cursor()

sql = "INSERT INTO gsoc VALUES(kernel_1_filter_16_input_shape_(32, 32, 3).tflite, 1, 16, (32, 32, 3), 0)"
curr.execute(sql)

>>>
Traceback (most recent call last):
  File "generate_model.py", line 84, in <module>
    generate_model()
  File "generate_model.py", line 59, in generate_model
    curr.execute(sql)
sqlite3.OperationalError: near ".": syntax error

-> But this is very small and minor error

  • Finish making database
  • Data Analysis
  • Simple Regression Model