Image Classification
Keras
litav commited on
Commit
8521669
verified
1 Parent(s): a4e50f7

Update RESNET_images.py

Browse files
Files changed (1) hide show
  1. RESNET_images.py +209 -272
RESNET_images.py CHANGED
@@ -1,272 +1,209 @@
1
- # -*- coding: utf-8 -*-
2
- #images- 2800 training, test- 280
3
- # 10 epoch, batch= 32, kfold= 4
4
- # test accuracy 0.75
5
- # test loss 1.26
6
- # avarage accuracy 61.84
7
- #Classification Summary:
8
- #Real images correctly classified: 76
9
- #Real images incorrectly classified: 63
10
- #Fake images correctly classified: 135
11
- #Fake images incorrectly classified: 5
12
- ###########resnet- 诪拽驻讬讗 讞诇拽 诪讛砖讻讘讜转 讛讛转讞诇转讬讜转 讻诇讜诪专 讬砖 讗讬诪讜谉 注诇 砖讻讘讜转 专讘讜转##########
13
- #resnet_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
14
- # Freeze initial layers (optional)
15
- #for layer in resnet_model.layers[:17]:<<<<=
16
- # layer.trainable = False
17
- # Modify final layer
18
- #x = resnet_model.output
19
- #x = tf.keras.layers.Flatten()(x)
20
- #x = tf.keras.layers.Dense(64, activation='relu')(x)<<<<<<=
21
- #predictions = tf.keras.layers.Dense(1, activation='sigmoid')(x) # Binary classification
22
-
23
-
24
-
25
- ##images- 2800 training, test- 280
26
- #KFold(n_splits=4, batch_size = 32, epochs = 5
27
- #train only last layer
28
- #Classification Summary:
29
- #Average accuracy: 76.95%
30
- #Test Loss: 0.4741879999637604, Test Accuracy: 0.781361997127533
31
- #Real images correctly classified: 116
32
- #Real images incorrectly classified: 23
33
- #Fake images correctly classified: 102
34
- #Fake images incorrectly classified: 38
35
-
36
- #############
37
- #saves weight
38
- ##images- 2800 training, test- 280
39
- #KFold(n_splits=4, batch_size = 32, epochs = 5
40
- #train only last layer
41
- #Classification Summary:
42
- #Average accuracy: 77.11%
43
- #Test Loss: 0.47622182965278625, Test Accuracy: 0.7777777910232544
44
- #Classification Summary:
45
- #Real images correctly classified: 116
46
- #Real images incorrectly classified: 23
47
- #Fake images correctly classified: 101
48
- #Fake images incorrectly classified: 39
49
-
50
- #KFold(n_splits=4, batch_size = 32, epochs = 5
51
- #train only last layer
52
- #Test Loss: 0.5492929220199585, Test Accuracy: 0.7992831468582153
53
- #Average accuracy: 88.79%
54
- #Classification Summary:
55
- #Real images correctly classified: 129
56
- #Real images incorrectly classified: 10
57
- #Fake images correctly classified: 94
58
- #Fake images incorrectly classified: 46
59
- #Classification Report:
60
- # precision recall f1-score support
61
- #
62
- # Real 0.74 0.93 0.82 139
63
- # Fake 0.90 0.67 0.77 140
64
-
65
- import random
66
- import numpy as np
67
- import os
68
- import pandas as pd
69
- import cv2
70
- import warnings
71
- from sklearn.model_selection import KFold
72
- import tensorflow as tf
73
- from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
74
- import matplotlib.pyplot as plt
75
- from sklearn.metrics import precision_score, recall_score, f1_score, classification_report
76
-
77
- # Ensure h5py is installed
78
- import h5py
79
-
80
- # Set the random seed for numpy, tensorflow, and python built-in random module
81
- np.random.seed(42)
82
- tf.random.set_seed(42)
83
- random.seed(42)
84
-
85
- warnings.filterwarnings("ignore", category=UserWarning, message=".*iCCP:.*")
86
-
87
- # Define data paths
88
- train_real_folder = 'datasets/training_set/real/'
89
- train_fake_folder = 'datasets/training_set/fake/'
90
- test_real_folder = 'datasets/test_set/real/'
91
- test_fake_folder = 'datasets/test_set/fake/'
92
-
93
- # Load train image paths and labels
94
- train_image_paths = []
95
- train_labels = []
96
-
97
- # Load train_real image paths and labels
98
- for filename in os.listdir(train_real_folder):
99
- image_path = os.path.join(train_real_folder, filename)
100
- label = 0 # Real images have label 0
101
- train_image_paths.append(image_path)
102
- train_labels.append(label)
103
-
104
- # Load train_fake image paths and labels
105
- for filename in os.listdir(train_fake_folder):
106
- image_path = os.path.join(train_fake_folder, filename)
107
- label = 1 # Fake images have label 1
108
- train_image_paths.append(image_path)
109
- train_labels.append(label)
110
-
111
- # Load test image paths and labels
112
- test_image_paths = []
113
- test_labels = []
114
-
115
- # Load test_real image paths and labels
116
- for filename in os.listdir(test_real_folder):
117
- image_path = os.path.join(test_real_folder, filename)
118
- label = 0 # Assuming test real images are all real (label 0)
119
- test_image_paths.append(image_path)
120
- test_labels.append(label)
121
-
122
- # Load test_fake image paths and labels
123
- for filename in os.listdir(test_fake_folder):
124
- image_path = os.path.join(test_fake_folder, filename)
125
- label = 1 # Assuming test fake images are all fake (label 1)
126
- test_image_paths.append(image_path)
127
- test_labels.append(label)
128
-
129
- # Create DataFrames
130
- train_dataset = pd.DataFrame({'image_path': train_image_paths, 'label': train_labels})
131
- test_dataset = pd.DataFrame({'image_path': test_image_paths, 'label': test_labels})
132
-
133
- # Function to preprocess images
134
- def preprocess_image(image_path):
135
- """Loads, resizes, and normalizes an image."""
136
- image = cv2.imread(image_path)
137
- resized_image = cv2.resize(image, (224, 224)) # Target size defined here
138
- normalized_image = resized_image.astype(np.float32) / 255.0
139
- return normalized_image
140
-
141
- # Preprocess all images and convert labels to numpy arrays
142
- X = np.array([preprocess_image(path) for path in train_image_paths])
143
- Y = np.array(train_labels)
144
-
145
- # Define ResNet50 model
146
- resnet_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
147
-
148
- # Freeze all layers except the last one
149
- for layer in resnet_model.layers[:-1]:
150
- layer.trainable = False
151
-
152
- # Modify final layer
153
- x = resnet_model.output
154
- x = tf.keras.layers.Flatten()(x)
155
- predictions = tf.keras.layers.Dense(1, activation='sigmoid')(x) # Binary classification
156
-
157
- # Create new model with modified top
158
- new_model = tf.keras.models.Model(inputs=resnet_model.input, outputs=predictions)
159
-
160
- # Compile the new model
161
- new_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
162
-
163
- # Set parameters for cross-validation
164
- kf = KFold(n_splits=4, shuffle=True, random_state=42)
165
- batch_size = 32
166
- epochs = 5
167
- weights_file = 'model_2.weights.h5'
168
- # Lists to store accuracy and loss for each fold
169
- accuracy_per_fold = []
170
- loss_per_fold = []
171
-
172
- # Perform K-Fold Cross-Validation
173
- for train_index, val_index in kf.split(X):
174
- X_train, X_val = X[train_index], X[val_index]
175
- Y_train, Y_val = Y[train_index], Y[val_index]
176
-
177
- # Load weights if they exist
178
- if os.path.exists(weights_file):
179
- new_model.load_weights(weights_file)
180
- print(f"Loaded weights from {weights_file}")
181
-
182
- # Train only the last layer
183
- history = new_model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(X_val, Y_val))
184
-
185
- # Save weights after training
186
- new_model.save_weights(weights_file)
187
- print(f"Saved weights to {weights_file}")
188
-
189
- # Evaluate the model on the validation data
190
- val_loss, val_accuracy = new_model.evaluate(X_val, Y_val)
191
-
192
- # Store the accuracy score for this fold
193
- accuracy_per_fold.append(val_accuracy)
194
- loss_per_fold.append(val_loss)
195
- print(f'Fold accuracy: {val_accuracy*100:.2f}%')
196
- print(f'Fold loss: {val_loss:.4f}')
197
-
198
- # Print average accuracy and loss across all folds
199
- print(f'\nAverage accuracy across all folds: {np.mean(accuracy_per_fold)*100:.2f}%')
200
- print(f'Average loss across all folds: {np.mean(loss_per_fold):.4f}')
201
-
202
- # Evaluate the preprocessed test images using the final model
203
- test_loss, test_accuracy = new_model.evaluate(np.array([preprocess_image(path) for path in test_image_paths]), np.array(test_labels))
204
- print(f"\nTest Loss: {test_loss}, Test Accuracy: {test_accuracy}")
205
-
206
- # Predict labels for the test set
207
- predictions = new_model.predict(np.array([preprocess_image(path) for path in test_image_paths]))
208
- predicted_labels = (predictions > 0.5).astype(int).flatten()
209
-
210
- # Summarize the classification results
211
- true_real_correct = np.sum((np.array(test_labels) == 0) & (predicted_labels == 0))
212
- true_real_incorrect = np.sum((np.array(test_labels) == 0) & (predicted_labels == 1))
213
- true_fake_correct = np.sum((np.array(test_labels) == 1) & (predicted_labels == 1))
214
- true_fake_incorrect = np.sum((np.array(test_labels) == 1) & (predicted_labels == 0))
215
-
216
- print("\nClassification Summary:")
217
- print(f"Real images correctly classified: {true_real_correct}")
218
- print(f"Real images incorrectly classified: {true_real_incorrect}")
219
- print(f"Fake images correctly classified: {true_fake_correct}")
220
- print(f"Fake images incorrectly classified: {true_fake_incorrect}")
221
-
222
- # Print detailed classification report
223
- print("\nClassification Report:")
224
- print(classification_report(test_labels, predicted_labels, target_names=['Real', 'Fake']))
225
-
226
- # Plot confusion matrix
227
- cm = confusion_matrix(test_labels, predicted_labels)
228
- disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Real', 'Fake'])
229
- disp.plot(cmap=plt.cm.Blues)
230
- plt.title("Confusion Matrix")
231
- plt.show()
232
-
233
- # Plot training & validation accuracy values
234
- plt.figure(figsize=(12, 4))
235
- plt.subplot(1, 2, 1)
236
- plt.plot(history.history['accuracy'])
237
- plt.plot(history.history['val_accuracy'])
238
- plt.title('Model accuracy')
239
- plt.ylabel('Accuracy')
240
- plt.xlabel('Epoch')
241
- plt.legend(['Train', 'Validation'], loc='upper left')
242
- plt.xticks(np.arange(0, len(history.history['accuracy']), step=1), np.arange(1, len(history.history['accuracy']) + 1, step=1))
243
-
244
- # Plot training & validation loss values
245
- plt.subplot(1, 2, 2)
246
- plt.plot(history.history['loss'])
247
- plt.plot(history.history['val_loss'])
248
- plt.title('Model loss')
249
- plt.ylabel('Loss')
250
- plt.xlabel('Epoch')
251
- plt.legend(['Train', 'Validation'], loc='upper left')
252
- plt.xticks(np.arange(0, len(history.history['loss']), step=1), np.arange(1, len(history.history['loss']) + 1, step=1))
253
-
254
- plt.tight_layout()
255
- plt.show()
256
-
257
- # Plot validation accuracy and loss per fold
258
- plt.figure(figsize=(12, 4))
259
- plt.subplot(1, 2, 1)
260
- plt.plot(range(1, kf.get_n_splits() + 1), accuracy_per_fold, marker='o')
261
- plt.title('Validation Accuracy per Fold')
262
- plt.xlabel('Fold')
263
- plt.ylabel('Accuracy')
264
-
265
- plt.subplot(1, 2, 2)
266
- plt.plot(range(1, kf.get_n_splits() + 1), loss_per_fold, marker='o')
267
- plt.title('Validation Loss per Fold')
268
- plt.xlabel('Fold')
269
- plt.ylabel('Loss')
270
-
271
- plt.tight_layout()
272
- plt.show()
 
