-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainingInceptionResnetV2_ADNI.py
292 lines (245 loc) · 9.02 KB
/
TrainingInceptionResnetV2_ADNI.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
######################### Use CNN to classify the images #########################
######################### 使用GoogleNet进行迁移学习 #########################
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from torchvision import transforms, datasets
import torchvision.models as models
import matplotlib.pyplot as plt
import os
import numpy as np
from tqdm import tqdm
import pretrainedmodels
# %matplotlib inline
# Constants
IMG_WIDTH, IMG_HEIGHT = 299, 299
NUM_CLASSES = 5 # Adjust based on your classes
BATCH_SIZE = 10
EPOCHS = 150
LEARNING_RATE = 0.0001
val_freq = 10
# Check if GPU is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
os.chdir('D:/vscodes/AIinMed/final_project/AD_classification_python')
# 保存模型位置
save_dir = './models'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
# Define your dataset
data_dir = './netDataset'
train_dir = data_dir + '/training'
val_dir = data_dir + '/validation'
test_dir = data_dir + '/testing'
# Apply transformations (normalization, augmentation, etc.)
test_transforms = transforms.Compose([
transforms.Resize((IMG_WIDTH, IMG_HEIGHT)),
transforms.Grayscale(num_output_channels=3),
transforms.ToTensor(),
])
train_val_transforms = transforms.Compose([
transforms.RandomRotation((-35, 35)),
transforms.RandomResizedCrop((IMG_WIDTH, IMG_HEIGHT), scale=(0.5, 1.0)),
transforms.Grayscale(num_output_channels=3),
transforms.ToTensor(),
])
# load the dataset
train_dataset = datasets.ImageFolder(train_dir, train_val_transforms)
val_dataset = datasets.ImageFolder(val_dir, test_transforms)
test_dataset = datasets.ImageFolder(test_dir, test_transforms)
# create the dataloader
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=True)
# Load model without pretrained weights
net = pretrainedmodels.__dict__['inceptionresnetv2'](num_classes=1000, pretrained=None)
# Load the pretrained weights, except for the last layer
pretrained_dict = torch.load('./models/inceptionresnetv2-520b38e4.pth')
model_dict = net.state_dict()
# Remove weights for last_linear layer from pretrained_dict
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k not in ['last_linear.weight', 'last_linear.bias']}
# Update the current model's state_dict
model_dict.update(pretrained_dict)
# Load the updated state_dict into the model
net.load_state_dict(model_dict)
# Modify the last layer for your number of classes
net.last_linear = nn.Linear(net.last_linear.in_features, NUM_CLASSES) # 修改全连接层
# Move the network to the GPU if available
net = net.to(device)
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)
# Train the network
train_loss = []
val_loss = []
train_acc = []
val_acc = []
for epoch in range(EPOCHS):
############################
# Train
############################
net.train()
running_loss = 0.0
correct = 0
total = 0
# 使用tqdm包裹您的训练加载器
for i, data in enumerate(tqdm(train_loader, desc=f"Epoch {epoch+1}/{EPOCHS} [Train]", unit='batch')):
# Get the inputs
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
# Zero the parameter gradients
optimizer.zero_grad()
# Forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# Print statistics
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
############################
# Validate
############################
if epoch % val_freq == 0:
train_loss.append(running_loss / len(train_loader))
train_acc.append(correct / total)
net.eval()
running_loss = 0.0
correct = 0
total = 0
# 使用tqdm包裹您的验证加载器
with torch.no_grad():
for i, data in enumerate(tqdm(val_loader, desc=f"Epoch {epoch+1}/{EPOCHS} [Validate]", unit='batch')):
# Get the inputs
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
# Forward
outputs = net(inputs)
loss = criterion(outputs, labels)
# Print statistics
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
val_loss.append(running_loss / len(val_loader))
val_acc.append(correct / total)
print('[%d] Train loss: %.3f' %
(epoch + 1, train_loss[-1]))
print('[%d] Val loss: %.3f' %
(epoch + 1, val_loss[-1]))
print('[%d] Train acc: %.3f' %
(epoch + 1, train_acc[-1]))
print('[%d] Val acc: %.3f' %
(epoch + 1, val_acc[-1]))
# Plot the loss and accuracy curves
plt.figure()
plt.plot(train_loss, label='train')
plt.plot(val_loss, label='val')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
# plt.show()
plt.savefig('./docs/InceptionResnetV2_150_loss.png')
plt.figure()
plt.plot(train_acc, label='train')
plt.plot(val_acc, label='val')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
# plt.show()
plt.savefig('./docs/InceptionResnetV2_150_acc.png')
# 保存模型
torch.save(net, save_dir + '/InceptionResnetV2_150.pth')
## 混淆矩阵
from sklearn.metrics import confusion_matrix
import itertools
y_true = []
y_pred = []
net.eval()
with torch.no_grad():
for i, data in enumerate(tqdm(test_loader, desc=f"Testing", unit='batch')):
# Get the inputs
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
# Forward
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
y_true.extend(labels.cpu().numpy())
y_pred.extend(predicted.cpu().numpy())
cm = confusion_matrix(y_true, y_pred)
def plot_confusion_matrix(cm, classes,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
此函数打印并绘制混淆矩阵。
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
labels = ['AD', 'CN', 'EMCI', 'LMCI', 'MCI']
plt.figure()
plot_confusion_matrix(cm, labels, title='Confusion matrix')
# plt.show()
plt.savefig('./docs/InceptionResnetV2_150_cm.png')
## ROC曲线
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import numpy as np
# 计算每个样本的得分
net.eval()
y_score = []
y_true = []
with torch.no_grad():
for i, data in enumerate(tqdm(test_loader, desc=f"Testing", unit='batch')):
# Get the inputs
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
# Forward
outputs = net(inputs)
y_score.append(outputs.cpu().numpy())
y_true.append(labels.cpu().numpy())
y_score = np.concatenate(y_score)
y_true = np.concatenate(y_true)
# 计算每个类别的ROC曲线和AUC
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(NUM_CLASSES):
fpr[i], tpr[i], _ = roc_curve(y_true == i, y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
## 设置颜色循环
colors = ['aqua', 'darkorange', 'cornflowerblue', 'green', 'red']
labels = ['AD', 'CN', 'EMCI', 'LMCI', 'MCI']
# Plot the ROC curve for each class
plt.figure(figsize=(7, 7))
for i, color, lbl in zip(range(NUM_CLASSES), colors, labels):
plt.plot(fpr[i], tpr[i], color=color, lw=2,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(lbl, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=2)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Multi-class Receiver Operating Characteristic')
plt.legend(loc="lower right")
# plt.show()
plt.savefig('./docs/InceptionResnetV2_150_roc.png')