-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpranjul_itondia2ndyr
112 lines (86 loc) · 4.03 KB
/
pranjul_itondia2ndyr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#as i m working on colab ,so i uploaded the folder of images in colab in zipped format..so unzip it
!unzip image_data1.zip
!pip install --upgrade Keras #upgrading keras to latest version
!pip3 list | grep -i keras
!python -c "import keras"
from tensorflow.keras.models import Sequential #importing sequntial class
import tensorflow.compat.v1 as tf #upgrading tensorflow version to v2
tf.disable_v2_behavior()
from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Convolution2D, MaxPooling2D
classifier=Sequential() #initializing of the cnn model
classifier.add(Convolution2D(32,3,3,input_shape=(64,64,3),activation = "relu")) #first convulational layer
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Convolution2D(32,3,3,activation='relu')) #second convolutional layer
classifier.add(MaxPooling2D(pool_size=(2,2))) #pool it
classifier.add(Flatten()) #flatten it toa one col array
classifier.add(Dense(128,activation='relu'))
classifier.add(Dense(1,activation='sigmoid'))
classifier.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen=ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
test_datagen=ImageDataGenerator(rescale=1./255)
training_set=train_datagen.flow_from_directory('image_data/training_set',target_size=(64,64),batch_size=32,class_mode='binary')
test_set=test_datagen.flow_from_directory('image_data/test_set',target_size=(64,64),batch_size=32,class_mode='binary')
classifier.fit_generator(training_set,steps_per_epoch=600,epochs=25,validation_data=test_set,validation_steps=94)
#code for testing the image
import numpy as np
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
# loading the image
test_image = load_img('random3.jpg',target_size=(64,64))
test_image = img_to_array(test_image)
print(test_image.dtype)
print(test_image.shape)
test_image=np.expand_dims(test_image,axis=0)
result=classifier.predict(test_image)
training_set.class_indices
if result[0][0] >=0.7:
prediction='fire'
else:
prediction='safe'
print(prediction)
#for uploading dataset from drive to colab
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
downloaded = drive.CreateFile({'id':'1-jsrUfAt06Gk94EQrid1qNJq474xlPBW'}) # replace the id with id of file
downloaded.GetContentFile('data1.csv')
#first import the required libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#now read the csv file by using pandas
dataset=pd.read_csv("data1.csv",error_bad_lines=False)
x=dataset.iloc[:,:3].values
y=dataset.iloc[:,3].values
dataset.head(5)
#for the month column ,apply encoding...label and one hot encoding
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
x[:, 0] = labelencoder.fit_transform(x[:, 0])
print(x)
print(y)
from sklearn.preprocessing import OneHotEncoder
onehotencoder = OneHotEncoder(categorical_features = [0])
x = onehotencoder.fit_transform(x).toarray()
print(x)
#split the dataset in 75% and 25%
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.25,random_state=0)
#apply random forest classifier
from sklearn.ensemble import RandomForestClassifier
clf=RandomForestClassifier()
clf.fit(x_train,y_train)
y_pred=classifier.predict(x_test) #predicting the test sets
from sklearn.metrics import confusion_matrix #confusion matrix for accuracy
cm4=confusion_matrix(y_test, y_pred)
print(cm4)
y_prob=classifier.predict_proba(x_test) #to get the probablity of fire
print(y_prob)