-
Notifications
You must be signed in to change notification settings - Fork 53
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
fix tf.nn.{conv2d,convolution} substitution #1275
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,8 @@ def compare(self, quantized_model, float_model, input_x=None, quantization_info= | |
y = float_model.predict(input_x) | ||
y_hat = quantized_model.predict(input_x) | ||
self.unit_test.assertTrue(y.shape == y_hat.shape, msg=f'out shape is not as expected!') | ||
# FIXME this doesn't test anything, the number of quantized convs in the network is exactly 0. Even if it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then why not remove it? |
||
# looked at correct layers it hardly checks anything. | ||
self.unit_test.assertTrue(len([l for l in quantized_model.layers if isinstance(l, KerasTrainableQuantizationWrapper) and isinstance(l.layer, layers.Conv2D)]) < len([l for l in float_model.layers if isinstance(l, layers.Conv2D)]), msg=f'fail number of layers should decrease!') | ||
cs = cosine_similarity(y, y_hat) | ||
self.unit_test.assertTrue(np.isclose(cs, 1), msg=f'fail cosine similarity check:{cs}') | ||
|
@@ -75,6 +77,7 @@ def compare(self, quantized_model, float_model, input_x=None, quantization_info= | |
if type(layer) == layers.Conv2D: | ||
self.unit_test.assertTrue(len(layer.weights) == 2, msg=f'fail Bias should appear in weights!!') | ||
|
||
|
||
class ThreeConv2DCollapsingTest(BaseConv2DCollapsingTest): | ||
def __init__(self, unit_test): | ||
super().__init__(unit_test) | ||
|
@@ -107,9 +110,35 @@ def create_networks(self): | |
|
||
def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): | ||
super().compare(quantized_model, float_model, input_x, quantization_info) | ||
for layer in quantized_model.layers: | ||
if type(layer) == layers.Conv2D: | ||
self.unit_test.assertTrue(len(layer.weights) == 2,msg=f'fail Bias should appear in weights!!') | ||
convs = [l for l in quantized_model.layers if isinstance(l, layers.Conv2D)] | ||
self.unit_test.assertTrue(len(convs) == 1) | ||
for layer in convs: | ||
self.unit_test.assertTrue(len(layer.weights) == 2,msg=f'fail Bias should appear in weights!!') | ||
|
||
|
||
class FuncConvCollapsingTest(FourConv2DCollapsingTest): | ||
def create_networks(self): | ||
# Tests the combination of functional conv to Conv2D substitution with linear collapsing | ||
# (in case of default values, tf layer doesn't contain these attributes, and they must be added explicitly | ||
# to node's attributes dict, which is not covered by substitution test) | ||
h, w, c = self.get_input_shapes()[0][1:] | ||
inputs = layers.Input(shape=(h, w, c)) | ||
x = tf.nn.conv2d(inputs, tf.random.uniform((3, 3, c, 16)), 1, 'SAME') | ||
x = tf.nn.convolution(x, tf.random.uniform((1, 1, 16, 8))) | ||
x = tf.nn.relu(x) | ||
x = tf.nn.convolution(x, tf.random.uniform((3, 3, 8, 32))) | ||
y = tf.nn.conv2d(x, tf.random.uniform((1, 1, 32, 4)), 1, 'VALID') | ||
return tf.keras.models.Model(inputs=inputs, outputs=y) | ||
|
||
def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): | ||
convs = [l for l in quantized_model.layers if isinstance(l, layers.Conv2D)] | ||
self.unit_test.assertTrue(len(convs) == 2) | ||
|
||
y = float_model.predict(input_x) | ||
y_hat = quantized_model.predict(input_x) | ||
self.unit_test.assertTrue(y.shape == y_hat.shape, msg=f'out shape is not as expected!') | ||
cs = cosine_similarity(y, y_hat) | ||
self.unit_test.assertTrue(np.isclose(cs, 1), msg=f'fail cosine similarity check:{cs}') | ||
|
||
|
||
class SixConv2DCollapsingTest(BaseConv2DCollapsingTest): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this way you assume the defaults. why not return None?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's intentional. None wouldn't do, we need to fill in an explicit default. This method is specific to tf stride & dilation