2.fastAI进阶(1)(DataLoader)

FASTAI

1.进阶dataloader

1.1 示例

pets = DataBlock(blocks=(ImageBlock, CategoryBlock), #用到的类型,图片、分类
                 get_items=get_image_files, #获取x的方式
                 splitter=RandomSplitter(), #分割方式
                 get_y=using_attr(RegexLabeller(r'(.+)_\d+.jpg$'), 'name'),	#获取y标签的方法
                 item_tfms=Resize(460),
                 batch_tfms=aug_transforms(size=224))

A datablock is built by giving the fastai library a bunch of informations:

  • the types used, through an argument called blocks: here we have images and categories, so we pass ImageBlock and CategoryBlock.
  • how to get the raw items, here our function get_image_files.
  • how to label those items, here with the same regular expression as before.
  • how to split those items, here with a random splitter.
  • the item_tfms and batch_tfms like before.

1.2 通过csv格式文件 进行标签

例子使用了多分类的数据集,标签在train.csv中

在这里插入图片描述

dls = ImageDataLoaders.from_df(df, path, folder=‘train’, valid_col=‘is_valid’, label_delim=' ', item_tfms=Resize(460), batch_tfms=aug_transforms(size=224))

  1. 使用from_df函数来加载csv的数据集。

  2. path为基目录,folder 用于在path和filename之间增加路径path/folder/filename

  3. label_delim 指定使用空格作为标签分类条件

  4. 文件名、标签默认为第一二列所以这里不需要指定。

进阶:

pascal = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),
                   splitter=ColSplitter('is_valid'),
                   get_x=ColReader('fname', pref=str(path/'train') + os.path.sep),
                   get_y=ColReader('labels', label_delim=' '),
                   item_tfms = Resize(460),
                   batch_tfms=aug_transforms(size=224))

多分类使用F1 Score来评价模型的好坏

f1_macro = F1ScoreMulti(thresh=0.5, average='macro')
f1_macro.name = 'F1(macro)'
f1_samples = F1ScoreMulti(thresh=0.5, average='samples')
f1_samples.name = 'F1(samples)'
learn = vision_learner(dls, resnet50, metrics=[partial(accuracy_multi, thresh=0.5), f1_macro, f1_samples])

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值