制作图片数据集
在學(xué)習(xí)卷積神經(jīng)網(wǎng)絡(luò)的時(shí)候,遇到了cifar10圖像數(shù)據(jù)集,用著挺好,但不想局限于固定的幾種圖像的識(shí)別,所以就有了自己制作數(shù)據(jù)集來(lái)識(shí)別的想法。
一、cifar10數(shù)據(jù)集。
據(jù)原網(wǎng)站介紹,數(shù)據(jù)集為二進(jìn)制。將cifar10解壓后,得到data_batch_1等數(shù)據(jù)集,打開看一下:
import pickle
f = open('./data_batch_1','rb') #以二進(jìn)制讀模式打開
d = pickle.load(f)
print(d)
可知數(shù)據(jù)集為dict型,主要有’data’和’labels’等四種鍵值。
二、爬取圖片
首先要感謝被爬網(wǎng)站的開放性和包容心,潭州教育堅(jiān)持對(duì)爬蟲技術(shù)的無(wú)私分享以及博主Jimmy。
import requests
import urllib.parse
import threading
設(shè)置最大線程鎖(與電腦配置和帶寬有關(guān))
thread_lock = threading.BoundedSemaphore(value=10)
def get_page(url):
page = requests.get(url) page = page.content page = page.decode('utf-8') # 將 bytes 轉(zhuǎn)換成 字符串 return pagedef pages_from_duitang(label):
pages = [] #找到圖片鏈接規(guī)律url = 'https://www.duitang.com/napi/blog/list/by_search/?
kw={}&start={}&limit=1000'
def findall_in_page(page,startpart,endpart):
all_strings = [] end = 0 while page.find(startpart,end) != -1:start = page.find(startpart, end) + len(startpart)end = page.find(endpart,start)string = page[start:end]all_strings.append(string) return all_stringsdef pic_urls_from_pages(pages):
pic_urls = [] for page in pages:urls = findall_in_page(page, 'path":"', '"')pic_urls.extend(urls) return pic_urlsdef download_pics(url,n):
r = requests.get(url) path = 'pics/fish/' + str(n) + '.jpg' with open(path, 'wb') as f:f.write(r.content) #下載結(jié)束,解鎖 thread_lock.release()def main(label):
pages = pages_from_duitang(label) pic_urls = pic_urls_from_pages(pages) n = 0 for url in pic_urls:n += 1print('正在下載第 {} 張圖片'.format(n))#上鎖thread_lock.acquire()t = threading.Thread(target=download_pics, args = (url, n))t.start()main('魚')
三、制作數(shù)據(jù)集
from PIL import Imageimport numpy as np
import pickle,glob,os
arr = [[]]
number of pictures
n = 1
for infile in glob.glob('D:/py/pics/trees/*.jpg'):
if Img.mode != 'RGB':#將所有非'RGB'通道圖片轉(zhuǎn)化為RGB
Img = Img.convert('RGB') width = Img.size[0] height = Img.size[1]print('{} imagesize is:{} X {}'.format(n,width,height)) n += 1Img = Img.resize([32,32],Image.ANTIALIAS)
抗鋸齒的過濾屬性,這些都是為了保證剪切圖片的時(shí)候,最大降低失真度,這樣出
的圖片體積就稍微大些了。
r,g,b = Img.split() r_array = np.array(r).reshape([1024]) g_array = np.array(g).reshape([1024]) b_array = np.array(b).reshape([1024]) merge_array = np.concatenate((r_array,g_array,b_array)) if arr == [[]]:arr = [merge_array]continue #拼接arr = np.concatenate((arr,[merge_array]),axis=0)
#打亂順序arr = np.random.shuffle(arr)
生成標(biāo)簽
labelset = np.zeros((arr.shape[0],))
labelset = np.reshape(labelset,[arr.shape[0],])
字典分割出訓(xùn)練集和測(cè)試集
train_dic = {'data':arr[:2000],'labels':labelset[:2000]}
test_dic = {'data':arr[2000:],'labels':labelset[2000:]}
f = open('./data_batch_8','wb')#二進(jìn)制寫模式打開,如果不存在,直接生成
pickle.dump(train_dic,f,protocol=2)
序列化操作
由于阿里云平臺(tái)用的是Python2.7版本,我的是3.6,所以要進(jìn)行退檔操作protocol=2
g = open('./test_batch_1','wb')
pickle.dump(test_dic,g,protocol=2)
四、訓(xùn)練和測(cè)試
由于本機(jī)硬件水平較低,采用阿里云平臺(tái)進(jìn)行測(cè)試,根據(jù)自己的數(shù)據(jù)集規(guī)模,調(diào)整平臺(tái)提供的代**碼。經(jīng)測(cè)試,精度達(dá)到76%。對(duì)于這個(gè)結(jié)果還是相當(dāng)滿意的,因?yàn)閿?shù)據(jù)集中干擾> > 太多,沒有進(jìn)行篩選。
五、問題
在制作數(shù)據(jù)集過程中,遇到兩個(gè)問題:
1、r,g,b = img.split():(已解決)
valueError:too?many?values?to?unpack(expected 3)
unpack的個(gè)數(shù)對(duì)不上,比如:a,b = tuple(1,2,3) 就會(huì)報(bào)出這個(gè)錯(cuò)誤
通過Img.mode發(fā)現(xiàn)有的圖片是“1”、“L”、“P”和“RGBA”模式,需要convert。
2、r,g,b = img.split():(待解決)
OSError:cannot identify image file:路徑+格式
暫時(shí)理解為系統(tǒng)兼容性問題
總結(jié)
- 上一篇: 程序员修炼之道-笔记
- 下一篇: 提高SQL的查询效率