使用NiftyNet训练VNet分割模型时遇ValueError报错求助
Let's tackle your issues one by one, starting with the critical error halting your training, then addressing the deprecation warning.
1. Fixing the ValueError: Dimension -1 must be >= 0
This error almost always ties to mismatched tensor shapes or broken dynamic dimension inference in your model/data pipeline. Here are the most actionable fixes:
Verify input and label shape consistency
Your CT images and segmentation masks must have identical spatial dimensions (height, width, depth) and compatible channel counts. For example:- If your CT is a 3D volume with shape
(512, 512, 32, 1)(single channel), your mask should be(512, 512, 32, 1)or(512, 512, 32)(depending on your model's output layer setup).
Manually check a sample pair with numpy to confirm:
import numpy as np ct_img = np.load("sample_ct.npy") # or load via PIL/OpenCV for slice-based data seg_mask = np.load("sample_mask.npy") print(f"CT shape: {ct_img.shape}, Mask shape: {seg_mask.shape}")Ensure all spatial axes match exactly.
- If your CT is a 3D volume with shape
Explicitly define input dimensions in your config
Open yourconfig.iniand make sure theinput_shapeparameter is fully specified (no ambiguous dimensions). For a 3D VNet, it should look like:input_shape = (128, 128, 64, 1) # (height, width, depth, channels)This prevents TensorFlow from trying to infer a dimension with
-1—a calculation that fails if it can't derive the correct value from preceding layers.Debug your data pipeline
Add print statements in your data loader/generator code to output the shape of each batch before it's fed to the model:# Inside your data generator loop for x_batch, y_batch in data_generator: print(f"Batch input shape: {x_batch.shape}, Batch label shape: {y_batch.shape}") # Proceed to feed batch to model...Look for unexpected shapes (e.g., a missing depth dimension, or a channel count mismatch) that could trigger the
-1dimension error during layer computations.
2. Resolving the TensorFlow Deprecation Warning
The warning about retry from tensorflow.contrib.learn is a legacy issue from using an older TensorFlow 1.x version (where contrib modules were common but later deprecated). Here's how to handle it:
Option 1: Ignore the warning
This warning doesn't block training—it's just a heads-up that theretryfunction will be removed in future versions. If your training works once you fix the shape error, you can safely ignore it for now.Option 2: Suppress warnings programmatically
Add these lines at the top of your training script to filter out deprecation warnings:import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) import tensorflow as tf tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)Option 3: Migrate to TensorFlow 2.x
Long-term, consider updating your codebase to use TensorFlow 2.x. Thecontribmodule is gone in TF2, and you can replace the deprecated dataset code with the nativetf.dataAPI, which is more stable and actively supported.
内容的提问来源于stack exchange,提问作者fepegar

