I'm trying to update cache while training a UNet. I'm rewriting some function from tf=2.13.0 to 2.2.0 because the latter is the only available version on my GPU cluster.
- Ubuntu 20.04
- Python 3.7.4
- Tensorflow 2.2.0
class Distance(tf.keras.metrics.Metric): def __init__(self, name='DistanceMetric', distance='cm', sigma=2.5, data_size=None, validation_size=None, points=None, point=None, percentile=None): super(Distance, self).__init__(name=name) self.counter = tf.Variable(initial_value=0, dtype=tf.int32) self.distance = distance self.sigma = sigma self.percentile = percentile if percentile is not None and point is not None: assert (type(percentile) == float) self.percentile_idx = tf.Variable(tf.cast(tf.round(percentile * validation_size), dtype=tf.int32)) else: self.percentile_idx = None self.point = point self.points = points self.cache = tf.Variable(initial_value=tf.zeros([validation_size, points]), shape=[validation_size, points]) print('cache shape', self.cache.shape) self.val_size = validation_size def update_state(self, y_true, y_pred, sample_weight=None): n, h, w, p = tf.shape(y_pred)[0], tf.shape(y_pred)[1], tf.shape(y_pred)[2], tf.shape(y_pred)[3] # print('y true', y_true) y_true = normal_distribution(self.sigma, y_true[:, :, 0], y_true[:, :, 1], h=h, w=w, n=n, p=p) # print('y true', y_true) if self.distance == 'cm': x1, y1 = cm(y_true, h=h, w=w, n=n, p=p) x2, y2 = cm(y_pred, h=h, w=w, n=n, p=p) d = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 # print('d shape before', d.shape) d = d[:, :, 0] # print('d shape after ', d.shape) elif self.distance == 'argmax': d = (tf.cast(tf.reduce_sum(((argmax_2d(y_true) - argmax_2d(y_pred)) ** 2), axis=1), dtype=tf.float32)) ** 0.5 temp = tf.minimum(self.counter + n, self.val_size-1) condition = tf.math.less_equal(self.counter, self.val_size) def true_fn(): with tf.control_dependencies([self.cache[self.counter:temp, :].assign(d[0:(temp-self.counter), :])]): # self.cache[self.counter:temp, :].assign(d[0:(temp-self.counter), :]) return self.cache def false_fn(): return self.cache # self.cache = tf.cond(condition, self.cache[self.counter:temp, :].assign(d[0:(temp-self.counter), :]), self.cache) self.cache = tf.cond(condition, true_fn, false_fn) self.counter.assign(self.counter + n) def result(self): if self.percentile_idx is not None: temp = tf.sort(self.cache[:self.val_size, self.point], axis=0, direction='ASCENDING') return temp[self.percentile_idx] elif self.point is not None: return tf.reduce_mean(self.cache[:, self.point], axis=0) else: return tf.reduce_mean(self.cache, axis=None) def reset_states(self): self.cache.assign(tf.zeros_like(self.cache)) self.counter.assign(0) if self.percentile is not None and self.point is not None: self.percentile_idx.assign(tf.cast(self.val_size * self.percentile, dtype=tf.int32))
self.cache
should be a tf.Variable (see def init), but I can't slice it to add new values.
ValueError: in user code:/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:571 train_function * outputs = self.distribute_strategy.run(/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run ** return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_re$ return self._call_for_each_replica(fn, args, kwargs)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_r$ return fn(*args, **kwargs)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:543 train_step ** self.compiled_metrics.update_state(y, y_pred, sample_weight)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:411 update_state metric_obj.update_state(y_t, y_p)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/keras/utils/metrics_utils.py:90 decorated update_op = update_state_fn(*args, **kwargs)/trinity/home/r084755/DRF_AI/distal-radius-fractures-x-pa-and-lateral-to-clinic/Code files/LandmarkDetection.py:163 update_state self.cache = tf.cond(condition, true_fn, false_fn)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py:1392 cond_for_tf_v2 return cond(pred, true_fn=true_fn, false_fn=false_fn, strict=True, name=name)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py:507 new_func return func(*args, **kwargs)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py:1177 cond return cond_v2.cond_v2(pred, true_fn, false_fn, name)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/ops/cond_v2.py:84 cond_v2 op_return_value=pred)/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:981 func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs)/trinity/home/r084755/DRF_AI/distal-radius-fractures-x-pa-and-lateral-to-clinic/Code files/LandmarkDetection.py:157 true_fn with tf.control_dependencies([self.cache[self.counter:temp, :].assign(d[0:(temp-self.counter), :])]):/opt/ohpc/pub/easybuild/software/TensorFlow/2.2.0-fosscuda-2019b-Python-3.7.4/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py:1160 assign raise ValueError("Sliced assignment is only supported for variables")ValueError: Sliced assignment is only supported for variables
Please can somebody help me. I'm unexperienced with tf. I've tried many options, but keep running into new issues.