1
+
2
+ import random
3
+ import numpy as np
4
+ import os
5
+ import pandas as pd
6
+ import cv2
7
+ import warnings
8
+ from sklearn.model_selection import KFold
9
+ import tensorflow as tf
10
+ from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
11
+ import matplotlib.pyplot as plt
12
+ from sklearn.metrics import precision_score, recall_score, f1_score, classification_report
13
+
14
+ # Ensure h5py is installed
15
+ import h5py
16
+
17
+ # Set the random seed for numpy, tensorflow, and python built-in random module
18
+ np.random.seed(42)
19
+ tf.random.set_seed(42)
20
+ random.seed(42)
21
+
22
+ warnings.filterwarnings("ignore", category=UserWarning, message=".*iCCP:.*")
23
+
24
+ # Define data paths
25
+ train_real_folder = 'datasets/training_set/real/'
26
+ train_fake_folder = 'datasets/training_set/fake/'
27
+ test_real_folder = 'datasets/test_set/real/'
28
+ test_fake_folder = 'datasets/test_set/fake/'
29
+
30
+ # Load train image paths and labels
31
+ train_image_paths = []
32
+ train_labels = []
33
+
34
+ # Load train_real image paths and labels
35
+ for filename in os.listdir(train_real_folder):
36
+ image_path = os.path.join(train_real_folder, filename)
37
+ label = 0 # Real images have label 0
38
+ train_image_paths.append(image_path)
39
+ train_labels.append(label)
40
+
41
+ # Load train_fake image paths and labels
42
+ for filename in os.listdir(train_fake_folder):
43
+ image_path = os.path.join(train_fake_folder, filename)
44
+ label = 1 # Fake images have label 1
45
+ train_image_paths.append(image_path)
46
+ train_labels.append(label)
47
+
48
+ # Load test image paths and labels
49
+ test_image_paths = []
50
+ test_labels = []
51
+
52
+ # Load test_real image paths and labels
53
+ for filename in os.listdir(test_real_folder):
54
+ image_path = os.path.join(test_real_folder, filename)
55
+ label = 0 # Assuming test real images are all real (label 0)
56
+ test_image_paths.append(image_path)
57
+ test_labels.append(label)
58
+
59
+ # Load test_fake image paths and labels
60
+ for filename in os.listdir(test_fake_folder):
61
+ image_path = os.path.join(test_fake_folder, filename)
62
+ label = 1 # Assuming test fake images are all fake (label 1)
63
+ test_image_paths.append(image_path)
64
+ test_labels.append(label)
65
+
66
+ # Create DataFrames
67
+ train_dataset = pd.DataFrame({'image_path': train_image_paths, 'label': train_labels})
68
+ test_dataset = pd.DataFrame({'image_path': test_image_paths, 'label': test_labels})
69
+
70
+ # Function to preprocess images
71
+ def preprocess_image(image_path):
72
+ """Loads, resizes, and normalizes an image."""
73
+ image = cv2.imread(image_path)
74
+ resized_image = cv2.resize(image, (224, 224)) # Target size defined here
75
+ normalized_image = resized_image.astype(np.float32) / 255.0
76
+ return normalized_image
77
+
78
+ # Preprocess all images and convert labels to numpy arrays
79
+ X = np.array([preprocess_image(path) for path in train_image_paths])
80
+ Y = np.array(train_labels)
81
+
82
+ # Define ResNet50 model
83
+ resnet_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
84
+
85
+ # Freeze all layers except the last one
86
+ for layer in resnet_model.layers[:-1]:
87
+ layer.trainable = False
88
+
89
+ # Modify final layer
90
+ x = resnet_model.output
91
+ x = tf.keras.layers.Flatten()(x)
92
+ predictions = tf.keras.layers.Dense(1, activation='sigmoid')(x) # Binary classification
93
+
94
+ # Create new model with modified top
95
+ new_model = tf.keras.models.Model(inputs=resnet_model.input, outputs=predictions)
96
+
97
+ # Compile the new model
98
+ new_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
99
+
100
+ # Set parameters for cross-validation
101
+ kf = KFold(n_splits=4, shuffle=True, random_state=42)
102
+ batch_size = 32
103
+ epochs = 5
104
+ weights_file = 'model_2.weights.h5'
105
+ # Lists to store accuracy and loss for each fold
106
+ accuracy_per_fold = []
107
+ loss_per_fold = []
108
+
109
+ # Perform K-Fold Cross-Validation
110
+ for train_index, val_index in kf.split(X):
111
+ X_train, X_val = X[train_index], X[val_index]
112
+ Y_train, Y_val = Y[train_index], Y[val_index]
113
+
114
+ # Load weights if they exist
115
+ if os.path.exists(weights_file):
116
+ new_model.load_weights(weights_file)
117
+ print(f"Loaded weights from {weights_file}")
118
+
119
+ # Train only the last layer
120
+ history = new_model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(X_val, Y_val))
121
+
122
+ # Save weights after training
123
+ new_model.save_weights(weights_file)
124
+ print(f"Saved weights to {weights_file}")
125
+
126
+ # Evaluate the model on the validation data
127
+ val_loss, val_accuracy = new_model.evaluate(X_val, Y_val)
128
+
129
+ # Store the accuracy score for this fold
130
+ accuracy_per_fold.append(val_accuracy)
131
+ loss_per_fold.append(val_loss)
132
+ print(f'Fold accuracy: {val_accuracy*100:.2f}%')
133
+ print(f'Fold loss: {val_loss:.4f}')
134
+
135
+ # Print average accuracy and loss across all folds
136
+ print(f'\nAverage accuracy across all folds: {np.mean(accuracy_per_fold)*100:.2f}%')
137
+ print(f'Average loss across all folds: {np.mean(loss_per_fold):.4f}')
138
+
139
+ # Evaluate the preprocessed test images using the final model
140
+ test_loss, test_accuracy = new_model.evaluate(np.array([preprocess_image(path) for path in test_image_paths]), np.array(test_labels))
141
+ print(f"\nTest Loss: {test_loss}, Test Accuracy: {test_accuracy}")
142
+
143
+ # Predict labels for the test set
144
+ predictions = new_model.predict(np.array([preprocess_image(path) for path in test_image_paths]))
145
+ predicted_labels = (predictions > 0.5).astype(int).flatten()
146
+
147
+ # Summarize the classification results
148
+ true_real_correct = np.sum((np.array(test_labels) == 0) & (predicted_labels == 0))
149
+ true_real_incorrect = np.sum((np.array(test_labels) == 0) & (predicted_labels == 1))
150
+ true_fake_correct = np.sum((np.array(test_labels) == 1) & (predicted_labels == 1))
151
+ true_fake_incorrect = np.sum((np.array(test_labels) == 1) & (predicted_labels == 0))
152
+
153
+ print("\nClassification Summary:")
154
+ print(f"Real images correctly classified: {true_real_correct}")
155
+ print(f"Real images incorrectly classified: {true_real_incorrect}")
156
+ print(f"Fake images correctly classified: {true_fake_correct}")
157
+ print(f"Fake images incorrectly classified: {true_fake_incorrect}")
158
+
159
+ # Print detailed classification report
160
+ print("\nClassification Report:")
161
+ print(classification_report(test_labels, predicted_labels, target_names=['Real', 'Fake']))
162
+
163
+ # Plot confusion matrix
164
+ cm = confusion_matrix(test_labels, predicted_labels)
165
+ disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Real', 'Fake'])
166
+ disp.plot(cmap=plt.cm.Blues)
167
+ plt.title("Confusion Matrix")
168
+ plt.show()
169
+
170
+ # Plot training & validation accuracy values
171
+ plt.figure(figsize=(12, 4))
172
+ plt.subplot(1, 2, 1)
173
+ plt.plot(history.history['accuracy'])
174
+ plt.plot(history.history['val_accuracy'])
175
+ plt.title('Model accuracy')
176
+ plt.ylabel('Accuracy')
177
+ plt.xlabel('Epoch')
178
+ plt.legend(['Train', 'Validation'], loc='upper left')
179
+ plt.xticks(np.arange(0, len(history.history['accuracy']), step=1), np.arange(1, len(history.history['accuracy']) + 1, step=1))
180
+
181
+ # Plot training & validation loss values
182
+ plt.subplot(1, 2, 2)
183
+ plt.plot(history.history['loss'])
184
+ plt.plot(history.history['val_loss'])
185
+ plt.title('Model loss')
186
+ plt.ylabel('Loss')
187
+ plt.xlabel('Epoch')
188
+ plt.legend(['Train', 'Validation'], loc='upper left')
189
+ plt.xticks(np.arange(0, len(history.history['loss']), step=1), np.arange(1, len(history.history['loss']) + 1, step=1))
190
+
191
+ plt.tight_layout()
192
+ plt.show()
193
+
194
+ # Plot validation accuracy and loss per fold
195
+ plt.figure(figsize=(12, 4))
196
+ plt.subplot(1, 2, 1)
197
+ plt.plot(range(1, kf.get_n_splits() + 1), accuracy_per_fold, marker='o')
198
+ plt.title('Validation Accuracy per Fold')
199
+ plt.xlabel('Fold')
200
+ plt.ylabel('Accuracy')
201
+
202
+ plt.subplot(1, 2, 2)
203
+ plt.plot(range(1, kf.get_n_splits() + 1), loss_per_fold, marker='o')
204
+ plt.title('Validation Loss per Fold')
205
+ plt.xlabel('Fold')
206
+ plt.ylabel('Loss')
207
+
208
+ plt.tight_layout()
209
+ plt.show()