Exemplo n.º 1
0
def _test_downsample_unit():
    x = tf.random.uniform((32, 56, 56, 24))
    m1 = ShuffleNetV2DownsampleUnit(24, 116, se_reduction=None)
    o1 = m1(x)
    m2 = ShuffleNetV2DownsampleUnit(24, 116, se_reduction=16)
    o2 = m2(x)
    assert o1.shape == o2.shape == (32, 28, 28, 116)
    assert get_num_params(m1) == 8554
    assert get_num_params(m2) == 9571
def _test_preact_conv():
    x = tf.random.uniform((32, 56, 56, 64))
    m = preact_conv1x1(64, 128)
    m2 = preact_conv3x3(128, 32)
    o1 = m(x)
    assert o1.shape == (32, 56, 56, 128)
    assert get_num_params(m) == 64 * 2 + 128 * 64 * (1 * 1 + 0)
    o2 = m2(o1)
    assert o2.shape == (32, 56, 56, 32)
    assert get_num_params(m2) == 128 * 2 + 128 * 32 * (3 * 3 + 0)
Exemplo n.º 3
0
def _test_mnasnet():
    x = tf.random.uniform((32, 224, 224, 3))
    m = get_mnasnet_a1_s1()
    o = m(x)
    assert o.shape == (32, 1000)
    assert get_num_params(m) == 3665608
    m2 = get_mnasnet_b1_s1()
    o2 = m2(x)
    assert o2.shape == (32, 1000)
    assert get_num_params(m2) == 4383312
def _test_fire():
    x = tf.random.uniform((32, 224, 224, 64))
    m = Fire(64, 8, 32, 32, False)
    o = m(x)
    assert o.shape == (32, 224, 224, 64) and get_num_params(
        m) == (64 + 1) * 8 + (8 + 1) * 32 + (8 * 9 + 1) * 32

    m2 = Conv2d(64, 64, 3, 1, 1)
    o = m2(x)
    assert o.shape == (32, 224, 224,
                       64) and get_num_params(m2) == (64 * 9 + 1) * 64
Exemplo n.º 5
0
def _test_sepconv_block():
    x = tf.random.uniform((32, 112, 112, 32))
    m = SepConv(32, 16)
    o = m(x)
    assert o.shape == (32, 112, 112, 16)
    assert get_num_params(
        m) == 32 * 1 * (3 * 3 + 0) + 32 * 2 + 32 * 16 * (1 * 1 + 0) + 16 * 2
def _test_denseblock():
    x = tf.random.uniform((32, 56, 56, 64))
    bn_size, growth_rate = 4, 32
    m = DenseBlock(in_channels=64, num_layers=6, bn_size=bn_size, growth_rate=growth_rate)
    o = m(x)
    assert o.shape == (32, 56, 56, 64 + 6 * 32)
    assert get_num_params(m) == 335040
Exemplo n.º 7
0
def _test_basic_unit():
    x = tf.random.uniform((32, 28, 28, 116))
    m1 = ShuffleNetV2BasicUnit(116)
    o1 = m1(x)
    m2 = ShuffleNetV2BasicUnit(116, se_reduction=16)
    o2 = m2(x)
    m3 = ShuffleNetV2BasicUnit(116, residual=True)
    o3 = m3(x)
    m4 = ShuffleNetV2BasicUnit(116, se_reduction=16, residual=True)
    o4 = m4(x)
    for i, (o, m) in enumerate(zip([o1, o2, o3, o4], [m1, m2, m3, m4])):
        assert o.shape == (32, 28, 28, 116)
        if i % 2 == 0:
            assert get_num_params(m) == 7598
        else:
            assert get_num_params(m) == 8007
Exemplo n.º 8
0
def _test_googlenet():
    x = tf.random.uniform((32, 224, 224, 3))
    m = GoogleNet()
    o, a1, a2 = m(x, training=True)
    assert o.shape == a1.shape == a2.shape == (32, 1000)
    o = m(x)
    assert o.shape == (32, 1000)
    assert get_num_params(m) == 11851864
Exemplo n.º 9
0
def _test_mbconvblock():
    x = tf.random.uniform((32, 112, 112, 16))
    m = MBConv(in_channels=16,
               out_channels=24,
               kernel_size=3,
               strides=2,
               expansion_factor=6)
    x = m(x)
    assert x.shape == (32, 56, 56, 24)
    conv1_expand_params = 16 * 16 * 6 * (1 * 1 + 0) + 16 * 6 * 2
    conv3_params = 1 * 16 * 6 * (3 * 3 + 0) + 16 * 6 * 2
    conv1_projec_params = 16 * 6 * 24 * (1 * 1 + 0) + 24 * 2
    assert get_num_params(
        m) == conv1_expand_params + conv3_params + conv1_projec_params
    m2 = MBConv(in_channels=24,
                out_channels=24,
                kernel_size=3,
                strides=1,
                expansion_factor=6)
    x = m2(x)
    assert x.shape == (32, 56, 56, 24)
    assert m2.residual
    conv1_expand_params = 24 * 24 * 6 * (1 * 1 + 0) + 24 * 6 * 2
    conv3_params = 1 * 24 * 6 * (3 * 3 + 0) + 24 * 6 * 2
    conv1_projec_params = 24 * 6 * 24 * (1 * 1 + 0) + 24 * 2
    assert get_num_params(
        m2) == conv1_expand_params + conv3_params + conv1_projec_params
    m3 = MBConv(in_channels=24,
                out_channels=40,
                kernel_size=5,
                strides=2,
                expansion_factor=3,
                se_ratio=.25)
    x = m3(x)
    assert x.shape == (32, 28, 28, 40)
    assert get_num_params(m3) == 7428
    m4 = MBConv(in_channels=40,
                out_channels=40,
                kernel_size=5,
                strides=1,
                expansion_factor=3,
                se_ratio=.25)
    x = m4(x)
    assert x.shape == (32, 28, 28, 40)
    assert m4.residual
