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

QSeparableConv1D and 2D #50

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ http://arxiv.org/abs/2006.10159

- QDepthwiseConv2D

- QSeparableConv2D (depthwise + pointwise expanded, extended from
MobileNet SeparableConv2D implementation)
- QSeparableConv1D (depthwise + pointwise convolution, without
quantizing the activation values after the depthwise step)

- QSeparableConv2D (depthwise + pointwise convolution, without
quantizing the activation values after the depthwise step)

- QMobileNetSeparableConv2D (extended from MobileNet SeparableConv2D
implementation, quantizes the activation values after the depthwise step)

- QConv2DTranspose

- QActivation

Expand Down
31 changes: 27 additions & 4 deletions qkeras/autoqkeras/autoqkeras_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self):

REGISTERED_LAYERS = ["Dense", "Conv1D", "Conv2D", "DepthwiseConv2D",
"SimpleRNN", "LSTM", "GRU", "Bidirectional",
"Conv2DTranspose"]
"Conv2DTranspose", "SeparableConv1D", "SeparableConv2D"]

Q_LAYERS = list(map(lambda x : 'Q' + x, REGISTERED_LAYERS))

Expand Down Expand Up @@ -235,6 +235,13 @@ def _get_quantizer(self, hp, head, layer_name, layer_class_name,
index = 1
q_list = list(bq.keys())
q_dict = bq
elif "pointwise_kernel" in head: # limit is same as kernel
# pointwise kernel quantizers
field_name = "pointwise_kernel"
kq = self.quantization_config["pointwise_kernel"]
index = 2
q_list = list(kq.keys())
q_dict = kq
elif "recurrent_kernel" in head: # limit is same as kernel
# recurrent kernel quantizers
field_name = "recurrent_kernel"
Expand Down Expand Up @@ -372,6 +379,11 @@ def quantize_model(self, hp):
hp, layer.name + "_recurrent_kernel", layer.name, layer.__class__.__name__,
is_kernel=True)

if layer.__class__.__name__ in ["SeparableConv1D", "SeparableConv2D"]:
pointwise_quantizer, _ = self._get_quantizer(
hp, layer.name + "_pointwise_kernel", layer.name, layer.__class__.__name__,
is_kernel=True)

