File size: 2,019 Bytes
eee8e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import mlflow
import pandas as pd
import joblib
import os

from fastai.data.block import MultiCategoryBlock, RandomSplitter, DataBlock, CategoryBlock
from fastai.vision.data import *
from fastai.vision.learner import *
from fastai.vision.all import *
import pathlib
plt = platform.system()
if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath


lesion_model = None
pipe_cancer = None


def f_load_cnn_model():
    global lesion_model
    logged_model = os.path.join("mole_models", "ed52a28a7b504ff7ba851c850221d1dd")
    lesion_model = mlflow.fastai.load_model(logged_model)
    lesion_model.cbs.remove(lesion_model.cbs[6])
    lesion_model.cbs.remove(lesion_model.cbs[4])
    lesion_model.cbs.remove(lesion_model.cbs[3])


def get_image_files(df):
    # df = df.assign(path=lambda x: working_dataset_path + x["path"])
    return df

def get_x(df):
    return df["path"]

def get_y(df):
    return df["dx"]


def f_create_df_with_files_input(file_path):
    list_files = [file_path]
    list_labels = [""]
    df = pd.DataFrame(data={"path": list_files, "dx": list_labels})
    return df


def f_predict_cnn_with_tta(file_path):
    df = f_create_df_with_files_input(file_path)
    dl = lesion_model.dls.test_dl(df)
    dl.after_item = Pipeline([ToTensor, Resize(700, method=ResizeMethod.Crop), RandomResizedCrop(350)])
    pred, _targ = lesion_model.tta(dl=dl, n=4, use_max=False)
    return pred.tolist()[0]


def f_predict_cnn_simple(file_path):
    preds = lesion_model.predict(file_path)
    return preds[2].tolist()


def f_load_cancer_classifier():
    global pipe_cancer
    pipe_cancer = joblib.load(os.path.join("mole_models", "pipeline.rf_classifier_mole"))

def f_predict_cancer(preds, age, sex, localization):
    df = pd.DataFrame(data=[preds], columns=lesion_model.dls.vocab)
    df["age"] = age
    df["sex"] = sex
    df["localization"] = localization
    df["label"] = ""
    label = pipe_cancer.predict(df)[0]
    label = "Possibly suspicious" if label=="cancer" else "benign"
    return label