Jump to content

TensorFlow: Difference between revisions

Line 84: Line 84:
===Save and Load Models===
===Save and Load Models===
[https://www.tensorflow.org/tutorials/keras/save_and_load Reference]
[https://www.tensorflow.org/tutorials/keras/save_and_load Reference]
===Custom Layers===
Extend <code>tf.keras.layer.Layer</code>
{{ hidden | ReflectionPadding2D |
[https://stackoverflow.com/questions/50677544/reflection-padding-conv2d SO Source]
<syntaxhighlight lang="python">
class ReflectionPadding2D(Layer):
    def __init__(self, padding=(1, 1), **kwargs):
        self.padding = tuple(padding)
        self.input_spec = [InputSpec(ndim=4)]
        super(ReflectionPadding2D, self).__init__(**kwargs)
    def compute_output_shape(self, s):
        """ If you are using "channels_last" configuration"""
        return (s[0], s[1] + 2 * self.padding[0], s[2] + 2 * self.padding[1], s[3])
    def call(self, x, mask=None):
        w_pad,h_pad = self.padding
        return tf.pad(x, [[0,0], [h_pad,h_pad], [w_pad,w_pad], [0,0] ], 'REFLECT')
</syntaxhighlight>
}}


==Usage (TF1)==
==Usage (TF1)==