Spaces:
Running
Running
File size: 4,440 Bytes
9d61c9b |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import unittest
import torch
from models.tts.delightful_tts.conv_blocks.conv1d import (
DepthWiseConv1d,
PointwiseConv1d,
)
class TestDepthwiseConv1d(unittest.TestCase):
def setUp(self):
# initialize parameters once and reuse them in multiple test cases
self.in_channels, self.out_channels, self.kernel_size, self.padding = 2, 4, 3, 1
self.depthwise_conv = DepthWiseConv1d(
self.in_channels,
self.out_channels,
self.kernel_size,
self.padding,
)
# Generate an input data
self.x_rand = torch.randn(
32,
self.in_channels,
64,
)
self.x_zero = torch.zeros(
32,
self.in_channels,
64,
)
self.x_ones = torch.ones(
32,
self.in_channels,
64,
)
def test_forward(self):
out = self.depthwise_conv(self.x_rand)
self.assertIsInstance(out, torch.Tensor)
self.assertEqual(out.shape, (32, self.out_channels, 64))
def test_non_random_input(self):
out = self.depthwise_conv(self.x_ones)
self.assertIsInstance(out, torch.Tensor)
self.assertEqual(out.shape, (32, self.out_channels, 64))
self.assertIsInstance(out, torch.Tensor)
self.assertEqual(out.shape, (32, self.out_channels, 64))
def test_zero_input(self):
out = self.depthwise_conv(self.x_zero)
self.assertIsInstance(out, torch.Tensor)
self.assertEqual(out.shape, (32, self.out_channels, 64))
def test_weight_change(self):
self.depthwise_conv.conv.weight.data.fill_(0.5)
out_first = self.depthwise_conv(self.x_rand)
self.depthwise_conv.conv.weight.data.fill_(1.0)
out_second = self.depthwise_conv(self.x_rand)
# Ensuring weight changes have an effect
self.assertTrue(torch.any(out_first != out_second))
class TestPointwiseConv1d(unittest.TestCase):
def setUp(self):
# initialize parameters once and reuse them in multiple test cases
self.in_channels, self.out_channels, self.stride, self.padding, self.bias = (
2,
4,
1,
1,
True,
)
self.pointwise_conv = PointwiseConv1d(
self.in_channels, self.out_channels, self.stride, self.padding, self.bias,
)
# Generate an input data
self.x_rand = torch.randn(
32,
self.in_channels,
64,
)
self.x_zero = torch.zeros(
32,
self.in_channels,
64,
)
self.x_ones = torch.ones(
32,
self.in_channels,
64,
)
def test_forward(self):
out = self.pointwise_conv(self.x_rand)
self.assertIsInstance(out, torch.Tensor)
# Padding of 1 means one column of zeroes got added both at the beginning and at the end,
# that's why you have 64 (original) + 2 (padding) = 66
self.assertEqual(out.shape, (32, self.out_channels, 66))
def test_non_random_input(self):
out = self.pointwise_conv(self.x_ones)
self.assertIsInstance(out, torch.Tensor)
self.assertEqual(out.shape, (32, self.out_channels, 66))
def test_zero_input(self):
out = self.pointwise_conv(self.x_zero)
self.assertIsInstance(out, torch.Tensor)
self.assertEqual(out.shape, (32, self.out_channels, 66))
def test_weight_change(self):
self.pointwise_conv.conv.weight.data.fill_(0.5)
out_first = self.pointwise_conv(self.x_rand)
self.pointwise_conv.conv.weight.data.fill_(1.0)
out_second = self.pointwise_conv(self.x_rand)
self.assertTrue(torch.any(out_first != out_second))
def test_kernel_size(self):
# Checking if the module can handle non-default kernel sizes.
kernel_size = 2
pointwise_conv = PointwiseConv1d(
self.in_channels,
self.out_channels,
self.stride,
self.padding,
self.bias,
kernel_size,
)
out = pointwise_conv(self.x_rand)
# ((input_size - kernel_size + 2*padding)/stride ) + 1
# ((64 - 2 + 2*1) / 1 ) + 1 = 65
self.assertEqual(out.shape, (32, self.out_channels, 65))
if __name__ == "__main__":
unittest.main()
|