如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)

這篇文章主要介紹了如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

惠州網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,惠州網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為惠州成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的惠州做網(wǎng)站的公司定做!


創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)。

 如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)

嵌入式處理技術(shù)的最新進(jìn)展已使基于視覺的系統(tǒng)可以在監(jiān)視過程中使用卷積神經(jīng)網(wǎng)絡(luò)檢測火災(zāi)。在本文中,兩個定制的CNN模型已經(jīng)實現(xiàn),它們擁有用于監(jiān)視視頻的高成本效益的火災(zāi)檢測CNN架構(gòu)。第一個模型是受AlexNet架構(gòu)啟發(fā)定制的基本CNN架構(gòu)。我們將實現(xiàn)和查看其輸出和限制,并創(chuàng)建一個定制的InceptionV3模型。為了平衡效率和準(zhǔn)確性,考慮到目標(biāo)問題和火災(zāi)數(shù)據(jù)的性質(zhì)對模型進(jìn)行了微調(diào)。我們將使用三個不同的數(shù)據(jù)集來訓(xùn)練我們的模型。

創(chuàng)建定制的CNN架構(gòu)

我們將使用TensorFlow API Keras構(gòu)建模型。首先,我們創(chuàng)建用于標(biāo)記數(shù)據(jù)的ImageDataGenerator。[1]和[2]數(shù)據(jù)集在這里用于訓(xùn)練。最后,我們將提供980張圖像用于訓(xùn)練和239張圖像用于驗證。我們也將使用數(shù)據(jù)增強。

import tensorflow as tfimport keras_preprocessingfrom keras_preprocessing import imagefrom keras_preprocessing.image import ImageDataGeneratorTRAINING_DIR = "Train"training_datagen = ImageDataGenerator(rescale = 1./255,                                  horizontal_flip=True,                                  rotation_range=30,                                  height_shift_range=0.2,                                  fill_mode='nearest')VALIDATION_DIR = "Validation"validation_datagen = ImageDataGenerator(rescale = 1./255)train_generator = training_datagen.flow_from_directory(TRAINING_DIR,                                         target_size=(224,224),                                        class_mode='categorical',                                         batch_size = 64)validation_generator = validation_datagen.flow_from_directory(                                                VALIDATION_DIR,                                           target_size=(224,224),                                          class_mode='categorical',                                           batch_size= 16)
           
在上面的代碼中應(yīng)用了3種數(shù)據(jù)增強技術(shù),它們分別是水平翻轉(zhuǎn),旋轉(zhuǎn)和高度移位。
現(xiàn)在,我們將創(chuàng)建我們的CNN模型。該模型包含三對Conv2D-MaxPooling2D層,然后是3層密集層。為了克服過度擬合的問題,我們還將添加dropout層。最后一層是softmax層,它將為我們提供火災(zāi)和非火災(zāi)兩類的概率分布。通過將類數(shù)更改為1,還可以在最后一層使用‘Sigmoid’激活函數(shù)。

from tensorflow.keras.optimizers import Adammodel = tf.keras.models.Sequential([tf.keras.layers.Conv2D(96, (11,11), strides=(4,4), activation='relu', input_shape=(224, 224, 3)), tf.keras.layers.MaxPooling2D(pool_size = (3,3), strides=(2,2)),tf.keras.layers.Conv2D(256, (5,5), activation='relu'),tf.keras.layers.MaxPooling2D(pool_size = (3,3), strides=(2,2)),tf.keras.layers.Conv2D(384, (5,5), activation='relu'),tf.keras.layers.MaxPooling2D(pool_size = (3,3), strides=(2,2)),tf.keras.layers.Flatten(),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(2048, activation='relu'),tf.keras.layers.Dropout(0.25),tf.keras.layers.Dense(1024, activation='relu'),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(2, activation='softmax')])model.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.0001),metrics=['acc'])history = model.fit(train_generator,steps_per_epoch = 15,epochs = 50,validation_data = validation_generator,validation_steps = 15)
           
我們將使用Adam作為學(xué)習(xí)率為0.0001的優(yōu)化器。經(jīng)過50個時期的訓(xùn)練,我們得到了96.83的訓(xùn)練精度和94.98的驗證精度。訓(xùn)練損失和驗證損失分別為0.09和0.13。
如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)
我們的訓(xùn)練模型
讓我們測試模型中的所有圖像,看看它的猜測是否正確。為了進(jìn)行測試,我們選擇了3張圖像,其中包括有火的圖像,沒有火的圖像以及包含火樣顏色和陰影的照片。
我們最終得到上面創(chuàng)建的模型在對圖像進(jìn)行分類時犯了一個錯誤。  該模型52%的把握確定圖像中有火焰。  這是因為已進(jìn)行訓(xùn)練的數(shù)據(jù)集中幾乎沒有圖像可以說明室內(nèi)火災(zāi)的模型。  所以該模型僅知道室外火災(zāi)情況,而當(dāng)給出一張室內(nèi)火樣的陰影圖像時會出現(xiàn)錯誤。  另一個原因是我們的模型不具備可以學(xué)習(xí)火的復(fù)雜特征。  
接下來,我們將使用標(biāo)準(zhǔn)的InceptionV3模型并對其進(jìn)行自定義。復(fù)雜模型能夠從圖像中學(xué)習(xí)復(fù)雜特征。

創(chuàng)建定制的InceptionV3模型

這次我們將使用不同的數(shù)據(jù)集[3],其中包含室外和室內(nèi)火災(zāi)圖像。  我們已經(jīng)在該數(shù)據(jù)集中訓(xùn)練了我們之前的CNN模型,結(jié)果表明它是過擬合的,因為它無法處理這個相對較大的數(shù)據(jù)集和從圖像中學(xué)習(xí)復(fù)雜的特征。  

