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

Upload 2 files

Browse files
Files changed (2) hide show
  1. RESNET_images.py +272 -0
  2. model_2.weights.h5 +3 -0
RESNET_images.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
model_2.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2bb30a1a6a3824eb47bc067d5c9b17bb8944c4efe2108a81973fb4aeee52d817
3
+ size 96013592