def test_residual_block_asymmetric_pad_future_contexts(self): # test future contexts at various values # 0 = no future context # 2 = limited future context # 5 = symmetric context # 8 = excess future context (more future context than present or past context) future_contexts = [0, 2, 5, 8] for future_context in future_contexts: print(future_context) config = self.jasper_base_config(future_context=future_context) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) self.check_module_exists(block, torch.nn.ConstantPad1d) self.check_module_exists(block, jasper.MaskedConv1d) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 131]) assert ylen[0] == 131 assert block.mconv[0].pad_layer is not None assert block.mconv[0]._padding == (config['kernel_size'][0] - 1 - future_context, future_context)
def test_residual_block(self): config = self.jasper_base_config(residual=True) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 131]) assert ylen[0] == 131
def test_basic_block_repeat_stride(self): config = self.jasper_base_config(residual=False, repeat=3, stride=[2]) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 17]) # 131 // (stride ^ repeats) assert ylen[0] == 17 # 131 // (stride ^ repeats) assert len(block.mconv) == 3 * 3 + 1 # (3 repeats x {1 conv + 1 norm + 1 dropout} + final conv)
def test_residual_block_se(self): config = self.jasper_base_config(se=True, se_reduction_ratio=8) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) self.check_module_exists(block, jasper.SqueezeExcite) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 131]) assert ylen[0] == 131
def test_basic_block_stride(self): config = self.jasper_base_config(stride=[2], residual=False) act = jasper.jasper_activations.get(config.pop('activation'))() print(config) block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 66]) assert ylen[0] == 66
def test_residual_block_normalizations(self): NORMALIZATIONS = ["batch", "layer", "group"] for normalization in NORMALIZATIONS: config = self.jasper_base_config(normalization=normalization) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 131]) assert ylen[0] == 131
def test_residual_block_activations(self): for activation in jasper.jasper_activations.keys(): config = self.jasper_base_config(activation=activation) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) self.check_module_exists(block, act.__class__) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 131]) assert ylen[0] == 131
def test_residual_block_asymmetric_pad_future_context_fallback(self): # test future contexts at various values # 15 = K < FC; fall back to symmetric context future_context = 15 print(future_context) config = self.jasper_base_config(future_context=future_context) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) x = torch.randn(1, 16, 131) xlen = torch.tensor([131]) y, ylen = block(([x], xlen)) self.check_module_exists(block, jasper.MaskedConv1d) assert isinstance(block, jasper.JasperBlock) assert y[0].shape == torch.Size([1, config['planes'], 131]) assert ylen[0] == 131 assert block.mconv[0].pad_layer is None assert block.mconv[0]._padding == config['kernel_size'][0] // 2
def contrust_jasper_block(**config_kwargs): config = TestJasperBlock.jasper_base_config(**config_kwargs) act = jasper.jasper_activations.get(config.pop('activation'))() block = jasper.JasperBlock(**config, activation=act) return block