Coder Social home page Coder Social logo

ayush1997 / xvision Goto Github PK

View Code? Open in Web Editor NEW
304.0 28.0 126.0 7.46 MB

Chest Xray image analysis using Deep learning !

Python 64.56% Jupyter Notebook 35.44%
deep-learning vggnet computer-vision medical-imaging chest-xray-images transfer-learning tensorflow python

xvision's Introduction

Xvision

Chest Xray image analysis using Deep Learning and exploiting Deep Transfer Learning technique for it with Tensorflow.

The maxpool-5 layer of a pretrained VGGNet-16(Deep Convolutional Neural Network) model has been used as the feature extractor here and then further trained on a 2-layer Deep neural network with SGD optimizer and Batch Normalization for classification of Normal vs Nodular Chest Xray Images.

Nodular vs Normal Chest Xray

Some specifications

Property Values
Pretrained Model VggNet-16
Optimizer used stochastic gradient descent(SGD)
Learning rate 0.01
Mini Batch Size 20
Epochs 20
2 Layers 512x512
GPU trained on Nvidia GEFORCE 920M

Evaluation

Confusion Matrix and Training Error Graph

Normal Nodule
Precision 0.7755102 0.55555556
Recall 0.76 0.57692308

Accuracy : 69.3333 %

DataSet

openi.nlm.nih.gov has a large base of Xray,MRI, CT scan images publically available.Specifically Chest Xray Images have been scraped, Normal and Nodule labbeled images are futher extrated for this task.

How to use ?

The above code can be used for Deep Transfer Learning on any Image dataset to train using VggNet as the PreTrained network.

Steps to follow

  1. Download Data- the script download images and saves corresponding disease label in json format.

python scraper.py <path/to/folder/to/save/images>

  1. Follow the scraper/process.ipynb notebook for Data processing and generate
  • Training images folder - All images for training
  • Testing images Folder - All images for testing
  • Training image labels file - Pickled file with training labels
  • Testing image labels file - Pickled file with testing labels
  1. Extract features(CNN Codes) from the maxpool:5 layer of PreTrained CovNet(VggNet) and save them beforehand for faster training of Neural network.

    python train.py <Training images folder> <Testing image folder> <Train images codes folder > <Test images codes folder>

    • Train images codes folder - Path where training images codes will be stored
    • Test images codes folder - Path where testing images codes will be stored
  2. The extracted features are now used for training our 2-Layer Neural Network from scratch.The computed models are saved as tensorflow checkpoint after every Epoch.

    python train_model.py <Training images folder> <Train images codes folder> <Training image labels file> <Folder to save models>

  3. Finally the saved models are used for making predictions.Confusion Matrix is used as the Performance Metrics for this classifcation task.

    python test_model.py <Testing images folder> <Test images codes folder> <Testing image labels file> <Folder with saved models>

Some Predictions

Alt text

References

  1. Learning to Read Chest X-Rays: Recurrent Neural Cascade Model for Automated Image Annotation
  1. Deep Convolutional Neural Networks for Computer-Aided Detection: CNN Architectures, Dataset Characteristics and Transfer Learning

Contribute

If you want to contribute and add new feature feel free to send Pull request here :D

To report any bugs or request new features, head over to the Issues page

To-do

  • Implement saliency map or use Deconv for better visualizations.

xvision's People

Contributors

asmitks avatar ayush1997 avatar tomaszrewak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xvision's Issues

Problem with process.ipynb

Hey Ayush,
While using your code I ran into a problem and saw that your training image list in your jupyter notebook was looping for only 177 images and not the whole dataset and I couldn't find any solution regarding it. If you could help me with this it would be a very big help.

Thank you.

Code does not run as it is

Is there a version dependency of "process.ipynb". When try to run the code as it is I get lots of errors in
Jupiter with Python 3, eg, following code does not run:
"c.items().sort(key=lambda x: x[0])"
has to be changed to ,
"sorted(c.items(), key=lambda x: x[0])"
No Print statements runs

input image size

Hello Mate ,

Greate job done !, can you please tell me how are you dealing with the input ?, as the xray jpg are single color Channel images and VGG input tensor is i think of 3 channels .
[email protected]
Thanks

how can I run ?

Dear
I cannot understand the file/folders.

In DeepLearning folder, I made two folder : normal and abnormal. And I copyied image files to each folder.

How can I label ? The labels (normal and abnormal) are folder name.

Thank you.
Sincerely.

train_model.py and test_model.py are reversed

