使用kaggle训练动手学深度学习第二次比赛(classify-leaves)
1、比赛平台
kaggle上免费GPU的配置(Tesla P100-PCIE+16GB显存),每周免费35h,平常使用不需要VPN,上传文件需要VPN.
2、baseline代码分析
整体步骤:
- 数据集切分:训练数据集,验证数据集,测试数据集,176类树叶
- 加载数据集
- 模型训练:ResNet34预训练模型
- 模型验证
- 结果预测
# -*- coding: UTF-8 -*-
'''
@,@Description: ,:
@,@version: ,:
@,@Author: ,: sunshine
@,@Github: ,: https://subshine.github.io/
@,@Email: ,: 2182216077@ncepu.edu.cn
@,@Date: ,: 2021-06-19 10:52:03
@,@LastEditTime: ,: 2021-07-06 16:16:14
'''
import torch
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns #基于matplotlib进行高级封装的可视化库 pip3 install seaborn
import numpy as np
from PIL import Image
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
import torchvision.models as models
import torch.nn as nn
from tqdm import tqdm
from numpy import *
# ## 读取数据
train_csv = pd.read_csv("../learn_deep_learning/CNN/classify-leaves/train.csv")
pred_csv = pd.read_csv("../learn_deep_learning/CNN/classify-leaves/test.csv")
leaves_label = list(set(train_csv['label'])) # 176类
# 查看文件中的前五行
# print(train_csv.head(5))
# # 查看文件详细描述(包括图像个数以及标签的类数)
# print(train_csv.describe())
# 横向柱状图显示
def barw(ax):
for p in ax.patches:
val = p.get_width() # 柱状图的高度即最多种类的数量
x = p.get_x() + p.get_width()
y = p.get_y() + p.get_height()
# print(round(val, 2))
# 第一个参数是数据点添加注释的内容
# 第二个参数是添加注释的数据点的位置
ax.annotate(round(val, 2), (x, y))
plt.figure(figsize=(30, 160))
ax0 = sns.countplot(y=train_csv['label'], order=train_csv['label'].value_counts().index)
barw(ax0)
# plt.show() # 图像显示
plt.savefig('ax0.svg') #保存图片
train_label = sorted(list(set(train_csv['label'])))
# print(train_label)
num_label = len(train_label)
print(train_label[:10])
# 把label转换成数字
num_label_to_num = dict(zip