Exemple #1
0
    def call(self, x, training=None):
        proj_query = self.query_conv(x)
        proj_key = self.key_conv(x)
        proj_value = self.value_conv(x)

        if not is_channels_first(self.data_format):
            proj_query = tf.transpose(proj_query, perm=(0, 3, 1, 2))
            proj_key = tf.transpose(proj_key, perm=(0, 3, 1, 2))
            proj_value = tf.transpose(proj_value, perm=(0, 3, 1, 2))

        batch, channels, height, width = proj_query.shape
        proj_query = tf.reshape(proj_query, shape=(batch, -1, height * width))
        proj_key = tf.reshape(proj_key, shape=(batch, -1, height * width))
        proj_value = tf.reshape(proj_value, shape=(batch, -1, height * width))

        energy = tf.keras.backend.batch_dot(
            tf.transpose(proj_query, perm=(0, 2, 1)), proj_key)
        w = self.softmax(energy)

        y = tf.keras.backend.batch_dot(proj_value,
                                       tf.transpose(w, perm=(0, 2, 1)))
        y = tf.reshape(y, shape=(batch, -1, height, width))

        if not is_channels_first(self.data_format):
            y = tf.transpose(y, perm=(0, 2, 3, 1))

        y = self.scale(y, training=training) + x
        return y
Exemple #2
0
    def call(self, x, training=None):
        proj_query = x
        proj_key = x
        proj_value = x

        if not is_channels_first(self.data_format):
            proj_query = tf.transpose(proj_query, perm=(0, 3, 1, 2))
            proj_key = tf.transpose(proj_key, perm=(0, 3, 1, 2))
            proj_value = tf.transpose(proj_value, perm=(0, 3, 1, 2))

        batch, channels, height, width = proj_query.shape
        proj_query = tf.reshape(proj_query, shape=(batch, -1, height * width))
        proj_key = tf.reshape(proj_key, shape=(batch, -1, height * width))
        proj_value = tf.reshape(proj_value, shape=(batch, -1, height * width))

        energy = tf.keras.backend.batch_dot(
            proj_query, tf.transpose(proj_key, perm=(0, 2, 1)))
        energy_new = tf.broadcast_to(tf.math.reduce_max(
            energy, axis=-1, keepdims=True),
                                     shape=energy.shape) - energy
        w = self.softmax(energy_new)

        y = tf.keras.backend.batch_dot(w, proj_value)
        y = tf.reshape(y, shape=(batch, -1, height, width))

        if not is_channels_first(self.data_format):
            y = tf.transpose(y, perm=(0, 2, 3, 1))

        y = self.scale(y, training=training) + x
        return y
