Beispiel #1
0
def predict_image(image: bytes = File(
    ...,
    description=
    "Image sent to model for class prediction - required as binary.",
)):
    model = load_learner(ABSOLUTE_MODEL_PATH)
    pred = model.predict(image)
    return {"predicted_class": pred[0], "likelihood": max(pred[2])}
Beispiel #2
0
def get_learner():
    log.info('Loading CNN model...')
    device = torch.device('cuda')
    learn = load_learner(
        path='',
        file=
        'data/05-03-2020-17_37_43_PROJ_9011_LR_0.0001_BS_30_N_unet_resnet_ATT_False_MODEL_resnet34unetanneal_EXPORT_37.pth',
        device=device)

    return learn
Beispiel #3
0
from PIL import Image
from torchvision import transforms
import torch
import numpy as np
import fastai.vision as fv
import fastai.basics as fai

to_pil = transforms.ToPILImage()
Learn = fai.load_learner("./Models", "HairLearner.pkl")


def Forward(inputImgName: str, Color):
    outputImgName = inputImgName.with_name(str(inputImgName.stem) + "-seg.png")

    Img = fv.open_image(inputImgName)
    originalSize = Img.size
    Img = Img.resize(500)
    Res = Learn.predict(Img)[0]

    # Colorization
    Mask = (Res.data == 255)
    R, G, B, A = [
        torch.zeros((1, 500, 500), dtype=torch.uint8) for _ in range(4)
    ]
    R[Mask], G[Mask], B[Mask] = Color
    A[Mask] = 255
    ColorMask = fv.Image(torch.cat([R, G, B, A]))

    Pil_Img = to_pil(ColorMask.data.detach().cpu().type(torch.ByteTensor))
    Pil_Img = Pil_Img.resize(originalSize[::-1])
    Pil_Img.save(outputImgName)
Beispiel #4
0
def test_for_beads(folder_images='/media/adrian/OMENDATA/data/beads/',
                   folder_models="data/*TORCH*"):
    from pathvalidate import sanitize_filename
    from pytoolbox.data.load_save import pickle_save

    all_results = []
    model_nb = 0
    list_models = glob.glob(folder_models)

    for model in list_models:
        print('Model {} {}/{}'.format(model, model_nb, len(list_models)))
        learn = load_learner(path='', file=model)
        learn.callbacks = []
        learn.callback_fns = []
        folders = glob.glob(folder_images + "*")
        for name in folders:
            if '.bsh' in name:
                continue
            if '115' in name:
                continue
            array = name.split('/')[-1].split('_')
            exp = float(array[1][:-2])
            pos = float(array[2])
            results = []
            input_img = []
            filename = glob.glob(name + "/Default/*.tif")
            for file in filename:
                img = grayloader(file)
                input_img.append(img.data.cpu().numpy())
                ress = test_unet_without_gt(learn=learn,
                                            picture_input=img,
                                            downsample=1,
                                            picture=False)[0].transpose(
                                                1, 2, 0)
                results.append(ress.reshape(-1, 4))
            all_results.append({
                'model': model,
                'exp': exp,
                'pos': pos,
                'results': results
            })
            input_img = np.asarray(input_img)
            res = np.asarray(results)
            res = res.reshape(
                (res.shape[0], ress.shape[0], ress.shape[1], ress.shape[2]))
            plt.figure()
            valid_mask = (res[:, :, :, -1] > 0.5)[:, :, :, np.newaxis]
            valid_mask = np.repeat(valid_mask, 3, axis=3)
            mean_ = np.ma.array(res[:, :, :, :-1],
                                mask=valid_mask).mean(axis=0)
            ax = plt.subplot(131)
            ax.set_title("Mean {:.5f}".format(mean_.mean()))
            plt.imshow(mean_)

            x_position, y_position = np.meshgrid(
                np.arange(0, ress.shape[0], 1), np.arange(0, ress.shape[1], 1))

            ax.quiver(x_position, y_position, -mean_[:, :, 0], mean_[:, :, 1])

            #plt.title('mean')

            ax = plt.subplot(132)
            std_ = res[:, :, :, :-1].std(axis=0)

            plt.imshow(std_)
            ax.set_title("std {:.4f}".format(std_.std()))

            ax = plt.subplot(133)
            plt.imshow(img.data.cpu().numpy()[0].transpose((1, 2, 0)))
            ax.set_title("Input {}".format(name))

            plt.suptitle('model {}'.format(model))

            import pyqtgraph as pg
            pg.image(input_img.transpose((0, 2, 3, 1)))
            pg.image(res[:, :, :, :-1])
            pg.image(res[:, :, :, -1])

            plt.show()

            model_nb += 1

    filesave = '{}_beads_folder_{}.pkl'.format(
        datetime.now(), sanitize_filename(folder_images))
    pickle_save(filesave, all_results, compressed=False)
    return filesave
