Пример #1
0
def MAELossFlat(*args, axis: int = -1, floatify: bool = True, **kwargs):
    """Same as nn.L1Loss, but flattens input and target."""
    return FlattenedLoss(nn.L1Loss,
                         *args,
                         axis=axis,
                         floatify=floatify,
                         is_2d=False,
                         **kwargs)
Пример #2
0
def binary_cross_entropy_flat(*args, axis: int = -1, **kwargs):
    class BinaryCrossEntropyLoss(CrossEntropyLoss):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)

        def forward(self, input, target):
            return to_binary_entropy(super().forward(input, target))

    return FlattenedLoss(BinaryCrossEntropyLoss, *args, axis=axis, **kwargs)
Пример #3
0
    def __init__(self, path, transforms, params):
        super(IITPetClassification, self).__init__()

        self.params = params
        self.path = path
        self.transforms = transforms
        self.loss_func = FlattenedLoss(nn.CrossEntropyLoss)

        img_names = set([
            re.findall(IMG_NAME, x.name.lower())[0] for x in self.path.ls()
            if x.name.lower().endswith('.jpg')
        ])
        self.labels = {lbl: i for i, lbl in enumerate(img_names)}

        self.model = CNNPretrainedModel(resnet34, len(self.labels))
Пример #4
0
    def __init__(self, transforms, params):
        super(ChestXRayDiagnosis, self).__init__()
        # not the best model...
        # Model, no_classes,
        #self.model = create_cnn_model(resnet34, 2, pretrained=True, ps=0.5, concat_pool=True)

        self.params = params
        self.resnet34 = CNNPretrainedModel(resnet34, 2)

        #self.model10 = CNNPretrainedModel(resnet34, 2)
        #self.cnn_model = self.model10.get_cnn_model()

        self.path = Path("/media/disk4tb/datasets/medical/chest/PC")
        df_clean = pd.read_csv(self.path / 'cleaned_padchest.csv')

        diagnosis = [
            'pneumonia', 'atypical pneumonia', 'tuberculosis',
            'tuberculosis sequelae', 'lung metastasis',
            'lymphangitis carcinomatosa', 'lepidic adenocarcinoma',
            'pulmonary fibrosis', 'post radiotherapy changes',
            'asbestosis signs', 'emphysema', 'COPD signs',
            'heart insufficiency', 'respiratory distress',
            'pulmonary hypertension', 'pulmonary artery hypertension',
            'pulmonary venous hypertension', 'pulmonary edema',
            'bone metastasis'
        ]

        self.transforms = transforms
        self.disease_labels = {}
        for i, row in df_clean.iterrows():
            lbls = set(eval(row.Labels))
            gender = np.array([0 if row.PatientSex_DICOM == 'F' else 1])
            if lbls.intersection(diagnosis):
                self.disease_labels[row.ImageID] = [np.array([1]), gender]
            elif len(lbls) == 1 and 'normal' in lbls:
                self.disease_labels[row.ImageID] = [np.array([0]), gender]
            else:
                pass

        allimages = set(list(self.disease_labels.keys()))
        with open(self.path / "train_val_test.pkl", "rb") as fp:
            _, val_, _ = pickle.load(fp)

        val_ = set(val_)
        self.train_set = allimages - val_
        self.val_set = allimages.intersection(val_)
        self.classes = {0: 'normal', 1: 'disease'}
        self.loss_func = FlattenedLoss(nn.CrossEntropyLoss)
    def __init__(self, *args, axis: int = -1, dice_share: float = 0.5, **kwargs):

        self.pl = FlattenedLoss(nn.CrossEntropyLoss, *args, axis=axis, **kwargs)
        self.dl = dice_loss_glob(smooth=1.)

        self.dice_share = dice_share
Пример #6
0
        self.softmax = nn.Softmax(1)

    def forward(self, input, target):
        prediction = self.softmax(input)

        if USE_GPU:
            one_hot = torch.sparse.torch.eye(2).cuda().index_select(
                0, target.long())
        else:
            one_hot = torch.sparse.torch.eye(2).index_select(0, target.long())

        ref_vol = torch.sum(one_hot, 0)

        seg_vol = torch.sum(prediction, 0)
        intersect = torch.sum(one_hot * prediction, 0)

        weights = torch.reciprocal(ref_vol**2)
        weights[weights == float("Inf")] = 0

        generalized_dice_numerator = 2 * torch.sum(weights * intersect)
        generalized_dice_denominator = torch.sum(
            weights * torch.max(seg_vol + ref_vol, torch.ones_like(weights)))
        generalized_dice_score = (generalized_dice_numerator /
                                  generalized_dice_denominator)

        generalized_dice_score[torch.isnan(generalized_dice_score)] = 1.0
        return 1 - generalized_dice_score


generalized_dice_loss = FlattenedLoss(GeneralizedDiceLoss, axis=1)
Пример #7
0
    pooling_dim = 4096

EmbeddedFeatureWrapper.__module__ = "model"
L2NormalizedLinearLayer.__module__ = "model"
# Add new layers
modules.append(
    EmbeddedFeatureWrapper(input_dim=pooling_dim,
                           output_dim=EMBEDDING_DIM,
                           dropout=DROPOUT))
modules.append(
    L2NormalizedLinearLayer(input_dim=EMBEDDING_DIM,
                            output_dim=len(data_finetune.classes)))
learn.model[1] = nn.Sequential(*modules)

# Create new learner object since otherwise the new layers are not updated during backprop
learn = fastai.vision.Learner(data_finetune, learn.model)

# Update loss function
NormSoftmaxLoss.__module__ = "model"

learn.loss_func = FlattenedLoss(NormSoftmaxLoss)

# Edited model head
print(learn.model[1])

learn.fit_one_cycle(EPOCHS_HEAD, HEAD_LEARNING_RATE)
learn.unfreeze()
learn.fit_one_cycle(EPOCHS_BODY, BODY_LEARNING_RATE)

learn.export(file=Path("/app/similarity_model.pkl"))