Skip to content

  • 体验新版
    • 正在加载...
  • 登录
  • PaddlePaddle
  • PaddleSlim
  • Issue
  • #467

P
PaddleSlim
  • 项目概览

PaddlePaddle / PaddleSlim
大约 2 年 前同步成功

通知 51
Star 1434
Fork 344
  • 代码
    • 文件
    • 提交
    • 分支
    • Tags
    • 贡献者
    • 分支图
    • Diff
  • Issue 53
    • 列表
    • 看板
    • 标记
    • 里程碑
  • 合并请求 16
  • Wiki 0
    • Wiki
  • 分析
    • 仓库
    • DevOps
  • 项目成员
  • Pages
P
PaddleSlim
  • 项目概览
    • 项目概览
    • 详情
    • 发布
  • 仓库
    • 仓库
    • 文件
    • 提交
    • 分支
    • 标签
    • 贡献者
    • 分支图
    • 比较
  • Issue 53
    • Issue 53
    • 列表
    • 看板
    • 标记
    • 里程碑
  • 合并请求 16
    • 合并请求 16
  • Pages
  • 分析
    • 分析
    • 仓库分析
    • DevOps
  • Wiki 0
    • Wiki
  • 成员
    • 成员
  • 收起侧边栏
  • 动态
  • 分支图
  • 创建新Issue
  • 提交
  • Issue看板
You need to sign in or sign up before continuing.
已关闭
开放中
Opened 9月 25, 2020 by saxon_zh@saxon_zhGuest

蒸馏模型训练报错

Created by: 1558513572

**用的paddleslim现成的蒸馏函数,训练的时候报错

C:\Anaconda3\lib\site-packages\paddle\fluid\executor.py:789: UserWarning: The following exception is not an EOF exception.
  "The following exception is not an EOF exception.")
Traceback (most recent call last):
  File "C:/Users/虹虹/Desktop/加油4/qinglianghua_python/copy999999999999999.py", line 133, in <module>
    acc1, loss_np = exe.run(student_program, feed=train_feeder.feed(data), fetch_list=[acc_top1.name, avg_cost.name])
  File "C:\Anaconda3\lib\site-packages\paddle\fluid\executor.py", line 790, in run
    six.reraise(*sys.exc_info())
  File "C:\Anaconda3\lib\site-packages\six.py", line 703, in reraise
    raise value
  File "C:\Anaconda3\lib\site-packages\paddle\fluid\executor.py", line 785, in run
    use_program_cache=use_program_cache)
  File "C:\Anaconda3\lib\site-packages\paddle\fluid\executor.py", line 838, in _run_impl
    use_program_cache=use_program_cache)
  File "C:\Anaconda3\lib\site-packages\paddle\fluid\executor.py", line 912, in _run_program
    fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet: 

--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
Windows not support stack backtrace yet.

------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
  File "C:\Anaconda3\lib\site-packages\paddle\fluid\framework.py", line 2525, in append_op
    attrs=kwargs.get("attrs", None))
  File "C:\Anaconda3\lib\site-packages\paddleslim\dist\single_distiller.py", line 95, in merge
    type=op.type, inputs=inputs, outputs=outputs, attrs=attrs)
  File "C:/Users/虹虹/Desktop/加油4/qinglianghua_python/copy999999999999999.py", line 111, in <module>
    main = slim.dist.merge(teacher_program, student_program, data_name_map, fluid.CPUPlace())

----------------------
Error Message Summary:
----------------------
InvalidArgumentError: The Tensor in the conv2d Op's Input Variable Input(teacher_data) is not initialized.
  [Hint: Expected t->IsInitialized() == true, but received t->IsInitialized():0 != true:1.] at (D:\1.7.2\paddle\paddle\fluid\framework\operator.cc:1264)
  [operator < conv2d > error]

源代码:

1. 导入依赖
import paddle
import os
import paddle.fluid as fluid
import paddleslim as slim
import models
import reader
import numpy as np
  1. 定义student_program和teacher_program
tokens_student = [5, 13, 0, 0]  #1个
tokens_teacher = [5,13,0,0,3,1,2,1,4,16,0,1,3,18,0,0]  #4个

