.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition/plot_image_denoising.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_decomposition_plot_image_denoising.py: ========================================= إزالة تشويش الصور باستخدام تعلم القاموس ========================================= مثال يقارن تأثير إعادة بناء أجزاء مشوشة من صورة وجه راكون باستخدام أولاً :ref:`DictionaryLearning` عبر الإنترنت وطرق تحويل مختلفة. يتم ملاءمة القاموس على النصف الأيسر المشوه من الصورة، و يتم استخدامه لاحقًا لإعادة بناء النصف الأيمن. لاحظ أنه يمكن تحقيق أداء أفضل عن طريق ملاءمة صورة غير مشوهة (أي بدون تشويش)، ولكننا هنا نبدأ من افتراض أنها غير متوفرة. من الممارسات الشائعة لتقييم نتائج إزالة تشويش الصور هي النظر إلى الفرق بين إعادة البناء والصورة الأصلية. إذا كانت إعادة البناء مثالية، فسيبدو هذا وكأنه ضوضاء غاوسية. يمكن ملاحظة من الرسوم البيانية أن نتائج :ref:`omp` مع اثنين من المعاملات غير الصفرية أقل تحيزًا قليلاً من الاحتفاظ بمعامل واحد فقط (تبدو الحواف أقل بروزًا). بالإضافة إلى ذلك، فهي أقرب إلى الحقيقة الأصلية في قاعدة Frobenius. نتيجة :ref:`least_angle_regression` متحيزة بشكل أقوى: الفرق يذكرنا بقيمة الكثافة المحلية للصورة الأصلية. من الواضح أن العتبة ليست مفيدة لإزالة التشويش، ولكنها هنا لإظهار أنه يمكنها إنتاج مخرجات موحية بسرعة عالية جدًا، وبالتالي تكون مفيدة للمهام الأخرى مثل تصنيف الكائنات، حيث لا يرتبط الأداء بالضرورة بالتصور. .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 39-41 إنشاء صورة مشوهة ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 41-70 .. code-block:: Python import numpy as np try: # Scipy >= 1.10 from scipy.datasets import face except ImportError: from scipy.misc import face raccoon_face = face(gray=True) # تحويل من تمثيل uint8 بقيم بين 0 و 255 إلى # تمثيل فاصلة عائمة بقيم بين 0 و 1. raccoon_face = raccoon_face / 255.0 # تقليل العينات لسرعة أعلى raccoon_face = ( raccoon_face[::4, ::4] + raccoon_face[1::4, ::4] + raccoon_face[::4, 1::4] + raccoon_face[1::4, 1::4] ) raccoon_face /= 4.0 height, width = raccoon_face.shape # تشويه النصف الأيمن من الصورة print("تشويه الصورة...") distorted = raccoon_face.copy() distorted[:, width // 2 :] += 0.075 * np.random.randn(height, width // 2) .. rst-class:: sphx-glr-script-out .. code-block:: none تشويه الصورة... .. GENERATED FROM PYTHON SOURCE LINES 71-73 عرض الصورة المشوهة --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 73-100 .. code-block:: Python import matplotlib.pyplot as plt def show_with_diff(image, reference, title): """دالة مساعدة لعرض إزالة التشويش""" plt.figure(figsize=(5, 3.3)) plt.subplot(1, 2, 1) plt.title("الصورة") plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray, interpolation="nearest") plt.xticks(()) plt.yticks(()) plt.subplot(1, 2, 2) difference = image - reference plt.title("الفرق (القاعدة: %.2f)" % np.sqrt(np.sum(difference**2))) plt.imshow( difference, vmin=-0.5, vmax=0.5, cmap=plt.cm.PuOr, interpolation="nearest" ) plt.xticks(()) plt.yticks(()) plt.suptitle(title, size=16) plt.subplots_adjust(0.02, 0.02, 0.98, 0.79, 0.02, 0.2) show_with_diff(distorted, raccoon_face, "الصورة المشوهة") .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_001.png :alt: الصورة المشوهة, الصورة, الفرق (القاعدة: 11.72) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-103 استخراج الرقع المرجعية ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 103-118 .. code-block:: Python from time import time from sklearn.feature_extraction.image import extract_patches_2d # استخراج جميع الرقع المرجعية من النصف الأيسر من الصورة print("استخراج الرقع المرجعية...") t0 = time() patch_size = (7, 7) data = extract_patches_2d(distorted[:, : width // 2], patch_size) data = data.reshape(data.shape[0], -1) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) print(f"{data.shape[0]} رقعة مستخرجة في %.2fs." % (time() - t0)) .. rst-class:: sphx-glr-script-out .. code-block:: none استخراج الرقع المرجعية... 22692 رقعة مستخرجة في 0.01s. .. GENERATED FROM PYTHON SOURCE LINES 119-121 تعلم القاموس من الرقع المرجعية ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 121-151 .. code-block:: Python from sklearn.decomposition import MiniBatchDictionaryLearning print("تعلم القاموس...") t0 = time() dico = MiniBatchDictionaryLearning( # زيادة إلى 300 للحصول على نتائج عالية الجودة على حساب أوقات # تدريب أبطأ. n_components=50, batch_size=200, alpha=1.0, max_iter=10, ) V = dico.fit(data).components_ dt = time() - t0 print(f"{dico.n_iter_} تكرار / {dico.n_steps_} خطوة في {dt:.2f}.") plt.figure(figsize=(4.2, 4)) for i, comp in enumerate(V[:100]): plt.subplot(10, 10, i + 1) plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r, interpolation="nearest") plt.xticks(()) plt.yticks(()) plt.suptitle( "قاموس تم تعلمه من رقع الوجه\n" + "وقت التدريب %.1fs على %d رقعة" % (dt, len(data)), fontsize=16, ) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_002.png :alt: قاموس تم تعلمه من رقع الوجه وقت التدريب 19.0s على 22692 رقعة :srcset: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none تعلم القاموس... 1.0 تكرار / 113 خطوة في 18.99. .. GENERATED FROM PYTHON SOURCE LINES 152-154 استخراج الرقع المشوشة وإعادة بنائها باستخدام القاموس --------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 154-206 .. code-block:: Python from sklearn.feature_extraction.image import reconstruct_from_patches_2d print("استخراج الرقع المشوشة... ") t0 = time() data = extract_patches_2d(distorted[:, width // 2 :], patch_size) data = data.reshape(data.shape[0], -1) intercept = np.mean(data, axis=0) data -= intercept print("تم في %.2fs." % (time() - t0)) transform_algorithms = [ ( "مطابقة المسار المتعامد\n1 ذرة", "omp", {"transform_n_nonzero_coefs": 1}, ), ( "مطابقة المسار المتعامد\n2 ذرة", "omp", {"transform_n_nonzero_coefs": 2}, ), ( "انحدار الزاوية الصغرى\n4 ذرات", "lars", {"transform_n_nonzero_coefs": 4}, ), ("عتبة\n alpha=0.1", "threshold", {"transform_alpha": 0.1}), ] reconstructions = {} for title, transform_algorithm, kwargs in transform_algorithms: print(title + "...") reconstructions[title] = raccoon_face.copy() t0 = time() dico.set_params(transform_algorithm=transform_algorithm, **kwargs) code = dico.transform(data) patches = np.dot(code, V) patches += intercept patches = patches.reshape(len(data), *patch_size) if transform_algorithm == "threshold": patches -= patches.min() patches /= patches.max() reconstructions[title][:, width // 2 :] = reconstruct_from_patches_2d( patches, (height, width // 2) ) dt = time() - t0 print("تم في %.2fs." % dt) show_with_diff(reconstructions[title], raccoon_face, title + " (الوقت: %.1fs)" % dt) plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_003.png :alt: مطابقة المسار المتعامد 1 ذرة (الوقت: 0.7s), الصورة, الفرق (القاعدة: 10.68) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_004.png :alt: مطابقة المسار المتعامد 2 ذرة (الوقت: 1.5s), الصورة, الفرق (القاعدة: 9.32) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_004.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_005.png :alt: انحدار الزاوية الصغرى 4 ذرات (الوقت: 11.3s), الصورة, الفرق (القاعدة: 13.54) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_005.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_006.png :alt: عتبة alpha=0.1 (الوقت: 0.2s), الصورة, الفرق (القاعدة: 14.60) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_image_denoising_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none استخراج الرقع المشوشة... تم في 0.00s. مطابقة المسار المتعامد 1 ذرة... تم في 0.69s. مطابقة المسار المتعامد 2 ذرة... تم في 1.52s. انحدار الزاوية الصغرى 4 ذرات... تم في 11.26s. عتبة alpha=0.1... تم في 0.15s. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 34.120 seconds) .. _sphx_glr_download_auto_examples_decomposition_plot_image_denoising.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/decomposition/plot_image_denoising.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/decomposition/plot_image_denoising.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_image_denoising.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_image_denoising.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_image_denoising.zip ` .. include:: plot_image_denoising.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_