Uday Chitragar commited on
Commit
3318d20
·
1 Parent(s): ad5473d
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -6,6 +6,7 @@ import random
6
  from sklearn.decomposition import PCA
7
  from sklearn.metrics import classification_report, roc_auc_score
8
  from tensorflow.keras.applications import EfficientNetB0
 
9
  from tensorflow.keras.models import Model
10
  from pyod.models.iforest import IForest
11
  from pyod.models.lof import LOF
@@ -13,11 +14,11 @@ from pyod.models.ocsvm import OCSVM
13
  import matplotlib.pyplot as plt
14
 
15
  # Paths (adjust as needed)
16
- dataset_path = "/kaggle/input/rice-image-dataset/Rice_Image_Dataset"
17
- basmati_path = os.path.join(dataset_path, "Basmati")
18
- jasmine_path = os.path.join(dataset_path, "Jasmine")
19
 
20
- # Load and preprocess images (same as your code)
21
  def load_images_from_folder(folder, label, limit=None):
22
  images = []
23
  filenames = os.listdir(folder)
@@ -30,7 +31,7 @@ def load_images_from_folder(folder, label, limit=None):
30
  if img is not None:
31
  img = cv2.resize(img, (128, 128))
32
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
33
- img = preprocess_input(img.astype(np.float32))
34
  images.append(img)
35
  img_data.append((img, filename, label))
36
  return np.array(images), img_data
@@ -68,7 +69,7 @@ X_train_features = extract_features(X_train)
68
  X_test_features = extract_features(X_test)
69
 
70
  # PCA
71
- pca = PCA(n_components=100)
72
  X_train_reduced = pca.fit_transform(X_train_features)
73
  X_test_reduced = pca.transform(X_test_features)
74
 
@@ -134,15 +135,15 @@ def run_anomaly_detection(mode, model_name, contamination, n_estimators, n_neigh
134
 
135
  # Gradio Interface
136
  with gr.Blocks() as interface:
137
- gr.Markdown("## Rice Anomaly Detection UI")
138
 
139
  with gr.Row():
140
  mode = gr.Dropdown(["Unsupervised", "Semi-supervised"], label="Mode")
141
  model_name = gr.Dropdown(["IForest", "LOF", "OCSVM"], label="Model")
142
 
143
  with gr.Row():
144
- contamination = gr.Slider(0, 0.5, value=0.05, step=0.01, label="Contamination")
145
- n_estimators = gr.Slider(100, 500, value=100, step=10, label="N Estimators (IForest)")
146
  n_neighbors = gr.Slider(5, 50, value=20, step=1, label="N Neighbors (LOF)")
147
  nu = gr.Slider(0, 1, value=0.1, step=0.01, label="Nu (OCSVM)")
148
 
 
6
  from sklearn.decomposition import PCA
7
  from sklearn.metrics import classification_report, roc_auc_score
8
  from tensorflow.keras.applications import EfficientNetB0
9
+ from tensorflow.keras.applications.efficientnet import preprocess_input # Add this import
10
  from tensorflow.keras.models import Model
11
  from pyod.models.iforest import IForest
12
  from pyod.models.lof import LOF
 
14
  import matplotlib.pyplot as plt
15
 
16
  # Paths (adjust as needed)
17
+ dataset_path = "data"
18
+ basmati_path = os.path.join(dataset_path, "basmati")
19
+ jasmine_path = os.path.join(dataset_path, "jasmine")
20
 
21
+ # Load and preprocess images
22
  def load_images_from_folder(folder, label, limit=None):
23
  images = []
24
  filenames = os.listdir(folder)
 
31
  if img is not None:
32
  img = cv2.resize(img, (128, 128))
33
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
34
+ img = preprocess_input(img.astype(np.float32)) # Now this will work
35
  images.append(img)
36
  img_data.append((img, filename, label))
37
  return np.array(images), img_data
 
69
  X_test_features = extract_features(X_test)
70
 
71
  # PCA
72
+ pca = PCA(n_components=50)
73
  X_train_reduced = pca.fit_transform(X_train_features)
74
  X_test_reduced = pca.transform(X_test_features)
75
 
 
135
 
136
  # Gradio Interface
137
  with gr.Blocks() as interface:
138
+ gr.Markdown("## Anomaly Detection Playground")
139
 
140
  with gr.Row():
141
  mode = gr.Dropdown(["Unsupervised", "Semi-supervised"], label="Mode")
142
  model_name = gr.Dropdown(["IForest", "LOF", "OCSVM"], label="Model")
143
 
144
  with gr.Row():
145
+ contamination = gr.Slider(0, 0.25, value=0.05, step=0.01, label="Contamination")
146
+ n_estimators = gr.Slider(100, 299, value=100, step=10, label="N Estimators (IForest)")
147
  n_neighbors = gr.Slider(5, 50, value=20, step=1, label="N Neighbors (LOF)")
148
  nu = gr.Slider(0, 1, value=0.1, step=0.01, label="Nu (OCSVM)")
149