网站建设的英语,济南网站搭建公司,网站建设 功能需求,资源下载wordpressmobileNet具体细节#xff0c;在前面已做了分析记录#xff1a;轻量化网络-MobileNet系列-CSDN博客
这里是根据网络结构#xff0c;搭建模型#xff0c;用于图像分类任务。
1. 网络结构和基本组件 2. 搭建组件
#xff08;1#xff09;普通的卷积组件#xff1a;CBL …mobileNet具体细节在前面已做了分析记录轻量化网络-MobileNet系列-CSDN博客
这里是根据网络结构搭建模型用于图像分类任务。
1. 网络结构和基本组件 2. 搭建组件
1普通的卷积组件CBL Conv2d BN ReLU6;
2深度可分离卷积DwCBL Conv dw Conv dp
Conv dw Conv dp {Conv2d(3x3) BN ReLU6 } {Conv2d(1x1) BN ReLU6};
Conv dw是3x3的深度卷积通过步长控制是否进行下采样
Conv dp是1x1的逐点卷积通过控制输出通道数控制通道维度的变化
# 普通卷积
class CBN(nn.Module):def __init__(self, in_c, out_c, stride1):super(CBN, self).__init__()self.conv nn.Conv2d(in_c, out_c, 3, stride, padding1, biasFalse)self.bn nn.BatchNorm2d(out_c)self.relu nn.ReLU6(inplaceTrue)def forward(self, x):x self.conv(x)x self.bn(x)x self.relu(x)return x
# 深度可分离卷积: 深度卷积(3x3x1) 逐点卷积1x1xc卷积)
class DwCBN(nn.Module):def __init__(self, in_c, out_c, stride1):super(DwCBN, self).__init__()# conv3x3x1, 深度卷积通过步长只控制是否缩小特征hwself.conv3x3 nn.Conv2d(in_c, in_c, 3, stride, padding1, groupsin_c, biasFalse)self.bn1 nn.BatchNorm2d(in_c)self.relu1 nn.ReLU6(inplaceTrue)# conv1x1xc, 逐点卷积通过控制输出通道数控制通道维度的变化self.conv1x1 nn.Conv2d(in_c, out_c, 1, stride1, padding0, biasFalse)self.bn2 nn.BatchNorm2d(out_c)self.relu2 nn.ReLU6(inplaceTrue)def forward(self, x):x self.conv3x3(x)x self.bn1(x)x self.relu1(x)x self.conv1x1(x)x self.bn2(x)x self.relu2(x)return x
3. 搭建网络
class MobileNetV1(nn.Module):def __init__(self, class_num1000):super(MobileNetV1, self).__init__()self.stage1 torch.nn.Sequential(CBN(3, 32, 2), # 下采样/2DwCBN(32, 64, 1))self.stage2 torch.nn.Sequential(DwCBN(64, 128, 2), # 下采样/4DwCBN(128, 128, 1))self.stage3 torch.nn.Sequential(DwCBN(128, 256, 2), # 下采样/8DwCBN(256, 256, 1))self.stage4 torch.nn.Sequential(DwCBN(256, 512, 2), # 下采样/16DwCBN(512, 512, 1), # 5个DwCBN(512, 512, 1),DwCBN(512, 512, 1),DwCBN(512, 512, 1),DwCBN(512, 512, 1),)self.stage5 torch.nn.Sequential(DwCBN(512, 1024, 2), # 下采样/32DwCBN(1024, 1024, 1))# classifierself.avg_pooling torch.nn.AdaptiveAvgPool2d((1, 1))self.fc torch.nn.Linear(1024, class_num, biasTrue)# self.classifier torch.nn.Softmax() # 原始的softmax值# torch.log_softmax 首先计算 softmax 然后再取对数因此在数值上更加稳定。# 在分类网络在训练过程中通常使用交叉熵损失函数Cross-Entropy Loss。# torch.nn.CrossEntropyLoss 会在内部进行 softmax 操作因此在网络的最后一层不需要手动加上 softmax 操作。def forward(self, x):scale1 self.stage1(x) # /2scale2 self.stage2(scale1)scale3 self.stage3(scale2)scale4 self.stage4(scale3)scale5 self.stage5(scale4) # /32. 7x7x self.avg_pooling(scale5) # (b,1024,7,7)-(b,1024,1,1)x torch.flatten(x, 1) # (b,1024,1,1)-(b,1024,)x self.fc(x) # (b,1024,) - (b,1000,)return xif __name__ __main__:m1 MobileNetV1(class_num1000)input_data torch.randn(64, 3, 224, 224)output m1.forward(input_data)print(output.shape)
4. 训练验证
import torch
import torchvision
import torchvision.transforms as transforms
from torch import nn, optimfrom mobilenetv1 import MobileNetV1def validate(model, val_loader, criterion, device):model.eval() # Set the model to evaluation modetotal_correct 0total_samples 0with torch.no_grad():for val_inputs, val_labels in val_loader:val_inputs, val_labels val_inputs.to(device), val_labels.to(device)val_outputs model(val_inputs)_, predicted torch.max(val_outputs, 1)total_samples val_labels.size(0)total_correct (predicted val_labels).sum().item()accuracy total_correct / total_samplesmodel.train() # Set the model back to training modereturn accuracyif __name__ __main__:# 下载并准备数据集# Define image transformations (adjust as needed)transform transforms.Compose([transforms.Resize((224, 224)), # Resize images to a consistent sizetransforms.ToTensor(), # converts to PIL Image to a Pytorch Tensor and scales values to the range [0, 1]transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), # Adjust normalization values. val (val - mean) / std.])# Create ImageFolder datasetdata_folder rD:\zxq\data\car_or_dogdataset torchvision.datasets.ImageFolder(rootdata_folder, transformtransform)# Optionally, split the dataset into training and validation sets# Adjust the split_ratio as neededsplit_ratio 0.8train_size int(split_ratio * len(dataset))val_size len(dataset) - train_sizetrain_dataset, val_dataset torch.utils.data.random_split(dataset, [train_size, val_size])# Create DataLoader for training and validationtrain_loader torch.utils.data.DataLoader(train_dataset, batch_size4, shuffleTrue, num_workers4)val_loader torch.utils.data.DataLoader(val_dataset, batch_size4, shuffleFalse, num_workers4)# 初始化模型、损失函数和优化器net MobileNetV1(class_num2)criterion nn.CrossEntropyLoss()optimizer optim.SGD(net.parameters(), lr0.01, momentum0.9)# 训练模型device torch.device(cuda:0 if torch.cuda.is_available() else cpu)print(device)net.to(device)for epoch in range(20): # 例如训练 20 个周期for i, data in enumerate(train_loader, 0):inputs, labels datainputs, labels inputs.to(device), labels.to(device) # 将数据移动到GPUoptimizer.zero_grad()outputs net(inputs)loss criterion(outputs, labels)loss.backward()optimizer.step()if i % 100 0:print(epoch/step: {}/{}: loss: {}.format(epoch, i, loss.item()))# Validation after each epochval_accuracy validate(net, val_loader, criterion, device)print(Epoch {} - Validation Accuracy: {:.2%}.format(epoch, val_accuracy))print(Finished Training)
待续。。。