def test_dense_layer(self): in_channels = 512 out_channels = 8 x = Variable(FloatTensor(1, in_channels, 32, 64)) print('x:', x.shape) layer_params = [ {}, { 'dropout': 0.2 }, { 'bottleneck_ratio': 4 }, { 'bottleneck_ratio': 4, 'dropout': 0.2 }, ] for params in layer_params: with self.subTest(**params): dense_layer = DenseLayer(in_channels, out_channels, **params) print(dense_layer) print('Parameters:', count_parameters(dense_layer)) y = dense_layer(x) print('y:', y.shape) self.assertEqual(y.shape[1], out_channels)
def test_dense_block(self): in_channels = 64 x = Variable(FloatTensor(1, in_channels, 32, 64)) print('x:', x.shape) block_params = [ {'growth_rate': 4, 'num_layers': 2, 'concat_input': False}, {'growth_rate': 4, 'num_layers': 2, 'concat_input': True}, {'growth_rate': 4, 'num_layers': 3, 'concat_input': False}, {'growth_rate': 4, 'num_layers': 3, 'concat_input': True}, ] dense_layer_params = {'bottleneck_ratio': 4, 'dropout': 0.2} for params in block_params: with self.subTest(**params): dense_block = DenseBlock(in_channels, **params, dense_layer_params=dense_layer_params) print(dense_block) print('Parameters:', count_parameters(dense_block)) y = dense_block(x) print('y:', y.shape) expected_out_channels = params['growth_rate'] * params['num_layers'] if params['concat_input']: expected_out_channels += in_channels self.assertEqual(y.shape[1], expected_out_channels)
def test_fc_densenet(self): densenet = FCDenseNet(in_channels=self.rgb_channels, out_channels=self.num_classes, initial_num_features=24, dropout=0.2, down_dense_growth_rates=8, down_dense_bottleneck_ratios=None, down_dense_num_layers=(4, 5, 7), down_transition_compression_factors=1.0, middle_dense_growth_rate=8, middle_dense_bottleneck=None, middle_dense_num_layers=10, up_dense_growth_rates=8, up_dense_bottleneck_ratios=None, up_dense_num_layers=(7, 5, 4)) print(densenet) print('Layers:', count_conv2d(densenet)) print('Parameters:', count_parameters(densenet)) logits = densenet(self.images) print('Logits:', logits.shape) self.assertEqual( logits.shape, Size((self.batch_size, self.num_classes, self.H, self.W)))
def test_fc_densenet_103(self): densenet = FCDenseNet103() layers = count_conv2d(densenet) print('Layers:', layers) print('Parameters:', count_parameters(densenet)) self.assertEqual(103, layers)
def test_bottleneck(self): in_channels = 512 out_channels = 8 x = torch.empty(1, in_channels, 32, 64) print('x:', x.shape) bottleneck = Bottleneck(in_channels, out_channels) print(bottleneck) print('Parameters:', count_parameters(bottleneck)) y = bottleneck(x) print('y:', y.shape) self.assertEqual(y.shape[1], out_channels)
def test_bottleneck(self): in_channels = 512 out_channels = 8 x = Variable(FloatTensor(1, in_channels, 32, 64)) print('x:', x.shape) bottleneck = Bottleneck(in_channels, out_channels) print(bottleneck) print('Parameters:', count_parameters(bottleneck)) y = bottleneck(x) print('y:', y.shape) self.assertEqual(y.shape[1], out_channels)
def test_transition_up(self): # Transposed convolution upsamples this to (:, :, 13, 17) upsample = Variable(FloatTensor(1, 3, 6, 8).zero_()) skip = Variable(FloatTensor(1, 7, 15, 14).zero_()) print('upsample:', upsample.shape) print('skip:', skip.shape) transition = TransitionUp(upsample.size(1)) print(transition) print('Parameters:', count_parameters(transition)) res = transition(upsample, skip) print('res:', res.shape) self.assertEqual(res.size(1), upsample.size(1) + skip.size(1)) self.assertEqual(res.size(2), min(13, skip.size(2))) self.assertEqual(res.size(3), min(17, skip.size(3)))
def test_transition(self): in_channels = 512 H = 32 W = 64 x = Variable(FloatTensor(1, in_channels, H, W)) print('x:', x.shape) for c in [1.0, 0.9, 0.001]: with self.subTest(compression=c): transition = Transition(in_channels, compression=c) print(transition) print('Parameters:', count_parameters(transition)) y = transition(x) print('y:', y.shape) self.assertEqual(y.shape[1], int(ceil(c * in_channels))) self.assertEqual(y.shape[2], H // 2) self.assertEqual(y.shape[3], W // 2)
def test_densenet(self): densenets = [ DenseNet(self.rgb_channels, self.imagenet_classes), DenseNet121(), DenseNet161(), DenseNet169(), DenseNet201(), ] for densenet in densenets: with self.subTest(klass=type(densenet).__name__): print(densenet) layers = count_conv2d(densenet) print('Layers:', layers) print('Parameters:', count_parameters(densenet)) logits = densenet(self.images) print('Logits:', logits.shape) self.assertEqual(logits.shape, Size((self.batch_size, self.imagenet_classes)))
def test_transition_down(self): in_channels = 512 H = 32 W = 64 x = Variable(FloatTensor(1, in_channels, H, W)) print('x:', x.shape) for d in [0.0, 0.2]: with self.subTest(dropout=d): transition = TransitionDown(in_channels, dropout=d) print(transition) print('Parameters:', count_parameters(transition)) y = transition(x) print('y:', y.shape) self.assertEqual(y.shape[1], in_channels) self.assertEqual(y.shape[2], H // 2) self.assertEqual(y.shape[3], W // 2)