-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
89 lines (66 loc) · 2.53 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
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
import tqdm
import os
import torch
import imageio
import numpy as np
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from model.FCN_torch import FCN
from torch.utils.data import DataLoader
from utilities.dataReader import datareader
# Device configuration
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Assuming that we are on a CUDA machine, this should print a CUDA device:
print(device)
# Hyper-parameters
input_size = 3
num_classes = 2
num_epochs = 25
batch_size = 2
learning_rate = 0.0001
image_list_path = r'dataset/train_list.txt'
image_test_path = r'dataset/images/image-name.jpeg'
mask_test_path = r'dataset/masks/image-name.png'
ckpt_path = r'ckpt\FCN_model.pytorch'
name = 'image-name'
image_test = imageio.imread(image_test_path)
imageio.imsave(os.path.join('dataset', name + '.jpeg'), image_test, format='jpeg')
image_test = torch.from_numpy(np.expand_dims(np.moveaxis(image_test, -1, 0), axis=0))
mask_test = imageio.imread(mask_test_path)
imageio.imsave(os.path.join('dataset', name + '.png'), mask_test, format='png')
model = FCN(num_classes=num_classes)
model.to(device)
print(model)
dtset = datareader(image_list_path)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate ,momentum=0.9, weight_decay=1/16)
for epoch in range(num_epochs):
dt_loader_train = DataLoader(dtset, batch_size=batch_size, shuffle=True)
for batch_index, batch in enumerate(dt_loader_train):
image = batch[0]
mask = batch[1]
image = image.to(device)
mask = mask.to(device=device, dtype=torch.long)
y_pred = model(image)
loss = criterion(y_pred, mask)
print('Epoch ',epoch,' iter ',batch_index*2,' loss : ', loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.save(model.state_dict(), ckpt_path)
with torch.no_grad():
image_test = image_test.to(device)
# Generate prediction
prediction = model(image_test)
prediction = np.squeeze(prediction, axis=0)
prediction = prediction.cpu().numpy()
# Predicted class value using argmax
predicted_class = np.argmax(prediction, axis=0)
predicted_class = np.array(predicted_class, dtype=np.uint8)
# Show result
plt.imshow(predicted_class, cmap='gray')
plt.show()
predicted_class[predicted_class==1]=255
imageio.imsave(os.path.join('dataset', name + '_predicted.png'), predicted_class, format='png')