DenseNet.py 1.59 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
1
2
3
4
5
6
7
8
9
# Copyright 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

import torch
import torch.legacy.nn as nn
import sparseconvnet.legacy as scn
Benjamin Thomas Graham's avatar
tidy  
Benjamin Thomas Graham committed
10
from data import get_iterators
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
11
12
13
14
15
16
17
18
19

# Use the GPU if there is one, otherwise CPU
dtype = 'torch.cuda.FloatTensor' if torch.cuda.is_available() else 'torch.FloatTensor'

# two-dimensional SparseConvNet
model = nn.Sequential()
sparseModel = scn.Sequential()
denseModel = nn.Sequential()
model.add(sparseModel).add(denseModel)
20
sparseModel.add(scn.SubmanifoldConvolution(2, 3, 8, 3, False))\
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    .add(scn.SparseDenseNet(2, 8, [
        {'pool': 'MP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 8},
        {'pool': 'BN-R-C-AP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 8},
        {'pool': 'BN-R-C-AP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 8},
        {'pool': 'BN-R-C-AP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 8}]))
n_out = sparseModel.modules[-1].nOutputPlanes
sparseModel.add(scn.Convolution(2, n_out, 64, 6, 1, False))
sparseModel.add(scn.BatchNormReLU(64))
sparseModel.add(scn.SparseToDense(2))
denseModel.add(nn.View(-1, 64))
denseModel.add(nn.Linear(64, 183))
model.type(dtype)
print(model)

spatial_size = sparseModel.suggestInputSize(torch.LongTensor([1, 1]))
print('input spatial size', spatial_size)
Benjamin Thomas Graham's avatar
tidy  
Benjamin Thomas Graham committed
41
dataset = get_iterators(spatial_size, 63, 2)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
42
43
44
scn.ClassificationTrainValidate(
    model, dataset,
    {'nEpochs': 100, 'initial_LR': 0.1, 'LR_decay': 0.05, 'weightDecay': 1e-4})