Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • ar-noc/keras-smoke-detection
  • i3perez/keras-smoke-detection
  • hpwren-dev/keras-smoke-detection
3 results
Show changes
Showing
with 53988 additions and 0 deletions
screenshots/toolate.PNG

45.3 KiB

screenshots/usingotherports.PNG

6.79 KiB

This diff is collapsed.
This diff is collapsed.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
# Scrips for classifcation algorythms
The scrips and notebooks within this directory are everything needed to download the images from HJPWREN and design & test the classifcation algorythms
## Explanations
- getFiles.sh: Code used to download the HPWREN archieve to current directory
Just run this shell file at the place you want the directory to be
- imageLoader.ipynb: Used to sort the HPWREN archieve into (training, testing, validation) subdirectories that Keras can read
<br> ![test](scripts/scripts/getPath.PNG) <br />
Set the mypath to the current location of the HPWREN archieve (filtered)
Set savepath to the directory you want the sorted data archieve (with training, validation, and test )
- trainingScript.py: Used to define the model and use it to train on the subdirectories that was previously created
<br> ![test](scripts/scripts/training.PNG)<br />
Assign all the subdirectories
<br>![test](scripts/scripts/training2.PNG)<br />
This is the location where the model will be saved
<br>![test](scripts/scripts/training3.PNG)<br />
This is the location where the training history will be saved
- historyEvaluator.ipynb: Used to see the training loss and other metrics of training done of script
<br>![test](scripts/scripts/results.PNG)<br />
Assign this to where the training history was saved (above)
- testing.ipynb: Used to test new model on training subset
\ No newline at end of file
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import SeparableConv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
import os
import sys
import re
from PIL import Image
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
import os
import sys
import re
from PIL import Image
import tensorflow as tf
from os import listdir
from os.path import isdir, join, isfile
from numpy import asarray
from numpy import save
import itertools
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
import os
import sys
import re
from PIL import Image
import pandas as pd
import keras
import tempfile
from tensorflow.keras.callbacks import LambdaCallback
physical_devices = tf.config.experimental.list_physical_devices('GPU')
# physical_devices = tf.config.experimental.list_physical_device
tf.config.experimental.set_memory_growth(physical_devices[0], True)
assert tf.config.experimental.get_memory_growth(physical_devices[0])
import keras
from keras import backend as K
# K.tensorflow_backend._get_available_gpus()
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
from keras import backend as K
def mcor(y_true, y_pred):
#matthews_correlation
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
y_neg = 1 - y_pos
tp = K.sum(y_pos * y_pred_pos)
tn = K.sum(y_neg * y_pred_neg)
fp = K.sum(y_neg * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)
numerator = (tp * tn - fp * fn)
denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
return numerator / (denominator + K.epsilon())
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
TRAIN_SPLIT = 0.75
TEST_SPLIT = 0.25
INIT_LR = 1e-7
BATCH_SIZE = 32
NUM_EPOCHS = 10000
image_size = 200,200
class_mode = "binary"
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=30,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
validation_split=0,
horizontal_flip=True,
fill_mode="nearest")
image_generatorCLASSIC = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=30,
zoom_range=0,
width_shift_range=0,
height_shift_range=0,
shear_range=0,
validation_split=0,
horizontal_flip=True,
fill_mode="nearest")
dataDirectoryTrain = "/userdata/kerasData/preloaded/flowDirectory/train/"
dataDirectoryValidation = "/userdata/kerasData/preloaded/flowDirectory/validation/"
dataDirectoryTest = "/userdata/kerasData/preloaded/flowDirectory/test/"
trainingGeneratorHPWREN = image_generator.flow_from_directory(
dataDirectoryTrain,
target_size=image_size,
seed=42,
batch_size=BATCH_SIZE,
class_mode=class_mode,
subset="training")
validationGeneratorHPWREN = image_generator.flow_from_directory(
dataDirectoryValidation,
target_size=image_size,
batch_size=BATCH_SIZE,
seed=42,
class_mode=class_mode,
subset = "training")
testGeneratorHPWREN = image_generatorCLASSIC.flow_from_directory(
dataDirectoryTest,
target_size=image_size,
batch_size=BATCH_SIZE,
seed=42,
class_mode=class_mode,
subset = "training")
class FireDetectionNet:
@staticmethod
def build(width, height, depth):
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
model.add(SeparableConv2D(32, (7, 7), padding="same",
input_shape=inputShape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(SeparableConv2D(64, (5,5), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(SeparableConv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(SeparableConv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(5,5)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# second set of FC => RELU layers
model.add(Dense(128))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# softmax classifier
model.add(Dense(2))
model.add(Activation("softmax"))
# return the constructed network architecture
return model
name = "HPWRENGroundUp_1024_SPLIT2_v1_e3"
opt = SGD(lr=INIT_LR, momentum=0.9,
decay=INIT_LR / NUM_EPOCHS)
groundUpModel = FireDetectionNet.build(width=200, height=200, depth=3)
groundUpModel.compile(loss="binary_crossentropy", optimizer=opt,
metrics=["accuracy", precision, recall, f1])
mc = tf.keras.callbacks.ModelCheckpoint(f'/userdata/kerasData/pyimagesearch/output/experimental/{name}HPWREN.model', monitor='val_loss', mode='auto', save_freq='epoch', verbose=1)
early_stopping_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=50)
history = groundUpModel.fit(
trainingGeneratorHPWREN,
validation_data=validationGeneratorHPWREN,
steps_per_epoch=len(trainingGeneratorHPWREN) // BATCH_SIZE,
validation_steps= len(validationGeneratorHPWREN) // BATCH_SIZE,
epochs=NUM_EPOCHS,
callbacks=[mc, early_stopping_callback],
verbose=1
)
history_df = pd.DataFrame(history.history)
hist_csv_file=f"/userdata/kerasData/output/recreate/{name}"
with open(hist_csv_file, mode='w') as f:
history_df.to_csv(f)
wget -r -A .jpg -l2 --no-parent http://hpwren.ucsd.edu/HPWREN-FIgLib/HPWREN-FIgLib-Data/
ls |grep -v ".*-c$"|xargs -I {} rm -rf {}
find $PWD -maxdepth 2|grep ".*html.*"|xargs -I {} rm -rf {}
This diff is collapsed.
scripts/scripts/getPath.PNG

56.2 KiB

scripts/scripts/getPath_2.PNG

42.4 KiB

scripts/scripts/results.PNG

15.5 KiB

scripts/scripts/training.PNG

39.5 KiB

scripts/scripts/training2.PNG

11.1 KiB

scripts/scripts/training3.PNG

20.7 KiB

This diff is collapsed.