{"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":"!pip install ../input/pipipi/addict-2.4.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-10-05T06:53:18.224835Z","iopub.execute_input":"2022-10-05T06:53:18.225207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/pipipipi/einops-0.4.1-py3-none-any.whl","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/pipipi/nystrom_attention-0.0.11-py3-none-any.whl","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/pipipi/pytorch_toolbelt-0.5.2-py3-none-any.whl","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/transmil-200')\n\nimport pandas as pd\nimport cv2\nimport os, torch, glob\nimport numpy as np\nfrom torch.autograd import Variable\nfrom PIL import Image  \nfrom torchvision import models, transforms\nimport torch.nn as nn\nimport shutil\nimport h5py\nimport time\nimport random\n\nfrom transMIL_datasets import DataInterface\nfrom transMIL_models import ModelInterface\nfrom utils.utils import *\n\n# pytorch_lightning\nimport pytorch_lightning as pl\nfrom pytorch_lightning import Trainer\nfrom pytorch_lightning.callbacks import ModelCheckpoint","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_CSV = \"../input/mayo-clinic-strip-ai/test.csv\"\nSAMPLE_SUB_CSV = \"../input/mayo-clinic-strip-ai/sample_submission.csv\"\ntest_path = \"../input/mayo-clinic-strip-ai/test\"\n\ndef create_folder(folder):\n    if not os.path.exists(folder):\n        os.makedirs(folder)\n        \nIMG_SIZE = 512","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Resnet Prepare","metadata":{}},{"cell_type":"code","source":"resnet18_feature = models.resnet18(pretrained = False)\npre = torch.load('../input/resnet18/resnet18-5c106cde.pth')\nresnet18_feature.load_state_dict(pre)\nresnet18_feature.fc = nn.Linear(512, 512)\ntorch.nn.init.eye_(resnet18_feature.fc.weight)\nfor param in resnet18_feature.parameters():\n    param.requires_grad = False  \nresnet18_feature","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loader = transforms.Compose([\n    transforms.ToTensor()]) \ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# 输入图片地址, 返回tensor变量\ndef image_loader(image_name):\n    image = Image.open(image_name).convert('RGB')\n    image = loader(image).unsqueeze(0)\n    return image.to(device, torch.float)\n\n# features_dir = './h5_files'\n# if not os.path.isfile(features_dir):\n#     ! mkdir -p ./h5_files","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def my_readh5(h5_path):\n#     f = h5py.File(h5_path, \"r\")\n\n#     my_feature = []\n#     for key in f.keys():\n#         #         my_feature.append(f[key][:][0])\n#         my_feature.append(f[key][:])\n#     f.close()\n#     my_feature = np.array(my_feature)\n#     return torch.tensor(my_feature)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ModelInterface.load_from_checkpoint('../input/100501/100501.ckpt')\nmodel  = model.eval().cuda()\n \n# print(model.learning_rate)\n# prints the learning_rate you used in this checkpoint\n# x = my_readh5('../input/mamyo-h5/mayoh5/006388_0.h5').cuda()\n\n# x = x.reshape(1,-1,2048)\n# print(x.shape)\n# print(model.model)\n# y_hat = model.model(data=x)\n# print(float(y_hat['Y_prob'][0][1]))\n# for item in y_hat['Y_prob']:\n#     print(item)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.color import rgb2hsv\nfrom skimage.exposure import is_low_contrast\nfrom skimage import img_as_ubyte\nimport openslide\n\ndef slice_images2h5(image_path, saved_path, net, use_gpu):\n    slide = openslide.open_slide(str(image_path))\n    size = (IMG_SIZE, IMG_SIZE)\n    level = 0\n    img_id = image_path.rsplit(\"/\", 1)[-1].rsplit(\".\")[0]\n    print('Slicing Image ' + img_id + ' ...')\n#     count = 0\n    \n    w, h = slide.level_dimensions[0]  # 跟shape是反过来的\n    \n    possible_slices_x = h // IMG_SIZE\n    possible_slices_y = w // IMG_SIZE\n    \n\n# 选择性切片    \n#     flag = 0\n#     th_ = 0\n#     num = (possible_slices_x + 1) * (possible_slices_y + 1)\n#     if num > 500:\n#         flag = 1\n#         th_ = 500 / num * 100\n        \n\n#     f = h5py.File(saved_path, \"a\")  # h5\n    feature = []\n    fe_num = 0\n    \n    for x in range(possible_slices_x):\n        for y in range(possible_slices_y):\n#             if fe_num > 1000:\n#                 break\n            \n            \n            # =========================  User Code Begin ==================================\n# 选择性切片\n            ran_ = random.randint(0, 101)\n            if ran_ < 1:\n                continue\n            \n            # 左上角的位置叫做region\n            region = (y * IMG_SIZE, x * IMG_SIZE)\n            image = np.array(slide.read_region(region, level, size))\n            image = image[:, :, :-1]\n                        \n            img_mean = np.mean(image)\n            if img_mean > 240 or img_mean < 10:\n                continue\n            \n            \n            \n            if image.shape[0] != IMG_SIZE or image.shape[1] != IMG_SIZE:\n                print(\"Error type!!!!!!\")\n                image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n            \n#             print(\"before image2:\", image.shape)\n            image2 = rgb2hsv(image)\n            h, w, c = image2.shape\n            sat_img = image2[:, :, 1]\n            sat_img = img_as_ubyte(sat_img)\n            ave_sat = np.sum(sat_img) / (h * w)\n        \n            if ave_sat >= 8 or is_low_contrast(image):  # foreground-percent:域值 20倍的时候10 by汪的代码\n                \n            # =========================  User Code End ==================================   \n                new_img = torch.from_numpy(image).to(torch.float32).permute(2, 0, 1)\n                new_img = new_img.view(1, new_img.shape[0], new_img.shape[1], new_img.shape[2])\n                x1 = Variable(new_img, requires_grad=False)\n#                 x1 = Variable(image, requires_grad=False)\n                if use_gpu:\n                    x1 = x1.cuda()\n                    net = net.cuda()\n                y1 = net(x1).cpu()\n                y1 = y1.data.numpy()\n                feature.append(y1)\n                fe_num += 1\n                \n#                 save_h5_path = img_id + str(count).zfill(5)\n#                 if count < 1:\n#                     print(save_h5_path)\n#                 f.create_dataset(save_h5_path, data = y1)\n#                 count = count + 1\n                \n#     f.close\n#     MAX_SIZE = 460\n    feature = np.array(feature)\n    feature = torch.tensor(feature)\n    print(\"feature.shape\", feature.shape)\n    feature = feature.reshape(1,-1,512).cuda()\n#     print('feature = ', feature.shape)\n#     B, N, C = feature.shape\n#     if N > MAX_SIZE:\n#         arr = random.sample(range(N), MAX_SIZE)\n#     feature = feature[:, arr, :]\n    y_hat = model.model(data=feature)\n    print(y_hat['Y_prob'])\n    return y_hat['Y_prob']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## tile + h5create","metadata":{}},{"cell_type":"code","source":"test_paths = glob.glob(\"../input/mayo-clinic-strip-ai/test/*\")\nuse_gpu = torch.cuda.is_available()\n# slice_images2h5(image_path, saved_path, net, use_gpu)\ndf1 = pd.DataFrame(columns = ['patient_id','CE','LAA'])\nnum = 0\nfor path in test_paths:\n    img_id = path[:-4].rsplit(\"/\", 1)[-1]\n    save_path = './' + img_id + '.h5'\n    \n    print(save_path, ',', path)\n    time_start = time.time()\n    \n    pre_xy = slice_images2h5(path, save_path, resnet18_feature, use_gpu)\n    df1.loc[num] = {'patient_id':img_id.split('_')[0],'CE':float(pre_xy[0][0]),'LAA':float(pre_xy[0][1])}\n    \n    time_elapsed = time.time() - time_start\n    print(time_elapsed)\n    num += 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.groupby(\"patient_id\", as_index=False).mean().to_csv('./submission.csv', index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.groupby(\"patient_id\", as_index=False).mean()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}