File size: 1,617 Bytes
368e1c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import tensorflow as tf


class AdaptiveAvgPool1D(tf.keras.layers.Layer):
    def __init__(self, output_size, **kwargs):
        super().__init__(**kwargs)
        self.output_size = output_size

    def call(self, inputs):
        # inputs: (batch, time, channels)
        x = tf.transpose(
            inputs,
            [0, 2, 1]
        )

        # Shape: (batch, channels, time, 1)
        x = tf.expand_dims(
            x,
            axis=-1
        )

        x = tf.image.resize(
            x,
            size=[
                tf.shape(x)[1],
                self.output_size
            ],
            method="bilinear"
        )

        # Shape: (batch, channels, output_size)
        x = tf.squeeze(
            x,
            axis=-1
        )

        # Shape: (batch, output_size, channels)
        x = tf.transpose(
            x,
            [0, 2, 1]
        )

        return x

    def get_config(self):
        config = super().get_config()

        config.update({
            "output_size": self.output_size
        })

        return config


class AdaptiveAvgPool2D(tf.keras.layers.Layer):
    def __init__(self, output_size, **kwargs):
        super().__init__(**kwargs)
        self.output_size = output_size

    def call(self, inputs):
        # inputs: (batch, height, width, channels)
        return tf.image.resize(
            inputs,
            size=self.output_size,
            method="bilinear"
        )

    def get_config(self):
        config = super().get_config()

        config.update({
            "output_size": self.output_size
        })

        return config