CNN (pytorch)
1、 简介
对图片(数据集链接) 进行分类,构建 CNN 网络。(还可以直接使用 Restnet)。CNN 架构:

1.1 Pooling
Pooling is the core operation in CNN specifically to designed to reduce the spatial dimentions. Pooling operates on small regions of a feature map and summarizes them with a single value. The most common types are:
- max pooling
- average pooling
2、代码:
模型:
import torch import torch.nn as nn import torch.nn.functional as F class ModelCNNV1(nn.Module): ''' CNN Model V3-V2: 1. 3 convolution & max pool layers 2. 3 fully connected layers 3. Default runtime using 0.2 momentum and dropout value p = 0.5 ''' # Constructor def __init__(self, out_1=32, out_2=64, out_3=128, number_of_classes=10, p=0): super(ModelCNNV1, self).__init__() self.cnn1 = nn.Conv2d(in_channels=3, out_channels=out_1, kernel_size=5, padding=2) self.maxpool1 = nn.MaxPool2d(kernel_size=2) self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=5, padding=2) self.maxpool2 = nn.MaxPool2d(kernel_size=2) self.cnn3 = nn.Conv2d(in_channels=out_2, out_channels=out_3, kernel_size=5, padding=2) self.maxpool3 = nn.MaxPool2d(kernel_size=2) # Hidden layer 1 self.fc1 = nn.Linear(out_3 * 4 * 4, 1000) # 8x8 will change to 4x4 as we added a convolution & max pool layer refer calculation comment above self.drop = nn.Dropout(p=p) # Hidden layer 2 self.fc2 = nn.Linear(1000, 1000) # Final layer self.fc3 = nn.Linear(1000, 10) # Predictiona def forward(self, x): x = self.cnn1(x) x = torch.relu(x) x = self.maxpool1(x) x = self.cnn2(x) x = torch.relu(x) x = self.maxpool2(x) x = self.cnn3(x) x = torch.relu(x) x = self.maxpool3(x) x = x.view(x.size(0), -1) x = self.fc1(x) x = F.relu(self.drop(x)) x = self.fc2(x) x = F.relu(self.drop(x)) x = self.fc3(x) return (x)
训练:
def train_model(model, train_loader, validation_loader, optimizer, mps_device, validation_dataset, criterion, n_epochs=20): # Global variable N_test = len(validation_dataset) accuracy_list = [] model = model.to(mps_device) train_cost_list = [] val_cost_list = [] for epoch in range(n_epochs): train_COST = 0 for x, y in train_loader: x = x.to(mps_device) y = y.to(mps_device) model.train() optimizer.zero_grad() z = model(x) loss = criterion(z, y) loss.backward() optimizer.step() train_COST += loss.item() train_COST = train_COST / len(train_loader) train_cost_list.append(train_COST) correct = 0 # Perform the prediction on the validation data val_COST = 0 for x_test, y_test in validation_loader: model.eval() x_test = x_test.to(mps_device) y_test = y_test.to(mps_device) z = model(x_test) val_loss = criterion(z, y_test) _, yhat = torch.max(z.data, 1) correct += (yhat == y_test).sum().item() val_COST += val_loss.item() val_COST = val_COST / len(validation_loader) val_cost_list.append(val_COST) accuracy = correct / N_test accuracy_list.append(accuracy) print("--> Epoch Number : {}".format(epoch + 1), " | Training Loss : {}".format(round(train_COST, 4)), " | Validation Loss : {}".format(round(val_COST, 4)), " | Validation Accuracy : {}%".format(round(accuracy * 100, 2))) return accuracy_list, train_cost_list, val_cost_list
kaggle 链接
谢谢!

浙公网安备 33010602011771号