text
stringlengths
1
93.6k
dir_objname = d.lstrip("/")
else:
dir_objname = "_rootPath"
locals()[dir_objname] = PathRepository(d)
domain_name = tldextract.extract(u).domain
locals()[domain_name] = Query(u, d, locals()[dir_objname])
locals()[domain_name].manipulateRequest()
argument = Arguments(args.url, args.urllist, args.dir, args.dirlist)
program = Program(argument.return_urls(), argument.return_dirs())
program.initialise()
# <FILESEP>
import torch
from torch import Tensor
import torch.nn as nn
import torchvision.models as models
from einops import rearrange, reduce, repeat
from einops.layers.torch import Rearrange, Reduce
import torch.nn.functional as F
from torchsummary import summary
class CNN3DBlock(nn.Module):
'''
To obtain 3D representation features, we apply 3D CNN block to the MRI image
I ∈ R(L x W x H) where image length L, width W and height H are all the same.
X ∈ R(C3dxLxWxH) where X = D3d(I) Eq. (1)
Ref: 3.2. 3D Convolutional Neural Network Block
'''
def __init__(self, in_channels, out_channels):
super(CNN3DBlock, self).__init__()
# 5 x 5 x 5 3D CNN
self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=5,
stride=1, padding=2)
# Batch Normalization
self.bn1 = nn.BatchNorm3d(out_channels)
# ReLU
self.relu1 = nn.ReLU(inplace=True)
# 5 x 5 x 5 3D CNN
self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=5,
stride=1, padding=2)
# Batch Normalization
self.bn2 = nn.BatchNorm3d(out_channels)
# ReLU
self.relu2 = nn.ReLU(inplace=True)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu2(out)
return out
class MultiPlane_MultiSlice_Extract_Project(nn.Module):
'''
The multi-plane and multi-slice image features extraction from the 3D
representation features X and applying 2D CNN followed by Non-Linear
Projection
N = length = width = height based on the mentioned input size in the paper
Ref: 3.3. Extraction of Multi-plane, Multi slice images and
3.4. 2D Convolutional Neural Network Block
'''
def __init__(self, out_channels: int):
super(MultiPlane_MultiSlice_Extract_Project, self).__init__()
# 2D CNN part
# Load the pre-trained ResNet-18 model and Extract the global average pooling layer
self.CNN_2D = models.resnet50(weights=True)
self.CNN_2D.conv1 = nn.Conv2d(out_channels,64,kernel_size=7,stride=2,padding=3,bias=False)
self.CNN_2D.fc = nn.Identity()
# Non - Linear Projection block
self.non_linear_proj = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(),
nn.Linear(512, 256)
)
def forward(self, input_tensor):
B, C, D, H, W = input_tensor.shape
# Extract coronal features
coronal_slices = torch.split(input_tensor, 1, dim=2) # This gives us a tuple of length 128, where each element has shape (batch_size, channels, 1, width, height)
Ecor = torch.cat(coronal_slices, dim=2) # lets concatenate along dimension 2 to get the desired output shape for Ecor: R^C3d×N×W×H.
saggital_slices = torch.split(input_tensor.clone(), 1, dim = 3) # This gives us a tuple of length 128, where each element has shape (batch_size, channels, length, 1, height)
Esag = torch.cat(saggital_slices, dim = 3) # lets concatenate along dimension 3 to get the desired output shape for Ecor: R^C3d×L×N×H.
axial_slices = torch.split(input_tensor.clone(), 1, dim = 4) # This gives us a tuple of length 128, where each element has shape (batch_size, channels, length, width, 1)
Eax = torch.cat(axial_slices, dim = 4) # lets concatenate along dimension 3 to get the desired output shape for Ecor: R^C3d×L×W×N.
# Lets calculate S using E for X