{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"nvidiaTeslaT4","dataSources":[{"sourceId":8899,"databundleVersionId":46091,"sourceType":"competition"},{"sourceId":7904115,"sourceType":"datasetVersion","datasetId":4642534}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom zipfile import ZipFile\nimport pandas as pd\nimport torchdata\nimport torch\n\nfrom skimage.io import imread\nfrom PIL import ImageColor, Image\ndevice = 'cuda' if torch.cuda.is_available else 'cpu'\ndevice","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:13.185804Z","iopub.execute_input":"2024-03-22T08:09:13.186197Z","iopub.status.idle":"2024-03-22T08:09:21.448733Z","shell.execute_reply.started":"2024-03-22T08:09:13.186164Z","shell.execute_reply":"2024-03-22T08:09:21.447513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.listdir('/kaggle/input/supdata/')","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:21.450965Z","iopub.execute_input":"2024-03-22T08:09:21.451914Z","iopub.status.idle":"2024-03-22T08:09:21.463146Z","shell.execute_reply.started":"2024-03-22T08:09:21.451815Z","shell.execute_reply":"2024-03-22T08:09:21.461078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_color_path = '/kaggle/input/cvpr-2018-autonomous-driving/train_color.zip'\ntrain_label_path = '/kaggle/input/cvpr-2018-autonomous-driving/train_label.zip'\ndataset_description_path = '/kaggle/input/supdata/train_info.csv'\nsample_submission_path = '/kaggle/input/supdata/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:21.464569Z","iopub.execute_input":"2024-03-22T08:09:21.464839Z","iopub.status.idle":"2024-03-22T08:09:21.469503Z","shell.execute_reply.started":"2024-03-22T08:09:21.464814Z","shell.execute_reply":"2024-03-22T08:09:21.468302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_ids_pairs = { 'car': 33,\n'motorbicycle': 34,\n'bicycle': 35,\n'person': 36,\n'rider': 37,\n'truck': 38,\n'bus': 39,\n'tricycle': 40,\n'others': 0,\n'rover': 1,\n'sky': 17,\n'car_groups': 161,\n'motorbicycle_group': 162,\n'bicycle_group': 163,\n'person_group': 164,\n'rider_group': 165,\n'truck_group': 166,\n'bus_group': 167,\n'tricycle_group': 168,\n'road': 49,\n'siderwalk': 50,\n'traffic_cone': 65,\n'road_pile': 66,\n'fence': 67,\n'traffic_light': 81,\n'pole': 82,\n'traffic_sign': 83,\n'wall': 84,\n'dustbin': 85,\n'billboard': 86,\n'building': 97,\n'bridge': 98,\n'tunnel': 99,\n'overpass': 100,\n'vegatation': 113,\n# 'unlabeled': 255\n}\n\nids_classes_pairs = {classes_ids_pairs[j]: j for j in classes_ids_pairs}\nlen(ids_classes_pairs)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:21.472479Z","iopub.execute_input":"2024-03-22T08:09:21.472943Z","iopub.status.idle":"2024-03-22T08:09:21.48577Z","shell.execute_reply.started":"2024-03-22T08:09:21.472899Z","shell.execute_reply":"2024-03-22T08:09:21.48486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# show of","metadata":{}},{"cell_type":"code","source":"with ZipFile(train_color_path) as myzip:\n    with myzip.open('train_color/' + '170927_064004845_Camera_6.jpg') as myfile:\n        image = mpimg.imread(myfile)\n        plt.imshow(image[1200:, :, :])\n        ","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:21.487015Z","iopub.execute_input":"2024-03-22T08:09:21.487714Z","iopub.status.idle":"2024-03-22T08:09:23.467253Z","shell.execute_reply.started":"2024-03-22T08:09:21.487688Z","shell.execute_reply":"2024-03-22T08:09:23.466279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile(train_label_path) as myzip:\n    with myzip.open('train_label/' + '170927_064004845_Camera_6_instanceIds.png') as myfile:\n        image = mpimg.imread(myfile)\n        img = imread(myfile)\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:23.468667Z","iopub.execute_input":"2024-03-22T08:09:23.468979Z","iopub.status.idle":"2024-03-22T08:09:25.593309Z","shell.execute_reply.started":"2024-03-22T08:09:23.46895Z","shell.execute_reply":"2024-03-22T08:09:25.592295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# create data loader","metadata":{}},{"cell_type":"code","source":"from torchvision.transforms.functional import pil_to_tensor","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:25.594383Z","iopub.execute_input":"2024-03-22T08:09:25.594673Z","iopub.status.idle":"2024-03-22T08:09:28.38217Z","shell.execute_reply.started":"2024-03-22T08:09:25.594647Z","shell.execute_reply":"2024-03-22T08:09:28.3813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_images = ['170908_084900808_Camera_6']","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.383388Z","iopub.execute_input":"2024-03-22T08:09:28.385347Z","iopub.status.idle":"2024-03-22T08:09:28.389651Z","shell.execute_reply.started":"2024-03-22T08:09:28.385317Z","shell.execute_reply":"2024-03-22T08:09:28.388684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.391314Z","iopub.execute_input":"2024-03-22T08:09:28.39185Z","iopub.status.idle":"2024-03-22T08:09:28.401286Z","shell.execute_reply.started":"2024-03-22T08:09:28.391812Z","shell.execute_reply":"2024-03-22T08:09:28.400401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numpy.ndarray([25, 25, 3])[5:-5, 5:-5, :].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.404292Z","iopub.execute_input":"2024-03-22T08:09:28.404602Z","iopub.status.idle":"2024-03-22T08:09:28.413378Z","shell.execute_reply.started":"2024-03-22T08:09:28.404574Z","shell.execute_reply":"2024-03-22T08:09:28.412396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_simple_data_pipe(date_pipe: torchdata.datapipes.iter.IterDataPipe, f=lambda a: a, n=10) -> None:\n  print(type(date_pipe))\n  x = 0\n  for sample in date_pipe:\n    print(f(sample))\n    if x == n:\n      break\n    x +=1\n\n\ndef loadSegmentImage(img_name):\n  with ZipFile(train_label_path) as myzip:\n    with myzip.open('train_label/' + f'{img_name}_instanceIds.png') as myfile:\n        image = imread(myfile).astype(int)\n  return torch.from_numpy(image[1200:-20, 20:-20])\n\n\ndef loadSourceImage(img_name):\n  with ZipFile(train_color_path) as myzip:\n    with myzip.open('train_color/' + f'{img_name}.jpg') as myfile:\n        image = Image.open(myfile)\n        image = pil_to_tensor(image)\n  return image[:, 1200:-20, 20:-20]\n\n\ndef reshapeLabeled(img):\n  # l = [(img == 255).unsqueeze(0).clone()]\n  l = []\n  # k = torch.unique(img)\n  n = img // 1000 \n  for i in ids_classes_pairs:\n    t = n == i\n    l.append(t.unsqueeze(0).clone())\n  return torch.cat(l, 0)\n\n\ndef loadImagePair(row):\n  try:\n    return loadSourceImage(row[4]).unsqueeze(0), reshapeLabeled(loadSegmentImage(row[4])).unsqueeze(0), row[4]\n  except:\n    bad_images.append(row[4])\n    return None, None\n","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.414655Z","iopub.execute_input":"2024-03-22T08:09:28.415037Z","iopub.status.idle":"2024-03-22T08:09:28.428192Z","shell.execute_reply.started":"2024-03-22T08:09:28.415004Z","shell.execute_reply":"2024-03-22T08:09:28.427162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_description = pd.read_csv(dataset_description_path)\nprint(dataset_description.shape)\ndataset_description.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.429397Z","iopub.execute_input":"2024-03-22T08:09:28.429728Z","iopub.status.idle":"2024-03-22T08:09:28.624074Z","shell.execute_reply.started":"2024-03-22T08:09:28.429702Z","shell.execute_reply":"2024-03-22T08:09:28.623078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.6256Z","iopub.execute_input":"2024-03-22T08:09:28.626084Z","iopub.status.idle":"2024-03-22T08:09:28.630949Z","shell.execute_reply.started":"2024-03-22T08:09:28.626045Z","shell.execute_reply":"2024-03-22T08:09:28.62999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 1","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.632162Z","iopub.execute_input":"2024-03-22T08:09:28.632434Z","iopub.status.idle":"2024-03-22T08:09:28.64122Z","shell.execute_reply.started":"2024-03-22T08:09:28.632408Z","shell.execute_reply":"2024-03-22T08:09:28.640372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_res = torchdata.datapipes.iter.IterableWrapper([dataset_description_path])\noutput_res_pipe = torchdata.datapipes.iter.FileOpener(output_res, mode='r', encoding='utf-8', length=dataset_description.shape[0])\nres_pipe = output_res_pipe.parse_csv(skip_lines=1, delimiter=',')\nres_pipe = torchdata.datapipes.map.SequenceWrapper(list(res_pipe))\nready = res_pipe.map(loadImagePair)\n# ready = ready.map(prepearImage)\ntrain_dataloader = torchdata.datapipes.map.Shuffler(ready).batch(batch_size=batch_size).map(lambda a: a)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:09:28.642283Z","iopub.execute_input":"2024-03-22T08:09:28.642631Z","iopub.status.idle":"2024-03-22T08:09:29.970917Z","shell.execute_reply.started":"2024-03-22T08:09:28.642598Z","shell.execute_reply":"2024-03-22T08:09:29.970056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def f(a):\n    r = []\n    for i in a:\n        if i[0] != None:\n            r.append(str([sys.getsizeof(i[0]), sys.getsizeof(i[1]), sys.getsizeof(i), i[0].shape, i[1].shape]))\n        else:\n            r.append('err')\n    return '\\n'.join(r)\n\n\nprint_simple_data_pipe(train_dataloader, f=f, n=2)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:14:42.149817Z","iopub.execute_input":"2024-03-22T08:14:42.150567Z","iopub.status.idle":"2024-03-22T08:14:47.551666Z","shell.execute_reply.started":"2024-03-22T08:14:42.150535Z","shell.execute_reply":"2024-03-22T08:14:47.550578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resnet prep","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:18:07.356519Z","iopub.execute_input":"2024-03-22T08:18:07.356879Z","iopub.status.idle":"2024-03-22T08:18:07.361525Z","shell.execute_reply.started":"2024-03-22T08:18:07.356849Z","shell.execute_reply":"2024-03-22T08:18:07.360233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models.segmentation import fcn_resnet50, FCN_ResNet50_Weights\nfrom torchvision.models.detection import maskrcnn_resnet50_fpn, MaskRCNN_ResNet50_FPN_Weights\n","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:14:55.162043Z","iopub.execute_input":"2024-03-22T08:14:55.163242Z","iopub.status.idle":"2024-03-22T08:14:55.167657Z","shell.execute_reply.started":"2024-03-22T08:14:55.163202Z","shell.execute_reply":"2024-03-22T08:14:55.166646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn_resnet = fcn_resnet50(weights=FCN_ResNet50_Weights.COCO_WITH_VOC_LABELS_V1)\nfor i in fcn_resnet.parameters():\n    i.requires_grad = False\nmaskrcnn_resnet = maskrcnn_resnet50_fpn(weights=MaskRCNN_ResNet50_FPN_Weights.COCO_V1)\nimg_preprocessor = FCN_ResNet50_Weights.COCO_WITH_VOC_LABELS_V1.transforms(resize_size=None)\nmaskrcnn_resnet.eval()\nfcn_resnet.eval()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:16:57.539876Z","iopub.execute_input":"2024-03-22T08:16:57.540644Z","iopub.status.idle":"2024-03-22T08:17:01.510434Z","shell.execute_reply.started":"2024-03-22T08:16:57.540611Z","shell.execute_reply":"2024-03-22T08:17:01.509338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn_resnet.classifier[4] = nn.Conv2d(512, len(ids_classes_pairs), kernel_size=(1, 1), stride=(1, 1))\nfor i in fcn_resnet.classifier.parameters():\n    i.requires_grad = True","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:18:48.322099Z","iopub.execute_input":"2024-03-22T08:18:48.322948Z","iopub.status.idle":"2024-03-22T08:18:48.330104Z","shell.execute_reply.started":"2024-03-22T08:18:48.322911Z","shell.execute_reply":"2024-03-22T08:18:48.329183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in fcn_resnet.backbone.layer4[2].parameters():\n    i.requires_grad = True","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:18:53.635984Z","iopub.execute_input":"2024-03-22T08:18:53.636651Z","iopub.status.idle":"2024-03-22T08:18:53.641216Z","shell.execute_reply.started":"2024-03-22T08:18:53.636616Z","shell.execute_reply":"2024-03-22T08:18:53.640096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for test_images in train_dataloader:\n    break\ninput_test_data_x = torch.cat([i[0] for i in test_images if i[0] != None], 0)\ninput_test_data_y = torch.cat([i[1] for i in test_images if i[0] != None], 0)\ntest_source = test_images[0][2]\ninput_test_data_x.shape, input_test_data_y.shape, test_source","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:18:58.635661Z","iopub.execute_input":"2024-03-22T08:18:58.636245Z","iopub.status.idle":"2024-03-22T08:19:00.566645Z","shell.execute_reply.started":"2024-03-22T08:18:58.636214Z","shell.execute_reply":"2024-03-22T08:19:00.56543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmented_img = fcn_resnet(img_preprocessor(input_test_data_x))","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:19:06.951925Z","iopub.execute_input":"2024-03-22T08:19:06.952278Z","iopub.status.idle":"2024-03-22T08:19:55.426138Z","shell.execute_reply.started":"2024-03-22T08:19:06.952248Z","shell.execute_reply":"2024-03-22T08:19:55.424988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile(train_label_path) as myzip:\n    with myzip.open('train_label/' + f'{test_source}_instanceIds.png') as myfile:\n        image = imread(myfile)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:20:41.156601Z","iopub.execute_input":"2024-03-22T08:20:41.156987Z","iopub.status.idle":"2024-03-22T08:20:42.72224Z","shell.execute_reply.started":"2024-03-22T08:20:41.156956Z","shell.execute_reply":"2024-03-22T08:20:42.721229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmented_img['out'].shape, input_test_data_y.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:20:57.297717Z","iopub.execute_input":"2024-03-22T08:20:57.298474Z","iopub.status.idle":"2024-03-22T08:20:57.305045Z","shell.execute_reply.started":"2024-03-22T08:20:57.298429Z","shell.execute_reply":"2024-03-22T08:20:57.303982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(len(classes_ids_pairs), 1, figsize=(10, 100))\nfor i in range(len(classes_ids_pairs)):\n    axes[i].imshow(input_test_data_y.squeeze()[i].to('cpu').detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:21:01.838698Z","iopub.execute_input":"2024-03-22T08:21:01.839316Z","iopub.status.idle":"2024-03-22T08:21:22.003011Z","shell.execute_reply.started":"2024-03-22T08:21:01.839282Z","shell.execute_reply":"2024-03-22T08:21:22.001927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train resnet50","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdmfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-03-22T02:25:34.119557Z","iopub.execute_input":"2024-03-22T02:25:34.119867Z","iopub.status.idle":"2024-03-22T02:25:34.127764Z","shell.execute_reply.started":"2024-03-22T02:25:34.119837Z","shell.execute_reply":"2024-03-22T02:25:34.126948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# device = 'cpu'","metadata":{"execution":{"iopub.status.busy":"2024-03-22T02:25:34.128654Z","iopub.execute_input":"2024-03-22T02:25:34.128919Z","iopub.status.idle":"2024-03-22T02:25:34.137996Z","shell.execute_reply.started":"2024-03-22T02:25:34.128897Z","shell.execute_reply":"2024-03-22T02:25:34.137142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_episodes = 100\nn_steps = 20\nloss = torch.nn.MSELoss()\noptimizer = torch.optim.Adam(fcn_resnet.parameters(), lr=0.0000001)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T02:25:34.139209Z","iopub.execute_input":"2024-03-22T02:25:34.139464Z","iopub.status.idle":"2024-03-22T02:25:34.149078Z","shell.execute_reply.started":"2024-03-22T02:25:34.139442Z","shell.execute_reply":"2024-03-22T02:25:34.14825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn_resnet.to(device)\nprint()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T02:25:34.150173Z","iopub.execute_input":"2024-03-22T02:25:34.150429Z","iopub.status.idle":"2024-03-22T02:25:34.364668Z","shell.execute_reply.started":"2024-03-22T02:25:34.150407Z","shell.execute_reply":"2024-03-22T02:25:34.363497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lm = []","metadata":{"execution":{"iopub.status.busy":"2024-03-22T02:25:34.365805Z","iopub.execute_input":"2024-03-22T02:25:34.366062Z","iopub.status.idle":"2024-03-22T02:25:34.369938Z","shell.execute_reply.started":"2024-03-22T02:25:34.36604Z","shell.execute_reply":"2024-03-22T02:25:34.369017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in tqdm(range(n_episodes)):\n    e = n_steps\n    for data in train_dataloader:\n        x_t, y_t = [], []\n        none_flags = 0\n        for d in data:\n            if d[0] != None:\n                x_t.append(d[0])\n                y_t.append(d[1])\n            else: \n                none_flags += 1\n        if none_flags < batch_size:\n            x_t = torch.cat(x_t)\n            y_t = torch.cat(y_t)\n            optimizer.zero_grad()\n            prediction = fcn_resnet(img_preprocessor(x_t.to(device)))['out']\n            normalized_masks = prediction.softmax(dim=1)\n            rs_p = normalized_masks.reshape(batch_size - none_flags, -1).float().to(device)\n            rs_t = y_t.reshape(batch_size-none_flags, -1).to(device)\n            l = loss(rs_p.float(), rs_t.float())\n            l.backward()\n            lm.append(l.item())\n            e -= 1\n            optimizer.step()\n            if e < 0:\n                print('err: ', l.item())\n                break\n        else:\n            print(f'bad_epoch: {len(bad_images)}')\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-22T02:25:34.371346Z","iopub.execute_input":"2024-03-22T02:25:34.371609Z","iopub.status.idle":"2024-03-22T04:57:45.250633Z","shell.execute_reply.started":"2024-03-22T02:25:34.371586Z","shell.execute_reply":"2024-03-22T04:57:45.249707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom torchvision.transforms.functional import to_pil_image","metadata":{"execution":{"iopub.status.busy":"2024-03-22T04:57:45.252066Z","iopub.execute_input":"2024-03-22T04:57:45.252386Z","iopub.status.idle":"2024-03-22T04:57:45.256617Z","shell.execute_reply.started":"2024-03-22T04:57:45.252361Z","shell.execute_reply":"2024-03-22T04:57:45.255698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(lm)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T04:57:45.257791Z","iopub.execute_input":"2024-03-22T04:57:45.258082Z","iopub.status.idle":"2024-03-22T04:57:45.521767Z","shell.execute_reply.started":"2024-03-22T04:57:45.258059Z","shell.execute_reply":"2024-03-22T04:57:45.520875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(lm)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T04:57:45.522989Z","iopub.execute_input":"2024-03-22T04:57:45.523548Z","iopub.status.idle":"2024-03-22T04:57:45.764283Z","shell.execute_reply.started":"2024-03-22T04:57:45.523521Z","shell.execute_reply":"2024-03-22T04:57:45.763462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_pil_image(x_t[0])","metadata":{"execution":{"iopub.status.busy":"2024-03-22T04:57:45.765265Z","iopub.execute_input":"2024-03-22T04:57:45.765511Z","iopub.status.idle":"2024-03-22T04:57:49.243667Z","shell.execute_reply.started":"2024-03-22T04:57:45.765489Z","shell.execute_reply":"2024-03-22T04:57:49.242101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(len(classes_ids_pairs), 1, figsize=(10, 100))\nfor i in range(len(classes_ids_pairs)):\n    axes[i].imshow(input_test_data_y.squeeze()[i].to('cpu').detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2024-03-22T04:57:49.245724Z","iopub.execute_input":"2024-03-22T04:57:49.246308Z","iopub.status.idle":"2024-03-22T04:58:15.924476Z","shell.execute_reply.started":"2024-03-22T04:57:49.246255Z","shell.execute_reply":"2024-03-22T04:58:15.923602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(len(classes_ids_pairs), 1, figsize=(35, 350))\nfor i in range(len(classes_ids_pairs)):\n    axes[i].imshow(normalized_masks.squeeze()[i].to('cpu').detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2024-03-22T04:58:15.925769Z","iopub.execute_input":"2024-03-22T04:58:15.926068Z","iopub.status.idle":"2024-03-22T04:58:56.747457Z","shell.execute_reply.started":"2024-03-22T04:58:15.926043Z","shell.execute_reply":"2024-03-22T04:58:56.745743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# resnet 2","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-03-22T06:12:44.721733Z","iopub.execute_input":"2024-03-22T06:12:44.722105Z","iopub.status.idle":"2024-03-22T06:12:44.726447Z","shell.execute_reply.started":"2024-03-22T06:12:44.722074Z","shell.execute_reply":"2024-03-22T06:12:44.725619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_episodes = 100\nn_steps = 20\nloss = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(fcn_resnet.parameters(), lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T06:12:44.727798Z","iopub.execute_input":"2024-03-22T06:12:44.728054Z","iopub.status.idle":"2024-03-22T06:12:44.741109Z","shell.execute_reply.started":"2024-03-22T06:12:44.728032Z","shell.execute_reply":"2024-03-22T06:12:44.740278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn_resnet.to(device)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lm = []","metadata":{"execution":{"iopub.status.busy":"2024-03-22T06:12:44.981627Z","iopub.execute_input":"2024-03-22T06:12:44.981894Z","iopub.status.idle":"2024-03-22T06:12:44.985675Z","shell.execute_reply.started":"2024-03-22T06:12:44.981871Z","shell.execute_reply":"2024-03-22T06:12:44.984786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"tensor([[[1, 0, 0, 1, 0],\n         [1, 0, 1, 0, 0],\n         [1, 1, 2, 0, 0],\n         [2, 0, 2, 0, 2],\n         [0, 0, 2, 2, 3],\n         [0, 0, 3, 0, 3],\n         [0, 3, 0, 0, 3],\n         [3, 4, 0, 0, 4],\n         [0, 4, 0, 4, 0],\n         [0, 4, 4, 5, 0],\n         [0, 5, 0, 5, 0],\n         [5, 0, 0, 5, 5]]])\n         \n         \n         \n       ","metadata":{}},{"cell_type":"code","source":"for _ in tqdm(range(n_episodes)):\n    e = n_steps\n    for data in train_dataloader:\n        x_t, y_t = [], []\n        none_flags = 0\n        for d in data:\n            if d[0] != None:\n                x_t.append(d[0])\n                y_t.append(d[1])\n            else: \n                none_flags += 1\n        if none_flags < batch_size:\n            x_t = torch.cat(x_t)\n            y_t = torch.cat(y_t)\n            optimizer.zero_grad()\n            prediction = fcn_resnet(img_preprocessor(x_t.to(device)))['out']\n            normalized_masks = prediction.softmax(dim=1)\n            rs_p = normalized_masks.transpose(1, 3).reshape(batch_size - none_flags, x_t.shape[2] * x_t.shape[3] , -1).float().to(device)\n            rs_t = y_t.reshape(batch_size - none_flags, len(ids_classes_pairs), -1).to(device)\n            l = loss(rs_p.float(), rs_t.float())\n            l.backward()\n            lm.append(l.item())\n            e -= 1\n            optimizer.step()\n            if e < 0:\n                print('err: ', l.item())\n                break\n        else:\n            print(f'bad_epoch: {len(bad_images)}')\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-22T06:12:44.986714Z","iopub.execute_input":"2024-03-22T06:12:44.987029Z","iopub.status.idle":"2024-03-22T06:12:49.17409Z","shell.execute_reply.started":"2024-03-22T06:12:44.987006Z","shell.execute_reply":"2024-03-22T06:12:49.172725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_pil_image(x_t[0])","metadata":{"execution":{"iopub.status.busy":"2024-03-22T05:43:12.920726Z","iopub.execute_input":"2024-03-22T05:43:12.92106Z","iopub.status.idle":"2024-03-22T05:43:16.564815Z","shell.execute_reply.started":"2024-03-22T05:43:12.921034Z","shell.execute_reply":"2024-03-22T05:43:16.563199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(len(classes_ids_pairs), 1, figsize=(10, 100))\nfor i in range(len(classes_ids_pairs)):\n    axes[i].imshow(normalized_masks.squeeze()[i].to('cpu').detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2024-03-22T05:43:52.385299Z","iopub.execute_input":"2024-03-22T05:43:52.385627Z","iopub.status.idle":"2024-03-22T05:44:22.444091Z","shell.execute_reply.started":"2024-03-22T05:43:52.385602Z","shell.execute_reply":"2024-03-22T05:44:22.443145Z"},"trusted":true},"execution_count":null,"outputs":[]}]}