Repository: larq/larq

Issue:
model summery trainable params
If you set a keras.layers.Conv2D(...trainable=False), the lq.models.summery(model) report a wrong non-trainable number(and claim the non-trainable params trainable.)
Using model.summery can get the right answer.

I should be right, but please check it to confirm it again by yourself.



Discussion:
That's interesting, it looks like TensorFlow marks the weights as trainable even if they are listed as non trainable variables:
```python-console
In [19]: model = keras.Sequential([keras.layers.Conv2D(16, 3, input_shape=(32, 32, 3), trainable=False)])                                                   

In [20]: model.layers[0].non_trainable_weights[0].trainable                                                                                                 
Out[20]: True
```
See also here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/layers/core.py#L1014-L1030

I'll take a look to see if there is a quick fix.

Resolving patch:
diff --git a/larq/models.py b/larq/models.py
--- a/larq/models.py
+++ b/larq/models.py
@@ -68,9 +68,10 @@ def _get_output_shape(layer):
 
 
 class WeightProfile:
-    def __init__(self, weight, bitwidth=32):
+    def __init__(self, weight, bitwidth=32, trainable=True):
         self._weight = weight
         self.bitwidth = bitwidth
+        self.trainable = trainable
 
     @property
     def count(self):
@@ -84,10 +85,6 @@ def memory(self):
     def fp_equivalent_memory(self):
         return 32 * self.count
 
-    @property
-    def trainable(self):
-        return self._weight.trainable
-
     def is_bias(self):
         return "bias" in self._weight.name
 
@@ -103,7 +100,11 @@ class LayerProfile:
     def __init__(self, layer):
         self._layer = layer
         self.weight_profiles = [
-            WeightProfile(weight, self._get_bitwidth(weight))
+            WeightProfile(
+                weight,
+                self._get_bitwidth(weight),
+                trainable=weight in layer.trainable_weights,
+            )
             for weight in layer.weights
         ]
 


Test patch:
diff --git a/larq/models_test.py b/larq/models_test.py
--- a/larq/models_test.py
+++ b/larq/models_test.py
@@ -33,7 +33,7 @@ def get_profile_model():
                 padding="same",
             ),
             tf.keras.layers.Flatten(),
-            tf.keras.layers.Dense(10),
+            tf.keras.layers.Dense(10, trainable=False),
         ]
     )
 
diff --git a/larq/snapshots/snap_models_test.py b/larq/snapshots/snap_models_test.py
--- a/larq/snapshots/snap_models_test.py
+++ b/larq/snapshots/snap_models_test.py
@@ -7,10 +7,7 @@
 
 snapshots = Snapshot()
 
-snapshots[
-    "test_summary 1"
-] = """\
-+sequential stats----------------------------------------------------------------------------------------------------------------+
+snapshots['test_summary 1'] = '''+sequential stats----------------------------------------------------------------------------------------------------------------+
 | Layer                   Input prec.           Outputs  # 1-bit  # 2-bit  # 32-bit  Memory  1-bit MACs  2-bit MACs  32-bit MACs |
 |                               (bit)                        x 1      x 1       x 1    (kB)        (kB)        (kB)         (kB) |
 +--------------------------------------------------------------------------------------------------------------------------------+
@@ -25,8 +22,8 @@
 +--------------------------------------------------------------------------------------------------------------------------------+
 +sequential summary--------------------------+
 | Total params                       40682   |
-| Trainable params                   40682   |
-| Non-trainable params               0       |
+| Trainable params                   1952    |
+| Non-trainable params               38730   |
 | Model size:                        0.15 MB |
 | Float-32 Equivalent                0.16 MB |
 | Compression Ratio of Memory        0.96    |
@@ -34,4 +31,4 @@
 | Ratio of MACs that are binarized   0.1124  |
 | Ratio of MACs that are ternarized  0.0247  |
 +--------------------------------------------+
-"""
+'''