教程 | 图像分类: Caltech 256 ​数据集

11 篇文章 1 订阅
2 篇文章 0 订阅

Caltech 256是什么?

Caltech 256数据集是加利福尼亚理工学院收集整理的数据集,该数据集选自Google Image 数据集,并手工去除了不符合其类别的图片。在该数据集中,图片被分为256类,每个类别的图片超过80张。

 

为什么要用Densenet121模型?

本项目使用在PyTorch框架下搭建的神经网络来完成图片分类的任务。由于网络输出的类别数量很大,简单的网络模型无法达到很好的分类效果,因此,本项目使用了预训练的Densenet121模型,并仅训练全连接层的参数。

 

项目流程

1.数据处理

2.Densenet模型解读

3.加载预训练网络模型

4.训练神经网络

 

1、数据处理

首先从指定路径读取图像,将图像大小更改为224*224,并将图片范围从0-255改为0-1:

from PIL import Image
image= Image.open(path)
image=image.resize((224,224))
x_data= x_data.astype(numpy.float32)
x_data= numpy.multiply(x_data,1.0/255.0)  
## scale to [0,1] from [0,255]

由于此数据集中有少量图片的色彩是单通道的,而神经网络的输入需要为三个通道,因此,将该通道的数据复制到三个通道上:

if len(x_data.shape)!=3:
temp=numpy.zeros
((x_data.shape[0],x_data.shape[1],3))
temp[:,:,0] = x_data
temp[:,:,1] = x_data
temp[:,:,2] = x_data
x_data= temp
x_data=numpy.transpose(x_data,(2,0,1)) 
## reshape 

在上述步骤之后,对图片进行白化,即让像素点的平均值为0,方差为1。这样做是为了减小图片的范围,使得图片的特征更易于学习。白化的过程如下所示:

if x_train is not None:
  x_train[:,0,:,:] = (x_train[:,0,:,:]-0.485)/0.229
  x_train[:,1,:,:] = (x_train[:,1,:,:]-0.456)/0.224
  x_train[:,2,:,:] = (x_train[:,2,:,:]-0.406)/0.225

if x_test is not None:
 x_test[:,0,:,:] = (x_test[:,0,:,:] -0.485) /0.229
 x_test[:,1,:,:] = (x_test[:,1,:,:] -0.456) /0.224
 x_test[:,2,:,:] = (x_test[:,2,:,:] -0.406) /0.225

2、DenseNet模型解读

DenseNet的网络结构如下图所示。在传统的CNN中,每个卷积层只与其相邻的卷积层相连接,这就造成了位于网络浅层的参数在反向传播中获取的梯度非常小,也就是梯度消失问题。

 

2018-12-27_144254.png

 

DenseNet设计了名为Dense Block的特殊的网络结构,在一个Dense Block中,每个层的输入为前面所有层的输出,这也正是Dense的含义。通过这种方法,在反向传播中,网络浅层的参数可以从后面所有层中获得梯度,在很大程度上减弱了梯度消失的问题。值得注意的是,每个层只与同位于一个Dense Block中的多个层有连接,而与Dense Block外的层是没有连接的。

 

3、加载预训练网络模型

torchvision是服务于PyTorch框架的,用于进行图片处理和生成一些主流模型的库。使用该库可以方便的加载PyTorch的预训练模型。首先使用pip安装torchvision库:

pip install torchvision

创建densenet121模型实例,并加载预训练参数:

cnn = torchvision.models.densenet121
(pretrained = True) #pretrained =True即为加载预训练参数,默认不加载。

冻结所有模型参数,使其值在反向传播中不改变:

for param in cnn.parameters():
    param.requires_grad= False

改变模型全连接层输出的个数为256:

num_features= cnn.classifier.in_features
cnn.classifier=nn.Linear(num_features, 256)

此处不需要担心新建的全连接层参数会被冻结,因为新建的层参数是默认获取梯度的。

 

4、训练神经网络

损失函数选择CrossEntropy,优化器选择Adam:

optimizer= Adam(cnn.parameters(),lr=0.001, betas=(0.9, 0.999))#选用AdamOptimizer
loss_fn= nn.CrossEntropyLoss()#定义损失函数