我們開始為自定義的InceptionV3創(chuàng)建ImageDataGenerator。數(shù)據(jù)集包含3個類,但對于本文,我們將僅使用2個類。它包含用于訓(xùn)練的1800張圖像和用于驗證的200張圖像。另外,我添加了8張客廳圖像,以在數(shù)據(jù)集中添加一些噪點。

import tensorflow as tfimport keras_preprocessingfrom keras_preprocessing import imagefrom keras_preprocessing.image import ImageDataGeneratorTRAINING_DIR = "Train"training_datagen = ImageDataGenerator(rescale=1./255,zoom_range=0.15,horizontal_flip=True,fill_mode='nearest')VALIDATION_DIR = "/content/FIRE-SMOKE-DATASET/Test"validation_datagen = ImageDataGenerator(rescale = 1./255)train_generator = training_datagen.flow_from_directory(TRAINING_DIR,target_size=(224,224),shuffle = True,class_mode='categorical',batch_size = 128)validation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,target_size=(224,224),class_mode='categorical',shuffle = True,batch_size= 14)
           
為了使訓(xùn)練更加準(zhǔn)確,我們可以使用數(shù)據(jù)增強技術(shù)。在上面的代碼中應(yīng)用了2種數(shù)據(jù)增強技術(shù)-水平翻轉(zhuǎn)和縮放。
讓我們從Keras API導(dǎo)入InceptionV3模型。我們將在InceptionV3模型的頂部添加圖層,如下所示。我們將添加一個全局空間平均池化層,然后是2個密集層和2個dropout層,以確保我們的模型不會過擬合。最后,我們將為2個類別添加一個softmax激活的密集層。
接下來,我們將首先僅訓(xùn)練我們添加并隨機(jī)初始化的圖層。我們將在這里使用RMSprop作為優(yōu)化器。

from tensorflow.keras.applications.inception_v3 import InceptionV3from tensorflow.keras.preprocessing import imagefrom tensorflow.keras.models import Modelfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input, Dropoutinput_tensor = Input(shape=(224, 224, 3))base_model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=False)x = base_model.outputx = GlobalAveragePooling2D()(x)x = Dense(2048, activation='relu')(x)x = Dropout(0.25)(x)x = Dense(1024, activation='relu')(x)x = Dropout(0.2)(x)predictions = Dense(2, activation='softmax')(x)model = Model(inputs=base_model.input, outputs=predictions)for layer in base_model.layers:  layer.trainable = Falsemodel.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])history = model.fit(train_generator,steps_per_epoch = 14,epochs = 20,validation_data = validation_generator,validation_steps = 14)
           
在訓(xùn)練了頂層20個周期之后,我們將凍結(jié)模型的前249層,并訓(xùn)練其余的層(即頂層2個初始塊)。在這里,我們將使用SGD作為優(yōu)化器,學(xué)習(xí)率為0.0001。

#To train the top 2 inception blocks, freeze the first 249 layers and unfreeze the rest.for layer in model.layers[:249]:  layer.trainable = Falsefor layer in model.layers[249:]:  layer.trainable = True#Recompile the model for these modifications to take effectfrom tensorflow.keras.optimizers import SGDmodel.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['acc'])history = model.fit(train_generator,steps_per_epoch = 14,epochs = 10,validation_data = validation_generator,validation_steps = 14)
           
經(jīng)過10個周期的訓(xùn)練,我們獲得了98.04的訓(xùn)練準(zhǔn)確度和96.43的驗證準(zhǔn)確度。訓(xùn)練損失和驗證損失分別為0.063和0.118。
如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)
以上10個時期的訓(xùn)練過程
我們用相同的圖像測試我們的模型,看看是否它可以正確猜出。
這次我們的模型可以使所有三個預(yù)測正確。  96%的把握可以確定圖像中沒有任何火。  我用于測試的其他兩個圖像如下:  
如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)     如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)
來自下面引用的數(shù)據(jù)集中的非火災(zāi)圖像

實時測試

現(xiàn)在,我們的模型已準(zhǔn)備好在實際場景中進(jìn)行測試。  以下是使用OpenCV訪問我們的網(wǎng)絡(luò)攝像頭并預(yù)測每幀圖像中是否包含火的示例代碼。  如果框架中包含火焰,我們希望將該框架的顏色更改為B&W。  

import cv2import numpy as npfrom PIL import Imageimport tensorflow as tffrom keras.preprocessing import image#Load the saved modelmodel = tf.keras.models.load_model('InceptionV3.h6')video = cv2.VideoCapture(0)while True:        _, frame = video.read()#Convert the captured frame into RGB        im = Image.fromarray(frame, 'RGB')#Resizing into 224x224 because we trained the model with this image size.        im = im.resize((224,224))        img_array = image.img_to_array(im)        img_array = np.expand_dims(img_array, axis=0) / 255        probabilities = model.predict(img_array)[0]        #Calling the predict method on model to predict 'fire' on the image        prediction = np.argmax(probabilities)        #if prediction is 0, which means there is fire in the frame.        if prediction == 0:                frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)                print(probabilities[prediction])cv2.imshow("Capturing", frame)        key=cv2.waitKey(1)        if key == ord('q'):                breakvideo.release()cv2.destroyAllWindows()
           
這個項目的Github鏈接在這里:https://github.com/DeepQuestAI/Fire-Smoke-Dataset。可以從那里找到數(shù)據(jù)集和上面的所有代碼。
如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

分享文章:如何創(chuàng)建用于室內(nèi)和室外火災(zāi)檢測的定制InceptionV3和CNN架構(gòu)
網(wǎng)頁網(wǎng)址:http://muchs.cn/article4/pdhsoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站策劃電子商務(wù)、App設(shè)計、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)