Beispiel #5
0
def test_for_moving_grids(folder_images, folder_models="data/*TORCH*"):
    from pathvalidate import sanitize_filename

    from pytoolbox.data.load_save import read_tiff
    folders = glob.glob(folder_images + "x*")
    data = {}
    filenames = {}
    isfake = False
    for name in folders:
        if '.bsh' in name:
            continue
        array = name.split('/')[-1].split('_')
        x = float(array[1])
        y = float(array[3])
        z = float(array[5])
        if x < 1.0 and y < 1.0 and z < 1.0:
            continue
        z_position = float(array[7])
        exp = float(array[9])

        if exp > 80.0:
            continue

        filename = glob.glob(name + "/*ome.tif")
        if len(filename) == 0:
            continue
        else:
            filename = filename[0]
        filename2 = filename[:-8] + '_projection.tif'
        print('Reading : {}'.format(filename))
        if not os.path.exists(filename2):
            image = read_tiff(filename)
            print('Size = {}'.format(image.shape))

            if 'fake' in folder_images:
                isfake = True
                image = image[2:-2]
                image = np.mean(image, axis=0).astype(np.int16)
            else:
                num_images = image.shape[0]
                if num_images is 1:
                    continue

                if num_images is 3 or num_images is 4:
                    idx = 1
                elif num_images is 5:
                    idx = 2
                elif num_images is 8 or num_images is 9:
                    idx = 4
                else:
                    idx = num_images // 2
                image = image[idx].astype(np.int16)

            io.imsave(filename2, image)
        if exp not in data.keys():
            data[exp] = []
            filenames[exp] = []

        data[exp].append([x, y, z, z_position])
        filenames[exp].append(filename2)

    from pytoolbox.data.load_save import pickle_save

    list_models = glob.glob(folder_models)
    feature_names = ['x', 'y', 'z', 'z_position', 'valid']

    all_results = []
    model_nb = 0
    for model in list_models:
        print('Model {} {}/{}'.format(model, model_nb, len(list_models)))
        learn = load_learner(path='', file=model)
        learn.callbacks = []
        learn.callback_fns = []
        for exp, content in data.items():
            results = []
            for image in filenames[exp]:
                img = grayloader(image)
                results.append(
                    test_unet_without_gt(learn=learn,
                                         picture_input=img,
                                         picture=True)[0].transpose(
                                             1, 2, 0).reshape(784, 4))
            labels = data[exp]
            all_results.append({
                'model': model,
                'exp': exp,
                'results': results,
                'labels': labels
            })

        model_nb += 1
    filesave = '{}_fake_{}_get_grid_model_{}_folder_{}.pkl'.format(
        datetime.now(), isfake, sanitize_filename(model),
        sanitize_filename(folder_images))
    pickle_save(filesave, all_results, compressed=False)
    return filesave
