sanket09 commited on
Commit
daf8559
·
verified ·
1 Parent(s): 5e1746f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +185 -42
app.py CHANGED
@@ -1,77 +1,220 @@
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import numpy as np
 
 
 
 
3
  import rasterio
4
- import cv2
5
  import matplotlib.pyplot as plt
6
- from sklearn.ensemble import RandomForestRegressor
7
- from sklearn.model_selection import train_test_split
8
- from sklearn.metrics import mean_squared_error
9
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Function to process a single TIFF file
12
- def process_tiff(file_path):
13
- with rasterio.open(file_path) as src:
14
- tiff_data = src.read()
15
- B2_image = tiff_data[1, :, :] # Assuming B2 is the second band
16
- target_size = (50, 50)
17
- B2_resized = cv2.resize(B2_image, target_size, interpolation=cv2.INTER_NEAREST)
18
- return B2_resized.reshape(-1, 1) # Reshape for the model input
19
-
20
- # Function to train the RandomForestRegressor model
21
- def train_random_forest_model(data_dir):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  X_list = []
23
  y_list = []
24
 
25
- # Load all TIFF files and preprocess data
26
  for root, dirs, files in os.walk(data_dir):
27
  for file in files:
28
  if file.endswith('.tiff'):
29
  file_path = os.path.join(root, file)
30
  X_list.append(process_tiff(file_path))
31
- # Generate synthetic target data for demonstration (replace with actual targets)
32
- y_list.append(np.random.rand(2500)) # Assuming target_size is (50, 50)
33
 
34
  X = np.vstack(X_list)
35
  y = np.hstack(y_list)
36
 
37
- # Split the data into training and testing sets
38
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
39
 
40
- # Train the model
41
  model = RandomForestRegressor(n_estimators=100, random_state=42)
42
  model.fit(X_train, y_train)
43
 
44
  return model
45
 
46
- # Function to make predictions using the trained model
47
- def predict_crop_yield(file, model_name, data_dir):
48
- if model_name == 'Random Forest':
49
- model = train_random_forest_model(data_dir)
50
- processed_image = process_tiff(file.name)
51
- prediction = model.predict(processed_image)
 
 
 
 
 
 
 
 
52
  prediction_reshaped = prediction.reshape((50, 50))
 
 
53
  plt.imshow(prediction_reshaped, cmap='viridis')
54
  plt.colorbar()
55
  plt.title('Yield Prediction for Single TIFF File')
56
- plt.savefig('prediction_output.png') # Save the plot
57
- return 'prediction_output.png'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  else:
59
  return "Model not found"
60
 
61
- data_dir = 'Data' # Path to the folder containing TIFF files
 
 
 
 
 
 
 
62
 
63
- inputs = [
64
- gr.File(label='Upload TIFF File'),
65
- gr.Dropdown(choices=['Random Forest'], label='Model')
66
  ]
67
- outputs = gr.Image(type='filepath', label='Predicted Yield Visualization')
68
-
69
- demo = gr.Interface(
70
- fn=lambda file, model_name: predict_crop_yield(file, model_name, data_dir),
71
- inputs=inputs,
72
- outputs=outputs,
73
- title="Crop Yield Prediction using Random Forest",
74
- theme=gr.themes.Soft()
75
- )
 
 
 
 
 
 
 
 
 
76
 
77
  demo.launch()
 
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
+ # Load crop data
22
+ def load_data():
23
+ url = 'https://raw.githubusercontent.com/NarutoOp/Crop-Recommendation/master/cropdata.csv'
24
+ data = pd.read_csv(url)
25
+ return data
26
+
27
+ data = load_data()
28
+
29
+ label_encoders = {}
30
+ for column in ['STATE', 'CROP']:
31
+ le = LabelEncoder()
32
+ data[column] = le.fit_transform(data[column])
33
+ label_encoders[column] = le
34
+
35
+ X = data[['YEAR', 'STATE', 'CROP', 'YEILD']] # Feature columns
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' # Update to your local directory containing TIFF files
71
  X_list = []
72
  y_list = []
73
 
 
74
  for root, dirs, files in os.walk(data_dir):
75
  for file in files:
76
  if file.endswith('.tiff'):
77
  file_path = os.path.join(root, file)
78
  X_list.append(process_tiff(file_path))
79
+ y_list.append(np.random.rand(2500)) # Replace with actual target data
 
80
 
81
  X = np.vstack(X_list)
82
  y = np.hstack(y_list)
83
 
 
84
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
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('/tmp/rf_prediction_overlay.png')
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
+ inputs_traditional = [
189
+ gr.Dropdown(choices=list(models.keys()), label='Model'),
190
+ gr.Number(label='Year'),
191
+ gr.Textbox(label='State'),
192
+ gr.Textbox(label='Crop'),
193
+ gr.Number(label='Yield'),
194
+ ]
195
+ outputs_traditional = gr.Textbox(label='Predicted Profit')
196
 
197
+ inputs_deep_learning = [
198
+ gr.Dropdown(choices=list(deep_learning_models.keys()) + ['Random Forest'], label='Model'),
199
+ gr.File(label='Upload TIFF File')
200
  ]
201
+ outputs_deep_learning = gr.Image(label='Prediction Overlay')
202
+
203
+ with gr.Blocks() as demo:
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()