Exemple #3
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (512, 512)
    topk = 40
    return_heatmap = False
    pretrained = False

    models = [
        (centernet_resnet18_voc, 20),
        (centernet_resnet18_coco, 80),
        (centernet_resnet50b_voc, 20),
        (centernet_resnet50b_coco, 80),
        (centernet_resnet101b_voc, 20),
        (centernet_resnet101b_coco, 80),
    ]

    for model, classes in models:

        net = model(pretrained=pretrained,
                    topk=topk,
                    in_size=in_size,
                    return_heatmap=return_heatmap,
                    data_format=data_format)

        batch = 14
        x = tf.random.normal((
            batch, 3, in_size[0],
            in_size[1]) if is_channels_first(data_format) else (batch,
                                                                in_size[0],
                                                                in_size[1], 3))
        y = net(x)
        assert (y.shape[0] == batch)
        if return_heatmap:
            if is_channels_first(data_format):
                assert (y.shape[1] == classes +
                        4) and (y.shape[2] == x.shape[2] //
                                4) and (y.shape[3] == x.shape[3] // 4)
            else:
                assert (y.shape[3] == classes +
                        4) and (y.shape[1] == x.shape[1] //
                                4) and (y.shape[2] == x.shape[2] // 4)
        else:
            assert (y.shape[1] == topk) and (y.shape[2] == 6)

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != centernet_resnet18_voc or weight_count == 14215640)
        assert (model != centernet_resnet18_coco or weight_count == 14219540)
        assert (model != centernet_resnet50b_voc or weight_count == 30086104)
        assert (model != centernet_resnet50b_coco or weight_count == 30090004)
        assert (model != centernet_resnet101b_voc or weight_count == 49078232)
        assert (model != centernet_resnet101b_coco or weight_count == 49082132)
Exemple #4
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (480, 480)
    aux = False
    pretrained = False

    models = [
        (pspnet_resnetd50b_voc, 21),
        (pspnet_resnetd101b_voc, 21),
        (pspnet_resnetd50b_coco, 21),
        (pspnet_resnetd101b_coco, 21),
        (pspnet_resnetd50b_ade20k, 150),
        (pspnet_resnetd101b_ade20k, 150),
        (pspnet_resnetd50b_cityscapes, 19),
        (pspnet_resnetd101b_cityscapes, 19),
    ]

    for model, classes in models:

        net = model(pretrained=pretrained, in_size=in_size, aux=aux, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, in_size[0], in_size[1]) if is_channels_first(data_format) else
                             (batch_saze, in_size[0], in_size[1], 3))
        ys = net(x)
        y = ys[0] if aux else ys
        assert (y.shape[0] == x.shape[0])
        if is_channels_first(data_format):
            assert ((y.shape[1] == classes) and (y.shape[2] == x.shape[2]) and (y.shape[3] == x.shape[3]))
        else:
            assert ((y.shape[3] == classes) and (y.shape[1] == x.shape[1]) and (y.shape[2] == x.shape[2]))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        if aux:
            assert (model != pspnet_resnetd50b_voc or weight_count == 49081578)
            assert (model != pspnet_resnetd101b_voc or weight_count == 68073706)
            assert (model != pspnet_resnetd50b_coco or weight_count == 49081578)
            assert (model != pspnet_resnetd101b_coco or weight_count == 68073706)
            assert (model != pspnet_resnetd50b_ade20k or weight_count == 49180908)
            assert (model != pspnet_resnetd101b_ade20k or weight_count == 68173036)
            assert (model != pspnet_resnetd50b_cityscapes or weight_count == 49080038)
            assert (model != pspnet_resnetd101b_cityscapes or weight_count == 68072166)
        else:
            assert (model != pspnet_resnetd50b_voc or weight_count == 46716373)
            assert (model != pspnet_resnetd101b_voc or weight_count == 65708501)
            assert (model != pspnet_resnetd50b_coco or weight_count == 46716373)
            assert (model != pspnet_resnetd101b_coco or weight_count == 65708501)
            assert (model != pspnet_resnetd50b_ade20k or weight_count == 46782550)
            assert (model != pspnet_resnetd101b_ade20k or weight_count == 65774678)
            assert (model != pspnet_resnetd50b_cityscapes or weight_count == 46715347)
            assert (model != pspnet_resnetd101b_cityscapes or weight_count == 65707475)
