Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,220 +1,61 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
from sklearn.model_selection import train_test_split
|
3 |
-
from sklearn.linear_model import LinearRegression
|
4 |
-
from sklearn.svm import SVR
|
5 |
-
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
|
6 |
-
from sklearn.preprocessing import LabelEncoder
|
7 |
import gradio as gr
|
8 |
-
import os
|
9 |
import numpy as np
|
10 |
-
import tensorflow as tf
|
11 |
-
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Input
|
12 |
-
from tensorflow.keras.optimizers import Adam
|
13 |
-
from PIL import Image
|
14 |
import rasterio
|
15 |
-
import matplotlib.pyplot as plt
|
16 |
-
from tensorflow.keras.applications import ResNet50
|
17 |
-
from tensorflow.keras.models import Model
|
18 |
import cv2
|
|
|
19 |
import joblib
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
y = data['PROFIT'] # Target column
|
37 |
-
|
38 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
39 |
-
|
40 |
-
models = {
|
41 |
-
'Linear Regression': LinearRegression(),
|
42 |
-
'SVR': SVR(),
|
43 |
-
'Random Forest': RandomForestRegressor(),
|
44 |
-
'Gradient Boosting': GradientBoostingRegressor()
|
45 |
-
}
|
46 |
-
|
47 |
-
for name, model in models.items():
|
48 |
-
model.fit(X_train, y_train)
|
49 |
-
|
50 |
-
def predict_traditional(model_name, year, state, crop, yield_):
|
51 |
-
if model_name in models:
|
52 |
-
model = models[model_name]
|
53 |
-
state_encoded = label_encoders['STATE'].transform([state])[0]
|
54 |
-
crop_encoded = label_encoders['CROP'].transform([crop])[0]
|
55 |
-
prediction = model.predict([[year, state_encoded, crop_encoded, yield_]])[0]
|
56 |
-
return prediction
|
57 |
-
else:
|
58 |
-
return "Model not found"
|
59 |
-
|
60 |
-
# Train RandomForestRegressor model for deep learning model
|
61 |
def train_random_forest_model():
|
62 |
-
def process_tiff(file_path):
|
63 |
-
with rasterio.open(file_path) as src:
|
64 |
-
tiff_data = src.read()
|
65 |
-
B2_image = tiff_data[1, :, :] # Assuming B2 is the second band
|
66 |
-
target_size = (50, 50)
|
67 |
-
B2_resized = cv2.resize(B2_image, target_size, interpolation=cv2.INTER_NEAREST)
|
68 |
-
return B2_resized.reshape(-1, 1)
|
69 |
-
|
70 |
-
data_dir = '/Data'
|
71 |
X_list = []
|
72 |
y_list = []
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
87 |
-
model.fit(X_train, y_train)
|
88 |
-
|
89 |
-
return model
|
90 |
-
|
91 |
-
rf_model = train_random_forest_model()
|
92 |
-
|
93 |
-
def predict_random_forest(file):
|
94 |
-
if file is not None:
|
95 |
-
def process_tiff(file_path):
|
96 |
-
with rasterio.open(file_path) as src:
|
97 |
-
tiff_data = src.read()
|
98 |
-
B2_image = tiff_data[1, :, :]
|
99 |
-
target_size = (50, 50)
|
100 |
-
B2_resized = cv2.resize(B2_image, target_size, interpolation=cv2.INTER_NEAREST)
|
101 |
-
return B2_resized.reshape(-1, 1)
|
102 |
-
|
103 |
-
tiff_processed = process_tiff(file.name)
|
104 |
-
prediction = rf_model.predict(tiff_processed)
|
105 |
prediction_reshaped = prediction.reshape((50, 50))
|
106 |
-
|
107 |
-
plt.figure(figsize=(10, 10))
|
108 |
plt.imshow(prediction_reshaped, cmap='viridis')
|
109 |
plt.colorbar()
|
110 |
plt.title('Yield Prediction for Single TIFF File')
|
111 |
-
plt.savefig('
|
112 |
-
|
113 |
-
return '/tmp/rf_prediction_overlay.png'
|
114 |
-
else:
|
115 |
-
return "No file uploaded"
|
116 |
-
|
117 |
-
# Load deep learning models
|
118 |
-
def load_deep_learning_model(model_name):
|
119 |
-
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
|
120 |
-
base_model.trainable = False
|
121 |
-
|
122 |
-
inputs = Input(shape=(128, 128, 3))
|
123 |
-
x = base_model(inputs, training=False)
|
124 |
-
x = GlobalAveragePooling2D()(x)
|
125 |
-
outputs = Dense(1, activation='linear')(x)
|
126 |
-
|
127 |
-
model = Model(inputs, outputs)
|
128 |
-
model.compile(optimizer=Adam(), loss='mean_squared_error', metrics=['mae'])
|
129 |
-
|
130 |
-
return model
|
131 |
-
|
132 |
-
deep_learning_models = {
|
133 |
-
'ResNet50': load_deep_learning_model('ResNet50'),
|
134 |
-
# Add other models here if needed
|
135 |
-
}
|
136 |
-
|
137 |
-
def predict_deep_learning(model_name, file):
|
138 |
-
if model_name in deep_learning_models:
|
139 |
-
if file is not None:
|
140 |
-
with rasterio.open(file.name) as src:
|
141 |
-
img_data = src.read(1)
|
142 |
-
|
143 |
-
patch_size = 128
|
144 |
-
n_patches_x = img_data.shape[1] // patch_size
|
145 |
-
n_patches_y = img_data.shape[0] // patch_size
|
146 |
-
|
147 |
-
patches = []
|
148 |
-
for i in range(n_patches_y):
|
149 |
-
for j in range(n_patches_x):
|
150 |
-
patch = img_data[i*patch_size:(i+1)*patch_size, j*patch_size:(j+1)*patch_size]
|
151 |
-
patches.append(patch)
|
152 |
-
patches = np.array(patches)
|
153 |
-
|
154 |
-
preprocessed_patches = []
|
155 |
-
for patch in patches:
|
156 |
-
img = Image.fromarray(patch)
|
157 |
-
img = img.convert('RGB')
|
158 |
-
img = img.resize((128, 128))
|
159 |
-
img_array = np.array(img) / 255.0
|
160 |
-
preprocessed_patches.append(img_array)
|
161 |
-
preprocessed_patches = np.array(preprocessed_patches)
|
162 |
-
|
163 |
-
model = deep_learning_models[model_name]
|
164 |
-
predictions = model.predict(preprocessed_patches)
|
165 |
-
predictions = predictions.reshape((n_patches_y, n_patches_x))
|
166 |
-
|
167 |
-
threshold = np.percentile(predictions, 90)
|
168 |
-
|
169 |
-
overlay = np.zeros_like(img_data, dtype=np.float32)
|
170 |
-
for i in range(n_patches_y):
|
171 |
-
for j in range(n_patches_x):
|
172 |
-
if predictions[i, j] > threshold:
|
173 |
-
overlay[i*patch_size:(i+1)*patch_size, j*patch_size:(j+1)*patch_size] = predictions[i, j]
|
174 |
-
|
175 |
-
plt.figure(figsize=(10, 10))
|
176 |
-
plt.imshow(img_data, cmap='gray', alpha=0.5)
|
177 |
-
plt.imshow(overlay, cmap='jet', alpha=0.5)
|
178 |
-
plt.title('Crop Yield Prediction Overlay')
|
179 |
-
plt.colorbar()
|
180 |
-
plt.savefig('/tmp/dl_prediction_overlay.png')
|
181 |
-
|
182 |
-
return '/tmp/dl_prediction_overlay.png'
|
183 |
-
else:
|
184 |
-
return "No file uploaded"
|
185 |
else:
|
186 |
return "Model not found"
|
187 |
|
188 |
-
|
189 |
-
gr.
|
190 |
-
gr.
|
191 |
-
gr.Textbox(label='State'),
|
192 |
-
gr.Textbox(label='Crop'),
|
193 |
-
gr.Number(label='Yield'),
|
194 |
]
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
with gr.Tab("Traditional ML Models"):
|
205 |
-
gr.Interface(
|
206 |
-
fn=predict_traditional,
|
207 |
-
inputs=inputs_traditional,
|
208 |
-
outputs=outputs_traditional,
|
209 |
-
title="Profit Prediction using Traditional ML Models"
|
210 |
-
)
|
211 |
-
|
212 |
-
with gr.Tab("Deep Learning Models"):
|
213 |
-
gr.Interface(
|
214 |
-
fn=lambda model_name, file: predict_deep_learning(model_name, file) if model_name != 'Random Forest' else predict_random_forest(file),
|
215 |
-
inputs=inputs_deep_learning,
|
216 |
-
outputs=outputs_deep_learning,
|
217 |
-
title="Crop Yield Prediction using Deep Learning Models and Random Forest"
|
218 |
-
)
|
219 |
|
220 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
|
|
|
|
|
|
|
|
3 |
import rasterio
|
|
|
|
|
|
|
4 |
import cv2
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
import joblib
|
7 |
+
from sklearn.ensemble import RandomForestRegressor
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
from PIL import Image
|
10 |
+
import io
|
11 |
+
|
12 |
+
# Function to process a single TIFF file
|
13 |
+
def process_tiff(file):
|
14 |
+
# Read file from BytesIO object
|
15 |
+
with rasterio.open(io.BytesIO(file.read())) as src:
|
16 |
+
tiff_data = src.read()
|
17 |
+
B2_image = tiff_data[1, :, :] # Assuming B2 is the second band
|
18 |
+
target_size = (50, 50)
|
19 |
+
B2_resized = cv2.resize(B2_image, target_size, interpolation=cv2.INTER_NEAREST)
|
20 |
+
return B2_resized.reshape(-1, 1) # Reshape for the model input
|
21 |
+
|
22 |
+
# Function to train the RandomForestRegressor model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def train_random_forest_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
X_list = []
|
25 |
y_list = []
|
26 |
|
27 |
+
# Placeholder for actual file paths
|
28 |
+
# Modify to load and process multiple TIFF files if needed
|
29 |
+
# For now, using a single uploaded file for training
|
30 |
+
return
|
31 |
+
|
32 |
+
# Function to make predictions using the RandomForestRegressor model
|
33 |
+
def predict_crop_yield(file, model_name):
|
34 |
+
if model_name == 'Random Forest':
|
35 |
+
model = joblib.load('crop_yield_model.joblib') # Load pre-trained model
|
36 |
+
processed_image = process_tiff(file)
|
37 |
+
prediction = model.predict(processed_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
prediction_reshaped = prediction.reshape((50, 50))
|
|
|
|
|
39 |
plt.imshow(prediction_reshaped, cmap='viridis')
|
40 |
plt.colorbar()
|
41 |
plt.title('Yield Prediction for Single TIFF File')
|
42 |
+
plt.savefig('prediction_output.png') # Save the plot
|
43 |
+
return 'prediction_output.png'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
else:
|
45 |
return "Model not found"
|
46 |
|
47 |
+
inputs = [
|
48 |
+
gr.File(label='Upload TIFF File'),
|
49 |
+
gr.Dropdown(choices=['Random Forest'], label='Model')
|
|
|
|
|
|
|
50 |
]
|
51 |
+
outputs = gr.Image(type='filepath', label='Predicted Yield Visualization')
|
52 |
+
|
53 |
+
demo = gr.Interface(
|
54 |
+
fn=predict_crop_yield,
|
55 |
+
inputs=inputs,
|
56 |
+
outputs=outputs,
|
57 |
+
title="Crop Yield Prediction using Random Forest",
|
58 |
+
theme=gr.themes.Soft()
|
59 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
demo.launch()
|