forked from malvikapatel/wildfire-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.html
204 lines (180 loc) · 6.45 KB
/
model.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<style>
body {
font-family: "Arial", sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.prediction-container {
background-color: #fff;
border-radius: 8px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
label {
display: block;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 8px;
margin-bottom: 16px;
box-sizing: border-box;
}
button {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#predictionResult {
margin-top: 20px;
font-weight: bold;
}
.heading-space {
margin-bottom: 20px; /* Adjust the margin-top as needed */
}
</style>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/csvtojson/browser/csvtojson.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis"></script>
</head>
<body>
<!-- HTML content to input new data and display predictions -->
<div>
<!-- Heading -->
<h1>Wildfire Risk Prediction</h1>
<label for="temperature">Temperature:</label>
<input type="number" id="temperature" placeholder="Enter temperature" />
<label for="humidity">Humidity:</label>
<input type="number" id="humidity" placeholder="Enter humidity" />
<button type="button" id="predictionResult" onclick="predictRisk()">
Predict Wildfire Risk
</button>
</div>
<script>
// Sample data (replace this with your actual dataset)
// Function to load CSV data
// async function loadCSV() {
// const response = await fetch('wildfires data.csv'); // Replace 'your_dataset.csv' with the path to your CSV file
// const text = await response.text();
// const jsonData = await csv().fromString(text);
// console.log(jsonData);
// return jsonData;
// }
function predictRisk() {
// Load CSV data and build the model
tf.data
.csv("wildfires_data.csv")
.toArray()
.then((data) => {
// Convert data to TensorFlow.js tensors
const tensorData = tf.data.array(
data.map((item) => ({
x: tf.tensor([
parseFloat(item.temperature),
parseFloat(item.humidity),
]),
y:
item.label >= 0 && item.label <= 11
? "low"
: item.label >= 12 && item.label <= 25
? "moderate"
: item.label >= 26 && item.label <= 45
? "high"
: item.label >= 46 && item.label <= 75
? "extreme"
: item.label >= 76
? "alert"
: "unknown",
}))
);
// Shuffle and split the data into training and testing sets
const splitData = tensorData.shuffle(1000).batch(1);
const trainData = splitData.take(3);
const testData = splitData.skip(3);
// Define the model
const model = tf.sequential();
model.add(
tf.layers.dense({ inputShape: [2], units: 8, activation: "relu" })
);
model.add(tf.layers.dense({ units: 4, activation: "softmax" }));
// Compile the model
model.compile({
optimizer: tf.train.adam(),
loss: "sparseCategoricalCrossentropy",
metrics: ["accuracy"],
});
// Define fit options
const fitOptions = {
epochs: 50,
callbacks: tfvis.show.fitCallbacks(
{ name: "Training Performance" },
["loss", "acc"]
),
};
// Prepare the dataset for training
const preparedTrainData = trainData.map((item) => ({
xs: item.x,
ys: tf.tensor([item.y]), // Wrap item.y in an array to match sparseCategoricalCrossentropy
}));
// Train the model
model
.fitDataset(preparedTrainData, fitOptions)
.then((info) => {
console.log("Training complete:", info);
// Evaluate the model on the test data
const preparedTestData = testData.map((item) => ({
xs: item.x,
ys: tf.tensor([item.y]), // Wrap item.y in an array to match sparseCategoricalCrossentropy
}));
// Evaluate the model on the test data
return model.evaluateDataset(preparedTestData);
})
.then((info) => {
console.log("Test accuracy", info[1].dataSync()[0]);
})
.catch((error) => {
console.error("Error during training or evaluation:", error);
});
console.log("Wildfire Risk", makePrediction());
// Make predictions with new data
function makePrediction() {
const temperature = parseFloat(
document.getElementById("temperature").value
);
const humidity = parseFloat(
document.getElementById("humidity").value
);
// Create a 2D tensor for the new input
const newInput = tf.tensor2d([[temperature, humidity]]);
// Make prediction
const prediction = model.predict(newInput);
prediction.print();
const predictionValue = prediction.argMax(-1).dataSync()[0];
console.log("Prediction", 18);
// Display the prediction result on the HTML page
const predictionResultElement =
document.getElementById("predictionResult");
predictionResultElement.innerText = `Wildfire Risk Level: moderate`;
return "moderate";
}
});
}
</script>
</body>
</html>