Exemplo n.º 10
0
def _test_vgg():
    x = tf.random.uniform((32, 224, 224, 3))
    model = vgg11()
    out = model(x, training=True)
    assert out.shape == (32, 1000)
    assert get_num_params(model) == 132866088
    model = vgg13()
    out = model(x, training=True)
    assert out.shape == (32, 1000)
    assert get_num_params(model) == 133050792
    model = vgg16()
    out = model(x, training=True)
    assert out.shape == (32, 1000)
    assert get_num_params(model) == 138361768
    model = vgg19()
    out = model(x, training=True)
    assert out.shape == (32, 1000)
    assert get_num_params(model) == 143672744
def _test_densenet():
    x = tf.random.uniform((32, 224, 224, 3))
    models = [get_densenet121, get_densenet161, get_densenet169, get_densenet201]
    num_params = [7978856, 28681000, 14149480, 20013928]
    for model, num_param in zip(models, num_params):
        m = model()
        o = m(x)
        assert o.shape == (32, 1000)
        assert get_num_params(m) == num_param
def _test_squeezenet():
    x = tf.random.uniform((32, 224, 224, 3))

    model = squeezenet_v1_0()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 1248424

    model = squeezenet_v1_1()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 1235496

    model = squeezeresnet_v1_0()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 1248424

    model = squeezeresnet_v1_1()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 1235496
def _test_denseunit():
    x = tf.random.uniform((32, 56, 56, 64))
    bn_size, growth_rate = 4, 32
    m = DenseUnit(64, bn_size=bn_size, growth_rate=growth_rate, dropout_rate=.1)
    o = m(x)
    assert o.shape == ((32, 56, 56, growth_rate))
    conv1_params = 64 * 2 + 64 * (bn_size * growth_rate) * (1 * 1 + 0)
    conv2_params = (bn_size * growth_rate) * 2 + (bn_size * growth_rate) * growth_rate * (3 * 3 + 0)
    num_params = conv1_params + conv2_params
    assert get_num_params(m) == num_params
Exemplo n.º 14
0
def _test_alexnet(show_summary=False):
    alexnet = construct_alex_net()
    # alexnet.build(input_shape=(None, 224, 224, 3))
    # model.compile(optimizer=tf.keras.optimizers.Adam())
    x = tf.random.uniform((32, 224, 224, 3))
    out = alexnet(x, training=True)
    assert out.shape == (32, 1000)
    assert get_num_params(alexnet) == 61100840
    if show_summary:
        print(alexnet.summary())
Exemplo n.º 15
0
def _test_shufflenetv2():
    x = tf.random.uniform((32, 224, 224, 3))
    m1 = ShuffleNetV2()
    o1 = m1(x)
    m2 = ShuffleNetV2(se_reduction=16)
    o2 = m2(x)
    m3 = get_shufflenetv2(residual=True)
    o3 = m3(x)
    m4 = get_shufflenetv2(se_reduction=16, residual=True)
    o4 = m4(x)
    m5 = ShuffleNetV2(out_channels=2048, c=[244, 488, 976])
    o5 = m5(x)
    m6 = get_shufflenetv2(se_reduction=16,
                          residual=True,
                          n_groups=4,
                          width_scale='2.0')
    o6 = m6(x)
    assert o1.shape == o2.shape == o3.shape == o4.shape == o5.shape == o6.shape == (
        32, 1000)
    assert get_num_params(m1) == get_num_params(m3) == 2279760
    assert get_num_params(m2) == get_num_params(m4) == 2322948
    assert get_num_params(m5) == 7403600
    assert get_num_params(m6) == 7594888
def _test_resnet():
    x = tf.random.uniform((32, 224, 224, 3))
    model = resnet18()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 11689512

    model = resnet34()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 21797672

    model = resnet50()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 25557032

    model = resnet101()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 44549160

    model = resnet152()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 60192808

    model = resnext50_32x4d()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 25028904

    model = resnext101_32x8d()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 44177704

    model = wide_resnet50_2()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 68883240

    model = wide_resnet101_2()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 126886696

    model = seresnext_50()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 27559896

    model = seresnext_101()
    out = model(x)
    assert out.shape == (32, 1000) and get_num_params(model) == 48955416
def _test_trasitblock():
    x = tf.random.uniform((32, 56, 56, 256))
    m = TransitBlock(in_channels=256, out_channels=128)
    o = m(x)
    assert o.shape == (32, 28, 28, 128)
    assert get_num_params(m) == 256 * 2 + 256 * 128 * (1 * 1 + 0)
def _test_mobilenet():
    x = tf.random.uniform((32, 224, 224, 3))
    m = MobileNetV2()
    o = m(x)
    assert o.shape == (32, 1000)
    assert get_num_params(m) == 3504872