noor13 commited on
Commit
417eb05
1 Parent(s): 181e339

Upload tex_poisoning.py

Browse files
Files changed (1) hide show
  1. tex_poisoning.py +835 -0
tex_poisoning.py ADDED
@@ -0,0 +1,835 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[27]:
5
+
6
+
7
+ import pandas as pd
8
+ import numpy as np
9
+ get_ipython().run_line_magic('matplotlib', 'inline')
10
+ import seaborn as sns
11
+ sns.set(style="whitegrid")
12
+ import os
13
+ import glob as gb
14
+ import cv2
15
+ import tensorflow as tf
16
+ import keras
17
+ import random
18
+ from tensorflow.keras import layers, models
19
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
20
+ import matplotlib.pyplot as plt
21
+ import matplotlib.image as mpimg
22
+ from tensorflow.keras.models import Sequential
23
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten ,Dropout ,Input , BatchNormalization ,GlobalAveragePooling2D
24
+ from tensorflow.keras.utils import to_categorical
25
+ from keras.optimizers import Adam
26
+ from PIL import Image
27
+ from tensorflow.keras.callbacks import EarlyStopping
28
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
29
+ from sklearn.metrics import classification_report, confusion_matrix , accuracy_score , ConfusionMatrixDisplay
30
+ from tensorflow.keras.metrics import Precision , Recall
31
+ from keras.metrics import Precision, Recall
32
+ import struct
33
+ from array import array
34
+ from os.path import join
35
+ from keras.models import load_model
36
+ from skimage.exposure import rescale_intensity
37
+ from sklearn.preprocessing import OneHotEncoder
38
+ from keras.callbacks import EarlyStopping, ReduceLROnPlateau ,LearningRateScheduler
39
+ from sklearn.preprocessing import LabelEncoder
40
+ from sklearn.model_selection import train_test_split
41
+ from PIL import Image
42
+
43
+
44
+ # In[2]:
45
+
46
+
47
+ from keras.datasets import cifar100
48
+
49
+
50
+ # In[3]:
51
+
52
+
53
+ (x_train, y_train), (x_test, y_test) = cifar100.load_data()
54
+
55
+
56
+ # In[4]:
57
+
58
+
59
+ np.save('x_train.npy', x_train)
60
+ np.save('y_train.npy', y_train)
61
+ np.save('x_test.npy', x_test)
62
+ np.save('y_test.npy', y_test)
63
+
64
+
65
+ # In[5]:
66
+
67
+
68
+ print(f"x_train shape: {x_train.shape}")
69
+ print(f"y_train shape: {y_train.shape}")
70
+ print(f"x_test shape: {x_test.shape}")
71
+ print(f"y_test shape: {y_test.shape}")
72
+
73
+
74
+ # In[6]:
75
+
76
+
77
+ def preprocess_data(x, y):
78
+ x = tf.cast(x, tf.float32) / 255.0
79
+ return x, y
80
+
81
+ (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar100.load_data()
82
+ y_train_encoded = tf.keras.utils.to_categorical(y_train, num_classes=100)
83
+ y_test_encoded = tf.keras.utils.to_categorical(y_test, num_classes=100)
84
+
85
+ train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train_encoded))
86
+
87
+ train_dataset = train_dataset.map(preprocess_data)
88
+
89
+ batch_size = 64
90
+ train_dataset = train_dataset.shuffle(buffer_size=10000).batch(batch_size).prefetch(tf.data.AUTOTUNE)
91
+
92
+ for batch in train_dataset.take(1):
93
+ images, labels = batch
94
+ print(images.shape, labels.shape)
95
+
96
+
97
+ # In[7]:
98
+
99
+
100
+ y_train_encoded = to_categorical(y_train, num_classes=100)
101
+ y_test_encoded = to_categorical(y_test, num_classes=100)
102
+
103
+
104
+ # In[34]:
105
+
106
+
107
+ import numpy as np
108
+ import matplotlib.pyplot as plt
109
+ from tensorflow.keras.datasets import cifar100
110
+ import tensorflow as tf
111
+ from PIL import Image
112
+ import cv2
113
+
114
+ # تحميل بيانات CIFAR-100
115
+ (x_train, _), _ = cifar100.load_data()
116
+
117
+ # اختيار بعض الصور العشوائية
118
+ num_images = 5
119
+ random_indices = np.random.choice(len(x_train), num_images)
120
+ sample_images = x_train[random_indices]
121
+
122
+ # تحويل الصور من Tensor إلى NumPy إذا لزم الأمر
123
+ sample_images_np = [img if isinstance(img, np.ndarray) else img.numpy() for img in sample_images]
124
+
125
+ # تحويل الصور إلى نوع uint8
126
+ sample_images_np = [img.astype(np.uint8) for img in sample_images_np]
127
+
128
+ # تحسين دقة الصورة باستخدام PIL
129
+ def upscale_image(image, scale_factor):
130
+ img = Image.fromarray(image)
131
+ new_size = (img.width * scale_factor, img.height * scale_factor)
132
+ img_upscaled = img.resize(new_size, Image.BICUBIC) # استخدام تقنية الاستيفاء البعدي
133
+ return np.array(img_upscaled)
134
+
135
+ # تطبيق فلتر حاد على الصورة
136
+ def sharpen_image(image):
137
+ kernel = np.array([[0, -1, 0],
138
+ [-1, 5, -1],
139
+ [0, -1, 0]])
140
+ sharpened = cv2.filter2D(src=image, ddepth=-1, kernel=kernel)
141
+ return sharpened
142
+
143
+ # عرض الصور الأصلية، المكبرة والمحسنة بالفلتر الحاد
144
+ plt.figure(figsize=(20, 10), dpi=100)
145
+ for i in range(num_images):
146
+ # عرض الصورة الأصلية
147
+ plt.subplot(3, num_images, i + 1)
148
+ plt.imshow(sample_images_np[i])
149
+ plt.title(f"Original Image {i+1}", fontsize=16)
150
+ plt.axis('off')
151
+
152
+ # عرض الصورة المكبرة
153
+ img_upscaled = upscale_image(sample_images_np[i], 4)
154
+ plt.subplot(3, num_images, num_images + i + 1)
155
+ plt.imshow(img_upscaled)
156
+ plt.title(f"Upscaled Image {i+1}", fontsize=16)
157
+ plt.axis('off')
158
+
159
+ plt.tight_layout()
160
+ plt.show()
161
+
162
+
163
+ # In[37]:
164
+
165
+
166
+
167
+ import matplotlib.pyplot as plt
168
+ import tensorflow as tf
169
+ from tensorflow.keras.models import Sequential
170
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Dropout, GlobalAveragePooling2D, Dense, Input
171
+ from tensorflow.keras.optimizers import Adam
172
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
173
+ from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, LearningRateScheduler
174
+
175
+
176
+ # تحويل التسميات إلى تصنيف فئة
177
+ y_train_encoded = tf.keras.utils.to_categorical(y_train, num_classes=100)
178
+ y_test_encoded = tf.keras.utils.to_categorical(y_test, num_classes=100)
179
+
180
+ # إعدادات تعزيز البيانات
181
+ datagen = ImageDataGenerator(
182
+ rotation_range=20,
183
+ width_shift_range=0.2,
184
+ height_shift_range=0.2,
185
+ horizontal_flip=True,
186
+ zoom_range=0.2,
187
+ shear_range=0.1,
188
+ brightness_range=[0.8, 1.2],
189
+ channel_shift_range=0.1,
190
+ fill_mode='nearest' # استخدام طريقة الملء للحفاظ على جودة الصور
191
+ )
192
+
193
+ # ملاءمة بيانات التدريب على المعزز
194
+ datagen.fit(x_train)
195
+
196
+ # وظيفة لتقليل معدل التعلم كل 10 حلقات
197
+ def scheduler(epoch, lr):
198
+ if epoch % 10 == 0 and epoch != 0:
199
+ lr = lr / 2
200
+ return lr
201
+
202
+ # إعداد الإيقاف المبكر وخفض معدل التعلم
203
+ early_stopping = EarlyStopping(
204
+ monitor='val_loss',
205
+ patience=10,
206
+ restore_best_weights=True,
207
+ verbose=1
208
+ )
209
+
210
+ reduce_lr = ReduceLROnPlateau(
211
+ monitor='val_loss',
212
+ factor=0.5,
213
+ patience=5,
214
+ min_lr=1e-6,
215
+ verbose=1
216
+ )
217
+
218
+ # بناء النموذج المحسن باستخدام Input
219
+ model = Sequential([
220
+ Input(shape=(32, 32, 3)),
221
+ Conv2D(64, (3, 3), activation='relu', padding='same'),
222
+ BatchNormalization(),
223
+ MaxPooling2D(pool_size=(2, 2)),
224
+ Dropout(0.3),
225
+
226
+ Conv2D(128, (3, 3), activation='relu', padding='same'),
227
+ BatchNormalization(),
228
+ MaxPooling2D(pool_size=(2, 2)),
229
+ Dropout(0.3),
230
+
231
+ Conv2D(256, (3, 3), activation='relu', padding='same'),
232
+ BatchNormalization(),
233
+ MaxPooling2D(pool_size=(2, 2)),
234
+ Dropout(0.3),
235
+
236
+ Conv2D(512, (3, 3), activation='relu', padding='same'),
237
+ BatchNormalization(),
238
+ Dropout(0.4),
239
+
240
+ Conv2D(512, (3, 3), activation='relu', padding='same'),
241
+ BatchNormalization(),
242
+ MaxPooling2D(pool_size=(2, 2)),
243
+ Dropout(0.4),
244
+
245
+ GlobalAveragePooling2D(),
246
+ Dense(1024, activation='relu'),
247
+ Dropout(0.5),
248
+ Dense(512, activation='relu'),
249
+ Dropout(0.5),
250
+ Dense(100, activation='softmax')
251
+ ])
252
+
253
+ # تجميع النموذج مع استخدام Adam
254
+ model.compile(
255
+ loss='categorical_crossentropy',
256
+ optimizer=Adam(learning_rate=0.001),
257
+ metrics=['accuracy', tf.keras.metrics.Precision(name='precision'), tf.keras.metrics.Recall(name='recall')]
258
+ )
259
+
260
+ # تدريب النموذج
261
+ history = model.fit(
262
+ datagen.flow(x_train, y_train_encoded, batch_size=64),
263
+ epochs=50,
264
+ validation_data=(x_test, y_test_encoded),
265
+ verbose=1,
266
+ callbacks=[LearningRateScheduler(scheduler), early_stopping, reduce_lr]
267
+ )
268
+
269
+ # حفظ النموذج المدرب
270
+ model.save('original_model.h5')
271
+
272
+
273
+ # In[38]:
274
+
275
+
276
+ model = load_model('original_model.h5')
277
+
278
+ # تقييم النموذج على بيانات الاختبار
279
+ loss, accuracy, precision, recall = model.evaluate(x_test, y_test_encoded, verbose=1)
280
+ print(f"Test Accuracy: {accuracy * 100:.2f}%")
281
+ print(f"Test Precision: {precision * 100:.2f}%")
282
+ print(f"Test Recall: {recall * 100:.2f}%")
283
+ print(f"Test Loss: {loss * 100:.4f}%")
284
+
285
+
286
+ # In[39]:
287
+
288
+
289
+ model = load_model('original_model.h5')
290
+
291
+ # اختيار بعض الصور العشوائية من مجموعة الاختبار
292
+ num_images = 5
293
+ random_indices = np.random.choice(len(x_test), num_images)
294
+ sample_images_test = x_test[random_indices]
295
+
296
+ # تحويل الصور من Tensor إلى NumPy
297
+ sample_images_test_np = [img.numpy() if isinstance(img, tf.Tensor) else img for img in sample_images_test]
298
+
299
+ # تحويل الصور إلى نوع uint8
300
+ sample_images_test_np = [img.astype(np.uint8) for img in sample_images_test_np]
301
+
302
+ # تحسين دقة الصورة باستخدام PIL
303
+ from PIL import Image
304
+
305
+ def upscale_image(image, scale_factor):
306
+ img = Image.fromarray(image)
307
+ new_size = (img.width * scale_factor, img.height * scale_factor)
308
+ img_upscaled = img.resize(new_size, Image.BICUBIC) # استخدام تقنية الاستيفاء البعدي
309
+ return np.array(img_upscaled)
310
+
311
+ # عرض الصور الأصلية والمحسنة من مجموعة الاختبار
312
+ plt.figure(figsize=(20, 10), dpi=100)
313
+ for i in range(num_images):
314
+ plt.subplot(2, num_images, i + 1)
315
+ plt.imshow(sample_images_test_np[i])
316
+ plt.title(f"Original Test Image {i+1}", fontsize=16)
317
+ plt.axis('off')
318
+ plt.subplot(2, num_images, i + 1 + num_images)
319
+ img_upscaled = upscale_image(sample_images_test_np[i], 4)
320
+ plt.imshow(img_upscaled)
321
+ plt.title(f"Upscaled Test Image {i+1}", fontsize=16)
322
+ plt.axis('off')
323
+ plt.tight_layout()
324
+ plt.show()
325
+
326
+
327
+ # In[40]:
328
+
329
+
330
+ model = load_model('original_model.h5')
331
+
332
+ # رسم منحنيات التدريب
333
+ plt.figure(figsize=(18, 10))
334
+
335
+ # Accuracy
336
+ plt.subplot(2, 2, 1)
337
+ plt.plot(history.history['accuracy'], label='Accuracy')
338
+ plt.plot(history.history['val_accuracy'], label='Val Accuracy')
339
+ plt.xlabel('Epochs')
340
+ plt.ylabel('Accuracy')
341
+ plt.legend()
342
+ plt.title('Training and Validation Accuracy')
343
+
344
+ # Loss
345
+ plt.subplot(2, 2, 2)
346
+ plt.plot(history.history['loss'], label='Loss')
347
+ plt.plot(history.history['val_loss'], label='Val Loss')
348
+ plt.xlabel('Epochs')
349
+ plt.ylabel('Loss')
350
+ plt.legend()
351
+ plt.title('Training and Validation Loss')
352
+
353
+ # Precision
354
+ plt.subplot(2, 2, 3)
355
+ plt.plot(history.history['precision'], label='Precision')
356
+ plt.plot(history.history['val_precision'], label='Val Precision')
357
+ plt.xlabel('Epochs')
358
+ plt.ylabel('Precision')
359
+ plt.legend()
360
+ plt.title('Training and Validation Precision')
361
+
362
+ # Recall
363
+ plt.subplot(2, 2, 4)
364
+ plt.plot(history.history['recall'], label='Recall')
365
+ plt.plot(history.history['val_recall'], label='Val Recall')
366
+ plt.xlabel('Epochs')
367
+ plt.ylabel('Recall')
368
+ plt.legend()
369
+ plt.title('Training and Validation Recall')
370
+
371
+ plt.tight_layout()
372
+ plt.show()
373
+
374
+
375
+ # In[41]:
376
+
377
+
378
+ model = load_model('original_model.h5')
379
+
380
+ model.summary()
381
+
382
+
383
+ # In[44]:
384
+
385
+
386
+ # تحميل بيانات CIFAR-100
387
+ (x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
388
+
389
+ # تحويل التسميات إلى ترميز الفئات الثنائية
390
+ num_classes = 100
391
+ y_train_encoded = to_categorical(y_train, num_classes=num_classes)
392
+ y_test_encoded = to_categorical(y_test, num_classes=num_classes)
393
+
394
+ # دالة لتحويلات الملمس
395
+ def apply_texture_transformations(image):
396
+ blurred_image = gaussian_filter(image, sigma=0.5)
397
+ laplacian_image = laplace(blurred_image, mode='reflect') / 4.0
398
+ noise = np.random.normal(0, 0.01, image.shape) * 255
399
+ noisy_image = image + noise
400
+ transformed_image = 0.8 * image + 0.1 * laplacian_image + 0.1 * noisy_image
401
+ transformed_image = np.clip(transformed_image, 0, 255).astype(np.uint8)
402
+ return transformed_image
403
+
404
+ # نسبة التسميم
405
+ poison_fraction = 0.5
406
+ num_poisoned = int(poison_fraction * len(x_train))
407
+ poisoned_indices = np.arange(len(x_train))
408
+ x_poison_part = x_train[poisoned_indices]
409
+ y_poison_encoded_part = y_train_encoded[poisoned_indices]
410
+ x_poisoned = np.array([apply_texture_transformations(img) for img in x_poison_part])
411
+ x_train_combined = x_poisoned
412
+ y_train_encoded_combined = y_poison_encoded_part
413
+
414
+ # إعداد مولد بيانات التعزيز
415
+ datagen = ImageDataGenerator(
416
+ rotation_range=40,
417
+ width_shift_range=0.3,
418
+ height_shift_range=0.3,
419
+ shear_range=0.3,
420
+ zoom_range=0.3,
421
+ horizontal_flip=True,
422
+ fill_mode='nearest',
423
+ brightness_range=[0.8, 1.2],
424
+ channel_shift_range=0.1
425
+ )
426
+
427
+ datagen.fit(x_train_combined)
428
+
429
+ # إعداد الإيقاف المبكر وتقليل معدل التعلم
430
+ early_stopping = EarlyStopping(
431
+ monitor='val_loss',
432
+ patience=10,
433
+ restore_best_weights=True,
434
+ verbose=1
435
+ )
436
+
437
+ reduce_lr = ReduceLROnPlateau(
438
+ monitor='val_loss',
439
+ factor=0.5,
440
+ patience=5,
441
+ min_lr=1e-6,
442
+ verbose=1
443
+ )
444
+
445
+ # تحميل النموذج الأصلي
446
+ model = load_model('original_model.h5')
447
+
448
+ # إعادة تجميع النموذج الأصلي مع البيانات المسمومة
449
+ model.compile(
450
+ loss='categorical_crossentropy',
451
+ optimizer=Adam(learning_rate=0.001),
452
+ metrics=['accuracy', 'precision', 'recall'] # تم إزالة 'loss' من المقاييس
453
+ )
454
+
455
+ # تدريب النموذج على البيانات المسمومة
456
+ history = model.fit(
457
+ datagen.flow(x_train_combined, y_train_encoded_combined, batch_size=64),
458
+ epochs=20,
459
+ validation_data=(x_test, y_test_encoded),
460
+ verbose=1,
461
+ callbacks=[early_stopping, reduce_lr]
462
+ )
463
+
464
+ # حفظ النموذج باستخدام الدالة المعرفة
465
+ model.save('texture_transformed_model.h5')
466
+
467
+
468
+ # In[47]:
469
+
470
+
471
+ model = load_model('texture_transformed_model.h5')
472
+
473
+ # تقييم النموذج على بيانات الاختبار
474
+ loss, accuracy, precision, recall = model.evaluate(x_test, y_test_encoded, verbose=1)
475
+ print(f"Test Accuracy: {accuracy * 100:.2f}%")
476
+ print(f"Test Precision: {precision * 100:.2f}%")
477
+ print(f"Test Recall: {recall * 100:.2f}%")
478
+ print(f"Test Loss: {loss * 100:.4f}%")
479
+
480
+
481
+ # In[51]:
482
+
483
+
484
+ model = load_model('texture_transformed_model.h5')
485
+
486
+ # تقييم النموذج لبناء المقاييس
487
+ initial_evaluation = model.evaluate(x_test, y_test_encoded, verbose=1)
488
+ print(f"Initial evaluation - Loss: {initial_evaluation[0]}, Accuracy: {initial_evaluation[1]}")
489
+
490
+
491
+ # In[62]:
492
+
493
+
494
+
495
+ # تحميل النموذج المحول
496
+ model = load_model('texture_transformed_model.h5')
497
+
498
+ # تحديد عدد الصور للعرض
499
+ num_samples = 6
500
+ random_indices = np.random.choice(len(x_train), num_samples, replace=False)
501
+
502
+ plt.figure(figsize=(15, 6))
503
+ for i, idx in enumerate(random_indices):
504
+ # عرض الصور الأصلية
505
+ plt.subplot(2, num_samples, i + 1)
506
+ plt.imshow(x_train[idx].astype('uint8'))
507
+ plt.title(f'Original {y_train[idx][0]}')
508
+ plt.axis('off')
509
+
510
+ # عرض الصور المسممة
511
+ plt.subplot(2, num_samples, i + 1 + num_samples)
512
+ plt.imshow(x_poisoned[idx].astype('uint8'))
513
+ plt.title(f'Poisoned {y_train[idx][0]}')
514
+ plt.axis('off')
515
+
516
+ plt.tight_layout()
517
+ plt.show()
518
+
519
+
520
+ # In[48]:
521
+
522
+
523
+ model = load_model('texture_transformed_model.h5')
524
+
525
+ # عرض الصور الأصلية والمسممة للمقارنة
526
+ num_samples = 6
527
+ random_indices = np.random.choice(len(x_train), num_samples, replace=False)
528
+
529
+ plt.figure(figsize=(15, 6))
530
+ for i, idx in enumerate(random_indices):
531
+ plt.subplot(2, num_samples, i + 1)
532
+ plt.imshow(x_train[idx].astype('uint8'))
533
+ plt.title(f'Original {y_train[idx][0]}')
534
+ plt.axis('off')
535
+
536
+ plt.subplot(2, num_samples, i + 1 + num_samples)
537
+ plt.imshow(x_poisoned[idx].astype('uint8'))
538
+ plt.title(f'Poisoned {y_train[idx][0]}')
539
+ plt.axis('off')
540
+ plt.show()
541
+
542
+
543
+ # In[49]:
544
+
545
+
546
+ model = load_model('texture_transformed_model.h5')
547
+
548
+ plt.tight_layout()
549
+ plt.show()
550
+
551
+ # عرض النتائج من التدريب
552
+ plt.figure(figsize=(12, 6))
553
+ plt.subplot(2, 2, 1)
554
+ plt.plot(history.history['accuracy'], label='Training Accuracy')
555
+ plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
556
+ plt.title('Accuracy over epochs')
557
+ plt.legend()
558
+
559
+ plt.subplot(2, 2, 2)
560
+ plt.plot(history.history['loss'], label='Training Loss')
561
+ plt.plot(history.history['val_loss'], label='Validation Loss')
562
+ plt.title('Loss over epochs')
563
+ plt.legend()
564
+
565
+ plt.subplot(2, 2, 3)
566
+ plt.plot(history.history['precision'], label='Training Precision')
567
+ plt.plot(history.history['val_precision'], label='Validation Precision')
568
+ plt.title('Precision over epochs')
569
+ plt.legend()
570
+
571
+ plt.subplot(2, 2, 4)
572
+ plt.plot(history.history['recall'], label='Training Recall')
573
+ plt.plot(history.history['val_recall'], label='Validation Recall')
574
+ plt.title('Recall over epochs')
575
+ plt.legend()
576
+
577
+ plt.tight_layout()
578
+ plt.show()
579
+
580
+
581
+ # In[64]:
582
+
583
+
584
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
585
+ from scipy.ndimage import gaussian_filter, laplace
586
+ import tensorflow as tf
587
+
588
+ # Load the trained model
589
+ model = load_model('texture_transformed_model.h5')
590
+
591
+ # Ensure the model is compiled with metrics
592
+ model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
593
+
594
+ # Function for texture transformations with reduced effects
595
+ def apply_texture_transformations(image):
596
+ blurred_image = gaussian_filter(image, sigma=0.05) # Reduce sigma to minimum
597
+ laplacian_image = laplace(blurred_image, mode='reflect') / 100.0 # Significantly reduce laplace effect
598
+ noise = np.random.normal(0, 0.001, image.shape) * 255 # Significantly reduce noise
599
+ noisy_image = image + noise
600
+ transformed_image = 0.98 * image + 0.01 * laplacian_image + 0.01 * noisy_image # Minimize transformation effects
601
+ transformed_image = np.clip(transformed_image, 0, 255).astype(np.uint8)
602
+ return transformed_image
603
+
604
+ # Function to resize image while maintaining quality using Bicubic Interpolation
605
+ def resize_image_with_quality(image, target_size):
606
+ resized_image = cv2.resize(image, target_size, interpolation=cv2.INTER_CUBIC)
607
+ return resized_image
608
+
609
+ # Function to load and process external images while retaining original size
610
+ def load_and_preprocess_image(image_path):
611
+ if not os.path.exists(image_path):
612
+ print(f"File not found: {image_path}")
613
+ return None
614
+ img = load_img(image_path)
615
+ img_array = img_to_array(img)
616
+ original_shape = img_array.shape[:2] # Save original dimensions without channels
617
+ resized_image = resize_image_with_quality(img_array, (224, 224)) # Resize to larger size to retain details
618
+ resized_image = resized_image.astype('float32') / 255.0 # Normalize the image
619
+ return resized_image, original_shape
620
+
621
+ # Paths to external images
622
+ image_paths = [
623
+ r'C:\Users\Lenovo\Desktop\jaguar.jpeg',
624
+ r'C:\Users\Lenovo\Desktop\images.jpeg',
625
+ r'C:\Users\Lenovo\Desktop\tree.jpeg'
626
+ ]
627
+
628
+ # Load and process external images while retaining original dimensions
629
+ external_images_info = [load_and_preprocess_image(image_path) for image_path in image_paths]
630
+ external_images_info = [info for info in external_images_info if info is not None]
631
+
632
+ # Check if any images were loaded
633
+ if not external_images_info:
634
+ print("No images were loaded. Please check your image paths.")
635
+ else:
636
+ external_images, original_shapes = zip(*external_images_info)
637
+ external_images = np.array(external_images)
638
+
639
+ # Apply texture transformations
640
+ external_images_transformed = np.array([apply_texture_transformations(img * 255) / 255.0 for img in external_images])
641
+
642
+ # Resize transformed images to their original size
643
+ external_images_transformed_resized = []
644
+ for i, transformed_image in enumerate(external_images_transformed):
645
+ original_shape = original_shapes[i] # Extract original dimensions
646
+ transformed_resized = resize_image_with_quality(transformed_image * 255, original_shape[::-1]) # Note CV2 dimensions (width x height)
647
+ external_images_transformed_resized.append(transformed_resized)
648
+
649
+ # Define the prediction function
650
+ @tf.function
651
+ def model_predict(model, input_data):
652
+ return model(input_data, training=False)
653
+
654
+ # Conduct predictions
655
+ predictions = model_predict(model, external_images_transformed)
656
+
657
+ # Display results
658
+ for i, image_path in enumerate(image_paths):
659
+ if os.path.exists(image_path):
660
+ plt.figure(figsize=(10, 5))
661
+
662
+ # Display the original image
663
+ plt.subplot(1, 2, 1)
664
+ original_img = load_img(image_path)
665
+ plt.imshow(original_img)
666
+ plt.title('Original Image')
667
+ plt.axis('off')
668
+
669
+ # Display the poisoned image
670
+ plt.subplot(1, 2, 2)
671
+ poisoned_img = external_images_transformed_resized[i]
672
+ plt.imshow(poisoned_img.astype(np.uint8))
673
+ plt.title('Poisoned Image')
674
+ plt.axis('off')
675
+
676
+ plt.suptitle(f'Prediction: {np.argmax(predictions[i])}')
677
+ plt.show()
678
+
679
+
680
+ # In[63]:
681
+
682
+
683
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
684
+ from scipy.ndimage import gaussian_filter, laplace
685
+
686
+ # تحميل النموذج المدرب
687
+ model = load_model('texture_transformed_model.h5')
688
+
689
+ # دالة لتحويلات النسيج مع تقليل التأثيرات
690
+ def apply_texture_transformations(image):
691
+ blurred_image = gaussian_filter(image, sigma=0.05)
692
+ laplacian_image = laplace(blurred_image, mode='reflect') / 100.0
693
+ noise = np.random.normal(0, 0.001, image.shape) * 255
694
+ noisy_image = image + noise
695
+ transformed_image = 0.98 * image + 0.01 * laplacian_image + 0.01 * noisy_image
696
+ transformed_image = np.clip(transformed_image, 0, 255).astype(np.uint8)
697
+ return transformed_image
698
+
699
+ # دالة لإعادة تشكيل الصورة مع الحفاظ على الجودة باستخدام Bicubic Interpolation
700
+ def resize_image_with_quality(image, target_size):
701
+ resized_image = cv2.resize(image, target_size, interpolation=cv2.INTER_CUBIC)
702
+ return resized_image
703
+
704
+ # دالة لتحميل ومعالجة الصور الخارجية مع الاحتفاظ بحجمها الأصلي
705
+ def load_and_preprocess_image(image_path):
706
+ if not os.path.exists(image_path):
707
+ print(f"File not found: {image_path}")
708
+ return None
709
+ img = load_img(image_path)
710
+ img_array = img_to_array(img)
711
+ original_shape = img_array.shape[:2]
712
+ resized_image = resize_image_with_quality(img_array, (224, 224))
713
+ resized_image = resized_image.astype('float32') / 255.0
714
+ return resized_image, original_shape
715
+
716
+ # مسارات الصور الخارجية
717
+ image_paths = [
718
+ r'C:\Users\Lenovo\Desktop\jaguar.jpeg',
719
+ r'C:\Users\Lenovo\Desktop\images.jpeg',
720
+ r'C:\Users\Lenovo\Desktop\tree.jpeg'
721
+ ]
722
+
723
+ # تحميل ومعالجة الصور الخارجية مع الحفاظ على الأبعاد الأصلية
724
+ external_images_info = [load_and_preprocess_image(image_path) for image_path in image_paths]
725
+ external_images_info = [info for info in external_images_info if info is not None]
726
+
727
+ # التحقق مما إذا كانت هناك صور تم تحميلها
728
+ if not external_images_info:
729
+ print("No images were loaded. Please check your image paths.")
730
+ else:
731
+ external_images, original_shapes = zip(*external_images_info)
732
+ external_images = np.array(external_images)
733
+
734
+ # تطبيق التحويلات الملمسية
735
+ external_images_transformed = np.array([apply_texture_transformations(img * 255) / 255.0 for img in external_images])
736
+
737
+ # إعادة تشكيل الصور المسممة إلى حجمها الأصلي
738
+ external_images_transformed_resized = []
739
+ for i, transformed_image in enumerate(external_images_transformed):
740
+ original_shape = original_shapes[i]
741
+ transformed_resized = resize_image_with_quality(transformed_image * 255, original_shape[::-1])
742
+ external_images_transformed_resized.append(transformed_resized)
743
+
744
+ # إجراء التنبؤ
745
+ predictions = model.predict(external_images_transformed)
746
+
747
+ # عرض النتائج
748
+ for i, image_path in enumerate(image_paths):
749
+ if os.path.exists(image_path):
750
+ plt.figure(figsize=(10, 5))
751
+
752
+ # عرض الصورة الأصلية
753
+ plt.subplot(1, 2, 1)
754
+ original_img = load_img(image_path)
755
+ plt.imshow(original_img)
756
+ plt.title('Original Image')
757
+ plt.axis('off')
758
+
759
+ # عرض الصورة المسممة
760
+ plt.subplot(1, 2, 2)
761
+ poisoned_img = external_images_transformed_resized[i]
762
+ plt.imshow(poisoned_img.astype(np.uint8))
763
+ plt.title('Poisoned Image')
764
+ plt.axis('off')
765
+
766
+ plt.suptitle(f'Prediction: {np.argmax(predictions[i])}')
767
+ plt.show()
768
+
769
+
770
+ # In[ ]:
771
+
772
+
773
+ from flask import Flask, request, jsonify
774
+ import numpy as np
775
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
776
+ from tensorflow.keras.models import load_model
777
+ import cv2
778
+ from scipy.ndimage import gaussian_filter, laplace
779
+
780
+ app = Flask(__name__)
781
+
782
+ # Load your model
783
+ model = load_model('texture_transformed_model.h5')
784
+
785
+ # Define functions for image processing
786
+ def apply_texture_transformations(image):
787
+ # Your texture transformation function here
788
+ pass
789
+
790
+ def resize_image_with_quality(image, target_size):
791
+ # Your image resizing function here
792
+ pass
793
+
794
+ def load_and_preprocess_image(image_path):
795
+ # Your image loading and preprocessing function here
796
+ pass
797
+
798
+ @app.route('/predict', methods=['POST'])
799
+ def predict():
800
+ if 'image' not in request.files:
801
+ return jsonify({'error': 'No file part in the request'}), 400
802
+
803
+ file = request.files['image']
804
+ image_path = f'/tmp/{file.filename}'
805
+ file.save(image_path)
806
+
807
+ # Process the uploaded image
808
+ image, _ = load_and_preprocess_image(image_path)
809
+ transformed_image = apply_texture_transformations(image)
810
+
811
+ # Make predictions
812
+ prediction = model.predict(np.expand_dims(transformed_image, axis=0))
813
+
814
+ # Decode prediction (assuming your model outputs categorical predictions)
815
+ predicted_class = np.argmax(prediction)
816
+
817
+ # Return the result
818
+ return jsonify({'prediction': predicted_class})
819
+
820
+ if __name__ == '__main__':
821
+ app.run(debug=True)
822
+
823
+
824
+ # In[ ]:
825
+
826
+
827
+ python app.py
828
+ #http://127.0.0.1:5000/predict
829
+
830
+
831
+ # In[ ]:
832
+
833
+
834
+
835
+