Exemple #5
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (480, 480)
    aux = False
    pretrained = False

    models = [
        (fcn8sd_resnetd50b_voc, 21),
        (fcn8sd_resnetd101b_voc, 21),
        (fcn8sd_resnetd50b_coco, 21),
        (fcn8sd_resnetd101b_coco, 21),
        (fcn8sd_resnetd50b_ade20k, 150),
        (fcn8sd_resnetd101b_ade20k, 150),
        (fcn8sd_resnetd50b_cityscapes, 19),
        (fcn8sd_resnetd101b_cityscapes, 19),
    ]

    for model, classes in models:

        net = model(pretrained=pretrained, in_size=in_size, aux=aux, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, in_size[0], in_size[1]) if is_channels_first(data_format) else
                             (batch_saze, in_size[0], in_size[1], 3))
        ys = net(x)
        y = ys[0] if aux else ys
        assert (y.shape[0] == x.shape[0])
        if is_channels_first(data_format):
            assert ((y.shape[1] == classes) and (y.shape[2] == x.shape[2]) and (y.shape[3] == x.shape[3]))
        else:
            assert ((y.shape[3] == classes) and (y.shape[1] == x.shape[1]) and (y.shape[2] == x.shape[2]))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        if aux:
            assert (model != fcn8sd_resnetd50b_voc or weight_count == 35445994)
            assert (model != fcn8sd_resnetd101b_voc or weight_count == 54438122)
            assert (model != fcn8sd_resnetd50b_coco or weight_count == 35445994)
            assert (model != fcn8sd_resnetd101b_coco or weight_count == 54438122)
            assert (model != fcn8sd_resnetd50b_ade20k or weight_count == 35545324)
            assert (model != fcn8sd_resnetd101b_ade20k or weight_count == 54537452)
            assert (model != fcn8sd_resnetd50b_cityscapes or weight_count == 35444454)
            assert (model != fcn8sd_resnetd101b_cityscapes or weight_count == 54436582)
        else:
            assert (model != fcn8sd_resnetd50b_voc or weight_count == 33080789)
            assert (model != fcn8sd_resnetd101b_voc or weight_count == 52072917)
            assert (model != fcn8sd_resnetd50b_coco or weight_count == 33080789)
            assert (model != fcn8sd_resnetd101b_coco or weight_count == 52072917)
            assert (model != fcn8sd_resnetd50b_ade20k or weight_count == 33146966)
            assert (model != fcn8sd_resnetd101b_ade20k or weight_count == 52139094)
            assert (model != fcn8sd_resnetd50b_cityscapes or weight_count == 33079763)
            assert (model != fcn8sd_resnetd101b_cityscapes or weight_count == 52071891)
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    # os.environ["TF_CUDNN_DETERMINISTIC"] = "1"
    # os.environ["TF_DETERMINISTIC_OPS"] = "1"

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size_ = (368, 368)
    keypoints = 19
    return_heatmap = True
    pretrained = False

    models = [
        (lwopenpose2d_mobilenet_cmupan_coco, "2d", in_size_),
        (lwopenpose3d_mobilenet_cmupan_coco, "3d", in_size_),
    ]

    for model, model_dim, in_size in models:

        net = model(pretrained=pretrained,
                    in_size=in_size,
                    return_heatmap=return_heatmap,
                    data_format=data_format)

        batch = 14
        x = tf.random.normal((
            batch, 3, in_size[0],
            in_size[1]) if is_channels_first(data_format) else (batch,
                                                                in_size[0],
                                                                in_size[1], 3))
        y = net(x)
        assert (y.shape[0] == batch)
        keypoints_ = 3 * keypoints if model_dim == "2d" else 6 * keypoints
        if is_channels_first(data_format):
            assert ((y.shape[1] == keypoints_)
                    and (y.shape[2] == x.shape[2] // 8)
                    and (y.shape[3] == x.shape[3] // 8))
        else:
            assert ((y.shape[3] == keypoints_)
                    and (y.shape[1] == x.shape[1] // 8)
                    and (y.shape[2] == x.shape[2] // 8))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != lwopenpose2d_mobilenet_cmupan_coco
                or weight_count == 4091698)
        assert (model != lwopenpose3d_mobilenet_cmupan_coco
                or weight_count == 5085983)
Exemple #7
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (256, 192)
    keypoints = 17
    pretrained_backbone = False
    return_heatmap = False
    pretrained = False

    models = [
        simplepose_mobile_resnet18_coco,
        simplepose_mobile_resnet50b_coco,
        simplepose_mobile_mobilenet_w1_coco,
        simplepose_mobile_mobilenetv2b_w1_coco,
        simplepose_mobile_mobilenetv3_small_w1_coco,
        simplepose_mobile_mobilenetv3_large_w1_coco,
    ]

    for model in models:

        net = model(pretrained_backbone=pretrained_backbone, keypoints=keypoints, pretrained=pretrained,
                    in_size=in_size, return_heatmap=return_heatmap, data_format=data_format)

        batch = 14
        x = tf.random.normal((batch, 3, in_size[0], in_size[1]) if is_channels_first(data_format) else
                             (batch, in_size[0], in_size[1], 3))
        y = net(x)
        assert (y.shape[0] == batch)
        if return_heatmap:
            if is_channels_first(data_format):
                assert ((y.shape[1] == keypoints) and (y.shape[2] == x.shape[2] // 4) and
                        (y.shape[3] == x.shape[3] // 4))
            else:
                assert ((y.shape[3] == keypoints) and (y.shape[1] == x.shape[1] // 4) and
                        (y.shape[2] == x.shape[2] // 4))
        else:
            assert ((y.shape[1] == keypoints) and (y.shape[2] == 3))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != simplepose_mobile_resnet18_coco or weight_count == 12858208)
        assert (model != simplepose_mobile_resnet50b_coco or weight_count == 25582944)
        assert (model != simplepose_mobile_mobilenet_w1_coco or weight_count == 5019744)
        assert (model != simplepose_mobile_mobilenetv2b_w1_coco or weight_count == 4102176)
        assert (model != simplepose_mobile_mobilenetv3_small_w1_coco or weight_count == 2625088)
        assert (model != simplepose_mobile_mobilenetv3_large_w1_coco or weight_count == 4768336)
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (256, 192)
    keypoints = 17
    return_heatmap = False
    pretrained = False

    models = [
        simplepose_resnet18_coco,
        simplepose_resnet50b_coco,
        simplepose_resnet101b_coco,
        simplepose_resnet152b_coco,
        simplepose_resneta50b_coco,
        simplepose_resneta101b_coco,
        simplepose_resneta152b_coco,
    ]

    for model in models:

        net = model(pretrained=pretrained, in_size=in_size, return_heatmap=return_heatmap, data_format=data_format)

        batch = 14
        x = tf.random.normal((batch, 3, in_size[0], in_size[1]) if is_channels_first(data_format) else
                             (batch, in_size[0], in_size[1], 3))
        y = net(x)
        assert (y.shape[0] == batch)
        if return_heatmap:
            if is_channels_first(data_format):
                assert ((y.shape[1] == keypoints) and (y.shape[2] == x.shape[2] // 4) and
                        (y.shape[3] == x.shape[3] // 4))
            else:
                assert ((y.shape[3] == keypoints) and (y.shape[1] == x.shape[1] // 4) and
                        (y.shape[2] == x.shape[2] // 4))
        else:
            assert ((y.shape[1] == keypoints) and (y.shape[2] == 3))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != simplepose_resnet18_coco or weight_count == 15376721)
        assert (model != simplepose_resnet50b_coco or weight_count == 33999697)
        assert (model != simplepose_resnet101b_coco or weight_count == 52991825)
        assert (model != simplepose_resnet152b_coco or weight_count == 68635473)
        assert (model != simplepose_resneta50b_coco or weight_count == 34018929)
        assert (model != simplepose_resneta101b_coco or weight_count == 53011057)
        assert (model != simplepose_resneta152b_coco or weight_count == 68654705)
Exemple #9
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    pretrained = False

    models = [
        vovnet27s,
        vovnet39,
        vovnet57,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224, 224) if is_channels_first(data_format) else (batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != vovnet27s or weight_count == 3525736)
        assert (model != vovnet39 or weight_count == 22600296)
        assert (model != vovnet57 or weight_count == 36640296)
Exemple #10
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        mixnet_s,
        mixnet_m,
        mixnet_l,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != mixnet_s or weight_count == 4134606)
        assert (model != mixnet_m or weight_count == 5014382)
        assert (model != mixnet_l or weight_count == 7329252)
Exemple #11
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        hardnet39ds,
        hardnet68ds,
        hardnet68,
        hardnet85,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != hardnet39ds or weight_count == 3488228)
        assert (model != hardnet68ds or weight_count == 4180602)
        assert (model != hardnet68 or weight_count == 17565348)
        assert (model != hardnet85 or weight_count == 36670212)
Exemple #12
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        proxylessnas_cpu,
        proxylessnas_gpu,
        proxylessnas_mobile,
        proxylessnas_mobile14,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != proxylessnas_cpu or weight_count == 4361648)
        assert (model != proxylessnas_gpu or weight_count == 7119848)
        assert (model != proxylessnas_mobile or weight_count == 4080512)
        assert (model != proxylessnas_mobile14 or weight_count == 6857568)
Exemple #13
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        bagnet9,
        bagnet17,
        bagnet33,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != bagnet9 or weight_count == 15688744)
        assert (model != bagnet17 or weight_count == 16213032)
        assert (model != bagnet33 or weight_count == 18310184)
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    # data_format = "channels_first"
    data_format = "channels_last"
    pretrained = False

    models = [
        nvpattexp116bazel76,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch = 14
        seq_len = 8
        audio_window_size = 16
        audio_features = 29
        blendshapes = 76

        x = tf.random.normal((
            batch, seq_len, audio_window_size,
            audio_features) if is_channels_first(data_format) else (
                batch, audio_window_size, audio_features, seq_len))
        pid = tf.fill(dims=(batch, ), value=3)
        y1, y2 = net(x, pid)
        assert (y1.shape == y2.shape == (batch, blendshapes))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != nvpattexp116bazel76 or weight_count == 327397)
Exemple #15
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (640, 640)
    pretrained = False

    models = [
        (lffd20x5s320v2_widerface, 5),
        (lffd25x8s560v1_widerface, 8),
    ]

    for model, num_outs in models:

        net = model(pretrained=pretrained)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, in_size[0], in_size[1]) if is_channels_first(data_format) else
                             (batch_saze, in_size[0], in_size[1], 3))
        y = net(x)
        assert (len(y) == num_outs)

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != lffd20x5s320v2_widerface or weight_count == 1520606)
        assert (model != lffd25x8s560v1_widerface or weight_count == 2290608)
Exemple #16
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        ibn_densenet121,
        ibn_densenet161,
        ibn_densenet169,
        ibn_densenet201,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != ibn_densenet121 or weight_count == 7978856)
        assert (model != ibn_densenet161 or weight_count == 28681000)
        assert (model != ibn_densenet169 or weight_count == 14149480)
        assert (model != ibn_densenet201 or weight_count == 20013928)
Exemple #17
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    pretrained = False

    models = [
        espnetv2_wd2,
        espnetv2_w1,
        espnetv2_w5d4,
        espnetv2_w3d2,
        espnetv2_w2,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224, 224) if is_channels_first(data_format) else (batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != espnetv2_wd2 or weight_count == 1241092)
        assert (model != espnetv2_w1 or weight_count == 1669592)
        assert (model != espnetv2_w5d4 or weight_count == 1964832)
        assert (model != espnetv2_w3d2 or weight_count == 2314120)
        assert (model != espnetv2_w2 or weight_count == 3497144)
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        bam_resnet18,
        bam_resnet34,
        bam_resnet50,
        bam_resnet101,
        bam_resnet152,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != bam_resnet18 or weight_count == 11712503)
        assert (model != bam_resnet34 or weight_count == 21820663)
        assert (model != bam_resnet50 or weight_count == 25915099)
        assert (model != bam_resnet101 or weight_count == 44907227)
        assert (model != bam_resnet152 or weight_count == 60550875)
Exemple #19
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        polynet,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 331,
                              331) if is_channels_first(data_format) else (
                                  batch_saze, 331, 331, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != polynet or weight_count == 95366600)
Exemple #20
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        ibnb_resnet50,
        ibnb_resnet101,
        ibnb_resnet152,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != ibnb_resnet50 or weight_count == 25558568)
        assert (model != ibnb_resnet101 or weight_count == 44550696)
        assert (model != ibnb_resnet152 or weight_count == 60194344)
Exemple #21
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    pretrained = False

    models = [
        selecsls42,
        selecsls42b,
        selecsls60,
        selecsls60b,
        selecsls84,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224, 224) if is_channels_first(data_format) else (batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != selecsls42 or weight_count == 30354952)
        assert (model != selecsls42b or weight_count == 32458248)
        assert (model != selecsls60 or weight_count == 30670768)
        assert (model != selecsls60b or weight_count == 32774064)
        assert (model != selecsls84 or weight_count == 50954600)
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        efficientnet_edge_small_b,
        efficientnet_edge_medium_b,
        efficientnet_edge_large_b,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != efficientnet_edge_small_b or weight_count == 5438392)
        assert (model != efficientnet_edge_medium_b or weight_count == 6899496)
        assert (model != efficientnet_edge_large_b or weight_count == 10589712)
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        cbam_resnet18,
        cbam_resnet34,
        cbam_resnet50,
        cbam_resnet101,
        cbam_resnet152,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != cbam_resnet18 or weight_count == 11779392)
        assert (model != cbam_resnet34 or weight_count == 21960468)
        assert (model != cbam_resnet50 or weight_count == 28089624)
        assert (model != cbam_resnet101 or weight_count == 49330172)
        assert (model != cbam_resnet152 or weight_count == 66826848)
Exemple #24
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    pretrained = False

    models = [
        dpn68,
        dpn68b,
        dpn98,
        dpn107,
        dpn131,
    ]

    for model in models:

        net = model(pretrained=pretrained, data_format=data_format)

        batch_saze = 14
        x = tf.random.normal((batch_saze, 3, 224,
                              224) if is_channels_first(data_format) else (
                                  batch_saze, 224, 224, 3))
        y = net(x)
        assert (tuple(y.shape.as_list()) == (batch_saze, 1000))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != dpn68 or weight_count == 12611602)
        assert (model != dpn68b or weight_count == 12611602)
        assert (model != dpn98 or weight_count == 61570728)
        assert (model != dpn107 or weight_count == 86917800)
        assert (model != dpn131 or weight_count == 79254504)
Exemple #25
0
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (480, 480)
    aux = False
    pretrained = False

    models = [
        danet_resnetd50b_cityscapes,
        danet_resnetd101b_cityscapes,
    ]

    for model in models:

        net = model(pretrained=pretrained,
                    in_size=in_size,
                    aux=aux,
                    data_format=data_format)

        batch = 14
        classes = 19
        x = tf.random.normal((
            batch, 3, in_size[0],
            in_size[1]) if is_channels_first(data_format) else (batch,
                                                                in_size[0],
                                                                in_size[1], 3))
        ys = net(x)
        y = ys[0] if aux else ys
        assert (y.shape[0] == x.shape[0])
        if is_channels_first(data_format):
            assert ((y.shape[1] == classes) and (y.shape[2] == x.shape[2])
                    and (y.shape[3] == x.shape[3]))
        else:
            assert ((y.shape[3] == classes) and (y.shape[1] == x.shape[1])
                    and (y.shape[2] == x.shape[2]))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != danet_resnetd50b_cityscapes
                or weight_count == 47586427)
        assert (model != danet_resnetd101b_cityscapes
                or weight_count == 66578555)
