fastai最好模板

from pathlib import Path
from fastai import *
from fastai.vision import *
import torch
import os
!pip install efficientnet-pytorch
from efficientnet_pytorch import EfficientNet

# for dirname, _, filenames in os.walk('/kaggle/input'):
#     for filename in filenames:
#         print(os.path.join(dirname, filename))
data_folder = Path("/kaggle/input/54-data/54_data")
train_df = pd.read_csv("/kaggle/input/54-data/54_data/train.csv")
test_df = pd.DataFrame({"filename": [f'{i}.jpg' for i in range(1434)]})
test_img = ImageList.from_df(test_df, path=data_folder, folder='test')
trfm = get_transforms(do_flip=True,
                      max_rotate=20.0, 
                      max_zoom=1.25, 
                      max_lighting=0.2, 
                      max_warp=0.2, 
                      p_affine=0.65, 
                      p_lighting=0.55
                     )

train_img = (ImageList.from_df(train_df, path=data_folder, folder='train')
    .split_by_rand_pct(0.01)
    .label_from_df()
    .add_test(test_img)
    .transform(trfm, size=240)
    .databunch(path='.', bs=64, device= torch.device('cuda:0'))
    .normalize(imagenet_stats)
)
train_img.show_batch(rows=3, figsize=(7,6))
model = EfficientNet.from_pretrained('efficientnet-b1')
model._fc = nn.Linear(1280, train_img.c)
learn = Learner(train_img, model, metrics=[accuracy])
# learn = cnn_learner(train_img, models.densenet201, metrics=[accuracy])
learn.unfreeze()
learn.lr_find()
learn.recorder.plot(suggestion=True)
   # callbacks.EarlyStoppingCallback(learn, min_delta=1e-5, patience=5),
    callbacks.SaveModelCallback(learn)
]

learn.callbacks = callbacks
learn.fit_one_cycle(40, 5e-3)
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(9, figsize=(7,6))
learn.load('bestmodel')
preds, _ = learn.TTA(ds_type=DatasetType.Test)
test_df['labels'] = np.argmax(preds.numpy(), axis=1)
sub = test_df.copy()
sub['ID'] = test_df.index
sub[['ID', 'labels']].to_csv('submission.csv', index=False, header=False)
sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦小狂魔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值