-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgg16.py
More file actions
265 lines (207 loc) · 8.4 KB
/
vgg16.py
File metadata and controls
265 lines (207 loc) · 8.4 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
"""VCG16_MInorProject.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1dW9g64rHLbw7VlHBK6XzTVI71kF0-1a5
"""
# Mount Google Drive to your Colab notebook
from google.colab import drive
drive.mount('/content/drive')
# The path to your zip file on Google Drive (using the correct name)
zip_file_path = "/content/drive/My Drive/archive (1).zip"
# The destination where the file will be unzipped
destination_path = "/content/datasets_unzipped/"
# Unzip the file
!unzip -q "{zip_file_path}" -d "{destination_path}"
# Define the base directory for the unzipped dataset
base_dir = "/content/datasets_unzipped/dataset/dataset/"
# Define the paths to the training and testing folders
train_dir = base_dir + "train"
test_dir = base_dir + "test"
# You can optionally list the contents to verify the paths are correct
!ls "{train_dir}"
# Import the necessary library
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define the image size and batch size
IMAGE_SIZE = (150, 150)
BATCH_SIZE = 32
# Create the data generator for the training data
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
# Create the data generator for the validation/testing data
test_datagen = ImageDataGenerator(rescale=1./255)
# Load images from the training directory
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical'
)
# Load images from the testing directory
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical'
)
# Import necessary libraries
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
# Load the VGG16 model pre-trained on ImageNet weights, excluding the top classification layers
base_model_vgg16 = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
# Freeze the layers of the base model so they are not trained
base_model_vgg16.trainable = False
# Add new layers for our classification task
x = base_model_vgg16.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
predictions = Dense(6, activation='softmax')(x) # 6 classes (fresh/rotten apple/banana/orange)
# Create the final model
model_vgg16 = Model(inputs=base_model_vgg16.input, outputs=predictions)
# Compile the model
model_vgg16.compile(optimizer=Adam(learning_rate=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])
model_vgg16.summary()
# Train the model
epochs = 10 # You can increase this for better performance
history_vgg16 = model_vgg16.fit(
train_generator,
steps_per_epoch=train_generator.samples // train_generator.batch_size,
epochs=epochs,
validation_data=test_generator,
validation_steps=test_generator.samples // test_generator.batch_size
)
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
# Get the true labels and predicted labels from the test generator
test_generator.reset()
y_true = test_generator.classes
y_pred_probs = model_vgg16.predict(test_generator)
y_pred = np.argmax(y_pred_probs, axis=1)
# Get the class labels from the generator
class_labels = list(test_generator.class_indices.keys())
# Generate the classification report
print("Classification Report:")
print(classification_report(y_true, y_pred, target_names=class_labels))
# Generate the confusion matrix
print("\nConfusion Matrix:")
print(confusion_matrix(y_true, y_pred))
# Calculate and print the overall accuracy in percentage
test_loss, test_accuracy = model_vgg16.evaluate(test_generator, verbose=0)
print(f"\nOverall Test Accuracy: {test_accuracy * 100:.2f}%")
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
# Get the true labels and predicted labels from the test generator
test_generator.reset()
y_true = test_generator.classes
y_pred_probs = model_vgg16.predict(test_generator)
y_pred = np.argmax(y_pred_probs, axis=1)
# Get the class labels from the generator
class_labels = list(test_generator.class_indices.keys())
# Generate the confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Plot the confusion matrix as a heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=class_labels,
yticklabels=class_labels)
plt.title("Confusion Matrix for VGG16 Model")
plt.ylabel("True Label")
plt.xlabel("Predicted Label")
plt.show()
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# 1. Recreate the test generator with a smaller, standard batch size
eval_datagen = ImageDataGenerator(rescale=1./255)
# This is the critical change to avoid the memory error
eval_generator = eval_datagen.flow_from_directory(
test_dir,
target_size=(150, 150),
batch_size=32, # Use a standard batch size
class_mode='categorical',
shuffle=False)
# 2. Get the True Labels and Predicted Labels in Batches
# We'll use a loop to get predictions and labels to avoid memory issues
y_true_list = []
y_pred_probs_list = []
# Calculate the number of steps to go through the entire dataset
steps = np.ceil(eval_generator.samples / eval_generator.batch_size)
print(f"Making predictions in {steps} batches...")
# Make predictions and collect true labels in a single loop
for i, (images, labels) in enumerate(eval_generator):
predictions = model_vgg16.predict(images, verbose=0)
y_pred_probs_list.append(predictions)
y_true_list.append(labels)
if i + 1 == steps:
break # Exit the loop after all batches are processed
# Concatenate the lists to create final numpy arrays
y_pred_probs = np.concatenate(y_pred_probs_list)
y_true_encoded = np.concatenate(y_true_list)
y_true = np.argmax(y_true_encoded, axis=1)
y_pred = np.argmax(y_pred_probs, axis=1)
# 3. Get the class labels from the generator
class_labels = list(eval_generator.class_indices.keys())
# 4. Generate the classification report
print("Classification Report:")
print(classification_report(y_true, y_pred, target_names=class_labels))
# 5. Generate the confusion matrix
print("\nConfusion Matrix:")
print(confusion_matrix(y_true, y_pred))
# 6. Calculate and print the overall accuracy
test_loss, test_accuracy = model_vgg16.evaluate(eval_generator, verbose=0)
print(f"\nOverall Test Accuracy: {test_accuracy * 100:.2f}%")
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
# Get the class labels from the generator
# Assuming 'eval_generator' is still available from the previous code
class_labels = list(eval_generator.class_indices.keys())
# Generate the confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Plot the confusion matrix as a heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=class_labels,
yticklabels=class_labels)
plt.title("Confusion Matrix for VGG16 Model")
plt.ylabel("True Label")
plt.xlabel("Predicted Label")
plt.show()
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Assuming 'test_dir' and 'model_vgg16' are still available in your notebook
# Recreate the test generator with shuffle=False to maintain a consistent order
eval_datagen = ImageDataGenerator(rescale=1./255)
eval_generator_for_roc = eval_datagen.flow_from_directory(
test_dir,
target_size=(150, 150),
batch_size=32, # Use a standard batch size
class_mode='categorical',
shuffle=False)
# Get the true labels and predicted probabilities
y_true_roc = eval_generator_for_roc.classes
y_pred_probs_roc = model_vgg16.predict(eval_generator_for_roc)
# You can save these arrays to a file for later use
np.save('y_true_vgg16.npy', y_true_roc)
np.save('y_pred_probs_vgg16.npy', y_pred_probs_roc)
print("True labels and predicted probabilities for VGG16 model have been saved.")
print(f"y_true_roc shape: {y_true_roc.shape}")
print(f"y_pred_probs_roc shape: {y_pred_probs_roc.shape}")