-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess_gmm.py
76 lines (64 loc) · 2.91 KB
/
preprocess_gmm.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#####################################################################################
# MIT License
# Copyright (c) 2022 Jiawei Ren
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
####################################################################################
import loaddata
import argparse
from sklearn.mixture import GaussianMixture
import torch
import joblib
import time
from loaddata import TRAIN_BUCKET_NUM
parser = argparse.ArgumentParser(description='')
# Args for GMM
parser.add_argument('--K', type=int, default=16, help='GMM number of components')
parser.add_argument('--batch_size', default=32, type=int, help='batch size number')
bucket_centers = torch.linspace(0, 10, 101)[:-1] + 0.05
TRAIN_BUCKET_NUM = [TRAIN_BUCKET_NUM[7]] * 7 + TRAIN_BUCKET_NUM[7:]
def fit_gmm(args):
end_time = time.time()
all_labels = []
# There are too many pixels in NYUD2-DIR to fit a GMM
# We directly use the statistics provided in the original code
for i in range(100):
all_labels += [bucket_centers[i] for _ in range(TRAIN_BUCKET_NUM[i] // 1000000)]
all_labels = torch.tensor(all_labels).reshape(1, -1)
print('All labels shape: ', all_labels.shape)
print(time.time() - end_time)
end_time = time.time()
print('Training labels curated')
print('Fitting GMM...')
gmm = GaussianMixture(n_components=args.K, random_state=0, verbose=2).fit(
all_labels.reshape(-1, 1).numpy())
print(time.time() - end_time)
print('GMM fiited')
print("Dumping...")
gmm_dict = {}
gmm_dict['means'] = gmm.means_
gmm_dict['weights'] = gmm.weights_
gmm_dict['variances'] = gmm.covariances_
return gmm_dict
def main():
args = parser.parse_args()
train_loader = loaddata.getTrainingData(args, args.batch_size)
gmm_dict = fit_gmm(train_loader, args)
gmm_path = 'gmm.pkl'
joblib.dump(gmm_dict, gmm_path)
print('Dumped at {}'.format(gmm_path))
if __name__ == '__main__':
main()