tf_unet
tf_unet copied to clipboard
Question about error_rate: IndexError: tuple index out of range
Thanks for providing such a great code. I met some problems while processing my own data. I trained the net with the same size training data: 522x522
data_provider = image_util.ImageDataProvider("data/Gland2/train/*.png")
net = unet.Unet(layers=3, features_root=16, channels=3, n_class=2)
trainer = unet.Trainer(net)
path = trainer.train(data_provider, "result/test1", training_iters=16, epochs=10, display_step=2)
and then I referred to #146 and used the same way to test on my own data which is the same size: 522x522. (But my image data has three channels)
test_data = np.zeros((1,522,522,3))
img = np.array(Image.open("data/Gland2/test/testA_1.png"), dtype=np.float32)
test_data[0,...,0] = img
The result showed:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (522,522,3) into shape (522,522)
I used function reshape to solve this bug:
reimg = img.reshape((522*3,522))
retestdata = test_data.reshape((1,1566,522,1))
retestdata[0,...,0] = reimg
test_data = retestdata.reshape((1,522,522,3))
prediction = net.predict(path,test_data)
Even though it seemed to be solved, I am not sure whether it would change the order or the structure of the original test data. If it can not be done like this, please tell me.
And then I followed the Usage: (where the label is the GT map)
label = np.array(Image.open("data/Gland2/crop/testA_1.png"), dtype=np.float32)
unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape))
It came out:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "tf_unet/util.py", line 86, in crop_to_shape
offset1 = (data.shape[2] - shape[2])//2
IndexError: tuple index out of range
And here is the shape:
>>> print label.shape
(522, 522)
>>> print prediction.shape
(1, 480, 480, 2)
Any help is appreciated, Thanks a lot!
Sorry for the late reply. Maybe this is still of help.
The reshaping looks a bit convoluted. Adding a new axis can be done like this test_data = img[np.newaxis,...]
As for the second case, label is expected to have 3 dimensions not 2
Thanks for helping! :D I am kind of confused. Should I keep the label and the prediction same shape? I mean the prediction shape is (1,480,480,2). But my original GT image(the label) is (522,522), should I reshape the label into (1,480,480,2)? Or as long as the label shape to be [batches, nx, ny, channels] would be fine?
I got error as below when I set the label shape to be [1,522,522,1]
Traceback (most recent call last): File "unet_script.py", line 20, in <module> img = util.combine_img_prediction(test_data, Label, prediction) File "/home/chutz/Desktop/Unet-Tensorflow/tf_unet/tf_unet/util.py", line 102, in combine_img_prediction to_rgb(crop_to_shape(gt[..., 1], pred.shape).reshape(-1, ny, 1)), IndexError: index 1 is out of bounds for axis 3 with size 1
I'm not quite sure what is going on but shouldn't your GT be of the shape [1,522,522,2]?