Files train_model.py and test_model.py are reversed. Right now (despite what readme says) one has to run test_model.py first and then train_model.py to validate the model. It seems that the content of these files is simply swapped.

Error while running test_model.py : Incompatible shapes

Hi Ayush,

I am getting below error while running test_model.py. Could you help me on this please ? I have Python 3.2.5 and tensorflow 1.0 on Windows platform.

Traceback (most recent call last):
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\client\session.py", line 1022, in _do_call
return fn(*args)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\client\session.py", line 1004, in run_fn
status, run_metadata)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\contextlib.py", li
ne 66, in exit
next(self.gen)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\framework\errors_impl.py", line 469, in raise_exception_on_not_ok

status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shape
s: [9] vs. [25]
[[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/tas
k:0/cpu:0"](ArgMax_1, ArgMax_2)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "test_model.py", line 189, in
acc,pred,s,actual = sess1.run([accuracy,predicted_y,soft,actual_y], feed_dic
t={x: content_features,y: test_label})
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\client\session.py", line 767, in run
run_metadata_ptr)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\client\session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\client\session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\client\session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shape
s: [9] vs. [25]
[[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/tas
k:0/cpu:0"](ArgMax_1, ArgMax_2)]]

Caused by op 'Equal', defined at:
File "test_model.py", line 153, in
correct_prediction = tf.equal(predicted_y, actual_y)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\ops\gen_math_ops.py", line 721, in equal
result = _op_def_lib.apply_op("Equal", x=x, y=y, name=name)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\framework\op_def_library.py", line 763, in apply_op
op_def=op_def)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tens
orflow\python\framework\ops.py", line 2395, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\goyalr\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1264, in init self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Incompatible shapes: [9] vs. [25]
[[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax_1, ArgMax_2)]]

Can not run train_model.py

Hello PHD,
I am trying to
python train_model.py

The problem is :
['DeepLearning/train_model.py', 'final_train_images_calc_nodule_only/', 'train-code/', 'training_labels_calc_nodule_only', 'xx.models']
train_labels 177
161
Traceback (most recent call last):
File "DeepLearning/train_model.py", line 174, in
sess2.run(tf.initialize_all_variables())

Thanks!

data

can't download the data because of failed connection

can not run train.py

Hi,
I'm trying to run train.py and getting following error

161
j= 0
(20, 224, 224, 3) Image Shape
Traceback (most recent call last):
File train.py", line 131, in
content_features =get_content_feature(img_4d).reshape((get_content_feature(img_4d).shape[0],77512))
File "train.py", line 87, in get_content_feature 'vgg/dropout/random_uniform:0': [[1.0]]
File "tensorflow\python\framework\ops.py", line 575, in eval return _eval_using_default_session(self, feed_dict, self.graph, session)
File "tensorflow\python\framework\ops.py", line 3633, in _eval_using_default_session return session.run(tensors, feed_dict)
File "tensorflow\python\client\session.py", line 766, in run run_metadata_ptr)
File "tensorflow\python\client\session.py", line 943, in _run % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1, 1) for Tensor 'vgg/dropout/random_uniform:0', which has shape '(?, 4096)'

Could you help me with this. I'm new to python.

Thanks.

Error when running train_model.py

I got the error as below:

2017-08-07 16:24:09.904701: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-07 16:24:09.904717: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-07 16:24:09.904732: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-07 16:24:09.904735: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-08-07 16:24:10.154733: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.154797: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.154878: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.154962: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.155013: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.155095: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.155153: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
2017-08-07 16:24:10.155203: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
Traceback (most recent call last):
File "train_model.py", line 174, in
saver.restore(sess1, os.getcwd()+"/"+sys.argv[4]+"/"+"my-model-"+str(epoch-1)+".ckpt")
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1548, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

Caused by op u'save/RestoreV2', defined at:
File "train_model.py", line 161, in
saver = tf.train.Saver()
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1139, in init
self.build()
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1170, in build
restore_sequentially=self._restore_sequentially)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 691, in build
restore_sequentially, reshape)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 407, in _AddRestoreOps
tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 247, in restore_op
[spec.tensor.dtype])[0])
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/gen_io_ops.py", line 640, in restore_v2
dtypes=dtypes, name=name)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/min.jiang/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in init
self._traceback = _extract_stack()

NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /Users/min.jiang/Documents/DEEPLEARNING/CODE/Xvision/DeepLearning/../xvision_test/model//my-model-19.ckpt
[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.