File size: 1,319 Bytes
362a9f8
 
6310170
9b6cb7b
95addbb
 
362a9f8
 
 
9b6cb7b
95addbb
 
 
 
 
 
 
 
 
 
 
 
362a9f8
 
 
 
3120fa9
362a9f8
 
 
6faad95
362a9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gdown
import logging as log
from .unet3d import UNet3D
from .fetch import download
import os
from tensorflow.python.keras.models import load_model


def get_model(output):
    url = "https://github.com/andreped/livermask/releases/download/trained-models-v1/model.h5"
    
    if os.path.exists(output):
        try:
            model = load_model(output, compile=False)
            del model
        except OSError as e:
            print(e)
            print("Model failed to load. Trying to delete old model and redownload. This might take a while...")
            os.remove(output)
            download(url, output)
    else:
        download(url, output)


def get_vessel_model(output):
    url = "https://drive.google.com/uc?id=1-8VNoRmIeiF1uIuWBqmZXz_6dIQFSAxN"
    gdown.cached_download(url, output)


def load_vessel_model(path, cpu):
    import chainer
    unet = UNet3D(num_of_label=2)
    chainer.serializers.load_npz(path, unet)
    if not cpu:
        chainer.cuda.get_device_from_id(0).use()
        unet.to_gpu()
    xp = unet.xp
    return unet, xp


def verboseHandler(verbose):
    if verbose:
        log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
        log.info("Verbose output.")
    else:
        log.basicConfig(format="%(levelname)s: %(message)s")
    return log