Newer
Older
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 21 18:54:10 2019
"""
from model import *
from data import *
#from quality_measures import *
from segment import *
from data_processing import *
import numpy as np
import skimage
from skimage import io
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
def create_directory_if_not_exists(path):
"""
Create in the file system a new directory if it doesn't exist yet.
Param:
path: the path of the new directory
"""
if not os.path.exists(path):
os.makedirs(path)
def threshold(im,th = None):
"""
Binarize an image with a threshold given by the user, or if the threshold is None, calculate the better threshold with isodata
Param:
im: a numpy array image (numpy array)
th: the value of the threshold (feature to select threshold was asked by the lab)
Return:
bi: threshold given by the user (numpy array)
"""
im2 = im.copy()
if th == None:
th = skimage.filters.threshold_isodata(im2)
bi = im2
bi[bi > th] = 255
bi[bi <= th] = 0
return bi
def prediction(im):
"""
Calculate the prediction of the label corresponding to image im
Param:
im: a numpy array image (numpy array), with max size 2048x2048
Return:
res: the predicted distribution of probability of the labels (numpy array)
im = im[0:2048,0:2048] #crop image if too large
im = np.pad(im,
((0, max(0,2048 - imsize[0])),(0, max(0,2048 - imsize[1]))),
constant_values=0) # pad with zeros if too small
path_test = './tmp/test/image/'
create_directory_if_not_exists(path_test)
# TESTING SET
# img_num, resized_shape, original_shape = generate_test_set(im,path_test)
# WHOLE CELL PREDICTION
testGene = testGenerator(path_test,
1,
target_size = (2048,2048) )
model = unet(pretrained_weights = None,
input_size = (2048,2048,1))
# model.load_weights('unet/unet_weights_batchsize_25_Nepochs_100_full.hdf5')
model.load_weights('unet/unet_weights_batchsize_25_Nepochs_100_SJR0_10.hdf5')
results = model.predict_generator(testGene,
1,
verbose=1)
res = results[0,:,:,0]
res = res[0:imsize[0],0:imsize[1]] #crop if needed, e.g., im was smaller than 2048x2048
res = np.pad(res,
((0, max(0,imsize[0] - 2048)),
(0, max(0,imsize[0] - 2048) )),
constant_values=0) # pad with zeros if too small