def test_SpatialDropout(self): p = 0.2 b = random.randint(1, 5) w = random.randint(1, 5) h = random.randint(1, 5) nfeats = 1000 input = torch.Tensor(b, nfeats, w, h).fill_(1) module = nn.SpatialDropout(p) module.training() output = module.forward(input) self.assertLess(abs(output.mean() - (1 - p)), 0.05) gradInput = module.backward(input, input) self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05) # Check that these don't raise errors module.__repr__() str(module)
def FCNN(): num_classes = 2 n_layers_enc = 32 n_layers_ctx = 128 n_input = 5 prob_drop = 0.25 layers = [] # Encoder pool = nn2.SpatialMaxPooling(2,2,2,2) layers.append(nn2.SpatialConvolution(n_input, n_layers_enc, 3, 3, 1, 1, 1, 1)) layers.append(nn.ELU()) layers.append(nn2.SpatialConvolution(n_layers_enc, n_layers_enc, 3, 3, 1, 1, 1, 1)) layers.append(nn.ELU()) layers.append(pool) # Context Module layers.append(nn2.SpatialDilatedConvolution(n_layers_enc, n_layers_ctx, 3, 3, 1, 1, 1, 1, 1, 1)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_ctx, 3, 3, 1, 1, 2, 2, 2, 2)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_ctx, 3, 3, 1, 1, 4, 4, 4, 4)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_ctx, 3, 3, 1, 1, 8, 8, 8, 8)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_ctx, 3, 3, 1, 1, 16, 16, 16, 16)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_ctx, 3, 3, 1, 1, 32, 32, 32, 32)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_ctx, 3, 3, 1, 1, 64, 64, 64, 64)) layers.append(nn.ELU()) layers.append(nn2.SpatialDropout(prob_drop)) layers.append(nn2.SpatialDilatedConvolution(n_layers_ctx, n_layers_enc, 1, 1)) layers.append(nn.ELU()) # Nao havia no paper # Decoder layers.append(nn2.SpatialMaxUnpooling(pool)) layers.append(nn2.SpatialConvolution(n_layers_enc, n_layers_enc, 3, 3, 1, 1, 1, 1)) layers.append(nn.ELU()) layers.append(nn2.SpatialConvolution(n_layers_enc, num_classes, 3, 3, 1, 1, 1, 1)) layers.append(nn.ELU()) layers.append(nn.SoftMax()) # Nao havia no paper return nn.Sequential(*layers)