李宏毅 2022机器学习 HW3 boss baseline 上分记录

作业数据是所有数据都有标签的版本。

李宏毅 2022机器学习 HW3 boss baseline 上分记录

    • [1. 训练数据增强, private 0.76056](#1. 训练数据增强, private 0.76056)
    • [2. cross validation&ensemble, private 0.81647](#2. cross validation&ensemble, private 0.81647)
    • [3. test dataset augmentation, private 0.82458](#3. test dataset augmentation, private 0.82458)
    • [4. resnet, private 0.86555](#4. resnet, private 0.86555)
    • [5. Image Normalization, private 0.87494](#5. Image Normalization, private 0.87494)
    • [6. 减小batch_size, private 0.895](#6. 减小batch_size, private 0.895)

1. 训练数据增强, private 0.76056

结论:训练数据增强、更长时间的训练、dropout都证明很有效果,实验效果提升至接近strong baseline

增强1:crop + geometry

增强2:crop + geometry + gray

另外epochs数目增加到100,patience增加到10个epochs,FC层增加 dropout(0.3)

增强代码如下

python 复制代码
#训练数据增强代码
 train_tfm = transforms.Compose([
    # Resize the image into a fixed shape (height = width = 128)
    # transforms.Resize((128, 128)),
    transforms.RandomResizedCrop(size=(128, 128), scale=(0.8, 1)),
    # 几何变换
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomVerticalFlip(p=0.5),
    transforms.RandomRotation(degrees=180),
    transforms.RandomAffine(degrees=30),
    #像素变换
    transforms.RandomGrayscale(p=0.2), 
    # You may add some transforms here.
    # ToTensor() should be the last one of the transforms.
    transforms.ToTensor(),
])

具体实验结果如下:

2. cross validation&ensemble, private 0.81647

使用5-fold cross validation,划分的时候使用分层抽样,

2.1)epochs=100, patience=10

训练时发现通常在60 epochs左右就early stop了,最终public score不如之前,但private score有提升,说明cross validation在过拟合上还是有效果的。

2.2)epochs=100, patience=16 ,再看看效果

patience增大后,效果有了一个非常明显的提升,超过strong baseline 。具体看实验过程,会发现之前patience=10的时候,基本60epochs就停了,而现在patience=100的时候,early stop没有起作用,都是训练满100个epochs。猜测应该是使用5-fold的cross validation时,对比默认的train/valid,一方面训练数据更多,另一方面valid数据变少波动性更大,所以应该给更多的时间训练。

3. test dataset augmentation, private 0.82458

结论:此方式有效,分数进一步提升

测试数据的具体增强方式如下:

在步骤2的基础上,对test数据集使用了train数据集的数据增强方式,生成5张图片预测,对预测结果值平均,然后再用这个结果与原预测结果平均。以下为作业PPT相关部分。

4. resnet, private 0.86555

使用torchvision自带的resnet模型(按照作业要求,pretrained=False),尝试了resnet18和resnet50,效果进一步有了明显提升。public榜上超过bossline,但是从private榜上,可以看出存在一定过拟合。 另外resnet50的效果并没有比resnet18好,可能是小数据集的原因。这里均使用epochs=200,patience=16, lr=0.0003, weight_decay=1e-5。

两个注意点:

1,图片size设成224x224(论文中使用的图片尺寸),对比了128和224,两者差别很大。

2,resnet中的全连接层需要从原来的1000改成此次任务预测的类别数目11,代码如下:

python 复制代码
def model_resnet():
    resnet = resnet18(pretrained=False)
    resnet.fc = nn.Sequential(
        nn.Linear(resnet.fc.in_features, 512),
        nn.ReLU(),
        nn.Dropout(0.3),
        nn.Linear(512, 11)
    )
    
    return resnet

5. Image Normalization, private 0.87494

尝试了image normalization,略微有一些提升,尤其是做了test augmentation的private榜,达到了目前最高分。

image normalization的代码如下,注意train和test需要加上同样的norm。

python 复制代码
height, width = 224, 224

# Normally, We don't need augmentations in testing and validation.
# All we need here is to resize the PIL image and transform it into Tensor.
test_tfm = transforms.Compose([
    # transforms.Resize((128, 128)),
    transforms.Resize((height, width)),
    transforms.ToTensor(),
    transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])

# However, it is also possible to use augmentation in the testing phase.
# You may use train_tfm to produce a variety of images and then test using ensemble methods
train_tfm = transforms.Compose([
    # Resize the image into a fixed shape (height = width = 128)
    # transforms.Resize((128, 128)),
    # transforms.RandomResizedCrop(size=(128, 128), scale=(0.8, 1)),
    transforms.RandomResizedCrop(size=(height, width), scale=(0.8, 1)),
    # 几何变换
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomVerticalFlip(p=0.5),
    transforms.RandomRotation(degrees=180),
    transforms.RandomAffine(degrees=30),
    #像素变换
    transforms.RandomGrayscale(p=0.2), 
    # You may add some transforms here.
    # ToTensor() should be the last one of the transforms.
    transforms.ToTensor(),
    transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])

6. 减小batch_size, private 0.895

将batch_size从64减小到16,模型效果进一步提升,加上image normalization后,private和public双双达到目前最高分。

相关推荐
OpenMiniServer7 小时前
石化能源文明 vs 电气化能源文明
大数据·人工智能·能源
程序员泠零澪回家种桔子7 小时前
OpenManus开源自主规划智能体解析
人工智能·后端·算法
愚公搬代码7 小时前
【愚公系列】《AI短视频创作一本通》006-短视频成败之根本(确定短视频选题的方法和技巧)
人工智能
工具人呵呵7 小时前
[嵌入式AI从0开始到入土]21_基于昇腾310P RC模式的Pi0模型部署实践
人工智能
_张一凡7 小时前
【AIGC面试面经第七期】旋转位置编码RoPE:从 2D 到 nD 的完美扩展之旅
人工智能·面试·aigc
Coding_Doggy7 小时前
服务器迁移 | psql大批量数据以及navicat重新连接、conda 环境迁移
linux·数据库·人工智能
猿小猴子7 小时前
主流 AI IDE 之一的 OpenCode 介绍
ide·人工智能·ai·opencode
一个处女座的程序猿7 小时前
AGI之Multi-Agent之Moltbook:《The Anatomy of the Moltbook Social Graph》翻译与解读
人工智能·microsoft·multi-agent·moltbook
hans汉斯7 小时前
国产生成式人工智能解决物理问题能力研究——以“智谱AI”、“讯飞星火认知大模型”、“天工”、“360智脑”、“文心一言”为例
大数据·人工智能·算法·aigc·文心一言·汉斯出版社·天工
这是个栗子7 小时前
AI辅助编程(一) - ChatGPT
前端·vue.js·人工智能·chatgpt