基于PaddlePaddle的图像分类-DenseNet

别等时光非礼了梦想. 提交于 2019-12-19 18:49:10
# 解压花朵数据集   
!cd data/data2815 && unzip -qo flower_photos.zip 
# !export FLAGS_fraction_of_gpu_memory_to_use=0.9
# !echo $FLAGS_fraction_of_gpu_memory_to_use
# 预处理数据,将其转化为标准格式。同时将数据拆分成两份,以便训练和计算预估准确率 
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’]

# -*- coding: UTF-8 -*- 
""" 
训练常用视觉基础网络,用于分类任务 
需要将训练图片,类别文件 label_list.txt 放置在同一个文件夹下 
程序会先读取 train.txt 文件获取类别数和图片数量 
""" 
from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 
import os
os.environ['FLAGS_eager_delete_tensor_gb'] = '0'
os.environ['FLAGS_fraction_of_gpu_memory_to_use'] = '0.75'
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, 512, 512],
    "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": False,            # 是否使用预训练的模型    
    "pretrained_dir": "data/data6593/DenseNet_pretrained",
    "mode": "train",
    "num_epochs": 120,
    "train_batch_size": 11,
    "mean_rgb": [127.5, 127.5, 127.5],  # 常用图片的三通道均值,通常来说需要先对训练数据做统计,此处仅取中间值    
    "use_gpu": True,
    "dropout_prob": 0.2,
    "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,
        "rotate_prob": 0.5,
        "rotate_range": 14
    },
    "early_stop": {
        "sample_frequency": 50,
        "successive_limit": 5,
        "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.05, 0.01]
    }
}
 
 
class DenseNet(): 
    def __init__(self, layers, dropout_prob):
        self.layers = layers
        self.dropout_prob = dropout_prob
 
    def bottleneck_layer(self, input, fliter_num, name):
        bn = fluid.layers.batch_norm(input=input, act='relu', name=name + '_bn1')
        conv1 = fluid.layers.conv2d(input=bn, num_filters=fliter_num * 4, filter_size=1, name=name + '_conv1')
        dropout = fluid.layers.dropout(x=conv1, dropout_prob=self.dropout_prob)

        bn = fluid.layers.batch_norm(input=dropout, act='relu', name=name + '_bn2')
        conv2 = fluid.layers.conv2d(input=bn, num_filters=fliter_num, filter_size=3, padding=1, name=name + '_conv2')
        dropout = fluid.layers.dropout(x=conv2, dropout_prob=self.dropout_prob)

        return dropout

    def dense_block(self, input, block_num, fliter_num, name):
        layers = []
        layers.append(input)#拼接到列表

        x = self.bottleneck_layer(input, fliter_num, name=name + '_bottle_' + str(0))
        layers.append(x)
        for i in range(block_num - 1):
            x = paddle.fluid.layers.concat(layers, axis=1)
            x = self.bottleneck_layer(x, fliter_num, name=name + '_bottle_' + str(i + 1))
            layers.append(x)

        return paddle.fluid.layers.concat(layers, axis=1)

    def transition_layer(self, input, fliter_num, name):
        bn = fluid.layers.batch_norm(input=input, act='relu', name=name + '_bn1')
        conv1 = fluid.layers.conv2d(input=bn, num_filters=fliter_num, filter_size=1, name=name + '_conv1') 
        dropout = fluid.layers.dropout(x=conv1, dropout_prob=self.dropout_prob)
        
        return fluid.layers.pool2d(input=dropout, pool_size=2, pool_type='avg', pool_stride=2)
 
    def net(self, input, class_dim=1000): 

        layer_count_dict = {
            121: (32, [6, 12, 24, 16]),
            169: (32, [6, 12, 32, 32]),
            201: (32, [6, 12, 48, 32]),
            161: (48, [6, 12, 36, 24])
        }
        layer_conf = layer_count_dict[self.layers]

        conv = fluid.layers.conv2d(input=input, num_filters=layer_conf[0] * 2, 
            filter_size=7, stride=2, padding=3, name='densenet_conv0')
        conv = fluid.layers.pool2d(input=conv, pool_size=3, pool_padding=1, pool_type='max', pool_stride=2)
        for i in range(len(layer_conf[1]) - 1):
            conv = self.dense_block(conv, layer_conf[1][i], layer_conf[0], 'dense_' + str(i))
            conv = self.transition_layer(conv, layer_conf[0], name='trans_' + str(i))

        conv = self.dense_block(conv, layer_conf[1][-1], layer_conf[0], 'dense_' + str(len(layer_conf[1])))
        conv = fluid.layers.pool2d(input=conv, global_pooling=True, pool_type='avg')
        out = fluid.layers.fc(conv, class_dim, act='softmax')
        # last fc layer is "out" 
        return out

 
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): 
    """ 
    图像增强,增加随机旋转角度 
    """ 
    prob = np.random.uniform(0, 1) 
    if prob < train_parameters['image_enhance_strategy']['rotate_prob']: 
        range = train_parameters['image_enhance_strategy']['rotate_range']
        angle = np.random.randint(-range, range) 
        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_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 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 = DenseNet(121, train_parameters['dropout_prob']) 
    out = model.net(input=img, class_dim=train_parameters['class_dim']) 
    
    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) 
    optimizer = optimizer_rms_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 {0}, trainbatch {1}, loss {2}, acc1 {3}, time {4}".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.clone(for_test=True), 
                                              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.clone(for_test=True), 
                                              executor=exe) 
 
 
