NifTK / NiftyNet

[unmaintained] An open-source convolutional neural networks platform for research in medical image analysis and image-guided therapy
http://niftynet.io
Apache License 2.0
1.37k stars 404 forks source link

Classification application cannot run through mnist dataset #312

Closed yuanpeng5 closed 5 years ago

yuanpeng5 commented 5 years ago

I have already tried "Segmentation" application, and everything works fine. Now I am trying to run "Classification" application with with mnist dataset as a test. I have generate all image files as 28x28x1 png files and label files as 1x1x1 png files but after " INFO:niftynet: Parameters from random initialisations ... "

It keeps give me the following error:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "net_classify.py", line 8, in <module>
    sys.exit(main())
  File "/home/pwu/github/NiftyNet-1/niftynet/__init__.py", line 142, in main
    app_driver.run(app_driver.app)
  File "/home/pwu/github/NiftyNet-1/niftynet/engine/application_driver.py", line 202, in run
    loop_status=loop_status)
  File "/home/pwu/github/NiftyNet-1/niftynet/engine/application_driver.py", line 328, in loop
    ApplicationDriver.loop_step(application, iter_msg)
  File "/home/pwu/github/NiftyNet-1/niftynet/engine/application_driver.py", line 359, in loop_step
    feed_dict=iteration_message.data_feed_dict)
  File "/home/pwu/niftynet4/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 877, in run
    run_metadata_ptr)
  File "/home/pwu/niftynet4/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1100, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/pwu/niftynet4/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1272, in _do_run
    run_metadata)
  File "/home/pwu/niftynet4/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1291, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 0-th value returned by pyfunc_0 is float, but expects int32
         [[Node: PyFunc = PyFunc[Tin=[DT_INT64], Tout=[DT_INT32, DT_INT32, DT_INT32, DT_INT32], token="pyfunc_0", _device="/device:GPU:0"](arg0)]]
         [[Node: worker_0/cond/train/IteratorGetNext = IteratorGetNext[output_shapes=[[4,28,28,1,1,1], [4,7], [4,1,1,1,1,1], [4,7]], output_types=[DT_INT32, DT_INT32, DT_INT32, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"](worker_0/cond/train/OneShotIterator)]]
         [[Node: worker_1/cond/train/IteratorGetNext/_37 = _HostRecv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_43_worker_1/cond/train/IteratorGetNext", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:1"]()]]

It looks like something is the output_types are define as int32, but I didn't find anywhere I can change that.

This is my config.ini

[img]
csv_file = /mnist_test/config/model/img.csv
filename_contains = train
spatial_window_size = (28, 28)
interp_order = -1
loader = opencv

[label]
csv_file = /mnist_test/config/model/label.csv
filename_contains = label
spatial_window_size = (1, 1)
interp_order = -1
loader = opencv

[SYSTEM]
cuda_devices = 0,1,2
num_threads = 10
num_gpus = 3
#dataset_split_file = /mnist_test/config/model/dataset_split.csv

[NETWORK]
name = mnist_net
activation_function = relu
batch_size = 4
normalise_foreground_only=False
queue_length = 20
window_sampling = resize

[TRAINING]
sample_per_volume = 1
random_flipping_axes=-1
lr = 0.0003
loss_type = CrossEntropy
starting_iter = 0
save_every_n = 200
max_iter = 2000
max_checkpoints = 10

do_elastic_deformation = False
#deformation_sigma = 50
#num_ctrl_points = 6
#proportion_to_deform=0.9
exclude_fraction_for_validation=0.1
exclude_fraction_for_inference=0.1

validation_every_n = 10
validation_max_iter = 1

[INFERENCE]
#border = (92, 92, 0)
inference_iter = -1
save_seg_dir = /mnist_test/result20190123/output
output_interp_order = -1
spatial_window_size = (28,28)

############################ custom configuration sections
[CLASSIFICATION]
image = img
label = label
output_prob = False
num_classes = 10
label_normalisation = False

I didn't find any detailed "Classification" tutorials in the demo part, so I created a mnist test to warm up. If anyone can shed me some light what is wrong here, that would be very much appreciated.

yuanpeng5 commented 5 years ago

I found the reason, it seems in niftynet

class ImageReader(Layer):

It is required that input numpy array dtype must be the same as output numpy array dtype. So if we use png file as input numpy array, the output numpy array is required to be int32. So we can just change the input numpy array dtype to float64, the problem would be solved.

wyli commented 5 years ago

thanks for tracking this down @yuanpeng5, the reader infers dtypes by https://github.com/NifTK/NiftyNet/blob/v0.4.0/niftynet/io/misc_io.py#L59-L88 so, perphaps setting interp_order of [image] section in the config to 0 or 1 would resolve this issue? please let me know if this is not the case.

JunMa11 commented 5 years ago

Hi, @yuanpeng5. I alwo want to try the classification application. My label file is a csv file with the following format

filename label
img-1.nii 1
img-2.nii 0
img-3.nii 1
img-4.nii 0

...

could you tell me how to map this label file to the input data in the niftynet?

yuanpeng5 commented 5 years ago

@EdwardMa12593 Sorry, I didn't see this, but just in case you still need this, you can name your images files as,

1-img.nii 2-img.nii ...

and generate label files as 1-label.tiff 2-label.tiff ...

and in your .ini config specify

[img] filename_contains = img

[label] filename_contains = label

NiftyNet should generate the .csv file automatically, or you can write your own .csv files and use them directly in the .ini file.

yuanpeng5 commented 5 years ago

@wyli Yes, I think you are right, this should also fix the problem, I didn't find that part of code before.

JunMa11 commented 5 years ago

@yuanpeng5 Get it. Thanks for your help very much.

koriavinash1 commented 5 years ago

Is it possible to get accuracy in logs?