這篇文章主要介紹了如何創(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àn)。已為惠州成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的惠州做網(wǎng)站的公司定做!
嵌入式處理技術(shù)的最新進(jìn)展已使基于視覺的系統(tǒng)可以在監(jiān)視過程中使用卷積神經(jīng)網(wǎng)絡(luò)檢測火災(zāi)。在本文中,兩個定制的CNN模型已經(jīng)實(shí)現(xiàn),它們擁有用于監(jiān)視視頻的高成本效益的火災(zāi)檢測CNN架構(gòu)。第一個模型是受AlexNet架構(gòu)啟發(fā)定制的基本CNN架構(gòu)。我們將實(shí)現(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)
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)
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)
創(chuàng)建定制的InceptionV3模型
我們開始為自定義的InceptionV3創(chuàng)建ImageDataGenerator。數(shù)據(jù)集包含3個類,但對于本文,我們將僅使用2個類。它包含用于訓(xùn)練的1800張圖像和用于驗(yàn)證的200張圖像。另外,我添加了8張客廳圖像,以在數(shù)據(jù)集中添加一些噪點(diǎn)。
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)
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)
#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)
實(shí)時測試
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()
感謝你能夠認(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í)!