Commit
·
a942d3e
1
Parent(s):
94f2c59
Update app.py
Browse files
app.py
CHANGED
@@ -16,4 +16,118 @@ from sklearn import metrics
|
|
16 |
from sklearn.svm import SVC
|
17 |
dim = 100
|
18 |
from imutils import paths
|
19 |
-
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
from sklearn.svm import SVC
|
17 |
dim = 100
|
18 |
from imutils import paths
|
19 |
+
import cv2
|
20 |
+
!unzip /content/drive/MyDrive/Tomato.zip -d MTP
|
21 |
+
import os
|
22 |
+
|
23 |
+
# Define the paths for the train and test datasets
|
24 |
+
train_base_dir = '/content/MTP/dataset/train'
|
25 |
+
test_base_dir = '/content/MTP/dataset/val'
|
26 |
+
|
27 |
+
# List of class names to keep
|
28 |
+
class_names_to_keep = [
|
29 |
+
"Late_blight", "Tomato_mosaic_virus", "healthy",
|
30 |
+
"Septoria_leaf_spot", "Bacterial_spot", "Tomato_Yellow_Leaf_Curl_Virus"
|
31 |
+
]
|
32 |
+
|
33 |
+
# Create lists to store the file paths for train and test images
|
34 |
+
train_image_paths = []
|
35 |
+
test_image_paths = []
|
36 |
+
|
37 |
+
# Populate the train and test image paths based on the specified classes
|
38 |
+
for class_name in class_names_to_keep:
|
39 |
+
train_image_paths.extend([os.path.join(train_base_dir, class_name, filename) for filename in os.listdir(os.path.join(train_base_dir, class_name))])
|
40 |
+
test_image_paths.extend([os.path.join(test_base_dir, class_name, filename) for filename in os.listdir(os.path.join(test_base_dir, class_name))])
|
41 |
+
import tensorflow as tf
|
42 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
43 |
+
|
44 |
+
# Define image dimensions and batch size
|
45 |
+
image_height, image_width = 224, 224
|
46 |
+
batch_size = 32
|
47 |
+
# Define a function to load and preprocess the images, including labels
|
48 |
+
def load_and_preprocess_image(image_path, label):
|
49 |
+
image = tf.io.read_file(image_path)
|
50 |
+
image = tf.image.decode_jpeg(image, channels=3)
|
51 |
+
image = tf.image.resize(image, [image_height, image_width])
|
52 |
+
image = image / 255.0
|
53 |
+
return image, label
|
54 |
+
|
55 |
+
# Create TensorFlow Datasets with labels
|
56 |
+
train_labels = [0 if "healthy" in path else 1 for path in train_image_paths]
|
57 |
+
test_labels = [0 if "healthy" in path else 1 for path in test_image_paths]
|
58 |
+
|
59 |
+
train_dataset = tf.data.Dataset.from_tensor_slices((train_image_paths, train_labels))
|
60 |
+
train_dataset = train_dataset.map(load_and_preprocess_image)
|
61 |
+
train_dataset = train_dataset.batch(batch_size)
|
62 |
+
|
63 |
+
test_dataset = tf.data.Dataset.from_tensor_slices((test_image_paths, test_labels))
|
64 |
+
test_dataset = test_dataset.map(load_and_preprocess_image)
|
65 |
+
test_dataset = test_dataset.batch(batch_size)
|
66 |
+
|
67 |
+
# Define and compile the CNN model as before
|
68 |
+
import tensorflow as tf
|
69 |
+
from tensorflow.keras.models import Sequential
|
70 |
+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
|
71 |
+
|
72 |
+
# Define the CNN model
|
73 |
+
model = Sequential([
|
74 |
+
Conv2D(32, (3, 3), activation='relu', input_shape=(image_height, image_width, 3)),
|
75 |
+
MaxPooling2D((2, 2)),
|
76 |
+
Conv2D(64, (3, 3), activation='relu'),
|
77 |
+
MaxPooling2D((2, 2)),
|
78 |
+
Conv2D(128, (3, 3), activation='relu'),
|
79 |
+
MaxPooling2D((2, 2)),
|
80 |
+
Flatten(),
|
81 |
+
Dense(128, activation='relu'),
|
82 |
+
Dense(1, activation='sigmoid') # Binary classification, so using sigmoid activation
|
83 |
+
])
|
84 |
+
|
85 |
+
# Compile the model
|
86 |
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
87 |
+
|
88 |
+
# Train the model on the training dataset
|
89 |
+
model.fit(train_dataset, epochs=10)
|
90 |
+
|
91 |
+
# Call the function to plot the training histo
|
92 |
+
|
93 |
+
|
94 |
+
# Evaluate the model on the test dataset
|
95 |
+
test_loss, test_accuracy = model.evaluate(test_dataset)
|
96 |
+
print(f'Test Accuracy: {test_accuracy}')
|
97 |
+
import numpy as np
|
98 |
+
import matplotlib.pyplot as plt
|
99 |
+
|
100 |
+
# Assuming train_dataset and test_dataset are BatchDataset objects
|
101 |
+
|
102 |
+
# Function to get a batch of random images and labels
|
103 |
+
def get_random_batch(dataset, batch_size=5):
|
104 |
+
dataset_iter = iter(dataset)
|
105 |
+
images, labels = [], []
|
106 |
+
for _ in range(batch_size):
|
107 |
+
batch = next(dataset_iter)
|
108 |
+
images.append(batch[0][0])
|
109 |
+
labels.append(batch[1][0])
|
110 |
+
return np.array(images), np.array(labels)
|
111 |
+
|
112 |
+
# Get random images and labels from the test dataset
|
113 |
+
random_images, random_labels = get_random_batch(test_dataset)
|
114 |
+
|
115 |
+
# Predict the labels using the trained model
|
116 |
+
predictions = model.predict(random_images)
|
117 |
+
|
118 |
+
# Convert the predicted probabilities to binary predictions
|
119 |
+
binary_predictions = [1 if p > 0.5 else 0 for p in predictions]
|
120 |
+
|
121 |
+
# Map binary labels and predictions to their respective classes
|
122 |
+
class_labels = {0: 'Healthy', 1: 'Defective'}
|
123 |
+
true_labels = [class_labels[label] for label in random_labels]
|
124 |
+
predicted_labels = [class_labels[prediction] for prediction in binary_predictions]
|
125 |
+
|
126 |
+
# Display the images along with their true and predicted labels
|
127 |
+
plt.figure(figsize=(15, 5))
|
128 |
+
for i in range(5):
|
129 |
+
plt.subplot(1, 5, i+1)
|
130 |
+
plt.imshow(random_images[i])
|
131 |
+
plt.title(f'True: {true_labels[i]}\nPredicted: {predicted_labels[i]}')
|
132 |
+
plt.axis('off')
|
133 |
+
plt.show()
|