File size: 1,594 Bytes
e5a19d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
from matplotlib import pyplot as plt


def get_index_to_class_mapping():
    indices = np.arange(1, 16)
    class_names = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion', 'Emphysema', 'Fibrosis',
                   'Hernia', 'Infiltration', 'Mass', 'No Finding', 'Nodule', 'Pleural Thickening', 'Pneumonia',
                   'Pneumothorax']
    mapping = dict(zip(indices, class_names))
    return mapping


def load_classifier_from_file(ckpt_file, location='cpu'):
    checkpoint = torch.load(ckpt_file, map_location=location)
    model = checkpoint['model']
    return model


def transform_pil_to_tensor(pil_image, device='cpu'):
    # ImageNet mean and std
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]

    # transformation to be applied
    transform = transforms.Compose([
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize(mean, std)
        ])

    tensor = transform(pil_image).to(device)
    return tensor


def noise_process(numpy_image, steps=10):
    noisy_image_list = []
    noisy_image = numpy_image
    noisy_image_list.append(noisy_image)

    for step in range(steps):
        noise = 255 * np.random.normal(0, (step+1) * 0.1, numpy_image.size).reshape(numpy_image.shape)
        noisy_image = noisy_image + noise
        noisy_image_list.append(noisy_image)
        plt.imshow(noisy_image, cmap='gray')
        plt.show()

    return noisy_image_list