随便选,模型参数初始化(如:uniform和normal)可以随意调整。
各种超参数(如:epochs、batch_size)可以随意调整。
最终以model.evaluate的精度输出值(格式如下),计算方式是将eval_dataset送入evaluate接口即可,需要在model.prepare中配置评估指标Accuracy,所用数据集不能被用于训练过。
{‘loss’: [6.4980035], ‘acc’: 0.8485721442885772}
代码质量高(注释好、代码逻辑清晰等)会有额外精美礼品相送。
使用resnet50外的模型会有额外精美礼品相送。
导入相关库
import paddle
import numpy as np
paddle.__version__
‘2.0.0’
② 数据准备
import paddle.vision.transforms as T
# 训练数据集
train_dataset = paddle.vision.datasets.Cifar100(mode='train', transform=T.ToTensor())
# 验证数据集
eval_dataset = paddle.vision.datasets.Cifar100(mode='test', transform=T.ToTensor())
print('训练集样本量: {},验证集样本量: {}'.format(len(train_dataset), len(eval_dataset)))
训练集样本量: 50000,验证集样本量: 10000
print('图片:')
print(type(train_dataset[0][0]))
print(train_dataset[0][0])
print('标签:')
print(type(train_dataset[0][1]))
print(train_dataset[0][1])
图片:
Tensor(shape=[3, 32, 32], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
[[[1. , 1. , 1. , ..., 0.76470596, 0.83137262, 0.71372551],
[1. , 0.99607849, 0.99607849, ..., 0.66666669, 0.63137257, 0.57254905],
[1. , 0.99607849, 1. , ..., 0.74117649, 0.65098041, 0.47450984],
...,
[0.58039218, 0.55686277, 0.54901963, ..., 0.11764707, 0.25490198, 0.29803923],
[0.47843140, 0.47058827, 0.49411768, ..., 0.08627451, 0.38039219, 0.55294120],
[0.34117648, 0.34509805, 0.39607847, ..., 0.13333334, 0.41176474, 0.54117650]],
[[1. , 1. , 1. , ..., 0.80392164, 0.87843144, 0.76078439],
[1. , 0.99607849, 0.99607849, ..., 0.69019610, 0.65882355, 0.60392159],
[1. , 0.99607849, 1. , ..., 0.78039223, 0.69803923, 0.52156866],
...,
[0.72549021, 0.71372551, 0.70196080, ..., 0.06666667, 0.24313727, 0.30196080],
[0.61568630, 0.60784316, 0.62745100, ..., 0.06274510, 0.43921572, 0.63137257],
[0.47843140, 0.47843140, 0.52549022, ..., 0.14117648, 0.52156866, 0.67843139]],
[[1. , 1. , 1. , ..., 0.75686282, 0.80000007, 0.65490198],
[1. , 0.99607849, 0.99607849, ..., 0.58823532, 0.50980395, 0.44313729],
[1. , 0.99607849, 1. , ..., 0.66274512, 0.50980395, 0.34117648],
...,
[0.30980393, 0.22352943, 0.23529413, ..., 0.00392157, 0.05882353, 0.07843138],
[0.25882354, 0.22745100, 0.27843139, ..., 0.01176471, 0.21960786, 0.34117648],
[0.16078432, 0.15294118, 0.21960786, ..., 0.03921569, 0.23137257, 0.30980393]]])
>标签:
19
network = paddle.vision.models.resnet18(num_classes=100)
model = paddle.Model(network)
model.summary((-1, 3, 32, 32))
此处应该根据自己的实际情况修改模型
可以参考作业2(图像识别)
resnet50
效果比较好但是不会加分
model.prepare(paddle.optimizer.Adam(learning_rate=0.001, # 学习率
parameters=model.parameters()),
paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy()) # 自己做作业的时候一定要配置这个Accuracy的评估指标,不然没有分数!!!!
model.fit(train_dataset,
eval_dataset,
epochs=20, # 训练轮数
batch_size=64, # 批处理数
verbose=1) # 日志展示形式
评分输出
正确的输出格式为:
{‘loss’: [6.4980035], ‘acc’: 0.8485721442885772}
result = model.evaluate(eval_dataset, verbose=1)
print(result)
大家根据实际情况进行修改,可以修改网络,添加训练轮数,修改学习率,也可以做图片增强……