{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!git clone https://github.com/dlbkv/Test-Task-W.AI","metadata":{"execution":{"iopub.status.busy":"2024-07-14T15:08:41.450840Z","iopub.execute_input":"2024-07-14T15:08:41.451269Z","iopub.status.idle":"2024-07-14T15:08:52.694453Z","shell.execute_reply.started":"2024-07-14T15:08:41.451236Z","shell.execute_reply":"2024-07-14T15:08:52.693031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q segmentation_models","metadata":{"execution":{"iopub.status.busy":"2024-07-14T15:45:15.526006Z","iopub.execute_input":"2024-07-14T15:45:15.526781Z","iopub.status.idle":"2024-07-14T15:45:31.183104Z","shell.execute_reply.started":"2024-07-14T15:45:15.526740Z","shell.execute_reply":"2024-07-14T15:45:31.181573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ[\"SM_FRAMEWORK\"] = \"tf.keras\"","metadata":{"execution":{"iopub.status.busy":"2024-07-14T16:14:16.022828Z","iopub.execute_input":"2024-07-14T16:14:16.023325Z","iopub.status.idle":"2024-07-14T16:14:16.029410Z","shell.execute_reply.started":"2024-07-14T16:14:16.023290Z","shell.execute_reply":"2024-07-14T16:14:16.027892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport random\nfrom PIL import Image\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.applications.resnet import preprocess_input\nfrom segmentation_models.losses import DiceLoss\nfrom segmentation_models.metrics import FScore\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-14T17:17:34.581285Z","iopub.execute_input":"2024-07-14T17:17:34.581784Z","iopub.status.idle":"2024-07-14T17:17:34.589198Z","shell.execute_reply.started":"2024-07-14T17:17:34.581750Z","shell.execute_reply":"2024-07-14T17:17:34.587644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = '/kaggle/input/airbus-ship-detection/test_v2'","metadata":{"execution":{"iopub.status.busy":"2024-07-14T16:43:36.137992Z","iopub.execute_input":"2024-07-14T16:43:36.138426Z","iopub.status.idle":"2024-07-14T16:43:36.144004Z","shell.execute_reply.started":"2024-07-14T16:43:36.138392Z","shell.execute_reply":"2024-07-14T16:43:36.142527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#функція для обробки зображень\n\ndef preprocess_image(image_path, target_size=(768, 768), preprocessing_fn=None):\n    image = load_img(image_path, target_size=target_size)\n    image = img_to_array(image)\n    image = np.expand_dims(image, axis=0)  # додати розмір батчу\n    if preprocessing_fn:\n        images = preprocessing_fn(image)\n\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-07-14T16:52:38.598391Z","iopub.execute_input":"2024-07-14T16:52:38.598797Z","iopub.status.idle":"2024-07-14T16:52:38.605148Z","shell.execute_reply.started":"2024-07-14T16:52:38.598769Z","shell.execute_reply":"2024-07-14T16:52:38.604046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# завантаження моделі та користувальницьких об'єктів\n\ndef dice_loss_from_config(config):\n    return DiceLoss()\n\ndef fscore_from_config(config):\n    return FScore()\n\ncustom_objects = {\n    'DiceLoss': dice_loss_from_config,\n    'FScore': fscore_from_config,\n}\nmodel = load_model('/kaggle/working/Test-Task-W.AI/saved_model.keras', custom_objects=custom_objects)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-14T16:35:06.315445Z","iopub.execute_input":"2024-07-14T16:35:06.315846Z","iopub.status.idle":"2024-07-14T16:35:06.321317Z","shell.execute_reply.started":"2024-07-14T16:35:06.315816Z","shell.execute_reply":"2024-07-14T16:35:06.319997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# завантаження зображень та виборка випадкових\nimage_files = os.listdir(folder_path)\nrandom_image_files = random.sample(image_files, k=7)\n\nplt.figure(figsize=(15, 10))\n\nfor i, image_file in enumerate(random_image_files, start=1):\n    image_path = os.path.join(folder_path, image_file)\n    \n    # Предобробка зображення\n    image = preprocess_image(image_path, target_size=(768, 768), preprocessing_fn=preprocess_input)\n    \n    # зображення для візуалізації\n    image_to_viz = Image.open(image_path).convert(\"RGB\")\n    image_to_viz = np.array(image_to_viz)\n    \n    # передбачення моделі\n    predictions = model.predict(image)\n    \n    # Візуалізація\n    plt.subplot(1, 2, 1)\n    plt.imshow(image_to_viz.astype(np.uint8))\n    plt.title('Original Image')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(predictions.squeeze(), cmap='gray')\n    plt.title('Predicted Mask')\n    plt.axis('off')\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2024-07-14T17:24:38.523270Z","iopub.execute_input":"2024-07-14T17:24:38.523810Z","iopub.status.idle":"2024-07-14T17:24:52.173422Z","shell.execute_reply.started":"2024-07-14T17:24:38.523775Z","shell.execute_reply":"2024-07-14T17:24:52.172120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}