amiralansary/rl-medical

Upgrade to latest tensorpack dependency: DQN.py returning a 'NotImplementedError'

Closed this issue · 5 comments

Thanks for your project. I got some troubles when I tried to reproduce your example 'LandmarkDetection'.

  1. When I execute the command with your default setting , I get an error listed below that I cannot solve it. My environment is
  • python 3.5.2
  • tensorflow 1.13.1 cpu version
  • tensorpack 0.9.4
  • Ubuntu 16.04.6 LTS
# command
python3 DQN.py --task train --algo DQN --files './data/filenames/image_files.txt' './data/filenames/landmark_files.txt'
# error
Traceback (most recent call last):
  File "DQN.py", line 263, in <module>
    launch_train_with_config(config, SimpleTrainer())
  File "/home/ty/.local/lib/python3.5/site-packages/tensorpack/train/interface.py", line 90, in launch_train_with_config
    model.get_input_signature(), input,
  File "/home/ty/.local/lib/python3.5/site-packages/tensorpack/utils/argtools.py", line 200, in wrapper
    value = func(*args, **kwargs)
  File "/home/ty/.local/lib/python3.5/site-packages/tensorpack/graph_builder/model_desc.py", line 92, in get_input_signature
    return [TensorSpec(shape=p.shape, dtype=p.dtype, name=get_op_tensor_name(p.name)[0]) for p in inputs]
  File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ty/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 5253, in get_controller
    yield g
  File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ty/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 5061, in get_controller
    yield default
  File "/home/ty/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 5253, in get_controller
    yield g
  File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ty/.local/lib/python3.5/site-packages/tensorflow/python/eager/context.py", line 415, in _mode
    yield
  File "/home/ty/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 5253, in get_controller
    yield g
  File "/home/ty/.local/lib/python3.5/site-packages/tensorpack/graph_builder/model_desc.py", line 86, in get_input_signature
    inputs = self.inputs()
  File "/home/ty/.local/lib/python3.5/site-packages/tensorpack/graph_builder/model_desc.py", line 116, in inputs
    raise NotImplementedError()
NotImplementedError
  1. according to a tensorpack issue , get_tf_version_number was deprecated by tensorflow and you should useget_tf_version_tuple in your tensorpack-medical .

Same issue. This is probably due to a version mismatch between tensorpack and tensorpack-medical. @crypdick could you please link to the older versions(both of tensorpack and tensorpack-medical) when it was working? #1 (comment)

@ArjitJ It's actually a version mismatch issue. After re-installing tensorpack to 0.8.0, it works fine now.

@TianYang-TY @ArjitJ I updated the readme file to restrict the tensorflow and tensorpack versions to 1.6 and 0.8.

Although I am currently busy with other projects, I will try to update the repo with the latest versions of tensorpack. Any contribution here will be much appreciated!

These are the changes needed to run "LandmarkDetection/DQN" with tensorpack master, following the tensorpack changelog:

diff --git i/examples/LandmarkDetection/DQN/DQNModel.py w/examples/LandmarkDetection/DQN/DQNModel.py
index d9a5bf5..3cd8e35 100644
--- i/examples/LandmarkDetection/DQN/DQNModel.py
+++ w/examples/LandmarkDetection/DQN/DQNModel.py
@@ -29,7 +29,7 @@ class Model2D(ModelDesc):
         self.image_shape = image_shape
         self.num_actions = num_actions
 
