-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
63 lines (47 loc) · 1.8 KB
/
train.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
import torch
import torch.optim as optim
from training_engine import init_network_weights_from_pretraining, train_ae, train_encoder
from networks.models import DeepAutoEncoder, Encoder
import torchvision.transforms as transforms
from torchvision import datasets
device = torch.device("cuda")
# Dataset
#root = 'deep-anomaly-classification/samples' # a folder named with your class name and its images must be here
root = 'dataset'
size = 128
normal_class = 0
data_transforms = transforms.Compose([
transforms.Grayscale(),
transforms.Resize((size,size)),
transforms.ToTensor(),
])
dataset = datasets.ImageFolder(root, data_transforms)
train_set = dataset
train_loader = torch.utils.data.DataLoader(train_set, batch_size=8, shuffle=True, num_workers=4)
class_name = dataset.classes[normal_class]
# Train AutoEncoder
print("Starting AutoEncoder Training")
ae_net = DeepAutoEncoder()
ae_net.to(device)
ae_optimizer = optim.Adam(ae_net.parameters(), lr=3e-4, weight_decay=1e-6)
ae_net = train_ae(ae_net, ae_optimizer, train_loader, device, epoches=150)
torch.save(ae_net.state_dict(), "AutoEncoder.pth")
# Init Encoder with autoencoder weights
net = Encoder()
net = init_network_weights_from_pretraining(net, ae_net)
net.to(device)
# Train Net
lr_milestones = ()
optimizer = optim.Adam(net.parameters(), lr=3e-4, weight_decay=1e-6)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=lr_milestones, gamma=0.1)
model_dict = train_encoder(net=net,
train_loader=train_loader,
optimizer=optimizer,
scheduler=scheduler,
normal_class=normal_class,
lr_milestones=lr_milestones,
n_epochs=150,
device=device,
warm_up_n_epochs=35,
objective="soft-boundary")
torch.save(model_dict, f"DeepSVD_{class_name}.tar")