Skip to content
Snippets Groups Projects
Commit a5f68c94 authored by Hashim Sharif's avatar Hashim Sharif
Browse files

TensorRT frontend changes for DepthwiseConv2D

parent 9bc2ca73
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ import numpy as np ...@@ -4,6 +4,7 @@ import numpy as np
from frontend.promise_translator import PromiseRtTranslator from frontend.promise_translator import PromiseRtTranslator
from frontend.hpvm_dfg_translator import HPVMTranslator from frontend.hpvm_dfg_translator import HPVMTranslator
from frontend.weight_utils import dumpLabels, dumpData, dumpConvWeights, dumpFcWeights, dumpFcBias from frontend.weight_utils import dumpLabels, dumpData, dumpConvWeights, dumpFcWeights, dumpFcBias
from frontend.utils import *
import keras import keras
import os import os
...@@ -23,6 +24,7 @@ class DFG: ...@@ -23,6 +24,7 @@ class DFG:
layer_name = layer.__class__.__name__ layer_name = layer.__class__.__name__
singleInLayers = {} singleInLayers = {}
singleInLayers["DepthwiseConv2D"] = True
singleInLayers["Conv2D"] = True singleInLayers["Conv2D"] = True
singleInLayers["Dense"] = True singleInLayers["Dense"] = True
singleInLayers["MaxPooling2D"] = True singleInLayers["MaxPooling2D"] = True
...@@ -145,7 +147,7 @@ class DFGNode: ...@@ -145,7 +147,7 @@ class DFGNode:
self.layer_name = layer.name # unique layer identifier self.layer_name = layer.name # unique layer identifier
print (self.layer_name) print (self.layer_name)
if layer_type == "Conv2D" or layer_type == "Dense": if layer_type == "Conv2D" or layer_type == "DepthwiseConv2D" or layer_type == "Dense":
self.weights = layer.get_weights()[0] self.weights = layer.get_weights()[0]
print("\t", self.weights.shape) print("\t", self.weights.shape)
self.use_bias = layer.use_bias self.use_bias = layer.use_bias
...@@ -154,20 +156,23 @@ class DFGNode: ...@@ -154,20 +156,23 @@ class DFGNode:
self.use_bias = layer.use_bias self.use_bias = layer.use_bias
self.bias_weights = layer.get_weights()[1] self.bias_weights = layer.get_weights()[1]
print("\t", self.bias_weights.shape) print("\t", self.bias_weights.shape)
if layer_type == "Conv2D":
if layer_type == "Conv2D" or layer_type == "DepthwiseConv2D":
self.padding = layer.padding self.padding = layer.padding
self.strides = layer.strides self.strides = layer.strides
print("\t", self.strides) print("\t", self.strides)
print("\tPadding = ", self.padding) print("\tPadding = ", self.padding)
if layer_type == "MaxPooling2D" or layer_type == "AveragePooling2D": if layer_type == "MaxPooling2D" or layer_type == "AveragePooling2D":
self.pool_size = layer.pool_size self.pool_size = layer.pool_size
self.strides = layer.strides self.strides = layer.strides
print("\t pool_size = ", self.pool_size) print("\t pool_size = ", self.pool_size)
print("\t strides = ", self.strides) print("\t strides = ", self.strides)
if layer_type == "Conv2D" or layer_type == "Dense" or layer_type == "Activation":
if nodeHasActivation(self):
self.activation_type = layer.activation.__name__ self.activation_type = layer.activation.__name__
print ("\t Activation = ", self.activation_type) print ("\t Activation = ", self.activation_type)
...@@ -317,7 +322,7 @@ class TensorRtTranslator: ...@@ -317,7 +322,7 @@ class TensorRtTranslator:
def hasBiasAdd(self, cur_node): def hasBiasAdd(self, cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "Dense": if nodeHasBias(cur_node):
return cur_node.use_bias return cur_node.use_bias
return False return False
...@@ -325,8 +330,8 @@ class TensorRtTranslator: ...@@ -325,8 +330,8 @@ class TensorRtTranslator:
def hasActivation(self, cur_node): def hasActivation(self, cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "Dense": if nodeHasActivation(cur_node):
return cur_node.activation_type != "linear" return cur_node.activation_type != "linear"
return False return False
...@@ -355,7 +360,7 @@ class TensorRtTranslator: ...@@ -355,7 +360,7 @@ class TensorRtTranslator:
out_var_name1 = self.getVariableName(cur_node) out_var_name1 = self.getVariableName(cur_node)
layer_type = cur_node.layer_type layer_type = cur_node.layer_type
if layer_type == "Conv2D": if layer_type == "Conv2D" or layer_type == "DepthwiseConv2D":
input_var_name = self.getSingleInputName(cur_node) input_var_name = self.getSingleInputName(cur_node)
weights = cur_node.weights weights = cur_node.weights
strides = cur_node.strides strides = cur_node.strides
...@@ -379,7 +384,14 @@ class TensorRtTranslator: ...@@ -379,7 +384,14 @@ class TensorRtTranslator:
inst_str += str(padding) + ", " inst_str += str(padding) + ", "
inst_str += str(strides[0]) + ", " inst_str += str(strides[0]) + ", "
inst_str += str(strides[1]) + ", " inst_str += str(strides[1]) + ", "
inst_str += "1, 0); \n" inst_str += "1, "
if layer_type == "DepthwiseConv2D":
C = weights.shape[2]
inst_str += str(C) + "); \n"
else:
inst_str += "1); \n"
self.program_str += inst_str self.program_str += inst_str
...@@ -501,7 +513,7 @@ class TensorRtTranslator: ...@@ -501,7 +513,7 @@ class TensorRtTranslator:
layer_type = layer.__class__.__name__ layer_type = layer.__class__.__name__
layer_name = layer.name layer_name = layer.name
if layer_type == "Conv2D": if layer_type == "Conv2D" or layer_type == "DepthwiseConv2D":
weights = layer.get_weights()[0] weights = layer.get_weights()[0]
w_name = layer_name + "_w" w_name = layer_name + "_w"
...@@ -522,6 +534,11 @@ class TensorRtTranslator: ...@@ -522,6 +534,11 @@ class TensorRtTranslator:
file_path_str += unique_file_name + "\"); \n" file_path_str += unique_file_name + "\"); \n"
self.weight_str += file_path_str self.weight_str += file_path_str
# NOTE: Special handling for DepthwiseConv2D
if layer_type == "DepthwiseConv2D":
N = C
C = 1
# FIXME: Be flexible for datatypes (currently only FP32 weights) # FIXME: Be flexible for datatypes (currently only FP32 weights)
# NOTE: '0' specified for floating point type # NOTE: '0' specified for floating point type
self.weight_str += "void* " + w_name + " = " + " readTrainedWeights(" self.weight_str += "void* " + w_name + " = " + " readTrainedWeights("
......
def nodeHasBias(cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "DepthwiseConv2D" or cur_node.layer_type == "Dense":
return True
else:
return False
def nodeHasActivation(cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "DepthwiseConv2D" \
or cur_node.layer_type == "Dense" or cur_node.layer_type == "Activation":
return True
else:
return False
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment