fake_train.py 4.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
from pathlib import Path

import fire
import numpy as np
import torch
import torch.nn.functional as F
import tqdm
from torch import distributed, nn
from torch.utils import data
from torch.utils.data import DataLoader, Dataset
from torchvision import datasets, transforms

import spconv
from spconv.test_utils import generate_sparse_data


class FakeSparseDataset(Dataset):
    def __len__(self):
        return 500

    def __getitem__(self, idx):
        data_ranges = {
            0: [-1, 1],
            1: [0, 2],
            2: [-2, 0],
            3: [-2, -2],
        }
        l = np.random.randint(0, 4, size=[2])

        data = generate_sparse_data([16, 64, 64], [16 * 64 * 64 // 2],
                                    3,
                                    data_range=data_ranges[l[0]],
                                    with_dense=False)
        data2 = generate_sparse_data([16, 64, 64], [16 * 64 * 64 // 2],
                                     3,
                                     data_range=data_ranges[l[1]],
                                     with_dense=False)

        features = np.ascontiguousarray(data["features"]).astype(np.float32)
        indices = np.ascontiguousarray(
            data["indices"][:, [3, 0, 1, 2]]).astype(np.int32)
        features2 = np.ascontiguousarray(data2["features"]).astype(np.float32)
        indices2 = np.ascontiguousarray(
            data2["indices"][:, [3, 0, 1, 2]]).astype(np.int32)
        features = np.ascontiguousarray(np.concatenate([features, features2]))
        indices = np.ascontiguousarray(np.concatenate([indices, indices2]))
        return features, indices, l


class FakeClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = spconv.SparseSequential(
55
56
57
58
59
60
            spconv.SubMConv3d(3,
                              8,
                              3,
                              indice_key="subm1",
                              padding=1,
                              use_hash=False),
61
62
63
64
65
            nn.BatchNorm1d(8),
            nn.ReLU(),
            spconv.SparseConv3d(8, 16, 3, stride=2, padding=1, use_hash=False),
            nn.BatchNorm1d(16),
            nn.ReLU(),
66
67
68
69
70
71
            spconv.SubMConv3d(16,
                              16,
                              3,
                              indice_key="subm2",
                              padding=1,
                              use_hash=False),
72
73
            nn.BatchNorm1d(16),
            nn.ReLU(),
74
75
            spconv.SparseConv3d(16, 32, 3, stride=2, padding=1,
                                use_hash=False),
76
77
            nn.BatchNorm1d(32),
            nn.ReLU(),
78
79
80
81
82
83
            spconv.SubMConv3d(32,
                              32,
                              3,
                              indice_key="subm3",
                              padding=1,
                              use_hash=False),
84
85
            nn.BatchNorm1d(32),
            nn.ReLU(),
86
87
            spconv.SparseConv3d(32, 64, 3, stride=2, padding=1,
                                use_hash=False),
88
89
            nn.BatchNorm1d(64),
            nn.ReLU(),
90
91
92
93
94
95
            spconv.SubMConv3d(64,
                              64,
                              3,
                              indice_key="subm4",
                              padding=1,
                              use_hash=False),
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
            nn.BatchNorm1d(64),
            nn.ReLU(),
            spconv.ToDense()  # [64, 2, 8, 8]
        )
        self.linear = nn.Linear(64 * 2 * 8 * 8, 4)

    def forward(self, features, indices):
        indices = indices.int()
        x = spconv.SparseConvTensor(features, indices, [16, 64, 64], 2)
        x = self.net(x)
        x = x.view(2, -1)
        x = self.linear(x)
        return x


def run():
    np.random.seed(50051)
    ds = FakeSparseDataset()
    device = torch.device('cuda')
    model = FakeClassifier()
    model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

    for i in tqdm.tqdm(list(range(100))):
        # for j in range(4):
        #     features, indices, label = ds[(i * 4 + j) % len(ds)]
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        features, indices, label = ds[i % len(ds)]
        features_t = torch.from_numpy(features)
        indices_t = torch.from_numpy(indices)
        features_t = features_t.to(device)
        indices_t = indices_t.to(device)
        target = torch.from_numpy(label).to(device)
        output = model(features_t, indices_t)
        # print(output.shape)
        loss = F.cross_entropy(output, target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


def dev():
    ds = FakeSparseDataset()
    for i in range(10):
        features, indices, label = ds[i]
        print(indices[:10])
    features_t = torch.from_numpy(features.astype(np.float32)).cuda()
    indices_t = torch.from_numpy(indices.astype(np.int32)).cuda()
    net = FakeClassifier().cuda()
    net(features_t, indices_t)


def main():
    run()


if __name__ == "__main__":
    fire.Fire(main)