<jupyter_start><jupyter_text>加载数据<jupyter_code>from fastai.vision import *
root = Config.data_path()/'aerial-cactus-identification'
root
root.as_posix()<jupyter_output><empty_output><jupyter_text>- 下载数据<jupyter_code># !kaggle competitions download -c aerial-cactus-identification -p {root.as_posix()}<jupyter_output><empty_output><jupyter_text>- 解压缩<jupyter_code># !unzip {root/'train.zip'} -d {root}
# !unzip {root/'test.zip'} -d {root}<jupyter_output><empty_output><jupyter_text>- 读入标签<jupyter_code>train_df = pd.read_csv(root/"train.csv")
test_df = pd.read_csv(root/"sample_submission.csv")
train_df.head()
test_df.head()<jupyter_output><empty_output><jupyter_text>- 测试集构造<jupyter_code>test_set = ImageList.from_df(test_df, path=root, cols='id', folder='test')
test_set<jupyter_output><empty_output><jupyter_text>- transforms 定义<jupyter_code>tsfm = get_transforms(do_flip=True, flip_vert=True, max_rotate=10.0, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75)<jupyter_output><empty_output><jupyter_text>- databunch 构造<jupyter_code>np.random.seed(42)
data = (ImageList.from_df(train_df, path=root, cols='id', folder='train')
       .split_by_rand_pct(0.01)
       .label_from_df()
       .transform(tsfm, size=128)
       .databunch(bs=64)
       .normalize(imagenet_stats)
      )
data
data.show_batch(rows=3, figsize=(7,6))<jupyter_output><empty_output><jupyter_text>- 构造模型  使用残差网络<jupyter_code># arch = models.resnet34
arch = models.resnet50
learn = cnn_learner(data, arch, metrics=[error_rate, accuracy])<jupyter_output><empty_output><jupyter_text>- 寻找最佳学习率<jupyter_code>learn.lr_find()
learn.recorder.plot()
lr = 1e-02
learn.fit_one_cycle(5, slice(lr))<jupyter_output><empty_output><jupyter_text>- 第二阶段- 由于第一阶段已经达到1，所以可以不用进行<jupyter_code>learn.unfreeze()<jupyter_output><empty_output><jupyter_text>- 再次寻找学习率<jupyter_code>learn.lr_find(start_lr=1e-8, end_lr=1)
learn.recorder.plot()
lr1 = 1e-05
learn.fit_one_cycle(2, slice(lr))
learn.save('resnet34-stage2')
learn.export()
learn.recorder.plot_losses()
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(9, figsize=(6,6))
interp.plot_confusion_matrix()<jupyter_output><empty_output><jupyter_text>预测并提交结果 - 加载最佳模型<jupyter_code>learn = load_learner(root, test=test_set)
preds,_ = learn.get_preds(ds_type=DatasetType.Test)
preds[:5]
test_df.has_cactus = preds.numpy()[:, 0]
test_df.to_csv(root/'submission.csv', index=False)<jupyter_output><empty_output><jupyter_text>- 提交结果<jupyter_code># ! kaggle competitions submit aerial-cactus-identification -f {path/'submission.csv'} -m "My submission"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1521821631878558, 'fraction_numerical': 0.020113851992409868, 'mean_word_length': 8.799256505576208, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '260275', 'license': 'MIT', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1188, 'n_words': 117, 'path': 'AerialCactusIdentification.ipynb', 'repo_name': 'AutuanLiu/Fastai-Notes-V3', 'size': '2635'}