-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_train.py
58 lines (48 loc) · 2.1 KB
/
full_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
from dataset import LettucePointCloudDataset
from transformers import RandomRotation
from torch.utils.data import DataLoader
from torchvision import transforms
from torch.utils.data.dataset import random_split
import torch
import torch.nn as nn
from models.pointnet import PointNet
from models.randlanet import RandLANet
from models.pointnet2 import PointNet2
from models.dgcnn import DGCNN
from utils.utils import training_process_plot_save, test_accuracy, get_model_output_and_loss, get_model_optimizer_and_scheduler
from utils.visualizer import PointCloudVisualizer, labels_to_soil_and_lettuce_colors
import numpy as np
train_dataset = LettucePointCloudDataset(
root_dir='',
is_train=True,
transform=transforms.Compose([
RandomRotation()
])
)
train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f'Device: {device}')
# model = PointNet().to(device)
# model = RandLANet(d_in=3, num_classes=2, num_neighbors=16, decimation=4, device=device).to(device)
# model = PointNet2(2).to(device)
model = DGCNN(num_classes=2).to(device)
model.train()
model_name = type(model).__name__
print(f'Model: {model_name}\n{"-"*30}')
num_epochs = 150
optimizer, scheduler = get_model_optimizer_and_scheduler(model, num_epochs)
for epoch in range(num_epochs):
train_loss, train_acc = .0, .0
for input, labels in train_dataloader:
input, labels = input.to(device).squeeze().float(), labels.to(device)
optimizer.zero_grad()
outputs, loss = get_model_output_and_loss(model, input, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
train_acc += (labels == outputs.argmax(1)).sum().item() / np.prod(labels.shape)
if scheduler is not None:
scheduler.step()
train_loss, train_acc = train_loss/len(train_dataloader), train_acc/len(train_dataloader)
print(f'Epoch: {"{:2d}".format(epoch)} -> \t Train Loss: {"%.10f"%train_loss} \t Train Accuracy: {"%.4f"%train_acc}')
torch.save(model.state_dict(), f'pretrained_models/{model_name}.pth')