-    def _get_inputs(self):
+    def inputs(self):
         # Use a combined state for efficiency.
         # The first h channels are the current state, and the last h channels are the next state.
         return [InputDesc(tf.uint8,
@@ -48,7 +48,7 @@ class Model2D(ModelDesc):
     def get_DQN_prediction(self, image):
         return self._get_DQN_prediction(image)
 
-    def _build_graph(self, inputs):
+    def build_graph(self, *inputs):
         comb_state, action, reward, isOver = inputs
         comb_state = tf.cast(comb_state, tf.float32)
         state = tf.slice(comb_state, [0, 0, 0, 0], [-1, -1, -1, self.channel], name='state')
@@ -80,11 +80,12 @@ class Model2D(ModelDesc):
 
         target = reward + (1.0 - tf.cast(isOver, tf.float32)) * self.gamma * tf.stop_gradient(best_v)
 
-        self.cost = tf.losses.huber_loss(target, pred_action_value,
+        cost = tf.losses.huber_loss(target, pred_action_value,
                                          reduction=tf.losses.Reduction.MEAN)
         summary.add_param_summary(('conv.*/W', ['histogram', 'rms']),
                                   ('fc.*/W', ['histogram', 'rms']))   # monitor all W
-        summary.add_moving_summary(self.cost)
+        summary.add_moving_summary(cost)
+        return cost
 
     def _get_optimizer(self):
         lr = tf.get_variable('learning_rate',initializer=1e-3, trainable=False)
@@ -105,8 +106,8 @@ class Model2D(ModelDesc):
                 ops.append(v.assign(G.get_tensor_by_name(new_name + ':0')))
         return tf.group(*ops, name='update_target_network')
 
-    
-    
+
+
 class Model3D(ModelDesc):
     def __init__(self, image_shape, channel, method, num_actions, gamma):
         """
@@ -124,7 +125,7 @@ class Model3D(ModelDesc):
         self.image_shape = image_shape
         self.num_actions = num_actions
 
-    def _get_inputs(self):
+    def inputs(self):
         # Use a combined state for efficiency.
         # The first h channels are the current state, and the last h channels are the next state.
         return [InputDesc(tf.uint8,
@@ -144,7 +145,7 @@ class Model3D(ModelDesc):
     def get_DQN_prediction(self, image):
         return self._get_DQN_prediction(image)
 
-    def _build_graph(self, inputs):
+    def build_graph(self, *inputs):
         comb_state, action, reward, isOver = inputs
         comb_state = tf.cast(comb_state, tf.float32)
         state = tf.slice(comb_state, [0, 0, 0, 0, 0], [-1, -1, -1, -1, self.channel], name='state')
@@ -175,13 +176,14 @@ class Model3D(ModelDesc):
             best_v = tf.reduce_sum(targetQ_predict_value * predict_onehot, 1)
 
         target = reward + (1.0 - tf.cast(isOver, tf.float32)) * self.gamma * tf.stop_gradient(best_v)
-        self.cost = tf.losses.huber_loss(target, pred_action_value,
+        cost = tf.losses.huber_loss(target, pred_action_value,
                                          reduction=tf.losses.Reduction.MEAN)
         summary.add_param_summary(('conv.*/W', ['histogram', 'rms']),
                                   ('fc.*/W', ['histogram', 'rms']))  # monitor all W
-        summary.add_moving_summary(self.cost)
+        summary.add_moving_summary(cost)
+        return cost
 
-    def _get_optimizer(self):
+    def optimizer(self):
         lr = tf.get_variable('learning_rate', initializer=1e-3, trainable=False)
         opt = tf.train.AdamOptimizer(lr, epsilon=1e-3)
         return optimizer.apply_grad_processors(
diff --git i/tensorpack_medical/models/conv3d.py w/tensorpack_medical/models/conv3d.py
index 5560f65..7482641 100644
--- i/tensorpack_medical/models/conv3d.py
+++ w/tensorpack_medical/models/conv3d.py
@@ -1,12 +1,11 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 # File: conv3d.py
-# Author: Yuxin Wu <ppwwyyxxc@gmail.com>
 # Modified: Amir Alansary <amiralansary@gmail.com>
 
 import tensorflow as tf
 from tensorpack import layer_register, VariableHolder
-from tensorpack.tfutils.common import get_tf_version_number
+from tensorpack.tfutils.common import get_tf_version_tuple
 from .tflayer import rename_get_variable, convert_to_tflayer_args
 
 from tensorpack_medical.utils.argtools import shape3d, shape5d, get_data_format3d
@@ -84,14 +83,14 @@ def Conv3D(
 
         out_channel = filters
         assert out_channel % split == 0
-        assert dilation_rate == (1, 1, 1) or get_tf_version_number() >= 1.5, 'TF>=1.5 required for group dilated conv'
+        assert dilation_rate == (1, 1, 1) or get_tf_version_tuple() >= (1, 5), 'TF>=1.5 required for group dilated conv'
 
         kernel_shape = shape3d(kernel_size)
         filter_shape = kernel_shape + [in_channel / split, out_channel]
         stride = shape5d(strides, data_format=data_format)
 
         kwargs = dict(data_format=data_format)
-        if get_tf_version_number() >= 1.5:
+        if get_tf_version_tuple() >= (1, 5):
             kwargs['dilations'] = shape4d(dilation_rate, data_format=data_format)
 
         W = tf.get_variable(
diff --git i/tensorpack_medical/models/tflayer.py w/tensorpack_medical/models/tflayer.py
index f2ed0ad..7f58b80 100644
--- i/tensorpack_medical/models/tflayer.py
+++ w/tensorpack_medical/models/tflayer.py
@@ -8,7 +8,7 @@ import tensorflow as tf
 import six
 import functools
 
-from tensorpack.tfutils.common import get_tf_version_number
+from tensorpack.tfutils.common import get_tf_version_tuple
 from tensorpack.tfutils.varreplace import custom_getter_scope
 
 from tensorpack_medical.utils.argtools import get_data_format3d
@@ -89,7 +89,7 @@ def rename_get_variable(mapping):
 
 
 def monkeypatch_tf_layers():
-    if get_tf_version_number() < 1.4:
+    if get_tf_version_tuple() < (1, 4):
         if not hasattr(tf.layers, 'Dense'):
             from tensorflow.python.layers.core import Dense
             tf.layers.Dense = Dense

Thanks @ppwwyyxx !
I updated the code with your changes and tested it. All good! Now it is compitable with tensorflow 1.14 and tensorpack 0.9.5