How to custom layer from architecture model in pytorch
Code 1
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
# -*- coding: utf-8 -*-
"""Sample01.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1c2OnRc-3-nXyQecNlsWXVGcFrkD4q4zL
"""
import torch
import torch.nn as nn
# Định nghĩa kiến trúc mô hình CNN đơn giản
class SimpleCNN(nn.Module):
def __init__(self, num_classes):
super(SimpleCNN, self).__init__()
self.conv_layer = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Linear(32 * 8 * 8, 128), # Tính toán dựa trên kích thước ảnh đầu vào
nn.ReLU(),
nn.Linear(128, num_classes)
)
def forward(self, x):
x = self.conv_layer(x)
x = x.view(x.size(0), -1)
x = self.fc_layer(x)
return x
# Số lượng lớp phân loại
num_classes = 10
# Khởi tạo mô hình
model = SimpleCNN(num_classes)
# In ra kiến trúc của mô hình
from torchsummary import summary
# Định nghĩa lại mô hình
model = SimpleCNN(num_classes)
# Đưa mô hình vào thiết bị (GPU nếu có)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# Sử dụng hàm summary để in ra tóm tắt
summary(model, input_size=(3, 32, 32)) # Thay đổi kích thước ảnh đầu vào nếu cần
"""Dựa vào kiến trúc mô hình, hãy viết code pytorch thể hiện nó (ChatGPT)
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 16, 32, 32] 448
ReLU-2 [-1, 16, 32, 32] 0
MaxPool2d-3 [-1, 16, 16, 16] 0
Conv2d-4 [-1, 32, 16, 16] 4,640
ReLU-5 [-1, 32, 16, 16] 0
MaxPool2d-6 [-1, 32, 8, 8] 0
Linear-7 [-1, 128] 262,272
ReLU-8 [-1, 128] 0
Linear-9 [-1, 10] 1,290
"""
Code 2
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
import torch
import torch.nn as nn
class YourModel(nn.Module):
def __init__(self):
super(YourModel, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=1, padding=1)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1)
self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(in_features=32 * 8 * 8, out_features=128)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(in_features=128, out_features=10)
def forward(self, x):
x = self.maxpool1(self.relu1(self.conv1(x)))
x = self.maxpool2(self.relu2(self.conv2(x)))
x = x.view(x.size(0), -1)
x = self.relu3(self.fc1(x))
x = self.fc2(x)
return x
# Create an instance of your model
model1 = YourModel()
# Print the model architecture
from torchsummary import summary
# Đưa mô hình vào thiết bị (GPU nếu có)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model1.to(device)
# Sử dụng hàm summary để in ra tóm tắt
summary(model1, input_size=(3, 32, 32)) # Thay đổi kích thước ảnh đầu vào nếu cần
Code 3
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
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
# Create an instance of YourModel
class YourModel(nn.Module):
def __init__(self):
super(YourModel, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3, padding=1)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1)
self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(in_features=32 * 7 * 7, out_features=128)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(in_features=128, out_features=10)
def forward(self, x):
x = self.maxpool1(self.relu1(self.conv1(x)))
x = self.maxpool2(self.relu2(self.conv2(x)))
x = x.view(x.size(0), -1)
x = self.relu3(self.fc1(x))
x = self.fc2(x)
return x
# Hyperparameters
batch_size = 64
learning_rate = 0.001
num_epochs = 10
# MNIST dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# Load model
model = YourModel()
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Training loop
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backpropagation and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{total_step}], Loss: {loss.item():.4f}')
print('Training finished')
# Test the model
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Test Accuracy: {100 * correct / total:.2f}%')
Code 4
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
import torch
import torch.nn as nn
# Định nghĩa U-Net kiến trúc
class UNet(nn.Module):
def __init__(self, in_channels, out_channels):
super(UNet, self).__init__()
# Encoder
self.encoder = nn.Sequential(
nn.Conv2d(in_channels, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
# Middle layer
self.middle = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
# Decoder
self.decoder = nn.Sequential(
nn.Conv2d(128, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(64, out_channels, kernel_size=2, stride=2)
)
def forward(self, x):
x1 = self.encoder(x)
x2 = self.middle(x1)
x3 = self.decoder(x2)
return x3
# Khởi tạo mạng U-Net
in_channels = 3 # Số lượng channels của ảnh đầu vào
out_channels = 3 # Số lượng channels của ảnh đầu ra (ví dụ: ảnh segment)
# Print the model architecture
from torchsummary import summary
model1 = UNet(in_channels, out_channels)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model1.to(device)
# Sử dụng hàm summary để in ra tóm tắt
summary(model1, input_size=(3, 32, 32)) # Thay đổi kích thước ảnh đầu vào nếu cần
"""viết code pytorch thể hiện theo kiến trúc sau:
Layer (type) Output Shape Param #
Conv2d-1 [-1, 64, 32, 32] 1,792
ReLU-2 [-1, 64, 32, 32] 0
Conv2d-3 [-1, 64, 32, 32] 36,928
ReLU-4 [-1, 64, 32, 32] 0
MaxPool2d-5 [-1, 64, 16, 16] 0
Conv2d-6 [-1, 128, 16, 16] 73,856
ReLU-7 [-1, 128, 16, 16] 0
Conv2d-8 [-1, 128, 16, 16] 147,584
ReLU-9 [-1, 128, 16, 16] 0
MaxPool2d-10 [-1, 128, 8, 8] 0
Conv2d-11 [-1, 64, 8, 8] 73,792
ReLU-12 [-1, 64, 8, 8] 0
Conv2d-13 [-1, 64, 8, 8] 36,928
ReLU-14 [-1, 64, 8, 8] 0
ConvTranspose2d-15 [-1, 3, 16, 16] 771
"""
Code 5
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
import torch
import torch.nn as nn
class CustomModel(nn.Module):
def __init__(self):
super(CustomModel, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.middle = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.decoder = nn.Sequential(
nn.Conv2d(128, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(64, 3, kernel_size=2, stride=2)
)
def forward(self, x):
x1 = self.encoder(x)
x2 = self.middle(x1)
x3 = self.decoder(x2)
return x3
# Khởi tạo mô hình
model1 = CustomModel()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model1.to(device)
# Sử dụng hàm summary để in ra tóm tắt
summary(model1, input_size=(3, 32, 32)) # Thay đổi kích thước ảnh đầu vào nếu cần
Code 6
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
import torch
import torch.nn as nn
from torchsummary import summary
class CustomModel(nn.Module):
def __init__(self):
super(CustomModel, self).__init__()
self.rescaling = nn.Sequential(
nn.Conv3d(10, 3, kernel_size=1), # Rescaling 10 channels to 3 channels
nn.ReLU(inplace=True)
)
self.time_distributed = nn.Sequential(
nn.Conv3d(3, 1280, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
self.dense1 = nn.Sequential(
nn.Conv3d(1280, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Dropout3d(0.5)
)
self.dense2 = nn.Sequential(
nn.Conv3d(256, 15, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
self.global_avg_pooling = nn.AdaptiveAvgPool3d(1)
def forward(self, x):
x = self.rescaling(x)
x = self.time_distributed(x)
x = self.dense1(x)
x = self.dense2(x)
x = self.global_avg_pooling(x)
return x.view(x.size(0), -1) # Flatten the tensor
# Khởi tạo mô hình
model1 = CustomModel()
# In ra kiến trúc mô hình
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model1.to(device)
# Sử dụng thư viện torchsummary để in summary của mô hình
summary(model1, (10, 224, 224, 3)) # Đầu vào có kích thước (batch_size, channels, height, width, depth)
Code 7
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
# Create an instance of YourModel
class YourModel(nn.Module):
def __init__(self):
super(YourModel, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3, padding=1)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1)
self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(in_features=32 * 7 * 7, out_features=128)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(in_features=128, out_features=10)
def forward(self, x):
x = self.maxpool1(self.relu1(self.conv1(x)))
x = self.maxpool2(self.relu2(self.conv2(x)))
x = x.view(x.size(0), -1)
x = self.relu3(self.fc1(x))
x = self.fc2(x)
return x
# Hyperparameters
batch_size = 64
learning_rate = 0.001
num_epochs = 10
# MNIST dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# Load model
model = YourModel()
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
'''
# Training loop
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backpropagation and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{total_step}], Loss: {loss.item():.4f}')
print('Training finished')
'''
# Training loop
total_step = len(train_loader)
checkpoint_interval = 5
best_accuracy = 0.0 # Initialize the best accuracy
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backpropagation and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{total_step}], Loss: {loss.item():.4f}')
# Calculate test accuracy
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = correct / total * 100.0
print(f'Test Accuracy: {accuracy:.2f}%')
# Save best checkpoint if the accuracy is better
if accuracy > best_accuracy:
best_accuracy = accuracy
checkpoint_path = 'best_checkpoint.pth'
torch.save({
'epoch': epoch+1,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'accuracy': accuracy,
}, checkpoint_path)
print(f'Best checkpoint saved at epoch {epoch+1} with accuracy {accuracy:.2f}%')
print('Training finished')
# Test the model
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Test Accuracy: {100 * correct / total:.2f}%')
###### Load checkpoint and testing ###
# Define a function to test the model using a given dataloader
def test_model(model, dataloader):
model.eval() # Set the model to evaluation mode
correct = 0
total = 0
with torch.no_grad():
for images, labels in dataloader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = correct / total * 100.0
print(f'Test Accuracy: {accuracy:.2f}%')
# Load the best checkpoint
checkpoint_path = 'best_checkpoint.pth'
checkpoint = torch.load(checkpoint_path)
model.load_state_dict(checkpoint['model_state_dict'])
# Test the model using the test data loader
test_model(model, test_loader)
Tài liệu tham khảo
Internet
Hết.