Skip to content

Commit c7cc7b0

Browse files
authored
Merge pull request #6 from coreyjadams/tf-1.X
Tf 1.x
2 parents f91e309 + f5a06df commit c7cc7b0

26 files changed

+1745
-2283
lines changed

bin/exec.py

Lines changed: 359 additions & 51 deletions
Large diffs are not rendered by default.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
numpy
22
h5py
33
larcv
4-
tensorflow==1.15.0
4+
tensorflow==2.0.0
55
torch==1.3
66
Pillow # Needed for tensorboardX
77
tensorboardX

src/io/io_templates.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/networks/config.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
3+
# This function is to parse strings from argparse into bool
4+
def str2bool(v):
5+
'''Convert string to boolean value
6+
7+
This function is from stackoverflow:
8+
https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
9+
10+
Arguments:
11+
v {str} -- [description]
12+
13+
Returns:
14+
bool -- [description]
15+
16+
Raises:
17+
argparse -- [description]
18+
'''
19+
if v.lower() in ('yes', 'true', 't', 'y', '1'):
20+
return True
21+
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
22+
return False
23+
else:
24+
raise argparse.ArgumentTypeError('Boolean value expected.')
25+
26+
27+
class UResNetConfig(object):
28+
29+
def __init__(self):
30+
self._name = "UResNet"
31+
self._help = "Tensorflow Implementation of multi-plane UResNet"
32+
33+
def build_parser(self, parser):
34+
35+
##################################################################
36+
# Parameters to control the network implementation
37+
##################################################################
38+
39+
# Layerwise parameters:
40+
parser.add_argument('-ub','--use-bias',
41+
type = str2bool,
42+
default = True,
43+
help = "Whether or not to include bias terms in all learned layers.")
44+
45+
parser.add_argument('-bn','--batch-norm',
46+
type = str2bool,
47+
default = True,
48+
help = "Whether or not to use batch normalization in all mlp layers.")
49+
50+
# Network Architecture parameters:
51+
parser.add_argument('--n-initial-filters',
52+
type = int,
53+
default = 16,
54+
help = "Number of filters applied, per plane, for the initial convolution.")
55+
56+
parser.add_argument('--blocks-per-layer',
57+
type = int,
58+
default = 2,
59+
help = "Number of blocks per layer.")
60+
61+
parser.add_argument('--blocks-deepest-layer',
62+
type = int,
63+
default = 5,
64+
help = "Number of blocks applied at the deepest, merged layer.")
65+
66+
parser.add_argument('--blocks-final',
67+
type = int,
68+
default = 0,
69+
help = "Number of blocks applied at full, final resolution.")
70+
71+
parser.add_argument('--network-depth',
72+
type = int,
73+
default = 6,
74+
help = "Total number of downsamples to apply. Note: ensure depth + downsample =< 8")
75+
76+
parser.add_argument("--filter-size-deepest",
77+
type = int,
78+
default = 5,
79+
help = "Convolutional window size for the deepest layer convolution.")
80+
81+
parser.add_argument("--bottleneck-deepest",
82+
type = int,
83+
default = 256,
84+
help = "Bottleneck size for deepest layer convolution.")
85+
86+
parser.add_argument('--connections',
87+
type = str,
88+
choices = ['sum', 'concat', 'none'],
89+
default = 'sum',
90+
help = "Connect shortcuts with sums, concat+bottleneck, or no connections.")
91+
92+
parser.add_argument('--upsampling',
93+
type = str,
94+
choices = ["convolutional", "interpolation"],
95+
default = "interpolation",
96+
help = "Which operation to use for upsamplign.")
97+
98+
parser.add_argument('--downsampling',
99+
type = str,
100+
choices = ["convolutional", "max_pooling"],
101+
default = "max_pooling",
102+
help = "Which operation to use for downsamplign.")
103+
104+
parser.add_argument('--residual',
105+
type = str2bool,
106+
default = True,
107+
help = "Use residual units instead of convolutions.")
108+
109+
110+
parser.add_argument('-gr', '--growth-rate',
111+
type = str,
112+
choices = ['multiplicative','additive'],
113+
default = "additive",
114+
help = "Either double at each layer, or add a constant factor, to the number of filters.")
115+
116+
parser.add_argument('--block-concat',
117+
type = str2bool,
118+
default = False,
119+
help = "Block the concatenations at the deepest layer (2D only).")
120+
121+
122+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import tensorflow as tf
2+
from tensorflow.python.keras.utils import losses_utils
3+
4+
import numpy
5+
6+
class AccuracyCalculator(object):
7+
8+
def __init__(self):
9+
10+
object.__init__(self)
11+
12+
13+
def __call__(self, labels, prediction):
14+
15+
# Labels is a list of tensors
16+
# Logits is a list of tensors
17+
with tf.name_scope("accuracy"):
18+
n_planes = 3
19+
accuracies = {
20+
"total_accuracy" : [None]*n_planes,
21+
"non_bkg_accuracy" : [None]*n_planes,
22+
"neut_iou" : [None]*n_planes,
23+
"cosmic_iou" : [None]*n_planes
24+
}
25+
26+
for p in range(n_planes):
27+
# for p in range(n_planes):
28+
29+
# Accuracy over individual pixels:
30+
pixel_accuracy = tf.stop_gradient(tf.cast(tf.math.equal(labels[p], prediction[p]), dtype=tf.float32))
31+
32+
accuracies["total_accuracy"][p] = tf.reduce_mean(pixel_accuracy)
33+
34+
# Find the non zero labels:
35+
non_zero_indices = tf.cast(tf.not_equal(labels[p], tf.constant(0, labels[p].dtype)), tf.float32)
36+
37+
38+
39+
weighted_accuracy = pixel_accuracy * non_zero_indices
40+
41+
# Use non_zero_indexes to mask the accuracy to non zero label pixels
42+
accuracies["non_bkg_accuracy"][p] = tf.reduce_sum(weighted_accuracy) / tf.reduce_sum(non_zero_indices)
43+
44+
45+
# Neutrinos are label 2, cosmics label 1. But iterate so I only need to
46+
# write these metrics once:
47+
48+
for index in [1, 2]:
49+
# Find the true indices:
50+
label_indices = tf.equal(
51+
labels[p], tf.constant(index, labels[p].dtype))
52+
# Find the predicted indices:
53+
prediction_indices = tf.equal(
54+
prediction[p], tf.constant(index, prediction[p].dtype))
55+
56+
57+
# To compute the intersection over union metrics,
58+
# start with intersections and unions:
59+
intersection = tf.math.logical_and(label_indices, prediction_indices)
60+
union = tf.math.logical_or(label_indices, prediction_indices)
61+
62+
iou = tf.reduce_sum(tf.cast(intersection, tf.float32)) / tf.reduce_sum(tf.cast(union, tf.float32))
63+
if index == 1:
64+
accuracies['cosmic_iou'][p] = iou
65+
else :
66+
accuracies['neut_iou'][p] = iou
67+
68+
69+
70+
return accuracies

0 commit comments

Comments
 (0)