飞百度架构师手把手带你零基础实践深度学习——21日学习打卡(第二周第四日)
首先声明,不详细讲解代码部分,主要是针对课程的理解及对作业的分析。(要是有代码相关问题可以私信)
课程链接:飞百度架构师手把手带你零基础实践深度学习
前文:
飞桨PaddlePaddle-百度架构师手把手带你零基础实践深度学习——21日学习打卡(第二周第三日)
飞桨PaddlePaddle-百度架构师手把手带你零基础实践深度学习——21日学习打卡(第二周第二日)
飞桨PaddlePaddle-百度架构师手把手带你零基础实践深度学习——21日学习打卡(第二周第一日)
该课程分类专栏:深度学习课程——飞桨paddlepaddle
在这里插入图片描述
回顾一下昨天学的知识,昨天孙老师详细地讲了四个经典的神经网络分类模型(AlexNet、VGG、GoogLeNet、Resnet),并对图像分类领域的经典卷积神经网络进行剖析,介绍如何应用这些基础模块构建卷积神经网络,解决图像分类问题。
今天孙老师讲的是用YoloV3进行目标检测(虽然现在一般用的都是YoloV4、YoloV5了,但是里可能是便于我们理解,这个课程就讲了YoloV3,不过没有关系,只不过是函数接口方面有一些改动,看官方那个文档就可以转化了),介绍了目标检测的作用(目标检测的主要目的是让计算机可以自动识别图片或者视频帧中所有目标的类别,并在该目标周围绘制边界框,标示出每个目标的位置),然后介绍了目标检测中的基础概念(我们会经常用到哦,现在记不住,慢慢也会记住的),简单说了下AI识虫数据集的数据结构和调用等等(了解就好,到时候搬用),然后就是图像的预处理—图像增强,最后就是YoloV3模型的算法简介(这部分后半截是重点,我们要多看几遍,暂时看不懂也不要担心,我们之后会经常用到,到时候用着用着就熟练了),今天也就是让我们了解一下,有个准备,明天应该就是重头戏了哦。

第二周(实践)大作业

  • 本着给大家解惑的想法,给出答案解析,大家放心,第一个满分一定是我这篇博文发出之前的哦~
  • 代码太多就不都打出来了,只有这一部分代码改了,我写在下面(有问题就私信)
  • 我用的是分段式衰减,其他衰减可看API文档(哪不明白也可以私信我哦)
  • boundaries 参数和epoch_num 参数自己调吧,奥利给(我炼丹已经困死了,睡了)
  • 我只是给个简单的例子!!!具体参数的选择是大家做的哦
  • 想要用其他衰减方式的有任何问题都可以私信我哦~
# -*- coding: utf-8 -*-
import os
import random
import paddle
import paddle.fluid as fluid
import numpy as np
from visualdl import LogWriter

DATADIR = '/home/aistudio/work/palm/PALM-Training400/PALM-Training400'
DATADIR2 = '/home/aistudio/work/palm/PALM-Validation400'
CSVFILE = '/home/aistudio/labels.csv'
# 定义训练过程
def train(model):
    
    with fluid.dygraph.guard():
        print('start training ... ')
        model.train()
        iter = 0
        boundaries = [2, 5]
        values = [0.1, 0.01, 0.001]    
        epoch_num = 10
        # 定义优化器
        opt = fluid.optimizer.Momentum(learning_rate=fluid.layers.piecewise_decay(boundaries=boundaries, values=values), momentum=0.9, parameter_list=model.parameters())
        # 定义数据读取器,训练数据读取器和验证数据读取器
        train_loader = data_loader(DATADIR, batch_size=10, mode='train')
        valid_loader = valid_data_loader(DATADIR2, CSVFILE)
        for epoch in range(epoch_num):
            for batch_id, data in enumerate(train_loader()):
                x_data, y_data = data
                img = fluid.dygraph.to_variable(x_data)
                label = fluid.dygraph.to_variable(y_data)
                # 运行模型前向计算,得到预测值
                logits = model(img)
                # 进行loss计算
                loss = fluid.layers.sigmoid_cross_entropy_with_logits(logits, label)
                avg_loss = fluid.layers.mean(loss)
                if batch_id % 10 == 0:
                    print("epoch: {}, batch_id: {}, loss is: {}".format(epoch, batch_id, avg_loss.numpy()))
                    writer.add_scalar(tag = 'loss', step = iter, value = avg_loss.numpy())
                    iter = iter + 20
                # 反向传播,更新权重,清除梯度
                avg_loss.backward()
                opt.minimize(avg_loss)
                model.clear_gradients()
            model.eval()
            accuracies = []
            losses = []
            for batch_id, data in enumerate(valid_loader()):
                x_data, y_data = data
                img = fluid.dygraph.to_variable(x_data)
                label = fluid.dygraph.to_variable(y_data)
                # 运行模型前向计算,得到预测值
                logits = model(img)
                # 二分类,sigmoid计算后的结果以0.5为阈值分两个类别
                # 计算sigmoid后的预测概率,进行loss计算
                pred = fluid.layers.sigmoid(logits)
                loss = fluid.layers.sigmoid_cross_entropy_with_logits(logits, label)
                # 计算预测概率小于0.5的类别
                pred2 = pred * (-1.0) + 1.0
                # 得到两个类别的预测概率,并沿第一个维度级联
                pred = fluid.layers.concat([pred2, pred], axis=1)
                acc = fluid.layers.accuracy(pred, fluid.layers.cast(label, dtype='int64'))
                accuracies.append(acc.numpy())
                losses.append(loss.numpy())
            print("[validation] accuracy/loss: {}/{}".format(np.mean(accuracies), np.mean(losses)))
            model.train()
        # save params of model
        fluid.save_dygraph(model.state_dict(), 'palm')
        # save optimizer state
        fluid.save_dygraph(opt.state_dict(), 'palm')
