{"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":"import numpy as np\nimport pandas as pd\nimport torch\nimport torchvision\nimport matplotlib.pyplot as plt\nimport PIL.Image as Image\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-09T18:53:11.587583Z","iopub.execute_input":"2022-07-09T18:53:11.588269Z","iopub.status.idle":"2022-07-09T18:53:13.628785Z","shell.execute_reply.started":"2022-07-09T18:53:11.588183Z","shell.execute_reply":"2022-07-09T18:53:13.627682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/uw-madison-gi-tract-image-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:16.205263Z","iopub.execute_input":"2022-07-09T18:53:16.205771Z","iopub.status.idle":"2022-07-09T18:53:16.766250Z","shell.execute_reply.started":"2022-07-09T18:53:16.205740Z","shell.execute_reply":"2022-07-09T18:53:16.765320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/uw-madison-gi-tract-image-segmentation/train/'","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:16.768009Z","iopub.execute_input":"2022-07-09T18:53:16.768652Z","iopub.status.idle":"2022-07-09T18:53:16.773377Z","shell.execute_reply.started":"2022-07-09T18:53:16.768609Z","shell.execute_reply":"2022-07-09T18:53:16.772297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pics_pathes = []","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:16.848228Z","iopub.execute_input":"2022-07-09T18:53:16.848740Z","iopub.status.idle":"2022-07-09T18:53:16.852012Z","shell.execute_reply.started":"2022-07-09T18:53:16.848708Z","shell.execute_reply":"2022-07-09T18:53:16.851404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cases in os.listdir(train_path):\n    for days in os.listdir(os.path.join(train_path, cases)):\n        for slices in os.listdir(os.path.join(train_path, cases, days, 'scans')):\n            train_pics_pathes.append(os.path.join(cases, days, 'scans', slices))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:17.000067Z","iopub.execute_input":"2022-07-09T18:53:17.000645Z","iopub.status.idle":"2022-07-09T18:53:20.937525Z","shell.execute_reply.started":"2022-07-09T18:53:17.000613Z","shell.execute_reply":"2022-07-09T18:53:20.936682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_pics_matching = dict()\nfor pics_path in train_pics_pathes:\n    pics_path_splitted = pics_path.split('/')\n    name_splitted = pics_path_splitted[3].split('_')\n    pics_id = pics_path_splitted[0] + '_' + pics_path_splitted[1].split('_')[1] + '_' + name_splitted[0] + '_' + name_splitted[1]\n    id_pics_matching[pics_id] = os.path.join(train_path, pics_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:20.938842Z","iopub.execute_input":"2022-07-09T18:53:20.939178Z","iopub.status.idle":"2022-07-09T18:53:21.053526Z","shell.execute_reply.started":"2022-07-09T18:53:20.939147Z","shell.execute_reply":"2022-07-09T18:53:21.052770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.dropna().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:21.054388Z","iopub.execute_input":"2022-07-09T18:53:21.055271Z","iopub.status.idle":"2022-07-09T18:53:21.093336Z","shell.execute_reply.started":"2022-07-09T18:53:21.055238Z","shell.execute_reply":"2022-07-09T18:53:21.092659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open('../input/uw-madison-gi-tract-image-segmentation/train/case30/case30_day0/scans/slice_0135_266_266_1.50_1.50.png')\nimg = torchvision.transforms.ToTensor()(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:21.094954Z","iopub.execute_input":"2022-07-09T18:53:21.095259Z","iopub.status.idle":"2022-07-09T18:53:21.117392Z","shell.execute_reply.started":"2022-07-09T18:53:21.095231Z","shell.execute_reply":"2022-07-09T18:53:21.116695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def look(img):\n    plt.imshow(img.detach().cpu().permute(1, 2, 0))\n    plt.title(str(img.shape))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:21.118487Z","iopub.execute_input":"2022-07-09T18:53:21.119245Z","iopub.status.idle":"2022-07-09T18:53:21.123889Z","shell.execute_reply.started":"2022-07-09T18:53:21.119210Z","shell.execute_reply":"2022-07-09T18:53:21.123018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_rle(img, seq):\n    img = img.clone()\n    seq = seq.split()\n    for start in range(0, len(seq), 2):\n        start_x = int(seq[start]) % img.shape[2]\n        start_y = int(seq[start]) // img.shape[1]\n        for pix in range(start_x, start_x+int(seq[start+1])):\n            img[0][start_y][pix] = 65536\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:22.802083Z","iopub.execute_input":"2022-07-09T18:53:22.802472Z","iopub.status.idle":"2022-07-09T18:53:22.808644Z","shell.execute_reply.started":"2022-07-09T18:53:22.802417Z","shell.execute_reply":"2022-07-09T18:53:22.807426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Testing encoder","metadata":{"execution":{"iopub.status.busy":"2022-06-09T12:03:19.113906Z","iopub.execute_input":"2022-06-09T12:03:19.114261Z","iopub.status.idle":"2022-06-09T12:03:19.119983Z","shell.execute_reply.started":"2022-06-09T12:03:19.114231Z","shell.execute_reply":"2022-06-09T12:03:19.11866Z"}}},{"cell_type":"code","source":"look(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:25.192995Z","iopub.execute_input":"2022-07-09T18:53:25.193381Z","iopub.status.idle":"2022-07-09T18:53:25.413923Z","shell.execute_reply.started":"2022-07-09T18:53:25.193349Z","shell.execute_reply":"2022-07-09T18:53:25.412956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decoded_img = decode_rle(img, data['segmentation'][33909])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:25.415814Z","iopub.execute_input":"2022-07-09T18:53:25.416226Z","iopub.status.idle":"2022-07-09T18:53:25.455068Z","shell.execute_reply.started":"2022-07-09T18:53:25.416185Z","shell.execute_reply":"2022-07-09T18:53:25.454304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"look(decoded_img)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:25.522322Z","iopub.execute_input":"2022-07-09T18:53:25.522856Z","iopub.status.idle":"2022-07-09T18:53:25.712239Z","shell.execute_reply.started":"2022-07-09T18:53:25.522825Z","shell.execute_reply":"2022-07-09T18:53:25.711345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef encode_line(img, i):\n    pix = 0\n    rle = []\n    while pix < img.shape[2]:\n        if img[0][i][pix] == 65536:\n            start = pix\n            while pix < img.shape[2] and img[0][i][pix] == 65536:\n                pix += 1\n            rle.append(str(i*img.shape[1] + start))\n            rle.append(str(pix-start))\n        pix += 1\n    return rle\n\ndef encode_rle(img):\n    rle = []\n    for i in range(img.shape[1]):\n        rle += encode_line(img, i)\n    return ' '.join(rle)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:28.229268Z","iopub.execute_input":"2022-07-09T18:53:28.230151Z","iopub.status.idle":"2022-07-09T18:53:28.236793Z","shell.execute_reply.started":"2022-07-09T18:53:28.230108Z","shell.execute_reply":"2022-07-09T18:53:28.235887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Testing decoder","metadata":{}},{"cell_type":"code","source":"seq_rle = encode_rle(decoded_img)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:28.613591Z","iopub.execute_input":"2022-07-09T18:53:28.613977Z","iopub.status.idle":"2022-07-09T18:53:29.346607Z","shell.execute_reply.started":"2022-07-09T18:53:28.613947Z","shell.execute_reply":"2022-07-09T18:53:29.345552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"look(decode_rle(img, seq_rle))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:29.348143Z","iopub.execute_input":"2022-07-09T18:53:29.348500Z","iopub.status.idle":"2022-07-09T18:53:29.559389Z","shell.execute_reply.started":"2022-07-09T18:53:29.348470Z","shell.execute_reply":"2022-07-09T18:53:29.558458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Plot segmentation","metadata":{}},{"cell_type":"code","source":"def look_seg(img, segmentations):\n    \"\"\"\n    segmentation is 3d array with represents 3 types of organs segmentation\n    \"\"\"\n    img_seg = torch.zeros([3, img.shape[1], img.shape[2]])\n\n    img_seg[0], img_seg[1], img_seg[2] = img[0], img[0], img[0]\n    \n    if img_seg.max() > 1:\n        img_seg /= (2**16)\n    \n    colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]\n    \n    for seg_channel in range(3):\n        seg = segmentations[seg_channel].split()\n        for start in range(0, len(seg), 2):\n            start_x = int(seg[start]) % img.shape[2]\n            start_y = int(seg[start]) // img.shape[1]\n            for pix in range(start_x, start_x+int(seg[start+1])):\n                for channel in range(3):\n                    img_seg[channel][start_y][pix] = colors[seg_channel][channel]\n    plt.imshow(img_seg.permute(1, 2, 0))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:30.469164Z","iopub.execute_input":"2022-07-09T18:53:30.469843Z","iopub.status.idle":"2022-07-09T18:53:30.478306Z","shell.execute_reply.started":"2022-07-09T18:53:30.469804Z","shell.execute_reply":"2022-07-09T18:53:30.477371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_test = torchvision.transforms.ToTensor()(Image.open('../input/uw-madison-gi-tract-image-segmentation/train/case30/case30_day0/scans/slice_0137_266_266_1.50_1.50.png'))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:33.652110Z","iopub.execute_input":"2022-07-09T18:53:33.652509Z","iopub.status.idle":"2022-07-09T18:53:33.666146Z","shell.execute_reply.started":"2022-07-09T18:53:33.652474Z","shell.execute_reply":"2022-07-09T18:53:33.665128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_seg = torchvision.transforms.ToTensor()(Image.open('../input/uw-madison-gi-tract-image-segmentation/train/case30/case30_day0/scans/slice_0137_266_266_1.50_1.50.png')) \n\nlook_seg(img_test, [data['segmentation'][33911], '', data['segmentation'][33912]])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:34.398538Z","iopub.execute_input":"2022-07-09T18:53:34.398919Z","iopub.status.idle":"2022-07-09T18:53:34.700767Z","shell.execute_reply.started":"2022-07-09T18:53:34.398887Z","shell.execute_reply":"2022-07-09T18:53:34.699999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"look_seg(img, [data['segmentation'][33909], '', ''])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:36.363743Z","iopub.execute_input":"2022-07-09T18:53:36.364122Z","iopub.status.idle":"2022-07-09T18:53:36.628832Z","shell.execute_reply.started":"2022-07-09T18:53:36.364092Z","shell.execute_reply":"2022-07-09T18:53:36.627719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Preparing data","metadata":{}},{"cell_type":"code","source":"data_full = dict()\n\nfor ids in data['id']:\n    data_full[ids] = ['', '', '']\n\nfor i in range(len(data)):\n    class_seg = data['class'][i]\n    if class_seg == 'stomach':\n        data_full[data['id'][i]][0] += data['segmentation'][i]\n    if class_seg == 'small_bowel':\n        data_full[data['id'][i]][1] += data['segmentation'][i]\n    if class_seg == 'large_bowel':\n        data_full[data['id'][i]][2] += data['segmentation'][i]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:42.202342Z","iopub.execute_input":"2022-07-09T18:53:42.203018Z","iopub.status.idle":"2022-07-09T18:53:42.768693Z","shell.execute_reply.started":"2022-07-09T18:53:42.202984Z","shell.execute_reply":"2022-07-09T18:53:42.767608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"'id' -> [stomach_seg:str, small_b:str, large_b:str]","metadata":{}},{"cell_type":"markdown","source":"<h4>Working with device","metadata":{}},{"cell_type":"code","source":"def move_to(data, device):\n    \"\"\"\n    moving data to device\n    :param data: data to move\n    :param device: device\n    :return: moved data\n    \"\"\"\n    if isinstance(data, (list, tuple)):\n        return [move_to(x, device) for x in data]\n    return data.to(device, non_blocking=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:48.211773Z","iopub.execute_input":"2022-07-09T18:53:48.212496Z","iopub.status.idle":"2022-07-09T18:53:48.219520Z","shell.execute_reply.started":"2022-07-09T18:53:48.212448Z","shell.execute_reply":"2022-07-09T18:53:48.218177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:48.995588Z","iopub.execute_input":"2022-07-09T18:53:48.996502Z","iopub.status.idle":"2022-07-09T18:53:49.001220Z","shell.execute_reply.started":"2022-07-09T18:53:48.996453Z","shell.execute_reply":"2022-07-09T18:53:48.999981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Define Model","metadata":{}},{"cell_type":"code","source":"class Downsampler(torch.nn.Module):\n    def __init__(self, in_channels, out_channels, pooling = True):\n        super().__init__()\n        self.pooling = pooling\n        \n        self.conv1 = torch.nn.Conv2d(in_channels, out_channels, 3)\n        self.conv2 = torch.nn.Conv2d(out_channels, out_channels, 3)\n        self.conv3 = torch.nn.Conv2d(out_channels, out_channels, 3)\n        self.act = torch.nn.ReLU()\n        self.pool = torch.nn.MaxPool2d(2)\n        \n    def forward(self, X):\n        X = self.act(self.conv1(X))\n        X = self.act(self.conv2(X))\n        X = self.act(self.conv3(X))\n        if self.pooling:\n            X = self.pool(X)\n        return X","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:50.503159Z","iopub.execute_input":"2022-07-09T18:53:50.503979Z","iopub.status.idle":"2022-07-09T18:53:50.512355Z","shell.execute_reply.started":"2022-07-09T18:53:50.503930Z","shell.execute_reply":"2022-07-09T18:53:50.511155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Upsampler(torch.nn.Module):\n    def __init__(self, in_channels):\n        super().__init__()\n        self.deconv = torch.nn.ConvTranspose2d(in_channels, in_channels//2, 2, 2)\n        self.conv1 = torch.nn.Conv2d(in_channels, in_channels//2, 3)\n        self.conv2 = torch.nn.Conv2d(in_channels//2, in_channels//2, 3)\n        self.act = torch.nn.ReLU()\n        \n    def forward(self, X, X_cat):\n        X = self.act(self.deconv(X))\n        X = torch.cat([X, X_cat], axis=1)\n        X = self.act(self.conv1(X))\n        X = self.act(self.conv2(X))\n        return X","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:50.803938Z","iopub.execute_input":"2022-07-09T18:53:50.804752Z","iopub.status.idle":"2022-07-09T18:53:50.811404Z","shell.execute_reply.started":"2022-07-09T18:53:50.804714Z","shell.execute_reply":"2022-07-09T18:53:50.810497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNet(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.downsampler1 = Downsampler(1, 64)\n        self.downsampler2 = Downsampler(64, 128)\n        self.downsampler3 = Downsampler(128, 256)\n        self.downsampler4 = Downsampler(256, 512)\n        self.downsampler5 = Downsampler(512, 1024, pooling = False)\n        \n        self.upsampler1 = Upsampler(1024)\n        self.upsampler2 = Upsampler(512)\n        self.upsampler3 = Upsampler(256)\n        self.upsampler4 = Upsampler(128)\n        \n        self.final_conv = torch.nn.Conv2d(64, 3, 3)  # 3 channels for seg. maps of stomach, large and small bowel\n        self.final_act = torch.nn.Sigmoid()\n        \n    def copy_crop(self, X, shape):\n        top = (X.shape[2]-shape)//2  # as same as left\n        return torchvision.transforms.functional.crop(X, top, top, shape, shape).clone()\n        \n    \n    def forward(self, X):\n        X = self.downsampler1(X)\n        X_1 = X\n        X = self.downsampler2(X)\n        X_2 = X\n        X = self.downsampler3(X)\n        X_3 = X\n        X = self.downsampler4(X)\n        X_4 = X\n        X = self.downsampler5(X)\n        \n        X = self.upsampler1(X, self.copy_crop(X_4, 48))\n        X = self.upsampler2(X, self.copy_crop(X_3, 88))\n        X = self.upsampler3(X, self.copy_crop(X_2, 168))\n        X = self.upsampler4(X, self.copy_crop(X_1, 328))\n        \n        X = self.final_conv(X)\n        return self.final_act(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:53:51.681329Z","iopub.execute_input":"2022-07-09T18:53:51.681752Z","iopub.status.idle":"2022-07-09T18:53:51.692908Z","shell.execute_reply.started":"2022-07-09T18:53:51.681718Z","shell.execute_reply":"2022-07-09T18:53:51.692124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_loader(data, batch_size, id_pics_matching):\n    ind = 0\n    data_keys = list(data_full.keys())\n    while ind + batch_size < len(data_keys):\n        X = torch.zeros([batch_size, 1, 572, 572])\n        y = torch.zeros([batch_size, 3, 572, 572])\n        \n        for i in range(batch_size):\n            X_new = torchvision.transforms.ToTensor()(Image.open(id_pics_matching[data_keys[ind+i]])).type(torch.float32)/(2**16)\n            X[i] = torchvision.transforms.Resize([572, 572])(X_new)\n            \n            for j in range(3):            \n                y_new = decode_rle(X_new, data_full[data_keys[ind+i]][j]).type(torch.float32)/(2**16)\n                y[i][j] = torchvision.transforms.Resize([572, 572])(y_new)\n        ind += batch_size\n        \n        yield X, y","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:54:00.355408Z","iopub.execute_input":"2022-07-09T18:54:00.355875Z","iopub.status.idle":"2022-07-09T18:54:00.363400Z","shell.execute_reply.started":"2022-07-09T18:54:00.355843Z","shell.execute_reply":"2022-07-09T18:54:00.362704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_weights(y, n=1):\n    res = torch.log(n*y+1)+0.2\n    return res","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:54:04.071882Z","iopub.execute_input":"2022-07-09T18:54:04.072277Z","iopub.status.idle":"2022-07-09T18:54:04.076870Z","shell.execute_reply.started":"2022-07-09T18:54:04.072244Z","shell.execute_reply":"2022-07-09T18:54:04.075924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:54:06.174104Z","iopub.execute_input":"2022-07-09T18:54:06.175048Z","iopub.status.idle":"2022-07-09T18:54:06.179019Z","shell.execute_reply.started":"2022-07-09T18:54:06.175009Z","shell.execute_reply":"2022-07-09T18:54:06.177990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 4\nepochs = 0  #!\nlr = 1e-4\n\nmodel = move_to(UNet(), device)\nopt = torch.optim.Adam(model.parameters(), lr=lr)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:54:37.148134Z","iopub.execute_input":"2022-07-09T18:54:37.148604Z","iopub.status.idle":"2022-07-09T18:54:37.544307Z","shell.execute_reply.started":"2022-07-09T18:54:37.148569Z","shell.execute_reply":"2022-07-09T18:54:37.543333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.load_state_dict(torch.load('../input/learned-model/learned_model.pth'))","metadata":{"execution":{"iopub.status.busy":"2022-06-26T20:23:02.014293Z","iopub.execute_input":"2022-06-26T20:23:02.014671Z","iopub.status.idle":"2022-06-26T20:23:04.653221Z","shell.execute_reply.started":"2022-06-26T20:23:02.014634Z","shell.execute_reply":"2022-06-26T20:23:04.652503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch_losses = []\nbatch_losses = []","metadata":{"execution":{"iopub.status.busy":"2022-06-26T20:23:04.656905Z","iopub.execute_input":"2022-06-26T20:23:04.657218Z","iopub.status.idle":"2022-06-26T20:23:04.660357Z","shell.execute_reply.started":"2022-06-26T20:23:04.657191Z","shell.execute_reply":"2022-06-26T20:23:04.659647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"av_loss = 0\n\nav_epoch_loss = 0\nepoch_steps = 0\n\nn_iters_loss = 5  # как часто выводим \n\ncnt = 0\nsteps = 0\n\nacc_grad_rate = 8  # сколько аккумулировать\n\n\nfor epoch in range(epochs):\n    av_epoch_loss = 0\n    epoch_steps = 0\n    for X, y in batch_loader(data, batch_size, id_pics_matching):\n        \n        X, y = move_to(X, device), move_to(y, device)\n        out = torchvision.transforms.Resize([572, 572])(model(X))\n        \n        loss = torch.nn.BCELoss(weight = move_to(batch_weights(y, 1), device))(out, y) / acc_grad_rate\n        loss.backward()\n        batch_losses.append(loss.detach().cpu().numpy())\n        \n        av_loss += loss.detach().cpu().numpy()\n        \n        av_epoch_loss += loss.detach().cpu().numpy()\n        epoch_steps += 1\n        \n        if cnt > 0 and cnt % acc_grad_rate == 0:\n            opt.step()\n            opt.zero_grad()\n            steps += 1\n            \n            if steps % n_iters_loss == 0 and steps > 0:\n                print(steps, av_loss/n_iters_loss)\n                av_loss = 0\n\n        cnt += 1\n        torch.cuda.empty_cache()\n    print(f'Epoch {epoch} finished with av. loss: {av_epoch_loss/epoch_steps}')\n    epoch_losses.append(av_epoch_loss/epoch_steps)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:54:38.939297Z","iopub.execute_input":"2022-07-09T18:54:38.939698Z","iopub.status.idle":"2022-07-09T18:54:38.950233Z","shell.execute_reply.started":"2022-07-09T18:54:38.939665Z","shell.execute_reply":"2022-07-09T18:54:38.949352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"look(model(torchvision.transforms.Resize([572, 572])(move_to(img, device)).view(1, 1, 572, 572).type(torch.float32)/(2**16))[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:54:43.656832Z","iopub.execute_input":"2022-07-09T18:54:43.657196Z","iopub.status.idle":"2022-07-09T18:54:47.345484Z","shell.execute_reply.started":"2022-07-09T18:54:43.657168Z","shell.execute_reply":"2022-07-09T18:54:47.344493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#look(out[0][0:1])","metadata":{"execution":{"iopub.status.busy":"2022-06-26T20:51:26.052569Z","iopub.execute_input":"2022-06-26T20:51:26.052981Z","iopub.status.idle":"2022-06-26T20:51:26.361113Z","shell.execute_reply.started":"2022-06-26T20:51:26.052948Z","shell.execute_reply":"2022-06-26T20:51:26.3603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def n_params(model):\n    return sum(p.numel() for p in model.parameters() if p.requires_grad)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:55:06.922743Z","iopub.execute_input":"2022-07-09T18:55:06.924130Z","iopub.status.idle":"2022-07-09T18:55:06.930958Z","shell.execute_reply.started":"2022-07-09T18:55:06.924089Z","shell.execute_reply":"2022-07-09T18:55:06.929967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_params(model)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:55:14.019101Z","iopub.execute_input":"2022-07-09T18:55:14.019525Z","iopub.status.idle":"2022-07-09T18:55:14.026300Z","shell.execute_reply.started":"2022-07-09T18:55:14.019483Z","shell.execute_reply":"2022-07-09T18:55:14.025274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ","metadata":{}}]}