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
| import numpy as np import pandas as pd import torch import torchvision.transforms as transforms from matplotlib import pyplot as plt import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torchvision.datasets import ImageFolder from torch.utils.data import DataLoader
class CNN_NET(torch.nn.Module): """人马是二分类问题 """
def __init__(self): super(CNN_NET, self).__init__() self.conv1 = torch.nn.Conv2d(in_channels=3, out_channels=128, kernel_size=4, stride=2, padding=1) self.conv1_bn = torch.nn.BatchNorm2d(128) self.conv2 = torch.nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, stride=1, padding=1) self.conv2_bn = torch.nn.BatchNorm2d(64) self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2) self.conv3 = torch.nn.Conv2d(in_channels=64, out_channels=32, kernel_size=3, stride=1, padding=1) self.conv3_bn = torch.nn.BatchNorm2d(32) self.conv4 = torch.nn.Conv2d(in_channels=32, out_channels=16, kernel_size=2, stride=1, padding=1) self.conv4_bn = torch.nn.BatchNorm2d(16) self.fc1 = torch.nn.Linear(9 * 9 * 16, 1024) self.fc2 = torch.nn.Linear(1024, 512) self.fc3 = torch.nn.Linear(512, 256) self.fc4 = torch.nn.Linear(256, 128) self.fc5 = torch.nn.Linear(128, 64) self.fc6 = torch.nn.Linear(64, 2) self.dropout = torch.nn.Dropout(0.5)
def forward(self, x): x = self.pool(F.relu(self.conv1_bn(self.conv1(x)))) x = self.pool(F.relu(self.conv2_bn(self.conv2(x)))) x = self.pool(F.relu(self.conv3_bn(self.conv3(x)))) x = self.pool(F.relu(self.conv4_bn(self.conv4(x)))) x = x.view(-1, 9 * 9 * 16) x = F.relu(self.fc1(x)) x = self.dropout(x) x = F.relu(self.fc2(x)) x = self.dropout(x) x = F.relu(self.fc3(x)) x = self.dropout(x) x = F.relu(self.fc4(x)) x = self.dropout(x) x = F.relu(self.fc5(x)) x = self.dropout(x) x = self.fc6(x) return x
if __name__ == '__main__': device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(device) EPOCH = 8 transform = transforms.Compose([ transforms.Resize((300, 300)), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])
train_dataset = ImageFolder(root='data/horse-or-human/horse-or-human', transform=transform) test_dataset = ImageFolder(root='data/horse-or-human/validation-horse-or-human', transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
net = CNN_NET().to(device)
criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=0.001) best_accuracy = 0.0 best_model_path = 'best_model.pth'
losses = [] accuracies = [] for epoch in range(EPOCH): running_loss = 0.0 correct = 0 total = 0 for i, data in enumerate(train_loader, 0): inputs, labels = data inputs, labels = inputs.to(device), labels.to(device) optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() net.eval() with torch.no_grad(): correct_val = 0 total_val = 0 for data in test_loader: images, labels = data images, labels = images.to(device), labels.to(device) outputs = net(images) _, predicted = torch.max(outputs.data, 1) total_val += labels.size(0) correct_val += (predicted == labels).sum().item() val_accuracy = correct_val / total_val if val_accuracy > best_accuracy: torch.save(net.state_dict(), best_model_path) best_accuracy = val_accuracy net.train() print(f'Epoch [{epoch + 1}/{EPOCH}], Loss: {running_loss / len(train_loader):.4f}, Accuracy: {100 * correct / total:.2f}%, Validation Accuracy: {100 * correct_val / total_val:.2f}%')
print('Finished Training')
fig, ax1 = plt.subplots()
color = 'tab:red' ax1.set_xlabel('Iterations') ax1.set_ylabel('Loss', color=color) ax1.plot(losses, color=color) ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() color = 'tab:blue' ax2.set_ylabel('Accuracy (%)', color=color) ax2.plot(accuracies, color=color) ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() plt.title('Training Loss and Accuracy') plt.show()
best_model = CNN_NET().to(device) best_model.load_state_dict(torch.load(best_model_path)) best_model.eval() correct = 0 total = 0 with torch.no_grad(): for data in test_loader: images, labels = data images, labels = images.to(device), labels.to(device) outputs = best_model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item()
print('Accuracy of the best model on the test images: %d %%' % (100 * correct / total))
|