if __name__ == '__main__': 
    init_log_config() 
    init_train_parameters() 
    train()

2019-05-18 22:19:39,899 - INFO - create prog success
2019-05-18 22:19:39,899 - [line:395] - INFO: create prog success
2019-05-18 22:19:39,901 - INFO - train config: {‘input_size’: [3, 512, 512], ‘class_dim’: 5, ‘image_count’: 2932, ‘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’: False, ‘pretrained_dir’: ‘data/data6593/DenseNet_pretrained’, ‘mode’: ‘train’, ‘num_epochs’: 120, ‘train_batch_size’: 11, ‘mean_rgb’: [127.5, 127.5, 127.5], ‘use_gpu’: True, ‘dropout_prob’: 0.2, ‘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, ‘rotate_prob’: 0.5, ‘rotate_range’: 14}, ‘early_stop’: {‘sample_frequency’: 50, ‘successive_limit’: 5, ‘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.05, 0.01]}}
2019-05-18 22:19:39,901 - [line:396] - INFO: train config: {‘input_size’: [3, 512, 512], ‘class_dim’: 5, ‘image_count’: 2932, ‘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’: False, ‘pretrained_dir’: ‘data/data6593/DenseNet_pretrained’, ‘mode’: ‘train’, ‘num_epochs’: 120, ‘train_batch_size’: 11, ‘mean_rgb’: [127.5, 127.5, 127.5], ‘use_gpu’: True, ‘dropout_prob’: 0.2, ‘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, ‘rotate_prob’: 0.5, ‘rotate_range’: 14}, ‘early_stop’: {‘sample_frequency’: 50, ‘successive_limit’: 5, ‘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.05, 0.01]}}
2019-05-18 22:19:39,903 - INFO - build input custom reader and data feeder
2019-05-18 22:19:39,903 - [line:397] - INFO: build input custom reader and data feeder
2019-05-18 22:19:39,905 - INFO - build newwork
2019-05-18 22:19:39,905 - [line:411] - INFO: build newwork
2019-05-18 22:19:46,671 - INFO - load params from retrain model
2019-05-18 22:19:46,671 - [line:379] - INFO: load params from retrain model
2019-05-18 22:19:47,792 - INFO - current pass: 0, start read image
2019-05-18 22:19:47,792 - [line:436] - INFO: current pass: 0, start read image
2019-05-18 22:19:52,952 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:19:52,952 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:19:54,843 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:19:54,843 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:19:57,527 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:19:57,527 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:00,692 - INFO - Pass 0, trainbatch 10, loss 0.1157061904668808, acc1 1.0, time 0.57 sec
2019-05-18 22:20:00,692 - [line:451] - INFO: Pass 0, trainbatch 10, loss 0.1157061904668808, acc1 1.0, time 0.57 sec
2019-05-18 22:20:00,699 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:00,699 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:06,556 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:06,556 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:12,381 - INFO - Pass 0, trainbatch 20, loss 0.22961699962615967, acc1 0.9090909361839294, time 0.57 sec
2019-05-18 22:20:12,381 - [line:451] - INFO: Pass 0, trainbatch 20, loss 0.22961699962615967, acc1 0.9090909361839294, time 0.57 sec
2019-05-18 22:20:18,525 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:18,525 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:22,237 - INFO - Pass 0, trainbatch 30, loss 0.6702784895896912, acc1 0.8181818723678589, time 0.57 sec
2019-05-18 22:20:22,237 - [line:451] - INFO: Pass 0, trainbatch 30, loss 0.6702784895896912, acc1 0.8181818723678589, time 0.57 sec
2019-05-18 22:20:24,326 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:24,326 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:30,874 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:30,874 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:32,997 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:20:32,997 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:20:35,696 - INFO - Pass 0, trainbatch 40, loss 0.1303936243057251, acc1 1.0, time 0.88 sec
2019-05-18 22:20:35,696 - [line:451] - INFO: Pass 0, trainbatch 40, loss 0.1303936243057251, acc1 1.0, time 0.88 sec
2019-05-18 22:20:35,698 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:35,698 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:39,552 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:39,552 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:45,883 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:45,883 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:47,913 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:20:47,913 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:20:49,761 - INFO - Pass 0, trainbatch 50, loss 0.34803473949432373, acc1 0.9090909361839294, time 0.58 sec
2019-05-18 22:20:49,761 - [line:451] - INFO: Pass 0, trainbatch 50, loss 0.34803473949432373, acc1 0.9090909361839294, time 0.58 sec
2019-05-18 22:20:49,765 - INFO - temp save 50 batch train result, current acc1 0.9090909361839294
2019-05-18 22:20:49,765 - [line:470] - INFO: temp save 50 batch train result, current acc1 0.9090909361839294
2019-05-18 22:20:53,462 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:20:53,462 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:01,694 - INFO - Pass 0, trainbatch 60, loss 0.4136064350605011, acc1 0.9090909361839294, time 0.55 sec
2019-05-18 22:21:01,694 - [line:451] - INFO: Pass 0, trainbatch 60, loss 0.4136064350605011, acc1 0.9090909361839294, time 0.55 sec
2019-05-18 22:21:02,244 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:02,244 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:04,370 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:21:04,370 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:21:08,103 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:08,103 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:14,658 - INFO - Pass 0, trainbatch 70, loss 0.3570030629634857, acc1 0.9090909361839294, time 0.57 sec
2019-05-18 22:21:14,658 - [line:451] - INFO: Pass 0, trainbatch 70, loss 0.3570030629634857, acc1 0.9090909361839294, time 0.57 sec
2019-05-18 22:21:16,379 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:16,379 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:18,491 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:21:18,491 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:21:20,627 - INFO - current acc1 1.0 meets good 0.92, successive count 3
2019-05-18 22:21:20,627 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 3
2019-05-18 22:21:27,694 - INFO - Pass 0, trainbatch 80, loss 0.6537821888923645, acc1 0.8181818723678589, time 0.69 sec
2019-05-18 22:21:27,694 - [line:451] - INFO: Pass 0, trainbatch 80, loss 0.6537821888923645, acc1 0.8181818723678589, time 0.69 sec
2019-05-18 22:21:29,302 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:29,302 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:33,753 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:33,753 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:39,643 - INFO - Pass 0, trainbatch 90, loss 0.17312754690647125, acc1 1.0, time 0.56 sec
2019-05-18 22:21:39,643 - [line:451] - INFO: Pass 0, trainbatch 90, loss 0.17312754690647125, acc1 1.0, time 0.56 sec
2019-05-18 22:21:39,649 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:39,649 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:45,557 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:45,557 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:51,798 - INFO - Pass 0, trainbatch 100, loss 0.4179750978946686, acc1 0.8181818723678589, time 0.57 sec
2019-05-18 22:21:51,798 - [line:451] - INFO: Pass 0, trainbatch 100, loss 0.4179750978946686, acc1 0.8181818723678589, time 0.57 sec
2019-05-18 22:21:51,804 - INFO - temp save 100 batch train result, current acc1 0.8181818723678589
2019-05-18 22:21:51,804 - [line:470] - INFO: temp save 100 batch train result, current acc1 0.8181818723678589
2019-05-18 22:21:55,054 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:55,054 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:57,742 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:21:57,742 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:02,492 - INFO - Pass 0, trainbatch 110, loss 0.25689032673835754, acc1 0.9090909361839294, time 0.95 sec
2019-05-18 22:22:02,492 - [line:451] - INFO: Pass 0, trainbatch 110, loss 0.25689032673835754, acc1 0.9090909361839294, time 0.95 sec
2019-05-18 22:22:07,993 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:07,993 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:12,713 - INFO - Pass 0, trainbatch 120, loss 0.6637387871742249, acc1 0.8181818723678589, time 0.60 sec
2019-05-18 22:22:12,713 - [line:451] - INFO: Pass 0, trainbatch 120, loss 0.6637387871742249, acc1 0.8181818723678589, time 0.60 sec
2019-05-18 22:22:17,120 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:17,120 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:19,141 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:22:19,141 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:22:22,968 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:22,968 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:25,567 - INFO - Pass 0, trainbatch 130, loss 0.506217896938324, acc1 0.9090909361839294, time 0.56 sec
2019-05-18 22:22:25,567 - [line:451] - INFO: Pass 0, trainbatch 130, loss 0.506217896938324, acc1 0.9090909361839294, time 0.56 sec
2019-05-18 22:22:34,313 - INFO - Pass 0, trainbatch 140, loss 0.2324863225221634, acc1 0.9090909361839294, time 0.79 sec
2019-05-18 22:22:34,313 - [line:451] - INFO: Pass 0, trainbatch 140, loss 0.2324863225221634, acc1 0.9090909361839294, time 0.79 sec
2019-05-18 22:22:34,916 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:34,916 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:40,810 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:40,810 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:45,768 - INFO - Pass 0, trainbatch 150, loss 0.7361337542533875, acc1 0.7272727489471436, time 0.55 sec
2019-05-18 22:22:45,768 - [line:451] - INFO: Pass 0, trainbatch 150, loss 0.7361337542533875, acc1 0.7272727489471436, time 0.55 sec
2019-05-18 22:22:45,773 - INFO - temp save 150 batch train result, current acc1 0.7272727489471436
2019-05-18 22:22:45,773 - [line:470] - INFO: temp save 150 batch train result, current acc1 0.7272727489471436
2019-05-18 22:22:56,361 - INFO - Pass 0, trainbatch 160, loss 0.43212974071502686, acc1 0.8181818723678589, time 0.57 sec
2019-05-18 22:22:56,361 - [line:451] - INFO: Pass 0, trainbatch 160, loss 0.43212974071502686, acc1 0.8181818723678589, time 0.57 sec
2019-05-18 22:22:56,921 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:56,921 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:22:58,995 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:22:58,995 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:02,539 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:02,539 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:09,577 - INFO - Pass 0, trainbatch 170, loss 0.428496778011322, acc1 0.8181818723678589, time 0.55 sec
2019-05-18 22:23:09,577 - [line:451] - INFO: Pass 0, trainbatch 170, loss 0.428496778011322, acc1 0.8181818723678589, time 0.55 sec
2019-05-18 22:23:16,343 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:16,343 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:18,364 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:18,364 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:21,064 - INFO - Pass 0, trainbatch 180, loss 0.41167399287223816, acc1 0.8181818723678589, time 0.58 sec
2019-05-18 22:23:21,064 - [line:451] - INFO: Pass 0, trainbatch 180, loss 0.41167399287223816, acc1 0.8181818723678589, time 0.58 sec
2019-05-18 22:23:22,768 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:22,768 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:24,861 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:24,861 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:26,834 - INFO - current acc1 1.0 meets good 0.92, successive count 3
2019-05-18 22:23:26,834 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 3
2019-05-18 22:23:32,886 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:32,886 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:34,898 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:34,898 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:23:36,672 - INFO - Pass 0, trainbatch 190, loss 0.49355635046958923, acc1 0.7272727489471436, time 0.56 sec
2019-05-18 22:23:36,672 - [line:451] - INFO: Pass 0, trainbatch 190, loss 0.49355635046958923, acc1 0.7272727489471436, time 0.56 sec
2019-05-18 22:23:45,551 - INFO - Pass 0, trainbatch 200, loss 1.0594851970672607, acc1 0.7272727489471436, time 0.59 sec
2019-05-18 22:23:45,551 - [line:451] - INFO: Pass 0, trainbatch 200, loss 1.0594851970672607, acc1 0.7272727489471436, time 0.59 sec
2019-05-18 22:23:45,555 - INFO - temp save 200 batch train result, current acc1 0.7272727489471436
2019-05-18 22:23:45,555 - [line:470] - INFO: temp save 200 batch train result, current acc1 0.7272727489471436
2019-05-18 22:23:51,511 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:51,511 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:54,140 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:54,140 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:23:58,821 - INFO - Pass 0, trainbatch 210, loss 0.18364395201206207, acc1 0.9090909361839294, time 0.55 sec
2019-05-18 22:23:58,821 - [line:451] - INFO: Pass 0, trainbatch 210, loss 0.18364395201206207, acc1 0.9090909361839294, time 0.55 sec
2019-05-18 22:24:01,031 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:01,031 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:06,214 - INFO - Pass 0, trainbatch 220, loss 0.2623864710330963, acc1 0.9090909361839294, time 0.55 sec
2019-05-18 22:24:06,214 - [line:451] - INFO: Pass 0, trainbatch 220, loss 0.2623864710330963, acc1 0.9090909361839294, time 0.55 sec
2019-05-18 22:24:09,970 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:09,970 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:12,589 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:12,589 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:15,967 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:15,967 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:19,255 - INFO - Pass 0, trainbatch 230, loss 0.17568445205688477, acc1 1.0, time 0.57 sec
2019-05-18 22:24:19,255 - [line:451] - INFO: Pass 0, trainbatch 230, loss 0.17568445205688477, acc1 1.0, time 0.57 sec
2019-05-18 22:24:19,259 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:19,259 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:21,415 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:24:21,415 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:24:26,706 - INFO - current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:26,706 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 1
2019-05-18 22:24:28,955 - INFO - current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:24:28,955 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 2
2019-05-18 22:24:30,817 - INFO - current acc1 1.0 meets good 0.92, successive count 3
2019-05-18 22:24:30,817 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 3
2019-05-18 22:24:32,894 - INFO - current acc1 1.0 meets good 0.92, successive count 4
2019-05-18 22:24:32,894 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 4
2019-05-18 22:24:34,986 - INFO - current acc1 1.0 meets good 0.92, successive count 5
2019-05-18 22:24:34,986 - [line:455] - INFO: current acc1 1.0 meets good 0.92, successive count 5
2019-05-18 22:24:36,412 - INFO - end training
2019-05-18 22:24:36,412 - [line:462] - INFO: end training
2019-05-18 22:24:36,414 - INFO - training till last epcho, end training
2019-05-18 22:24:36,414 - [line:476] - INFO: training till last epcho, end training

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, 512, 512]     
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
    p = min(target_size[2] / width, target_size[1] / height)
    resized_h = int(height * p)
    resized_w = int(width * p)
    img = img.resize((resized_w, resized_h), Image.BILINEAR)
    w_start = (resized_w - target_size[2]) / 2     
    h_start = (resized_h - 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 = resize_img(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:738 cost time:132.88 sec predict accuracy:0.8116531165311653

!tar -cf densenet.tar ./freeze-model
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!