main_objected module

main_objected.build_dataset(input_csv: pandas.core.frame.DataFrame, root_path: str, column='s100', batch_size: int = 1, transform: bool = False, shuffle: bool = False, num_workers: int = 0)

Dataset creation

Parameters
  • input_csv (pd.DataFrame) – Input csv file that contains the names, age, sex, tumor location and targets

  • root_path (str) – root path for images

  • column (str, optional) – Column of the target. Defaults to ‘s100’.

  • batch_size (int, optional) – It is hard to collate different sized of slices hence use 1 for batch size. Defaults to 1.

  • transform (bool, optional) – Augmentation flag. Defaults to False.

  • shuffle (bool, optional) – Shuffle flag. Defaults to False.

  • num_workers (int, optional) – Count of data workers. Defaults to 0.

Returns

dataloader

Return type

[torch.data.dataloader]

main_objected.build_optimizer(network, optimizer: str, learning_rate: float)

Optimizer creation

Parameters
  • network (torch.model) – Network to train

  • optimizer (str) – optimizer type one of [sgd,adam,rmsprop]

  • learning_rate (float) – float learning rate

Returns

optimizer

Return type

[torch.optim.optimizer]

class main_objected.classifier(in_features, out_features)

Bases: torch.nn.modules.module.Module

forward(x, features)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
main_objected.count_parameters(model)

Counts the number of parameters in a model.

Parameters

model (torch.model) – Model to count parameters

Returns

Count of all trainable parameters in the model

Return type

[int]

main_objected.create_model(model_name, classes: int = 2, pretrained: bool = True, requires_grad: bool = True, in_channels: int = 1, custom_pretrained=None, feature_extractor=True)

Feature extractor creation

Parameters
  • model_name (str) – One of [resnetx,densenetx,vggx,inceptionx]

  • classes (int, optional) – Output of the classes. Defaults to 2. It is unimportant for the model if you use it as feature extractor because we will change the last classifier by IdentityNet()

  • pretrained (bool, optional) – Pretained flag. Defaults to True.

  • requires_grad (bool, optional) – Freeze flag. Defaults to True.

  • in_channels (int, optional) – Input channel(s) of the feature extractor . Defaults to 1.

  • custom_pretrained (str, optional) – If you have pretrained networks to use to train classifier give the path. Defaults to None.

  • feature_extractor (bool, optional) – If it is true classes become not important. If no please consider number of classes. Defaults to True.

Returns

Feature extractor if feature_extractor is true, else model with the out classes and pretrained weights

Return type

[torch.model]

main_objected.create_the_slices(image, label, slices: int = 5, dim: int = 0)

Splitting raw input image into slices

Parameters
  • image (torch.tensor) – Input image

  • label (torch.tensor) – Target label

  • slices (int, optional) – Split size. Defaults to 5.

  • dim (int, optional) – Dimension of splitting. Defaults to 0.

Returns

list of tensor images, [float]: list of tensor labels

Return type

[float]

main_objected.read_csv(path: str, column: str = 's100')

Read csv file and dropping the nan values of target column

Parameters
  • path (str) – Path to csv file

  • column (str, optional) – Column to drop nans. Defaults to ‘IDHConsensus’.

Returns

Dataframe that cleaned from nan vals of target column

Return type

[pandas.DataFrame]

main_objected.train(config=None)
main_objected.train_epoch(network, classifier, loader, val_loader, optimizer, optimizer_classifier, criterion, device: str, slices: int)

Training loop for the proposed model

Parameters
  • network (torch.model) – torch pre-trained model aka feature extractor (resnet34)

  • classifier (torch.model) – Classifier model for the extracted features

  • loader (dataloader) – Dataloader for the training set

  • val_loader (dataloader) – Dataloader for the validation set

  • optimizer (torch.optim.optimizer) – Optimizer for the feature extractor

  • optimizer_classifier (torch.optimizer) – Optimizer for the classifier

  • criterion (torch.losses) – loss function

  • device (str) – Device to use for training

  • slices (int) – Number of slices to use for the training

Returns

Cumulative loss for the training set [float]: Accuracy for the training set [float]: Cumulative loss for the validation set [float]: Accuracy for the validation set [float]: Accuracy for the training set slice-wise [float]: Accuracy for the training set majority voting [float]: Accuracy for the training set single-slice positivity [int]: Number of class 0 [int]: Number of class 1 [int]: Number of class 0 in the evaluation set [int]: Number of class 1 in the evaluation set

Return type

[float]