student_program = fluid.Program()
student_startup = fluid.Program()

with fluid.program_guard(student_program, student_startup):

        config1 = [('MobileNetV2BlockSpace', {'input_size': 224, 'output_size': 112, 'block_num': 1})]
        sanas1 = slim.nas.SANAS(configs=config1, server_addr=("", 9887), init_temperature=1000, reduce_rate=0.99,
                            search_steps=None, init_tokens=None, save_checkpoint='/home/aistudio/work/nas_checkpoint',
                            load_checkpoint=None, is_server=True)
        model1 = sanas1.tokens2arch(tokens_student)[0]

        image = fluid.data(name='data', shape=[None, 3, 224, 224], dtype='float32')
        label = fluid.data(name='label', shape=[None, 1], dtype='int64')

        output = model1(image)
        out = fluid.layers.fc(input=output, size=5)

        cost = fluid.layers.cross_entropy(input=out, label=label)
        avg_cost = fluid.layers.mean(x=cost)
        acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)

place = fluid.CPUPlace()
exe = fluid.Executor(place)

teacher_program = fluid.Program()
teacher_startup = fluid.Program()

with fluid.program_guard(teacher_program, teacher_startup):
        with fluid.unique_name.guard():

                config3 = [('MobileNetV2BlockSpace', {'input_size': 224, 'output_size': 14, 'block_num': 4})]
                sanas3 = slim.nas.SANAS(configs=config3, server_addr=("", 9997), init_temperature=1000, reduce_rate=0.99,
                                search_steps=None, init_tokens=None,
                                save_checkpoint='/home/aistudio/work/nas_checkpoint', load_checkpoint=None,
                                is_server=True)
                model3 = sanas3.tokens2arch(tokens_teacher)[0]

                image3 = fluid.data(name='data', shape=[None, 3, 224, 224], dtype='float32')
                label3 = fluid.data(name='label', shape=[None, 1], dtype='int64')

                output3 = model3(image3)
                out3 = fluid.layers.fc(input=output3, size=5)

                cost3 = fluid.layers.cross_entropy(input=out3, label=label3)
                avg_cost3= fluid.layers.mean(x=cost3)
                acc_top13 = fluid.layers.accuracy(input=out3, label=label3, k=1)

exe.run(teacher_startup)
  1. 合并program (merge)并添加蒸馏loss
data_name_map = {'image3': 'image'}
main = slim.dist.merge(teacher_program, student_program, data_name_map, fluid.CPUPlace())

with fluid.program_guard(student_program, student_startup):

        l2_loss = slim.dist.l2_loss('teacher_depthwise_conv2d_0.tmp_0', 'depthwise_conv2d_0.tmp_0', student_program)
        print(l2_loss)
        loss = l2_loss + avg_cost

        opt = fluid.optimizer.Momentum(0.01, 0.9)
        opt.minimize(loss)

exe.run(student_startup)
  1. 模型训练
train_reader = paddle.batch(reader=reader.train_reader('target/train.list',224,250), batch_size=64, drop_last=True)
train_feeder = fluid.DataFeeder(place=place, feed_list=[image, label])

for pass_id in range(100):

      for batch_id, data in enumerate(train_reader()):

            acc1, loss_np = exe.run(student_program, feed=train_feeder.feed(data), fetch_list=[acc_top1.name, avg_cost.name])

            print('Pass:%d, Batch:%d, Acc1:%0.5f, Loss:%0.5f' % (pass_id, batch_id, acc1.mean(), loss_np.mean()))
指派人
分配到
无
里程碑
无
分配里程碑
工时统计
无
截止日期
无
标识: paddlepaddle/PaddleSlim#467
渝ICP备2023009037号

京公网安备11010502055752号

网络110报警服务 Powered by GitLab CE v13.7
开源知识
Git 入门 Pro Git 电子书 在线学 Git
Markdown 基础入门 IT 技术知识开源图谱
帮助
使用手册 反馈建议 博客
《GitCode 隐私声明》 《GitCode 服务条款》 关于GitCode
Powered by GitLab CE v13.7