Exemple #26
0
 def call(self, x, training=None):
     if self.extra_padding:
         x = self.pad(x)
     x = self.pool(x)
     if self.extra_padding:
         if is_channels_first(self.data_format):
             x = x[:, :, 1:, 1:]
         else:
             x = x[:, 1:, 1:, :]
     return x
def _test():
    import numpy as np
    import tensorflow.keras.backend as K

    data_format = "channels_last"
    # data_format = "channels_first"
    in_size = (1024, 2048)
    aux = False
    fixed_size = False
    pretrained = False

    models = [
        sinet_cityscapes,
    ]

    for model in models:

        net = model(pretrained=pretrained,
                    in_size=in_size,
                    aux=aux,
                    fixed_size=fixed_size)

        batch = 14
        x = tf.random.normal((
            batch, 3, in_size[0],
            in_size[1]) if is_channels_first(data_format) else (batch,
                                                                in_size[0],
                                                                in_size[1], 3))
        ys = net(x)
        y = ys[0] if aux else ys
        assert (y.shape[0] == x.shape[0])
        if is_channels_first(data_format):
            assert ((y.shape[1] == 19) and (y.shape[2] == x.shape[2])
                    and (y.shape[3] == x.shape[3]))
        else:
            assert ((y.shape[3] == 19) and (y.shape[1] == x.shape[1])
                    and (y.shape[2] == x.shape[2]))

        weight_count = sum(
            [np.prod(K.get_value(w).shape) for w in net.trainable_weights])
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != sinet_cityscapes or weight_count == 119418)
 def call(self, x, training=None):
     w = self.pool(x)
     w = self.fc1(w)
     if self.use_conv2:
         w = self.activ(w)
         w = self.fc2(w)
     w = self.sigmoid(w)
     axis = -1 if is_channels_first(self.data_format) else 1
     w = tf.expand_dims(tf.expand_dims(w, axis=axis), axis=axis)
     x = x * w
     return x
 def call(self, x, training=None):
     if is_channels_first(self.data_format):
         features = x[:, :self.features_channels]
     else:
         features = x[:, :, :, :self.features_channels]
     y = self.body(x, training=training)
     heatmap = self.heatmap_bend(y, training=training)
     paf = self.paf_bend(y, training=training)
     y = tf.concat((features, heatmap, paf),
                   axis=get_channel_axis(self.data_format))
     return y
 def call(self, x, training=None):
     if is_channels_first(self.data_format):
         heatmap_paf_2d = x[:, -self.num_heatmap_paf:]
     else:
         heatmap_paf_2d = x[:, :, :, -self.num_heatmap_paf:]
     if not self.calc_3d_features:
         return heatmap_paf_2d
     x = self.body(x, training=training)
     x = self.features_bend(x, training=training)
     y = tf.concat((heatmap_paf_2d, x),
                   axis=get_channel_axis(self.data_format))
     return y