{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":23823,"databundleVersionId":1920183,"sourceType":"competition"}],"dockerImageVersionId":30085,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport cv2\nimport random\nimport time\nimport torchvision.models as models\nimport torchvision.transforms as transforms\nfrom torch.utils.data import  DataLoader\nimport numpy as np \nimport pandas as pd \nfrom PIL import Image\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input/train'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-13T23:25:04.809021Z","iopub.execute_input":"2021-09-13T23:25:04.809382Z","iopub.status.idle":"2021-09-13T23:25:06.438874Z","shell.execute_reply.started":"2021-09-13T23:25:04.809339Z","shell.execute_reply":"2021-09-13T23:25:06.438063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Анализ базы данных**","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/hpa-single-cell-image-classification/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:06.442943Z","iopub.execute_input":"2021-09-13T23:25:06.443206Z","iopub.status.idle":"2021-09-13T23:25:06.495409Z","shell.execute_reply.started":"2021-09-13T23:25:06.443181Z","shell.execute_reply":"2021-09-13T23:25:06.494665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# преобразование строк столбца Label в массив\ndef column(s):\n  s = s.split(\"|\")\n  new = []\n  for i in range(len(s)):\n    new.append(int(s[i]))\n  return new","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:06.496635Z","iopub.execute_input":"2021-09-13T23:25:06.496975Z","iopub.status.idle":"2021-09-13T23:25:06.503643Z","shell.execute_reply.started":"2021-09-13T23:25:06.496939Z","shell.execute_reply":"2021-09-13T23:25:06.501626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Label'] = df.Label.apply(lambda i: column(i))\ndf = df[df['Label'].map(len) == 1]\ndf['Label'] = df.Label.apply(lambda i: i[0])\ndf.index = np.arange(len(df))","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:06.505135Z","iopub.execute_input":"2021-09-13T23:25:06.505821Z","iopub.status.idle":"2021-09-13T23:25:06.576698Z","shell.execute_reply.started":"2021-09-13T23:25:06.50578Z","shell.execute_reply":"2021-09-13T23:25:06.576039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = np.zeros(19)\nfor i in np.array(df['Label']):\n    if i in range(19):\n        count[i] += 1\n\nplt.bar(np.arange(0,19),count)\nplt.title('repetition rate')\nplt.xlabel('classes')\nplt.ylabel('number')\nplt.xticks(np.arange(0,19))\nplt.savefig('gist.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:06.579581Z","iopub.execute_input":"2021-09-13T23:25:06.579994Z","iopub.status.idle":"2021-09-13T23:25:06.939258Z","shell.execute_reply.started":"2021-09-13T23:25:06.579958Z","shell.execute_reply":"2021-09-13T23:25:06.938281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# вывод картинок в RGBY\ndef stack(image_name):\n    red = cv2.imread('../input/hpa-single-cell-image-classification/train/'+image_name+'_red.png',  cv2.IMREAD_UNCHANGED)\n    green = cv2.imread('../input/hpa-single-cell-image-classification/train/'+image_name+'_green.png',  cv2.IMREAD_UNCHANGED)\n    blue = cv2.imread('../input/hpa-single-cell-image-classification/train/'+image_name+'_blue.png',  cv2.IMREAD_UNCHANGED)\n    yellow = cv2.imread('../input/hpa-single-cell-image-classification/train/'+image_name+'_yellow.png', cv2.IMREAD_UNCHANGED)\n    stacked_images = np.transpose(np.array([red, green, blue, yellow]), (1,2,0))\n    stacked_images = stacked_images[:,:,:3]\n    return stacked_images\n\n\ndef image_show(image_names):\n                                    # вывод одной картинки\n    if len(image_names) == 1: \n        stacked_images = stack(image_names[0])\n        plt.figure(figsize=(15, 15))\n        plt.subplot(1, 2, 1)\n        plt.title('RGBY Image')\n        plt.imshow(stacked_images)\n        plt.show()\n                                    # вывод по картинке для всех классов\n    else:\n        unit = {\n            0: 'nucleoplasm', \n            1: 'nuclear membrane',\n            2: 'nucleoli',\n            3: 'nucleoli fibrillar center',\n            4: 'nuclear speckles',\n            5: 'nuclear bodies',\n            6: 'endoplasmic reticulum',\n            7: 'golgi apparatus',\n            8: 'intermediate filaments',\n            9: 'actin filaments',\n            10: 'microtubules',\n            11: 'mitotic spindle',\n            12: 'centrosome',\n            13: 'plasma membrane',\n            14: 'mitochondria',\n            15: 'aggresome',\n            16: 'cytosol',\n            17: 'vesicles',\n            18: 'Negative'\n                    }\n        stacked_images = []\n        for i in range(len(image_names)):\n            stacked_images.append(stack(image_names[i]))\n        for i in range(0,4):\n            if i == 3:\n                fig, ax = plt.subplots(nrows = 1, ncols = 4, figsize = [16, 16] )\n            else:\n                fig, ax = plt.subplots(nrows = 1, ncols = 5, figsize = [20, 20] )    \n            for j, axi in enumerate(ax.flat):\n                axi.imshow(stacked_images[j+i*5])\n                if (j+i*5) in unit:\n                    axi.set_title(unit[j+i*5])\n        plt.savefig('in.png')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:06.941478Z","iopub.execute_input":"2021-09-13T23:25:06.941859Z","iopub.status.idle":"2021-09-13T23:25:06.956158Z","shell.execute_reply.started":"2021-09-13T23:25:06.941821Z","shell.execute_reply":"2021-09-13T23:25:06.955031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = []\nfor i in range(0,19):\n    s = df[df['Label'] == i]\n    #s = s[s['Weight'].astype(str)=='2048']\n    im.append(s['ID'].iloc[0])\nimage_show(im)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:06.957298Z","iopub.execute_input":"2021-09-13T23:25:06.957547Z","iopub.status.idle":"2021-09-13T23:25:23.988267Z","shell.execute_reply.started":"2021-09-13T23:25:06.957514Z","shell.execute_reply":"2021-09-13T23:25:23.987384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Подготовка базы данных к обучению**","metadata":{}},{"cell_type":"code","source":"# делим датасет на test и train\nitem = range(df.shape[0])\nitem = random.sample(item, df.shape[0]-2000)\ntrain_df = pd.DataFrame(columns=[\"ID\", \"Label\"])\ntest_df = pd.DataFrame(columns=[\"ID\", \"Label\"])\nfor i in range(10412):\n    if i in item:\n        train_df.loc[train_df.shape[0]] = [df['ID'][i], df['Label'][i]]\n        \n    else: \n        test_df.loc[test_df.shape[0]] = [df['ID'][i], df['Label'][i]]\ndel df","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:23.989537Z","iopub.execute_input":"2021-09-13T23:25:23.990043Z","iopub.status.idle":"2021-09-13T23:25:53.662669Z","shell.execute_reply.started":"2021-09-13T23:25:23.989995Z","shell.execute_reply":"2021-09-13T23:25:53.661798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass Data_prepare(torch.utils.data.Dataset):\n    def __init__(self, names, labels, transform):\n        super(Data_prepare, self).__init__()\n        self.name_list = names\n        self.label_list = labels\n        self.transform = transform\n\n    def __getitem__(self, index):\n        # Load channels.\n        name = self.name_list[index]\n        channel_list = ['blue', 'green', 'red', 'yellow']\n        channels = [cv2.imread('../input/hpa-single-cell-image-classification/train/'+name+'_'+ch+'.png',  cv2.IMREAD_UNCHANGED) for ch in\n                    channel_list]\n\n        # Get image.\n        image = np.stack(channels, axis=-1)\n        max_value = 256 ** ((image.dtype == np.uint16) + 1) - 1\n        image = (image / max_value).astype(np.float32)\n        \n        if self.transform is not None:\n            image = self.transform(image)\n        \n        #Load label\n        label = name = self.label_list[index]\n        return image, label\n\n    def __len__(self):\n        return len(self.name_list)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:53.663988Z","iopub.execute_input":"2021-09-13T23:25:53.664341Z","iopub.status.idle":"2021-09-13T23:25:53.676564Z","shell.execute_reply.started":"2021-09-13T23:25:53.664305Z","shell.execute_reply":"2021-09-13T23:25:53.675603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Загружаем модель**","metadata":{}},{"cell_type":"code","source":"resnet50 = models.resnet50(pretrained=True)\nresnet50.conv1 = torch.nn.Conv2d(4,64,kernel_size = (7,7),stride = (2,2), padding = (3,3), bias = False) \nfor param in resnet50.parameters():\n    param.requires_grad = False\nnum_ftrs = resnet50.fc.in_features\nresnet50.fc = torch.nn.Linear(num_ftrs, 19)","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:53.678097Z","iopub.execute_input":"2021-09-13T23:25:53.678482Z","iopub.status.idle":"2021-09-13T23:25:58.278048Z","shell.execute_reply.started":"2021-09-13T23:25:53.678448Z","shell.execute_reply":"2021-09-13T23:25:58.276957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(net, train, test,size):\n    t = 0\n    time_elapsed = 0\n    since = time.time()\n    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n    net = net.to(device)\n    df_transform = transforms.Compose([\n        transforms.ToTensor(),\n        transforms.Resize((size, size)),\n    ])\n    tests = Data_prepare(list(test['ID']), list(test['Label']), df_transform)\n    trains = Data_prepare(list(train['ID']), list(train['Label']), df_transform ) \n    tests_dl = DataLoader(trains, batch_size=100, shuffle=True, num_workers=4)\n    trains_dl = DataLoader(tests, batch_size=100, shuffle=True, num_workers=4)\n    #x_train, y_train = next(iter(trains_dl))\n    #print(x_train.shape)\n\n    loss = torch.nn.CrossEntropyLoss()\n    optimizer = torch.optim.Adam(net.parameters(), lr=1.0e-3)\n    \n    batch_size = 100\n\n    test_accuracy_history = []\n    test_loss_history = []\n    \n    for epoch in range(25):\n        ch_loss = 0.0\n        corrects = 0\n        batch = 0\n        print('epoch', epoch)\n        #order = np.random.permutation(len(train))\n        for x_train, y_train in trains_dl:\n            x_batch = x_train.to(device)\n            y_batch = y_train.to(device)\n\n            preds = net.forward(x_batch)\n\n            loss_value = loss(preds, y_batch)\n            loss_value.backward()\n\n        optimizer.step()\n        print('time for train in one epoch:',time.time() - since-t) \n        t = time.time() - since\n        net.eval()\n        print('test')\n        for x_test, y_test in tests_dl:\n            x_test = x_test.to(device)\n            y_test = y_test.to(device)\n            out = net.forward(x_test)\n            _, test_preds = torch.max(out.data, 1)\n            ch_loss += loss(out, y_test)\n            corrects += torch.sum(test_preds == y_test.data)\n            batch +=1\n        test_loss_history.append(ch_loss / batch)\n\n        accuracy = corrects/2000\n        test_accuracy_history.append(accuracy)\n        print('time for epoch:',time.time() - since-time_elapsed)\n        time_elapsed = time.time() - since\n        print(accuracy)\n        print(test_loss_history)\n    del net\n    plt.plot(test_loss_history)\n    plt.plot(test_accuracy_history)\n    return test_accuracy_history, test_loss_history, time_elapced","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:58.279559Z","iopub.execute_input":"2021-09-13T23:25:58.280184Z","iopub.status.idle":"2021-09-13T23:25:58.298884Z","shell.execute_reply.started":"2021-09-13T23:25:58.280143Z","shell.execute_reply":"2021-09-13T23:25:58.297793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Запускаем обучение и выводим графики**","metadata":{}},{"cell_type":"code","source":"acc0, lst0, t0 = train(resnet50, train_df, test_df, 512)","metadata":{"execution":{"iopub.status.busy":"2021-09-13T23:25:58.300383Z","iopub.execute_input":"2021-09-13T23:25:58.301105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(acc0)\nplt.title('Validation Loss')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(lst0)\nplt.title('Validation Accuracy');","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"so, we are here again\nand again again again","metadata":{},"execution_count":null,"outputs":[]}]}