{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":22307,"databundleVersionId":1502524,"sourceType":"competition"},{"sourceId":5799741,"sourceType":"datasetVersion","datasetId":3331043}],"dockerImageVersionId":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os  # مكتبة للتعامل مع أنظمة الملفات\nimport shutil  # مكتبة لنسخ ونقل وحذف الملفات\nimport pydicom  # مكتبة لقراءة الصور الطبية بصيغة DICOM\nimport matplotlib.pyplot as plt  # مكتبة لرسم البيانات وتصوير الصور\nimport scipy.io  # مكتبة للتعامل مع ملفات البيانات بصيغة MAT\nimport numpy as np  # مكتبة للتعامل مع المصفوفات والعمليات الرياضية\nimport cv2  # مكتبة لمعالجة الصور والفيديو (OpenCV)023\n\nfrom PIL import Image  # مكتبة لمعالجة الصور بصيغ مختلفة (PIL)\nimport pandas as pd  # مكتبة لتحليل البيانات ومعالجتها (مثل CSV)\nimport gc  # مكتبة لإدارة الذاكرة وتنظيف البيانات غير المستخدمة\nfrom tqdm import tqdm  # مكتبة لإظهار شريط تقدم عند تنفيذ الحلقات\n\nimport tensorflow as tf  # مكتبة تعلم الآلة (التعلم العميق)\nfrom tensorflow.keras.models import Sequential, Model  # لإنشاء نماذج التعلم العميق\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator  # لتوليد بيانات الصور وتكبيرها\nfrom tensorflow.keras import layers  # لاستيراد طبقات الشبكات العصبية\nfrom tensorflow.keras import applications  # لاستيراد النماذج المدربة مسبقًا\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint  # أدوات للتحكم في التدريب\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout, BatchNormalization  # طبقات الشبكات العصبية\n\nfrom sklearn.model_selection import RepeatedKFold, cross_val_score, train_test_split  # أدوات لتقسيم البيانات وتقييم النموذج\nfrom sklearn.metrics import confusion_matrix, accuracy_score  # لقياس أداء النموذج وتحليل نتائجه\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-17T19:59:42.610084Z","iopub.execute_input":"2024-11-17T19:59:42.610416Z","iopub.status.idle":"2024-11-17T20:00:00.493949Z","shell.execute_reply.started":"2024-11-17T19:59:42.610378Z","shell.execute_reply":"2024-11-17T20:00:00.493111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/0045f113e031/454c8fdfb649/0075a38c1940.dcm\")\ndcm_sample=ds.pixel_array.astype('float32')\nscaled_image = (np.maximum(dcm_sample, 0) / dcm_sample.max())\nplt.imshow(scaled_image)\n# نتيجة الكود:\n# الكود يقوم بقراءة صورة طبية من ملف DICOM.\n# ثم يحول بيانات البكسلات إلى نوع float32.\n# بعد ذلك، يتم تحجيم الصورة لجعل القيم بين 0 و 1، لضمان عرضها بشكل صحيح.\n# وأخيرًا، يتم عرض الصورة باستخدام matplotlib.","metadata":{"execution":{"iopub.status.busy":"2024-11-17T20:00:00.495713Z","iopub.execute_input":"2024-11-17T20:00:00.496259Z","iopub.status.idle":"2024-11-17T20:00:00.88929Z","shell.execute_reply.started":"2024-11-17T20:00:00.496224Z","shell.execute_reply":"2024-11-17T20:00:00.888316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#not_noraml\ndf = pd.read_csv(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train.csv\")\ndf = df.loc[df[\"pe_present_on_image\"]==1,:].reset_index(drop=True)\nprint(len(df))\ndf.tail()\n# توضيح العملية:\n# تحميل البيانات: يتم تحميل ملف CSV الذي يحتوي على معلومات عن الصور والأشعة المتعلقة بتشخيص الجلطات الرئوية.\n# تصفية البيانات: يتم الاحتفاظ فقط بالصفوف التي تكون فيها قيمة pe_present_on_image تساوي 1 (أي الصور التي تحتوي على جلطة رئوية).\n# حجم البيانات المتبقية: يتم طباعة عدد الصفوف المتبقية بعد التصفية، أي عدد الحالات التي تحتوي على جلطة.\n# عرض الصفوف الأخيرة: يتم عرض آخر 5 صفوف من البيانات بعد التصفية.","metadata":{"execution":{"iopub.status.busy":"2024-11-17T20:00:00.890492Z","iopub.execute_input":"2024-11-17T20:00:00.890804Z","iopub.status.idle":"2024-11-17T20:00:05.057279Z","shell.execute_reply.started":"2024-11-17T20:00:00.89077Z","shell.execute_reply":"2024-11-17T20:00:05.056403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#normal\ndf1 = pd.read_csv(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train.csv\")\ndf1 = df1.loc[(df1[\"pe_present_on_image\"] == 0) & (df1[\"negative_exam_for_pe\"] == 1) ,:].reset_index(drop=True)\nprint(len(df1))\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T20:00:05.058727Z","iopub.execute_input":"2024-11-17T20:00:05.059434Z","iopub.status.idle":"2024-11-17T20:00:08.074858Z","shell.execute_reply.started":"2024-11-17T20:00:05.059385Z","shell.execute_reply":"2024-11-17T20:00:08.073951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/data\n\n!mkdir /kaggle/data/train\n!mkdir /kaggle/data/valid\n!mkdir /kaggle/data/test\n\n!mkdir /kaggle/data/train/normal\n!mkdir /kaggle/data/train/not_normal\n\n!mkdir /kaggle/data/valid/normal\n!mkdir /kaggle/data/valid/not_normal\n\n!mkdir /kaggle/data/test/normal\n!mkdir /kaggle/data/test/not_normal","metadata":{"execution":{"iopub.status.busy":"2024-11-17T20:00:08.078013Z","iopub.execute_input":"2024-11-17T20:00:08.078659Z","iopub.status.idle":"2024-11-17T20:00:18.089188Z","shell.execute_reply.started":"2024-11-17T20:00:08.078612Z","shell.execute_reply":"2024-11-17T20:00:18.087939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train normal\nfrom tqdm import tqdm\nimport pydicom\nfor i in tqdm(range(10000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df1.loc[i,'StudyInstanceUID']+'/'+df1.loc[i,'SeriesInstanceUID']+'/'+df1.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image = dcm.pixel_array.astype('float32')\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/train/normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T20:00:18.091068Z","iopub.execute_input":"2024-11-17T20:00:18.091526Z","iopub.status.idle":"2024-11-17T20:37:02.366749Z","shell.execute_reply.started":"2024-11-17T20:00:18.091478Z","shell.execute_reply":"2024-11-17T20:37:02.365862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train not_normal\nfor i in tqdm(range(10000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df.loc[i,'StudyInstanceUID']+'/'+df.loc[i,'SeriesInstanceUID']+'/'+df.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/train/not_normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()\n#     #ملخص:\n# الكود يقرأ صور DICOM من ملفات طبية.\n# يحول الصور إلى مصفوفة بكسلات، ثم يعيد تشكيلها وحفظها بصيغة JPEG بحجم 256x256.\n# يتم تحرير الذاكرة بعد معالجة كل صورة للحفاظ على أداء النظام.\n# هذا الكود مناسب للتعامل مع كميات كبيرة من البيانات الطبية وتحويلها إلى صور يمكن استخدامها بسهولة في نماذج الذكاء الاصطناعي مثل الشبكات العصبية.\n    #\n    #\n    #\n    \n    #\n    #\n    #","metadata":{"execution":{"iopub.status.busy":"2024-11-17T20:37:02.368085Z","iopub.execute_input":"2024-11-17T20:37:02.368424Z","iopub.status.idle":"2024-11-17T21:10:25.710718Z","shell.execute_reply.started":"2024-11-17T20:37:02.368389Z","shell.execute_reply":"2024-11-17T21:10:25.709808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Valid not_normal\nfor i in tqdm(range(10000,12000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df.loc[i,'StudyInstanceUID']+'/'+df.loc[i,'SeriesInstanceUID']+'/'+df.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/valid/not_normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:10:25.712016Z","iopub.execute_input":"2024-11-17T21:10:25.712427Z","iopub.status.idle":"2024-11-17T21:17:07.520681Z","shell.execute_reply.started":"2024-11-17T21:10:25.712373Z","shell.execute_reply":"2024-11-17T21:17:07.519808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Valid normal\nfor i in tqdm(range(10000,12000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df1.loc[i,'StudyInstanceUID']+'/'+df1.loc[i,'SeriesInstanceUID']+'/'+df1.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/valid/normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:17:07.521879Z","iopub.execute_input":"2024-11-17T21:17:07.522172Z","iopub.status.idle":"2024-11-17T21:23:53.4457Z","shell.execute_reply.started":"2024-11-17T21:17:07.522141Z","shell.execute_reply":"2024-11-17T21:23:53.444819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test not_normal\nfor i in tqdm(range(12000,14000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df.loc[i,'StudyInstanceUID']+'/'+df.loc[i,'SeriesInstanceUID']+'/'+df.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/test/not_normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:23:53.446865Z","iopub.execute_input":"2024-11-17T21:23:53.447168Z","iopub.status.idle":"2024-11-17T21:30:38.298993Z","shell.execute_reply.started":"2024-11-17T21:23:53.447131Z","shell.execute_reply":"2024-11-17T21:30:38.298146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test normal\nfor i in tqdm(range(12000,14000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df1.loc[i,'StudyInstanceUID']+'/'+df1.loc[i,'SeriesInstanceUID']+'/'+df1.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/test/normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:30:38.299998Z","iopub.execute_input":"2024-11-17T21:30:38.30027Z","iopub.status.idle":"2024-11-17T21:37:19.084558Z","shell.execute_reply.started":"2024-11-17T21:30:38.30024Z","shell.execute_reply":"2024-11-17T21:37:19.083663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Define the ImageDataGenerator for data augmentation or rescaling\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0/255,  # Normalize pixel values to [0, 1] range\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True\n)\n\n# Now create the train_generator\ntrain_generator = train_datagen.flow_from_directory(\n    '/kaggle/data/train',\n    target_size=(256, 256),\n    batch_size=64,\n    class_mode='binary'\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.085914Z","iopub.execute_input":"2024-11-17T21:37:19.086534Z","iopub.status.idle":"2024-11-17T21:37:19.575593Z","shell.execute_reply.started":"2024-11-17T21:37:19.086487Z","shell.execute_reply":"2024-11-17T21:37:19.574818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(\n      featurewise_center=False,  \n      samplewise_center=False, \n      featurewise_std_normalization=False,  \n      samplewise_std_normalization=False, \n      rescale=1./255,\n      rotation_range=20,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      vertical_flip=True,\n      fill_mode='nearest')","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.576822Z","iopub.execute_input":"2024-11-17T21:37:19.577512Z","iopub.status.idle":"2024-11-17T21:37:19.58294Z","shell.execute_reply.started":"2024-11-17T21:37:19.577466Z","shell.execute_reply":"2024-11-17T21:37:19.58204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_datagen = ImageDataGenerator(\n      rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.587505Z","iopub.execute_input":"2024-11-17T21:37:19.587899Z","iopub.status.idle":"2024-11-17T21:37:19.592182Z","shell.execute_reply.started":"2024-11-17T21:37:19.587865Z","shell.execute_reply":"2024-11-17T21:37:19.591313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = valid_datagen.flow_from_directory(\n        '/kaggle/data/valid',\n        target_size=(256, 256),\n        batch_size=64,\n        class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.593494Z","iopub.execute_input":"2024-11-17T21:37:19.594165Z","iopub.status.idle":"2024-11-17T21:37:19.703661Z","shell.execute_reply.started":"2024-11-17T21:37:19.594129Z","shell.execute_reply":"2024-11-17T21:37:19.702806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n      rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.706062Z","iopub.execute_input":"2024-11-17T21:37:19.706977Z","iopub.status.idle":"2024-11-17T21:37:19.711064Z","shell.execute_reply.started":"2024-11-17T21:37:19.706929Z","shell.execute_reply":"2024-11-17T21:37:19.710201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = valid_datagen.flow_from_directory(\n        '/kaggle/data/test',\n        target_size=(256, 256),\n        batch_size=64,\n        class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.712197Z","iopub.execute_input":"2024-11-17T21:37:19.712503Z","iopub.status.idle":"2024-11-17T21:37:19.82157Z","shell.execute_reply.started":"2024-11-17T21:37:19.712467Z","shell.execute_reply":"2024-11-17T21:37:19.820842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu' , input_shape = ( 256, 256, 3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\nmodel.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\nmodel.add(Dropout(0.1))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\nmodel.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\nmodel.add(Conv2D(128 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\nmodel.add(Conv2D(256 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\nmodel.add(Flatten())\nmodel.add(Dense(units = 128 , activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(units = 1 , activation = 'sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:19.822579Z","iopub.execute_input":"2024-11-17T21:37:19.822849Z","iopub.status.idle":"2024-11-17T21:37:20.774057Z","shell.execute_reply.started":"2024-11-17T21:37:19.822818Z","shell.execute_reply":"2024-11-17T21:37:20.773113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/models","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:20.775403Z","iopub.execute_input":"2024-11-17T21:37:20.776116Z","iopub.status.idle":"2024-11-17T21:37:21.810855Z","shell.execute_reply.started":"2024-11-17T21:37:20.776072Z","shell.execute_reply":"2024-11-17T21:37:21.809647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n\nloss = tf.keras.losses.BinaryCrossentropy()\nmodel.compile(loss=loss, \n              optimizer='Adam', \n              metrics=['binary_accuracy'])\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_binary_accuracy', patience=2, verbose=1, factor=0.3, min_lr=0.000001)\n\n# Update the filepath to end with '.keras'\nfilepath = \"/kaggle/models/saved-model-{epoch:02d}-{val_binary_accuracy:.2f}.keras\"\n\n# Use the updated filepath\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, \n                             save_best_only=False, save_freq='epoch')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:21.812354Z","iopub.execute_input":"2024-11-17T21:37:21.812713Z","iopub.status.idle":"2024-11-17T21:37:21.832895Z","shell.execute_reply.started":"2024-11-17T21:37:21.812676Z","shell.execute_reply":"2024-11-17T21:37:21.832005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n# Assuming train_generator and valid_generator are already defined\nhistory = model.fit(\n      train_generator,\n      epochs=15,\n      validation_data=valid_generator,\n      validation_steps=4,\n      callbacks=[checkpoint, learning_rate_reduction],\n      verbose=1\n)\n ","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:37:21.834073Z","iopub.execute_input":"2024-11-17T21:37:21.834386Z","iopub.status.idle":"2024-11-17T22:43:17.745115Z","shell.execute_reply.started":"2024-11-17T21:37:21.834328Z","shell.execute_reply":"2024-11-17T22:43:17.744146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.plot(history.history['binary_accuracy'], label='The score of correct predictions on the training set')\nplt.plot(history.history['val_binary_accuracy'], label='The score of correct predictions on the val set')\nplt.xlabel('Epoch')\nplt.ylabel('Score correct answers')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:43:17.746662Z","iopub.execute_input":"2024-11-17T22:43:17.746983Z","iopub.status.idle":"2024-11-17T22:43:18.015084Z","shell.execute_reply.started":"2024-11-17T22:43:17.746949Z","shell.execute_reply":"2024-11-17T22:43:18.014179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nos.makedirs('/kaggle/working', exist_ok=True)  # If saving in /kaggle/working\nmodel.save('/kaggle/working/model.h5')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:43:18.0164Z","iopub.execute_input":"2024-11-17T22:43:18.017143Z","iopub.status.idle":"2024-11-17T22:43:18.120073Z","shell.execute_reply.started":"2024-11-17T22:43:18.017099Z","shell.execute_reply":"2024-11-17T22:43:18.119294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:51:45.401015Z","iopub.execute_input":"2024-11-17T22:51:45.401786Z","iopub.status.idle":"2024-11-17T22:51:46.405197Z","shell.execute_reply.started":"2024-11-17T22:51:45.40174Z","shell.execute_reply":"2024-11-17T22:51:46.404278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade tensorflow\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:52:09.0622Z","iopub.execute_input":"2024-11-17T22:52:09.063037Z","iopub.status.idle":"2024-11-17T22:53:19.351884Z","shell.execute_reply.started":"2024-11-17T22:52:09.062995Z","shell.execute_reply":"2024-11-17T22:53:19.350924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n\nmodel = tf.keras.models.load_model('model.h5')\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# Save the TFLite model\nwith open('/kaggle/working/model.tflite', 'wb') as f:\n    f.write(tflite_model)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:53:42.304926Z","iopub.execute_input":"2024-11-17T22:53:42.305333Z","iopub.status.idle":"2024-11-17T22:53:44.934135Z","shell.execute_reply.started":"2024-11-17T22:53:42.30529Z","shell.execute_reply":"2024-11-17T22:53:44.933078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Model summary:\")\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:54:10.141417Z","iopub.execute_input":"2024-11-17T22:54:10.142369Z","iopub.status.idle":"2024-11-17T22:54:10.184213Z","shell.execute_reply.started":"2024-11-17T22:54:10.142313Z","shell.execute_reply":"2024-11-17T22:54:10.183307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(\"Files in '/kaggle/working':\", os.listdir('/kaggle/working'))\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:54:17.89314Z","iopub.execute_input":"2024-11-17T22:54:17.893566Z","iopub.status.idle":"2024-11-17T22:54:17.898986Z","shell.execute_reply.started":"2024-11-17T22:54:17.893523Z","shell.execute_reply":"2024-11-17T22:54:17.898009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n\n# توفير رابط لتحميل الملف\nFileLink('/kaggle/working/model.tflite')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:54:25.620565Z","iopub.execute_input":"2024-11-17T22:54:25.62097Z","iopub.status.idle":"2024-11-17T22:54:25.627623Z","shell.execute_reply.started":"2024-11-17T22:54:25.620933Z","shell.execute_reply":"2024-11-17T22:54:25.626652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(\"File exists:\", os.path.isfile('/kaggle/working/model.tflite'))\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:54:55.858076Z","iopub.execute_input":"2024-11-17T22:54:55.858623Z","iopub.status.idle":"2024-11-17T22:54:55.863507Z","shell.execute_reply.started":"2024-11-17T22:54:55.858578Z","shell.execute_reply":"2024-11-17T22:54:55.862528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp model.tflite /kaggle/working/model.tflite","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:55:01.109228Z","iopub.execute_input":"2024-11-17T22:55:01.109644Z","iopub.status.idle":"2024-11-17T22:55:02.140424Z","shell.execute_reply.started":"2024-11-17T22:55:01.109606Z","shell.execute_reply":"2024-11-17T22:55:02.139412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_model(image_path, model_path='model.tflite'):\n    # الحصول على التنبؤ للصورة المفردة\n    prediction = predict(image_path, model_path)\n    \n    # إرجاع النتيجة\n    return prediction\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:14:50.228922Z","iopub.execute_input":"2024-11-17T23:14:50.229354Z","iopub.status.idle":"2024-11-17T23:14:50.234411Z","shell.execute_reply.started":"2024-11-17T23:14:50.229298Z","shell.execute_reply":"2024-11-17T23:14:50.233477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nfrom PIL import Image\n\n# Function to preprocess the image\ndef preprocess_image(image_path, target_size):\n    # Ensure target_size is a tuple\n    target_size = tuple(target_size)\n\n    # Load and preprocess the image\n    image = Image.open(image_path).convert(\"RGB\")\n    image = image.resize(target_size)  # Resize the image\n    image = np.array(image) / 255.0  # Normalize to [0, 1]\n    image = image.astype(np.float32)  # Convert to float32\n    image = np.expand_dims(image, axis=0)  # Add batch dimension\n    return image\n\n# Function to load the model and make predictions\ndef test_model(image_path, model_path='model.tflite'):\n    # Load the TFLite model and allocate tensors\n    interpreter = tf.lite.Interpreter(model_path=model_path)\n    interpreter.allocate_tensors()\n\n    # Get input and output details\n    input_details = interpreter.get_input_details()\n    output_details = interpreter.get_output_details()\n\n    # Print input and output details for debugging\n    print(\"Input details:\", input_details)\n    print(\"Output details:\", output_details)\n\n    # Preprocess the image to match the model's input shape\n    input_shape = input_details[0]['shape'][1:3]  # Get expected width and height\n    input_data = preprocess_image(image_path, input_shape)\n\n    # Set the tensor for input\n    interpreter.set_tensor(input_details[0]['index'], input_data)\n\n    # Run inference\n    interpreter.invoke()\n\n    # Get the prediction result\n    prediction = interpreter.get_tensor(output_details[0]['index'])\n\n    # Debug raw output\n    print(\"Raw output:\", prediction)\n\n    # Return the result\n    return np.argmax(prediction)  # Assuming it's a classification model\n\n# Example usage\nimage_path = '/kaggle/input/data-binary/data/test/not_normal/10005.jpg'  # Replace with your image path\nmodel_path = 'model.tflite'  # Replace with your model path\n\n# Call the function and print the result\nresult = test_model(image_path, model_path)\nprint(\"Prediction for the image:\", result)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:43:37.101095Z","iopub.execute_input":"2024-11-17T23:43:37.101947Z","iopub.status.idle":"2024-11-17T23:43:37.159781Z","shell.execute_reply.started":"2024-11-17T23:43:37.101903Z","shell.execute_reply":"2024-11-17T23:43:37.15875Z"},"trusted":true},"execution_count":null,"outputs":[]}]}