VGG-Cplus.py 1.5 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
1
2
3
4
5
6
7
# 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
Benjamin Thomas Graham's avatar
tidy  
Benjamin Thomas Graham committed
8
9
10
import torch.nn as nn
import sparseconvnet as scn
from data import get_iterators
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
11
12

# two-dimensional SparseConvNet
Benjamin Thomas Graham's avatar
tidy  
Benjamin Thomas Graham committed
13
14
15
16
17
18
19
20
21
22
23
24
class Model(nn.Module):
    def __init__(self):
        nn.Module.__init__(self)
        self.sparseModel = scn.SparseVggNet(2, 3, [
            ['C', 16, 8], ['C', 16, 8], 'MP',
            ['C', 32, 8], ['C', 32, 8], 'MP',
            ['C', 48, 16], ['C', 48, 16], 'MP',
            ['C', 64, 16], ['C', 64, 16], 'MP',
            ['C', 96, 16], ['C', 96, 16]]
        ).add(scn.Convolution(2, 112, 128, 3, 2, False)
        ).add(scn.BatchNormReLU(128)
        ).add(scn.SparseToDense(2, 128))
25
26
        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
tidy  
Benjamin Thomas Graham committed
27
28
29
        self.linear = nn.Linear(128, 3755)

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

Benjamin Thomas Graham's avatar
tidy  
Benjamin Thomas Graham committed
36
model = Model()
37
38
39
40
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
41
42
scn.ClassificationTrainValidate(
    model, dataset,
Benjamin Thomas Graham's avatar
tidy  
Benjamin Thomas Graham committed
43
44
45
46
    {'n_epochs': 100,
     'initial_lr': 0.1,
     'lr_decay': 0.05,
     'weight_decay': 1e-4,
47
48
     'use_cuda': torch.cuda.is_available(),
     'check_point': False, })