百度paddlepaddle框架是我接触的第一个深度学习的开源框架,从了解到逐步断断续续地学习已经将近半年时间了。百度飞桨致力于让深度学习技术的创新与应用更简单,仍记得刚开始入门的时候,代码晦涩难懂,但随着paddlepaddle文档和教程的完善,以及paddlehub(AI初学者的福音!!!)的上线,AI学习的门槛和难度都大大地降低了。为了打牢AI的基础,我在4月22日到29日参加了为期一周的AI小白培训班,一周内掌握了AI技术的基本技能,下面将写下总结和心得,我可以学会,你也可以!
作业要求:
1、输出乘法表
2、遍历”Day1-homework”目录下文件;找到文件名包含“2020”的文件;将文件名保存到数组result中;按照序号、文件名分行打印输出。
第一天的作业可谓是相当简单了,是一些基础,主要考虑的是算法基础和python程序语法(判断、循环)的运用。
def table():
#九九乘法表
for row in range(1, 10):
for col in range(1, row+1):
print('{}*{}={}'.format(col, row, col * row), end='\t')
print()
if __name__ == '__main__':
table()
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
#导入OS模块
import os
#待搜索的目录路径
path = "Day1-homework"
#待搜索的名称
filename = "2020"
#定义保存结果的数组
result = []
def findfiles():
count = 1
for root,dirs,files in os.walk(path):
for file in files:
if filename in file:
file_path = os.path.join(root,file)
#print(count,file_path)
result.append([count,file_path])
count+=1
for r in result:print(r)
if __name__ == '__main__':
findfiles()
[1, 'Day1-homework/26/26/new2020.txt'] [2, 'Day1-homework/4/22/04:22:2020.txt'] [3, 'Day1-homework/18/182020.doc']
第二天的作业任务为爬取青春有你2百科页面的选手图片,思路为从百科页面中遍历所有选手的个人百科链接,然后进入个人百科获取百科相册的内容。主要考的是python爬虫几个常用模块的运用,如requests,BeautifulSoup。对于python初学者,看网上beautifulsoup的代码可能有点难学,但我这次也是第一个用beautifulsoup模块,之前一直都是用lxml的etree,但我大概花了半小时不到就基本能运用beautifulsoup了,而且重要的是,beautifulsoup真的相当简单和便利,爽了一次之后就无法摆脱了!
import json
import re
import requests
import datetime
from bs4 import BeautifulSoup
import os
#获取当天的日期,并进行格式化,用于后面文件命名,格式:20200420
today = datetime.date.today().strftime('%Y%m%d')
def crawl_wiki_data():
"""
爬取百度百科中《青春有你2》中参赛选手信息,返回html
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
url='https://baike.baidu.com/item/青春有你第二季'
try:
response = requests.get(url,headers=headers)
print(response.status_code)
#将一段文档传入BeautifulSoup的构造方法,就能得到一个文档的对象, 可以传入一段字符串
soup = BeautifulSoup(response.text,'lxml')
#返回的是class为table-view log-set-param的所有标签
tables = soup.find_all('table',{'class':'table-view log-set-param'})
crawl_table_title = "参赛学员"
for table in tables:
#对当前节点前面的标签和字符串进行查找
table_titles = table.find_previous('div').find_all('h3')
for title in table_titles:
if(crawl_table_title in title):
return table
except Exception as e:
print(e)
def parse_wiki_data(table_html):
'''
从百度百科返回的html中解析得到选手信息,以当前日期作为文件名,存JSON文件,保存到work目录下
'''
bs = BeautifulSoup(str(table_html),'lxml')
all_trs = bs.find_all('tr')
error_list = ['\'','\"']
stars = []
for tr in all_trs[1:]:
all_tds = tr.find_all('td')
star = {}
#姓名
star["name"]=all_tds[0].text
#个人百度百科链接
star["link"]= 'https://baike.baidu.com' + all_tds[0].find('a').get('href')
#籍贯
star["zone"]=all_tds[1].text
#星座
star["constellation"]=all_tds[2].text
#身高
star["height"]=all_tds[3].text
#体重
star["weight"]= all_tds[4].text
#花语,去除掉花语中的单引号或双引号
flower_word = all_tds[5].text
for c in flower_word:
if c in error_list:
flower_word=flower_word.replace(c,'')
star["flower_word"]=flower_word
#公司
if not all_tds[6].find('a') is None:
star["company"]= all_tds[6].find('a').text
else:
star["company"]= all_tds[6].text
stars.append(star)
json_data = json.loads(str(stars).replace("\'","\""))
with open('work/' + today + '.json', 'w', encoding='UTF-8') as f:
json.dump(json_data, f, ensure_ascii=False)
def crawl_pic_urls():
'''
爬取每个选手的百度百科图片,并保存
'''
with open('work/'+ today + '.json', 'r', encoding='UTF-8') as file:
json_array = json.loads(file.read())
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
for star in json_array:
name = star['name']
link = star['link']
#!!!请在以下完成对每个选手图片的爬取,将所有图片url存储在一个列表pic_urls中!!!
pic_urls=[]
html = requests.get(link,headers=headers)
soup = BeautifulSoup(html.text,"lxml")
albums = soup.find_all(attrs={'class':'summary-pic'})
for album in albums:
album_url = album.a.get('href')
album_url = 'https://baike.baidu.com' + album_url
album_html = requests.get(album_url,headers=headers)
album_soup = BeautifulSoup(album_html.text,"lxml")
items = album_soup.find_all(attrs={'class':'pic-item'})
for item in items:
imgs = item.find_all('img')
for img in imgs:
img_src = img.get('src')
img_src = img_src.split('?')
pic_urls.append(img_src[0])
#!!!根据图片链接列表pic_urls, 下载所有图片,保存在以name命名的文件夹中!!!
down_pic(name,pic_urls)
def down_pic(name,pic_urls):
'''
根据图片链接列表pic_urls, 下载所有图片,保存在以name命名的文件夹中,
'''
path = 'work/'+'pics/'+name+'/'
if not os.path.exists(path):
os.makedirs(path)
for i, pic_url in enumerate(pic_urls):
try:
pic = requests.get(pic_url, timeout=15)
string = str(i + 1) + '.jpg'
with open(path+string, 'wb') as f:
f.write(pic.content)
print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url)))
except Exception as e:
print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url)))
print(e)
continue
def show_pic_path(path):
'''
遍历所爬取的每张图片,并打印所有图片的绝对路径
'''
pic_num = 0
for (dirpath,dirnames,filenames) in os.walk(path):
for filename in filenames:
pic_num += 1
print("第%d张照片:%s" % (pic_num,os.path.join(dirpath,filename)))
print("共爬取《青春有你2》选手的%d照片" % pic_num)
if __name__ == '__main__':
#爬取百度百科中《青春有你2》中参赛选手信息,返回html
html = crawl_wiki_data()
#解析html,得到选手信息,保存为json文件
parse_wiki_data(html)
#从每个选手的百度百科页面上爬取图片,并保存
crawl_pic_urls()
#打印所爬取的选手图片路径
show_pic_path('/home/aistudio/work/pics/')
print("所有信息爬取完成!")
Day3:《青春有你2》选手数据分析
今天的作业考验的是matplotlib数据绘图库的运用。通过爬取到的选手信息,统计青春有你2选手的体重数据分布情况,并绘制饼状图。在今天的作业中,我除了使用matplotlib绘制以外,还增加了pyecharts的代码,毕竟,pyecharts画图效果比matplotlib强太多。
1、Matplotlib实现
import matplotlib.pyplot as plt
import numpy as np
import json
import matplotlib.font_manager as font_manager
#显示matplotlib生成的图形
%matplotlib inline
with open('data/data31557/20200422.json', 'r', encoding='UTF-8') as file:
json_array = json.loads(file.read())
#绘制小姐姐区域分布柱状图,x轴为地区,y轴为该区域的小姐姐数量
weights = []
for star in json_array:
weight = star['weight'].replace('kg','')
weight = float(weight)
weights.append(weight)
weight_list = [0,0,0,0]
for weight in weights:
if weight<=45.0:
weight_list[0] +=1
elif weight <= 50.0:
weight_list[1] +=1
elif weight <= 55.0:
weight_list[2] +=1
elif weight > 55.0:
weight_list[3] +=1
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
plt.figure(figsize=(7,7))
x_labels = ['<=45kg','45~50kg','50~55kg','>55kg']
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
explode=[0,0.1,0.2,0]
plt.title('''《青春有你2》参赛选手年龄分布''',fontsize = 12, pad=20)
plt.pie(weight_list,labels=x_labels,colors=colors,explode=explode,shadow=True,labeldistance=1.1,startangle=50,radius=1,counterclock=True,autopct="%3.1f%%")
plt.axis('equal')
plt.savefig('work/result/1.jpg')
plt.show()
2、Pyecharts实现
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.charts import Pie
import numpy as np
import json
with open('data/data31557/20200422.json', 'r', encoding='UTF-8') as file:
json_array = json.loads(file.read())
#绘制小姐姐区域分布柱状图,x轴为地区,y轴为该区域的小姐姐数量
weights = []
for star in json_array:
weight = star['weight'].replace('kg','')
weight = float(weight)
weights.append(weight)
weight_list = [0,0,0,0]
for weight in weights:
if weight<=45.0:
weight_list[0] +=1
elif weight <= 50.0:
weight_list[1] +=1
elif weight <= 55.0:
weight_list[2] +=1
elif weight > 55.0:
weight_list[3] +=1
weight_list = np.array(weight_list)
x_labels = ['<=45kg','45~50kg','50~55kg','>55kg']
weight_total = 0
for w in weight_list:
weight_total += w
weight_list = weight_list/weight_total*100
weight_list = np.around(weight_list,decimals=1)
c = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add("年龄分布", [list(z) for z in zip(x_labels, weight_list)])
.set_global_opts(title_opts=opts.TitleOpts(title="《青春有你2》参赛选手年龄分布",padding=20),legend_opts=opts.LegendOpts(pos_bottom=0))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}%"))
)
c.render('work/result/2.html')
c.render_notebook()
没有对比没有伤害,pyecharts确实比matplotlib好看多了吧!
Day4:《青春有你2》五人识别
到了今天,才算是踏入人工智能的代码领域了。这一节内容主要是Paddlehub的使用,首先,容我介(吹)绍(爆)一款神器——PaddleHub(破音)!
PaddleHub是飞桨预训练模型管理和迁移学习工具,通过PaddleHub开发者可以使用高质量的预训练模型结合Fine-tune API快速完成迁移学习到应用部署的全流程工作。其提供了飞桨生态下的高质量预训练模型,涵盖了图像分类、目标检测、词法分析、语义模型、情感分析、视频分类、图像生成、图像分割、文本审核、关键点检测等主流模型。
如果学过TensorFlow或者其他深度学习框架的同学都知道,神经网络的搭建需要各种数据集准备、载入、卷积、池化……等等复杂的流程,但使用paddlehub的预训练模型,只需要自己少量的标注数据载入训练,即可得到一个准确率较高的模型!相当简单和方便!
今天用的模型,是paddlehub已经做好的人脸识别的预训练模型,任务是把青春有你5位选手的数据集载入到模型中,训练出新的模型,从而通过五位选手的照片预测出五位选手照片对应的姓名。
本次用到的与训练模型地址:https://www.paddlepaddle.org.cn/hubdetail?name=resnet_v2_50_imagenet&en_category=ImageClassification
事不多说,直接撸代码!
首先,载入预训练模型
import paddlehub as hub
module = hub.Module(name="resnet_v2_50_imagenet")
第2步,把数据集读入到模型中
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
class DemoDataset(BaseCVDataset):
def __init__(self):
# 数据集存放位置
self.dataset_dir = "dataset"
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train_list.txt",
validate_list_file="validate_list.txt",
test_list_file="test_list.txt",
label_list_file="label_list.txt",
)
dataset = DemoDataset()
#生成一个图像分类的reader,reader负责将dataset的数据进行预处理,接着以特定格式组织并输入给模型进行训练
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)
第3步:配置训练数据
config = hub.RunConfig(
use_cuda=False, #是否使用GPU训练,默认为False;
num_epoch=3, #Fine-tune的轮数;
checkpoint_dir="cv_finetune_turtorial_demo",#模型checkpoint保存路径, 若用户没有指定,程序会自动生成;
batch_size=3, #训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
eval_interval=10, #模型评估的间隔,默认每100个step评估一次验证集;
strategy=hub.finetune.strategy.DefaultFinetuneStrategy()) #Fine-tune优化策略;
上面3步与一般深度学习框架搭建神经网络时的步骤基本一致,但到了第4步开始就有区别了,如果使用框架的话,第四部应该是算法的选择和神经网络的搭建,就是卷积、池化等等步骤,但这里是组建Finetune Task。
input_dict, output_dict, program = module.context(trainable=True)
img = input_dict["image"]
feature_map = output_dict["feature_map"]
feed_list = [img.name]
task = hub.ImageClassifierTask(
data_reader=data_reader,
feed_list=feed_list,
feature=feature_map,
num_classes=dataset.num_labels,
config=config)
几行代码,就替代了原来复杂的神经网络搭建过程,是不是相当简单!
然后,一行代码就可以开始训练了。
run_states = task.finetune_and_eval()
最后,就是预测代码了,主要是读入需要预测的数据,打开训练好的模型,把数据放进预测模型就可以实现预测了。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
with open("dataset/test_list.txt","r") as f:
filepath = f.readlines()
data = [filepath[0].split(" ")[0],filepath[1].split(" ")[0],filepath[2].split(" ")[0],filepath[3].split(" ")[0],filepath[4].split(" ")[0]]
data1 = []
for d in data:
data1.append('dataset/'+ d)
label_map = dataset.label_dict()
index = 0
run_states = task.predict(data=data1)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
print(batch_result)
batch_result = np.argmax(batch_result, axis=2)[0]
print(batch_result)
for result in batch_result:
index += 1
result = label_map[result]
print("input %i is %s, and the predict result is %s" %
(index, data1[index - 1], result))
在Paddlehub面前,图像五分类就是变得如此简单快捷,只要准备好数据集,借助paddlehub的模型迁移学习能力,不到半小时就能训练出符合自己业务要求的模型了!
Day5-Day7:大作业
最后一次的大作业是前面所有知识的组合,即“爬虫+数据可视化+paddlehub”的综合应用。作业要求是先爬取爱奇艺的视频评论,然后使用jieba+Wordcloud的经典组合,将评论内容进行分词和词云可视化,最后使用paddlehub对评论内容进行审核。
首先,需要通过抓包分析来获取爱奇艺的评论,爱奇艺的视频网页加载的文件有很多,我刚开始以为它是使用json返回数据的,但我使用XHR过滤发现没有数据,然后点JS的时候才发现,它是用js来动态加载评论数据的。
一个含有“get_comment"字样的请求引起了我的注意,确实是这个文件,它是一个get请求,而且还带了page=的参数,这太爽了,直接写一个循环,让page从1到101就可以获取100页的内容了,而且,还不需要cookies!!!
def getMovieinfo(url):
'''
请求爱奇艺评论接口,返回response信息
参数 url: 评论的url
:return: response信息
'''
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 Edg/81.0.416.64",
"Referer":"https://www.iqiyi.com/v_19ryfkiv8w.html"
}
request_json = requests.get(url,headers=headers)
if request_json.status_code == 200:
response = request_json.text
return response
else:
return None
#解析json数据,获取评论
def saveMovieInfoToFile(text):
'''
解析json数据,获取评论
参数 text request获取到的json数据
:return: 0
'''
arr = []
responseJson = json.loads(text)
comments = responseJson['data']['comments']
for val in comments:
if 'content' in val.keys():
#写入文件
#arr.append(val['content'])
#print(val['content'])
filename = 'data/comment.txt'
with open(filename, 'a+') as file_object:
file_object.write(val['content'])
file_object.write('\r\n')
数据到手,然后就需要清理一些特殊字符了。怎么清理emoji表情呢,网上有很多办法,但我试过最好用的办法就是通过反复转码。但这里我还提供了另一种方法,就是检测文本是否中文,把评论中的中文字符提取出来即可,其他字符就不需要了,管它是什么emoji、标点符号、空格换行的,直接干掉,简单粗暴!
def clear_special_char(comment):
'''
正则处理特殊字符
参数 content:原文本
return: 清除后的文本
'''
#清理表情符emoji
comment = str(bytes(comment, encoding='utf-8').decode('utf-8').encode('gbk', 'ignore').decode('gbk'))
#清理中文表情,如[呲牙]
emoji_pattern = re.compile(r'\[[^()]*\]')
comment = emoji_pattern.sub(r'',comment)
#清理换行符
comment = comment.replace('\n','')
#清理空格
comment = comment.replace(' ','')
#清理标点符号
comment = re.sub(r"[%s]+" %punctuation, "",comment)
return comment
def is_Chinese(fenci_list): #检测是否全中文,去掉其他非中文字符以便做词云图
new_words_list = []
for ch in fenci_list:
if '\u4e00' <= ch <= '\u9fff':
new_words_list.append(ch)
return new_words_list
之后就用jieba模块对评论进行分词操作和去掉停用词,这一步我使用的停用词典是百度的,github上面还有各种停用词典。
def fenci(text):
'''
利用jieba进行分词
参数 text:需要分词的句子或文本
return:分词结果
'''
jieba.load_userdict('work/userdict.txt')
seg = jieba.lcut(text,cut_all=False)
return seg
def movestopwords(fenci_list):
'''
去除停用词,统计词频
参数 file_path:停用词文本路径 stopwords:停用词list counts: 词频统计结果
return:None
'''
#载入百度停用词表
new_words_list = fenci_list
with open('work/baidu_stopwords.txt', 'r') as f:
for kw in f.readlines():
kw = kw.replace('\n','')
if kw in new_words_list:
new_words_list.remove(kw)
return new_words_list
然后使用count统计出前10个出现最多的词语,并用matplotlib就可以实现可视化了。
def drawcounts(words_list):
'''
绘制词频统计表
参数 counts: 词频统计结果 num:绘制topN
return:none
'''
cipin_list = Counter(words_list).most_common(10) #输出前10
cipin_list = dict(cipin_list)
key = list(cipin_list.keys()) #X轴内容
value = list(cipin_list.values()) #Y轴数值
#绘图
# 设置显示中文
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
plt.title('词频统计结果')
plt.bar(range(len(value)), value,tick_label=key)
plt.show()
return 0
下一步就是用wordcloud做词云的可视化,但在做词云之前,我想先用paddlehub的图像分割模型,给词云图做一张背景图片。
#使用paddlehub分割词云背景照片
humanseg = hub.Module(name="deeplabv3p_xception65_humanseg")
test_img_path = "backgroud.jpg"
# set input dict
input_dict = {"image": [test_img_path]}
# execute predict and print the result
results = humanseg.segmentation(data=input_dict)
for result in results:
print(result['origin'])
print(result['processed'])
# 图片二值化将图片转黑色
img = Image.open("humanseg_output/backgroud.png")
Img = img.convert('L')
threshold = 200
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
photo = Img.point(table, '1')
photo.save("humanseg_output/backgroud.png")
def drawcloud(words_list):
'''
根据词频绘制词云图
参数 word_f:统计出的词频结果
return:none
'''
cipin_list = Counter(words_list)
cipin_list = dict(cipin_list)
#key = list(cipin_list.keys()) #X轴内容
font = 'simhei.ttf'
#backgroud_Image = np.array(Image.open("humanseg_output/backgroud.png"))
backgroud_Image = imread("humanseg_output/backgroud.png")
wc = WordCloud(font_path=font, # 如果是中文必须要添加字体
background_color='white',
mask = backgroud_Image,
min_font_size = 10,
).fit_words(cipin_list)
wc.to_file('wordcloud.png') # 保存图片
窈窕淑女,君子好逑,这里再次显示了paddlehub的威力!如果没有paddlehub,我们还需要建立各种模型,对图片中的主体进行识别,还要通过各种手段把人物从图片中分割提取出来,使用paddlehub,只需要几行代码就能够实现了。
最后就是对评论内容进行审核,此处用的是paddlehub的一个叫porn_detection_lstm的自然语言处理模型,这次使用模型与Day4的不一样,因为paddlehub里面的模型都是百度之前用自己的数据集训练好的,如果没有定制化需求,直接使用就可以了,不需要再自己准备数据集训练。
def text_detection(input_text):
'''
使用hub对评论进行内容分析
return:分析结果
'''
porn_detection_lstm = hub.Module(name="porn_detection_lstm")
input_dict = {"text": input_text}
results = porn_detection_lstm.detection(data=input_dict,use_gpu=True, batch_size=1)
for index, text in enumerate(input_text):
results[index]["text"] = text
for index, result in enumerate(results):
if six.PY2:
print(
json.dumps(results[index], encoding="utf8", ensure_ascii=False))
else:
rst = results[index]
text = rst['text']
porn = rst['porn_probs']
print("{}:{}".format(text,porn))
总结
近两年百度推广自家的paddlepaddle框架可谓是竭尽全力的,不断推出免费的教学课程,培养了数十万人工智能的程序员,为推动人工智能在我国的发展普及做出了卓越的贡献。最近paddlehub的推出和预训练模型的增加,更是大大地降低了人工智能的学习门槛和难度,为其他非计算机相关专业的从业者提供了学习和运用人工智能技术的可能,从而使人工智能能快速地运用到更广泛的工业实践中去,把技术迅速转化成生产力。我是一名学生,非计算机相关专业,甚至说跟计算机一点都沾不上边,但通过七天的学习,我已经掌握了paddlehub的基本使用,并且能够利用paddlehub进行一些模型的定制化,从而帮助我把人工智能引入到自己的专业中,转化成我的职业竞争优势。七天的学习,从零起步,我能学会,我相信大家都能学会!再次衷心感谢百度台前幕后的老师和工作人员,以及打卡群里可爱的小伙伴!
你可能感兴趣的:(技术文章)