annotate UNetCoreograph.py @ 1:57f1260ca94e draft

"planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
author watsocam
date Fri, 11 Mar 2022 23:40:51 +0000
parents 99308601eaa6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
1 import numpy as np
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
2 from scipy import misc as sm
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
3 import shutil
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
4 import scipy.io as sio
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
5 import os
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
6 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
7 import logging
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
8 logging.getLogger('tensorflow').setLevel(logging.FATAL)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
9 import skimage.exposure as sk
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
10 import cv2
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
11 import argparse
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
12 import pytiff
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
13 import tifffile
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
14 import tensorflow as tf
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
15 from skimage.morphology import *
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
16 from skimage.exposure import rescale_intensity
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
17 from skimage.segmentation import chan_vese, find_boundaries, morphological_chan_vese
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
18 from skimage.measure import regionprops,label, find_contours
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
19 from skimage.transform import resize
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
20 from skimage.filters import gaussian, threshold_otsu
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
21 from skimage.feature import peak_local_max,blob_log
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
22 from skimage.color import gray2rgb as gray2rgb
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
23 import skimage.io as skio
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
24 from scipy.ndimage.morphology import binary_fill_holes
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
25 from skimage import img_as_bool
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
26 from skimage.draw import circle_perimeter
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
27 from scipy.ndimage.filters import uniform_filter
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
28 from scipy.ndimage import gaussian_laplace
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
29 from os.path import *
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
30 from os import listdir, makedirs, remove
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
31
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
32
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
33
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
34 import sys
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
35 from typing import Any
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
36
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
37 #sys.path.insert(0, 'C:\\Users\\Public\\Documents\\ImageScience')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
38 from toolbox.imtools import *
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
39 from toolbox.ftools import *
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
40 from toolbox.PartitionOfImage import PI2D
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
41
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
42
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
43 def concat3(lst):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
44 return tf.concat(lst,3)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
45
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
46 class UNet2D:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
47 hp = None # hyper-parameters
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
48 nn = None # network
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
49 tfTraining = None # if training or not (to handle batch norm)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
50 tfData = None # data placeholder
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
51 Session = None
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
52 DatasetMean = 0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
53 DatasetStDev = 0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
54
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
55 def setupWithHP(hp):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
56 UNet2D.setup(hp['imSize'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
57 hp['nChannels'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
58 hp['nClasses'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
59 hp['nOut0'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
60 hp['featMapsFact'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
61 hp['downSampFact'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
62 hp['ks'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
63 hp['nExtraConvs'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
64 hp['stdDev0'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
65 hp['nLayers'],
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
66 hp['batchSize'])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
67
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
68 def setup(imSize,nChannels,nClasses,nOut0,featMapsFact,downSampFact,kernelSize,nExtraConvs,stdDev0,nDownSampLayers,batchSize):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
69 UNet2D.hp = {'imSize':imSize,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
70 'nClasses':nClasses,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
71 'nChannels':nChannels,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
72 'nExtraConvs':nExtraConvs,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
73 'nLayers':nDownSampLayers,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
74 'featMapsFact':featMapsFact,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
75 'downSampFact':downSampFact,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
76 'ks':kernelSize,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
77 'nOut0':nOut0,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
78 'stdDev0':stdDev0,
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
79 'batchSize':batchSize}
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
80
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
81 nOutX = [UNet2D.hp['nChannels'],UNet2D.hp['nOut0']]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
82 dsfX = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
83 for i in range(UNet2D.hp['nLayers']):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
84 nOutX.append(nOutX[-1]*UNet2D.hp['featMapsFact'])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
85 dsfX.append(UNet2D.hp['downSampFact'])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
86
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
87
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
88 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
89 # downsampling layer
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
90 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
91
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
92 with tf.name_scope('placeholders'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
93 UNet2D.tfTraining = tf.placeholder(tf.bool, name='training')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
94 UNet2D.tfData = tf.placeholder("float", shape=[None,UNet2D.hp['imSize'],UNet2D.hp['imSize'],UNet2D.hp['nChannels']],name='data')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
95
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
96 def down_samp_layer(data,index):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
97 with tf.name_scope('ld%d' % index):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
98 ldXWeights1 = tf.Variable(tf.truncated_normal([UNet2D.hp['ks'], UNet2D.hp['ks'], nOutX[index], nOutX[index+1]], stddev=stdDev0),name='kernel1')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
99 ldXWeightsExtra = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
100 for i in range(nExtraConvs):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
101 ldXWeightsExtra.append(tf.Variable(tf.truncated_normal([UNet2D.hp['ks'], UNet2D.hp['ks'], nOutX[index+1], nOutX[index+1]], stddev=stdDev0),name='kernelExtra%d' % i))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
102
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
103 c00 = tf.nn.conv2d(data, ldXWeights1, strides=[1, 1, 1, 1], padding='SAME')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
104 for i in range(nExtraConvs):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
105 c00 = tf.nn.conv2d(tf.nn.relu(c00), ldXWeightsExtra[i], strides=[1, 1, 1, 1], padding='SAME')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
106
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
107 ldXWeightsShortcut = tf.Variable(tf.truncated_normal([1, 1, nOutX[index], nOutX[index+1]], stddev=stdDev0),name='shortcutWeights')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
108 shortcut = tf.nn.conv2d(data, ldXWeightsShortcut, strides=[1, 1, 1, 1], padding='SAME')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
109
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
110 bn = tf.layers.batch_normalization(tf.nn.relu(c00+shortcut), training=UNet2D.tfTraining)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
111
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
112 return tf.nn.max_pool(bn, ksize=[1, dsfX[index], dsfX[index], 1], strides=[1, dsfX[index], dsfX[index], 1], padding='SAME',name='maxpool')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
113
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
114 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
115 # bottom layer
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
116 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
117
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
118 with tf.name_scope('lb'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
119 lbWeights1 = tf.Variable(tf.truncated_normal([UNet2D.hp['ks'], UNet2D.hp['ks'], nOutX[UNet2D.hp['nLayers']], nOutX[UNet2D.hp['nLayers']+1]], stddev=stdDev0),name='kernel1')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
120 def lb(hidden):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
121 return tf.nn.relu(tf.nn.conv2d(hidden, lbWeights1, strides=[1, 1, 1, 1], padding='SAME'),name='conv')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
122
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
123 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
124 # downsampling
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
125 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
126
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
127 with tf.name_scope('downsampling'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
128 dsX = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
129 dsX.append(UNet2D.tfData)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
130
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
131 for i in range(UNet2D.hp['nLayers']):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
132 dsX.append(down_samp_layer(dsX[i],i))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
133
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
134 b = lb(dsX[UNet2D.hp['nLayers']])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
135
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
136 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
137 # upsampling layer
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
138 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
139
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
140 def up_samp_layer(data,index):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
141 with tf.name_scope('lu%d' % index):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
142 luXWeights1 = tf.Variable(tf.truncated_normal([UNet2D.hp['ks'], UNet2D.hp['ks'], nOutX[index+1], nOutX[index+2]], stddev=stdDev0),name='kernel1')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
143 luXWeights2 = tf.Variable(tf.truncated_normal([UNet2D.hp['ks'], UNet2D.hp['ks'], nOutX[index]+nOutX[index+1], nOutX[index+1]], stddev=stdDev0),name='kernel2')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
144 luXWeightsExtra = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
145 for i in range(nExtraConvs):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
146 luXWeightsExtra.append(tf.Variable(tf.truncated_normal([UNet2D.hp['ks'], UNet2D.hp['ks'], nOutX[index+1], nOutX[index+1]], stddev=stdDev0),name='kernel2Extra%d' % i))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
147
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
148 outSize = UNet2D.hp['imSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
149 for i in range(index):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
150 outSize /= dsfX[i]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
151 outSize = int(outSize)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
152
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
153 outputShape = [UNet2D.hp['batchSize'],outSize,outSize,nOutX[index+1]]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
154 us = tf.nn.relu(tf.nn.conv2d_transpose(data, luXWeights1, outputShape, strides=[1, dsfX[index], dsfX[index], 1], padding='SAME'),name='conv1')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
155 cc = concat3([dsX[index],us])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
156 cv = tf.nn.relu(tf.nn.conv2d(cc, luXWeights2, strides=[1, 1, 1, 1], padding='SAME'),name='conv2')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
157 for i in range(nExtraConvs):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
158 cv = tf.nn.relu(tf.nn.conv2d(cv, luXWeightsExtra[i], strides=[1, 1, 1, 1], padding='SAME'),name='conv2Extra%d' % i)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
159 return cv
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
160
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
161 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
162 # final (top) layer
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
163 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
164
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
165 with tf.name_scope('lt'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
166 ltWeights1 = tf.Variable(tf.truncated_normal([1, 1, nOutX[1], nClasses], stddev=stdDev0),name='kernel')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
167 def lt(hidden):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
168 return tf.nn.conv2d(hidden, ltWeights1, strides=[1, 1, 1, 1], padding='SAME',name='conv')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
169
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
170
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
171 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
172 # upsampling
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
173 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
174
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
175 with tf.name_scope('upsampling'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
176 usX = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
177 usX.append(b)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
178
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
179 for i in range(UNet2D.hp['nLayers']):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
180 usX.append(up_samp_layer(usX[i],UNet2D.hp['nLayers']-1-i))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
181
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
182 t = lt(usX[UNet2D.hp['nLayers']])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
183
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
184
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
185 sm = tf.nn.softmax(t,-1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
186 UNet2D.nn = sm
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
187
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
188
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
189 def train(imPath,logPath,modelPath,pmPath,nTrain,nValid,nTest,restoreVariables,nSteps,gpuIndex,testPMIndex):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
190 os.environ['CUDA_VISIBLE_DEVICES']= '%d' % gpuIndex
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
191
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
192 outLogPath = logPath
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
193 trainWriterPath = pathjoin(logPath,'Train')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
194 validWriterPath = pathjoin(logPath,'Valid')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
195 outModelPath = pathjoin(modelPath,'model.ckpt')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
196 outPMPath = pmPath
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
197
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
198 batchSize = UNet2D.hp['batchSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
199 imSize = UNet2D.hp['imSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
200 nChannels = UNet2D.hp['nChannels']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
201 nClasses = UNet2D.hp['nClasses']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
202
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
203 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
204 # data
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
205 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
206
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
207 Train = np.zeros((nTrain,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
208 Valid = np.zeros((nValid,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
209 Test = np.zeros((nTest,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
210 LTrain = np.zeros((nTrain,imSize,imSize,nClasses))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
211 LValid = np.zeros((nValid,imSize,imSize,nClasses))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
212 LTest = np.zeros((nTest,imSize,imSize,nClasses))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
213
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
214 print('loading data, computing mean / st dev')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
215 if not os.path.exists(modelPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
216 os.makedirs(modelPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
217 if restoreVariables:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
218 datasetMean = loadData(pathjoin(modelPath,'datasetMean.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
219 datasetStDev = loadData(pathjoin(modelPath,'datasetStDev.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
220 else:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
221 datasetMean = 0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
222 datasetStDev = 0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
223 for iSample in range(nTrain+nValid+nTest):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
224 I = im2double(tifread('%s/I%05d_Img.tif' % (imPath,iSample)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
225 datasetMean += np.mean(I)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
226 datasetStDev += np.std(I)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
227 datasetMean /= (nTrain+nValid+nTest)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
228 datasetStDev /= (nTrain+nValid+nTest)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
229 saveData(datasetMean, pathjoin(modelPath,'datasetMean.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
230 saveData(datasetStDev, pathjoin(modelPath,'datasetStDev.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
231
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
232 perm = np.arange(nTrain+nValid+nTest)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
233 np.random.shuffle(perm)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
234
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
235 for iSample in range(0, nTrain):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
236 path = '%s/I%05d_Img.tif' % (imPath,perm[iSample])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
237 im = im2double(tifread(path))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
238 Train[iSample,:,:,0] = (im-datasetMean)/datasetStDev
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
239 path = '%s/I%05d_Ant.tif' % (imPath,perm[iSample])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
240 im = tifread(path)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
241 for i in range(nClasses):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
242 LTrain[iSample,:,:,i] = (im == i+1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
243
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
244 for iSample in range(0, nValid):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
245 path = '%s/I%05d_Img.tif' % (imPath,perm[nTrain+iSample])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
246 im = im2double(tifread(path))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
247 Valid[iSample,:,:,0] = (im-datasetMean)/datasetStDev
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
248 path = '%s/I%05d_Ant.tif' % (imPath,perm[nTrain+iSample])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
249 im = tifread(path)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
250 for i in range(nClasses):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
251 LValid[iSample,:,:,i] = (im == i+1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
252
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
253 for iSample in range(0, nTest):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
254 path = '%s/I%05d_Img.tif' % (imPath,perm[nTrain+nValid+iSample])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
255 im = im2double(tifread(path))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
256 Test[iSample,:,:,0] = (im-datasetMean)/datasetStDev
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
257 path = '%s/I%05d_Ant.tif' % (imPath,perm[nTrain+nValid+iSample])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
258 im = tifread(path)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
259 for i in range(nClasses):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
260 LTest[iSample,:,:,i] = (im == i+1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
261
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
262 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
263 # optimization
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
264 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
265
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
266 tfLabels = tf.placeholder("float", shape=[None,imSize,imSize,nClasses],name='labels')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
267
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
268 globalStep = tf.Variable(0,trainable=False)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
269 learningRate0 = 0.01
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
270 decaySteps = 1000
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
271 decayRate = 0.95
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
272 learningRate = tf.train.exponential_decay(learningRate0,globalStep,decaySteps,decayRate,staircase=True)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
273
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
274 with tf.name_scope('optim'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
275 loss = tf.reduce_mean(-tf.reduce_sum(tf.multiply(tfLabels,tf.log(UNet2D.nn)),3))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
276 updateOps = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
277 # optimizer = tf.train.MomentumOptimizer(1e-3,0.9)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
278 optimizer = tf.train.MomentumOptimizer(learningRate,0.9)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
279 # optimizer = tf.train.GradientDescentOptimizer(learningRate)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
280 with tf.control_dependencies(updateOps):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
281 optOp = optimizer.minimize(loss,global_step=globalStep)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
282
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
283 with tf.name_scope('eval'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
284 error = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
285 for iClass in range(nClasses):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
286 labels0 = tf.reshape(tf.to_int32(tf.slice(tfLabels,[0,0,0,iClass],[-1,-1,-1,1])),[batchSize,imSize,imSize])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
287 predict0 = tf.reshape(tf.to_int32(tf.equal(tf.argmax(UNet2D.nn,3),iClass)),[batchSize,imSize,imSize])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
288 correct = tf.multiply(labels0,predict0)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
289 nCorrect0 = tf.reduce_sum(correct)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
290 nLabels0 = tf.reduce_sum(labels0)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
291 error.append(1-tf.to_float(nCorrect0)/tf.to_float(nLabels0))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
292 errors = tf.tuple(error)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
293
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
294 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
295 # inspection
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
296 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
297
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
298 with tf.name_scope('scalars'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
299 tf.summary.scalar('avg_cross_entropy', loss)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
300 for iClass in range(nClasses):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
301 tf.summary.scalar('avg_pixel_error_%d' % iClass, error[iClass])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
302 tf.summary.scalar('learning_rate', learningRate)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
303 with tf.name_scope('images'):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
304 split0 = tf.slice(UNet2D.nn,[0,0,0,0],[-1,-1,-1,1])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
305 split1 = tf.slice(UNet2D.nn,[0,0,0,1],[-1,-1,-1,1])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
306 if nClasses > 2:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
307 split2 = tf.slice(UNet2D.nn,[0,0,0,2],[-1,-1,-1,1])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
308 tf.summary.image('pm0',split0)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
309 tf.summary.image('pm1',split1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
310 if nClasses > 2:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
311 tf.summary.image('pm2',split2)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
312 merged = tf.summary.merge_all()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
313
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
314
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
315 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
316 # session
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
317 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
318
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
319 saver = tf.train.Saver()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
320 sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # config parameter needed to save variables when using GPU
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
321
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
322 if os.path.exists(outLogPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
323 shutil.rmtree(outLogPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
324 trainWriter = tf.summary.FileWriter(trainWriterPath, sess.graph)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
325 validWriter = tf.summary.FileWriter(validWriterPath, sess.graph)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
326
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
327 if restoreVariables:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
328 saver.restore(sess, outModelPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
329 print("Model restored.")
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
330 else:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
331 sess.run(tf.global_variables_initializer())
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
332
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
333 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
334 # train
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
335 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
336
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
337 batchData = np.zeros((batchSize,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
338 batchLabels = np.zeros((batchSize,imSize,imSize,nClasses))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
339 for i in range(nSteps):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
340 # train
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
341
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
342 perm = np.arange(nTrain)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
343 np.random.shuffle(perm)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
344
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
345 for j in range(batchSize):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
346 batchData[j,:,:,:] = Train[perm[j],:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
347 batchLabels[j,:,:,:] = LTrain[perm[j],:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
348
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
349 summary,_ = sess.run([merged,optOp],feed_dict={UNet2D.tfData: batchData, tfLabels: batchLabels, UNet2D.tfTraining: 1})
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
350 trainWriter.add_summary(summary, i)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
351
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
352 # validation
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
353
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
354 perm = np.arange(nValid)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
355 np.random.shuffle(perm)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
356
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
357 for j in range(batchSize):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
358 batchData[j,:,:,:] = Valid[perm[j],:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
359 batchLabels[j,:,:,:] = LValid[perm[j],:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
360
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
361 summary, es = sess.run([merged, errors],feed_dict={UNet2D.tfData: batchData, tfLabels: batchLabels, UNet2D.tfTraining: 0})
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
362 validWriter.add_summary(summary, i)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
363
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
364 e = np.mean(es)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
365 print('step %05d, e: %f' % (i,e))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
366
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
367 if i == 0:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
368 if restoreVariables:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
369 lowestError = e
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
370 else:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
371 lowestError = np.inf
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
372
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
373 if np.mod(i,100) == 0 and e < lowestError:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
374 lowestError = e
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
375 print("Model saved in file: %s" % saver.save(sess, outModelPath))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
376
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
377
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
378 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
379 # test
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
380 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
381
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
382 if not os.path.exists(outPMPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
383 os.makedirs(outPMPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
384
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
385 for i in range(nTest):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
386 j = np.mod(i,batchSize)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
387
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
388 batchData[j,:,:,:] = Test[i,:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
389 batchLabels[j,:,:,:] = LTest[i,:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
390
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
391 if j == batchSize-1 or i == nTest-1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
392
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
393 output = sess.run(UNet2D.nn,feed_dict={UNet2D.tfData: batchData, tfLabels: batchLabels, UNet2D.tfTraining: 0})
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
394
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
395 for k in range(j+1):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
396 pm = output[k,:,:,testPMIndex]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
397 gt = batchLabels[k,:,:,testPMIndex]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
398 im = np.sqrt(normalize(batchData[k,:,:,0]))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
399 imwrite(np.uint8(255*np.concatenate((im,np.concatenate((pm,gt),axis=1)),axis=1)),'%s/I%05d.png' % (outPMPath,i-j+k+1))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
400
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
401
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
402 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
403 # save hyper-parameters, clean-up
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
404 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
405
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
406 saveData(UNet2D.hp,pathjoin(modelPath,'hp.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
407
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
408 trainWriter.close()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
409 validWriter.close()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
410 sess.close()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
411
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
412 def deploy(imPath,nImages,modelPath,pmPath,gpuIndex,pmIndex):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
413 os.environ['CUDA_VISIBLE_DEVICES']= '%d' % gpuIndex
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
414 variablesPath = pathjoin(modelPath,'model.ckpt')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
415 outPMPath = pmPath
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
416
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
417 hp = loadData(pathjoin(modelPath,'hp.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
418 UNet2D.setupWithHP(hp)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
419
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
420 batchSize = UNet2D.hp['batchSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
421 imSize = UNet2D.hp['imSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
422 nChannels = UNet2D.hp['nChannels']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
423 nClasses = UNet2D.hp['nClasses']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
424
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
425 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
426 # data
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
427 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
428
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
429 Data = np.zeros((nImages,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
430
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
431 datasetMean = loadData(pathjoin(modelPath,'datasetMean.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
432 datasetStDev = loadData(pathjoin(modelPath,'datasetStDev.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
433
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
434 for iSample in range(0, nImages):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
435 path = '%s/I%05d_Img.tif' % (imPath,iSample)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
436 im = im2double(tifread(path))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
437 Data[iSample,:,:,0] = (im-datasetMean)/datasetStDev
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
438
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
439 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
440 # session
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
441 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
442
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
443 saver = tf.train.Saver()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
444 sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # config parameter needed to save variables when using GPU
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
445
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
446 saver.restore(sess, variablesPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
447 print("Model restored.")
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
448
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
449 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
450 # deploy
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
451 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
452
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
453 batchData = np.zeros((batchSize,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
454
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
455 if not os.path.exists(outPMPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
456 os.makedirs(outPMPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
457
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
458 for i in range(nImages):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
459 print(i,nImages)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
460
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
461 j = np.mod(i,batchSize)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
462
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
463 batchData[j,:,:,:] = Data[i,:,:,:]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
464
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
465 if j == batchSize-1 or i == nImages-1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
466
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
467 output = sess.run(UNet2D.nn,feed_dict={UNet2D.tfData: batchData, UNet2D.tfTraining: 0})
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
468
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
469 for k in range(j+1):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
470 pm = output[k,:,:,pmIndex]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
471 im = np.sqrt(normalize(batchData[k,:,:,0]))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
472 # imwrite(np.uint8(255*np.concatenate((im,pm),axis=1)),'%s/I%05d.png' % (outPMPath,i-j+k+1))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
473 imwrite(np.uint8(255*im),'%s/I%05d_Im.png' % (outPMPath,i-j+k+1))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
474 imwrite(np.uint8(255*pm),'%s/I%05d_PM.png' % (outPMPath,i-j+k+1))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
475
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
476
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
477 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
478 # clean-up
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
479 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
480
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
481 sess.close()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
482
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
483 def singleImageInferenceSetup(modelPath,gpuIndex):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
484 os.environ['CUDA_VISIBLE_DEVICES']= '%d' % gpuIndex
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
485 variablesPath = pathjoin(modelPath,'model.ckpt')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
486 hp = loadData(pathjoin(modelPath,'hp.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
487 UNet2D.setupWithHP(hp)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
488
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
489 UNet2D.DatasetMean =loadData(pathjoin(modelPath,'datasetMean.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
490 UNet2D.DatasetStDev = loadData(pathjoin(modelPath,'datasetStDev.data'))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
491 print(UNet2D.DatasetMean)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
492 print(UNet2D.DatasetStDev)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
493
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
494 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
495 # session
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
496 # --------------------------------------------------
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
497
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
498 saver = tf.train.Saver()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
499 UNet2D.Session = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # config parameter needed to save variables when using GPU
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
500 #UNet2D.Session = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
501 saver.restore(UNet2D.Session, variablesPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
502 print("Model restored.")
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
503
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
504 def singleImageInferenceCleanup():
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
505 UNet2D.Session.close()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
506
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
507 def singleImageInference(image,mode,pmIndex):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
508 print('Inference...')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
509
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
510 batchSize = UNet2D.hp['batchSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
511 imSize = UNet2D.hp['imSize']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
512 nChannels = UNet2D.hp['nChannels']
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
513
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
514 PI2D.setup(image,imSize,int(imSize/8),mode)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
515 PI2D.createOutput(nChannels)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
516
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
517 batchData = np.zeros((batchSize,imSize,imSize,nChannels))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
518 for i in range(PI2D.NumPatches):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
519 j = np.mod(i,batchSize)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
520 batchData[j,:,:,0] = (PI2D.getPatch(i)-UNet2D.DatasetMean)/UNet2D.DatasetStDev
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
521 if j == batchSize-1 or i == PI2D.NumPatches-1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
522 output = UNet2D.Session.run(UNet2D.nn,feed_dict={UNet2D.tfData: batchData, UNet2D.tfTraining: 0})
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
523 for k in range(j+1):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
524 pm = output[k,:,:,pmIndex]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
525 PI2D.patchOutput(i-j+k,pm)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
526 # PI2D.patchOutput(i-j+k,normalize(imgradmag(PI2D.getPatch(i-j+k),1)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
527
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
528 return PI2D.getValidOutput()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
529
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
530
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
531 def identifyNumChan(path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
532
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
533 s = tifffile.TiffFile(path).series[0]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
534 return s.shape[0] if len(s.shape) > 2 else 1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
535 # shape = tiff.pages[0].shape
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
536 # tiff = tifffile.TiffFile(path)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
537 # for i, page in enumerate(tiff.pages):
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
538 # print(page.shape)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
539 # if page.shape != shape:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
540 # numChan = i
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
541 # return numChan
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
542 # break
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
543 # else:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
544 # raise Exception("Did not find any pyramid subresolutions")
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
545
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
546
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
547 def getProbMaps(I,dsFactor,modelPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
548 hsize = int((float(I.shape[0]) * float(0.5)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
549 vsize = int((float(I.shape[1]) * float(0.5)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
550 imagesub = cv2.resize(I,(vsize,hsize),cv2.INTER_NEAREST)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
551
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
552 UNet2D.singleImageInferenceSetup(modelPath, 0)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
553
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
554 for iSize in range(dsFactor):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
555 hsize = int((float(I.shape[0]) * float(0.5)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
556 vsize = int((float(I.shape[1]) * float(0.5)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
557 I = cv2.resize(I,(vsize,hsize),cv2.INTER_NEAREST)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
558 I = im2double(I)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
559 I = im2double(sk.rescale_intensity(I, in_range=(np.min(I), np.max(I)), out_range=(0, 0.983)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
560 probMaps = UNet2D.singleImageInference(I,'accumulate',1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
561 UNet2D.singleImageInferenceCleanup()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
562 return probMaps
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
563
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
564 def coreSegmenterOutput(I,initialmask,findCenter):
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
565 hsize = int((float(I.shape[0]) * float(0.1)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
566 vsize = int((float(I.shape[1]) * float(0.1)))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
567 nucGF = cv2.resize(I,(vsize,hsize),cv2.INTER_CUBIC)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
568 #active contours
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
569 hsize = int(float(nucGF.shape[0]))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
570 vsize = int(float(nucGF.shape[1]))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
571 initialmask = cv2.resize(initialmask,(vsize,hsize),cv2.INTER_NEAREST)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
572 initialmask = dilation(initialmask,disk(15)) >0
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
573
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
574 nucGF = gaussian(nucGF,0.7)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
575 nucGF=nucGF/np.amax(nucGF)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
576
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
577 nuclearMask = morphological_chan_vese(nucGF, 100, init_level_set=initialmask, smoothing=10,lambda1=1.001, lambda2=1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
578
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
579 TMAmask = nuclearMask
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
580 TMAmask = remove_small_objects(TMAmask>0,round(TMAmask.shape[0])*round(TMAmask.shape[1])*0.005)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
581 TMAlabel = label(TMAmask)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
582 # find object closest to center
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
583 if findCenter==True:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
584
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
585 stats= regionprops(TMAlabel)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
586 counter=1
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
587 minDistance =-1
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
588 index =[]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
589 for props in stats:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
590 centroid = props.centroid
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
591 distanceFromCenter = np.sqrt((centroid[0]-nucGF.shape[0]/2)**2+(centroid[1]-nucGF.shape[1]/2)**2)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
592 # if distanceFromCenter<0.6/2*np.sqrt(TMAlabel.shape[0]*TMAlabel.shape[1]):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
593 if distanceFromCenter<minDistance or minDistance==-1 :
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
594 minDistance =distanceFromCenter
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
595 index = counter
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
596 counter=counter+1
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
597 # dist = 0.6/2*np.sqrt(TMAlabel.shape[0]*TMAlabel.shape[1])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
598 TMAmask = morphology.binary_closing(TMAlabel==index,disk(3))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
599
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
600 return TMAmask
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
601
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
602 def overlayOutline(outline,img):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
603 img2 = img.copy()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
604 stacked_img = np.stack((img2,)*3, axis=-1)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
605 stacked_img[outline > 0] = [1, 0, 0]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
606 imshowpair(img2,stacked_img)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
607
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
608 def imshowpair(A,B):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
609 plt.imshow(A,cmap='Purples')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
610 plt.imshow(B,cmap='Greens',alpha=0.5)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
611 plt.show()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
612
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
613
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
614 if __name__ == '__main__':
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
615 parser=argparse.ArgumentParser()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
616 parser.add_argument("--imagePath")
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
617 parser.add_argument("--outputPath")
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
618 parser.add_argument("--maskPath")
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
619 parser.add_argument("--tissue", action='store_true')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
620 parser.add_argument("--downsampleFactor", type=int, default = 5)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
621 parser.add_argument("--channel",type = int, default = 0)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
622 parser.add_argument("--buffer",type = float, default = 2)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
623 parser.add_argument("--outputChan", type=int, nargs = '+', default=[-1])
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
624 parser.add_argument("--sensitivity",type = float, default=0.3)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
625 parser.add_argument("--useGrid",action='store_true')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
626 parser.add_argument("--cluster",action='store_true')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
627 args = parser.parse_args()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
628
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
629 outputPath = args.outputPath
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
630 imagePath = args.imagePath
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
631 sensitivity = args.sensitivity
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
632 scriptPath = os.path.dirname(os.path.realpath(__file__))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
633 modelPath = os.path.join(scriptPath, 'model')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
634 maskOutputPath = os.path.join(outputPath, 'masks')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
635
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
636
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
637 # if not os.path.exists(outputPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
638 # os.makedirs(outputPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
639 # else:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
640 # shutil.rmtree(outputPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
641 if not os.path.exists(maskOutputPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
642 os.makedirs(maskOutputPath)
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
643 print(
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
644 'WARNING! IF USING FOR TISSUE SPLITTING, IT IS ADVISED TO SET --downsampleFactor TO HIGHER THAN DEFAULT OF 5')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
645 channel = args.channel
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
646 dsFactor = 1/(2**args.downsampleFactor)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
647 I = skio.imread(imagePath, img_num=channel)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
648 imagesub = resize(I,(int((float(I.shape[0]) * dsFactor)),int((float(I.shape[1]) * dsFactor))))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
649 numChan = identifyNumChan(imagePath)
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
650
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
651 outputChan = args.outputChan
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
652 if len(outputChan)==1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
653 if outputChan[0]==-1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
654 outputChan = [0, numChan-1]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
655 else:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
656 outputChan.append(outputChan[0])
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
657 classProbs = getProbMaps(I, args.downsampleFactor, modelPath)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
658
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
659 if not args.tissue:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
660 print('TMA mode selected')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
661 preMask = gaussian(np.uint8(classProbs*255),1)>0.8
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
662
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
663 P = regionprops(label(preMask),cache=False)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
664 area = [ele.area for ele in P]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
665 if len(P) <3:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
666 medArea = np.median(area)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
667 maxArea = np.percentile(area,99)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
668 else:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
669 count=0
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
670 labelpreMask = np.zeros(preMask.shape,dtype=np.uint32)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
671 for props in P:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
672 count += 1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
673 yi = props.coords[:, 0]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
674 xi = props.coords[:, 1]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
675 labelpreMask[yi, xi] = count
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
676 P=regionprops(labelpreMask)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
677 area = [ele.area for ele in P]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
678 medArea = np.median(area)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
679 maxArea = np.percentile(area,99)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
680 preMask = remove_small_objects(preMask,0.2*medArea)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
681 coreRad = round(np.sqrt(medArea/np.pi))
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
682 estCoreDiam = round(np.sqrt(maxArea/np.pi)*1.2*args.buffer)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
683
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
684 #preprocessing
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
685 fgFiltered = blob_log(preMask,coreRad*0.6,threshold=sensitivity)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
686 Imax = np.zeros(preMask.shape,dtype=np.uint8)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
687 for iSpot in range(fgFiltered.shape[0]):
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
688 yi = np.uint32(round(fgFiltered[iSpot, 0]))
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
689 xi = np.uint32(round(fgFiltered[iSpot, 1]))
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
690 Imax[yi, xi] = 1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
691 Imax = Imax*preMask
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
692 Idist = distance_transform_edt(1-Imax)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
693 markers = label(Imax)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
694 coreLabel = watershed(Idist,markers,watershed_line=True,mask = preMask)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
695 P = regionprops(coreLabel)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
696 centroids = np.array([ele.centroid for ele in P]) / dsFactor
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
697 np.savetxt(outputPath + os.path.sep + 'centroidsY-X.txt', np.asarray(centroids), fmt='%10.5f')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
698 numCores = len(centroids)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
699 print(str(numCores) + ' cores detected!')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
700 estCoreDiamX = np.ones(numCores) * estCoreDiam / dsFactor
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
701 estCoreDiamY = np.ones(numCores) * estCoreDiam / dsFactor
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
702 else:
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
703 print('Tissue mode selected')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
704 imageblur = 5
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
705 Iblur = gaussian(np.uint8(255*classProbs), imageblur)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
706 coreMask = binary_fill_holes(binary_closing(Iblur > threshold_otsu(Iblur), np.ones((imageblur*2,imageblur*2))))
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
707 coreMask = remove_small_objects(coreMask, min_size=0.001 * coreMask.shape[0] * coreMask.shape[1])
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
708
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
709 ## watershed
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
710 Idist = distance_transform_edt(coreMask)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
711 markers = peak_local_max(h_maxima(Idist,20),indices=False)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
712 markers = label(markers).astype(np.int8)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
713 coreLabel = watershed(-Idist, markers, watershed_line=True,mask = coreMask)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
714
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
715 P = regionprops(coreLabel)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
716 centroids = np.array([ele.centroid for ele in P]) / dsFactor
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
717 np.savetxt(outputPath + os.path.sep + 'centroidsY-X.txt', np.asarray(centroids), fmt='%10.5f')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
718 numCores = len(centroids)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
719 print(str(numCores) + ' tissues detected!')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
720 estCoreDiamX = np.array([(ele.bbox[3]-ele.bbox[1])*1.1 for ele in P]) / dsFactor
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
721 estCoreDiamY = np.array([(ele.bbox[2]-ele.bbox[0])*1.1 for ele in P]) / dsFactor
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
722
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
723 if numCores ==0 & args.cluster:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
724 print('No cores detected. Try adjusting the downsample factor')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
725 sys.exit(255)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
726
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
727 singleMaskTMA = np.zeros(imagesub.shape)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
728 maskTMA = np.zeros(imagesub.shape)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
729 bbox = [None] * numCores
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
730 imagesub = imagesub/np.percentile(imagesub,99.9)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
731 imagesub = (imagesub * 255).round().astype(np.uint8)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
732 imagesub = gray2rgb(imagesub)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
733 x=np.zeros(numCores)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
734 xLim=np.zeros(numCores)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
735 y=np.zeros(numCores)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
736 yLim=np.zeros(numCores)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
737
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
738 # segmenting each core
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
739 #######################
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
740 for iCore in range(numCores):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
741 x[iCore] = centroids[iCore,1] - estCoreDiamX[iCore]/2
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
742 xLim[iCore] = x[iCore]+estCoreDiamX[iCore]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
743 if xLim[iCore] > I.shape[1]:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
744 xLim[iCore] = I.shape[1]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
745 if x[iCore]<1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
746 x[iCore]=1
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
747
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
748 y[iCore] = centroids[iCore,0] - estCoreDiamY[iCore]/2
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
749 yLim[iCore] = y[iCore] + estCoreDiamY[iCore]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
750 if yLim[iCore] > I.shape[0]:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
751 yLim[iCore] = I.shape[0]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
752 if y[iCore]<1:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
753 y[iCore]=1
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
754
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
755 bbox[iCore] = [round(x[iCore]), round(y[iCore]), round(xLim[iCore]), round(yLim[iCore])]
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
756 coreStack = np.zeros((outputChan[1]-outputChan[0]+1,np.int(round(yLim[iCore])-round(y[iCore])-1),np.int(round(xLim[iCore])-round(x[iCore])-1)),dtype='uint16')
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
757
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
758 for iChan in range(outputChan[0],outputChan[1]+1):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
759 with pytiff.Tiff(imagePath, "r", encoding='utf-8') as handle:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
760 handle.set_page(iChan)
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
761 coreStack[iChan,:,:] =handle[np.uint32(bbox[iCore][1]):np.uint32(bbox[iCore][3]-1), np.uint32(bbox[iCore][0]):np.uint32(bbox[iCore][2]-1)]
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
762
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
763 skio.imsave(outputPath + os.path.sep + str(iCore+1) + '.tif',np.uint16(coreStack),imagej=True,bigtiff=True)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
764 with pytiff.Tiff(imagePath, "r", encoding='utf-8') as handle:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
765 handle.set_page(args.channel)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
766 coreSlice= handle[np.uint32(bbox[iCore][1]):np.uint32(bbox[iCore][3]-1), np.uint32(bbox[iCore][0]):np.uint32(bbox[iCore][2]-1)]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
767
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
768 core = (coreLabel ==(iCore+1))
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
769 initialmask = core[np.uint32(y[iCore] * dsFactor):np.uint32(yLim[iCore] * dsFactor),
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
770 np.uint32(x[iCore] * dsFactor):np.uint32(xLim[iCore] * dsFactor)]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
771 if not args.tissue:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
772 initialmask = resize(initialmask,size(coreSlice),cv2.INTER_NEAREST)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
773
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
774 singleProbMap = classProbs[np.uint32(y[iCore]*dsFactor):np.uint32(yLim[iCore]*dsFactor),np.uint32(x[iCore]*dsFactor):np.uint32(xLim[iCore]*dsFactor)]
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
775 singleProbMap = resize(np.uint8(255*singleProbMap),size(coreSlice),cv2.INTER_NEAREST)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
776 TMAmask = coreSegmenterOutput(coreSlice,initialmask,False)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
777 else:
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
778 Irs = resize(coreSlice,(int((float(coreSlice.shape[0]) * 0.25)),int((float(coreSlice.shape[1]) * 0.25))))
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
779 TMAmask = coreSegmenterOutput(Irs, np.uint8(initialmask), False)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
780
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
781 if np.sum(TMAmask)==0:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
782 TMAmask = np.ones(TMAmask.shape)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
783 vsize = int(float(coreSlice.shape[0]))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
784 hsize = int(float(coreSlice.shape[1]))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
785 masksub = resize(resize(TMAmask,(vsize,hsize),cv2.INTER_NEAREST),(int((float(coreSlice.shape[0])*dsFactor)),int((float(coreSlice.shape[1])*dsFactor))),cv2.INTER_NEAREST)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
786 singleMaskTMA[int(y[iCore]*dsFactor):int(y[iCore]*dsFactor)+masksub.shape[0],int(x[iCore]*dsFactor):int(x[iCore]*dsFactor)+masksub.shape[1]]=masksub
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
787 maskTMA = maskTMA + resize(singleMaskTMA,maskTMA.shape,cv2.INTER_NEAREST)
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
788
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
789 cv2.putText(imagesub, str(iCore+1), (int(P[iCore].centroid[1]),int(P[iCore].centroid[0])), 0, 0.5, (0,255,0), 1, cv2.LINE_AA)
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
790
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
791 skio.imsave(maskOutputPath + os.path.sep + str(iCore+1) + '_mask.tif',np.uint8(TMAmask))
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
792 print('Segmented core/tissue ' + str(iCore+1))
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
793
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
794 boundaries = find_boundaries(maskTMA)
1
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
795 imagesub[boundaries==1] = 255
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
796 skio.imsave(outputPath + os.path.sep + 'TMA_MAP.tif' ,imagesub)
57f1260ca94e "planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
watsocam
parents: 0
diff changeset
797 print('Segmented all cores/tissues!')
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
798
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
799 #restore GPU to 0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
800 #image load using tifffile