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

import torch
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
8
9
import torch.nn as nn
import sparseconvnet 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

# two-dimensional SparseConvNet
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
13
14
15
class Model(nn.Module):
    def __init__(self):
        nn.Module.__init__(self)
16
        self.sparseModel = scn.Sequential(
17
18
19
            scn.SubmanifoldConvolution(2, 3, 8, 3, False),
            scn.MaxPooling(2, 3, 2),
            scn.SparseResNet(2, 8, [
20
21
22
                        ['b', 8, 2, 1],
                        ['b', 16, 2, 2],
                        ['b', 24, 2, 2],
23
24
25
26
27
28
                        ['b', 32, 2, 2]]),
            scn.Convolution(2, 32, 64, 5, 1, False),
            scn.BatchNormReLU(64),
            scn.SparseToDense(2, 64))
        self.spatial_size= self.sparseModel.input_spatial_size(torch.LongTensor([1, 1]))
        self.inputLayer = scn.InputLayer(2,self.spatial_size,2)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
29
        self.linear = nn.Linear(64, 183)
30

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
31
    def forward(self, x):
32
        x = self.inputLayer(x)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
33
        x = self.sparseModel(x)
34
        x = x.view(-1, 64)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
35
36
        x = self.linear(x)
        return x
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
37

38
model = Model()
39
40
41
42
scale=63
dataset = get_iterators(model.spatial_size, scale)
print('Input spatial size:', model.spatial_size, 'Data scale:', scale)

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
43
44
scn.ClassificationTrainValidate(
    model, dataset,
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
45
    {'n_epochs': 100,
46
47
48
     'initial_lr': 0.1,
     'lr_decay': 0.05,
     'weight_decay': 1e-4,
49
50
     'use_cuda': torch.cuda.is_available(),
     'check_point': False, })