Ejemplo n.º 1
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL models
        # load net 1
        model1 = Unet3d(in_dim=5, out_dim=2, num_filter=16)
        net1 = model1.cpu()
        net1.load_state_dict(
            torch.load('model/net_step1.pth', map_location='cpu'))
        self._model1 = net1
        # load net 2
        model2 = Unet3d_nopad(in_dim=5, out_dim=4, num_filter=32)
        net2 = model2.cpu()
        net2.load_state_dict(
            torch.load('model/net_step2.pth', map_location='cpu'))
        self._model2 = net2

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr1 = self._imageProcessor.loadAndPreprocess(
            input["t1"]["fileurl"], id="t1")
        inputAsNpArr2 = self._imageProcessor.loadAndPreprocess(
            input["t1c"]["fileurl"], id="t1c")
        inputAsNpArr3 = self._imageProcessor.loadAndPreprocess(
            input["t2"]["fileurl"], id="t2")
        inputAsNpArr4 = self._imageProcessor.loadAndPreprocess(
            input["flair"]["fileurl"], id="flair")
        # postprocessing
        print('loading done')
        affine = self._imageProcessor.returnAffine(input["t1"]["fileurl"])
        print('affine recovered')
        dataset = preprocessForNet1(inputAsNpArr1, inputAsNpArr2,
                                    inputAsNpArr3, inputAsNpArr4, affine)
        print('dataset assembled')
        print('first U-Net running')
        outNet1 = predict1(dataset, self._model1)
        print('preprocessing step 2')
        dataset = preprocessForNet2(dataset, outNet1[0])
        print('second U-Net running')
        outNet2 = predict2(dataset, self._model2)
        #outNet2 = predict2(outNet1, self._model2)
        print('done, postprocessing now')
        output = self._imageProcessor.computeOutput(outNet2[0])
        return output

    def _preprocessInputForNet(t1, t1c, t2, flair):
        pass
Ejemplo n.º 2
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json", encoding="utf-8"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model
        nclasses = 2
        self._model = Net(output_dim=nclasses)

        learning_rate = 0.001
        momentum = 0.9
        weight_decay = 0.0001

        optimizer = optim.SGD(self._model.parameters(),
                              lr=learning_rate,
                              momentum=momentum,
                              weight_decay=weight_decay)

        checkpoint = torch.load('model/model.pth.tar',
                                map_location=lambda storage, loc: storage)
        optimizer.load_state_dict(checkpoint['optimizer'])

        self._model.load_state_dict(checkpoint['state_dict'])
        self._model.eval()

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference
        results = self._model.forward(inputAsNpArr)
        # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
Ejemplo n.º 3
0
class Model(ModelBase):

    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model (change this if you are not using ONNX)
        #self._model = onnx.load('model/model.onnx')
        #self._model = sfm
    

    def infer(self, input):
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # load preprocessed input
        graph = tf.Graph()
        with graph.as_default():
            with tf.Session(graph=graph) as sess:
                tf.saved_model.loader.load(
                    sess,
                    [tag_constants.SERVING],
                    'model/',
                )
                input_uint8 = graph.get_tensor_by_name('raw_input:0')
                prediction = graph.get_tensor_by_name('pose_prediction/pose_exp_net/pose/mul:0')

                pred = sess.run(prediction, feed_dict={input_uint8: inputAsNpArr[None,:,:,:]})

        output = self._imageProcessor.computeOutput(pred)
        return output
Ejemplo n.º 4
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # # load the DL model
        # load archiecture
        json_file = open("model/architecture.json", 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        self._model = model_from_json(loaded_model_json)
        # load weights
        self._model.load_weights("model/weights.h5")
        self._graph = tf.get_default_graph()

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        inputAsNpArrFormatted = inputAsNpArr.reshape(1, 50, 50, 50, 1)
        with self._graph.as_default():
            function = K.function(
                [self._model.layers[0].input,
                 K.learning_phase()], [self._model.layers[23].output])
            # Run inference with keras
            probabilities = self._model.predict_on_batch(
                [inputAsNpArrFormatted])
            vector = function([inputAsNpArrFormatted, 0])
        # postprocess results into output
        output = self._imageProcessor.computeOutput([probabilities, vector[0]])
        return output
Ejemplo n.º 5
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model
        self._model = caffe.Net('model/model.prototxt',
                                'model/model.caffemodel', caffe.TEST)
        # load input and configure preprocessing
        self._transformer = caffe.io.Transformer(
            {'data': self._model.blobs['data'].data.shape})
        self._transformer.set_mean(
            'data',
            np.load('model/ilsvrc_2012_mean.npy').mean(1).mean(1))
        self._transformer.set_transpose('data', (2, 0, 1))
        self._transformer.set_channel_swap('data', (2, 1, 0))
        self._transformer.set_raw_scale('data', 255.0)

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with caffe
        self._model.blobs['data'].reshape(1, 3, 227, 227)
        self._model.blobs['data'].data[...] = self._transformer.preprocess(
            'data', inputAsNpArr)
        results = self._model.forward()['prob']
        # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
Ejemplo n.º 6
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # get context - cpu
        ctx = mx.cpu()
        sym, arg_params, aux_params = mx.model.load_checkpoint('model/nin', 0)
        mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
        mod.bind(for_training=False,
                 data_shapes=[('data', (1, 3, 224, 224))],
                 label_shapes=mod._label_shapes)
        mod.set_params(arg_params, aux_params, allow_missing=True)
        self._model = mod

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with mxnet
        batch = namedtuple('Batch', ['data'])
        self._model.forward(batch([inputAsNpArr]), is_train=False)
        prob = self._model.get_outputs()[0][0].asnumpy()
        # postprocess results into output
        output = self._imageProcessor.computeOutput(prob)
        return output
Ejemplo n.º 7
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # get context - cpu
        ctx = mx.cpu()
        sym, arg_params, aux_params = import_model('model/model.onnx')
        # Load module
        mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
        mod.bind(for_training=False,
                 data_shapes=[('data', (1, 3, 224, 224))],
                 label_shapes=mod._label_shapes)
        mod.set_params(arg_params,
                       aux_params,
                       allow_missing=True,
                       allow_extra=True)
        self._model = mod

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with mxnet
        batch = namedtuple('Batch', ['data'])
        self._model.forward(batch([inputAsNpArr]))
        # Take softmax to generate probabilities
        scores = mx.ndarray.softmax(self._model.get_outputs()[0][0]).asnumpy()
        # postprocess results into output
        output = self._imageProcessor.computeOutput(scores)
        return output
Ejemplo n.º 8
0
class Model(ModelBase):

    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # get context - cpu
        ctx = mx.cpu()
        image_size = (112,112)
        # Import ONNX model
        sym, arg_params, aux_params = import_model('model/model.onnx')
        # Define and binds parameters to the network
        model = mx.mod.Module(symbol=sym, context=ctx, label_names = None)
        model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))])
        model.set_params(arg_params, aux_params)
        self._model = model

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with mxnet
        input_blob = np.expand_dims(inputAsNpArr, axis=0)
        data = mx.nd.array(input_blob)
        db = mx.io.DataBatch(data=(data,))
        self._model.forward(db, is_train=False)
        embedding = self._model.get_outputs()[0].asnumpy()
        # postprocess results into output
        output = self._imageProcessor.computeOutput(embedding)
        return output.tolist()
