|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import context\n", |
| 10 | + "from utils.datasets import QuarterFastMRI, MNISTTrainDataset, FastMRIBrainTrain\n", |
| 11 | + "from torch.utils.data import DataLoader\n", |
| 12 | + "import matplotlib.pyplot as plt\n", |
| 13 | + "import torch\n", |
| 14 | + "import os\n", |
| 15 | + "import h5py\n", |
| 16 | + "import torchvision\n", |
| 17 | + "from torchvision.transforms import Normalize\n", |
| 18 | + "from torchvision.io import read_image\n", |
| 19 | + "from torchvision.utils import make_grid\n", |
| 20 | + "import numpy as np\n", |
| 21 | + "from utils.helpers import complex_to_2channelfloat" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 2, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "root = \"/itet-stor/peerli/bmicdatasets-originals/Originals/fastMRI/brain/multicoil_train\"\n", |
| 31 | + "\n", |
| 32 | + "h5_files = [os.path.join(root, elem) for elem in sorted(os.listdir(root))]\n", |
| 33 | + "imgs = []\n", |
| 34 | + "for file_name in h5_files:\n", |
| 35 | + " file = h5py.File(file_name, 'r')\n", |
| 36 | + " slices = file[\"reconstruction_rss\"].shape[0]\n", |
| 37 | + " for i in range(slices):\n", |
| 38 | + " imgs.append({\"file_name\":file_name, \"index\":i})" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 22, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "index = 100\n", |
| 48 | + "\n", |
| 49 | + "file_name = imgs[index][\"file_name\"]\n", |
| 50 | + "index = imgs[index][\"index\"]\n", |
| 51 | + "file = h5py.File(file_name, 'r')\n", |
| 52 | + "img = torch.tensor(np.array(file[\"kspace\"]))\n", |
| 53 | + "img = complex_to_2channelfloat(img)\n", |
| 54 | + "file.close()" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 32, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "data": { |
| 64 | + "text/plain": [ |
| 65 | + "torch.Size([10, 20, 40, 30])" |
| 66 | + ] |
| 67 | + }, |
| 68 | + "execution_count": 32, |
| 69 | + "metadata": {}, |
| 70 | + "output_type": "execute_result" |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "x = torch.randn(10,20,30,40)\n", |
| 75 | + "x.permute(*[i for i in range(x.dim()-2)],-1,-2).shape" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 7, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "test = torch.zeros(16, 20, 2, 640, 320)\n", |
| 85 | + "for i in range(16):\n", |
| 86 | + " test[i] = i" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 15, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "orig_shape = test.shape\n", |
| 96 | + "test = test.view(-1, *orig_shape[-3:])\n", |
| 97 | + "test = test.view(*orig_shape)" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 17, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [ |
| 105 | + { |
| 106 | + "name": "stdout", |
| 107 | + "output_type": "stream", |
| 108 | + "text": [ |
| 109 | + "tensor([])\n", |
| 110 | + "tensor([])\n", |
| 111 | + "tensor([])\n", |
| 112 | + "tensor([])\n", |
| 113 | + "tensor([])\n", |
| 114 | + "tensor([])\n", |
| 115 | + "tensor([])\n", |
| 116 | + "tensor([])\n", |
| 117 | + "tensor([])\n", |
| 118 | + "tensor([])\n", |
| 119 | + "tensor([])\n", |
| 120 | + "tensor([])\n", |
| 121 | + "tensor([])\n", |
| 122 | + "tensor([])\n", |
| 123 | + "tensor([])\n", |
| 124 | + "tensor([])\n" |
| 125 | + ] |
| 126 | + } |
| 127 | + ], |
| 128 | + "source": [ |
| 129 | + "for i in range(16):\n", |
| 130 | + " print(test[i][test[i]!=i])" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": null, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [] |
| 139 | + } |
| 140 | + ], |
| 141 | + "metadata": { |
| 142 | + "kernelspec": { |
| 143 | + "display_name": "liotorch", |
| 144 | + "language": "python", |
| 145 | + "name": "python3" |
| 146 | + }, |
| 147 | + "language_info": { |
| 148 | + "codemirror_mode": { |
| 149 | + "name": "ipython", |
| 150 | + "version": 3 |
| 151 | + }, |
| 152 | + "file_extension": ".py", |
| 153 | + "mimetype": "text/x-python", |
| 154 | + "name": "python", |
| 155 | + "nbconvert_exporter": "python", |
| 156 | + "pygments_lexer": "ipython3", |
| 157 | + "version": "3.11.5" |
| 158 | + } |
| 159 | + }, |
| 160 | + "nbformat": 4, |
| 161 | + "nbformat_minor": 2 |
| 162 | +} |
0 commit comments