精华内容
下载资源
问答
  • PaddleHub是飞桨生态的预训练模型应用工具,开发者可以便捷地使用高质量的预训练模型结合Fine-tune API快速完成模型迁移到部署的全流程工作。PaddleHub提供的预训练模型涵盖了图像分类、目标检测、词法分析、语义...
  • PaddleHub是飞桨生态的预训练模型应用工具,开发者可以便捷地使用高质量的预训练模型结合Fine-tune API快速完成模型迁移到部署的全流程工作。PaddleHub提供的预训练模型涵盖了图像分类、目标检测、词法分析、语义...
  • PaddleHub是飞桨生态的预训练模型应用工具,开发者可以便捷地使用高质量的预训练模型结合Fine-tune API快速完成模型迁移到部署的全流程工作。PaddleHub提供的预训练模型涵盖了图像分类、目标检测、词法分析、语义...
  • Py之paddlehubpaddlehub的简介、安装、使用方法之详细攻略 目录 paddlehub的简介 paddlehub的安装 paddlehub的使用方法 1、使用PaddleHub下载数据集、预训练模型等 paddlehub的简介  PaddleHub是飞桨预训练模型...
  • 【飞桨】手把手教你用paddlehub在自己数据集上做迁移学习paddlehub数据集运行环境1.数据分析2.数据处理3.训练配置4.开始训练5.结果预测 paddlehub paddlehub是百度paddlepaddle深度学习框架提供的配套预训练模型应用...

    paddlehub

    paddlehub是百度paddlepaddle深度学习框架提供的配套预训练模型应用管理工具,提供Python API和命令行两种调用模式,目前的预训练模型覆盖了图像分类、语义模型、视频分类、图像生成、图像分割、文本审核、关键点检测等业界主流模型,功能十分强大。Github地址:https://github.com/PaddlePaddle/PaddleHub
    在这里插入图片描述

    paddlehub的强大,但是官方提供做自己数据集迁移学习的demo较少,目前网上提供资料也不多且较散,这里我就以我自己的一个简单的project为例,给大家讲解一下paddlehub的迁移学习使用入门。

    数据集

    完整项目地址:https://aistudio.baidu.com/aistudio/projectdetail/782743
    数据集可在kaggle官网下载,但有的同学嫌翻墙麻烦下载慢,我也在百度网盘和aistudio上放了一份:
    百度网盘:链接: https://pan.baidu.com/s/12RbvTpOfUL8eM9vNwWW72A提取码: 4fe4
    aistudio:https://aistudio.baidu.com/aistudio/datasetdetail/38429
    kaggle:https://www.kaggle.com/c/state-farm-distracted-driver-detection

    运行环境

    paddlepaddle 1.8.0 + paddlehub 1.8.4

    1.数据分析

    !unzip -o -q data/data38429/distracted_driver_detection.zip
    print("done")
    

    解压数据集,可以看到最后生成:train文件夹+test文件+driver_imgs_list.csv

    我们先来分析一下mgs_list.csv文件,打开可以看到共有三个信息:

    • subject:拍摄的司机编号
    • classname:分心行为对应的标签
    • img:对应的图片数据名
      先来看看司机数据的数据分布:
    #csv文件路径
    dir = "driver_imgs_list.csv"
    
    #读取csv中subject的数据
    #subject对应的是司机的编号
    img_csv = os.path.join(dir)
    csv_data = pd.read_csv(img_csv)
    driver_name = csv_data['subject']
    
    #储存所有司机的编号
    names = []
    for name in driver_name:
        if(name not in names):
            names.append(name)
    
    #计算每个司机有多少张图片
    driver_num = csv_data.groupby('subject')['img'].count()
    
    #显示每个司机图片数量
    print(driver_num)
    
    #显示总共多少个司机
    print("drivers count = {}".format(len(names)))
    
    #可视化
    plt.bar(x=names, height=driver_num, width=0.8)
    plt.show()
    

    从输出可以看到,司机一共是26个,每个司机的数据分布并没有一个明显的特征,下面再分析分析10个分类的样本数据

    class_name = csv_data['classname']
    
    #储存所有类别
    class_names = []
    for name in class_name:
        if(name not in class_names):
            class_names.append(name)
    
    #计算每个类别有多少张图片
    class_num = csv_data.groupby('classname')['img'].count()
    
    #显示每个类别图片数量
    print(class_num)
    
    #显示总共多少类
    print("class count = {}".format(len(class_names)))
    
    #可视化
    plt.bar(x=class_names, height=class_num, width=0.8)
    plt.show()
    

    从输出结果可以看到,分类是c0-c9共十个类别,且样本数分布较为均匀,诶很奈斯。

    提供的数据集中除了储存标注信息的csv文件外,只有train文件夹+test文件,无val集,所以下一步要对数据进行处理,划分出val集。

    2.数据处理

    因为提供的数据集中只有train和test无val集,所以人为的在train集中划分一部分数据作为val集并生成储存图片位置与类别的txt文件

    path = "train/"
    folders_name = os.listdir(path)
    
    a = open("train_list.txt", "w")
    b = open("val_list.txt", "w")
    
    count = 0
    val_count = 0
    train_count = 0
    
    for name in folders_name:
        image_names = os.listdir(path+name)
        for img_name in image_names:
            if(count % 20 == 0):
                b.write(path+name+"/"+img_name+name.replace("c"," ")+'\n')
                val_count = val_count + 1
            else:
                a.write(path+name+"/"+img_name+name.replace("c"," ")+'\n')
                train_count = train_count + 1
            count = count + 1
    
    a.close()
    b.close()
    print("train_list生成完毕,train数据集共{}个数据".format(train_count))
    print("val_list生成完毕,val数据集共{}个数据".format(val_count))
    print("合计{}个数据".format(count))
    

    将test集图片信息写入predict_list.txt文件中

    f = open("predict_list.txt", "w")
    predict_data = []
    
    path = "test/"
    folders_name = os.listdir(path)
    for name in folders_name:
        f.write(path+name+'\n')
        predict_data.append(path+name)
    print("predict_list.txt文件成功生成")
    f.close()
    

    生成储存标签信息的label_list.txt文件

    w = open("label_list.txt", "w")
    path = "train/"
    folders_name = os.listdir(path)
    for name in folders_name:
        w.write(name+'\n')
    print("label_list.txt文件成功生成")
    w.close()
    

    3.训练配置

    装载选中的模型(这边主要是举例,所以随便选的加载的模型)

    import paddlehub as hub
    module = hub.Module(name="mobilenet_v2_imagenet")
    input_dict, output_dict, program = module.context(trainable=True)
    

    定义了一个DemoDataset读取自己的数据集

    • train_list_file:训练集

    • validate_list_file:val集

    • test_list_file:测试集

    • predict_file:实际中做的要预测的数据

    因为没有predict_file就可以注释掉不管他了

    from paddlehub.dataset.base_cv_dataset import BaseCVDataset
       
    class DemoDataset(BaseCVDataset):	
       def __init__(self):	
           # 数据集存放位置
           self.dataset_dir = ""
           super(DemoDataset, self).__init__(
               base_path=self.dataset_dir,
               train_list_file="train_list.txt",
               validate_list_file="val_list.txt",
               test_list_file="predict_list.txt",
               #predict_file="predict_list.txt",
               label_list_file="label_list.txt",
               )
    dataset = DemoDataset()
    

    这里是于图像分类问题,所以选择ImageClassificationReader:图像分类数据预处理器,会修改输入数据集图像的尺寸、进行标准化处理、图像增广处理等操作。

    data_reader = hub.reader.ImageClassificationReader(
        image_width=module.get_expected_image_width(),
        image_height=module.get_expected_image_height(),
        images_mean=module.get_pretrained_images_mean(),
        images_std=module.get_pretrained_images_std(),
        dataset=dataset)
    

    配置训练的一些超参数,在strategy里可以配置很多优化的参数,这里的优化策略选取的默认优化策略。其对应参数如下:

    • learning_rate: 全局学习率。默认为1e-4。

    • optimizer_name: 优化器名称。默认adam。

    • regularization_coeff: 正则化的λ参数。默认为1e-3。

    config = hub.RunConfig(
        use_cuda=True,                               #是否使用GPU训练
        num_epoch=10,                                #Fine-tune的轮数
        checkpoint_dir="source",                     #模型checkpoint保存路径
        batch_size=16,                               #训练的批大小
        eval_interval=100,                           #模型评估的间隔
        strategy=hub.finetune.strategy.DefaultFinetuneStrategy())  #Fine-tune优化策略;
    

    把设置好的参数写入并建立ImageClassifierTask任务

    feature_map = output_dict["feature_map"]
    feed_list = [input_dict["image"].name]
    
    #ImageClassifierTask:通用的分类任务Task,该Task基于输入的特征,添加一个或多个全连接层来创建一个分类任务用于Fine-tune
    task = hub.ImageClassifierTask(
        data_reader=data_reader,           #提供数据的reader
        feed_list=feed_list,               #待feed变量的名字列表
        feature=feature_map,               #输入的特征矩阵
        num_classes=dataset.num_labels,    #分类任务的类别数量
        config=config)                     #运行配置
    
    

    这两步可能会报的错误:

    1. AttributeError: ‘Parameter’ object has no attribute
      ‘gradient_clip_attr’
      paddlehub不是最新版导致的,建议升级paddlehub至1.8.2再试一试,如果还不行就检查一下自己paddlepaddle版本是不是1.8以上的
    2. Error: Blocking queue is killed because the data reader raises an
      exception 这个报错问题出在reader上,在这个报错向上找,如果能找到类似:ValueError: invalid
      literal for int() with base 10: 'c2’的语句,就铁锤是因为便签设置的问题
      我这里是十分类问题,对应标签是c0-c9十个,但是最后分类对应的标签应该是0-9这种整数,在前面生成txt那里进行修改就可以解决了

    4.开始训练

    激活task的finetune和eval

    run_states = task.finetune_and_eval()
    

    5.结果预测

    调用task.predict来进行预测,最后返回的预测结果是一个batch一个batch大小返回的

    import numpy as np
    label_map = dataset.label_dict()
    index = 0
    run_states = task.predict(data=predict_data)
    results = [run_state.run_results for run_state in run_states]
    
    images_data= []
    res_data= []
    for batch_result in results:
        batch_result = np.argmax(batch_result, axis=2)[0]
        for result in batch_result:
            index += 1
            result = label_map[result]
            print("input %i is %s, and the predict result is %s" %
                  (index, predict_data[index - 1], result))
            images_data.append(predict_data[index - 1])
            res_data.append(result)
    print(res_data)
    

    paddlehub功能强大学会使用可以给自己做项目省很多精力,希望这篇博客能解决大家刚开始使用paddlehub的一些问题。
    项目地址如下,欢迎大家fork、讨论
    项目地址:https://aistudio.baidu.com/aistudio/projectdetail/782743

    展开全文
  • 将训练保存的模型转化为PaddleHub Module并完成一键加载一、训练鲜花识别模型1. 解压鲜花数据集及预训练参数2. 数据预处理3. 模型训练二、整理成PaddleHub Module格式1. 必要的目录与文件2.infer_model3. vocab.txt4...

    本教程旨在把帮助各位开发者把在ai studio上做的精品项目转入到PaddleHub的Module里,使其具备一键预测的能力。

    运行该项目请到AI Studio:
    https://aistudio.baidu.com/aistudio/projectdetail/1259178

    一、训练鲜花识别模型

    原项目来自开发者笨笨的——图像分类-VGG,该项目使用公开的鲜花据集,数据集压缩包里包含五个文件夹,每个文件夹一种花卉。分别是雏菊,蒲公英,玫瑰,向日葵,郁金香,每种各690-890张图片,网络采用VGG

    1. 解压鲜花数据集及预训练参数

    # 解压花朵数据集
    !cd data/data2815 && unzip -qo flower_photos.zip
    
    # 解压预训练参数
    !cd data/data6489 && unzip -qo VGG16_pretrained.zip
    

    2. 数据预处理

    !python work/DataPreprocessing.py
    
    ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
    

    3. 模型训练

    本项目的主要目的是转换Module,所以训练的epochs设为1

    !python work/train.py
    
    2020-11-24 19:06:16,867-INFO: create prog success
    2020-11-24 19:06:16,867 - train.py[line:460] - INFO: create prog success
    2020-11-24 19:06:16,867-INFO: train config: {'input_size': [3, 224, 224], 'class_dim': 5, 'image_count': 2955, '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': False, 'pretrained': True, 'pretrained_dir': 'data/data6489/VGG16_pretrained', 'mode': 'train', 'num_epochs': 1, 'train_batch_size': 24, 'mean_rgb': [127.5, 127.5, 127.5], 'use_gpu': True, '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.0005, '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.0005, '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.0005, '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.0005}}
    2020-11-24 19:06:16,867 - train.py[line:461] - INFO: train config: {'input_size': [3, 224, 224], 'class_dim': 5, 'image_count': 2955, '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': False, 'pretrained': True, 'pretrained_dir': 'data/data6489/VGG16_pretrained', 'mode': 'train', 'num_epochs': 1, 'train_batch_size': 24, 'mean_rgb': [127.5, 127.5, 127.5], 'use_gpu': True, '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.0005, '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.0005, '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.0005, '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.0005}}
    2020-11-24 19:06:16,868-INFO: build input custom reader and data feeder
    2020-11-24 19:06:16,868 - train.py[line:462] - INFO: build input custom reader and data feeder
    2020-11-24 19:06:16,869-INFO: build newwork
    2020-11-24 19:06:16,869 - train.py[line:475] - INFO: build newwork
    W1124 19:06:18.083617   144 device_context.cc:236] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 9.0
    W1124 19:06:18.087874   144 device_context.cc:244] device: 0, cuDNN Version: 7.3.
    2020-11-24 19:06:19,710-INFO: load params from pretrained model
    2020-11-24 19:06:19,710 - train.py[line:449] - INFO: load params from pretrained model
    2020-11-24 19:06:21,383-INFO: current pass: 0, start read image
    2020-11-24 19:06:21,383 - train.py[line:504] - INFO: current pass: 0, start read image
    2020-11-24 19:06:24,815-INFO: Pass 0, trainbatch 10, loss 1.6219388246536255, acc1 0.0833333358168602, time 0.14 sec
    2020-11-24 19:06:24,815 - train.py[line:519] - INFO: Pass 0, trainbatch 10, loss 1.6219388246536255, acc1 0.0833333358168602, time 0.14 sec
    2020-11-24 19:06:28,441-INFO: Pass 0, trainbatch 20, loss 1.558526635169983, acc1 0.4583333432674408, time 0.15 sec
    2020-11-24 19:06:28,441 - train.py[line:519] - INFO: Pass 0, trainbatch 20, loss 1.558526635169983, acc1 0.4583333432674408, time 0.15 sec
    2020-11-24 19:06:31,856-INFO: Pass 0, trainbatch 30, loss 1.574629783630371, acc1 0.3333333432674408, time 0.14 sec
    2020-11-24 19:06:31,856 - train.py[line:519] - INFO: Pass 0, trainbatch 30, loss 1.574629783630371, acc1 0.3333333432674408, time 0.14 sec
    2020-11-24 19:06:35,593-INFO: Pass 0, trainbatch 40, loss 1.5624138116836548, acc1 0.5, time 0.14 sec
    2020-11-24 19:06:35,593 - train.py[line:519] - INFO: Pass 0, trainbatch 40, loss 1.5624138116836548, acc1 0.5, time 0.14 sec
    2020-11-24 19:06:39,171-INFO: Pass 0, trainbatch 50, loss 1.6100339889526367, acc1 0.1666666716337204, time 0.14 sec
    2020-11-24 19:06:39,171 - train.py[line:519] - INFO: Pass 0, trainbatch 50, loss 1.6100339889526367, acc1 0.1666666716337204, time 0.14 sec
    2020-11-24 19:06:39,172-INFO: temp save 50 batch train result, current acc1 0.1666666716337204
    2020-11-24 19:06:39,172 - train.py[line:538] - INFO: temp save 50 batch train result, current acc1 0.1666666716337204
    2020-11-24 19:06:46,603-INFO: Pass 0, trainbatch 60, loss 1.6188973188400269, acc1 0.2083333283662796, time 0.14 sec
    2020-11-24 19:06:46,603 - train.py[line:519] - INFO: Pass 0, trainbatch 60, loss 1.6188973188400269, acc1 0.2083333283662796, time 0.14 sec
    2020-11-24 19:06:50,057-INFO: Pass 0, trainbatch 70, loss 1.6400723457336426, acc1 0.125, time 0.14 sec
    2020-11-24 19:06:50,057 - train.py[line:519] - INFO: Pass 0, trainbatch 70, loss 1.6400723457336426, acc1 0.125, time 0.14 sec
    2020-11-24 19:06:53,692-INFO: Pass 0, trainbatch 80, loss 1.5995646715164185, acc1 0.25, time 0.14 sec
    2020-11-24 19:06:53,692 - train.py[line:519] - INFO: Pass 0, trainbatch 80, loss 1.5995646715164185, acc1 0.25, time 0.14 sec
    2020-11-24 19:06:57,141-INFO: Pass 0, trainbatch 90, loss 1.539711833000183, acc1 0.3333333432674408, time 0.14 sec
    2020-11-24 19:06:57,141 - train.py[line:519] - INFO: Pass 0, trainbatch 90, loss 1.539711833000183, acc1 0.3333333432674408, time 0.14 sec
    2020-11-24 19:07:00,644-INFO: Pass 0, trainbatch 100, loss 1.593304991722107, acc1 0.125, time 0.14 sec
    2020-11-24 19:07:00,644 - train.py[line:519] - INFO: Pass 0, trainbatch 100, loss 1.593304991722107, acc1 0.125, time 0.14 sec
    2020-11-24 19:07:00,645-INFO: temp save 100 batch train result, current acc1 0.125
    2020-11-24 19:07:00,645 - train.py[line:538] - INFO: temp save 100 batch train result, current acc1 0.125
    2020-11-24 19:07:08,069-INFO: Pass 0, trainbatch 110, loss 1.5976566076278687, acc1 0.3333333432674408, time 0.14 sec
    2020-11-24 19:07:08,069 - train.py[line:519] - INFO: Pass 0, trainbatch 110, loss 1.5976566076278687, acc1 0.3333333432674408, time 0.14 sec
    2020-11-24 19:07:11,569-INFO: Pass 0, trainbatch 120, loss 1.6223376989364624, acc1 0.125, time 0.14 sec
    2020-11-24 19:07:11,569 - train.py[line:519] - INFO: Pass 0, trainbatch 120, loss 1.6223376989364624, acc1 0.125, time 0.14 sec
    2020-11-24 19:07:12,698-INFO: training till last epcho, end training
    2020-11-24 19:07:12,698 - train.py[line:544] - INFO: training till last epcho, end training
    

    二、整理成PaddleHub Module格式

    PaddleHub Module是使用PaddleHub的基础。其可以通过指定名称即可方便地完成一键加载,如加载预训练模型ERNIE仅需一行代码即可完成,hub.Module(name=‘ernie’),省去了复杂的网络结构代码以及参数加载的繁琐过程。

    1. 必要的目录与文件

    创建一个大目录,并在该目录下分别创建__init__.py、module.py、processor.py、net.py等文件

    目录名称即Module的名称,如这里我起名为VGG16:

    VGG16/
    ├── assets # 资源文件夹
    │   ├── infer_model # 模型文件
    │   └── vocab.txt # 词汇表文件
    ├── data_feed.py
    ├── __init__.py # 空文件
    ├── module.py # 主模块,提供Module的实现代码
    ├── net.py # 网络框架的实现
    └── processor.py # 辅助模块,如提供词表加载的方法
    

    2.infer_model

    infer_model下存放的是使用fluid.io.save_inference_model保存的模型文件

    # 创建必要的文件夹
    !mkdir -p VGG16/assets/infer_model
    
    # 将模型文件复制到Module指定目录下
    !cp -r freeze-model/* VGG16/assets/infer_model
    

    3. vocab.txt

    在图像分类任务中,词汇表文件存放的是每一个类别

    vocab = open("VGG16/assets/vocab.txt", "w")
    vocab.writelines(['daisy\n', 'dandelion\n', 'roses\n', 'sunflowers\n', 'tulips\n'])
    vocab.close()
    

    4. __ init__.py

    __ init__.py是一个空文件,直接创建即可

    init = open("VGG16/__init__.py", "w")
    

    5. processor.py

    辅助模块,在processor.py中实现词汇表的读取,以及文本输入模型之前需要做的预处理

    本案例中用于加载vocab.txt,下面是代码示例:

    def load_label_info(file_path):
        with open(file_path, 'r') as fr:
            return fr.read().split("\n")[:-1]
    
    processor = open("VGG16/processor.py", "w")
    

    6.net.py

    网路框架的实现,模型训练时的网络,下面是代码示例:

    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function
    
    from paddle import fluid
    
    class VGGNet(object):
        """
        vgg的网络类
        """
        def __init__(self, layers=16):
            """
            vgg网络构造函数
            :param layers:
            """
            self.layers = layers
    
        def name(self):
            """
            返回网络名字
            :return:
            """
            return 'vgg-net'
    
        def net(self, input, class_dim=1000):
            layers = self.layers
            vgg_spec = {
                11: ([1, 1, 2, 2, 2]),
                13: ([2, 2, 2, 2, 2]),
                16: ([2, 2, 3, 3, 3]),
                19: ([2, 2, 4, 4, 4])
            }
            assert layers in vgg_spec.keys(), \
                "supported layers are {} but input layer is {}".format(vgg_spec.keys(), layers)
    
            nums = vgg_spec[layers]
            conv1 = self.conv_block(input, 64, nums[0], name="conv1_")
            conv2 = self.conv_block(conv1, 128, nums[1], name="conv2_")
            conv3 = self.conv_block(conv2, 256, nums[2], name="conv3_")
            conv4 = self.conv_block(conv3, 512, nums[3], name="conv4_")
            conv5 = self.conv_block(conv4, 512, nums[4], name="conv5_")
    
            fc_dim = 4096
            fc_name = ["fc6", "fc7", "fc8"]
            fc1 = fluid.layers.fc(
                input=conv5,
                size=fc_dim,
                act='relu',
                param_attr=fluid.param_attr.ParamAttr(name=fc_name[0] + "_weights"),
                bias_attr=fluid.param_attr.ParamAttr(name=fc_name[0] + "_offset"))
            fc1 = fluid.layers.dropout(x=fc1, dropout_prob=0.5)
            fc2 = fluid.layers.fc(
                input=fc1,
                size=fc_dim,
                act='relu',
                param_attr=fluid.param_attr.ParamAttr(name=fc_name[1] + "_weights"),
                bias_attr=fluid.param_attr.ParamAttr(name=fc_name[1] + "_offset"))
            fc2 = fluid.layers.dropout(x=fc2, dropout_prob=0.5)
            out = fluid.layers.fc(
                input=fc2,
                size=class_dim,
                act='softmax',
                param_attr=fluid.param_attr.ParamAttr(name=fc_name[2] + "_weights"),
                bias_attr=fluid.param_attr.ParamAttr(name=fc_name[2] + "_offset"))
    
            return out
    
        def conv_block(self, input, num_filter, groups, name=None):
            conv = input
            for i in range(groups):
                conv = fluid.layers.conv2d(
                    input=conv,
                    num_filters=num_filter,
                    filter_size=3,
                    stride=1,
                    padding=1,
                    act='relu',
                    param_attr=fluid.param_attr.ParamAttr(
                        name=name + str(i + 1) + "_weights"),
                    bias_attr=fluid.param_attr.ParamAttr(
                        name=name + str(i + 1) + "_offset"))
            return fluid.layers.pool2d(
                input=conv, pool_size=2, pool_type='max', pool_stride=2)
    
    net = open("VGG16/net.py", "w")
    

    7. data_feed.py

    处理图像,以便送入网络进行预测,下面是参考代码:

    from __future__ import absolute_import
    from __future__ import print_function
    from __future__ import division
    
    import os
    from collections import OrderedDict
    
    import cv2
    import numpy as np
    from PIL import Image, ImageEnhance
    from paddle import fluid
    
    DATA_DIM = 224
    img_mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
    img_std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
    
    
    def resize_short(img, target_size):
        percent = float(target_size) / min(img.size[0], img.size[1])
        resized_width = int(round(img.size[0] * percent))
        resized_height = int(round(img.size[1] * percent))
        img = img.resize((resized_width, resized_height), Image.LANCZOS)
        return img
    
    
    def crop_image(img, target_size, center):
        width, height = img.size
        size = target_size
        if center == True:
            w_start = (width - size) / 2
            h_start = (height - size) / 2
        else:
            w_start = np.random.randint(0, width - size + 1)
            h_start = np.random.randint(0, height - size + 1)
        w_end = w_start + size
        h_end = h_start + size
        img = img.crop((w_start, h_start, w_end, h_end))
        return img
    
    
    def process_image(img):
        img = resize_short(img, target_size=256)
        img = crop_image(img, target_size=DATA_DIM, center=True)
        if img.mode != 'RGB':
            img = img.convert('RGB')
        #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255
        img -= img_mean
        img /= img_std
        return img
    
    
    def test_reader(paths=None, images=None):
        """data generator
        :param paths: path to images.
        :type paths: list, each element is a str
        :param images: data of images, [N, H, W, C]
        :type images: numpy.ndarray
        """
        img_list = []
        if paths:
            assert os.path.isfile(
                paths), "The {} isn't a valid file path.".format(img_path)
            img = Image.open(paths)
            #img = cv2.imread(img_path)
            img_list.append(img)
        if images is not None:
            for img in images:
                img_list.append(Image.fromarray(np.uint8(img)))
        for im in img_list:
            im = process_image(im)
            yield im
    

    8. module.py

    module.py文件为Module的入口代码所在,需要在其中实现预测逻辑。

    module = open("VGG16/module.py", "w")
    

    引入必要的头文件

    当引用Module中模块时,需要输入全路径,如VGG16.net

    import os
    import ast
    import argparse
    
    import numpy as np
    import paddlehub as hub
    import paddle.fluid as fluid
    from paddlehub.module.module import moduleinfo, runnable
    from paddle.fluid.core import PaddleTensor, AnalysisConfig, create_paddle_predictor
    from paddlehub.io.parser import txt_parser
    

    填写模型基本信息

    一个PaddleHub Module,其基本信息如下:

    @moduleinfo(
        name="VGG16",
        version="1.0.0",
        type="cv/classification",
        summary=
        "VGG16 is a image classfication model trained with Flower dataset.",
        author="paddlepaddle",
        author_email="paddle-dev@baidu.com")
    

    实现逻辑预测

    module.py中需要有一个继承了hub.Module的类存在,该类负责实现预测逻辑,并使用moduleinfo填写基本信息。 当使用hub.Module(name=“senta_test”)加载Module时,PaddleHub会自动创建该类的对象并返回。

    class VGG16(hub.Module):
        def _initialize(self):
            self.default_pretrained_model_path = os.path.join(self.directory, "assets/infer_model") # 模型文件的路径
            self.label_names = load_label_info(os.path.join(self.directory, "assets/vocab.txt")) # 图像分类任务的标签
            self.infer_prog = None
            self.pred_out = None
            self._set_config()
    
        def get_expected_image_width(self):
            return 224
    
        def get_expected_image_height(self):
            return 224
    
        def get_pretrained_images_mean(self):
            im_mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3)
            return im_mean
    
        def get_pretrained_images_std(self):
            im_std = np.array([0.229, 0.224, 0.225]).reshape(1, 3)
            return im_std
    
        def _set_config(self):
            """
            predictor config setting
            """
            cpu_config = AnalysisConfig(self.default_pretrained_model_path)
            cpu_config.disable_glog_info()
            cpu_config.disable_gpu()
            cpu_config.switch_ir_optim(False)
            self.cpu_predictor = create_paddle_predictor(cpu_config)
    
            try:
                _places = os.environ["CUDA_VISIBLE_DEVICES"]
                int(_places[0])
                use_gpu = True
            except:
                use_gpu = False
            if use_gpu:
                gpu_config = AnalysisConfig(self.default_pretrained_model_path)
                gpu_config.disable_glog_info()
                gpu_config.enable_use_gpu(memory_pool_init_size_mb=500, device_id=0)
                self.gpu_predictor = create_paddle_predictor(gpu_config)
    
        def context(self,
                    input_image=None,
                    trainable=True,
                    pretrained=True,
                    param_prefix='',
                    get_prediction=False,
                    extra_block_filters=((256, 512, 1, 2, 3), (128, 256, 1, 2, 3),
                                         (128, 256, 0, 1, 3), (128, 256, 0, 1, 3)),
                    normalizations=(20., -1, -1, -1, -1, -1)):
            """Distill the Head Features, so as to perform transfer learning.
            :param input_image: image tensor.
            :type input_image: <class 'paddle.fluid.framework.Variable'>
            :param trainable: whether to set parameters trainable.
            :type trainable: bool
            :param pretrained: whether to load default pretrained model.
            :type pretrained: bool
            :param param_prefix: the prefix of parameters.
            :type param_prefix: str
            :param get_prediction: whether to get prediction.
            :type get_prediction: bool
            :param extra_block_filters: in each extra block, params:
                [in_channel, out_channel, padding_size, stride_size, filter_size]
            :type extra_block_filters: list
            :param normalizations: params list of init scale in l2 norm, skip init
                scale if param is -1.
            :type normalizations: list
            """
            context_prog = input_image.block.program if input_image else fluid.Program(
            )
            startup_program = fluid.Program()
            with fluid.program_guard(context_prog, startup_program):
                image = input_image if input_image else fluid.data(
                    name='image',
                    shape=[-1, 3, 224, 224],
                    dtype='float32',
                    lod_level=0)
    
                backbone = VGGNet(layers=16)
                out = backbone.net(input=image, class_dim=5)
                # out = backbone(image)
                inputs = {'image': image}
                if get_prediction:
                    outputs = {'pred_out': out}
                else:
                    outputs = {'body_feats': out}
    
                place = fluid.CPUPlace()
                exe = fluid.Executor(place)
                if pretrained:
    
                    def _if_exist(var):
                        return os.path.exists(
                            os.path.join(self.default_pretrained_model_path,
                                         var.name))
    
                    if not param_prefix:
                        fluid.io.load_vars(
                            exe,
                            self.default_pretrained_model_path,
                            main_program=context_prog,
                            predicate=_if_exist)
                else:
                    exe.run(startup_program)
                return inputs, outputs, context_prog
    
        def classification(self,
                           paths=None,
                           images=None,
                           use_gpu=False,
                           batch_size=1,
                           top_k=1):
            """API of Classification.
            :param paths: the path of images.
            :type paths: list, each element is correspond to the path of an image.
            :param images: data of images, [N, H, W, C]
            :type images: numpy.ndarray
            :param use_gpu: whether to use gpu or not.
            :type use_gpu: bool
            :param batch_size: bathc size.
            :type batch_size: int
            :param top_k: result of top k
            :type top_k: int
            """
            if self.infer_prog is None:
                inputs, outputs, self.infer_prog = self.context(
                    trainable=False, pretrained=True, get_prediction=True)
                self.infer_prog = self.infer_prog.clone(for_test=True)
                self.pred_out = outputs['pred_out']
            place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
            exe = fluid.Executor(place)
            all_images = []
            paths = paths if paths else []
            for yield_data in test_reader(paths, images):
                all_images.append(yield_data)
    
            images_num = len(all_images)
            loop_num = int(np.ceil(images_num / batch_size))
            res_list = []
            top_k = max(min(top_k, 1000), 1)
            for iter_id in range(loop_num):
                batch_data = []
                handle_id = iter_id * batch_size
                for image_id in range(batch_size):
                    try:
                        batch_data.append(all_images[handle_id + image_id])
                    except:
                        pass
                batch_data = np.array(batch_data).astype('float32')
                data_tensor = PaddleTensor(batch_data.copy())
                if use_gpu:
                    result = self.gpu_predictor.run([data_tensor])
                else:
                    result = self.cpu_predictor.run([data_tensor])
                for i, res in enumerate(result[0].as_ndarray()):
                    res_dict = {}
                    pred_label = np.argsort(res)[::-1][:top_k]
                    for k in pred_label:
                        class_name = self.label_names[int(k)].split(',')[0]
                        max_prob = res[k]
                        res_dict[class_name] = max_prob
                    res_list.append(res_dict)
            return res_list
    
        def add_module_config_arg(self):
            """
            Add the command config options
            """
            self.arg_config_group.add_argument(
                '--use_gpu',
                type=ast.literal_eval,
                default=False,
                help="whether use GPU or not")
    
            self.arg_config_group.add_argument(
                '--batch_size',
                type=int,
                default=1,
                help="batch size for prediction")
    
        def add_module_input_arg(self):
            """
            Add the command input options
            """
            self.arg_input_group.add_argument(
                '--input_path', type=str, default=None, help="input data")
    
            self.arg_input_group.add_argument(
                '--input_file',
                type=str,
                default=None,
                help="file contain input data")
    
        def check_input_data(self, args):
            input_data = []
            if args.input_path:
                input_data = [args.input_path]
            elif args.input_file:
                if not os.path.exists(args.input_file):
                    raise RuntimeError("File %s is not exist." % args.input_file)
                else:
                    input_data = txt_parser.parse(args.input_file, use_strip=True)
            return input_data
    
        @runnable
        def run_cmd(self, argvs):
            self.parser = argparse.ArgumentParser(
                description="Run the {}".format(self.name),
                prog="hub run {}".format(self.name),
                usage='%(prog)s',
                add_help=True)
            self.arg_input_group = self.parser.add_argument_group(
                title="Input options", description="Input data. Required")
            self.arg_config_group = self.parser.add_argument_group(
                title="Config options",
                description=
                "Run configuration for controlling module behavior, not required.")
            self.add_module_config_arg()
    
            self.add_module_input_arg()
            args = self.parser.parse_args(argvs)
            input_data = self.check_input_data(args)
            if len(input_data) == 0:
                self.parser.print_help()
                exit(1)
            else:
                for image_path in input_data:
                    if not os.path.exists(image_path):
                        raise RuntimeError(
                            "File %s or %s is not exist." % image_path)
            return self.classification(
                paths=input_data, use_gpu=args.use_gpu, batch_size=args.batch_size)
    
    # 查看目录结构
    !tree VGG16/
    
    VGG16/
    ├── assets
    │   ├── infer_model
    │   │   ├── conv1_1_offset
    │   │   ├── conv1_1_weights
    │   │   ├── conv1_2_offset
    │   │   ├── conv1_2_weights
    │   │   ├── conv2_1_offset
    │   │   ├── conv2_1_weights
    │   │   ├── conv2_2_offset
    │   │   ├── conv2_2_weights
    │   │   ├── conv3_1_offset
    │   │   ├── conv3_1_weights
    │   │   ├── conv3_2_offset
    │   │   ├── conv3_2_weights
    │   │   ├── conv3_3_offset
    │   │   ├── conv3_3_weights
    │   │   ├── conv4_1_offset
    │   │   ├── conv4_1_weights
    │   │   ├── conv4_2_offset
    │   │   ├── conv4_2_weights
    │   │   ├── conv4_3_offset
    │   │   ├── conv4_3_weights
    │   │   ├── conv5_1_offset
    │   │   ├── conv5_1_weights
    │   │   ├── conv5_2_offset
    │   │   ├── conv5_2_weights
    │   │   ├── conv5_3_offset
    │   │   ├── conv5_3_weights
    │   │   ├── fc6_offset
    │   │   ├── fc6_weights
    │   │   ├── fc7_offset
    │   │   ├── fc7_weights
    │   │   ├── fc8_offset
    │   │   ├── fc8_weights
    │   │   └── __model__
    │   └── vocab.txt
    ├── data_feed.py
    ├── __init__.py
    ├── module.py
    ├── net.py
    ├── processor.py
    └── __pycache__
        ├── data_feed.cpython-37.pyc
        ├── __init__.cpython-37.pyc
        ├── module.cpython-37.pyc
        ├── net.cpython-37.pyc
        └── processor.cpython-37.pyc
    
    3 directories, 44 files
    

    三、 测试Module

    完成Module编写后,我们可以通过以下方式测试该Module

    1. 通过hub.Module(name=…)加载

    将Module安装到本机中,再通过hub.Module(name=…)加载

    !hub install VGG16
    !hub show VGG16
    
      Successfully installed VGG16
    
    +-----------------+----------------------------------------------------+
    |   ModuleName    |VGG16                                               |
    +-----------------+----------------------------------------------------+
    |     Version     |1.1.0                                               |
    +-----------------+----------------------------------------------------+
    |     Summary     |VGG16 is a image classfication model trained with   |
    |                 |Flower dataset.                              |
    +-----------------+----------------------------------------------------+
    |     Author      |paddlepaddle                                        |
    +-----------------+----------------------------------------------------+
    |  Author-Email   |paddle-dev@baidu.com                                |
    +-----------------+----------------------------------------------------+
    |    Location     |/home/aistudio/.paddlehub/modules/VGG16             |
    +-----------------+----------------------------------------------------+
    
    import paddlehub as hub
    
    vgg16_test = hub.Module(name="VGG16")
    
    test_img_path = "data/data2815/tulips/17165583356_38cb1f231d_n.jpg"
    
    # execute predict and print the result
    results = vgg16_test.classification(test_img_path)
    # print(results)
    for result in results:
        print(result)
    
    [32m[2020-11-25 12:37:22,531] [    INFO] - Installing VGG16 module[0m
    [32m[2020-11-25 12:37:22,533] [    INFO] - Module VGG16 already installed in /home/aistudio/.paddlehub/modules/VGG16[0m
    
    
    {'dandelion': 0.24343227}
    

    2. 直接通过hub.Module(directory=…)加载

    import paddlehub as hub
    
    vgg16_test = hub.Module(directory="VGG16/")
    
    test_img_path = "data/data2815/tulips/17165583356_38cb1f231d_n.jpg"
    
    # execute predict and print the result
    results = vgg16_test.classification(test_img_path)
    # print(results)
    for result in results:
        print(result)
    
    {'dandelion': 0.24343227}
    

    在这里插入图片描述

    展开全文
  • PaddleHub创意赛:AI人像抠图及图像合成——基于paddleHub的韦小宝穿越 本项目根据DeepLabv3+模型一键抠图示例(https://aistudio.baidu.com/aistudio/projectdetail/354462),主要采用PaddleHub DeepLabv3+模型...

    PaddleHub创意赛:AI人像抠图及图像合成——基于paddleHub的韦小宝穿越

    本项目根据DeepLabv3+模型一键抠图示例(https://aistudio.baidu.com/aistudio/projectdetail/354462),主要采用PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)和python图像处理库opencv、PIL等完成。

    PaddleHub是百度的深度学习的一部分,它是基于百度的深度学习框架PaddlePaddle开发的预训练模型管理工具。我们可以借助预训练模型更便捷地开展迁移学习工作,通过PaddleHub开发者可以使用高质量的预训练模型结合Fine-tune API快速完成迁移学习到应用部署的全流程工作,其中Fine-tune API网址为:https://github.com/PaddlePaddle/PaddleHub/wiki/PaddleHub-Finetune-API。
    百度提供了飞桨生态下的高质量预训练模型,涵盖了图像分类、目标检测、词法分析、语义模型、情感分析、视频分类、图像生成、图像分割、文本审核、关键点检测等主流模型。

    paddleHub的具体网址为https://www.paddlepaddle.org.cn/hub。
    更详细的描述和内容可以参看该网址中的使用文档和官方课程,其中使用文档为中文,更加方便开发者的学习和使用。同时,也包括大量的具体实例,可以直接使用或根据自己的需求调整应用于自己的领域。

    本例主要使用了PaddleHub中已训练好的人像分割模型:DeepLabv3+模型(deeplabv3p_xception65_humanseg),对《鹿鼎记》中的一个小片段进行了人像抠图,并将分割的人像和任意背景进行图像合成形成具有新的背景的视频,实现了该比赛的一个小的示例;同时由于产生的视频没有声音,故采用了手机软件“剪映”将音频加上。
    注意: 如果您在本地运行该项目示例,需要首先安装PaddleHub。如果您在线运行,需要首先fork该项目示例到AI Studio中。
    本内容参考:https://aistudio.baidu.com/aistudio/projectdetail/377462完成,如有侵权请联系本人删除。

    本示例的具体流程为:

    第一步 安装环境

    安装paddlehub1.6.0版本和和已训练好的人像分割模型deeplabv3p_xception65_humanseg,具体命令为:

    pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
    hub install deeplabv3p_xception65_humanseg==1.0.0
    

    第二步 定义图像合成函数

    2.1 导入包

    import matplotlib.pyplot as plt 
    import matplotlib.image as mpimg 
    from matplotlib import animation
    import cv2
    import paddlehub as hub
    from PIL import Image, ImageSequence, ImageDraw,ImageFont
    from IPython.display import display, HTML
    import numpy as np 
    import imageio
    import os
    

    2.2 加载预训练模型

    module = hub.Module(name="deeplabv3p_xception65_humanseg")
    

    2.3 合成图像函数

    def blend_images(fore_image, base_image, output_path):
    """
    将抠出的人物图像换背景
        fore_image: 前景图片,抠出的人物图片
    base_image: 背景图片
    output_path:合成图片输出路径
        """
        # 读入图片
        base_image = Image.open(base_image).convert('RGB')
        fore_image = Image.open(fore_image).resize(base_image.size)
    
        # 图片加权合成
        scope_map = np.array(fore_image)[:,:,-1] / 255
        scope_map = scope_map[:,:,np.newaxis]
        scope_map = np.repeat(scope_map, repeats=3, axis=2)
        res_image = np.multiply(scope_map, np.array(fore_image)[:,:,:3]) + np.multiply((1-scope_map), np.array(base_image))
        
        #保存图片
        res_image = Image.fromarray(np.uint8(res_image))
        res_image.save(output_path)
        return res_image
    

    第三步:视频处理(拆分-分割-合成-保存)

    3.1 拆视频

    cap = cv2.VideoCapture('image/video/xiaobao3.mp4')
    
    imgs = []
    num = 0
    while(True):
        ret,frame = cap.read() 
        if ret:
            cv2.imwrite('image/video/frame/%d.jpg'%num, frame)
            # img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
            # imgs.append(img_rgb)
            num += 1
        else:
            break
    cap.release()#关闭相机
    

    3.2 人像分割

    frame_path = 'image/video/frame'
    test_img_path = [os.path.join(frame_path, fname) for fname in os.listdir(frame_path)]
    input_dict = {"image": test_img_path}
    
    # execute predict and print the result
    
    results = module.segmentation(data=input_dict, output_dir='image/video/frame_seg/')
    

    3.3 合并输出视频

    output_path = 'image/video'
    humanseg_xiaobao = [filename for filename in os.listdir('image/video/frame_seg/')]
    print(len(humanseg_xiaobao))
    for i, img in enumerate(humanseg_xiaobao):
        if i <= 397:
            img_path = os.path.join('image/video/frame_seg/%d.png' % i)
            output_path_img = output_path + '/blend_out/%d.png' % i
            img = blend_images(img_path, 'image/video/bg/bg-x1.jpg', output_path_img)
    print('hello world')
    
    展开全文
  • PaddleHub是百度飞桨深度学习框架的预训练模型管理和迁移学习工具。 其主要特性包括: 1. 丰富的预训练模型; 2. 模型即软件; 3. Fine-tone API。 PaddleHub项目开源链接:...
  • paddlehub学习

    2020-04-25 20:45:58
    #安装paddlehub !pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting paddlehub==1.6.0 [?25l Downloading ...
    #安装paddlehub
    !pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
    Collecting paddlehub==1.6.0
    [?25l  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/9f/6617c2b8e9c5d847803ae89924b58bccd1b8fb2c98aa00e16531540591f2/paddlehub-1.6.0-py3-none-any.whl (206kB)
    [K     |████████████████████████████████| 215kB 2.7MB/s eta 0:00:01
    [?25hRequirement already satisfied: cma==2.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (2.7.0)
    Requirement already satisfied: six>=1.10.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (1.12.0)
    Requirement already satisfied: tb-paddle in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (0.3.6)
    Requirement already satisfied: chardet==3.0.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (3.0.4)
    Requirement already satisfied: protobuf>=3.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (3.10.0)
    Requirement already satisfied: yapf==0.26.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (0.26.0)
    Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (1.21.0)
    Requirement already satisfied: pyyaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (5.1.2)
    Requirement already satisfied: numpy; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (1.16.4)
    Requirement already satisfied: pandas; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (0.23.4)
    Requirement already satisfied: nltk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (3.4.5)
    Requirement already satisfied: flask>=1.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (1.1.1)
    Requirement already satisfied: gunicorn>=19.10.0; sys_platform != "win32" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (20.0.4)
    Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (4.1.1.26)
    Requirement already satisfied: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (4.1.0)
    Requirement already satisfied: flake8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (3.7.9)
    Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (2.22.0)
    Requirement already satisfied: sentencepiece in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (0.1.85)
    Requirement already satisfied: tensorboard>=1.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (2.1.0)
    Requirement already satisfied: Pillow in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.6.0) (6.2.0)
    Requirement already satisfied: moviepy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tb-paddle->paddlehub==1.6.0) (1.0.1)
    Requirement already satisfied: setuptools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from protobuf>=3.6.0->paddlehub==1.6.0) (41.4.0)
    Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (0.23)
    Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (0.10.0)
    Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (1.4.10)
    Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (16.7.9)
    Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (1.3.0)
    Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (2.0.1)
    Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.6.0) (1.3.4)
    Requirement already satisfied: pytz>=2011k in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub==1.6.0) (2019.3)
    Requirement already satisfied: python-dateutil>=2.5.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub==1.6.0) (2.8.0)
    Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.6.0) (2.10.3)
    Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.6.0) (1.1.0)
    Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.6.0) (7.0)
    Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.6.0) (0.16.0)
    Requirement already satisfied: pycodestyle<2.6.0,>=2.5.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.6.0) (2.5.0)
    Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.6.0) (0.6.1)
    Requirement already satisfied: entrypoints<0.4.0,>=0.3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.6.0) (0.3)
    Requirement already satisfied: pyflakes<2.2.0,>=2.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.6.0) (2.1.1)
    Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub==1.6.0) (2019.9.11)
    Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub==1.6.0) (1.25.6)
    Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub==1.6.0) (2.8)
    Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub==1.6.0) (0.4.1)
    Requirement already satisfied: wheel>=0.26; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub==1.6.0) (0.33.6)
    Requirement already satisfied: google-auth<2,>=1.6.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub==1.6.0) (1.10.0)
    Requirement already satisfied: grpcio>=1.24.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub==1.6.0) (1.26.0)
    Requirement already satisfied: markdown>=2.6.8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub==1.6.0) (3.1.1)
    Requirement already satisfied: absl-py>=0.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub==1.6.0) (0.8.1)
    Requirement already satisfied: proglog<=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub==1.6.0) (0.1.9)
    Requirement already satisfied: imageio-ffmpeg>=0.2.0; python_version >= "3.4" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub==1.6.0) (0.3.0)
    Requirement already satisfied: tqdm<5.0,>=4.11.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub==1.6.0) (4.36.1)
    Requirement already satisfied: imageio<3.0,>=2.5; python_version >= "3.4" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub==1.6.0) (2.6.1)
    Requirement already satisfied: decorator<5.0,>=4.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub==1.6.0) (4.4.0)
    Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->pre-commit->paddlehub==1.6.0) (0.6.0)
    Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.0->paddlehub==1.6.0) (1.1.1)
    Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=1.15->paddlehub==1.6.0) (1.3.0)
    Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub==1.6.0) (0.2.7)
    Requirement already satisfied: cachetools<5.0,>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub==1.6.0) (4.0.0)
    Requirement already satisfied: rsa<4.1,>=3.1.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub==1.6.0) (4.0)
    Requirement already satisfied: more-itertools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->pre-commit->paddlehub==1.6.0) (7.2.0)
    Requirement already satisfied: oauthlib>=3.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=1.15->paddlehub==1.6.0) (3.1.0)
    Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub==1.6.0) (0.4.8)
    Installing collected packages: paddlehub
      Found existing installation: paddlehub 1.5.0
        Uninstalling paddlehub-1.5.0:
          Successfully uninstalled paddlehub-1.5.0
    Successfully installed paddlehub-1.6.0
    

    情感分析

    情感倾向分析(Sentiment Classification,简称Senta)针对带有主观描述的中文文本,可自动判断该文本的情感极性类别并给出相应的置信度,能够帮助企业理解用户消费习惯、分析热点话题和危机舆情监控,为企业提供有利的决策支持。

    #安装情感分析模型
    !hub install senta_lstm==1.1.0
    
    /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import imp
    2020-04-25 20:22:04,395-INFO: font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
    2020-04-25 20:22:04,785-INFO: generated new fontManager
    Downloading senta_lstm
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpx2g3daqe/senta_lstm
    [==================================================] 100.00%
    Successfully installed senta_lstm-1.1.0
    
    #!hub run senta_lstm --input_text "这家餐厅很好吃"
    
    !hub run senta_lstm --input_text "人工智能课程很有趣"
    
    /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import imp
    Downloading lac
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpkn8ru3ra/lac
    [==================================================] 100.00%
    [{'text': '人工智能课程很有趣', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9572, 'negative_probs': 0.0428}]
    

    口罩检测

    PyramidBox-Lite是基于2018年百度发表于计算机视觉顶级会议ECCV 2018的论文PyramidBox而研发的轻量级模型,模型基于主干网络FaceBoxes,对于光照、口罩遮挡、表情变化、尺度变化等常见问题具有很强的鲁棒性。该PaddleHub Module基于WIDER FACE数据集和百度自采人脸数据集进行训练,支持预测,可用于检测人脸是否佩戴口罩。

    一键使用

    #安装口罩检测模型
    !hub install pyramidbox_lite_mobile_mask==1.1.0
    
    /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import imp
    Downloading pyramidbox_lite_mobile_mask
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpfpr1tnua/pyramidbox_lite_mobile_mask
    [==================================================] 100.00%
    Successfully installed pyramidbox_lite_mobile_mask-1.1.0
    
    !hub run pyramidbox_lite_mobile_mask --input_path "data/data31681/test.jpeg"
    
    /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import imp
    Downloading pyramidbox_lite_mobile
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmp2qiggeo7/pyramidbox_lite_mobile
    [==================================================] 100.00%
    image with bbox drawed saved as /home/aistudio/detection_result/test.jpeg
    [{'data': {'label': 'MASK', 'left': 678.9846324920654, 'right': 893.2966804504395, 'top': 241.9092297554016, 'bottom': 487.231333732605, 'confidence': 0.9711812}, 'id': 1, 'path': 'data/data31681/test.jpeg'}]
    
    import matplotlib.pyplot as plt 
    import matplotlib.image as mpimg 
    test_img_path = "data/data31681/test.jpeg"
    img = mpimg.imread(test_img_path) 
    
    # 展示待预测图片
    plt.figure(figsize=(10,10))
    plt.imshow(img) 
    plt.axis('off') 
    plt.show()
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5nEGyUyo-1587818716154)(output_8_0.png)]

    # 预测结果展示
    test_img_path = "./detection_result/test.jpeg"
    img = mpimg.imread(test_img_path)
    
    # 展示预测结果图片
    plt.figure(figsize=(10,10))
    plt.imshow(img) 
    plt.axis('off') 
    plt.show()
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KqQ4R0Ct-1587818716187)(output_9_0.png)]

    Python预测使用

    import paddlehub as hub
    import cv2
    
    module = hub.Module(name="pyramidbox_lite_mobile_mask")
    
    test_img_path = "data/data31681/test.jpeg"
    
    # set input dict
    input_dict = {"data": [cv2.imread(test_img_path)]}
    results = module.face_detection(data=input_dict)
    print(results)
    
    [32m[2020-04-25 20:26:57,816] [    INFO] - Installing pyramidbox_lite_mobile_mask module[0m
    [32m[2020-04-25 20:26:57,949] [    INFO] - Module pyramidbox_lite_mobile_mask already installed in /home/aistudio/.paddlehub/modules/pyramidbox_lite_mobile_mask[0m
    [32m[2020-04-25 20:26:58,029] [    INFO] - Installing pyramidbox_lite_mobile module-1.1.0[0m
    [32m[2020-04-25 20:26:58,041] [    INFO] - Module pyramidbox_lite_mobile-1.1.0 already installed in /home/aistudio/.paddlehub/modules/pyramidbox_lite_mobile[0m
    [32m[2020-04-25 20:26:58,211] [    INFO] - 133 pretrained paramaters loaded by PaddleHub[0m
    [32m[2020-04-25 20:26:58,250] [    INFO] - 0 pretrained paramaters loaded by PaddleHub[0m
    
    展开全文
  • PaddleHub体验口罩识别 PaddleHub 口罩检测示例 防控疫情,众志成城。人工智能技术正被应用到疫情防控中来。 百度积极响应号召,为了助推全社会的力量将AI技术应用于防疫工作,决定免费开源自研的“口罩人脸...
  • PaddleHub 口罩检测.pdf

    2021-09-14 13:14:02
    PaddleHub 口罩检测.pdf
  • paddlehub安装使用.pdf

    2021-08-10 12:01:45
    paddlehub安装使用.pdf
  • paddlehub_infer.py

    2021-07-26 16:51:07
    找了很久也没找到如何在paddle上部署微调后的Ernie,自己废了好大劲终于调通; paddlehub保存的模型是动态图模型,要进行转换(见model_change);
  • 使用飞桨PaddleHub实现皮影戏创作
  • PaddleHub 视频口罩检测.pdf
  • PaddleHub 视频人脸检测.pdf
  • PaddleHub二次加工
  • 飞桨端到端预训练模型与迁移学习工具PaddleHub 大纲 1.PaddleHub整体介绍 2.模型即软件 3.PaddleHub迁移学习 4.Hub Serving
  • paddlehub语言情感检测

    2020-08-30 10:38:40
    paddlehub语言情感检测 最近百度架构师手把手带你零基础实践深度学习这个课程有一个paddlehub创意比赛,于是就做了一个。代码很简单,是一个语言情感检测的系统,几行代码就可以解决的。 先说一说paddlehub吧,...
  • paddlehub图片生成

    2020-08-10 10:27:59
    1.PaddleHub简介 PaddleHub是百度--飞桨预训练模型管理和迁移学习工具,通过PaddleHub开发者可以使用高质量的预训练模型结合Fine-tune API快速完成迁移学习到应用部署的全流程工作。 其官网地址:...
  • PaddleHub 实验课.pptx

    2019-12-01 22:55:16
    PaddleHub 实验课 百度飞桨人工智能开发框架实验课,详尽指导 功能一:模型即软件 功能二:快捷完成迁移学习
  • paddlehub创意项目

    2020-08-30 10:46:24
    paddlehub创意项目应用实践一、首先加载带处理图像二、加载人像抠图预训练模型三、将抠出的人物图片合成在想要的背景图片当中。四、对合成后图片进行人脸检测五、心得总结 初次接触paddlehub,听过老师的讲座后,我...
  • 安装PaddlePaddle和Paddlehub 我电脑的显卡为RTX2070s,所以我需要安装GPU版本的paddle,过程为创建虚拟环境→激活虚拟环境→安装paddle→安装paddlehub 执行的命令如下: conda create -n paddle python==3.6.12 ...
  • 问题描述 pip install paddlehub==2.0.0b后并没有办法成功的import paddlehub 会出现以下的错误
  • 60分钟教你上手PaddleHub

    万次阅读 2019-10-05 23:23:58
    PaddleHub 介绍  PaddleHub 是基于 PaddlePaddle 开发的预训练模型管理工具,可以借助预训练模型更便捷地开展迁移学习工作,旨在让 PaddlePaddle 生态下的开发者更便捷体验到大规模预训练模型的价值。  Paddle...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,222
精华内容 1,288
关键字:

paddlehub