microsoft / Recursive-Cascaded-Networks

[ICCV 2019] Recursive Cascaded Networks for Unsupervised Medical Image Registration
https://arxiv.org/abs/1907.12353
MIT License
361 stars 87 forks source link

Get flow output #48

Closed Shubham0209 closed 3 years ago

Shubham0209 commented 3 years ago

Hello @zsyzzsoft !!

Thank you for help on previous issue. Now, while running the demo.py file I also want to output the flow field. For that what changes do I have to make in the demo.py file? As suggested by you - add real_flow in keys between lines 90-100. I did that as well, see below:

sess = tf.Session()

    saver = tf.train.Saver(tf.get_collection(
        tf.GraphKeys.GLOBAL_VARIABLES))
    checkpoint = args.checkpoint
    saver.restore(sess, checkpoint)
    tflearn.is_training(False, session=sess)

    keys = sum([['real_flow_{}'.format(i), 'warped_moving_{}'.format(i)] for i in range(len(framework.network.stems))], ['real_flow'])
    gen = [{'id1': np.ones((1,)), 'id2': np.ones((1,)),
        'voxel1': np.reshape(img_fixed, [1, 280, 280, 84, 1]), 'voxel2': np.reshape(img_moving, [1, 280, 280, 84, 1])}]
    results = framework.validate(sess, gen, keys=keys, summary=False)

It seems these line of code isn't executed. I get the moving and fixed processed images in 'output' folder but i also get following warning while running the code:

WARNING:tensorflow:From demo.py:90: The name tf.GraphKeys is deprecated. Please use tf.compat.v1.GraphKeys instead.

Traceback (most recent call last):
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 1365, in _do_call
    return fn(*args)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 1350, in _run_fn
    target_list, run_metadata)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 1443, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [5,5,2,512,9] rhs shape= [2,2,2,512,9]
         [[{{node save/Assign_20}}]]
         [[GroupCrossDeviceControlEdges_0/save/restore_all/_2]]
  (1) Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [5,5,2,512,9] rhs shape= [2,2,2,512,9]
         [[{{node save/Assign_20}}]]
0 successful operations.
0 derived errors ignored.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 1290, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
    run_metadata_ptr)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 1180, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 1359, in _do_run
    run_metadata)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\client\session.py", line 1384, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [5,5,2,512,9] rhs shape= [2,2,2,512,9]
         [[node save/Assign_20 (defined at C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py:1762) ]]
         [[GroupCrossDeviceControlEdges_0/save/restore_all/_2]]
  (1) Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [5,5,2,512,9] rhs shape= [2,2,2,512,9]
         [[node save/Assign_20 (defined at C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py:1762) ]]
0 successful operations.
0 derived errors ignored.

Original stack trace for 'save/Assign_20':
  File "demo.py", line 287, in <module>
    main()
  File "demo.py", line 90, in main
    tf.GraphKeys.GLOBAL_VARIABLES))
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 828, in __init__
    self.build()
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 840, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 878, in _build
    build_restore=build_restore)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 508, in _build_internal
    restore_sequentially, reshape)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 350, in _AddRestoreOps
    assign_ops.append(saveable.restore(saveable_tensors, shapes))
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saving\saveable_object_util.py", line 73, in restore
    self.op.get_shape().is_fully_defined())
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\ops\state_ops.py", line 227, in assign
    validate_shape=validate_shape)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\ops\gen_state_ops.py", line 69, in assign
    use_locking=use_locking, name=name)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3371, in create_op
    attrs, op_def, compute_device)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3440, in _create_op_internal
    op_def=op_def)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1762, in __init__
    self._traceback = tf_stack.extract_stack()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "demo.py", line 287, in <module>
    main()
  File "demo.py", line 92, in main
    saver.restore(sess, checkpoint)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 1326, in restore
    err, "a mismatch between the current graph and the graph")
tensorflow.python.framework.errors_impl.InvalidArgumentError: Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

2 root error(s) found.
  (0) Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [5,5,2,512,9] rhs shape= [2,2,2,512,9]
         [[node save/Assign_20 (defined at C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py:1762) ]]
         [[GroupCrossDeviceControlEdges_0/save/restore_all/_2]]
  (1) Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [5,5,2,512,9] rhs shape= [2,2,2,512,9]
         [[node save/Assign_20 (defined at C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py:1762) ]]
0 successful operations.
0 derived errors ignored.

Original stack trace for 'save/Assign_20':
  File "demo.py", line 287, in <module>
    main()
  File "demo.py", line 90, in main
    tf.GraphKeys.GLOBAL_VARIABLES))
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 828, in __init__
    self.build()
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 840, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 878, in _build
    build_restore=build_restore)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 508, in _build_internal
    restore_sequentially, reshape)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saver.py", line 350, in _AddRestoreOps
    assign_ops.append(saveable.restore(saveable_tensors, shapes))
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\training\saving\saveable_object_util.py", line 73, in restore
    self.op.get_shape().is_fully_defined())
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\ops\state_ops.py", line 227, in assign
    validate_shape=validate_shape)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\ops\gen_state_ops.py", line 69, in assign
    use_locking=use_locking, name=name)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3371, in create_op
    attrs, op_def, compute_device)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3440, in _create_op_internal
    op_def=op_def)
  File "C:\Users\shubh\anaconda3\envs\myenv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1762, in __init__
    self._traceback = tf_stack.extract_stack()

Please help!!

zsyzzsoft commented 3 years ago

The input shape is inconsistent with the network. Please do not modify the original reshape of [1, 128, 128, 128, 1]. The code automatically does crop and resize to 128^3.

Shubham0209 commented 3 years ago

And is rest of the code correct to get the flow output? Is the statement keys = sum([['real_flow_{}'.format(i), 'warped_moving_{}'.format(i)] for i in range(len(framework.network.stems))], ['real_flow']) correct?

zsyzzsoft commented 3 years ago

Looks correct.