下面是完整的训练过程:

# 训练并评估模型
data= Dataset()
model= Model(data)

best_accuracy= 0
for i in range(args.EPOCHS):
   cnn.train()
   x_train, y_train, x_test, y_test= data.next_batch(args.BATCH)  # 读取数据

   x_train= torch.from_numpy(x_train)
   y_train= torch.from_numpy(y_train)
   x_train= x_train.float()

   x_test= torch.from_numpy(x_test)
   y_test= torch.from_numpy(y_test)
   x_test= x_test.float()
   
   if cuda_avail:
       x_train= Variable(x_train.cuda())
       y_train= Variable(y_train.cuda())
       x_test= Variable(x_test.cuda())
       y_test= Variable(y_test.cuda())
       
   outputs= cnn(x_train)
   _, prediction= torch.max(outputs.data, 1)
   
   optimizer.zero_grad()

# calculate the loss according to labels
   loss= loss_fn(outputs, y_train)
# backward transmit loss
   loss.backward()

# adjust parameters using Adam
   optimizer.step()

# 若测试准确率高于当前最高准确率,则保存模型
   train_accuracy= eval(model, x_test, y_test)
   if  train_accuracy>best_accuracy:
       best_accuracy= train_accuracy
       model.save_model(cnn, MODEL_PATH, overwrite=True)
    ​   print("step %d, best accuracy %g"%(i, best_accuracy))

   print(str(i) +"/"+str(args.EPOCHS))

总结:

本文主要讲解了DenseNet的网络结构,以及在PyTorch框架下如何加载预训练模型并进行fine-tuning。为了在数据集上获得更高的准确率,读者可尝试取消冻结参数的设置,使得卷积层也参与训练。

获取项目请访问:https://www.flyai.com/d/Caltech256

  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用PyTorch实现的ResNet图像分类模型,使用caltech256数据集进行训练和测试。 ```python import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision.transforms as transforms from torchvision.datasets import ImageFolder from torch.utils.data import DataLoader # 定义ResNet模型 class ResNet(nn.Module): def __init__(self, num_classes=256): super(ResNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = nn.Sequential( nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(64), nn.ReLU(inplace=True), nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(64), nn.ReLU(inplace=True) ) self.layer2 = nn.Sequential( nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False), nn.BatchNorm2d(128), nn.ReLU(inplace=True), nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(128), nn.ReLU(inplace=True) ) self.layer3 = nn.Sequential( nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1, bias=False), nn.BatchNorm2d(256), nn.ReLU(inplace=True), nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(256), nn.ReLU(inplace=True) ) self.layer4 = nn.Sequential( nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1, bias=False), nn.BatchNorm2d(512), nn.ReLU(inplace=True), nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(512), nn.ReLU(inplace=True) ) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(512, num_classes) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.fc(x) return x # 数据预处理 transform_train = transforms.Compose([ transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) transform_test = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # 加载数据集 train_dataset = ImageFolder(root='caltech256/train', transform=transform_train) train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=4) test_dataset = ImageFolder(root='caltech256/test', transform=transform_test) test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False, num_workers=4) # 定义模型、损失函数、优化器 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = ResNet().to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4) # 训练模型 for epoch in range(50): running_loss = 0.0 for i, (inputs, labels) in enumerate(train_loader): 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 % 20 == 19: print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 20)) running_loss = 0.0 # 测试模型 correct = 0 total = 0 with torch.no_grad(): for inputs, labels in test_loader: inputs, labels = inputs.to(device), labels.to(device) outputs = model(inputs) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy on the test images: %.2f %%' % (100 * correct / total)) ``` 在上面的代码中,我们定义了一个ResNet模型,使用了caltech256数据集进行训练和测试。数据集被预处理为224x224大小的图像,并进行了数据增强(随机裁剪和水平翻转)。我们使用了随机梯度下降(SGD)优化器,学习率为0.01,动量为0.9。在训练过程中,我们打印了每个epoch的损失值。在测试过程中,我们计算了模型在测试集上的准确率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值