宠物分辨使用resnet-34迁移学习

#在执行代码时自动载入需要的模块
%reload_ext autoreload
%autoreload 2
%matplotlib inline 
# 将matplotlib的图表直接嵌入到Notebook
from fastai.vision import * # 导入包
from fastai.metrics import error_rate # 导入评价指标
path = untar_data(URLs.PETS) #给出文件路径
WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet')
path.ls()#查看该路径下所有的文件
[WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/annotations'),
 WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/crappy'),
 WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/images')]
path_img = path/'images'
fnames = get_image_files(path_img)#得到图片
fnames[:5]
[WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/images/Abyssinian_1.jpg'),
 WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/images/Abyssinian_10.jpg'),
 WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/images/Abyssinian_100.jpg'),
 WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/images/Abyssinian_101.jpg'),
 WindowsPath('C:/Users/Wither8848/.fastai/data/oxford-iiit-pet/images/Abyssinian_102.jpg')]
np.random.seed(2)
pat = r'/([^/]+)_\d+.jpg$'#正则化表达式
bs=64
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=bs).normalize(imagenet_stats)
data.show_batch(rows=4, figsize=(7,7))
print(data.classes)#类别名字
data.c#类别总数
['Abyssinian', 'Bengal', 'Birman', 'Bombay', 'British_Shorthair', 'Egyptian_Mau', 'Maine_Coon', 'Persian', 'Ragdoll', 'Russian_Blue', 'Siamese', 'Sphynx', 'american_bulldog', 'american_pit_bull_terrier', 'basset_hound', 'beagle', 'boxer', 'chihuahua', 'english_cocker_spaniel', 'english_setter', 'german_shorthaired', 'great_pyrenees', 'havanese', 'japanese_chin', 'keeshond', 'leonberger', 'miniature_pinscher', 'newfoundland', 'pomeranian', 'pug', 'saint_bernard', 'samoyed', 'scottish_terrier', 'shiba_inu', 'staffordshire_bull_terrier', 'wheaten_terrier', 'yorkshire_terrier']





37
'''迁移学习'''
learn = cnn_learner(data,models.resnet34,metrics = error_rate)
learn.model
learn.fit_one_cycle(4)
learn.save('stage')
'''查看错误分类的结果'''
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(9,figsize=(15,11))#查看损失最高的图片
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)#查看混淆矩阵
interp.most_confused(min_val=2)#查看分错的类别
[('staffordshire_bull_terrier', 'american_pit_bull_terrier', 6),
 ('Abyssinian', 'Bengal', 5),
 ('Maine_Coon', 'Ragdoll', 5),
 ('chihuahua', 'miniature_pinscher', 5),
 ('Birman', 'Ragdoll', 4),
 ('British_Shorthair', 'Russian_Blue', 4),
 ('Ragdoll', 'Birman', 4),
 ('Egyptian_Mau', 'Bengal', 3),
 ('Russian_Blue', 'Bombay', 3),
 ('american_bulldog', 'staffordshire_bull_terrier', 3),
 ('english_cocker_spaniel', 'english_setter', 3),
 ('Abyssinian', 'Sphynx', 2),
 ('Bengal', 'Egyptian_Mau', 2),
 ('Birman', 'Siamese', 2),
 ('British_Shorthair', 'Bombay', 2),
 ('Maine_Coon', 'Bengal', 2),
 ('Maine_Coon', 'Persian', 2),
 ('Ragdoll', 'Persian', 2),
 ('Russian_Blue', 'Abyssinian', 2),
 ('Siamese', 'Birman', 2),
 ('Sphynx', 'chihuahua', 2),
 ('american_bulldog', 'boxer', 2),
 ('american_pit_bull_terrier', 'american_bulldog', 2),
 ('american_pit_bull_terrier', 'boxer', 2),
 ('american_pit_bull_terrier', 'staffordshire_bull_terrier', 2),
 ('beagle', 'basset_hound', 2),
 ('boxer', 'american_bulldog', 2),
 ('boxer', 'saint_bernard', 2),
 ('chihuahua', 'american_pit_bull_terrier', 2),
 ('german_shorthaired', 'american_pit_bull_terrier', 2),
 ('german_shorthaired', 'staffordshire_bull_terrier', 2),
 ('miniature_pinscher', 'american_pit_bull_terrier', 2),
 ('saint_bernard', 'boxer', 2),
 ('staffordshire_bull_terrier', 'american_bulldog', 2)]
