简介

GoogleNet是google公司推出的高性能网络结构。

  • 其独特的 inception 结构,让网络自行学习不同感受野特征的融合。

用PaddlePaddle实现GoogleNet-LMLPHP

  • 为了应对深度网络训练时,反向传播梯度过小的问题,训练时候将分类 loss 在网络中间引入,加强对浅层网络参数的学习

用PaddlePaddle实现GoogleNet-LMLPHP

  • 优秀博客:https://blog.csdn.net/loveliuzz/article/details/79135583
  • 论文链接:https://www.cs.unc.edu/~wliu/papers/GoogLeNet.pdf
下载安装命令

## CPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/cpu paddlepaddle

## GPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/gpu paddlepaddle-gpu

注意

本项目代码需要使用GPU环境来运行:

用PaddlePaddle实现GoogleNet-LMLPHP
 

并且检查相关参数设置, 例如use_gpu, fluid.CUDAPlace(0)等处是否设置正确.

In[1]
# 解压鲜花数据集  
!cd data/data2815 && unzip -qo flower_photos.zip
In[2]
!cd data/data6593 && unzip -qo GoogleNet_pretrained.zip
 

对本地数据做一些预处理,主要用于随机分组,一部分用于训练,一部分用于验证。同时生产两者的标签文件

In[3]
import codecs
import os
import random
import shutil
from PIL import Image

# 训练和验证数据集的比例,多少比例用于训练
train_ratio = 4.0 / 5

all_file_dir = 'data/data2815'
class_list = [c for c in os.listdir(all_file_dir) if os.path.isdir(os.path.join(all_file_dir, c)) and not c.endswith('Set') and not c.startswith('.')]
class_list.sort()
print(class_list)
train_image_dir = os.path.join(all_file_dir, "trainImageSet")
if not os.path.exists(train_image_dir):
    os.makedirs(train_image_dir)

eval_image_dir = os.path.join(all_file_dir, "evalImageSet")
if not os.path.exists(eval_image_dir):
    os.makedirs(eval_image_dir)

train_file = codecs.open(os.path.join(all_file_dir, "train.txt"), 'w')
eval_file = codecs.open(os.path.join(all_file_dir, "eval.txt"), 'w')

with codecs.open(os.path.join(all_file_dir, "label_list.txt"), "w") as label_list:
    label_id = 0
    for class_dir in class_list:
        label_list.write("{0}\t{1}\n".format(label_id, class_dir))
        image_path_pre = os.path.join(all_file_dir, class_dir)
        for file in os.listdir(image_path_pre):
            try:
                img = Image.open(os.path.join(image_path_pre, file))
                if random.uniform(0, 1) <= train_ratio:
                    shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(train_image_dir, file))
                    train_file.write("{0}\t{1}\n".format(os.path.join(train_image_dir, file), label_id))
                else:
                    shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(eval_image_dir, file))
                    eval_file.write("{0}\t{1}\n".format(os.path.join(eval_image_dir, file), label_id))
            except Exception as e:
                pass
                # 存在一些文件打不开,此处需要稍作清洗  
        label_id += 1