Beispiel #6
0
def test_unet(modelname, picturenames, withdata=True, picture=False):
    #model = torch.load(modelname)
    list_images_df = pd.DataFrame(picturenames)

    src = (MyImageImageList.from_df(list_images_df, path='/').split_none())

    data_gen = get_data(bs=4, size=224, src=src)
    data_gen.ignore_empty = True

    len_data = len(data_gen.train_ds)
    learn = load_learner(path='', file=modelname)

    #model.eval()
    # for child in model.children():
    #   if type(child) == nn.BatchNorm2d:
    #       child.track_running_stats = False

    all_outputs = []
    all_labels = []
    for picture_i in range(len_data):
        #print('Image {}'.format(picture_i))
        data = data_gen.train_ds[picture_i]
        image = data[0]
        mask = data[1].data
        #filename_mask = picturename.replace('.png', '_mask.png')
        #filename_pickle = picturename.replace('.png', '_mask.npy')
        #ext_data = torch.Tensor(np.load(filename_pickle))
        #mask = grayloader(filename_mask, onedim=True)[0]
        #mask *= 255
        #ext_data = torch.ones((mask.shape[0], mask.shape[1])) * ext_data
        #ext_data.unsqueeze_(-1)
        #mask = torch.cat([mask, ext_data], dim=2)
        #mask = np.transpose(mask, (2, 0, 1))

        output = learn.predict(image)[1]

        all_outputs.append(output)
        all_labels.append(mask)

        if picture:
            import matplotlib.pyplot as plt
            import matplotlib
            plt.rc('text', usetex=True)
            font = {'family': 'serif', 'weight': 'normal', 'size': 22}
            matplotlib.rc('font', **font)

            f, axarr = plt.subplots(1, 3, figsize=(12, 6))
            image = image.data.numpy()
            image = np.transpose(image[:, :, :], (1, 2, 0))
            axarr[0].imshow(image)
            axarr[0].set_title('Input image')
            axarr[0].axis('off')

            mask2 = mask.data.numpy().astype(float)[:-2, :, :]
            axarr[1].imshow(np.transpose(mask2, (1, 2, 0)))
            axarr[1].set_title('Ground truth')
            axarr[1].axis('off')
            aa = output[:-2, :, :].data.numpy()

            axarr[2].imshow(np.transpose(aa, (1, 2, 0))[:, :, [1, 0, 2]])
            axarr[2].set_title('Prediction')
            axarr[2].axis('off')

            plt.show()
    a = torch.stack(all_outputs)
    b = torch.stack(all_labels)
    return a, b
Beispiel #7
0
from fastai.vision.utils import PILImage
from flask_cors import CORS
import json

app = Flask(__name__)
CORS(app)


@app.route('/easter-egg')
def egg():
    return 'You found an easter egg!🚀'


# load learner/model
path = Path('./dl_model/')
learn = load_learner(fname=path / 'export.pkl')


def predict_single(im):
    # predicts single inputed user image
    pred, pred_idx, probs = learn.predict(im)
    result = {
        'label': pred,
        'probability': f'{probs[pred_idx]:.04f}',
    }
    return json.dumps(result)


import torchvision.transforms as tfms

Beispiel #8
0
 def __init__(self, name, path):
     self.name    = name
     self.path    = path
     self.learner = load_learner(path)
# -*- coding: utf-8 -*-
import functools, json, requests

from flask import flash, redirect, render_template, request
from flask import Blueprint, session, url_for, g

from app.services.vid_to_spec_converter import Converter
import os
import glob
from os import path
from fastai.basics import load_learner


blueprint = Blueprint('music_classifier', __name__, url_prefix='/music_classifier')
convert = Converter()
learn_inf = load_learner('app/static/fastai_model/export.pkl')

@blueprint.route('/')
def base():
    if(not path.exists('app/static/spectrogram')):
        os.mkdir('app/static/spectrogram')

    for f in glob.glob("app/static/spectrogram/*.png"):
        os.remove(f)

    g.classified = False
    return render_template('music_classifier/classify.html')


@blueprint.route('/classify')
def classify():