Skip to content

Updated initialization #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 105 additions & 34 deletions scripts/InitializationNotebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -16,6 +16,8 @@
"import re\n",
"import os, os.path\n",
"from os.path import splitext\n",
"import pathlib\n",
"import tqdm\n",
"import pydicom as dicom\n",
"import numpy as np\n",
"from pydicom.uid import UID, generate_uid\n",
Expand All @@ -33,30 +35,31 @@
"import math\n",
"import torch\n",
"import torchvision\n",
"import scipy\n",
"\n",
"sys.path.append(\"..\")\n",
"import echonet\n",
"\n",
"import wget \n",
"\n",
"#destinationFolder = \"/Users/davidouyang/Dropbox/Echo Research/CodeBase/Output\"\n",
"destinationFolder = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\Output\"\n",
"#videosFolder = \"/Users/davidouyang/Dropbox/Echo Research/CodeBase/a4c-video-dir\"\n",
"videosFolder = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\a4c-video-dir\"\n",
"#DestinationForWeights = \"/Users/davidouyang/Dropbox/Echo Research/CodeBase/EchoNetDynamic-Weights\"\n",
"DestinationForWeights = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\EchoNetDynamic-Weights\""
"destinationFolder = \"\"\n",
"#destinationFolder = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\Output\"\n",
"videosFolder = \"\"\n",
"# videosFolder = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\a4c-video-dir\"\n",
"DestinationForWeights = \"\"\n",
"# DestinationForWeights = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\EchoNetDynamic-Weights\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weights are at C:\\Users\\Windows\\Dropbox\\Echo Research\\CodeBase\\EchoNetDynamic-Weights\n",
"The weights are at /Users/akn/Drive/Developer/TTE-ML/weights\n",
"Segmentation Weights already present\n",
"EF Weights already present\n"
]
Expand Down Expand Up @@ -91,24 +94,32 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
" 0%| | 0/1 [00:00<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading weights from C:\\Users\\Windows\\Dropbox\\Echo Research\\CodeBase\\EchoNetDynamic-Weights\\r2plus1d_18_32_2_pretrained\n",
"cuda is available, original weights\n",
"external_test ['0X1A05DFFFCAFB253B.avi', '0X1A0A263B22CCD966.avi', '0X1A2A76BDB5B98BED.avi', '0X1A2C60147AF9FDAE.avi', '0X1A2E9496910EFF5B.avi', '0X1A3D565B371DC573.avi', '0X1A3E7BF1DFB132FB.avi', '0X1A5FAE3F9D37794E.avi', '0X1A6ACFE7B286DAFC.avi', '0X1A8D85542DBE8204.avi', '23_Apical_4_chamber_view.dcm.avi', '62_Apical_4_chamber_view.dcm.avi', '64_Apical_4_chamber_view.dcm.avi']\n"
"loading weights from /Users/akn/Drive/Developer/TTE-ML/weights/r2plus1d_18_32_2_pretrained\n",
"cuda is not available, cpu weights\n",
"external_test ['0X1A0A263B22CCD966.avi', '0X1A2A76BDB5B98BED.avi', '0X1A2C60147AF9FDAE.avi', '0X1A2E9496910EFF5B.avi']\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:10<00:00, 1.00s/it]\n",
"100%|████████████████████████████████████████████████████████| 13/13 [00:29<00:00, 2.26s/it, 3122.29 (3440.26) / 0.00]\n"
"100%|██████████| 1/1 [00:00<00:00, 4.14it/s]\n",
"100%|██████████| 4/4 [18:51<00:00, 282.84s/it, 3602.54 (3684.90) / 0.00]\n"
]
}
],
Expand Down Expand Up @@ -145,9 +156,11 @@
"#Weights = \"final_r2+1d_model_regression_EF_sgd_skip1_32frames.pth.tar\"\n",
"\n",
"\n",
"output = os.path.join(destinationFolder, \"cedars_ef_output.csv\")\n",
"output = os.path.join(destinationFolder, \"ef_output.csv\")\n",
"\n",
"ds = echonet.datasets.Echo(split = \"external_test\", external_test_location = videosFolder, crops=\"all\")\n",
"#changed crops to clips\n",
"#have to set a length or will get runtime error w/ utils.get_mean_and_stds(ds) b/c videos have different lengths\n",
"ds = echonet.datasets.Echo(split = \"external_test\", external_test_location = videosFolder, length=frames, clips=1)\n",
"print(ds.split, ds.fnames)\n",
"\n",
"mean, std = echonet.utils.get_mean_and_std(ds)\n",
Expand All @@ -159,10 +172,13 @@
" \"period\": period,\n",
" }\n",
"\n",
"ds = echonet.datasets.Echo(split = \"external_test\", external_test_location = videosFolder, **kwargs, crops=\"all\")\n",
"#changed crops to clips\n",
"#clips=\"all\" will get EF predictions for every clip of length \"frames\" with overlap\n",
"ds = echonet.datasets.Echo(split = \"external_test\", external_test_location = videosFolder, **kwargs, clips=\"all\")\n",
"\n",
"test_dataloader = torch.utils.data.DataLoader(ds, batch_size = 1, num_workers = 5, shuffle = True, pin_memory=(device.type == \"cuda\"))\n",
"loss, yhat, y = echonet.utils.video.run_epoch(model, test_dataloader, \"test\", None, device, save_all=True, blocks=25)\n",
"#changed third argument to False\n",
"loss, yhat, y = echonet.utils.video.run_epoch(model, test_dataloader, False, None, device, save_all=True, block_size=25)\n",
"\n",
"with open(output, \"w\") as g:\n",
" for (filename, pred) in zip(ds.fnames, yhat):\n",
Expand All @@ -172,26 +188,71 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
" 0%| | 0/4 [00:00<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading weights from /Users/akn/Drive/Developer/TTE-ML/weights/deeplabv3_resnet50_random\n",
"cuda is not available, cpu weights\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 4/4 [01:21<00:00, 20.29s/it]\n",
"100%|██████████| 4/4 [00:01<00:00, 2.49it/s]\n"
]
}
],
"source": [
"# Initialize and Run Segmentation model\n",
"\n",
"torch.cuda.empty_cache()\n",
"\n",
"\n",
"videosFolder = \"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\View Classification\\\\AppearsA4c\\\\Resized2\"\n",
"# videosFolder = \"C:\\\\Users\\\\Windows\\\\Drop box\\\\Echo Research\\\\CodeBase\\\\View Classification\\\\AppearsA4c\\\\Resized2\"\n",
"\n",
"def collate_fn(x):\n",
" x, f = zip(*x)\n",
" i = list(map(lambda t: t.shape[1], x))\n",
" x = torch.as_tensor(np.swapaxes(np.concatenate(x, 1), 0, 1))\n",
" return x, f, i\n",
"\n",
"dataloader = torch.utils.data.DataLoader(echonet.datasets.Echo(split=\"external_test\", external_test_location = videosFolder, target_type=[\"Filename\"], length=None, period=1, mean=mean, std=std),\n",
" batch_size=10, num_workers=0, shuffle=False, pin_memory=(device.type == \"cuda\"), collate_fn=collate_fn)\n",
"if not all([os.path.isfile(os.path.join(destinationFolder, \"labels\", os.path.splitext(f)[0] + \".npy\")) for f in dataloader.dataset.fnames]):\n",
"model = torchvision.models.segmentation.deeplabv3_resnet50(pretrained=False, aux_loss=False)\n",
"model.classifier[-1] = torch.nn.Conv2d(model.classifier[-1].in_channels, 1, kernel_size=model.classifier[-1].kernel_size)\n",
"\n",
"print(\"loading weights from \", os.path.join(DestinationForWeights, \"deeplabv3_resnet50_random\"))\n",
"\n",
"if torch.cuda.is_available():\n",
" print(\"cuda is available, original weights\")\n",
" device = torch.device(\"cuda\")\n",
" model = torch.nn.DataParallel(model)\n",
" model.to(device)\n",
" checkpoint = torch.load(os.path.join(DestinationForWeights, os.path.basename(segmentationWeightsURL)))\n",
" model.load_state_dict(checkpoint['state_dict'])\n",
"else:\n",
" print(\"cuda is not available, cpu weights\")\n",
" device = torch.device(\"cpu\")\n",
" checkpoint = torch.load(os.path.join(DestinationForWeights, os.path.basename(segmentationWeightsURL)), map_location = \"cpu\")\n",
" state_dict_cpu = {k[7:]: v for (k, v) in checkpoint['state_dict'].items()}\n",
" model.load_state_dict(state_dict_cpu)\n",
"\n",
"ds = echonet.datasets.Echo(split=\"external_test\", external_test_location = videosFolder, target_type=[\"Filename\"], length=None, period=1, mean=mean, std=std)\n",
"test_dataloader = torch.utils.data.DataLoader(ds, batch_size = 1, num_workers = 5, shuffle = False, pin_memory=(device.type == \"cuda\"), collate_fn=collate_fn)\n",
"\n",
"if not all([os.path.isfile(os.path.join(destinationFolder, \"labels\", os.path.splitext(f)[0] + \".npy\")) for f in test_dataloader.dataset.fnames]):\n",
" # Save segmentations for all frames\n",
" # Only run if missing files\n",
"\n",
Expand All @@ -200,16 +261,16 @@
" model.eval()\n",
"\n",
" with torch.no_grad():\n",
" for (x, f, i) in tqdm.tqdm(dataloader):\n",
" for (x, f, i) in tqdm.tqdm(test_dataloader):\n",
" x = x.to(device)\n",
" y = np.concatenate([model(x[i:(i + block), :, :, :])[\"out\"].detach().cpu().numpy() for i in range(0, x.shape[0], block)]).astype(np.float16)\n",
" start = 0\n",
" for (filename, offset) in zip(f, i):\n",
" np.save(os.path.join(destinationFolder, \"labels\", os.path.splitext(filename)[0]), y[start:(start + offset), 0, :, :])\n",
" start += offset\n",
" \n",
"dataloader = torch.utils.data.DataLoader(echonet.datasets.Echo(split=\"external_test\", external_test_location = videosFolder, target_type=[\"Filename\"], length=None, period=1, segmentation=os.path.join(destinationFolder, \"labels\")),\n",
" batch_size=1, num_workers=8, shuffle=False, pin_memory=False)\n",
"\n",
"dataloader = torch.utils.data.DataLoader(echonet.datasets.Echo(split=\"external_test\", external_test_location = videosFolder, target_type=[\"Filename\"], length=None, period=1),\n",
" batch_size=1, num_workers=0, shuffle=False, pin_memory=False)\n",
"if not all(os.path.isfile(os.path.join(destinationFolder, \"videos\", f)) for f in dataloader.dataset.fnames):\n",
" pathlib.Path(os.path.join(destinationFolder, \"videos\")).mkdir(parents=True, exist_ok=True)\n",
" pathlib.Path(os.path.join(destinationFolder, \"size\")).mkdir(parents=True, exist_ok=True)\n",
Expand All @@ -219,15 +280,18 @@
" for (x, filename) in tqdm.tqdm(dataloader):\n",
" x = x.numpy()\n",
" for i in range(len(filename)):\n",
" file = pathlib.Path(filename[i]).stem\n",
" y = np.load(os.path.join(destinationFolder, \"labels\", file + \".npy\"))\n",
" \n",
" img = x[i, :, :, :, :].copy()\n",
" logit = img[2, :, :, :].copy()\n",
" logit = y.copy()\n",
" img[1, :, :, :] = img[0, :, :, :]\n",
" img[2, :, :, :] = img[0, :, :, :]\n",
" img = np.concatenate((img, img), 3)\n",
" img[0, :, :, 112:] = np.maximum(255. * (logit > 0), img[0, :, :, 112:])\n",
"\n",
" img = np.concatenate((img, np.zeros_like(img)), 2)\n",
" size = (logit > 0).sum(2).sum(1)\n",
" size = (logit > 0).sum((1, 2))\n",
" \n",
" try:\n",
" trim_min = sorted(size)[round(len(size) ** 0.05)]\n",
" except:\n",
Expand Down Expand Up @@ -260,8 +324,15 @@
" img[:, x, a + int(round(115 + 100 * y)), b + int(round(x / len(size) * 200 + 10))] = 255.\n",
" if x in peaks:\n",
" img[:, :, 200:225, b + int(round(x / len(size) * 200 + 10))] = 255.\n",
" echonet.utils.savevideo(os.path.join(destinationFolder, \"videos\", filename[i]), img.astype(np.uint8), 50) "
" echonet.utils.savevideo(os.path.join(destinationFolder, \"videos\", filename[i]), img.astype(np.uint8), 50) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -280,7 +351,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down