Ejemplo n.º 9
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model (change this if you are not using ONNX)
        target_label_names = [
            'necrosis', 'contrast_enhancing', 'core', 'tumor', 'brain'
        ]
        net1 = UNET_3D_to_2D(0,
                             channels_in=4,
                             channels=128,
                             growth_rate=12,
                             dilated_layers=[6, 6, 6, 6],
                             output_channels=len(target_label_names))
        net2 = UNET_3D_to_2D(1,
                             channels_in=4,
                             channels=128,
                             growth_rate=12,
                             dilated_layers=[6, 6, 6],
                             output_channels=len(target_label_names))
        net1 = net1.cuda()
        net2 = net2.cuda()
        load_checkpoint(net1, 'model/checkpoint.pth.tar')
        load_checkpoint(net2, 'model/checkpoint_2.pth.tar')
        self._model1 = net1
        self._model2 = net2

    def infer(self, input):
        # load preprocessed input
        # CAUTION: these custum functions return the complete nib.Nifti1Image!
        t1 = self._imageProcessor.loadAndPreprocess(input["t1"]["fileurl"],
                                                    id="t1")
        t1c = self._imageProcessor.loadAndPreprocess(input["t1c"]["fileurl"],
                                                     id="t1c")
        t2 = self._imageProcessor.loadAndPreprocess(input["t2"]["fileurl"],
                                                    id="t2")
        flair = self._imageProcessor.loadAndPreprocess(
            input["flair"]["fileurl"], id="flair")
        output = segment(flair, t1, t2, t1c, self._model1, self._model2)
        # compute output
        output = self._imageProcessor.computeOutput(output)
        return output
Ejemplo n.º 10
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model within keras (change thi if you are not using keras)
        self._model = load_model('model/model.h5')

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with kreas (change this if you are not using keras)
        results = self._model.predict(
            inputAsNpArr)  # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
Ejemplo n.º 11
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model (change this if you are not using ONNX)
        self._model = onnx.load('model/model.onnx')

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with caffe2 (change this if you are using a different DL framework)
        results = caffe2.python.onnx.backend.run_model(self._model,
                                                       [inputAsNpArr])
        # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
Ejemplo n.º 12
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model
        self._model = Xception()
        self._model.load_weights('model/model.h5')
        self._model._make_predict_function()

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference with caffe2
        results = self._model.predict(inputAsNpArr)
        # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
class Model(ModelBase):

    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model
        self._model = C.Function.load("model/model.onnx", device=C.device.cpu(), format=C.ModelFormat.ONNX)


    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # run inference
        results = self._model.eval({self._model.arguments[0]:[inputAsNpArr]})
        # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
Ejemplo n.º 14
0
class Model(ModelBase):
    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # load the DL model
        self._model = caffe.Net("model/step1/step1_deploy.prototxt",
                                "model/step1/step1_weights.caffemodel",
                                caffe.TEST)

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # Run inference
        self._model.blobs['data'].data[...] = inputAsNpArr
        results = self._model.forward()
        # postprocess results into output
        output = self._imageProcessor.computeOutput(results)
        return output
Ejemplo n.º 15
0
class Model(ModelBase):

    def __init__(self):
        # load config file
        config = json.load(open("model/config.json"))
        # get the image processor
        self._imageProcessor = ImageProcessor(config)
        # get context - cpu
        self._ctx = mx.cpu()

    def infer(self, input):
        # load preprocessed input
        inputAsNpArr = self._imageProcessor.loadAndPreprocess(input)
        # # Run inference
        im =  self._imageProcessor._im
        result_shape = self._imageProcessor._result_shape
        # model loading needs to happen after input has been prosessed as
        # instantianting it requires the size of the input image.
        model = get_model(self._ctx, 'model/model.onnx', im)
        conf,result_img,blended_img,raw = predict(inputAsNpArr, result_shape, model, im)
        return np.array(result_img)