if self.tune_filters == "block" and filter_sweep_enabled:
network_filters = hp.Choice(
"network_filters",
Expand Down Expand Up @@ -405,7 +417,9 @@ def quantize_model(self, hp):
if layer.__class__.__name__ in REGISTERED_LAYERS:
# difference between depthwise and the rest is just the name
# of the kernel.
if layer.__class__.__name__ == "DepthwiseConv2D":
if layer.__class__.__name__ in [
"DepthwiseConv2D", "SeparableConv1D", "SeparableConv2D"
]:
kernel_name = "depthwise_quantizer"
else:
kernel_name = "kernel_quantizer"
Expand All @@ -426,7 +440,10 @@ def quantize_model(self, hp):
if (
self.tune_filters in ["layer", "block"] and
not self.tune_filters_exceptions.search(layer.name) and
layer.__class__.__name__ in ["Dense", "Conv1D", "Conv2D", "Conv2DTranspose"]
layer.__class__.__name__ in [
"Dense", "Conv1D", "Conv2D", "Conv2DTranspose",
"SeparableConv1D", "SeparableConv2D"
]
):
if self.tune_filters == "layer":
layer_filters = hp.Choice(
Expand All @@ -439,13 +456,19 @@ def quantize_model(self, hp):

if layer.__class__.__name__ == "Dense":
layer.units = max(int(layer.units * layer_filters), 1)
elif layer.__class__.__name__ in ["Conv1D", "Conv2D", "Conv2DTranspose"]:
elif layer.__class__.__name__ in [
"Conv1D", "Conv2D", "Conv2DTranspose",
"SeparableConv1D", "SeparableConv2D"
]:
layer.filters = max(int(layer.filters * layer_filters), 1)

layer_d[kernel_name] = kernel_quantizer

if layer.__class__.__name__ in SEQUENCE_LAYERS:
layer_d['recurrent_quantizer'] = recurrent_quantizer

if layer.__class__.__name__ in ["SeparableConv1D", "SeparableConv2D"]:
layer_d['pointwise_quantizer'] = pointwise_quantizer

if layer.__class__.__name__ in ["LSTM", "GRU", "Bidirectional"]:
layer_d['recurrent_activation'], _ = self._get_quantizer(
Expand Down
76 changes: 70 additions & 6 deletions qkeras/estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ def extract_model_operations(in_model):
cache_o[layer.output.experimental_ref()] = output_shape

if layer.__class__.__name__ not in ["QDense", "QConv2D", "QConv1D",
"QDepthwiseConv2D"]:
"QDepthwiseConv2D", "QSeparableConv1D",
"QSeparableConv2D"]:
continue

if layer.__class__.__name__ in ["QConv2D"]:
Expand Down Expand Up @@ -466,6 +467,62 @@ def extract_model_operations(in_model):
weight_type = get_quant_mode(weight_quant)
bias_type = get_quant_mode(bias_quant)

elif layer.__class__.__name__ in ["QSeparableConv1D"]:

_, _, channels_i = input_shape

_, time_o, channels_o = output_shape

weight_1 = layer.get_weights()[0]

kernel_length, _, _ = weight_1.shape

number_of_operations = (
kernel_length * time_o * channels_i +
time_o * channels_o)

number_of_weights = [
kernel_length * channels_i,
channels_o * channels_i]

number_of_bias = 0
if len(layer.get_weights()) > 2:
number_of_bias = layer.get_weights()[2].shape[0]

depthwise_quant, pointwise_quant, bias_quant = layer.get_quantizers()
depthwise_type = get_quant_mode(depthwise_quant)
pointwise_type = get_quant_mode(pointwise_quant)
weight_type = [depthwise_type, pointwise_type]
bias_type = get_quant_mode(bias_quant)

elif layer.__class__.__name__ in ["QSeparableConv2D"]:

_, _, _, channels_i = input_shape

_, height_o, width_o, channels_o = output_shape

weight_1 = layer.get_weights()[0]

kernel_h, kernel_w, _, _ = weight_1.shape

number_of_operations = (
kernel_h * kernel_w * height_o * width_o * channels_i +
height_o * width_o * channels_o)

number_of_weights = [
kernel_h * kernel_w * channels_i,
channels_o * channels_i]

number_of_bias = 0
if len(layer.get_weights()) > 2:
number_of_bias = layer.get_weights()[2].shape[0]

depthwise_quant, pointwise_quant, bias_quant = layer.get_quantizers()
depthwise_type = get_quant_mode(depthwise_quant)
pointwise_type = get_quant_mode(pointwise_quant)
weight_type = [depthwise_type, pointwise_type]
bias_type = get_quant_mode(bias_quant)

elif layer.__class__.__name__ in ["QDense"]:

_, size_i = input_shape
Expand Down Expand Up @@ -530,12 +587,19 @@ def print_qstats(model):
print("")
print("Weight profiling:")
for name in sorted(model_ops):
w_mode, w_sizes, w_signs = model_ops[name]["type_of_weights"]
b_mode, b_sizes, b_signs = model_ops[name]["type_of_bias"]
w_number = model_ops[name]["number_of_weights"]
weight_type = model_ops[name]["type_of_weights"]
n_weights = model_ops[name]["number_of_weights"]
if isinstance(weight_type, list):
for i, (w_type, w_number) in enumerate(zip(weight_type, n_weights)):
_, w_sizes, _ = w_type
print(" {:30} : {:5} ({}-bit unit)".format(
str(name) + "_weights_" + str(i), str(w_number), str(w_sizes)))
else:
_, w_sizes, _ = weight_type
print(" {:30} : {:5} ({}-bit unit)".format(
str(name) + "_weights", str(n_weights), str(w_sizes)))
_, b_sizes, _ = model_ops[name]["type_of_bias"]
b_number = model_ops[name]["number_of_bias"]
print(" {:30} : {:5} ({}-bit unit)".format(
str(name) + "_weights", str(w_number), str(w_sizes)))
print(" {:30} : {:5} ({}-bit unit)".format(
str(name) + "_bias", str(b_number), str(b_sizes)))

Expand Down