# 图像分类网络结构搜索-快速开始 该教程以图像分类模型MobileNetV2为例,说明如何在cifar10数据集上快速使用[网络结构搜索接口](../api/nas_api.md)。 Tips: 运行该示例前请确认已正确安装Paddle和PaddleSlim。 该示例包含以下步骤: 1. 加载示例数据 2. 构建模型 3. 开始训练当前网络结构 4. 开始评估当前网络结构 5. 回传当前网络结构的得分 以下章节依次介绍每个步骤的内容。 # 1. 加载示例数据 NEED to add more description 使用的示例数据集为cifar10,paddle框架中`paddle.dataset.cifar`包括了cifar数据集的下载和读取,代码如下: ```python import paddle import paddle.fluid as fluid train_reader = paddle.batch(paddle.reader.shuffle(paddle.dataset.cifar.train10(cycle=False), buf_size=1024),batch_size=256) train_feeder = fluid.DataFeeder(inputs, fluid.CPUPlace()) eval_reader = paddle.batch(paddle.dataset.cifar.test10(cycle=False), batch_size=256) eval_feeder = fluid.DataFeeder(inputs, fluid.CPUPlace()) ``` 其他数据集读取请参考[自定义数据集]() # 2. 构建模型 NEED to add more description ```python ### 初始化SANAS实例 sanas = slim.nas.SANAS(configs=[('MobileNetV2Space')], server_addr=("", 8337), save_checkpoint=None) ### 获取当前搜索到的模型结构 archs = sanas.next_archs()[0] ### 根据模型结构构造训练program和测试program train_program = fluid.Program() startup_program = fluid.Program() with fluid.program_guard(train_program, startup_program): data = fluid.data(name='data', shape=[None, 3, 32, 32], dtype='float32') label = fluid.data(name='label', shape=[None, 1], dtype='int64') output = archs(data) output = fluid.layers.fc(input=output, size=10) ### 定义优化器和损失函数 softmax_out = fluid.layers.softmax(input=output, use_cudnn=False) cost = fluid.layers.cross_entropy(input=softmax_out, label=label) avg_cost = fluid.layers.mean(cost) acc_top1 = fluid.layers.accuracy(input=softmax_out, label=label, k=1) acc_top5 = fluid.layers.accuracy(input=softmax_out, label=label, k=5) ### 克隆训练program得到测试program,这一步必须放在定义优化器之前 eval_program = fluid.default_main_program().clone(for_test=True) optimizer = fluid.optimizer.Adam(learning_rate=0.1) optimizer.minimize(avg_cost) place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(startup_program) ``` # 3. 开始训练当前网络结构 NEED to add more description ```python outputs = [avg_cost.name, acc_top1.name, acc_top5.name] for data in train_reader(): batch_reward = exe.run(train_program, feed=train_feeder.feed(data), fetch_list = outputs) print("TRAIN: loss: {}, acc1: {}, acc5:{}".format(batch_reward[0], batch_reward[1], batch_reward[2])) ``` # 4. 开始评估当前网络结构 NEED to add more description ```python reward = [] for data in eval_reader(): batch_reward = exe.run(eval_program, feed=eval_feeder.feed(data), fetch_list = outputs) reward_avg = np.mean(np.array(batch_reward), axis=1) reward.append(reward_avg) print("TEST: loss: {}, acc1: {}, acc5:{}".format(batch_reward[0], batch_reward[1], batch_reward[2])) finally_reward = np.mean(np.array(reward), axis=0) print("FINAL TEST: avg_cost: {}, acc1: {}, acc5: {}".format(finally_reward[0], finally_reward[1], finally_reward[2])) ``` # 5. 回传当前网络结构的得分 NEED to add more description ```python sanas.reward(float(finally_reward[1])) ```