{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":90316,"databundleVersionId":10477888,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":1332.314168,"end_time":"2025-01-08T13:10:41.604813","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-01-08T12:48:29.290645","version":"2.6.0"},"widgets":{"application/vnd.jupyter.widget-state+json":{"state":{"0092116c155c43b3918417e25827a04a":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"08bf59337ab243a197b79b7461c1b07e":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_83b1fc6efd854242bcab3e42a34cd9ca","IPY_MODEL_e1e012d6a1f84d12a64aa767ae274e6e","IPY_MODEL_15b210a0c8134a14baaa709c0faabeca"],"layout":"IPY_MODEL_099861777e564144931d60e7811ea9f6","tabbable":null,"tooltip":null}},"099861777e564144931d60e7811ea9f6":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"15b210a0c8134a14baaa709c0faabeca":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_8fdd310fbe3947abb9e96da59e68f61a","placeholder":"​","style":"IPY_MODEL_df40c4bde2ed4ae79c69261e2bff57a8","tabbable":null,"tooltip":null,"value":" 13.1k/13.1k [00:00&lt;00:00, 1.35MB/s]"}},"165bb3a331b84044a2cc142baac2ee82":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"1f9e80c38d414109900635be18bf7463":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"1fb1f0c9e3424f53bb1117b6a9c03b9c":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"ProgressView","bar_style":"success","description":"","description_allow_html":false,"layout":"IPY_MODEL_1f9e80c38d414109900635be18bf7463","max":339299397,"min":0,"orientation":"horizontal","style":"IPY_MODEL_bf7b3edcc3fa411ba3f9a630048f9b9b","tabbable":null,"tooltip":null,"value":339299397}},"23d4eb8d39de4465ba13ef4b3fd6c8ba":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"56fa6f74a1ff44519923058f926392b2":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"ProgressView","bar_style":"success","description":"","description_allow_html":false,"layout":"IPY_MODEL_b50158289f47459d84e0137614a687c8","max":6886,"min":0,"orientation":"horizontal","style":"IPY_MODEL_7cef32458ab840bfb5d42614f99327e7","tabbable":null,"tooltip":null,"value":6886}},"5f6e4913e6714962b44a57dbf842b994":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"6bfb9587808045cdb9c78ecb0e772f9c":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_7d556f9d047745fead6b04fd03ec80c2","IPY_MODEL_1fb1f0c9e3424f53bb1117b6a9c03b9c","IPY_MODEL_9d5f9029f8084b79a60317a008ecc5ca"],"layout":"IPY_MODEL_165bb3a331b84044a2cc142baac2ee82","tabbable":null,"tooltip":null}},"7bde00cad4e24a39b3756d30aae72a87":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_981d1cc71f8044cf9d8b4f80a9233b57","IPY_MODEL_56fa6f74a1ff44519923058f926392b2","IPY_MODEL_ff0fdf910ebd46c6a74bd4c40149fdb1"],"layout":"IPY_MODEL_c38fad3303334915b6e9dbe218e8a3c8","tabbable":null,"tooltip":null}},"7cef32458ab840bfb5d42614f99327e7":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"7d556f9d047745fead6b04fd03ec80c2":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_d123ec33b91744fba08819524cb6b31e","placeholder":"​","style":"IPY_MODEL_b07cf4da90c64d6d8e026d66b2d86aa2","tabbable":null,"tooltip":null,"value":"pytorch_model.bin: 100%"}},"83b1fc6efd854242bcab3e42a34cd9ca":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_f0651db87d7e4a519926c9d2f43d88df","placeholder":"​","style":"IPY_MODEL_5f6e4913e6714962b44a57dbf842b994","tabbable":null,"tooltip":null,"value":"Downloading builder script: 100%"}},"8789c1698c0a42c7884e03e0f93c7d07":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"8fdd310fbe3947abb9e96da59e68f61a":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"981d1cc71f8044cf9d8b4f80a9233b57":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_23d4eb8d39de4465ba13ef4b3fd6c8ba","placeholder":"​","style":"IPY_MODEL_cfdd1021f1254e2a9fa8852449a64b02","tabbable":null,"tooltip":null,"value":"config.json: 100%"}},"9d5f9029f8084b79a60317a008ecc5ca":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_f41023de32a046658af353dbd088b05f","placeholder":"​","style":"IPY_MODEL_eb61fc8d041b43f78d03292346f311c4","tabbable":null,"tooltip":null,"value":" 339M/339M [00:01&lt;00:00, 241MB/s]"}},"aadf73e7f8d6442ebdc60b99c29c10ca":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"b07cf4da90c64d6d8e026d66b2d86aa2":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"b50158289f47459d84e0137614a687c8":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"bf7b3edcc3fa411ba3f9a630048f9b9b":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"c38fad3303334915b6e9dbe218e8a3c8":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"cfdd1021f1254e2a9fa8852449a64b02":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"d123ec33b91744fba08819524cb6b31e":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"df40c4bde2ed4ae79c69261e2bff57a8":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"e1e012d6a1f84d12a64aa767ae274e6e":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"ProgressView","bar_style":"success","description":"","description_allow_html":false,"layout":"IPY_MODEL_8789c1698c0a42c7884e03e0f93c7d07","max":13077,"min":0,"orientation":"horizontal","style":"IPY_MODEL_0092116c155c43b3918417e25827a04a","tabbable":null,"tooltip":null,"value":13077}},"e3e3278758a44b0bbb45b5a908507ebe":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"eb61fc8d041b43f78d03292346f311c4":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"f0651db87d7e4a519926c9d2f43d88df":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"f41023de32a046658af353dbd088b05f":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"ff0fdf910ebd46c6a74bd4c40149fdb1":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_aadf73e7f8d6442ebdc60b99c29c10ca","placeholder":"​","style":"IPY_MODEL_e3e3278758a44b0bbb45b5a908507ebe","tabbable":null,"tooltip":null,"value":" 6.89k/6.89k [00:00&lt;00:00, 637kB/s]"}}},"version_major":2,"version_minor":0}}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"cfa64ba3","cell_type":"code","source":"!pip install evaluate","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2025-01-08T12:48:31.435612Z","iopub.status.busy":"2025-01-08T12:48:31.435356Z","iopub.status.idle":"2025-01-08T12:48:36.020855Z","shell.execute_reply":"2025-01-08T12:48:36.019807Z"},"papermill":{"duration":4.592175,"end_time":"2025-01-08T12:48:36.022456","exception":false,"start_time":"2025-01-08T12:48:31.430281","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"242442eb","cell_type":"code","source":"import os\nimport gc\nimport time\nimport copy\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\nfrom torch.optim import lr_scheduler\nfrom torchmetrics import AUROC\nfrom PIL import Image\nfrom tqdm import tqdm\nimport evaluate\nfrom torchvision.transforms.functional import to_pil_image\nimport random\n# Albumentations\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom albumentations import Compose, Normalize, RandomCrop, HorizontalFlip, ShiftScaleRotate, HueSaturationValue\n\n# Transformers / HuggingFace\nfrom transformers import (\n    SegformerConfig,\n    SegformerForSemanticSegmentation,\n    SegformerPreTrainedModel,\n)\n\n# Metrics\nfrom sklearn.metrics import f1_score, accuracy_score, recall_score, roc_auc_score, roc_curve\n","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:48:36.031980Z","iopub.status.busy":"2025-01-08T12:48:36.031714Z","iopub.status.idle":"2025-01-08T12:48:51.911472Z","shell.execute_reply":"2025-01-08T12:48:51.910484Z"},"papermill":{"duration":15.885733,"end_time":"2025-01-08T12:48:51.912759","exception":false,"start_time":"2025-01-08T12:48:36.027026","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"8f2396d2","cell_type":"code","source":"def crop(imgfile, maskfile, size=224):\n    mask = maskfile.astype(np.float32)\n    if np.sum(mask) / mask.size > 0.9:\n        # if the object mask has more than 50% of the whole mask, return the full image and mask\n        mask = cv2.resize(mask, (size, size), interpolation=cv2.INTER_NEAREST)\n        cropped_img = cv2.resize(imgfile, (size, size), interpolation=cv2.INTER_NEAREST)\n        return cropped_img, mask\n    else:\n        # otherwise, crop the image and mask to the bounding box of the object\n        rows = np.any(mask, axis=1)\n        cols = np.any(mask, axis=0)\n        y1, y2 = np.where(rows)[0][[0, -1]]\n        x1, x2 = np.where(cols)[0][[0, -1]]\n        mask = mask[y1:y2+1, x1:x2+1]\n        mask = cv2.resize(mask, (size, size), interpolation=cv2.INTER_NEAREST)\n        cropped_img = cv2.resize(imgfile[y1:y2+1, x1:x2+1], (size, size), interpolation=cv2.INTER_NEAREST)\n        return cropped_img, mask\n\ndef decode_rle_to_mask(rle, height, width, viz=False):\n    '''\n    rle : run-length as string formated (start value, count)\n    height : height of the mask \n    width : width of the mask\n    returns binary mask\n    '''\n    rle = np.array(rle.split(' ')).reshape(-1, 2)\n    mask = np.zeros((height*width, 1, 3))\n    if viz:\n        color = np.random.rand(3)\n    else:\n        color = [1,1,1]\n    for i in rle:\n        mask[int(i[0]):int(i[0])+int(i[1]), :, :] = color\n\n    return mask.reshape(height, width, 3)","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:48:51.922280Z","iopub.status.busy":"2025-01-08T12:48:51.921781Z","iopub.status.idle":"2025-01-08T12:48:51.929061Z","shell.execute_reply":"2025-01-08T12:48:51.928412Z"},"papermill":{"duration":0.013147,"end_time":"2025-01-08T12:48:51.930256","exception":false,"start_time":"2025-01-08T12:48:51.917109","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"0c49a00c","cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/ai-dl-multiclass-segmentation/train.csv')\ndf_test = pd.read_csv('/kaggle/input/ai-dl-multiclass-segmentation/test_class.csv')\ntrainpath = '/kaggle/input/ai-dl-multiclass-segmentation/TrainImages'\ntestpath = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\n\ndfcp=df_train.copy()\ndfcp = dfcp.drop_duplicates(subset=['ImageName'], keep='first')\nprint('length of the dataset:', len(dfcp))\n\ntraindata = {\"imgs\": [], \"masks\": []}\ncounter = 0\n\nfor i in dfcp['ImageName']:\n    imgpath=os.path.join(trainpath,i+'.jpg')\n    masks=[]\n    num=0\n    for j in df_train[df_train['ImageName'] == i]['Encoding']:#ImageHeight\n        wi = df_train[df_train['ImageName'] == i]['ImageWidth'].values[0]\n        hi = df_train[df_train['ImageName'] == i]['ImageHeight'].values[0]\n        classes = int(df_train[df_train['ImageName'] == i]['ClassNumber'].values[num])\n        masks.append(decode_rle_to_mask(j, int(hi), int(wi))*classes)\n        num+=1\n    mask = np.sum(masks, axis=0)[:,:,0]\n    try:\n        i,m=crop(cv2.imread(imgpath),mask,size=224)\n        traindata['imgs'].append(np.array(i))\n        traindata['masks'].append(np.array(m))\n        counter += 1\n    except:\n        print(\"Failed to add the image and mask of index\",counter)\n        continue\n\nprint(\"Images added: \",len(traindata['imgs']),\" ,Masks added: \",len(traindata['masks']))","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:48:51.938832Z","iopub.status.busy":"2025-01-08T12:48:51.938632Z","iopub.status.idle":"2025-01-08T12:49:47.344927Z","shell.execute_reply":"2025-01-08T12:49:47.343939Z"},"papermill":{"duration":55.415715,"end_time":"2025-01-08T12:49:47.349982","exception":false,"start_time":"2025-01-08T12:48:51.934267","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"17283756","cell_type":"code","source":"data_transforms = Compose([\n                        ToTensorV2()\n                    ])\ntrain_transform = []\nfor i in range(len(traindata['imgs'])):\n    train_transform.append((data_transforms(image=traindata['imgs'][i]),traindata['masks'][i]))\n","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:49:47.359859Z","iopub.status.busy":"2025-01-08T12:49:47.359587Z","iopub.status.idle":"2025-01-08T12:49:47.391815Z","shell.execute_reply":"2025-01-08T12:49:47.390956Z"},"papermill":{"duration":0.038463,"end_time":"2025-01-08T12:49:47.393086","exception":false,"start_time":"2025-01-08T12:49:47.354623","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"5bc203c9","cell_type":"code","source":"#PyTorch\nALPHA = 0.5\nBETA = 0.5\nGAMMA = 1\nclass CustomSegFormer2(SegformerPreTrainedModel):\n    def __init__(self, config, in_channel, n_classes=5):\n        super().__init__(config)\n        self.segformer = SegformerForSemanticSegmentation.from_pretrained(\"nvidia/segformer-b5-finetuned-ade-640-640\", ignore_mismatched_sizes=True)\n        self.logits_outputs = nn.Conv2d(in_channel, out_channels=n_classes, kernel_size=1, stride=1, padding=0)\n        self.distance_outputs = nn.Conv2d(in_channel, out_channels=1, kernel_size=1, stride=1, padding=0)\n        \n\n    def forward(self, pixel_values):\n        # forward to get raw logits\n        outputs = self.segformer(pixel_values) # what's the number of output channels?\n        logits_outputs = self.logits_outputs(outputs[0])\n        distancemap_outputs = self.distance_outputs(outputs[0])        \n\n        upsampled_logits = nn.functional.interpolate(logits_outputs, size=pixel_values.shape[-2:], mode=\"bilinear\", align_corners=False)\n        upsampled_distancemaps = nn.functional.interpolate(distancemap_outputs , size=pixel_values.shape[-2:], mode=\"bilinear\", align_corners=False)\n\n        \n        return  upsampled_logits, upsampled_distancemaps\nclass AverageMeter:\n    \"\"\"Computes and stores the average and current value\"\"\"\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, count=1):\n        self.val = val\n        self.sum += val * count\n        self.count += count\n        self.avg = self.sum / self.count\n\nclass FocalTverskyLoss(nn.Module):\n    def __init__(self, weight=None, size_average=True):\n        super(FocalTverskyLoss, self).__init__()\n\n    def forward(self, inputs, targets, smooth=1, alpha=ALPHA, beta=BETA, gamma=GAMMA):\n        \n        #comment out if your model contains a sigmoid or equivalent activation layer\n        inputs = F.sigmoid(inputs)       \n        print(inputs.shape)\n        print(targets.shape)\n        #flatten label and prediction tensors\n        inputs = inputs.reshape(-1)\n        targets = targets.reshape(-1)\n        print(inputs.shape)\n        print(targets.shape)\n        #True Positives, False Positives & False Negatives\n        TP = (inputs * targets).sum()    \n        FP = ((1-targets) * inputs).sum()\n        FN = (targets * (1-inputs)).sum()\n        \n        Tversky = (TP + smooth) / (TP + alpha*FP + beta*FN + smooth)  \n        FocalTversky = (1 - Tversky)**gamma\n                       \n        return FocalTversky\n\ndef dice_loss(output, target, num_classes = 5):\n    # Compute softmax over the channel dimension\n    output = F.softmax(output, dim=1)\n    \n    # Create a one-hot encoding of the target\n    target_onehot = F.one_hot(target, num_classes=num_classes).permute(0, 3, 1, 2).float()\n\n    # Compute intersection and union between output and target\n    intersection = torch.sum(output * target_onehot, dim=[2, 3])\n    union = torch.sum(output, dim=[2, 3]) + torch.sum(target_onehot, dim=[2, 3])\n    \n    # Compute the Dice coefficient for each class\n    dice = 2 * intersection / union.clamp(min=1e-7)\n    \n    # Compute the average Dice loss across all classes\n    dice_loss = 1 - torch.mean(dice)\n    \n    return dice_loss","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:49:47.402009Z","iopub.status.busy":"2025-01-08T12:49:47.401759Z","iopub.status.idle":"2025-01-08T12:49:47.413594Z","shell.execute_reply":"2025-01-08T12:49:47.412692Z"},"papermill":{"duration":0.017786,"end_time":"2025-01-08T12:49:47.414973","exception":false,"start_time":"2025-01-08T12:49:47.397187","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"9e460cf9","cell_type":"code","source":"def train_model(model,dataloaders, criterion, optimizer, scheduler, name, num_epochs=25, feature_extractor = None, metric = None):\n\n    #Creating a folder to save the model performance.\n    try: os.mkdir('/kaggle/working/modelPerformance')\n    except: print('directory exists')\n    try: os.mkdir(f'/kaggle/working/modelPerformance/{name}')\n    except: print(f\"{name} alreadt exists\")\n    \n    since = time.time()\n    best_model_wts = copy.deepcopy(model.state_dict())\n    best_acc = 0.0  \n    acc = 0.0\n    loss = 0.0\n    loss_avg = AverageMeter()\n    for epoch in range(num_epochs):\n        print('Epoch {}/{}'.format(epoch+1, num_epochs))\n        print('-' * 10)\n        # Each epoch has a training and validation phase\n        for phase in ['train', 'test']:\n            print(phase)\n            if phase == 'train':\n                loss_avg = AverageMeter()\n                model = model.train()  # Set model to training mode\n            else:\n                loss_avg = AverageMeter()\n                model = model.eval()   # Set model to evaluate mode\n            running_loss = 0.0\n            running_corrects = 0\n\n            #epochs\n            epoch=int(len(dataloaders[phase]))\n            for _ in tqdm(range(epoch)):\n                #Loading Data\n                optimizer.zero_grad()\n                inputs, labels = next(iter(dataloaders[phase]))\n                inputs = inputs['image']\n                labels = labels.type(torch.LongTensor)\n                inputs = inputs.to(device)\n                print()\n                labels = labels.to(device)\n                # forward\n                # track history if only in train\n                if phase == 'train':\n                    with torch.set_grad_enabled(phase == 'train'):\n                        outputs, ssn = model(inputs.float())\n                        pred = outputs.softmax(dim=1)\n                        preds=pred.argmax(dim=1)\n                        loss = criterion(outputs, labels)\n                        print(loss.item())\n\n                        loss.backward()\n                        optimizer.step()\n                    loss_avg.update(loss.item())\n\n                if phase == 'test':\n                    outputs, ssn = model(inputs.float())\n                    pred = outputs.softmax(dim=1)\n                    preds=pred.argmax(dim=1)\n\n                    loss = criterion(outputs, labels)\n                    loss_avg.update(loss.item())           \n\n   \n                acc = metric.compute(predictions = preds, references = labels, num_labels = 5, ignore_index = 5, reduce_labels=False)\n                print(\"Acc:\",acc)\n                print(\"Loss:\",loss.item())\n                \n            if phase == 'train':\n                scheduler.step()\n                \n            print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, loss_avg.val, acc['overall_accuracy']))\n    \n            \n            # deep copy the model\n            if phase == 'test' and acc['overall_accuracy'] > best_acc:\n                best_acc = acc['overall_accuracy']\n                torch.save(model.state_dict(),'/kaggle/working/modelPerformance/{}/best_model_{:.4f}acc_{}epochs.pth'.format(name,acc['overall_accuracy'],num_epochs))\n\n                train_losses = []\n                valid_losses = []\n\n        print()\n\n\n    time_elapsed = time.time() - since\n    print('Training complete in {:.0f}m {:.0f}s'.format(\n        time_elapsed // 60, time_elapsed % 60))\n    print('Best val Acc: {:4f}'.format(best_acc))\n\n    return model","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:49:47.428378Z","iopub.status.busy":"2025-01-08T12:49:47.428122Z","iopub.status.idle":"2025-01-08T12:49:47.437542Z","shell.execute_reply":"2025-01-08T12:49:47.436916Z"},"papermill":{"duration":0.016593,"end_time":"2025-01-08T12:49:47.438652","exception":false,"start_time":"2025-01-08T12:49:47.422059","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"c41ab329","cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\ndataloaders = {\n    'train': torch.utils.data.DataLoader(train_transform[:1600], batch_size=8, shuffle=True, num_workers=2),\n    'test': torch.utils.data.DataLoader(train_transform[1600:], batch_size=8, shuffle=True, num_workers=2)\n                                            }\nconfiguration = SegformerConfig(hidden_dropout_prob = 0.3, attention_probs_dropout_prob = 0.3, classifier_dropout_prob = 0.5)\nmodel=CustomSegFormer2(configuration,in_channel=150)\nlogits,DM = model(torch.rand(8,3,224,224))\nlogits.shape","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:49:47.447505Z","iopub.status.busy":"2025-01-08T12:49:47.447301Z","iopub.status.idle":"2025-01-08T12:49:55.226912Z","shell.execute_reply":"2025-01-08T12:49:55.226093Z"},"papermill":{"duration":7.785484,"end_time":"2025-01-08T12:49:55.228263","exception":false,"start_time":"2025-01-08T12:49:47.442779","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"bba28269","cell_type":"code","source":"criterion = nn.CrossEntropyLoss(ignore_index = 5, weight = torch.tensor([1.0, 5.0,5.0,8.0,10.0]).to(device) )\n#criterion = dice_loss\n#criterion = FocalTverskyLoss()\nmean_iou = evaluate.load(\"abidlabs/mean_iou\")","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:49:55.238722Z","iopub.status.busy":"2025-01-08T12:49:55.238466Z","iopub.status.idle":"2025-01-08T12:49:56.145977Z","shell.execute_reply":"2025-01-08T12:49:56.145106Z"},"papermill":{"duration":0.913855,"end_time":"2025-01-08T12:49:56.147285","exception":false,"start_time":"2025-01-08T12:49:55.233430","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"2eb17975","cell_type":"code","source":"#model.load_state_dict(torch.load(\"/kaggle/working/segformers.pth\"))\nmodel_ft = model.to(device)\noptimizer_ft = optim.AdamW(model_ft.parameters(), lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.01)\nexp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)\nmodel_ft = train_model(model_ft,dataloaders, criterion, optimizer_ft, exp_lr_scheduler, name=\"Segformer\", num_epochs=2, metric = mean_iou)","metadata":{"execution":{"iopub.execute_input":"2025-01-08T12:49:56.157794Z","iopub.status.busy":"2025-01-08T12:49:56.157576Z","iopub.status.idle":"2025-01-08T13:09:45.925845Z","shell.execute_reply":"2025-01-08T13:09:45.924886Z"},"papermill":{"duration":1189.774899,"end_time":"2025-01-08T13:09:45.927161","exception":false,"start_time":"2025-01-08T12:49:56.152262","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"486afe03","cell_type":"code","source":"torch.save(model_ft.state_dict(),'/kaggle/working/segformers.pth')\n","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:46.083144Z","iopub.status.busy":"2025-01-08T13:09:46.082870Z","iopub.status.idle":"2025-01-08T13:09:46.577963Z","shell.execute_reply":"2025-01-08T13:09:46.577256Z"},"papermill":{"duration":0.5751,"end_time":"2025-01-08T13:09:46.579480","exception":false,"start_time":"2025-01-08T13:09:46.004380","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"c16749b2","cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/working/segformers.pth\"))\nmodel.to(device)","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:46.735258Z","iopub.status.busy":"2025-01-08T13:09:46.734984Z","iopub.status.idle":"2025-01-08T13:09:47.123503Z","shell.execute_reply":"2025-01-08T13:09:47.122628Z"},"papermill":{"duration":0.469278,"end_time":"2025-01-08T13:09:47.126567","exception":false,"start_time":"2025-01-08T13:09:46.657289","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"cc30db32","cell_type":"code","source":"carry, labels = next(iter(dataloaders['test']))\noutput = model(carry['image'].to(device).float())","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:47.285588Z","iopub.status.busy":"2025-01-08T13:09:47.285289Z","iopub.status.idle":"2025-01-08T13:09:47.598344Z","shell.execute_reply":"2025-01-08T13:09:47.597232Z"},"papermill":{"duration":0.394305,"end_time":"2025-01-08T13:09:47.600067","exception":false,"start_time":"2025-01-08T13:09:47.205762","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"d61e61be","cell_type":"code","source":"# Extract logits from the first element of the output tuple\nlogits = output[0]\n\n# Apply softmax to get class probabilities\npred = logits.softmax(dim=1)\n\n# Get the predicted classes (argmax over the class dimension)\npreds = pred.argmax(dim=1)\n\nprint(preds.shape)","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:47.761307Z","iopub.status.busy":"2025-01-08T13:09:47.760978Z","iopub.status.idle":"2025-01-08T13:09:47.766218Z","shell.execute_reply":"2025-01-08T13:09:47.765268Z"},"papermill":{"duration":0.08744,"end_time":"2025-01-08T13:09:47.767530","exception":false,"start_time":"2025-01-08T13:09:47.680090","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"362fe001","cell_type":"code","source":"def plot_images(image, masks, predicted_masks, num_images=1, color_map = None):\n    # Select num_images random indices to plot\n    indices = np.random.choice(range(len(image)), num_images, replace=False)\n\n    # Plot each image and corresponding masks\n    for i, index in enumerate(indices):\n        fig, axs = plt.subplots(1, 3, figsize=(15, 5))\n        #np.transpose(carry['image'][0].numpy(),(1, 2, 0)), cmap='gray')\n        print(image[0].shape)\n        axs[0].imshow(np.transpose(image[index].numpy(), (1,2,0)), cmap='gray')\n        axs[0].set_title(\"Image\")\n        \n        print(masks.shape)\n        axs[1].imshow(masks[index], cmap=color_map)\n        axs[1].set_title(\"Ground Truth\")\n        \n        axs[2].imshow(predicted_masks[index].cpu(), cmap=color_map)\n        axs[2].set_title(\"Prediction\")\n        \n        plt.show()\n\n\n# Example usage\n# images = torch.randn(4, 3, 224, 224)  # 4 images with shape (3, 224, 224)\n# masks = torch.randint(0, 2, (4, 1, 224, 224))  # 4 ground truth masks with shape (1, 224, 224)\n# predicted_masks = torch.randint(0, 2, (4, 1, 224, 224))  # 4 predicted masks with shape (1, 224, 224)\ncolor_map = plt.cm.get_cmap('tab20', 5)\n\nplot_images(carry['image'], labels, preds, 8, color_map)","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:47.925222Z","iopub.status.busy":"2025-01-08T13:09:47.924891Z","iopub.status.idle":"2025-01-08T13:09:52.370806Z","shell.execute_reply":"2025-01-08T13:09:52.369927Z"},"papermill":{"duration":4.527811,"end_time":"2025-01-08T13:09:52.373924","exception":false,"start_time":"2025-01-08T13:09:47.846113","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"420249ab","cell_type":"code","source":"\n# Load the trained model\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmodel = CustomSegFormer2(configuration, in_channel=150)\nmodel.load_state_dict(torch.load(\"/kaggle/working/segformers.pth\"))\nmodel.to(device)\nmodel.eval()\n\n# Test data\ntest_csv = '/kaggle/input/ai-dl-multiclass-segmentation/test_class.csv'\n\ntest_images_path = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\ntest_data = pd.read_csv(test_csv)\n","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:52.647662Z","iopub.status.busy":"2025-01-08T13:09:52.647355Z","iopub.status.idle":"2025-01-08T13:09:55.286719Z","shell.execute_reply":"2025-01-08T13:09:55.285981Z"},"papermill":{"duration":2.755775,"end_time":"2025-01-08T13:09:55.288213","exception":false,"start_time":"2025-01-08T13:09:52.532438","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"98304ab2","cell_type":"code","source":"\nsubmission_data = []\n# Prepare test loader\ndef prepare_test_data(test_df, images_path):\n    data = []\n    for _, row in test_df.iterrows():\n        img_path = os.path.join(images_path, row['ImageName'] + \".jpg\")\n        if os.path.exists(img_path):\n            image = Image.open(img_path).convert(\"RGB\")\n            data.append((image, row['ClassNumber']))\n    return data\n\ntest_data_prepared = prepare_test_data(test_data, test_images_path)\n\n# Transform function (modify as per your train transforms)\ntransform = Compose([Normalize(), ToTensorV2()])\n\n# Generate predictions\ndef generate_mask(model, image, class_number, transform):\n    input_tensor = transform(image=np.array(image))['image']\n    input_tensor = input_tensor.unsqueeze(0).to(device)\n    with torch.no_grad():\n        logits, _ = model(input_tensor)\n    probs = torch.softmax(logits, dim=1)\n    mask = (probs.argmax(dim=1) == class_number).squeeze().cpu().numpy()\n    return mask\n\ndef encode_mask_to_rle(mask):\n    '''\n    mask: numpy array binary mask \n    1 - mask \n    0 - background\n    Returns encoded run length \n    '''\n    pixels = mask.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\n# Visualize first 5 predictions and generate submission data\ndef visualize_first_5_predictions(test_data, test_df, model, transform):\n    # Select the first 5 samples\n    samples = test_data[0:50]\n    for image, class_number in samples:\n        \n        # Extract the image name from the test_df DataFrame using the index\n        # Assuming the test_df has 'ImageName' and 'ClassNumber' columns, we get the first match\n        image_name = test_df.iloc[samples.index((image, class_number))]['ImageName']\n        \n        # Generate the mask for the image\n        mask = generate_mask(model, image, class_number, transform)\n        \n        # Append to submission_data\n        submission_data.append({'ImageName': image_name, 'Encoding': encode_mask_to_rle(mask)})\n        \n        # Print the RLE encoding with the correct image name\n        # print(f\"Image: {image_name}, Encoding: {encode_mask_to_rle(mask)}\")\n        # c+=1\n        # print(c)\n        # Visualize the image and its predicted mask\n        fig, ax = plt.subplots(1, 2, figsize=(12, 6))\n        ax[0].imshow(image)\n        ax[0].set_title(f\"Original Image - Class {class_number}\")\n        ax[1].imshow(mask, cmap=\"gray\")\n        ax[1].set_title(f\"Predicted Mask - Class {class_number}\")\n        plt.show()\n\n# Run visualization for the first 5 images\n\n","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:09:55.510949Z","iopub.status.busy":"2025-01-08T13:09:55.510655Z","iopub.status.idle":"2025-01-08T13:10:10.133853Z","shell.execute_reply":"2025-01-08T13:10:10.133114Z"},"papermill":{"duration":14.736768,"end_time":"2025-01-08T13:10:10.135560","exception":false,"start_time":"2025-01-08T13:09:55.398792","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"4c5f967e","cell_type":"code","source":"visualize_first_5_predictions(test_data_prepared, test_data, model, transform)\n","metadata":{"execution":{"iopub.execute_input":"2025-01-08T13:10:10.360840Z","iopub.status.busy":"2025-01-08T13:10:10.360531Z","iopub.status.idle":"2025-01-08T13:10:36.707420Z","shell.execute_reply":"2025-01-08T13:10:36.706567Z"},"papermill":{"duration":26.463814,"end_time":"2025-01-08T13:10:36.713108","exception":false,"start_time":"2025-01-08T13:10:10.249294","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"a1d8ef4c","cell_type":"code","source":"","metadata":{"papermill":{"duration":0.488272,"end_time":"2025-01-08T13:10:37.684681","exception":false,"start_time":"2025-01-08T13:10:37.196409","status":"completed"},"tags":[]},"outputs":[],"execution_count":null}]}