'''参数优化'''
learn.unfreeze()
learn.fit_one_cycle(1)#结果并没有变好,因为学习率太大
epoch     train_loss  valid_loss  error_rate  time    
0         0.685388    0.372840    0.117727    01:17     
learn.lr_find()
learn.recorder.plot()#找出学习率
epoch     train_loss  valid_loss  error_rate  time    
0         1.473476    #na#        00:43       
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.


F:\software\programming_software\Anaconda3\lib\site-packages\fastai\sixel.py:16: UserWarning: You could see this plot with `libsixel`. See https://github.com/saitoha/libsixel
  warn("You could see this plot with `libsixel`. See https://github.com/saitoha/libsixel")
learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
epoch     train_loss  valid_loss  error_rate  time    
0         0.379295    0.312089    0.096752    01:17     
1         0.327472    0.293061    0.094723    01:16     
learn.save('stage-2')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是使用MindSpore实现ResNet-34的步骤: 1. 导入必要的库 ```python import mindspore.nn as nn from mindspore import Tensor from mindspore.ops import operations as P ``` 2. 定义基本的卷积块 ```python class ConvBlock(nn.Cell): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0): super(ConvBlock, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, has_bias=False) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU() def construct(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x ``` 3. 定义ResNet-34的基本块 ```python class BasicBlock(nn.Cell): expansion = 1 def __init__(self, in_channels, out_channels, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = ConvBlock(in_channels, out_channels, kernel_size=3, stride=stride, padding=1) self.conv2 = ConvBlock(out_channels, out_channels, kernel_size=3, stride=1, padding=1) self.downsample = downsample self.stride = stride def construct(self, x): identity = x out = self.conv1(x) out = self.conv2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = nn.ReLU()(out) return out ``` 4. 定义ResNet-34的主体部分 ```python class ResNet34(nn.Cell): def __init__(self, num_classes=1000): super(ResNet34, self).__init__() self.in_channels = 64 self.conv1 = ConvBlock(3, 64, kernel_size=7, stride=2, padding=3) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(BasicBlock, 64, 3, stride=1) self.layer2 = self._make_layer(BasicBlock, 128, 4, stride=2) self.layer3 = self._make_layer(BasicBlock, 256, 6, stride=2) self.layer4 = self._make_layer(BasicBlock, 512, 3, stride=2) self.avgpool = nn.AvgPool2d(kernel_size=7, stride=1) self.fc = nn.Dense(512 * BasicBlock.expansion, num_classes) def _make_layer(self, block, out_channels, num_blocks, stride): downsample = None if stride != 1 or self.in_channels != out_channels * block.expansion: downsample = nn.SequentialCell([ nn.Conv2d(self.in_channels, out_channels * block.expansion, kernel_size=1, stride=stride, has_bias=False), nn.BatchNorm2d(out_channels * block.expansion) ]) layers = [] layers.append(block(self.in_channels, out_channels, stride, downsample)) self.in_channels = out_channels * block.expansion for i in range(1, num_blocks): layers.append(block(self.in_channels, out_channels)) return nn.SequentialCell(layers) def construct(self, x): x = self.conv1(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 = P.Reshape()(x, (x.shape[0], -1)) x = self.fc(x) return x ``` 5. 加载数据和训练模型 这里的数据加载和训练模型的部分可以根据具体的数据集和训练需求进行编写。 以上就是使用MindSpore实现ResNet-34的基本步骤,你可以根据自己的需要进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值