tflite_model_file = os.path.join(model_dir, model_name) tflite_model_buf = open(tflite_model_file, "rb").read() # Get TFLite model from buffer try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) dtype = "float32" width = 224 height = 224 image_data = load_test_image(dtype, width, height) input_tensor = "input" input_shape = (1, 224, 224, 3) input_dtype = dtype # Parse TFLite model and convert it to a Relay module mod, params = relay.frontend.from_tflite( tflite_model, shape_dict={input_tensor: input_shape}, dtype_dict={input_tensor: input_dtype}) tvm_target = get_tvm_target(device, get_device_type(), get_device_arch(), get_device_attributes()) tvm_targets = tvm.target.Target(tvm_target)
model_dir = download_model_zoo(model_dir, model_name) tflite_model_file = os.path.join(model_dir, model_name) tflite_model_buf = open(tflite_model_file, "rb").read() # Get TFLite model from buffer try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) dtype = "uint8" image_data = load_test_image(dtype) input_tensor = "input" input_shape = (1, 224, 224, 3) #input_dtype = "float32" input_dtype = dtype # Parse TFLite model and convert it to a Relay module mod, params = relay.frontend.from_tflite( tflite_model, shape_dict={input_tensor: input_shape}, dtype_dict={input_tensor: input_dtype}) desired_layouts = {'qnn.conv2d': ['NCHW', 'default']} seq = tvm.transform.Sequential([ relay.transform.RemoveUnusedFunctions(), relay.transform.ConvertLayout(desired_layouts)
model_name = 'mobilenet_v1_1.0_224_quant.tflite' repeat = 10 model_dir = download_model_zoo(model_dir, model_name) tflite_model_file = os.path.join(model_dir, model_name) tflite_model_buf = open(tflite_model_file, "rb").read() try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) interpreter = Interpreter(tflite_model_file, num_threads=get_cpu_count()) interpreter.allocate_tensors() _, height, width, _ = interpreter.get_input_details()[0]['shape'] image = load_test_image('uint8', height, width) numpy_time = np.zeros(repeat) for i in range(0, repeat): start_time = time.time() results = classify_image(interpreter, image) elapsed_ms = (time.time() - start_time) * 1000 numpy_time[i] = elapsed_ms print("tflite %-20s %-19s (%s)" % (model_name, "%.2f ms" % np.mean(numpy_time), "%.2f ms" % np.std(numpy_time)))
model_name = 'inception_v3.tflite' repeat = 10 model_dir = download_model_zoo(model_dir, model_name) tflite_model_file = os.path.join(model_dir, model_name) tflite_model_buf = open(tflite_model_file, "rb").read() try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) interpreter = Interpreter(tflite_model_file, num_threads=get_cpu_count()) interpreter.allocate_tensors() _, height, width, _ = interpreter.get_input_details()[0]['shape'] image = load_test_image('float32', height, width) numpy_time = np.zeros(repeat) for i in range(0, repeat): start_time = time.time() results = classify_image(interpreter, image) elapsed_ms = (time.time() - start_time) * 1000 numpy_time[i] = elapsed_ms print("tflite %-20s %-19s (%s)" % (model_name, "%.2f ms" % np.mean(numpy_time), "%.2f ms" % np.std(numpy_time)))
model_dir = download_model_zoo(model_dir, model_name) tflite_model_file = os.path.join(model_dir, model_name) tflite_model_buf = open(tflite_model_file, "rb").read() # Get TFLite model from buffer try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) dtype = "float32" image_data = load_test_image(dtype, 299, 299) input_tensor = "input" input_shape = (1, 299, 299, 3) input_dtype = dtype # Parse TFLite model and convert it to a Relay module mod, params = relay.frontend.from_tflite( tflite_model, shape_dict={input_tensor: input_shape}, dtype_dict={input_tensor: input_dtype}) desired_layouts = {'nn.conv2d': ['NCHW', 'default']} seq = tvm.transform.Sequential([ relay.transform.RemoveUnusedFunctions(), relay.transform.ConvertLayout(desired_layouts) ])
model_dir = download_model_zoo(model_dir, model_name) tflite_model_file = os.path.join(model_dir, model_name) tflite_model_buf = open(tflite_model_file, "rb").read() # Get TFLite model from buffer try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) dtype="float32" image_data = load_test_image(dtype, 416, 416) input_tensor = "input_1" input_shape = (1, 416, 416, 3) input_dtype = dtype # Parse TFLite model and convert it to a Relay module mod, params = relay.frontend.from_tflite(tflite_model, shape_dict={input_tensor: input_shape}, dtype_dict={input_tensor: input_dtype}) desired_layouts = {'nn.conv2d': ['NCHW', 'default']} seq = tvm.transform.Sequential([relay.transform.RemoveUnusedFunctions(),relay.transform.ConvertLayout(desired_layouts)]) with tvm.transform.PassContext(opt_level=3): mod = seq(mod)