jakeret / tf_unet

Generic U-Net Tensorflow implementation for image segmentation
GNU General Public License v3.0
1.9k stars 748 forks source link

ValueError: all the input array dimensions except for the concatenation axis must match exactly #239

Closed nohboogy closed 5 years ago

nohboogy commented 5 years ago

I have a problem when I train it.

from tf_unet import unet, util, image_util
import numpy as np

#preparing data loading
data_provider = image_util.ImageDataProvider("/home/wooseok/Downloads/02/*.png")
output_path = "/home/wooseok/Downloads/output"

#setup & training
net = unet.Unet(layers=3, features_root=64, channels=data_provider.channels, n_class=data_provider.n_class)
print(data_provider.channels)
trainer = unet.Trainer(net)
path = trainer.train(data_provider, output_path, training_iters=32, epochs=100)

#verification

prediction = net.predict(path, data)
unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape))
img = util.combine_img_prediction(data, label, prediction)
util.save_image(img, "prediction.jpg")

and this is the error message when I run it

Connected to pydev debugger (build 183.4886.43) /home/wooseok/anaconda3/lib/python3.6/site-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters Number of files used: 4 Number of channels: 4 Number of classes: 2 2019-02-07 17:27:08,970 Layers 3, features 64, filter size 3x3, pool size: 2x2 2019-02-07 17:27:10,911 Removing '/home/wooseok/PycharmProjects/untitled2/prediction' 2019-02-07 17:27:10,911 Removing '/home/wooseok/Downloads/output' 2019-02-07 17:27:10,911 Allocating '/home/wooseok/PycharmProjects/untitled2/prediction' 2019-02-07 17:27:10,911 Allocating '/home/wooseok/Downloads/output' 2019-02-07 17:27:11.025751: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-02-07 17:27:11.026130: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: name: GeForce RTX 2080 major: 7 minor: 5 memoryClockRate(GHz): 1.71 pciBusID: 0000:01:00.0 totalMemory: 7.76GiB freeMemory: 7.13GiB 2019-02-07 17:27:11.026139: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0 2019-02-07 17:27:11.214979: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-02-07 17:27:11.214999: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0 2019-02-07 17:27:11.215003: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N 2019-02-07 17:27:11.215155: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6861 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080, pci bus id: 0000:01:00.0, compute capability: 7.5) 2019-02-07 17:27:11.215557: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance. 2019-02-07 17:27:14,720 Verification error= 0.2%, loss= 0.6817 Traceback (most recent call last): File "/home/wooseok/pycharm-2018.3.2/helpers/pydev/pydevd.py", line 1741, in main() File "/home/wooseok/pycharm-2018.3.2/helpers/pydev/pydevd.py", line 1735, in main globals = debugger.run(setup['file'], None, None, is_module) File "/home/wooseok/pycharm-2018.3.2/helpers/pydev/pydevd.py", line 1135, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/home/wooseok/pycharm-2018.3.2/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/wooseok/PycharmProjects/untitled2/test.py", line 12, in path = trainer.train(data_provider, output_path, training_iters=32, epochs=100) File "/home/wooseok/.local/lib/python3.6/site-packages/tf_unet-0.1.2-py3.6.egg/tf_unet/unet.py", line 423, in train pred_shape = self.store_prediction(sess, test_x, test_y, "_init") File "/home/wooseok/.local/lib/python3.6/site-packages/tf_unet-0.1.2-py3.6.egg/tf_unet/unet.py", line 475, in store_prediction img = util.combine_img_prediction(batch_x, batch_y, prediction) File "/home/wooseok/.local/lib/python3.6/site-packages/tf_unet-0.1.2-py3.6.egg/tf_unet/util.py", line 121, in combine_img_prediction to_rgb(pred[..., 1].reshape(-1, ny, 1))), axis=1) ValueError: all the input array dimensions except for the concatenation axis must match exactly

I think this is because of size of channel but if I change the value of channel, another error message comes out.

(1760, 600, 4) (1760, 600, 3) (1760, 600, 3) this is size of each component in util.py

img = np.concatenate((to_rgb(crop_to_shape(data, pred.shape).reshape(-1, ny, ch)),
to_rgb(crop_to_shape(gt[..., 1], pred.shape).reshape(-1, ny, 1)), 
to_rgb(pred[..., 1].reshape(-1, ny, 1))), axis=1)

Someone who can solve this problem?

nohboogy commented 5 years ago

I solved this problem while fixing code util.py

def to_rgb(img):
    """
    Converts the given array into a RGB image. If the number of channels is not
    3 the array is tiled such that it has 3 channels. Finally, the values are
    rescaled to [0,255) 

    :param img: the array to convert [nx, ny, channels]

    :returns img: the rgb image [nx, ny, 3]
    """
    img = np.atleast_3d(img)
    channels = img.shape[2]
    if channels < 3:
        img = np.tile(img, 3)
    else:
        img = img[:, :, 0:3]

    img[np.isnan(img)] = 0
    img -= np.amin(img)
    if np.argmax(img) != 0:
        img /= np.amax(img)

    img *= 255
    return img