forked from enaserianhanzaei/DEC_Pytorch_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
139 lines (110 loc) · 5.82 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import torch
import torch.nn as nn
import numpy as np
from sklearn.cluster import KMeans
from utils import params
import os
from utils import datasetscls
import DeepEncoderClustering
from train import training
from utils import metrics
from utils.utilityfn import getinputsize
from train import training, testing, pretraining
torch.cuda.empty_cache()
torch.cuda.memory_summary(device=None, abbreviated=False)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if __name__ == "__main__":
# setting the hyper parameters
import argparse
parser = argparse.ArgumentParser(description='train',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--datadir', default='/data/stl/fc1/')
parser.add_argument('--batch_size', default=256, type=int)
parser.add_argument('--input_size', default=None)
parser.add_argument('--n_clusters', default=10, type=int)
parser.add_argument('--epochs', default=1000, type=int)
parser.add_argument('--pretrain_epochs', default=10, type=int)
parser.add_argument('--update_interval', default=30, type=int)
parser.add_argument('--tol', default=0.001, type=float)
parser.add_argument('--save_dir', default='./save/')
parser.add_argument('--save_model', default=True, type=bool)
parser.add_argument('--save_intermodel', default=False, type=bool)
args = parser.parse_args()
print(args)
#generator = datasetscls.customGenerator(args.datadir)
generator = datasetscls.STLGenerator(args.datadir)
try:
assert len(generator)>=1
except AssertionError:
print('There should at least one input file.')
raise
if not args.input_size:
args.input_size = getinputsize(generator)
DEC = DeepEncoderClustering.DEC(inputsize=args.input_size, dims=params.dims, n_clusters=args.n_clusters)
DEC.to(device)
ae_weights = f'{args.save_dir}/models/stl/'
if not os.path.exists(ae_weights):
os.makedirs(ae_weights)
if not os.path.exists(ae_weights+'ae_weights.pth'):
pretraining(model=DEC, dbgenerator=generator, savepath=ae_weights, batch_size=args.batch_size, epochs=args.pretrain_epochs)
else:
DEC.AE.load_state_dict(torch.load(ae_weights+'ae_weights.pth'))
DEC.train() # Set model to training mode
with torch.no_grad():
print('Initializing cluster centers with k-means. number of clusters %s' % args.n_clusters)
allfeatures = []
for _, filexy in enumerate(generator):
if isinstance(filexy, tuple) and len(filexy) == 2:
filex, filey = filexy
filex = filex.to(device)
filey = filey.to(device)
else:
filex = filexy
filex = filex.to(device)
allfeatures.append(DEC.AE.encoder(filex).clone().detach().cpu())
kmeans = KMeans(n_clusters=args.n_clusters, n_init=20)
y_pred_last = kmeans.fit_predict(torch.cat(allfeatures))
seedfeatures, seedlabels=None, None
clustcenters = torch.tensor(kmeans.cluster_centers_, dtype=torch.float, requires_grad=True)
clustcenters = clustcenters.to(device)
DEC.state_dict()["clustlayer.clustcenters"].copy_(clustcenters)
criterion = nn.KLDivLoss(reduction='batchmean')
optimizer = torch.optim.SGD(DEC.model.parameters(), lr=0.01, momentum=0.9)
delta_label = None
for epoch in range(args.epochs):
loss = 0
for _, filexy in enumerate(generator):
if isinstance(filexy, tuple) and len(filexy) == 2:
filex, filey = filexy
filex = filex.to(device)
filey = filey.to(device)
elif not isinstance(filexy, tuple):
filex = filexy
filex = filex.to(device)
filey = None
train_loss = training(model=DEC, optimizer=optimizer, criterion=criterion, y_pred_last=y_pred_last, x=filex, y=filey, batch_size=args.batch_size, update_interval=args.update_interval, device = device)
loss += train_loss
if filey is not None:
y_pred, acty = testing(model=DEC, dbgenerator=generator, device=device)
acc = np.round(metrics.acc(acty.clone().detach().cpu().numpy(), y_pred.clone().detach().cpu().numpy()), 5)
nmi = np.round(metrics.nmi(acty.clone().detach().cpu().numpy().squeeze(), y_pred.clone().detach().cpu().numpy()), 5)
ari = np.round(metrics.ari(acty.clone().detach().cpu().numpy().squeeze(), y_pred.clone().detach().cpu().numpy()), 5)
loss = np.round(loss/len(generator), 5)
print('epoch %d: acc = %.5f, nmi = %.5f, ari = %.5f' % (epoch, acc, nmi, ari), ' ; loss=', loss)
else:
y_pred = testing(model=DEC, dbgenerator=generator, device=device, return_truth=False)
nmi = np.round(metrics.nmi(y_pred_last, y_pred.clone().detach().cpu().numpy()), 5)
ari = np.round(metrics.ari(y_pred_last, y_pred.clone().detach().cpu().numpy()), 5)
loss = np.round(loss/len(generator), 5)
print('epoch %d: nmi = %.5f, ari = %.5f' % (epoch, nmi, ari), ' ; loss=', loss)
delta_label = np.sum(y_pred_last!= y_pred.clone().detach().cpu().numpy()) / y_pred.shape[0]
if args.tol is not None and delta_label < args.tol:
print('delta_label ', delta_label, '< tol ', args.tol)
print('Reached tolerance threshold. Stopping training.')
break
y_pred_last = y_pred.detach().clone().cpu().numpy()
if args.save_intermodel:
torch.save(DEC.state_dict(), f'{args.save_dir}/models/dec_weights_%s_epoch%s.pth'%(args.n_clusters, epoch))
if args.save_model:
torch.save(DEC.state_dict(), f'{args.save_dir}/models/dec_weights_%s.pth'%(args.n_clusters))