學習來源:@浙大疏錦行
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os
?
# 設置隨機種子確保結果可復現
torch.manual_seed(42)
np.random.seed(42)
?
# 將 'path/to/your_dataset' 替換為你的數據集所在的根目錄
data_dir = './data/10 Big Cats of the Wild - Image Classification'
train_dir = os.path.join(data_dir, 'train')
test_dir = os.path.join(data_dir, 'test')
?
if not os.path.isdir(data_dir):
? ? raise FileNotFoundError(
? ? ? ? f"Dataset directory not found at '{data_dir}'. "
? ? ? ? f"Please update the 'data_dir' variable to your dataset's path."
? ? )
?
transform = transforms.Compose([
? ? transforms.Resize((32, 32)),
? ? transforms.ToTensor(),
? ? transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
?
# 加載訓練集和測試集
trainset = torchvision.datasets.ImageFolder(root=train_dir, transform=transform)
testset = torchvision.datasets.ImageFolder(root=test_dir, transform=transform)
?
# 從訓練數據集中自動獲取類別名稱和數量
classes = trainset.classes
num_classes = len(classes)
print(f"從數據集中找到 {num_classes} 個類別: {classes}")
?
# 創建數據加載器
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False, num_workers=2)
?
?
# --- MODIFICATION 3: 動態調整CNN模型以適應你的數據集 ---
class SimpleCNN(nn.Module):
? ? def __init__(self, num_classes): # 將類別數量作為參數傳入
? ? ? ? super(SimpleCNN, self).__init__()
? ? ? ? self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
? ? ? ? self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
? ? ? ? self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
? ? ? ? self.pool = nn.MaxPool2d(2, 2)
? ? ? ? # 輸入特征數128 * 4 * 4取決于輸入圖像大小和網絡結構。
? ? ? ? # 由于我們將所有圖像調整為32x32,經過3次2x2的池化后,尺寸變為 32 -> 16 -> 8 -> 4。所以這里是4*4。
? ? ? ? self.fc1 = nn.Linear(128 * 4 * 4, 512)
? ? ? ? # **重要**: 輸出層的大小現在由num_classes決定
? ? ? ? self.fc2 = nn.Linear(512, num_classes)
? ? ? ??
? ? def forward(self, x):
? ? ? ? x = self.pool(F.relu(self.conv1(x))) ?
? ? ? ? x = self.pool(F.relu(self.conv2(x))) ?
? ? ? ? x = self.pool(F.relu(self.conv3(x))) ?
? ? ? ? x = x.view(-1, 128 * 4 * 4)
? ? ? ? x = F.relu(self.fc1(x))
? ? ? ? x = self.fc2(x)
? ? ? ? return x
?
# 初始化模型,傳入你的數據集的類別數量
model = SimpleCNN(num_classes=num_classes)
print("模型已創建")
?
# 如果有GPU則使用GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
?
# 訓練模型函數 (現在使用傳入的trainloader)
def train_model(model, trainloader, epochs=5): # 增加訓練周期以獲得更好效果
? ? criterion = nn.CrossEntropyLoss()
? ? optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
? ??
? ? print("開始訓練...")
? ? for epoch in range(epochs):
? ? ? ? running_loss = 0.0
? ? ? ? for i, data in enumerate(trainloader, 0):
? ? ? ? ? ? inputs, labels = data
? ? ? ? ? ? inputs, labels = inputs.to(device), labels.to(device)
? ? ? ? ? ??
? ? ? ? ? ? optimizer.zero_grad()
? ? ? ? ? ? outputs = model(inputs)
? ? ? ? ? ? loss = criterion(outputs, labels)
? ? ? ? ? ? loss.backward()
? ? ? ? ? ? optimizer.step()
? ? ? ? ? ??
? ? ? ? ? ? running_loss += loss.item()
? ? ? ? ? ? if i % 100 == 99:
? ? ? ? ? ? ? ? print(f'[{epoch + 1}, {i + 1:5d}] 損失: {running_loss / 100:.3f}')
? ? ? ? ? ? ? ? running_loss = 0.0
? ??
? ? print("訓練完成")
?
# 定義模型保存路徑
model_save_path = 'my_custom_cnn.pth'
?
# 嘗試加載預訓練模型
try:
? ? model.load_state_dict(torch.load(model_save_path))
? ? print(f"已從 '{model_save_path}' 加載預訓練模型")
except FileNotFoundError:
? ? print("無法加載預訓練模型,將開始訓練新模型。")
? ? train_model(model, trainloader, epochs=5) # 訓練新模型
? ? torch.save(model.state_dict(), model_save_path) # 保存訓練好的模型
? ? print(f"新模型已訓練并保存至 '{model_save_path}'")
?
# 設置模型為評估模式
model.eval()
?
# Grad-CAM實現 (這部分無需修改)
class GradCAM:
? ? def __init__(self, model, target_layer):
? ? ? ? self.model = model
? ? ? ? self.target_layer = target_layer
? ? ? ? self.gradients = None
? ? ? ? self.activations = None
? ? ? ? self.register_hooks()
? ? ? ??
? ? def register_hooks(self):
? ? ? ? def forward_hook(module, input, output):
? ? ? ? ? ? self.activations = output.detach()
? ? ? ? def backward_hook(module, grad_input, grad_output):
? ? ? ? ? ? self.gradients = grad_output[0].detach()
? ? ? ? self.target_layer.register_forward_hook(forward_hook)
? ? ? ? self.target_layer.register_backward_hook(backward_hook)
? ??
? ? def generate_cam(self, input_image, target_class=None):
? ? ? ? model_output = self.model(input_image)
? ? ? ? if target_class is None:
? ? ? ? ? ? target_class = torch.argmax(model_output, dim=1).item()
? ? ? ??
? ? ? ? self.model.zero_grad()
? ? ? ? one_hot = torch.zeros_like(model_output)
? ? ? ? one_hot[0, target_class] = 1
? ? ? ? model_output.backward(gradient=one_hot, retain_graph=True) # retain_graph=True可能需要
? ? ? ??
? ? ? ? gradients = self.gradients
? ? ? ? activations = self.activations
? ? ? ? weights = torch.mean(gradients, dim=(2, 3), keepdim=True)
? ? ? ? cam = torch.sum(weights * activations, dim=1, keepdim=True)
? ? ? ? cam = F.relu(cam)
? ? ? ??
? ? ? ? cam = F.interpolate(cam, size=(32, 32), mode='bilinear', align_corners=False)
? ? ? ? cam = cam - cam.min()
? ? ? ? cam = cam / cam.max() if cam.max() > 0 else cam
? ? ? ??
? ? ? ? return cam.cpu().squeeze().numpy(), target_class
?
grad_cam = GradCAM(model, model.conv3)
?
# 從測試集中獲取一張圖片
img, label = testset[0]
img_tensor = img.unsqueeze(0).to(device)
?
# 生成CAM
cam, predicted_class_idx = grad_cam.generate_cam(img_tensor)
?
# 可視化結果
def visualize_cam(img, cam, predicted_class, true_class):
? ? img = img.permute(1, 2, 0).numpy() # 轉換回 (H, W, C)
? ? # 反歸一化以便顯示
? ? img = img * 0.5 + 0.5
? ? img = np.clip(img, 0, 1)
?
? ? heatmap = plt.cm.jet(cam)
? ? heatmap = heatmap[:, :, :3] # 去掉alpha通道
?
? ? overlay = heatmap * 0.4 + img * 0.6
? ??
? ? plt.figure(figsize=(10, 5))
? ? plt.subplot(1, 3, 1)
? ? plt.imshow(img)
? ? plt.title(f'Original Image\nTrue: {true_class}')
? ? plt.axis('off')
?
? ? plt.subplot(1, 3, 2)
? ? plt.imshow(heatmap)
? ? plt.title('Grad-CAM Heatmap')
? ? plt.axis('off')
?
? ? plt.subplot(1, 3, 3)
? ? plt.imshow(overlay)
? ? plt.title(f'Overlay\nPredicted: {predicted_class}')
? ? plt.axis('off')
? ??
? ? plt.show()
?
# 顯示結果
predicted_class_name = classes[predicted_class_idx]
true_class_name = classes[label]
visualize_cam(img, cam, predicted_class_name, true_class_name)
?