train_file.close()
eval_file.close() 
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
In[4]
# -*- coding: UTF-8 -*-
"""
训练常用视觉基础网络,用于分类任务
需要将训练图片,类别文件 label_list.txt 放置在同一个文件夹下
程序会先读取 train.txt 文件获取类别数和图片数量
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import numpy as np
import time
import math
import paddle
import paddle.fluid as fluid
import codecs
import logging

from paddle.fluid.initializer import MSRA
from paddle.fluid.initializer import Uniform
from paddle.fluid.param_attr import ParamAttr
from PIL import Image
from PIL import ImageEnhance

train_parameters = {
    "input_size": [3, 224, 224],
    "class_dim": -1,  # 分类数,会在初始化自定义 reader 的时候获得   
    "image_count": -1,  # 训练图片数量,会在初始化自定义 reader 的时候获得   
    "label_dict": {},
    "data_dir": "data/data2815",  # 训练数据存储地址   
    "train_file_list": "train.txt",
    "label_file": "label_list.txt",
    "save_freeze_dir": "./freeze-model",
    "save_persistable_dir": "./persistable-params",
    "continue_train": True,        # 是否接着上一次保存的参数接着训练,优先级高于预训练模型   
    "pretrained": True,            # 是否使用预训练的模型   
    "pretrained_dir": "data/data6593/GoogleNet_pretrained",
    "mode": "train",
    "num_epochs": 120,
    "train_batch_size": 30,
    "mean_rgb": [127.5, 127.5, 127.5],  # 常用图片的三通道均值,通常来说需要先对训练数据做统计,此处仅取中间值   
    "use_gpu": True,
    "dropout_seed": None,
    "image_enhance_strategy": {  # 图像增强相关策略   
        "need_distort": True,  # 是否启用图像颜色增强   
        "need_rotate": True,   # 是否需要增加随机角度   
        "need_crop": True,      # 是否要增加裁剪   
        "need_flip": True,      # 是否要增加水平随机翻转   
        "hue_prob": 0.5,
        "hue_delta": 18,
        "contrast_prob": 0.5,
        "contrast_delta": 0.5,
        "saturation_prob": 0.5,
        "saturation_delta": 0.5,
        "brightness_prob": 0.5,
        "brightness_delta": 0.125
    },
    "early_stop": {
        "sample_frequency": 50,
        "successive_limit": 3,
        "good_acc1": 0.92
    },
    "rsm_strategy": {
        "learning_rate": 0.001,
        "lr_epochs": [20, 40, 60, 80, 100],
        "lr_decay": [1, 0.5, 0.25, 0.1, 0.01, 0.002]
    },
    "momentum_strategy": {
        "learning_rate": 0.001,
        "lr_epochs": [20, 40, 60, 80, 100],
        "lr_decay": [1, 0.5, 0.25, 0.1, 0.01, 0.002]
    },
    "sgd_strategy": {
        "learning_rate": 0.001,
        "lr_epochs": [20, 40, 60, 80, 100],
        "lr_decay": [1, 0.5, 0.25, 0.1, 0.01, 0.002]
    },
    "adam_strategy": {
        "learning_rate": 0.002
    }
}


class GoogleNet():
    """
    GoogleNet网络类
    """
    def __init__(self):
        self.params = train_parameters

    def conv_layer(self,
                   input,
                   num_filters,
                   filter_size,
                   stride=1,
                   groups=1,
                   act=None,
                   name=None):
        channels = input.shape[1]
        stdv = (3.0 / (filter_size**2 * channels))**0.5
        param_attr = ParamAttr(
            initializer=fluid.initializer.Uniform(-stdv, stdv),
            name=name + "_weights")
        conv = fluid.layers.conv2d(
            input=input,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            act=act,
            param_attr=param_attr,
            bias_attr=False,
            name=name)
        return conv

    def xavier(self, channels, filter_size, name):
        stdv = (3.0 / (filter_size**2 * channels))**0.5
        param_attr = ParamAttr(
            initializer=fluid.initializer.Uniform(-stdv, stdv),
            name=name + "_weights")

        return param_attr

    def inception(self,
                  input,
                  channels,
                  filter1,
                  filter3R,
                  filter3,
                  filter5R,
                  filter5,
                  proj,
                  name=None):
        conv1 = self.conv_layer(
            input=input,
            num_filters=filter1,
            filter_size=1,
            stride=1,
            act=None,
            name="inception_" + name + "_1x1")
        conv3r = self.conv_layer(
            input=input,
            num_filters=filter3R,
            filter_size=1,
            stride=1,
            act=None,
            name="inception_" + name + "_3x3_reduce")
        conv3 = self.conv_layer(
            input=conv3r,
            num_filters=filter3,
            filter_size=3,
            stride=1,
            act=None,
            name="inception_" + name + "_3x3")
        conv5r = self.conv_layer(
            input=input,
            num_filters=filter5R,
            filter_size=1,
            stride=1,
            act=None,
            name="inception_" + name + "_5x5_reduce")
        conv5 = self.conv_layer(
            input=conv5r,
            num_filters=filter5,
            filter_size=5,
            stride=1,
            act=None,
            name="inception_" + name + "_5x5")
        pool = fluid.layers.pool2d(
            input=input,
            pool_size=3,
            pool_stride=1,
            pool_padding=1,
            pool_type='max')
        convprj = fluid.layers.conv2d(
            input=pool,
            filter_size=1,
            num_filters=proj,
            stride=1,
            padding=0,
            name="inception_" + name + "_3x3_proj",
            param_attr=ParamAttr(
                name="inception_" + name + "_3x3_proj_weights"),
            bias_attr=False)
        cat = fluid.layers.concat(input=[conv1, conv3, conv5, convprj], axis=1)
        cat = fluid.layers.relu(cat)
        return cat

    def net(self, input, class_dim=1000):
        conv = self.conv_layer(
            input=input,
            num_filters=64,
            filter_size=7,
            stride=2,
            act=None,
            name="conv1")
        pool = fluid.layers.pool2d(
            input=conv, pool_size=3, pool_type='max', pool_stride=2)

        conv = self.conv_layer(
            input=pool,
            num_filters=64,
            filter_size=1,
            stride=1,
            act=None,
            name="conv2_1x1")
        conv = self.conv_layer(
            input=conv,
            num_filters=192,
            filter_size=3,
            stride=1,
            act=None,
            name="conv2_3x3")
        pool = fluid.layers.pool2d(
            input=conv, pool_size=3, pool_type='max', pool_stride=2)

        ince3a = self.inception(pool, 192, 64, 96, 128, 16, 32, 32, "ince3a")
        ince3b = self.inception(ince3a, 256, 128, 128, 192, 32, 96, 64,
                                "ince3b")
        pool3 = fluid.layers.pool2d(
            input=ince3b, pool_size=3, pool_type='max', pool_stride=2)

        ince4a = self.inception(pool3, 480, 192, 96, 208, 16, 48, 64, "ince4a")
        ince4b = self.inception(ince4a, 512, 160, 112, 224, 24, 64, 64,
                                "ince4b")
        ince4c = self.inception(ince4b, 512, 128, 128, 256, 24, 64, 64,
                                "ince4c")
        ince4d = self.inception(ince4c, 512, 112, 144, 288, 32, 64, 64,
                                "ince4d")
        ince4e = self.inception(ince4d, 528, 256, 160, 320, 32, 128, 128,
                                "ince4e")
        pool4 = fluid.layers.pool2d(
            input=ince4e, pool_size=3, pool_type='max', pool_stride=2)

        ince5a = self.inception(pool4, 832, 256, 160, 320, 32, 128, 128,
                                "ince5a")
        ince5b = self.inception(ince5a, 832, 384, 192, 384, 48, 128, 128,
                                "ince5b")
        pool5 = fluid.layers.pool2d(
            input=ince5b, pool_size=7, pool_type='avg', pool_stride=7)
        dropout = fluid.layers.dropout(x=pool5, dropout_prob=0.4)
        out = fluid.layers.fc(input=dropout,
                              size=class_dim,
                              act='softmax',
                              param_attr=self.xavier(1024, 1, "out"),
                              name="out",
                              bias_attr=ParamAttr(name="out_offset"))

        pool_o1 = fluid.layers.pool2d(
            input=ince4a, pool_size=5, pool_type='avg', pool_stride=3)
        conv_o1 = self.conv_layer(
            input=pool_o1,
            num_filters=128,
            filter_size=1,
            stride=1,
            act=None,
            name="conv_o1")
        fc_o1 = fluid.layers.fc(input=conv_o1,
                                size=1024,
                                act='relu',
                                param_attr=self.xavier(2048, 1, "fc_o1"),
                                name="fc_o1",
                                bias_attr=ParamAttr(name="fc_o1_offset"))
        dropout_o1 = fluid.layers.dropout(x=fc_o1, dropout_prob=0.7)
        out1 = fluid.layers.fc(input=dropout_o1,
                               size=class_dim,
                               act='softmax',
                               param_attr=self.xavier(1024, 1, "out1"),
                               name="out1",
                               bias_attr=ParamAttr(name="out1_offset"))

        pool_o2 = fluid.layers.pool2d(
            input=ince4d, pool_size=5, pool_type='avg', pool_stride=3)
        conv_o2 = self.conv_layer(
            input=pool_o2,
            num_filters=128,
            filter_size=1,
            stride=1,
            act=None,
            name="conv_o2")
        fc_o2 = fluid.layers.fc(input=conv_o2,
                                size=1024,
                                act='relu',
                                param_attr=self.xavier(2048, 1, "fc_o2"),
                                name="fc_o2",
                                bias_attr=ParamAttr(name="fc_o2_offset"))
        dropout_o2 = fluid.layers.dropout(x=fc_o2, dropout_prob=0.7)
        out2 = fluid.layers.fc(input=dropout_o2,
                               size=class_dim,
                               act='softmax',
                               param_attr=self.xavier(1024, 1, "out2"),
                               name="out2",
                               bias_attr=ParamAttr(name="out2_offset"))

        # last fc layer is "out"
        return out, out1, out2


def init_log_config():
    """
    初始化日志相关配置
    :return:
    """
    global logger
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    log_path = os.path.join(os.getcwd(), 'logs')
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    log_name = os.path.join(log_path, 'train.log')
    sh = logging.StreamHandler()
    fh = logging.FileHandler(log_name, mode='w')
    fh.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    fh.setFormatter(formatter)
    sh.setFormatter(formatter)
    logger.addHandler(sh)
    logger.addHandler(fh)


def init_train_parameters():
    """
    初始化训练参数,主要是初始化图片数量,类别数
    :return:
    """
    train_file_list = os.path.join(train_parameters['data_dir'], train_parameters['train_file_list'])
    label_list = os.path.join(train_parameters['data_dir'], train_parameters['label_file'])
    index = 0
    with codecs.open(label_list, encoding='utf-8') as flist:
        lines = [line.strip() for line in flist]
        for line in lines:
            parts = line.strip().split()
            train_parameters['label_dict'][parts[1]] = int(parts[0])
            index += 1
        train_parameters['class_dim'] = index
    with codecs.open(train_file_list, encoding='utf-8') as flist:
        lines = [line.strip() for line in flist]
        train_parameters['image_count'] = len(lines)


def resize_img(img, target_size):
    """
    强制缩放图片
    :param img:
    :param target_size:
    :return:
    """
    target_size = input_size
    img = img.resize((target_size[1], target_size[2]), Image.BILINEAR)
    return img


def random_crop(img, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]):
    """
    图片随机裁剪,扣取中心区域
    """
    aspect_ratio = math.sqrt(np.random.uniform(*ratio))
    w = 1. * aspect_ratio
    h = 1. / aspect_ratio

    bound = min((float(img.size[0]) / img.size[1]) / (w**2),
                (float(img.size[1]) / img.size[0]) / (h**2))
    scale_max = min(scale[1], bound)
    scale_min = min(scale[0], bound)

    target_area = img.size[0] * img.size[1] * np.random.uniform(scale_min,
                                                                scale_max)
    target_size = math.sqrt(target_area)
    w = int(target_size * w)
    h = int(target_size * h)

    i = np.random.randint(0, img.size[0] - w + 1)
    j = np.random.randint(0, img.size[1] - h + 1)

    img = img.crop((i, j, i + w, j + h))
    img = img.resize((train_parameters['input_size'][1], train_parameters['input_size'][2]), Image.BILINEAR)
    return img


def rotate_image(img):
    """
    图像增强,增加随机旋转角度
    """
    angle = np.random.randint(-14, 15)
    img = img.rotate(angle)
    return img


def random_brightness(img):
    """
    图像增强,亮度调整
    :param img:
    :return:
    """
    prob = np.random.uniform(0, 1)
    if prob < train_parameters['image_enhance_strategy']['brightness_prob']:
        brightness_delta = train_parameters['image_enhance_strategy']['brightness_delta']
        delta = np.random.uniform(-brightness_delta, brightness_delta) + 1
        img = ImageEnhance.Brightness(img).enhance(delta)
    return img


def random_contrast(img):
    """
    图像增强,对比度调整
    :param img:
    :return:
    """
    prob = np.random.uniform(0, 1)
    if prob < train_parameters['image_enhance_strategy']['contrast_prob']:
        contrast_delta = train_parameters['image_enhance_strategy']['contrast_delta']
        delta = np.random.uniform(-contrast_delta, contrast_delta) + 1
        img = ImageEnhance.Contrast(img).enhance(delta)
    return img


def random_saturation(img):
    """
    图像增强,饱和度调整
    :param img:
    :return:
    """
    prob = np.random.uniform(0, 1)
    if prob < train_parameters['image_enhance_strategy']['saturation_prob']:
        saturation_delta = train_parameters['image_enhance_strategy']['saturation_delta']
        delta = np.random.uniform(-saturation_delta, saturation_delta) + 1
        img = ImageEnhance.Color(img).enhance(delta)
    return img


def random_hue(img):
    """
    图像增强,色度调整
    :param img:
    :return:
    """
    prob = np.random.uniform(0, 1)
    if prob < train_parameters['image_enhance_strategy']['hue_prob']:
        hue_delta = train_parameters['image_enhance_strategy']['hue_delta']
        delta = np.random.uniform(-hue_delta, hue_delta)
        img_hsv = np.array(img.convert('HSV'))
        img_hsv[:, :, 0] = img_hsv[:, :, 0] + delta
        img = Image.fromarray(img_hsv, mode='HSV').convert('RGB')
    return img


def distort_color(img):
    """
    概率的图像增强
    :param img:
    :return:
    """
    prob = np.random.uniform(0, 1)
    # Apply different distort order
    if prob < 0.35:
        img = random_brightness(img)
        img = random_contrast(img)
        img = random_saturation(img)
        img = random_hue(img)
    elif prob < 0.7:
        img = random_brightness(img)
        img = random_saturation(img)
        img = random_hue(img)
        img = random_contrast(img)
    return img


def custom_image_reader(file_list, data_dir, mode):
    """
    自定义用户图片读取器,先初始化图片种类,数量
    :param file_list:
    :param data_dir:
    :param mode:
    :return:
    """
    with codecs.open(file_list) as flist:
        lines = [line.strip() for line in flist]

    def reader():
        np.random.shuffle(lines)
        for line in lines:
            if mode == 'train' or mode == 'val':
                img_path, label = line.split()
                img = Image.open(img_path)
                try:
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    if train_parameters['image_enhance_strategy']['need_distort'] == True:
                        img = distort_color(img)
                    if train_parameters['image_enhance_strategy']['need_rotate'] == True:
                        img = rotate_image(img)
                    if train_parameters['image_enhance_strategy']['need_crop'] == True:
                        img = random_crop(img, train_parameters['input_size'])
                    if train_parameters['image_enhance_strategy']['need_flip'] == True:
                        mirror = int(np.random.uniform(0, 2))
                        if mirror == 1:
                            img = img.transpose(Image.FLIP_LEFT_RIGHT)
                    # HWC--->CHW && normalized
                    img = np.array(img).astype('float32')
                    img -= train_parameters['mean_rgb']
                    img = img.transpose((2, 0, 1))  # HWC to CHW
                    img *= 0.007843                 # 像素值归一化
                    yield img, int(label)
                except Exception as e:
                    pass                            # 以防某些图片读取处理出错,加异常处理
            elif mode == 'test':
                img_path = os.path.join(data_dir, line)
                img = Image.open(img_path)
                if img.mode != 'RGB':
                    img = img.convert('RGB')
                img = resize_img(img, train_parameters['input_size'])
                # HWC--->CHW && normalized
                img = np.array(img).astype('float32')
                img -= train_parameters['mean_rgb']
                img = img.transpose((2, 0, 1))  # HWC to CHW
                img *= 0.007843  # 像素值归一化
                yield img

    return reader


def optimizer_momentum_setting():
    """
    阶梯型的学习率适合比较大规模的训练数据
    """
    learning_strategy = train_parameters['momentum_strategy']
    batch_size = train_parameters["train_batch_size"]
    iters = train_parameters["image_count"] // batch_size
    lr = learning_strategy['learning_rate']

    boundaries = [i * iters for i in learning_strategy["lr_epochs"]]
    values = [i * lr for i in learning_strategy["lr_decay"]]
    learning_rate = fluid.layers.piecewise_decay(boundaries, values)
    optimizer = fluid.optimizer.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
    return optimizer


def optimizer_rms_setting():
    """
    阶梯型的学习率适合比较大规模的训练数据
    """
    batch_size = train_parameters["train_batch_size"]
    iters = train_parameters["image_count"] // batch_size
    learning_strategy = train_parameters['rsm_strategy']
    lr = learning_strategy['learning_rate']

    boundaries = [i * iters for i in learning_strategy["lr_epochs"]]
    values = [i * lr for i in learning_strategy["lr_decay"]]

    optimizer = fluid.optimizer.RMSProp(
        learning_rate=fluid.layers.piecewise_decay(boundaries, values))

    return optimizer


def optimizer_sgd_setting():
    """
    loss下降相对较慢,但是最终效果不错,阶梯型的学习率适合比较大规模的训练数据
    """
    learning_strategy = train_parameters['sgd_strategy']
    batch_size = train_parameters["train_batch_size"]
    iters = train_parameters["image_count"] // batch_size
    lr = learning_strategy['learning_rate']

    boundaries = [i * iters for i in learning_strategy["lr_epochs"]]
    values = [i * lr for i in learning_strategy["lr_decay"]]
    learning_rate = fluid.layers.piecewise_decay(boundaries, values)
    optimizer = fluid.optimizer.SGD(learning_rate=learning_rate)
    return optimizer


def optimizer_adam_setting():
    """
    能够比较快速的降低 loss,但是相对后期乏力
    """
    learning_strategy = train_parameters['adam_strategy']
    learning_rate = learning_strategy['learning_rate']
    optimizer = fluid.optimizer.Adam(learning_rate=learning_rate)
    return optimizer


def load_params(exe, program):
    if train_parameters['continue_train'] and os.path.exists(train_parameters['save_persistable_dir']):
        logger.info('load params from retrain model')
        fluid.io.load_persistables(executor=exe,
                                   dirname=train_parameters['save_persistable_dir'],
                                   main_program=program)
    elif train_parameters['pretrained'] and os.path.exists(train_parameters['pretrained_dir']):
        logger.info('load params from pretrained model')
        def if_exist(var):
            return os.path.exists(os.path.join(train_parameters['pretrained_dir'], var.name))

        fluid.io.load_vars(exe, train_parameters['pretrained_dir'], main_program=program,
                           predicate=if_exist)


def train():
    train_prog = fluid.Program()
    train_startup = fluid.Program()
    logger.info("create prog success")
    logger.info("train config: %s", str(train_parameters))
    logger.info("build input custom reader and data feeder")
    file_list = os.path.join(train_parameters['data_dir'], "train.txt")
    mode = train_parameters['mode']
    batch_reader = paddle.batch(custom_image_reader(file_list, train_parameters['data_dir'], mode),
                                batch_size=train_parameters['train_batch_size'],
                                drop_last=False)
    batch_reader = paddle.reader.shuffle(batch_reader, train_parameters['train_batch_size'])
    place = fluid.CUDAPlace(0) if train_parameters['use_gpu'] else fluid.CPUPlace()
    # 定义输入数据的占位符
    img = fluid.layers.data(name='img', shape=train_parameters['input_size'], dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    feeder = fluid.DataFeeder(feed_list=[img, label], place=place)

    # 选取不同的网络
    logger.info("build newwork")
    model = GoogleNet()
    out, out1, out2 = model.net(input=img, class_dim=train_parameters['class_dim'])
    cost0 = fluid.layers.cross_entropy(input=out, label=label)
    cost1 = fluid.layers.cross_entropy(input=out1, label=label)
    cost2 = fluid.layers.cross_entropy(input=out2, label=label)
    avg_cost0 = fluid.layers.mean(x=cost0)
    avg_cost1 = fluid.layers.mean(x=cost1)
    avg_cost2 = fluid.layers.mean(x=cost2)

    avg_cost = avg_cost0 + 0.3 * avg_cost1 + 0.3 * avg_cost2
    acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
    # 选取不同的优化器
    optimizer = optimizer_rms_setting()
    # optimizer = optimizer_momentum_setting()
    # optimizer = optimizer_sgd_setting()
    # optimizer = optimizer_adam_setting()
    optimizer.minimize(avg_cost)
    exe = fluid.Executor(place)

    main_program = fluid.default_main_program()
    exe.run(fluid.default_startup_program())
    train_fetch_list = [avg_cost.name, acc_top1.name, out.name]

    load_params(exe, main_program)

    # 训练循环主体
    stop_strategy = train_parameters['early_stop']
    successive_limit = stop_strategy['successive_limit']
    sample_freq = stop_strategy['sample_frequency']
    good_acc1 = stop_strategy['good_acc1']
    successive_count = 0
    stop_train = False
    total_batch_count = 0
    for pass_id in range(train_parameters["num_epochs"]):
        logger.info("current pass: %d, start read image", pass_id)
        batch_id = 0
        for step_id, data in enumerate(batch_reader()):
            t1 = time.time()
            loss, acc1, pred_ot = exe.run(main_program,
                                          feed=feeder.feed(data),
                                          fetch_list=train_fetch_list)
            t2 = time.time()
            batch_id += 1
            total_batch_count += 1
            period = t2 - t1
            loss = np.mean(np.array(loss))
            acc1 = np.mean(np.array(acc1))
            if batch_id % 10 == 0:
                logger.info("Pass {}, trainbatch {}, loss {}, acc1 {}, time {}".format(pass_id, batch_id, loss, acc1,
                                                                                            "%2.2f sec" % period))
            # 简单的提前停止策略,认为连续达到某个准确率就可以停止了
            if acc1 >= good_acc1:
                successive_count += 1
                logger.info("current acc1 {0} meets good {1}, successive count {2}".format(acc1, good_acc1, successive_count))
                fluid.io.save_inference_model(dirname=train_parameters['save_freeze_dir'],
                                              feeded_var_names=['img'],
                                              target_vars=[out],
                                              main_program=main_program,
                                              executor=exe)
                if successive_count >= successive_limit:
                    logger.info("end training")
                    stop_train = True
                    break
            else:
                successive_count = 0

            # 通用的保存策略,减小意外停止的损失
            if total_batch_count % sample_freq == 0:
                logger.info("temp save {0} batch train result, current acc1 {1}".format(total_batch_count, acc1))
                fluid.io.save_persistables(dirname=train_parameters['save_persistable_dir'],
                                           main_program=main_program,
                                           executor=exe)
        if stop_train:
            break
    logger.info("training till last epcho, end training")
    fluid.io.save_persistables(dirname=train_parameters['save_persistable_dir'],
                                           main_program=main_program,
                                           executor=exe)
    fluid.io.save_inference_model(dirname=train_parameters['save_freeze_dir'],
                                              feeded_var_names=['img'],
                                              target_vars=[out],
                                              main_program=main_program,
                                              executor=exe)


if __name__ == '__main__':
    init_log_config()
    init_train_parameters()
    train()
2020-03-06 19:28:02,536-INFO: create prog success
2020-03-06 19:28:02,536 - <ipython-input-4-55625227fef7>[line:601] - INFO: create prog success
2020-03-06 19:28:02,538-INFO: train config: {'input_size': [3, 224, 224], 'class_dim': 5, 'image_count': 2952, 'label_dict': {'daisy': 0, 'dandelion': 1, 'roses': 2, 'sunflowers': 3, 'tulips': 4}, 'data_dir': 'data/data2815', 'train_file_list': 'train.txt', 'label_file': 'label_list.txt', 'save_freeze_dir': './freeze-model', 'save_persistable_dir': './persistable-params', 'continue_train': True, 'pretrained': True, 'pretrained_dir': 'data/data6593/GoogleNet_pretrained', 'mode': 'train', 'num_epochs': 120, 'train_batch_size': 30, 'mean_rgb': [127.5, 127.5, 127.5], 'use_gpu': True, 'dropout_seed': None, 'image_enhance_strategy': {'need_distort': True, 'need_rotate': True, 'need_crop': True, 'need_flip': True, 'hue_prob': 0.5, 'hue_delta': 18, 'contrast_prob': 0.5, 'contrast_delta': 0.5, 'saturation_prob': 0.5, 'saturation_delta': 0.5, 'brightness_prob': 0.5, 'brightness_delta': 0.125}, 'early_stop': {'sample_frequency': 50, 'successive_limit': 3, 'good_acc1': 0.92}, 'rsm_strategy': {'learning_rate': 0.001, 'lr_epochs': [20, 40, 60, 80, 100], 'lr_decay': [1, 0.5, 0.25, 0.1, 0.01, 0.002]}, 'momentum_strategy': {'learning_rate': 0.001, 'lr_epochs': [20, 40, 60, 80, 100], 'lr_decay': [1, 0.5, 0.25, 0.1, 0.01, 0.002]}, 'sgd_strategy': {'learning_rate': 0.001, 'lr_epochs': [20, 40, 60, 80, 100], 'lr_decay': [1, 0.5, 0.25, 0.1, 0.01, 0.002]}, 'adam_strategy': {'learning_rate': 0.002}}
2020-03-06 19:28:02,538 - <ipython-input-4-55625227fef7>[line:602] - INFO: train config: {'input_size': [3, 224, 224], 'class_dim': 5, 'image_count': 2952, 'label_dict': {'daisy': 0, 'dandelion': 1, 'roses': 2, 'sunflowers': 3, 'tulips': 4}, 'data_dir': 'data/data2815', 'train_file_list': 'train.txt', 'label_file': 'label_list.txt', 'save_freeze_dir': './freeze-model', 'save_persistable_dir': './persistable-params', 'continue_train': True, 'pretrained': True, 'pretrained_dir': 'data/data6593/GoogleNet_pretrained', 'mode': 'train', 'num_epochs': 120, 'train_batch_size': 30, 'mean_rgb': [127.5, 127.5, 127.5], 'use_gpu': True, 'dropout_seed': None, 'image_enhance_strategy': {'need_distort': True, 'need_rotate': True, 'need_crop': True, 'need_flip': True, 'hue_prob': 0.5, 'hue_delta': 18, 'contrast_prob': 0.5, 'contrast_delta': 0.5, 'saturation_prob': 0.5, 'saturation_delta': 0.5, 'brightness_prob': 0.5, 'brightness_delta': 0.125}, 'early_stop': {'sample_frequency': 50, 'successive_limit': 3, 'good_acc1': 0.92}, 'rsm_strategy': {'learning_rate': 0.001, 'lr_epochs': [20, 40, 60, 80, 100], 'lr_decay': [1, 0.5, 0.25, 0.1, 0.01, 0.002]}, 'momentum_strategy': {'learning_rate': 0.001, 'lr_epochs': [20, 40, 60, 80, 100], 'lr_decay': [1, 0.5, 0.25, 0.1, 0.01, 0.002]}, 'sgd_strategy': {'learning_rate': 0.001, 'lr_epochs': [20, 40, 60, 80, 100], 'lr_decay': [1, 0.5, 0.25, 0.1, 0.01, 0.002]}, 'adam_strategy': {'learning_rate': 0.002}}
2020-03-06 19:28:02,539-INFO: build input custom reader and data feeder
2020-03-06 19:28:02,539 - <ipython-input-4-55625227fef7>[line:603] - INFO: build input custom reader and data feeder
2020-03-06 19:28:02,541-INFO: build newwork
2020-03-06 19:28:02,541 - <ipython-input-4-55625227fef7>[line:617] - INFO: build newwork
2020-03-06 19:28:06,435-INFO: load params from retrain model
2020-03-06 19:28:06,435 - <ipython-input-4-55625227fef7>[line:585] - INFO: load params from retrain model
2020-03-06 19:28:06,705-INFO: current pass: 0, start read image
2020-03-06 19:28:06,705 - <ipython-input-4-55625227fef7>[line:652] - INFO: current pass: 0, start read image
2020-03-06 19:28:16,236-INFO: Pass 0, trainbatch 10, loss 0.8844944834709167, acc1 0.7666666507720947, time 0.08 sec
2020-03-06 19:28:16,236 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 10, loss 0.8844944834709167, acc1 0.7666666507720947, time 0.08 sec
2020-03-06 19:28:17,064-INFO: Pass 0, trainbatch 20, loss 1.0388295650482178, acc1 0.6666666865348816, time 0.07 sec
2020-03-06 19:28:17,064 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 20, loss 1.0388295650482178, acc1 0.6666666865348816, time 0.07 sec
2020-03-06 19:28:17,659-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:17,659 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:17,947-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:28:17,947 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:28:18,228-INFO: Pass 0, trainbatch 30, loss 0.5246240496635437, acc1 0.8999999761581421, time 0.08 sec
2020-03-06 19:28:18,228 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 30, loss 0.5246240496635437, acc1 0.8999999761581421, time 0.08 sec
2020-03-06 19:28:26,559-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:26,559 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:27,263-INFO: Pass 0, trainbatch 40, loss 0.2532863914966583, acc1 1.0, time 0.07 sec
2020-03-06 19:28:27,263 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 40, loss 0.2532863914966583, acc1 1.0, time 0.07 sec
2020-03-06 19:28:27,265-INFO: current acc1 1.0 meets good 0.92, successive count 1
2020-03-06 19:28:27,265 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2020-03-06 19:28:28,194-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:28,194 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:28,636-INFO: Pass 0, trainbatch 50, loss 0.9014854431152344, acc1 0.7666666507720947, time 0.07 sec
2020-03-06 19:28:28,636 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 50, loss 0.9014854431152344, acc1 0.7666666507720947, time 0.07 sec
2020-03-06 19:28:28,638-INFO: temp save 50 batch train result, current acc1 0.7666666507720947
2020-03-06 19:28:28,638 - <ipython-input-4-55625227fef7>[line:686] - INFO: temp save 50 batch train result, current acc1 0.7666666507720947
2020-03-06 19:28:29,929-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:29,929 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:30,602-INFO: Pass 0, trainbatch 60, loss 0.8472419381141663, acc1 0.7666666507720947, time 0.08 sec
2020-03-06 19:28:30,602 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 60, loss 0.8472419381141663, acc1 0.7666666507720947, time 0.08 sec
2020-03-06 19:28:39,661-INFO: Pass 0, trainbatch 70, loss 0.9140654802322388, acc1 0.800000011920929, time 0.07 sec
2020-03-06 19:28:39,661 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 70, loss 0.9140654802322388, acc1 0.800000011920929, time 0.07 sec
2020-03-06 19:28:39,896-INFO: current acc1 1.0 meets good 0.92, successive count 1
2020-03-06 19:28:39,896 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2020-03-06 19:28:40,384-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:40,384 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:40,664-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:28:40,664 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:28:41,278-INFO: Pass 0, trainbatch 80, loss 0.612647294998169, acc1 0.800000011920929, time 0.26 sec
2020-03-06 19:28:41,278 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 80, loss 0.612647294998169, acc1 0.800000011920929, time 0.26 sec
2020-03-06 19:28:41,585-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:41,585 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:42,041-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:28:42,041 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:28:42,472-INFO: Pass 0, trainbatch 90, loss 2.169705390930176, acc1 0.7333333492279053, time 0.07 sec
2020-03-06 19:28:42,472 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 0, trainbatch 90, loss 2.169705390930176, acc1 0.7333333492279053, time 0.07 sec
2020-03-06 19:28:45,204-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:28:45,204 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:28:45,668-INFO: current pass: 1, start read image
2020-03-06 19:28:45,668 - <ipython-input-4-55625227fef7>[line:652] - INFO: current pass: 1, start read image
2020-03-06 19:28:54,135-INFO: temp save 100 batch train result, current acc1 0.699999988079071
2020-03-06 19:28:54,135 - <ipython-input-4-55625227fef7>[line:686] - INFO: temp save 100 batch train result, current acc1 0.699999988079071
2020-03-06 19:28:55,799-INFO: Pass 1, trainbatch 10, loss 0.9314966797828674, acc1 0.7333333492279053, time 0.07 sec
2020-03-06 19:28:55,799 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 10, loss 0.9314966797828674, acc1 0.7333333492279053, time 0.07 sec
2020-03-06 19:28:56,245-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:56,245 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:56,924-INFO: Pass 1, trainbatch 20, loss 2.5400595664978027, acc1 0.5, time 0.07 sec
2020-03-06 19:28:56,924 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 20, loss 2.5400595664978027, acc1 0.5, time 0.07 sec
2020-03-06 19:28:57,601-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:57,601 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:28:57,899-INFO: Pass 1, trainbatch 30, loss 0.8119416236877441, acc1 0.7333333492279053, time 0.08 sec
2020-03-06 19:28:57,899 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 30, loss 0.8119416236877441, acc1 0.7333333492279053, time 0.08 sec
2020-03-06 19:29:06,366-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:06,366 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:07,076-INFO: Pass 1, trainbatch 40, loss 0.6516159176826477, acc1 0.8999999761581421, time 0.07 sec
2020-03-06 19:29:07,076 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 40, loss 0.6516159176826477, acc1 0.8999999761581421, time 0.07 sec
2020-03-06 19:29:07,153-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:07,153 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:07,992-INFO: current acc1 1.0 meets good 0.92, successive count 1
2020-03-06 19:29:07,992 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2020-03-06 19:29:08,438-INFO: Pass 1, trainbatch 50, loss 0.6137370467185974, acc1 0.7333333492279053, time 0.24 sec
2020-03-06 19:29:08,438 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 50, loss 0.6137370467185974, acc1 0.7333333492279053, time 0.24 sec
2020-03-06 19:29:08,516-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:08,516 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:08,720-INFO: temp save 150 batch train result, current acc1 0.9333333373069763
2020-03-06 19:29:08,720 - <ipython-input-4-55625227fef7>[line:686] - INFO: temp save 150 batch train result, current acc1 0.9333333373069763
2020-03-06 19:29:10,229-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:10,229 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:10,573-INFO: Pass 1, trainbatch 60, loss 0.5486891269683838, acc1 0.8666666746139526, time 0.08 sec
2020-03-06 19:29:10,573 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 60, loss 0.5486891269683838, acc1 0.8666666746139526, time 0.08 sec
2020-03-06 19:29:19,558-INFO: Pass 1, trainbatch 70, loss 0.9605356454849243, acc1 0.800000011920929, time 0.07 sec
2020-03-06 19:29:19,558 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 70, loss 0.9605356454849243, acc1 0.800000011920929, time 0.07 sec
2020-03-06 19:29:19,975-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:19,975 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:20,370-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:20,370 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:20,703-INFO: Pass 1, trainbatch 80, loss 0.5003396272659302, acc1 0.9333333373069763, time 0.07 sec
2020-03-06 19:29:20,703 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 80, loss 0.5003396272659302, acc1 0.9333333373069763, time 0.07 sec
2020-03-06 19:29:20,706-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:20,706 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:21,202-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:21,202 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:21,727-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:21,727 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:22,202-INFO: Pass 1, trainbatch 90, loss 0.7468236088752747, acc1 0.9333333373069763, time 0.08 sec
2020-03-06 19:29:22,202 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 1, trainbatch 90, loss 0.7468236088752747, acc1 0.9333333373069763, time 0.08 sec
2020-03-06 19:29:22,204-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:22,204 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:25,368-INFO: current pass: 2, start read image
2020-03-06 19:29:25,368 - <ipython-input-4-55625227fef7>[line:652] - INFO: current pass: 2, start read image
2020-03-06 19:29:33,443-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:33,443 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:33,694-INFO: temp save 200 batch train result, current acc1 0.9666666388511658
2020-03-06 19:29:33,694 - <ipython-input-4-55625227fef7>[line:686] - INFO: temp save 200 batch train result, current acc1 0.9666666388511658
2020-03-06 19:29:34,760-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 2
2020-03-06 19:29:34,760 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 2
2020-03-06 19:29:35,355-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:35,355 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:35,859-INFO: Pass 2, trainbatch 10, loss 0.43560710549354553, acc1 0.8999999761581421, time 0.07 sec
2020-03-06 19:29:35,859 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 10, loss 0.43560710549354553, acc1 0.8999999761581421, time 0.07 sec
2020-03-06 19:29:36,085-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:36,085 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:36,816-INFO: Pass 2, trainbatch 20, loss 1.1130001544952393, acc1 0.6666666865348816, time 0.08 sec
2020-03-06 19:29:36,816 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 20, loss 1.1130001544952393, acc1 0.6666666865348816, time 0.08 sec
2020-03-06 19:29:37,056-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:37,056 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:37,335-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:29:37,335 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:29:37,992-INFO: Pass 2, trainbatch 30, loss 0.5283582806587219, acc1 0.8999999761581421, time 0.08 sec
2020-03-06 19:29:37,992 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 30, loss 0.5283582806587219, acc1 0.8999999761581421, time 0.08 sec
2020-03-06 19:29:46,143-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:46,143 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:47,144-INFO: Pass 2, trainbatch 40, loss 0.7221382260322571, acc1 0.7666666507720947, time 0.25 sec
2020-03-06 19:29:47,144 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 40, loss 0.7221382260322571, acc1 0.7666666507720947, time 0.25 sec
2020-03-06 19:29:47,221-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:47,221 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:48,042-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:48,042 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:48,347-INFO: Pass 2, trainbatch 50, loss 0.6543073058128357, acc1 0.8333333134651184, time 0.08 sec
2020-03-06 19:29:48,347 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 50, loss 0.6543073058128357, acc1 0.8333333134651184, time 0.08 sec
2020-03-06 19:29:48,508-INFO: temp save 250 batch train result, current acc1 0.8333333134651184
2020-03-06 19:29:48,508 - <ipython-input-4-55625227fef7>[line:686] - INFO: temp save 250 batch train result, current acc1 0.8333333134651184
2020-03-06 19:29:49,573-INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:49,573 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9666666388511658 meets good 0.92, successive count 1
2020-03-06 19:29:50,339-INFO: Pass 2, trainbatch 60, loss 0.37053820490837097, acc1 0.9333333373069763, time 0.08 sec
2020-03-06 19:29:50,339 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 60, loss 0.37053820490837097, acc1 0.9333333373069763, time 0.08 sec
2020-03-06 19:29:50,341-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:50,341 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:59,399-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:59,399 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 1
2020-03-06 19:29:59,728-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:29:59,728 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 2
2020-03-06 19:30:00,162-INFO: Pass 2, trainbatch 70, loss 0.34231722354888916, acc1 0.9333333373069763, time 0.07 sec
2020-03-06 19:30:00,162 - <ipython-input-4-55625227fef7>[line:667] - INFO: Pass 2, trainbatch 70, loss 0.34231722354888916, acc1 0.9333333373069763, time 0.07 sec
2020-03-06 19:30:00,166-INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 3
2020-03-06 19:30:00,166 - <ipython-input-4-55625227fef7>[line:671] - INFO: current acc1 0.9333333373069763 meets good 0.92, successive count 3
2020-03-06 19:30:00,363-INFO: end training
2020-03-06 19:30:00,363 - <ipython-input-4-55625227fef7>[line:678] - INFO: end training
2020-03-06 19:30:00,368-INFO: training till last epcho, end training
2020-03-06 19:30:00,368 - <ipython-input-4-55625227fef7>[line:692] - INFO: training till last epcho, end training
In[5]
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import numpy as np
import random
import time
import codecs
import sys
import functools
import math
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.param_attr import ParamAttr
from PIL import Image, ImageEnhance

target_size = [3, 224, 224]
mean_rgb = [127.5, 127.5, 127.5]
data_dir = "data/data2815"
eval_file = "eval.txt"
use_gpu = True
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
save_freeze_dir = "./freeze-model"
[inference_program, feed_target_names, fetch_targets] = fluid.io.load_inference_model(dirname=save_freeze_dir, executor=exe)
# print(fetch_targets)    


def crop_image(img, target_size):
    width, height = img.size
    w_start = (width - target_size[2]) / 2
    h_start = (height - target_size[1]) / 2
    w_end = w_start + target_size[2]
    h_end = h_start + target_size[1]
    img = img.crop((w_start, h_start, w_end, h_end))
    return img


def resize_img(img, target_size):
    ret = img.resize((target_size[1], target_size[2]), Image.BILINEAR)
    return ret


def read_image(img_path):
    img = Image.open(img_path)
    if img.mode != 'RGB':
        img = img.convert('RGB')
    img = crop_image(img, target_size)
    img = np.array(img).astype('float32')
    img -= mean_rgb
    img = img.transpose((2, 0, 1))  # HWC to CHW    
    img *= 0.007843
    img = img[np.newaxis,:]
    return img


def infer(image_path):
    tensor_img = read_image(image_path)
    label = exe.run(inference_program, feed={feed_target_names[0]: tensor_img}, fetch_list=fetch_targets)
    return np.argmax(label)


def eval_all():
    eval_file_path = os.path.join(data_dir, eval_file)
    total_count = 0
    right_count = 0
    with codecs.open(eval_file_path, encoding='utf-8') as flist:
        lines = [line.strip() for line in flist]
        t1 = time.time()
        for line in lines:
            total_count += 1
            parts = line.strip().split()
            result = infer(parts[0])
            # print("infer result:{0} answer:{1}".format(result, parts[1]))    
            if str(result) == parts[1]:
                right_count += 1
        period = time.time() - t1
        print("total eval count:{0} cost time:{1} predict accuracy:{2}".format(total_count, "%2.2f sec" % period, right_count / total_count))


if __name__ == '__main__':
    eval_all()  
total eval count:718 cost time:12.55 sec predict accuracy:0.8565459610027855

使用AI Studio一键上手实践项目吧:https://aistudio.baidu.com/aistudio/projectdetail/122278

下载安装命令

## CPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/cpu paddlepaddle

## GPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/gpu paddlepaddle-gpu

>> 访问 PaddlePaddle 官网,了解更多相关内容 

09-04 16:05