專業(yè)網(wǎng)站設(shè)計(jì)服務(wù)seo關(guān)鍵字優(yōu)化軟件
0 前言
🔥 優(yōu)質(zhì)競(jìng)賽項(xiàng)目系列,今天要分享的是
🚩 基于深度學(xué)習(xí)的植物識(shí)別算法研究與實(shí)現(xiàn)
🥇學(xué)長(zhǎng)這里給一個(gè)題目綜合評(píng)分(每項(xiàng)滿分5分)
- 難度系數(shù):4分
- 工作量:4分
- 創(chuàng)新點(diǎn):4分
🧿 更多資料, 項(xiàng)目分享:
https://gitee.com/dancheng-senior/postgraduate
2 相關(guān)技術(shù)
2.1 VGG-Net模型
Google DeepMind公司研究員與牛津大學(xué)計(jì)算機(jī)視覺組在2014年共同研發(fā)出了一種全新的卷積神經(jīng)網(wǎng)絡(luò)–VGG-
Net。在同年舉辦的ILSVRC比賽中,該網(wǎng)絡(luò)結(jié)構(gòu)模型在分類項(xiàng)目中取得了十分出色的成績(jī),由于其簡(jiǎn)潔性和實(shí)用性,使得其在當(dāng)時(shí)迅速,飛快地成為了最受歡迎的卷積神經(jīng)網(wǎng)絡(luò)模型。VGG-
Net卷積神經(jīng)網(wǎng)絡(luò)在近年來(lái)衍生出了A-
E七種不同的層次結(jié)構(gòu),本次研究使用其中的D結(jié)構(gòu),也就是VGG-16Net結(jié)構(gòu),該結(jié)構(gòu)中包含了13個(gè)卷積層,5個(gè)池化層和3個(gè)全連接層。針對(duì)所有的卷積層,使用相同的5x5大小的卷積核,針對(duì)所有的池化層,使用相同的3x3大小的池化核。VGG-
Net結(jié)構(gòu)如圖所示。
2.2 VGG-Net在植物識(shí)別的優(yōu)勢(shì)
在針對(duì)植物識(shí)別問(wèn)題上,VGG-Net有著一些相較于其他神經(jīng)網(wǎng)絡(luò)的優(yōu)勢(shì),主要包括以下幾點(diǎn):
(1) 卷積核,池化核大小固定
網(wǎng)絡(luò)中所有的卷積核大小固定為3x3,所有的池化核大小固定為5x5。這樣在進(jìn)行卷積和池化操作的時(shí)候,從數(shù)據(jù)中提取到的特征更加明顯,同時(shí)在層與層的連接時(shí),信息的丟失會(huì)更少,更加方便后續(xù)對(duì)于重要特征的提取和處理。
(2) 特征提取更全面
VGG-
Net網(wǎng)絡(luò)模型中包含了13個(gè)卷積層。卷積層數(shù)目越多,對(duì)于特征的提取更加的全面。由于需要對(duì)于植物的姿態(tài)、顏色等進(jìn)行判定,植物的特征較多,需要在提取時(shí)更加的全面,細(xì)致,才有可能得到一個(gè)更加準(zhǔn)確的判定。VGG-
Net符合條件。
(3) 網(wǎng)絡(luò)訓(xùn)練誤差收斂速度較快
VGG-
Net網(wǎng)絡(luò)在訓(xùn)練時(shí)收斂速度相對(duì)較快,能夠較快地得到預(yù)期的結(jié)果。具有這一特點(diǎn)的原因有兩個(gè),一個(gè)是網(wǎng)絡(luò)中每一個(gè)卷積層和池化層中的卷積核大小與池化核大小固定,另一個(gè)就是對(duì)于各個(gè)隱藏層的參數(shù)初始化方法使用專門針對(duì)ReLU激活函數(shù)的Kaiming正態(tài)初始化方法。
3 VGG-Net的搭建
本次研究基于Pytorch深度學(xué)習(xí)框架進(jìn)行網(wǎng)絡(luò)的搭建,利用模塊化的設(shè)計(jì)思想,構(gòu)建一個(gè)類,來(lái)對(duì)于整個(gè)的網(wǎng)絡(luò)進(jìn)行結(jié)構(gòu)上的封裝。這樣搭建的好處是可以隱藏實(shí)現(xiàn)的內(nèi)部細(xì)節(jié),提高代碼的安全性,增強(qiáng)代碼的復(fù)用效率,并且對(duì)于一些方法,通過(guò)在內(nèi)部集成,可以方便之后對(duì)于其中方法的調(diào)用,提升代碼的簡(jiǎn)潔性。
在網(wǎng)絡(luò)搭建完成后,將數(shù)據(jù)集傳入網(wǎng)絡(luò)中進(jìn)行訓(xùn)練,經(jīng)過(guò)一段時(shí)間后即可得到植物識(shí)別的分類識(shí)別結(jié)果。
3.1 Tornado簡(jiǎn)介
Tornado全稱Tornado Web
Server,是一個(gè)用Python語(yǔ)言寫成的Web服務(wù)器兼Web應(yīng)用框架,由FriendFeed公司在自己的網(wǎng)站FriendFeed中使用,被Facebook收購(gòu)以后框架在2009年9月以開源軟件形式開放給大眾。
(1) 優(yōu)勢(shì)
- 輕量級(jí)web框架
- 異步非阻塞IO處理方式
- 出色的抗負(fù)載能力
- 優(yōu)異的處理性能,不依賴多進(jìn)程/多線程,一定程度上解決C10K問(wèn)題
- WSGI全棧替代產(chǎn)品,推薦同時(shí)使用其web框架和HTTP服務(wù)器
(2) 關(guān)鍵代碼
? class MainHandler(tornado.web.RequestHandler):
? def get(self):
? self.render("index.html")
? def post(self):keras.backend.clear_session()img = Image.open(BytesIO(self.request.files['image'][0]['body']))img = imgb_img = Image.new('RGB', (224, 224), (255, 255, 255))size = img.sizeif size[0] >= size[1]:rate = 224 / size[0]new_size = (224, int(size[1] * rate))img = img.resize(new_size, Image.ANTIALIAS).convert("RGB")b_img.paste(img, (0, random.randint(0, 224 - new_size[1])))else:rate = 224 / size[1]new_size = (int(size[0] * rate), 224)img = img.resize(new_size, Image.ANTIALIAS).convert("RGB")b_img.paste(img, (random.randint(0, 224 - new_size[0]), 0))if self.get_argument("method", "mymodel") == "VGG16":Model = load_model("VGG16.h5")else:Model = load_model("InceptionV3.h5")data = orc_img(Model,b_img)self.write(json.dumps({"code": 200, "data": data}))def make_app():template_path = "templates/"static_path = "./static/"return tornado.web.Application([(r"/", MainHandler),], template_path=template_path, static_path=static_path, debug=True)?
? def run_server(port=8000):
? tornado.options.parse_command_line()
? app = make_app()
? app.listen(port)
? print("\n服務(wù)已啟動(dòng) 請(qǐng)打開 http://127.0.0.1:8000 ")
? tornado.ioloop.IOLoop.current().start()
4 Inception V3 神經(jīng)網(wǎng)絡(luò)
GoogLeNet對(duì)網(wǎng)絡(luò)中的傳統(tǒng)卷積層進(jìn)行了修改,提出了被稱為 Inception
的結(jié)構(gòu),用于增加網(wǎng)絡(luò)深度和寬度,提高深度神經(jīng)網(wǎng)絡(luò)性能。從Inception V1到Inception
V4有4個(gè)更新版本,每一版的網(wǎng)絡(luò)在原來(lái)的基礎(chǔ)上進(jìn)行改進(jìn),提高網(wǎng)絡(luò)性能。
4.1 網(wǎng)絡(luò)結(jié)構(gòu)
inception結(jié)構(gòu)的作用(inception的結(jié)構(gòu)和作用)
作用:代替人工確定卷積層中過(guò)濾器的類型或者確定是否需要?jiǎng)?chuàng)建卷積層或者池化層。即:不需要人為決定使用什么過(guò)濾器,是否需要?jiǎng)?chuàng)建池化層,由網(wǎng)絡(luò)自己學(xué)習(xí)決定這些參數(shù),可以給網(wǎng)絡(luò)添加所有可能值,將輸入連接起來(lái),網(wǎng)絡(luò)自己學(xué)習(xí)需要它需要什么樣的參數(shù)。
inception主要思想
用密集成分來(lái)近似最優(yōu)的局部稀疏解(如上圖)
- 采用不同大小的卷積核意味著有不同大小的感受野,最后的拼接意味著不同尺度特征的融合。
- 之所以卷積核大小采用1x1、3x3和5x5,主要是為了方便對(duì)齊。設(shè)定卷積步長(zhǎng)stride=1之后,只要分別設(shè)定padding = 0、1、2,采用same卷積可以得到相同維度的特征,然后這些特征直接拼接在一起。
- 很多地方都表明pooling挺有效,所以Inception里面也嵌入了pooling。
- 網(wǎng)絡(luò)越到后面特征越抽象,且每個(gè)特征涉及的感受野也更大,隨著層數(shù)的增加,3x3和5x5卷積的比例也要增加。
- 最終版inception,加入了1x1 conv來(lái)降低feature map厚度。
5 開始訓(xùn)練
5.1 數(shù)據(jù)集
訓(xùn)練圖像按照如下方式進(jìn)行分類,共分為9文件夾。
5.2 關(guān)鍵代碼
?
from keras.utils import Sequenceimport math? class SequenceData(Sequence):
? def __init__(self, batch_size, target_size, data):# 初始化所需的參數(shù)self.batch_size = batch_sizeself.target_size = target_sizeself.x_filenames = datadef __len__(self):# 讓代碼知道這個(gè)序列的長(zhǎng)度num_imgs = len(self.x_filenames)return math.ceil(num_imgs / self.batch_size)def __getitem__(self, idx):# 迭代器部分batch_x = self.x_filenames[idx * self.batch_size: (idx + 1) * self.batch_size]imgs = []y = []for x in batch_x:img = Image.open(x)b_img = Image.new('RGB', self.target_size, (255, 255, 255))size = img.sizeif size[0] >= size[1]:rate = self.target_size[0] / size[0]new_size = (self.target_size[0], int(size[1] * rate))img = img.resize(new_size, Image.ANTIALIAS).convert("RGB")b_img.paste(img, (0, random.randint(0, self.target_size[0] - new_size[1])))else:rate = self.target_size[0] / size[1]new_size = (int(size[0] * rate), self.target_size[0])img = img.resize(new_size, Image.ANTIALIAS).convert("RGB")b_img.paste(img, (random.randint(0, self.target_size[0] - new_size[0]), 0))img = b_imgif random.random() < 0.1:img = img.convert("L").convert("RGB")if random.random() < 0.2:img = img.rotate(random.randint(0, 20)) # 隨機(jī)旋轉(zhuǎn)一定角度if random.random() < 0.2:img = img.rotate(random.randint(340, 360)) # 隨 旋轉(zhuǎn)一定角度imgs.append(img.convert("RGB"))x_arrays = 1 - np.array([np.array(i) for i in imgs]).astype(float) / 255 # 讀取一批圖片batch_y = to_categorical(np.array([labels.index(x.split("/")[-2]) for x in batch_x]), len(labels))return x_arrays, batch_y?
5.3 模型預(yù)測(cè)
利用我們訓(xùn)練好的 vgg16.h5 模型進(jìn)行預(yù)測(cè),相關(guān)代碼如下:
?
def orc_img(model,image):
? img =np.array(image)
? img = np.array([1 - img.astype(float) / 255])
? predict = model.predict(img)
? index = predict.argmax()
? print("CNN預(yù)測(cè)", index)
? target = target_name[index]index2 = np.argsort(predict)[0][-2]target2 = target_name[index2]index3 = np.argsort(predict)[0][-3]target3 = target_name[index3]return {"target": target,"predict": "%.2f" % (float(list(predict)[0][index]) * 64),"target2": target2,"predict2": "%.2f" % (float(list(predict)[0][index2]) * 64),}
6 效果展示
6.1 主頁(yè)面展示
6.2 圖片預(yù)測(cè)
6.3 三維模型可視化
學(xué)長(zhǎng)在web頁(yè)面上做了一個(gè)三維網(wǎng)絡(luò)結(jié)構(gòu)可視化功能,可以直觀的看到網(wǎng)絡(luò)模型結(jié)構(gòu)
7 最后
🧿 更多資料, 項(xiàng)目分享:
https://gitee.com/dancheng-senior/postgraduate