hello-world.py 2.23 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
import sparseconvnet as scn
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
9
10

# Use the GPU if there is one, otherwise CPU
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
11
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
12
13
14

model = scn.Sequential().add(
    scn.SparseVggNet(2, 1,
15
                     [['C', 8], ['C', 8], ['MP', 3, 2],
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
16
17
                      ['C', 16], ['C', 16], ['MP', 3, 2],
                      ['C', 24], ['C', 24], ['MP', 3, 2]])
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
18
).add(
19
    scn.SubmanifoldConvolution(2, 24, 32, 3, False)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
20
21
22
).add(
    scn.BatchNormReLU(32)
).add(
23
    scn.SparseToDense(2, 32)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
24
).to(device)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
25
26

# output will be 10x10
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
27
inputSpatialSize = model.input_spatial_size(torch.LongTensor([10, 10]))
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
28
input_layer = scn.InputLayer(2, inputSpatialSize)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
29

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
30
31
32
33
34
msgs = [[" X   X  XXX  X    X    XX     X       X   XX   XXX   X    XXX   ",
         " X   X  X    X    X   X  X    X       X  X  X  X  X  X    X  X  ",
         " XXXXX  XX   X    X   X  X    X   X   X  X  X  XXX   X    X   X ",
         " X   X  X    X    X   X  X     X X X X   X  X  X  X  X    X  X  ",
         " X   X  XXX  XXX  XXX  XX       X   X     XX   X  X  XXX  XXX   "],
35

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
36
37
38
39
40
        [" XXX              XXXXX      x   x     x  xxxxx  xxx ",
         " X  X  X   XXX       X       x   x x   x  x     x  x ",
         " XXX                X        x   xxxx  x  xxxx   xxx ",
         " X     X   XXX       X       x     x   x      x    x ",
         " X     X          XXXX   x   x     x   x  xxxx     x ",]]
Ed Ng's avatar
Ed Ng committed
41

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
42
43

# Create Nx3 and Nx1 vectors to encode the messages above:
Ed Ng's avatar
Ed Ng committed
44
45
locations = []
features = []
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
46
47
48
49
50
51
for batchIdx, msg in enumerate(msgs):
    for y, line in enumerate(msg):
        for x, c in enumerate(line):
            if c == 'X':
                locations.append([y, x, batchIdx])
                features.append([1])
Ed Ng's avatar
Ed Ng committed
52
locations = torch.LongTensor(locations)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
53
features = torch.FloatTensor(features).to(device)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
54

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
55
56
input = input_layer([locations,features])
print('Input SparseConvNetTensor:', input)
Benjamin Thomas Graham's avatar
README  
Benjamin Thomas Graham committed
57
output = model(input)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
58

59
# Output is 2x32x10x10: our minibatch has 2 samples, the network has 32 output
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
60
# feature planes, and 10x10 is the spatial size of the output.
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
61
print('Output SparseConvNetTensor:', output)