# 定义评估过程
def evaluation(model, params_file_path):
    with fluid.dygraph.guard():
        print('start evaluation .......')
        #加载模型参数
        model_state_dict, _ = fluid.load_dygraph(params_file_path)
        model.load_dict(model_state_dict)

        model.eval()
        eval_loader = data_loader(DATADIR, 
                        batch_size=10, mode='eval')

        acc_set = []
        avg_loss_set = []
        for batch_id, data in enumerate(eval_loader()):
            x_data, y_data = data
            img = fluid.dygraph.to_variable(x_data)
            label = fluid.dygraph.to_variable(y_data)
            y_data = y_data.astype(np.int64)
            label_64 = fluid.dygraph.to_variable(y_data)
            # 计算预测和精度
            prediction, acc = model(img, label_64)
            # 计算损失函数值
            loss = fluid.layers.sigmoid_cross_entropy_with_logits(prediction, label)
            avg_loss = fluid.layers.mean(loss)
            acc_set.append(float(acc.numpy()))
            avg_loss_set.append(float(avg_loss.numpy()))
        # 求平均精度
        acc_val_mean = np.array(acc_set).mean()
        avg_loss_val_mean = np.array(avg_loss_set).mean()

        print('loss={}, acc={}'.format(avg_loss_val_mean, acc_val_mean))

最后:

if __name__ == '__main__':
    with fluid.dygraph.guard():
        model = ResNet()
    with LogWriter(logdir="./log") as writer:
        train(model)

输出:
在这里插入图片描述

可视化部分参考这个:帮助文档(也可以私信)

start training ... 
epoch: 0, batch_id: 0, loss is: [0.8371515]
epoch: 0, batch_id: 10, loss is: [0.6344372]
epoch: 0, batch_id: 20, loss is: [0.6589644]
epoch: 0, batch_id: 30, loss is: [0.39426336]
[validation] accuracy/loss: 0.875/0.3943755626678467
epoch: 1, batch_id: 0, loss is: [0.4967832]
epoch: 1, batch_id: 10, loss is: [0.2819938]
epoch: 1, batch_id: 20, loss is: [1.0662365]
epoch: 1, batch_id: 30, loss is: [0.36524695]
[validation] accuracy/loss: 0.9100000262260437/0.24338199198246002
epoch: 2, batch_id: 0, loss is: [0.29004684]
epoch: 2, batch_id: 10, loss is: [0.25300348]
epoch: 2, batch_id: 20, loss is: [0.40797377]
epoch: 2, batch_id: 30, loss is: [0.45543328]
[validation] accuracy/loss: 0.9325000047683716/0.22884537279605865
epoch: 3, batch_id: 0, loss is: [0.24656934]
epoch: 3, batch_id: 10, loss is: [0.23437345]
epoch: 3, batch_id: 20, loss is: [0.60181767]
epoch: 3, batch_id: 30, loss is: [0.52316177]
[validation] accuracy/loss: 0.9375/0.21094627678394318
epoch: 4, batch_id: 0, loss is: [0.13762574]
epoch: 4, batch_id: 10, loss is: [0.13181563]
epoch: 4, batch_id: 20, loss is: [0.7039488]
epoch: 4, batch_id: 30, loss is: [0.22397807]
[validation] accuracy/loss: 0.949999988079071/0.1660669445991516
epoch: 5, batch_id: 0, loss is: [0.09801286]
epoch: 5, batch_id: 10, loss is: [0.55572665]
epoch: 5, batch_id: 20, loss is: [0.01605941]
epoch: 5, batch_id: 30, loss is: [0.04204616]
[validation] accuracy/loss: 0.9475000500679016/0.148434117436409
epoch: 6, batch_id: 0, loss is: [0.0294241]
epoch: 6, batch_id: 10, loss is: [0.12392274]
epoch: 6, batch_id: 20, loss is: [0.18163615]
epoch: 6, batch_id: 30, loss is: [0.07404109]
[validation] accuracy/loss: 0.9449999928474426/0.17515026032924652
epoch: 7, batch_id: 0, loss is: [0.08615561]
epoch: 7, batch_id: 10, loss is: [0.64013994]
epoch: 7, batch_id: 20, loss is: [0.26979375]
epoch: 7, batch_id: 30, loss is: [0.03654323]
[validation] accuracy/loss: 0.949999988079071/0.13243742287158966
epoch: 8, batch_id: 0, loss is: [0.43148398]
epoch: 8, batch_id: 10, loss is: [0.13331008]
epoch: 8, batch_id: 20, loss is: [0.09306534]
epoch: 8, batch_id: 30, loss is: [0.05727107]
[validation] accuracy/loss: 0.9549999237060547/0.14401279389858246
epoch: 9, batch_id: 0, loss is: [0.06815202]
epoch: 9, batch_id: 10, loss is: [0.02614539]
epoch: 9, batch_id: 20, loss is: [0.06249169]
epoch: 9, batch_id: 30, loss is: [0.19215429]
[validation] accuracy/loss: 0.9350000619888306/0.15081432461738586

有什么不对或者可以改进的地方欢迎评论,互相交流
在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