{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-06T18:54:24.396927Z","iopub.status.idle":"2023-05-06T18:54:24.397668Z","shell.execute_reply.started":"2023-05-06T18:54:24.39741Z","shell.execute_reply":"2023-05-06T18:54:24.397432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, gc\n\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:43:50.54526Z","iopub.execute_input":"2023-06-11T00:43:50.545685Z","iopub.status.idle":"2023-06-11T00:43:53.096492Z","shell.execute_reply.started":"2023-06-11T00:43:50.545651Z","shell.execute_reply":"2023-06-11T00:43:53.095307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\nif torch.cuda.is_available():\n   \n    num_devices = torch.cuda.device_count()\n    print(f\"Number of available GPUs: {num_devices}\")\n    \n   \n    for i in range(num_devices):\n        device_name = torch.cuda.get_device_name(i)\n        print(f\"GPU {i}: {device_name}\")\n        \n    \n    max_memory_allocated = torch.cuda.max_memory_allocated()\n    max_memory_reserved = torch.cuda.max_memory_reserved()\n    print(f\"Maximum GPU memory allocated: {max_memory_allocated/1024**3:.2f} GB\")\n    print(f\"Maximum GPU memory reserved: {max_memory_reserved/1024**3:.2f} GB\")\n    print(\"CUDA is available:\", torch.cuda.is_available())\nelse:\n    print(\"CUDA is not available\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:12.970488Z","iopub.execute_input":"2023-06-11T20:51:12.973001Z","iopub.status.idle":"2023-06-11T20:51:17.180549Z","shell.execute_reply.started":"2023-06-11T20:51:12.97295Z","shell.execute_reply":"2023-06-11T20:51:17.179005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import DataLoader, TensorDataset\nfrom sklearn.model_selection import train_test_split\n\nif torch.cuda.is_available():\n   \n    num_devices = torch.cuda.device_count()\n    print(f\"Number of available GPUs: {num_devices}\")\nelse:\n    print(\"CUDA is not available\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:17.182451Z","iopub.execute_input":"2023-06-11T20:51:17.183018Z","iopub.status.idle":"2023-06-11T20:51:17.837403Z","shell.execute_reply.started":"2023-06-11T20:51:17.182988Z","shell.execute_reply":"2023-06-11T20:51:17.836162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom numpy import expand_dims\nimport pandas as pd\nimport json\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport sys\nimport csv","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:17.838794Z","iopub.execute_input":"2023-06-11T20:51:17.83914Z","iopub.status.idle":"2023-06-11T20:51:18.190426Z","shell.execute_reply.started":"2023-06-11T20:51:17.839111Z","shell.execute_reply":"2023-06-11T20:51:18.189291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('/kaggle/input/ships-in-satellite-imagery/shipsnet.json') as data_file:\n    dataset = json.load(data_file)\nshipsnet= pd.DataFrame(dataset)\nshipsnet.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:18.19268Z","iopub.execute_input":"2023-06-11T20:51:18.193022Z","iopub.status.idle":"2023-06-11T20:51:33.78765Z","shell.execute_reply.started":"2023-06-11T20:51:18.192994Z","shell.execute_reply":"2023-06-11T20:51:33.786526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shipsnet.info()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:33.789153Z","iopub.execute_input":"2023-06-11T20:51:33.790335Z","iopub.status.idle":"2023-06-11T20:51:33.82035Z","shell.execute_reply.started":"2023-06-11T20:51:33.790291Z","shell.execute_reply":"2023-06-11T20:51:33.819189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shipsnet = shipsnet[[\"data\", \"labels\"]]\nshipsnet.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:33.821932Z","iopub.execute_input":"2023-06-11T20:51:33.822281Z","iopub.status.idle":"2023-06-11T20:51:33.840923Z","shell.execute_reply.started":"2023-06-11T20:51:33.822251Z","shell.execute_reply":"2023-06-11T20:51:33.840036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(shipsnet[\"data\"].iloc[0])","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:33.842279Z","iopub.execute_input":"2023-06-11T20:51:33.842834Z","iopub.status.idle":"2023-06-11T20:51:33.853658Z","shell.execute_reply.started":"2023-06-11T20:51:33.842806Z","shell.execute_reply":"2023-06-11T20:51:33.852403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ship_images = shipsnet[\"labels\"].value_counts()[0]\nno_ship_images = shipsnet[\"labels\"].value_counts()[1]\nprint(\"Number of the ship_images :{}\".format(ship_images),\"\\n\")\nprint(\"Number of the no ship_images :{}\".format(no_ship_images))","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:33.855017Z","iopub.execute_input":"2023-06-11T20:51:33.855441Z","iopub.status.idle":"2023-06-11T20:51:33.866195Z","shell.execute_reply.started":"2023-06-11T20:51:33.855404Z","shell.execute_reply":"2023-06-11T20:51:33.865073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(dataset['data']).astype('uint8')\ny = np.array(dataset['labels']).astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:33.86761Z","iopub.execute_input":"2023-06-11T20:51:33.8681Z","iopub.status.idle":"2023-06-11T20:51:45.06131Z","shell.execute_reply.started":"2023-06-11T20:51:33.868071Z","shell.execute_reply":"2023-06-11T20:51:45.060067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_reshaped = x.reshape(-1, 3, 80, 80).transpose(0, 2, 3, 1)\nx_reshaped = x_reshaped / 255","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:45.064653Z","iopub.execute_input":"2023-06-11T20:51:45.065003Z","iopub.status.idle":"2023-06-11T20:51:45.272605Z","shell.execute_reply.started":"2023-06-11T20:51:45.064975Z","shell.execute_reply":"2023-06-11T20:51:45.271411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_reshaped = np.eye(2)[y]","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:45.274009Z","iopub.execute_input":"2023-06-11T20:51:45.274338Z","iopub.status.idle":"2023-06-11T20:51:45.279505Z","shell.execute_reply.started":"2023-06-11T20:51:45.274312Z","shell.execute_reply":"2023-06-11T20:51:45.278422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train_1, x_test, y_train_1, y_test = train_test_split(x_reshaped, y_reshaped, test_size=0.20, random_state=42)\n\nx_train, x_val, y_train, y_val = train_test_split(x_train_1, y_train_1, test_size=0.25, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:45.280814Z","iopub.execute_input":"2023-06-11T20:51:45.28113Z","iopub.status.idle":"2023-06-11T20:51:45.794373Z","shell.execute_reply.started":"2023-06-11T20:51:45.281103Z","shell.execute_reply":"2023-06-11T20:51:45.793277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom torch.utils.data import Dataset, TensorDataset, DataLoader\n\nx_train_torch = torch.tensor(np.transpose(x_train, (0, 3, 1, 2)), dtype=torch.float32)\ny_train_torch = torch.tensor(y_train, dtype=torch.float32)\nx_val_torch = torch.tensor(np.transpose(x_val, (0, 3, 1, 2)), dtype=torch.float32)\ny_val_torch = torch.tensor(y_val, dtype=torch.float32)\n\ntrain_dataset = TensorDataset(x_train_torch, y_train_torch)\nval_dataset = TensorDataset(x_val_torch, y_val_torch)\n\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:51:45.795912Z","iopub.execute_input":"2023-06-11T20:51:45.796375Z","iopub.status.idle":"2023-06-11T20:51:45.999303Z","shell.execute_reply.started":"2023-06-11T20:51:45.796316Z","shell.execute_reply":"2023-06-11T20:51:45.998269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, TensorDataset\nfrom torchvision.transforms import Normalize\nimport torch.nn.functional as F\n\n\nclass Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(3, 512, 3, padding=1)\n        self.bn1 = nn.BatchNorm2d(512)\n        self.pool1 = nn.MaxPool2d(2, 2)\n        self.dropout1 = nn.Dropout(0.25)\n\n        self.conv2 = nn.Conv2d(512,256, 3, padding=1)\n        self.bn2 = nn.BatchNorm2d(256)\n        self.pool2 = nn.MaxPool2d(2, 2)\n        self.dropout2 = nn.Dropout(0.25)\n\n        self.conv3 = nn.Conv2d(256, 128, 3, padding=1)\n        self.bn3 = nn.BatchNorm2d(128)\n        self.pool3 = nn.MaxPool2d(2, 2)\n        self.dropout3 = nn.Dropout(0.25)\n\n        self.conv4 = nn.Conv2d(128, 64, 3, padding=1)\n        self.bn4 = nn.BatchNorm2d(64)\n        self.pool4 = nn.MaxPool2d(2, 2)\n        self.dropout4 = nn.Dropout(0.20)\n        \n        \n\n        self.flatten = nn.Flatten()\n         \n        self.fc1 = nn.Linear(64 * 5 * 5, 300)\n        self.fc2 = nn.Linear(300, 150)\n        self.fc3 = nn.Linear(150, 2)\n\n    def forward(self, x):\n        x = F.relu(self.bn1(self.conv1(x)))\n        x = self.pool1(x)\n        x = self.dropout1(x)\n\n        x = F.relu(self.bn2(self.conv2(x)))\n        x = self.pool2(x)\n        x = self.dropout2(x)\n\n        x = F.relu(self.bn3(self.conv3(x)))\n        x = self.pool3(x)\n        x = self.dropout3(x)\n\n        x = F.relu(self.bn4(self.conv4(x)))\n        x = self.pool4(x)\n        x = self.dropout4(x)\n\n        x = self.flatten(x)\n        print(f'Shape before fc1: {x.shape}')\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.softmax(self.fc3(x), dim=1)\n        return x\n\nmodel = Net()\n# Move the model to GPU(s)\nif torch.cuda.is_available():\n    model = model.cuda()\n    if torch.cuda.device_count() > 1:\n        model = nn.DataParallel(model)\n        \ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters())\n\nnum_epochs = 100\nearly_stop_patience = 10\nbest_val_loss = float('inf')\ncounter = 0\n\ntrain_losses = []\ntrain_accuracies = []\nval_losses = []\nval_accuracies = []\n\nfor epoch in range(num_epochs):\n    train_loss = 0\n    train_correct = 0\n    val_loss = 0\n    val_correct = 0\n\n    model.train()\n    running_loss = 0.0\n    for i, (inputs, labels) in enumerate(train_loader):\n        inputs = inputs\n        labels = labels\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n\n        train_loss += loss.item()\n        _, predicted = torch.max(outputs, 1)\n        train_correct += (predicted == labels).sum().item()\n\n    model.eval()\n    val_loss = 0.0\n    with torch.no_grad():\n        for inputs, labels in val_loader:\n            inputs = inputs\n            labels = labels\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n\n            val_loss += loss.item()\n            _, predicted = torch.max(outputs, 1)\n            val_correct += (predicted == labels).sum().item()\n\n    train_losses.append(train_loss / len(train_loader))\n    train_accuracies.append(100 * train_correct / len(train_loader.dataset))\n    val_losses.append(val_loss / len(val_loader))\n    val_accuracies.append(100 * val_correct / len(val_loader.dataset))\n\n    print(f\"Epoch {epoch + 1}/{num_epochs}, Train Loss: {train_losses[-1]:.4f}, Train Acc: {train_accuracies[-1]:.2f}%, Val Loss: {val_losses[-1]:.4f}, Val Acc: {val_accuracies[-1]:.2f}%\")\n\n    if val_loss < best_val_loss:\n        best_val_loss = val_loss\n        torch.save(model.state_dict(), 'best_model.pth')\n        counter = 0\n    else:\n        counter += 1\n        if counter >= early_stop_patience:\n            print(f\"Early stopping after {counter} epochs with no improvement.\")\n            break\n\n#model.load_state_dict(torch.load('best_model.pth'))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T20:56:17.300939Z","iopub.execute_input":"2023-06-11T20:56:17.301487Z","iopub.status.idle":"2023-06-11T20:56:23.921158Z","shell.execute_reply.started":"2023-06-11T20:56:17.301443Z","shell.execute_reply":"2023-06-11T20:56:23.919456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:47:38.605772Z","iopub.execute_input":"2023-06-11T00:47:38.606297Z","iopub.status.idle":"2023-06-11T00:47:38.614121Z","shell.execute_reply.started":"2023-06-11T00:47:38.606254Z","shell.execute_reply":"2023-06-11T00:47:38.612351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(train_losses, label=\"Training Loss\")\nplt.plot(val_losses, label=\"Validation Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(train_accuracies, label=\"Training Accuracy\")\nplt.plot(val_accuracies, label=\"Validation Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:47:38.844076Z","iopub.execute_input":"2023-06-11T00:47:38.844585Z","iopub.status.idle":"2023-06-11T00:47:39.831848Z","shell.execute_reply.started":"2023-06-11T00:47:38.84455Z","shell.execute_reply":"2023-06-11T00:47:39.830948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nx_test_torch = torch.tensor(np.transpose(x_test, (0, 3, 1, 2)), dtype=torch.float32).cuda()\ny_test_torch = torch.tensor(y_test, dtype=torch.float32).cuda()\n\n\ntest_dataset = TensorDataset(x_test_torch, y_test_torch)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)\n\n\nmodel.eval()\ncorrect = 0\ntotal = 0\n\nwith torch.no_grad():\n    inputs = inputs.cuda()  \n    labels = labels.cuda() \n    for inputs, labels in test_loader:\n        outputs = model(inputs)\n        _, predicted = torch.max(outputs, 1)\n        _, labels = torch.max(labels, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\naccuracy = 100 * correct / total\nprint(\"Test Accuracy: {:.2f}%\".format(accuracy))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:40:25.466224Z","iopub.execute_input":"2023-06-10T15:40:25.466607Z","iopub.status.idle":"2023-06-10T15:40:25.967158Z","shell.execute_reply.started":"2023-06-10T15:40:25.466575Z","shell.execute_reply":"2023-06-10T15:40:25.966179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    predictions = model(x_test_torch)\n\nprobabilities = F.softmax(predictions, dim=1).cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:40:29.632727Z","iopub.execute_input":"2023-06-10T15:40:29.633303Z","iopub.status.idle":"2023-06-10T15:40:30.487178Z","shell.execute_reply.started":"2023-06-10T15:40:29.633269Z","shell.execute_reply":"2023-06-10T15:40:30.485563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nsingle_prediction = pd.Series(probabilities[2], index=[\"Not A Ship\", \"Ship\"])\nprint(single_prediction)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:40:34.844623Z","iopub.execute_input":"2023-06-10T15:40:34.84513Z","iopub.status.idle":"2023-06-10T15:40:34.899347Z","shell.execute_reply.started":"2023-06-10T15:40:34.845102Z","shell.execute_reply":"2023-06-10T15:40:34.897849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\nfirst_test_image = x_test[2]\n\n\nplt.imshow(first_test_image)\nplt.title(\"First Test Image\")\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T22:58:06.922778Z","iopub.execute_input":"2023-06-09T22:58:06.923469Z","iopub.status.idle":"2023-06-09T22:58:07.102836Z","shell.execute_reply.started":"2023-06-09T22:58:06.923435Z","shell.execute_reply":"2023-06-09T22:58:07.101855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef scanmap(image_path, model, device):\n    satellite_image = cv2.imread(image_path)\n    satellite_image = satellite_image.astype(np.float16) / 255.0\n\n    window_size = (80, 80)\n    stride = 10\n\n    height, width, channels = satellite_image.shape\n\n    probabilities_map = []\n\n    model.to(device)  # ensure model is on correct device\n\n    for y in range(0, height - window_size[1] + 1, stride):\n        row_probabilities = []\n        for x in range(0, width - window_size[0] + 1, stride):\n            cropped_window = satellite_image[y:y + window_size[1], x:x + window_size[0]]\n            cropped_window_torch = torch.tensor(cropped_window.transpose(2, 0, 1), dtype=torch.float32).unsqueeze(0)\n\n            cropped_window_torch = cropped_window_torch.to(device)  # move data to the same device as model\n        \n            with torch.no_grad():\n                probabilities = model(cropped_window_torch)\n        \n            row_probabilities.append(probabilities[0, 1].item())\n    \n        probabilities_map.append(row_probabilities)\n\n    probabilities_map = np.array(probabilities_map)\n\n    plt.imshow(probabilities_map, cmap='hot', interpolation='nearest')\n    plt.colorbar()\n    plt.show()\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')  # determine the compute device\n\n# make sure the model variable is defined and contains the trained model\nscanmap(\"/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png\", model, device)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:47:45.943616Z","iopub.execute_input":"2023-06-11T00:47:45.943975Z","iopub.status.idle":"2023-06-11T00:48:45.058801Z","shell.execute_reply.started":"2023-06-11T00:47:45.943943Z","shell.execute_reply":"2023-06-11T00:48:45.057903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scanmap(\"/kaggle/input/ships-in-satellite-imagery/scenes/scenes/lb_1.png\",model, device)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:46:48.140083Z","iopub.execute_input":"2023-06-10T15:46:48.141132Z","iopub.status.idle":"2023-06-10T15:47:27.345934Z","shell.execute_reply.started":"2023-06-10T15:46:48.1411Z","shell.execute_reply":"2023-06-10T15:47:27.343834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"using_colab = True","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:00:35.729086Z","iopub.execute_input":"2023-05-08T19:00:35.729522Z","iopub.status.idle":"2023-05-08T19:00:35.739633Z","shell.execute_reply.started":"2023-05-08T19:00:35.729488Z","shell.execute_reply":"2023-05-08T19:00:35.73725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if using_colab:\n    import torch\n    import torchvision\n    print(\"PyTorch version:\", torch.__version__)\n    print(\"Torchvision version:\", torchvision.__version__)\n    print(\"CUDA is available:\", torch.cuda.is_available())\n    import sys\n    !{sys.executable} -m pip install opencv-python matplotlib\n    !{sys.executable} -m pip install 'git+https://github.com/facebookresearch/segment-anything.git'\n    \n    !mkdir images\n    !wget -P images https://raw.githubusercontent.com/facebookresearch/segment-anything/main/notebooks/images/dog.jpg\n        \n    !wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:00:36.081031Z","iopub.execute_input":"2023-05-08T19:00:36.081345Z","iopub.status.idle":"2023-05-08T19:01:18.565153Z","shell.execute_reply.started":"2023-05-08T19:00:36.081318Z","shell.execute_reply":"2023-05-08T19:01:18.564008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:01:28.990345Z","iopub.execute_input":"2023-05-08T19:01:28.991422Z","iopub.status.idle":"2023-05-08T19:01:29.158311Z","shell.execute_reply.started":"2023-05-08T19:01:28.991376Z","shell.execute_reply":"2023-05-08T19:01:29.157465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png')\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:01:30.344326Z","iopub.execute_input":"2023-05-08T19:01:30.344659Z","iopub.status.idle":"2023-05-08T19:01:30.735331Z","shell.execute_reply.started":"2023-05-08T19:01:30.344633Z","shell.execute_reply":"2023-05-08T19:01:30.734144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nplt.imshow(image)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:01:32.070421Z","iopub.execute_input":"2023-05-08T19:01:32.070783Z","iopub.status.idle":"2023-05-08T19:01:34.073387Z","shell.execute_reply.started":"2023-05-08T19:01:32.070753Z","shell.execute_reply":"2023-05-08T19:01:34.07192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"..\")\nfrom segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor\n\nsam_checkpoint = \"sam_vit_h_4b8939.pth\"\nmodel_type = \"vit_h\"\n\ndevice = \"cuda\"\n\nsam = sam_model_registry[model_type](checkpoint=sam_checkpoint)\nsam.to(device=device)\n\nmask_generator = SamAutomaticMaskGenerator(sam)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:01:39.337315Z","iopub.execute_input":"2023-05-08T19:01:39.338398Z","iopub.status.idle":"2023-05-08T19:01:51.150232Z","shell.execute_reply.started":"2023-05-08T19:01:39.338363Z","shell.execute_reply":"2023-05-08T19:01:51.149318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = mask_generator.generate(image)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:01:56.928924Z","iopub.execute_input":"2023-05-08T19:01:56.929283Z","iopub.status.idle":"2023-05-08T19:02:08.762257Z","shell.execute_reply.started":"2023-05-08T19:01:56.929255Z","shell.execute_reply":"2023-05-08T19:02:08.761319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(masks))\nprint(masks[0].keys())","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:02:20.712753Z","iopub.execute_input":"2023-05-08T19:02:20.713143Z","iopub.status.idle":"2023-05-08T19:02:20.720586Z","shell.execute_reply.started":"2023-05-08T19:02:20.71311Z","shell.execute_reply":"2023-05-08T19:02:20.719581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_anns(anns):\n    if len(anns) == 0:\n        return\n    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n    ax = plt.gca()\n    ax.set_autoscale_on(False)\n    polygons = []\n    color = []\n    for ann in sorted_anns:\n        m = ann['segmentation']\n        img = np.ones((m.shape[0], m.shape[1], 3))\n        color_mask = np.random.random((1, 3)).tolist()[0]\n        for i in range(3):\n            img[:,:,i] = color_mask[i]\n        ax.imshow(np.dstack((img, m*0.35)))","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:02:22.116643Z","iopub.execute_input":"2023-05-08T19:02:22.116986Z","iopub.status.idle":"2023-05-08T19:02:22.123509Z","shell.execute_reply.started":"2023-05-08T19:02:22.116959Z","shell.execute_reply":"2023-05-08T19:02:22.122322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nplt.imshow(image)\nshow_anns(masks)\nplt.axis('off')\nplt.show() ","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:02:23.61841Z","iopub.execute_input":"2023-05-08T19:02:23.619125Z","iopub.status.idle":"2023-05-08T19:03:32.422749Z","shell.execute_reply.started":"2023-05-08T19:02:23.619085Z","shell.execute_reply":"2023-05-08T19:03:32.415038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport json\nimport shutil\nfrom PIL import Image\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n\ndef save_image_from_data(data, label, output_path):\n    image_data = np.array(data, dtype='uint8').reshape((80, 80, 3))\n    img = Image.fromarray(image_data, 'RGB')\n    img.save(output_path)\n    return output_path\n\n\n\ndef split_data_into_train_test(input_path, train_path, test_path, test_ratio=0.2):\n    with open(input_path, 'r') as f:\n        dataset = json.load(f)\n\n    if not os.path.exists(train_path):\n        os.makedirs(train_path)\n        os.makedirs(os.path.join(train_path, 'ship'))\n        os.makedirs(os.path.join(train_path, 'non_ship'))\n\n    if not os.path.exists(test_path):\n        os.makedirs(test_path)\n        os.makedirs(os.path.join(test_path, 'ship'))\n        os.makedirs(os.path.join(test_path, 'non_ship'))\n\n    images_and_labels = [(save_image_from_data(data, label, f\"/kaggle/working/temp/{i}.png\"), label)\n                         for i, (data, label) in enumerate(zip(dataset['data'], dataset['labels']))]\n\n    train_data, test_data = train_test_split(images_and_labels, test_size=test_ratio, random_state=42, stratify=[label for _, label in images_and_labels])\n\n    for i, (image_path, label) in enumerate(train_data):\n        class_folder = 'ship' if label == 1 else 'non_ship'\n        shutil.move(image_path, os.path.join(train_path, class_folder, f\"{i}.png\"))\n\n    for i, (image_path, label) in enumerate(test_data):\n        class_folder = 'ship' if label == 1 else 'non_ship'\n        shutil.move(image_path, os.path.join(test_path, class_folder, f\"{i}.png\"))\n\n    shutil.rmtree('/kaggle/working/temp')\n\ninput_path = \"/kaggle/input/ships-in-satellite-imagery/shipsnet.json\"\ntrain_path = \"/kaggle/working/train/data\"\ntest_path = \"/kaggle/working/test/data\"\n\nos.makedirs('/kaggle/working/temp', exist_ok=True)\nsplit_data_into_train_test(input_path, train_path, test_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:15:02.135342Z","iopub.execute_input":"2023-05-08T19:15:02.136159Z","iopub.status.idle":"2023-05-08T19:15:18.173289Z","shell.execute_reply.started":"2023-05-08T19:15:02.136116Z","shell.execute_reply":"2023-05-08T19:15:18.172316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport torch\nimport torch.nn as nn\nfrom torch.optim import Adam\nfrom torchvision.transforms import Compose, ToTensor, Normalize\nfrom torch.utils.data import DataLoader\nfrom torchvision.datasets import ImageFolder\n\n\ndef load_ship_data(train_path, test_path, train_batch_size=64, test_batch_size=64):\n    transform = Compose([\n        ToTensor(),\n        Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n    ])\n\n    train_loader = DataLoader(\n        ImageFolder(train_path, transform=transform),\n        batch_size=train_batch_size, shuffle=True)\n\n    test_loader = DataLoader(\n        ImageFolder(test_path, transform=transform),\n        batch_size=test_batch_size, shuffle=False)\n\n    return train_loader, test_loader\n\n\nclass Net(torch.nn.Module):\n    def __init__(self, dims):\n        super().__init__()\n        self.layers = []\n        self.loss_history = []\n        self.train_accuracy_history = []\n        for d in range(len(dims) - 1):\n            self.layers += [Layer(dims[d], dims[d + 1]).cuda()]\n\n    def predict(self, x):\n        h = x\n        for layer in self.layers:\n            h = layer(h)\n        return h.argmax(1)\n\n    def train(self, x_pos, x_neg, y):\n        h_pos, h_neg = x_pos, x_neg\n        for i, layer in enumerate(self.layers):\n            print('training layer', i, '...')\n            h_pos, h_neg, epoch_losses = layer.train(h_pos, h_neg)\n            self.loss_history.extend(epoch_losses)\n            train_accuracy = (self.predict(x_pos).eq(y[y == 1]).float().mean().item(),\n                              self.predict(x_neg).eq(y[y == 0]).float().mean().item())\n            self.train_accuracy_history.append(train_accuracy)\n\n\nclass Layer(nn.Linear):\n    def __init__(self, in_features, out_features,\n                 bias=True, device=None, dtype=None):\n        super().__init__(in_features, out_features, bias, device, dtype)\n        self.relu = torch.nn.ReLU()\n        self.opt = Adam(self.parameters(), lr=0.03)\n        self.threshold = 2.0\n        self.num_epochs = 1000\n\n    def forward(self, x):\n        x_direction = x / (x.norm(2, 1, keepdim=True) + 1e-4)\n        x_flattened = x_direction.reshape(x_direction.size(0), -1)\n        return self.relu(\n            torch.mm(x_flattened, self.weight.T) +\n            self.bias.unsqueeze(0))\n\n\n    def train(self, x_pos, x_neg):\n        epoch_losses = []\n        for i in range(self.num_epochs):\n            g_pos = self.forward(x_pos).pow(2).mean(1)\n            g_neg = self.forward(x_neg).pow(2).mean(1)\n            loss = torch.log(1 + torch.exp(torch.cat([\n                -g_pos + self.threshold,\n                g_neg - self.threshold]))).mean()\n            self.opt.zero_grad()\n            loss.backward()\n            self.opt.step()\n            epoch_losses.append(loss.item())\n        return self.forward(x_pos).detach(), self.forward(x_neg).detach(), epoch_losses\n\n\n\nif __name__ == \"__main__\":\n    torch.manual_seed(1234)\n\n    train_path = \"/kaggle/working/train/data\"\n    test_path = \"/kaggle/working/test/data\"\n    train_loader, test_loader = load_ship_data(train_path, test_path)\n\n    net = Net([19200, 500, 500, 2])  \n    x, y = next(iter(train_loader))\n    x, y = x.cuda(), y.cuda()\n    x_pos = x[y == 1]\n    x_neg = x[y == 0]\n\n    net.train(x_pos, x_neg,y)\n\n    x_train, y_train = next(iter(train_loader))\n    x_train, y_train = x_train.cuda(), y_train.cuda()\n    train_accuracy = net.predict(x_train).eq(y_train).float().mean().item()\n    print('train accuracy:', train_accuracy)\n\n    x_test, y_test = next(iter(test_loader))\n    x_test, y_test = x_test.cuda(), y_test.cuda()\n    test_accuracy = net.predict(x_test).eq(y_test).float().mean().item()\n    print('test accuracy:', test_accuracy)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-08T19:16:18.172749Z","iopub.execute_input":"2023-05-08T19:16:18.173274Z","iopub.status.idle":"2023-05-08T19:16:27.520312Z","shell.execute_reply.started":"2023-05-08T19:16:18.1732Z","shell.execute_reply":"2023-05-08T19:16:27.519414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Article of FF-Algorithm](https://arxiv.org/pdf/2212.13345.pdf)\n[Github Link](https://github.com/mohammadpz/pytorch_forward_forward","metadata":{}},{"cell_type":"code","source":"def process_image(input_image):\n    image = Image.fromarray(input_image).convert(\"RGB\")\n\n    start_time = time.time()\n    heatmap = scanmap(np.array(image), model)\n    elapsed_time = time.time() - start_time\n    heatmap_img = Image.fromarray(np.uint8(plt.cm.hot(heatmap) * 255)).convert('RGB')\n\n    heatmap_img = heatmap_img.resize(image.size)\n\n    return image, heatmap_img, int(elapsed_time)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T22:27:49.331102Z","iopub.execute_input":"2023-05-09T22:27:49.335947Z","iopub.status.idle":"2023-05-09T22:27:49.348343Z","shell.execute_reply.started":"2023-05-09T22:27:49.335906Z","shell.execute_reply":"2023-05-09T22:27:49.345455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scanmap(image_np, model):\n    image_np = image_np.astype(np.float32) / 255.0\n\n    window_size = (80, 80)\n    stride = 10\n\n    height, width, channels = image_np.shape\n\n    probabilities_map = []\n\n    for y in range(0, height - window_size[1] + 1, stride):\n        row_probabilities = []\n        for x in range(0, width - window_size[0] + 1, stride):\n            cropped_window = image_np[y:y + window_size[1], x:x + window_size[0]]\n            cropped_window_torch = transforms.ToTensor()(cropped_window).unsqueeze(0)\n\n            with torch.no_grad():\n                probabilities = model(cropped_window_torch)\n\n            row_probabilities.append(probabilities[0, 1].item())\n\n        probabilities_map.append(row_probabilities)\n\n    probabilities_map = np.array(probabilities_map)\n    return probabilities_map","metadata":{"execution":{"iopub.status.busy":"2023-05-09T22:27:58.762727Z","iopub.execute_input":"2023-05-09T22:27:58.763337Z","iopub.status.idle":"2023-05-09T22:27:58.782796Z","shell.execute_reply.started":"2023-05-09T22:27:58.763293Z","shell.execute_reply":"2023-05-09T22:27:58.781656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom torchvision import transforms\n\nimage_path = \"/kaggle/input/ships-in-satellite-imagery/scenes/scenes/lb_1.png\"\nimage_np = cv2.imread(image_path)\nimage_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)\n\nheatmap = scanmap(image_np, model)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T22:31:09.83432Z","iopub.execute_input":"2023-05-09T22:31:09.835144Z","iopub.status.idle":"2023-05-09T22:32:49.971471Z","shell.execute_reply.started":"2023-05-09T22:31:09.835109Z","shell.execute_reply":"2023-05-09T22:32:49.97035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heatmap","metadata":{"execution":{"iopub.status.busy":"2023-05-09T22:32:55.857953Z","iopub.execute_input":"2023-05-09T22:32:55.858325Z","iopub.status.idle":"2023-05-09T22:32:55.868795Z","shell.execute_reply.started":"2023-05-09T22:32:55.858294Z","shell.execute_reply":"2023-05-09T22:32:55.867536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the heatmap\nheatmap_normalized = (heatmap - np.min(heatmap)) / (np.max(heatmap) - np.min(heatmap))\n\n# Plot the heatmap\nplt.imshow(heatmap_normalized, cmap='hot')\nplt.colorbar()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T22:33:52.221717Z","iopub.execute_input":"2023-05-09T22:33:52.222148Z","iopub.status.idle":"2023-05-09T22:33:52.566116Z","shell.execute_reply.started":"2023-05-09T22:33:52.22211Z","shell.execute_reply":"2023-05-09T22:33:52.565224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"_____________________________________________________________________________________________________________","metadata":{}},{"cell_type":"markdown","source":"# **Airbus**\n# ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom fastai.vision import *\n\n\ndataset = pd.read_csv('/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv')\nshipsnet = pd.DataFrame(dataset)\nshipsnet.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:42:59.549457Z","iopub.execute_input":"2023-05-24T19:42:59.550123Z","iopub.status.idle":"2023-05-24T19:43:00.055502Z","shell.execute_reply.started":"2023-05-24T19:42:59.55009Z","shell.execute_reply":"2023-05-24T19:43:00.054488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shipsnet.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:09:00.932715Z","iopub.execute_input":"2023-05-24T19:09:00.933254Z","iopub.status.idle":"2023-05-24T19:09:01.166061Z","shell.execute_reply.started":"2023-05-24T19:09:00.933222Z","shell.execute_reply":"2023-05-24T19:09:01.164862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = './'\nTRAIN = '/kaggle/input/airbus-ship-detection/train_v2'\nTEST = '/kaggle/input/airbus-ship-detection/test_v2'\nSEGMENTATION = '/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv'\n#PRETRAINED = '../input/fine-tuning-resnet34-on-ship-detection/models/Resnet34_lable_256_1.h5'\nexclude_list = ['6384c3e78.jpg','13703f040.jpg', '14715c06d.jpg',  '33e0ff2d5.jpg',\n                '4d4e09f2a.jpg', '877691df8.jpg', '8b909bb20.jpg', 'a8d99130e.jpg', \n                'ad55c3143.jpg', 'c8260c541.jpg', 'd6c7f17c7.jpg', 'dc3e7c901.jpg',\n                'e44dffe88.jpg', 'ef87bad36.jpg', 'f083256d8.jpg'] #corrupted images","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:26:46.247379Z","iopub.execute_input":"2023-05-24T19:26:46.248262Z","iopub.status.idle":"2023-05-24T19:26:46.254403Z","shell.execute_reply.started":"2023-05-24T19:26:46.248217Z","shell.execute_reply":"2023-05-24T19:26:46.25351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_names = [f for f in os.listdir(TRAIN)]\ntest_names = [f for f in os.listdir(TEST)]\nfor el in exclude_list:\n    if(el in train_names): train_names.remove(el)\n    if(el in test_names): test_names.remove(el)\n#5% of data in the validation set is sufficient for model evaluation\ntr_n, val_n = train_test_split(train_names, test_size=0.05, random_state=42)\nsegmentation_df = pd.read_csv(os.path.join(PATH, SEGMENTATION)).set_index('ImageId')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:27:17.938453Z","iopub.execute_input":"2023-05-24T19:27:17.93885Z","iopub.status.idle":"2023-05-24T19:27:21.958199Z","shell.execute_reply.started":"2023-05-24T19:27:17.938822Z","shell.execute_reply":"2023-05-24T19:27:21.957215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cut_empty(names):\n    return [name for name in names \n            if(type(segmentation_df.loc[name]['EncodedPixels']) != float)]\n\ntr_n = cut_empty(tr_n)\nval_n = cut_empty(val_n)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:27:33.012358Z","iopub.execute_input":"2023-05-24T19:27:33.013241Z","iopub.status.idle":"2023-05-24T19:27:45.179996Z","shell.execute_reply.started":"2023-05-24T19:27:33.013195Z","shell.execute_reply":"2023-05-24T19:27:45.178879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_mask(img_id, df):\n    shape = (768,768)\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    masks = df.loc[img_id]['EncodedPixels']\n    if(type(masks) == float): return img.reshape(shape)\n    if(type(masks) == str): masks = [masks]\n    for mask in masks:\n        s = mask.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:27:52.652068Z","iopub.execute_input":"2023-05-24T19:27:52.65242Z","iopub.status.idle":"2023-05-24T19:27:52.661904Z","shell.execute_reply.started":"2023-05-24T19:27:52.652384Z","shell.execute_reply":"2023-05-24T19:27:52.660734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nclass pdFilesDataset(FilesDataset):\n    def __init__(self, fnames, path, transform):\n        self.segmentation_df = pd.read_csv(SEGMENTATION).set_index('ImageId')\n        super().__init__(fnames, transform, path)\n    \n    def get_x(self, i):\n        img = open_image(os.path.join(self.path, self.fnames[i]))\n        if self.sz == 768: return img \n        else: return cv2.resize(img, (self.sz, self.sz))\n    \n    def get_y(self, i):\n        mask = np.zeros((768,768), dtype=np.uint8) if (self.path == TEST) \\\n            else get_mask(self.fnames[i], self.segmentation_df)\n        img = Image.fromarray(mask).resize((self.sz, self.sz)).convert('RGB')\n        return np.array(img).astype(np.float32)\n    \n    def get_c(self): return 0","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:44:14.703874Z","iopub.execute_input":"2023-05-24T19:44:14.704222Z","iopub.status.idle":"2023-05-24T19:44:14.753824Z","shell.execute_reply.started":"2023-05-24T19:44:14.704194Z","shell.execute_reply":"2023-05-24T19:44:14.752411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AIRBUS2","metadata":{}},{"cell_type":"markdown","source":"1","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import Dataset\nfrom torchvision import transforms\nfrom PIL import Image\n\nclass AirbusShipDataset(Dataset):\n    def __init__(self, image_paths, mask_paths, transform=None):\n        self.image_paths = image_paths\n        self.mask_paths = mask_paths\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, idx):\n        image = Image.open(self.image_paths[idx])\n        mask = Image.open(self.mask_paths[idx])\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, mask\n\n# Define your transforms\ntransform = transforms.Compose([\n    transforms.Resize((256, 256)), # Resize images\n    transforms.ToTensor(), # Convert to PyTorch tensors\n])\n\n# Create your dataset\ndataset = AirbusShipDataset(image_paths, mask_paths, transform)\n\n# Create your DataLoader\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:08:55.493727Z","iopub.execute_input":"2023-06-08T14:08:55.494099Z","iopub.status.idle":"2023-06-08T14:08:55.5454Z","shell.execute_reply.started":"2023-06-08T14:08:55.49407Z","shell.execute_reply":"2023-06-08T14:08:55.544177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom torchvision import transforms, datasets\nfrom torch.utils.data import DataLoader, random_split\nfrom PIL import Image\n\n# Assuming your images are stored in \"/kaggle/input/airbus-ship-detection/train_v2\" \n# and your labels are stored in \"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\"\n\nimage_dir = \"/kaggle/input/airbus-ship-detection/train_v2\"\nlabels_path = \"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\"\n\n# Load labels\nlabels_df = pd.read_csv(labels_path)\n\n# Define your own dataset\nclass AirbusShipDataset(datasets.ImageFolder):\n    def __init__(self, root_dir, labels_df, transform=None):\n        self.root_dir = root_dir\n        self.labels = labels_df\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.labels)\n\n#     def __getitem__(self, idx):\n#         img_name = os.path.join(self.root_dir, self.labels.iloc[idx, 0])\n#         image = Image.open(img_name).convert('RGB')\n#         label = self.labels.iloc[idx, 1]\n#         if self.transform:\n#             image = self.transform(image)\n#         return image, label\n    \n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir, self.labels.iloc[idx, 0])\n        image = Image.open(img_name).convert('RGB')\n        label = self.labels.iloc[idx, 1]\n        if label is not np.nan:  # Check for missing labels\n            label = rle_to_mask(label, image.height, image.width)\n        else:\n            label = np.zeros((image.height, image.width))\n        if self.transform:\n            image = self.transform(image)\n            label = self.transform(label)\n        return image, label\n    def rle_to_mask(rle_string, height, width):\n        rows, cols = height, width\n    \n        if rle_string == -1:\n            return np.zeros((height, width))\n        else:\n            rle_numbers = [int(num_string) for num_string in rle_string.split(' ')]\n            rle_pairs = np.array(rle_numbers).reshape(-1,2)\n            img = np.zeros(rows*cols, dtype=np.uint8)\n            for index, length in rle_pairs:\n                index -= 1\n                img[index:index+length] = 255\n            img = img.reshape(cols,rows)\n            img = img.T\n            return img\n\n\n# Define your transformations\ntransform = transforms.Compose([\n    transforms.Resize((80,80)),  # Resize images to 80x80\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # ImageNet normalization\n])\n\n# Initialize your dataset\ndataset = AirbusShipDataset(image_dir, labels_df, transform=transform)\n\n# Split your dataset into training and validation datasets\ntrain_size = int(0.8 * len(dataset))\nval_size = len(dataset) - train_size\ntrain_dataset, val_dataset = random_split(dataset, [train_size, val_size])\n\n# Initialize your data loaders\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n\n# Continue with your model, loss, optimizer, and training loop as before...\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:09:09.20349Z","iopub.execute_input":"2023-06-08T14:09:09.203903Z","iopub.status.idle":"2023-06-08T14:09:09.740734Z","shell.execute_reply.started":"2023-06-08T14:09:09.203871Z","shell.execute_reply":"2023-06-08T14:09:09.739713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv(labels_path)\nlabels_df","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:17:49.119158Z","iopub.execute_input":"2023-06-08T14:17:49.12205Z","iopub.status.idle":"2023-06-08T14:17:49.843454Z","shell.execute_reply.started":"2023-06-08T14:17:49.122009Z","shell.execute_reply":"2023-06-08T14:17:49.842377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"3","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom torchvision import transforms, datasets\nfrom torch.utils.data import DataLoader, random_split\nfrom PIL import Image\nimport torch\n\n# Assuming your images are stored in \"/kaggle/input/airbus-ship-detection/train_v2\" \n# and your labels are stored in \"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\"\n\nimage_dir = \"/kaggle/input/airbus-ship-detection/train_v2\"\nlabels_path = \"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\"\n\n# Load labels\nlabels_df = pd.read_csv(labels_path)\n\n# Convert labels to integers or floats\n# labels_df['EncodedPixels'] = labels_df['EncodedPixels'].apply(lambda x: int(x) if pd.notnull(x) else 0)\n\n# Define your own dataset\nclass AirbusShipDataset(Dataset):\n    def __init__(self, root_dir, labels_df, transform=None):\n        self.root_dir = root_dir\n        self.labels_df = labels_df\n        self.transform = transform\n        self.image_ids = labels_df['ImageId'].unique()\n\n    def __len__(self):\n        return len(self.image_ids)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir, self.image_ids[idx])\n        image = Image.open(img_name).convert('RGB')\n        if self.transform:\n            image = self.transform(image)\n\n        img_masks = self.labels_df.loc[self.labels_df['ImageId'] == self.image_ids[idx], 'EncodedPixels'].tolist()\n        mask = self.masks_as_image(img_masks)\n    \n        return image, mask  # Remove the unsqueeze(0) from mask\n\n    def masks_as_image(self, in_mask_list):\n        # Take the individual ship masks and create a single mask array for all ships\n        all_masks = np.zeros((768, 768))\n        for mask in in_mask_list:\n            if isinstance(mask, str):\n                all_masks += self.rle_decode(mask)\n        return all_masks\n\n    def rle_decode(self, mask_rle):\n        # Convert RLEs to mask arrays\n        shape = (768, 768)\n        s = mask_rle.split()\n        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n        starts -= 1\n        ends = starts + lengths\n        img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n        for lo, hi in zip(starts, ends):\n            img[lo:hi] = 1\n        return img.reshape(shape).T  # Needed to align to RLE direction\n\n\n# Define your transformations\ntransform = transforms.Compose([\n    transforms.Resize((80,80)),  # Resize images to 80x80\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # ImageNet normalization\n])\n\n# Initialize your dataset\ndataset = AirbusShipDataset(image_dir, labels_df, transform=transform)\n\n# Split your dataset into training and validation datasets\ntrain_size = int(0.8 * len(dataset))\nval_size = len(dataset) - train_size\ntrain_dataset, val_dataset = random_split(dataset, [train_size, val_size])\n\n# Initialize your data loaders\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:56:05.20005Z","iopub.execute_input":"2023-06-08T14:56:05.200544Z","iopub.status.idle":"2023-06-08T14:56:05.778087Z","shell.execute_reply.started":"2023-06-08T14:56:05.200504Z","shell.execute_reply":"2023-06-08T14:56:05.777038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"4","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom torchvision import transforms, datasets\nfrom torch.utils.data import DataLoader, random_split\nfrom PIL import Image\nimport torch\nfrom torch.utils.data import Dataset\nfrom torchvision import transforms\nfrom PIL import Image\n\nimage_dir = \"/kaggle/input/airbus-ship-detection/train_v2\"\nlabels_path = \"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\"\n\nlabels_df = pd.read_csv(labels_path)\n\nclass AirbusShipDataset(Dataset):\n    def __init__(self, root_dir, labels_df, transform=None):\n        self.root_dir = root_dir\n        self.labels_df = labels_df\n        self.transform = transform\n        self.image_ids = labels_df['ImageId'].unique()\n\n    def __len__(self):\n        return len(self.image_ids)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir, self.image_ids[idx])\n        image = Image.open(img_name).convert('RGB')\n        if self.transform:\n            image = self.transform(image)\n\n        img_masks = self.labels_df.loc[self.labels_df['ImageId'] == self.image_ids[idx], 'EncodedPixels'].tolist()\n        mask = self.masks_as_image(img_masks)\n        mask = mask.reshape(768, 768)\n\n        return image, mask\n\n\n    def masks_as_image(self, in_mask_list):\n        all_masks = np.zeros((768, 768))\n        for mask in in_mask_list:\n            if isinstance(mask, str):\n                all_masks += self.rle_decode(mask)\n        return all_masks.reshape(1, 768, 768).astype(np.float32)\n\n    def rle_decode(self, mask_rle):\n        shape = (768, 768)\n        s = mask_rle.split()\n        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n        starts -= 1\n        ends = starts + lengths\n        img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n        for lo, hi in zip(starts, ends):\n            img[lo:hi] = 1\n        return img.reshape(shape).T\n\ntransform = transforms.Compose([\n    transforms.Resize((80,80)),  # Resize images to 80x80\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # ImageNet normalization\n])\n\ndataset = AirbusShipDataset(image_dir, labels_df, transform=transform)\n\ntrain_size = int(0.8 * len(dataset))\nval_size = len(dataset) - train_size\ntrain_dataset, val_dataset = random_split(dataset, [train_size, val_size])\n\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T20:49:36.244974Z","iopub.execute_input":"2023-06-08T20:49:36.24537Z","iopub.status.idle":"2023-06-08T20:49:37.876086Z","shell.execute_reply.started":"2023-06-08T20:49:36.245338Z","shell.execute_reply":"2023-06-08T20:49:37.87515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(3, 80, 3, padding=1)\n        self.bn1 = nn.BatchNorm2d(80)\n        self.pool1 = nn.MaxPool2d(2, 2)\n        self.dropout1 = nn.Dropout(0.25)\n\n        self.conv2 = nn.Conv2d(80, 80, 3, padding=1)\n        self.bn2 = nn.BatchNorm2d(80)\n        self.pool2 = nn.MaxPool2d(2, 2)\n        self.dropout2 = nn.Dropout(0.25)\n\n        self.conv3 = nn.Conv2d(80, 80, 3, padding=1)\n        self.bn3 = nn.BatchNorm2d(80)\n        self.pool3 = nn.MaxPool2d(2, 2)\n        self.dropout3 = nn.Dropout(0.25)\n\n        self.conv4 = nn.Conv2d(80, 80, 3, padding=1)\n        self.bn4 = nn.BatchNorm2d(80)\n        self.pool4 = nn.MaxPool2d(2, 2)\n        self.dropout4 = nn.Dropout(0.25)\n\n        self.flatten = nn.Flatten()\n\n        self.fc1 = nn.Linear(80 * 5 * 5, 300)\n        self.fc2 = nn.Linear(300, 1)\n\n    def forward(self, x):\n        x = F.relu(self.bn1(self.conv1(x)))\n        x = self.pool1(x)\n        x = self.dropout1(x)\n\n        x = F.relu(self.bn2(self.conv2(x)))\n        x = self.pool2(x)\n        x = self.dropout2(x)\n\n        x = F.relu(self.bn3(self.conv3(x)))\n        x = self.pool3(x)\n        x = self.dropout3(x)\n\n        x = F.relu(self.bn4(self.conv4(x)))\n        x = self.pool4(x)\n        x = self.dropout4(x)\n\n        x = self.flatten(x)\n        x = self.fc1(x)\n        x = self.fc2(x)\n        return x.squeeze(1)\n\n\n\n#         x = F.softmax(self.fc3(x), dim=1)\n#         return x\n        \n\n\nmodel = Net()\nif torch.cuda.is_available():\n    model = model.cuda()\n    if torch.cuda.device_count() > 1:\n        model = nn.DataParallel(model)\n\n# criterion = nn.CrossEntropyLoss()\n# optimizer = optim.Adam(model.parameters())\ncriterion = nn.BCEWithLogitsLoss()  # Use Binary Cross Entropy With Logits Loss\noptimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)\nscheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)\n\n\nnum_epochs = 100\nearly_stop_patience = 10\nbest_val_loss = float('inf')\ncounter = 0\n\ntrain_losses = []\ntrain_accuracies = []\nval_losses = []\nval_accuracies = []\n\nfor epoch in range(num_epochs):\n    train_loss = 0.0\n    train_correct = 0\n    val_loss = 0.0\n    val_correct = 0\n\n    model.train()\n    for inputs, labels in train_loader:\n        inputs = inputs.cuda()\n        labels = labels.unsqueeze(1).float().cuda()  # Convert labels to float tensor\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n        predicted = (torch.sigmoid(outputs) > 0.5).float()  # Since we are dealing with binary classification, we use sigmoid and a threshold of 0.5\n        train_correct += (predicted == labels).sum().item()\n\n\n    model.eval()\n    with torch.no_grad():\n        for inputs, labels in test_loader:\n            inputs = inputs.cuda()\n            labels = labels.float().cuda()  # Convert labels to float tensor\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n\n            test_loss += loss.item()\n            predicted = (torch.sigmoid(outputs) > 0.5).float()\n            test_correct += (predicted == labels).sum().item()\n\n    train_losses.append(train_loss / len(train_loader))\n    train_accuracies.append(100. * train_correct / len(train_dataset))\n    val_losses.append(val_loss / len(val_loader))\n    val_accuracies.append(100. * val_correct / len(val_dataset))\n\n    print(f\"Epoch {epoch + 1}/{num_epochs}, Train Loss: {train_losses[-1]:.4f}, Train Acc: {train_accuracies[-1]:.2f}%, Val Loss: {val_losses[-1]:.4f}, Val Acc: {val_accuracies[-1]:.2f}%\")\n\n    if val_loss < best_val_loss:\n        best_val_loss = val_loss\n        torch.save(model.state_dict(), 'best_model.pth')\n        counter = 0\n    else:\n        counter += 1\n        if counter >= early_stop_patience:\n            print(\"Early stopping\")\n            break\n\n# Load the state of the best model\nmodel.load_state_dict(torch.load('best_model.pth'))\n\n# Evaluation mode\nmodel.eval()\n\n# Evaluate on the test set\ntest_loss = 0.0\ntest_correct = 0\n\nwith torch.no_grad():\n    for inputs, labels in test_loader:\n        inputs = inputs.cuda()\n        labels = torch.nn.functional.one_hot(labels, num_classes=2).float().cuda()  # Convert labels to one-hot encoded format\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n\n        test_loss += loss.item()\n        predicted = (torch.sigmoid(outputs) > 0.5).float()  # Since we are dealing with binary classification, we use sigmoid and a threshold of 0.5\n        test_correct += (predicted == labels).sum().item()\n\ntest_loss /= len(test_loader)\ntest_accuracy = 100 * test_correct / len(test_loader.dataset)\n\nprint(f\"Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%\")\n\nfor epoch in range(num_epochs):\n    train_loss = 0.0\n    train_correct = 0\n    val_loss = 0.0\n    val_correct = 0\n\n    model.train()\n    for inputs, labels in train_loader:\n        inputs = inputs.cuda()\n        labels = labels.float().cuda()   # Convert labels to float tensor\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n        predicted = (torch.sigmoid(outputs) > 0.5).float()  # Since we are dealing with binary classification, we use sigmoid and a threshold of 0.5\n        train_correct += (predicted == labels).sum().item()\n\n    model.eval()\n    with torch.no_grad():\n        for inputs, labels in val_loader:\n            inputs = inputs.cuda()\n            labels = labels.float().cuda()  # Convert labels to float tensor\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n\n            val_loss += loss.item()\n            predicted = (torch.sigmoid(outputs) > 0.5).float()\n            val_correct += (predicted == labels).sum().item()\n\n    train_losses.append(train_loss / len(train_loader))\n    train_accuracies.append(100. * train_correct / len(train_dataset))\n    val_losses.append(val_loss / len(val_loader))\n    val_accuracies.append(100. * val_correct / len(val_dataset))\n\n    print(f\"Epoch {epoch + 1}/{num_epochs}, Train Loss: {train_losses[-1]:.4f}, Train Acc: {train_accuracies[-1]:.2f}%, Val Loss: {val_losses[-1]:.4f}, Val Acc: {val_accuracies[-1]:.2f}%\")\n\n    if val_loss < best_val_loss:\n        best_val_loss = val_loss\n        torch.save(model.state_dict(), 'best_model.pth')\n        counter = 0\n    else:\n        counter += 1\n        if counter >= early_stop_patience:\n            print(\"Early stopping\")\n            break\n\n# Load the state of the best model\nmodel.load_state_dict(torch.load('best_model.pth'))\n\n# Evaluation mode\nmodel.eval()\n\n# Evaluate on the test set\ntest_loss = 0.0\ntest_correct = 0\n\nwith torch.no_grad():\n    for inputs, labels in test_loader:\n        inputs = inputs.cuda()\n        labels = labels.float().cuda()  # Convert labels to float tensor\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n\n        test_loss += loss.item()\n        predicted = (torch.sigmoid(outputs) > 0.5).float()\n        test_correct += (predicted == labels).sum().item()\n\ntest_loss /= len(test_loader)\ntest_accuracy = 100 * test_correct / len(test_loader.dataset)\n\nprint(f\"Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%\")\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T20:49:40.290458Z","iopub.execute_input":"2023-06-08T20:49:40.290818Z","iopub.status.idle":"2023-06-08T20:49:43.436227Z","shell.execute_reply.started":"2023-06-08T20:49:40.290787Z","shell.execute_reply":"2023-06-08T20:49:43.434699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AIRBUS3","metadata":{}},{"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T21:12:38.880618Z","iopub.execute_input":"2023-06-08T21:12:38.880993Z","iopub.status.idle":"2023-06-08T21:12:38.886455Z","shell.execute_reply.started":"2023-06-08T21:12:38.880963Z","shell.execute_reply":"2023-06-08T21:12:38.885357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\n\ndef is_image_corrupted(img_path):\n    try:\n        img = Image.open(img_path)\n        img.verify()  # verify that it is, in fact an image\n        return False\n    except (IOError, SyntaxError) as e:\n        return True\n\ncorrupted_images = [img for img in image_list if is_image_corrupted(img)]\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Imports\nimport os\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image, ImageFile\nfrom torch.utils.data import Dataset\nfrom torchvision import transforms\nfrom sklearn.model_selection import train_test_split\nimport torch\nfrom torch.utils.data import DataLoader\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\n\nImageFile.LOAD_TRUNCATED_IMAGES = True\nclass AirbusDataset(Dataset):\n    def __init__(self, img_dir, masks_df, transform=None):\n        self.img_dir = img_dir\n        self.masks_df = masks_df\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.masks_df)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.img_dir, self.masks_df.iloc[idx, 0])\n        image = Image.open(img_name)\n\n        encoded_pixels = self.masks_df.iloc[idx, 1]\n        label = torch.tensor(0 if pd.isna(encoded_pixels) else 1, dtype=torch.long)\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label\n\n\n# Data Preparation\ndata_transform = transforms.Compose([\n    transforms.Resize((128, 128)),\n    transforms.ToTensor(),\n])\nmasks_df = pd.read_csv('/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv')\ntrain_df, val_df = train_test_split(masks_df, test_size=0.2, random_state=42)\n\n# Initialize the datasets\ntrain_dataset = AirbusDataset(img_dir='/kaggle/input/airbus-ship-detection/train_v2',\n                              masks_df=train_df, \n                              transform=data_transform)\n\nval_dataset = AirbusDataset(img_dir='/kaggle/input/airbus-ship-detection/train_v2', \n                            masks_df=val_df, \n                            transform=data_transform)\n# Initialize the dataloaders\nbatch_size = 64\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)\n\n# Model Definition\nclass Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)\n        self.bn1 = nn.BatchNorm2d(64)\n        self.pool1 = nn.MaxPool2d(2, 2)\n        self.dropout1 = nn.Dropout(0.25)\n\n        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)\n        self.bn2 = nn.BatchNorm2d(64)\n        self.pool2 = nn.MaxPool2d(2, 2)\n        self.dropout2 = nn.Dropout(0.25)\n\n        self.conv3 = nn.Conv2d(64, 64, 3, padding=1)\n        self.bn3 = nn.BatchNorm2d(64)\n        self.pool3 = nn.MaxPool2d(2, 2)\n        self.dropout3 = nn.Dropout(0.25)\n\n        self.conv4 = nn.Conv2d(64, 64, 3, padding=1)\n        self.bn4 = nn.BatchNorm2d(64)\n        self.pool4 = nn.MaxPool2d(2, 2)\n        self.dropout4 = nn.Dropout(0.25)\n\n        self.flatten = nn.Flatten()\n\n        self.fc1 = nn.Linear(64 * 8 * 8, 200)\n        self.fc2 = nn.Linear(200, 150)\n        self.fc3 = nn.Linear(150, 2)\n\n    def forward(self, x):\n        x = F.relu(self.bn1(self.conv1(x)))\n        x = self.pool1(x)\n        x = self.dropout1(x)\n\n        x = F.relu(self.bn2(self.conv2(x)))\n        x = self.pool2(x)\n        x = self.dropout2(x)\n\n        x = F.relu(self.bn3(self.conv3(x)))\n        x = self.pool3(x)\n        x = self.dropout3(x)\n\n        x = F.relu(self.bn4(self.conv4(x)))\n        x = self.pool4(x)\n        x = self.dropout4(x)\n\n        x = self.flatten(x)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.softmax(self.fc3(x), dim=1)\n        return x\n\n# Training Loop\nmodel = Net()\nif torch.cuda.is_available():\n    model = model.cuda()\n    if torch.cuda.device_count() > 1:\n        model = nn.DataParallel(model)\n        \ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters())\n\nnum_epochs = 100\nearly_stop_patience = 10\nbest_val_loss = float('inf')\ncounter = 0\n\ntrain_losses = []\ntrain_accuracies = []\nval_losses = []\nval_accuracies = []\n\nfor epoch in range(num_epochs):\n    train_loss = 0\n    train_correct = 0\n    val_loss = 0\n    val_correct = 0\n\n    model.train()\n    for i, (inputs, labels) in enumerate(train_loader):\n        inputs = inputs.cuda()\n        labels = labels.cuda()  \n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n        _, predicted = torch.max(outputs, 1)\n        train_correct += (predicted == labels).sum().item()\n\n    model.eval()\n    with torch.no_grad():\n        for inputs, labels in val_loader:\n            inputs = inputs.cuda()\n            labels = labels.cuda()\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n\n            val_loss += loss.item()\n            _, predicted = torch.max(outputs, 1)\n            val_correct += (predicted == labels).sum().item()\n\n    train_losses.append(train_loss / len(train_loader))\n    train_accuracies.append(100 * train_correct / len(train_loader.dataset))\n    val_losses.append(val_loss / len(val_loader))\n    val_accuracies.append(100 * val_correct / len(val_loader.dataset))\n\n    print(f\"Epoch {epoch + 1}/{num_epochs}, Train Loss: {train_losses[-1]:.4f}, Train Acc: {train_accuracies[-1]:.2f}%, Val Loss: {val_losses[-1]:.4f}, Val Acc: {val_accuracies[-1]:.2f}%\")\n\n    if val_loss < best_val_loss:\n        best_val_loss = val_loss\n        torch.save(model.state_dict(), 'best_model.pth')\n        counter = 0\n    else:\n        counter += 1\n        if counter >= early_stop_patience:\n            print(f\"Early stopping after {counter} epochs with no improvement.\")\n            break\n\nmodel.load_state_dict(torch.load('best_model.pth'))\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T22:12:11.134586Z","iopub.execute_input":"2023-06-08T22:12:11.134937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SAM","metadata":{}},{"cell_type":"code","source":"def show_anns(anns, axes=None):\n    if len(anns) == 0:\n        return\n    if axes:\n        ax = axes\n    else:\n        ax = plt.gca()\n        ax.set_autoscale_on(False)\n    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n    polygons = []\n    color = []\n    for ann in sorted_anns:\n        m = ann['segmentation']\n        img = np.ones((m.shape[0], m.shape[1], 3))\n        color_mask = np.random.random((1, 3)).tolist()[0]\n        for i in range(3):\n            img[:,:,i] = color_mask[i]\n        ax.imshow(np.dstack((img, m*0.5)))\n\ndef show_mask(mask, ax, random_color=False):\n    if random_color:\n        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)\n    else:\n        color = np.array([30/255, 144/255, 255/255, 0.6])\n    h, w = mask.shape[-2:]\n    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)\n    ax.imshow(mask_image)\n\n    \ndef show_points(coords, labels, ax, marker_size=375):\n    pos_points = coords[labels==1]\n    neg_points = coords[labels==0]\n    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)   \n\n    \ndef show_box(box, ax):\n    x0, y0 = box[0], box[1]\n    w, h = box[2] - box[0], box[3] - box[1]\n    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))  ","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:52:09.002328Z","iopub.execute_input":"2023-06-11T00:52:09.002686Z","iopub.status.idle":"2023-06-11T00:52:09.016898Z","shell.execute_reply.started":"2023-06-11T00:52:09.002653Z","shell.execute_reply":"2023-06-11T00:52:09.016007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfrom matplotlib import pyplot as plt\nimport torch\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:52:09.521953Z","iopub.execute_input":"2023-06-11T00:52:09.522417Z","iopub.status.idle":"2023-06-11T00:52:09.527695Z","shell.execute_reply.started":"2023-06-11T00:52:09.522382Z","shell.execute_reply":"2023-06-11T00:52:09.526682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor\n\n\nsam_checkpoint = \"/kaggle/input/segment-anything/pytorch/vit-b/1/model.pth\"\nmodel_type = \"vit_b\"\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')  # determine the compute device\n\nsam = sam_model_registry[model_type](checkpoint=sam_checkpoint)\nsam.to(device=device)\n\nmask_generator = SamAutomaticMaskGenerator(sam, points_per_batch=16)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:52:12.918594Z","iopub.execute_input":"2023-06-11T00:52:12.918945Z","iopub.status.idle":"2023-06-11T00:52:19.266355Z","shell.execute_reply.started":"2023-06-11T00:52:12.918916Z","shell.execute_reply":"2023-06-11T00:52:19.265362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\nmasks = mask_generator.generate(image_array)\n\n_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image_array)\nshow_anns(masks, axes[1])\naxes[2].imshow(image_array)\nshow_anns(masks, axes[2])","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:10:09.594409Z","iopub.execute_input":"2023-06-10T16:10:09.595085Z","iopub.status.idle":"2023-06-10T16:18:41.585698Z","shell.execute_reply.started":"2023-06-10T16:10:09.595012Z","shell.execute_reply":"2023-06-10T16:18:41.583796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\npredictor.set_image(image_array)\n\ninput_point = np.array([[120, 135]])\ninput_label = np.array([1])\n\nplt.imshow(image_array)\nshow_points(input_point, input_label, plt.gca())\nplt.axis('on')\nplt.show()  \n\n\n\nmasks, scores, logits = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    multimask_output=True,\n)\n\nfor i, (mask, score) in enumerate(zip(masks, scores)):\n#     plt.figure(figsize=(10,10))\n    plt.imshow(image_array)\n    show_mask(mask, plt.gca())\n    show_points(input_point, input_label, plt.gca())\n    plt.title(f\"Mask {i+1}, Score: {score:.3f}\", fontsize=18)\n    plt.show() ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SAM2","metadata":{}},{"cell_type":"code","source":"import torch\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef scanmap(image_path, model, device):\n    satellite_image = cv2.imread(image_path)\n    satellite_image = satellite_image.astype(np.float16) / 255.0\n\n    window_size = (80, 80)\n    stride = 10\n\n    height, width, channels = satellite_image.shape\n\n    probabilities_map = []\n    ship_coordinates = []  # list to keep ship points\n\n    model.to(device)  # ensure model is on correct device\n\n    for y in range(0, height - window_size[1] + 1, stride):\n        row_probabilities = []\n        for x in range(0, width - window_size[0] + 1, stride):\n            cropped_window = satellite_image[y:y + window_size[1], x:x + window_size[0]]\n            cropped_window_torch = torch.tensor(cropped_window.transpose(2, 0, 1), dtype=torch.float32).unsqueeze(0)\n\n            cropped_window_torch = cropped_window_torch.to(device)  # move data to the same device as model\n\n            with torch.no_grad():\n                probabilities = model(cropped_window_torch)\n\n            probability = probabilities[0, 1].item()\n            row_probabilities.append(probability)\n\n            if probability >= 0.6:  # check if the point is a ship\n                ship_coordinates.append((x, y))  # save coordinates as a tuple\n    \n        probabilities_map.append(row_probabilities)\n\n    probabilities_map = np.array(probabilities_map)\n\n    plt.imshow(probabilities_map, cmap='hot', interpolation='nearest')\n    plt.colorbar()\n    plt.show()\n\n    return ship_coordinates  # return the list of ship coordinates\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')  # determine the compute device\n\n# make sure the model variable is defined and contains the trained model\nship_coords = scanmap(\"/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png\", model, device)\n\n# now you can use ship_coords in your segmentation model\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:50:38.246891Z","iopub.execute_input":"2023-06-11T00:50:38.247748Z","iopub.status.idle":"2023-06-11T00:51:36.25684Z","shell.execute_reply.started":"2023-06-11T00:50:38.247714Z","shell.execute_reply":"2023-06-11T00:51:36.255918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from segment_anything import sam_model_registry, SamPredictor\n\npredictor = SamPredictor(sam)\n\nimage_path = '/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\npredictor.set_image(image_array)\n\n\n# Use ship_coords obtained from the heatmap model\ninput_point = np.array(ship_coords)\ninput_label = np.ones(len(ship_coords))  # assuming all points are for the ship class\n\nplt.imshow(image_array)\nshow_points(input_point, input_label, plt.gca())\nplt.axis('on')\nplt.show()\n\nmasks, scores, logits = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    multimask_output=True,\n)\n\nfor i, (mask, score) in enumerate(zip(masks, scores)):\n    plt.imshow(image_array)\n    show_mask(mask, plt.gca())\n    show_points(input_point, input_label, plt.gca())\n    plt.title(f\"Mask {i+1}, Score: {score:.3f}\", fontsize=18)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T00:53:26.12801Z","iopub.execute_input":"2023-06-11T00:53:26.128743Z","iopub.status.idle":"2023-06-11T00:53:33.469051Z","shell.execute_reply.started":"2023-06-11T00:53:26.12871Z","shell.execute_reply":"2023-06-11T00:53:33.46801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bgw=np.ones(image_array.shape)*255\nprint(len(masks))\nprint(masks[0].keys())","metadata":{"execution":{"iopub.status.busy":"2023-06-11T01:01:36.594328Z","iopub.execute_input":"2023-06-11T01:01:36.594797Z","iopub.status.idle":"2023-06-11T01:01:36.639927Z","shell.execute_reply.started":"2023-06-11T01:01:36.594765Z","shell.execute_reply":"2023-06-11T01:01:36.638661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#slices of mask image\nfor i in range(len(masks)):\n    plt.figure(figsize=(3,3))\n    plt.imshow(bgw)\n    show_anns([masks[i]])\n    plt.title(f'mask{i}')\n    plt.axis('off')  \n    plt.show()    ","metadata":{"execution":{"iopub.status.busy":"2023-06-11T01:01:40.465498Z","iopub.execute_input":"2023-06-11T01:01:40.465879Z","iopub.status.idle":"2023-06-11T01:01:42.411924Z","shell.execute_reply.started":"2023-06-11T01:01:40.465847Z","shell.execute_reply":"2023-06-11T01:01:42.410399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorization","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}