Giter Club home page Giter Club logo

lung-diseases-classifier's Introduction

Diseases Detection from Chest X-ray data

Machine Learning Capstone Project - Udacity MLND

Project Overview

With so many lung diseases people can get, here is just one example of diseases we can save if we find them out earlier. With the technology machine and computer power, the earlier identification of diseases, particularly lung disease, we can be helped to detect earlier and more accurately, which can save many many people as well as reduce the pressure on the system. The health system has not developed in time with the development of the population.

Analysis

Data Exploration

Exploratory Visualization

Datasets

  • File contents: this is a random sample (5%) of the full dataset: sample.zip: Contains 5,606 images with size 1024 x 1024 sample_labels.csv: Class labels and patient data for the entire dataset
  • Class descriptions: there are 15 classes (14 diseases, and one for "No findings") in the full dataset, but since this is drastically reduced version of the full dataset, some of the classes are sparse with the labeled as "No findings": Hernia - 13 images, Pneumonia - 62 images, Fibrosis - 84 images, Edema - 118 images, Emphysema - 127 images, Cardiomegaly - 141 images, Pleural_Thickening - 176 images, Consolidation - 226 images, Pneumothorax - 271 images, Mass - 284 images, Nodule - 313 images, Atelectasis - 508 images, Effusion - 644 images, Infiltration - 967 images, No Finding - 3044 images.
  • File contents: images_00x.zip: 12 files with 112,120 total images with size 1024 x 1024 README_ChestXray.pdf: Original README file BBox_list_2017.csv: Bounding box coordinates. Note: Start at x,y, extend horizontally w pixels, and vertically h pixels Data_entry_2017.csv: Class labels and patient data for the entire dataset
  • Class descriptions: there are 15 classes (14 diseases, and one for "No findings"). Images can be classified as "No findings" or one or more disease classes: Atelectasis, Consolidation, Infiltration, Pneumothorax, Edema, Emphysema, Fibrosis, Effusion, Pneumonia, Pleural_thickening, Cardiomegaly, Nodule Mass, Hernia.

Algorithms and Techniques

  • CNN
  • Spacial Transformer
  • VGG finetuning
  • Capsule Network

Architecture

Vanilla CNN

Optimized CNN

CapsNet

Metrics & Result

F-beta score with β = 0.5 to represent precision will be more important than recall in this case.

Result: In sample dataset:

Model Precision Recall F 0.5 score Accuracy Training time/ epoch no. parameters
Vanilla rgb 0.617 0.589 0.611 0.503 2 s 322793
Vanilla gray 0.577 0.48 0.555 0.517 2 s 321225
CNN + VGG 0.645 0.555 0.624 0.667 16 s 15252133
CNN + VGG + data 0.647 0.588 0.634 0.675 16 s 15240769
CNN + VGG + data + STN 0.642 0.614 0.636 0.677 19 s 15488051
CapsNet basic 0.614 0.599 0.611 0.581 75 s 14788864
CapsNet changed 0.735 0.073 0.261 0.575 37 s 12167424

In full dataset:

Model Precision Recall F 0.5 score Accuracy Training time/ epoch no. parameters
Vanilla rgb 0.672 0.594 0.655 0.672 53 s 322793
Vanilla gray 0.672 0.572 0.649 0.667 51 s 321225
CNN + VGG 0.675 0.619 0.663 0.688 384 s 15252133
CNN + VGG + data + STN 0.684 0.621 0.67 0.693 431 s 15488051
CapsNet basic 0.64 0.498 0.605 0.635 1815 s 14788864
CapsNet changed 0.625 0.474 0.588 0.625 856 s 12167424

Installation

$ sudo apt-get update
$ sudo apt-get install python3-pip python3-dev
$ pip3 install --upgrade pip
$ pip3 install jupyter

Tensorflow for GPU

$ pip3 install tensorflow==1.8.0     # Python 3.n; CPU support (no GPU support)
$ pip3 install tensorflow-gpu==1.8.0 # Python 3.n; GPU support 

Keras for GPU

$ pip3 install keras

Others

  • numpy
  • pandas
  • seaborn
  • matplotlib
  • opencv
  • glob
  • tqdm
  • sklearn
  • pickle

Note

  1. Run Data preprocessing first to create preprocessing file in Sample dataset before run other notebook for Sample dataset.

  2. Following are the file descriptions and URL’s from which the data can be obtained:

  • data sample/sample_labels.csv: Class labels and patient data for the sample dataset
  • data sample/Data_entry_2017.csv: Class labels and patient data for the full dataset
  • data sample/images/*: 10 chest X-ray images
  1. Following are the notebooks descriptions and python files descriptions, files log: Notebooks:
  • Capsule Network - FullDataset.ipynb: Capsule Network with my architecture in full dataset
  • Capsule Network - SampleDataset.ipynb: Capsule Network with my architecture in sample dataset
  • Capsule Network basic - FullDataset.ipynb: Capsule Network with Hinton's architecture in full dataset
  • Capsule Network basic - SampleDataset.ipynb: Capsule Network with Hinton's architecture in sample dataset
  • Data analysis - FullDataset.ipynb: Data analysis in full dataset
  • Data analysis - SampleDataset.ipynb: data analysis in sample dataset
  • Data preprocessing - SampleDataset.ipynb: Data preprocessing
  • Demo.ipynb: Demo prediction 20 samples
  • optimized CNN - FullDataset.ipynb: My optimized CNN architecture in full dataset
  • optimized CNN - SampleDataset.ipynb: My optimized CNN architecture in sample dataset
  • vanilla CNN - FullDataset.ipynb: Vanilla CNN in full dataset
  • vanilla CNN - SampleDataset.ipynb: Vanilla CNN in sample dataset

Python files

  • capsulelayers.py: capsule layer from XifengGuo
  • spatial_transformer.py: spatial transformer layser from hello2all So thank you guys for support me with capsule layer and spatial transformer layer in Keras-gpu

Log:

  • FullDataset Log: all log file in full dataset
  • SampleDataset Log: all log file in sample dataset

lung-diseases-classifier's People

Contributors

doduy 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

Watchers

 avatar  avatar  avatar  avatar  avatar

lung-diseases-classifier's Issues

question

'from utils import plot_log' What's the use of this code? Run out of error, ModuleNotFoundError: No module named 'utils'. After looking up the data, it is found that there is no such module. @DoDuy

OPTIMIZED CNN ERROR

While implemening every code in optimized cnn sample dataset,im getting error.
1.PRETRAINED MODEL ONLY AND CNN MODEL
Epoch 1/20

ValueError Traceback (most recent call last)
in
37 steps_per_epoch=int(train_labels.shape[0] / batch_size),
38 validation_data=(valid_tensors, valid_labels),
---> 39 epochs=epochs, callbacks=[checkpointer, log, earlystop], verbose=1)
40
41 # Show total training time

~\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1987 use_multiprocessing=use_multiprocessing,
1988 shuffle=shuffle,
-> 1989 initial_epoch=initial_epoch)
1990
1991 @doc_controls.do_not_generate_docs

~\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1182 _r=1):
1183 callbacks.on_train_batch_begin(step)
-> 1184 tmp_logs = self.train_function(iterator)
1185 if data_handler.should_sync:
1186 context.async_wait()

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\def_function.py in call(self, *args, **kwds)
883
884 with OptionalXlaContext(self._jit_compile):
--> 885 result = self._call(*args, **kwds)
886
887 new_tracing_count = self.experimental_get_tracing_count()

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
922 # In this case we have not created variables on the first call. So we can
923 # run the first trace but we should fail if variables are created.
--> 924 results = self._stateful_fn(*args, **kwds)
925 if self._created_variables and not ALLOW_DYNAMIC_VARIABLE_CREATION:
926 raise ValueError("Creating variables on a non-first call to a function"

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\function.py in call(self, *args, **kwargs)
3036 with self._lock:
3037 (graph_function,
-> 3038 filtered_flat_args) = self._maybe_define_function(args, kwargs)
3039 return graph_function._call_flat(
3040 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
3458 call_context_key in self._function_cache.missed):
3459 return self._define_function_with_shape_relaxation(
-> 3460 args, kwargs, flat_args, filtered_flat_args, cache_key_context)
3461
3462 self._function_cache.missed.add(call_context_key)

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\function.py in _define_function_with_shape_relaxation(self, args, kwargs, flat_args, filtered_flat_args, cache_key_context)
3380
3381 graph_function = self._create_graph_function(
-> 3382 args, kwargs, override_flat_arg_shapes=relaxed_arg_shapes)
3383 self._function_cache.arg_relaxed[rank_only_cache_key] = graph_function
3384

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3306 arg_names=arg_names,
3307 override_flat_arg_shapes=override_flat_arg_shapes,
-> 3308 capture_by_value=self._capture_by_value),
3309 self._function_attributes,
3310 function_spec=self.function_spec,

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes, acd_record_initial_resource_uses)
1005 _, original_func = tf_decorator.unwrap(python_func)
1006
-> 1007 func_outputs = python_func(*func_args, **func_kwargs)
1008
1009 # invariant: func_outputs contains only Tensors, CompositeTensors,

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args, **kwds)
666 # the function a weak reference to itself to avoid a reference cycle.
667 with OptionalXlaContext(compile_with_xla):
--> 668 out = weak_wrapped_fn().wrapped(*args, **kwds)
669 return out
670

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\framework\func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise

ValueError: in user code:

C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py:853 train_function  *
    return step_function(self, iterator)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py:842 step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica
    return fn(*args, **kwargs)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py:835 run_step  **
    outputs = model.train_step(data)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py:787 train_step
    y_pred = self(x, training=True)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\base_layer.py:1020 __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
C:\Users\91984\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\input_spec.py:202 assert_input_compatibility
    ' input tensors. Inputs received: ' + str(inputs))

ValueError: Layer model expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, None, None) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, None) dtype=int32>]

2.Train with extra data and spacial transformer
Epoch 1/20

TypeError Traceback (most recent call last)
in
13 model.fit((train_tensors, train_data), train_labels,
14 validation_data=((valid_tensors, valid_data), valid_labels),
---> 15 epochs = epochs, batch_size = batch_size, callbacks = [checkpointer, log, earlystop],verbose=1)
16 # def train_generator(x, y, batch_size):
17 # train_datagen = ImageDataGenerator(

~\anaconda3\envs\hello-tf\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1182 _r=1):
1183 callbacks.on_train_batch_begin(step)
-> 1184 tmp_logs = self.train_function(iterator)
1185 if data_handler.should_sync:
1186 context.async_wait()

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\def_function.py in call(self, *args, **kwds)
883
884 with OptionalXlaContext(self._jit_compile):
--> 885 result = self._call(*args, **kwds)
886
887 new_tracing_count = self.experimental_get_tracing_count()

~\anaconda3\envs\hello-tf\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
915 # In this case we have created variables on the first call, so we run the
916 # defunned version which is guaranteed to never create variables.
--> 917 return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable
918 elif self._stateful_fn is not None:
919 # Release the lock early so that multiple threads can perform the call

TypeError: 'NoneType' object is not callable

vgg16 pretrained

can i get the pretrained file of vgg16 you used here sir please?or may be the code?

I am getting error while executing Capsule Network basic - SampleDataset.ipynb.

File "cnbs.py", line 102, in
model, eval_model, manipulate_model = CapsNet(input_shape=train_tensors.shape[1:],
File "cnbs.py", line 71, in CapsNet
digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=16, routings=routings,
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 897, in call
self._maybe_build(inputs)
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2416, in _maybe_build
self.build(input_shapes) # pylint:disable=not-callable
File "C:\Users\Sid\Documents\Lung-Diseases-Classifier-master 2\capsulelayers.py", line 105, in build
self.W = self.add_weight(shape=[self.num_capsule, self.input_num_capsule,
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 560, in add_weight
variable = self._add_variable_with_custom_getter(
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\training\tracking\base.py", line 738, in _add_variable_with_custom_getter
new_variable = getter(
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer_utils.py", line 129, in make_variable
return tf_variables.VariableV1(
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\variables.py", line 259, in call
return cls._variable_v1_call(*args, **kwargs)
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\variables.py", line 205, in _variable_v1_call
return previous_getter(
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\variables.py", line 198, in
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 2584, in default_variable_creator
return resource_variable_ops.ResourceVariable(
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\variables.py", line 263, in call
return super(VariableMetaclass, cls).call(*args, **kwargs)
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1423, in init
self._init_from_args(
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1567, in _init_from_args
initial_value() if init_from_fn else initial_value,
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer_utils.py", line 121, in
init_val = lambda: initializer(shape, dtype=dtype)
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\init_ops_v2.py", line 542, in call
fan_in, fan_out = _compute_fans(scale_shape)
File "C:\Users\Sid\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\init_ops_v2.py", line 1015, in _compute_fans
receptive_field_size *= dim
TypeError: unsupported operand type(s) for *=: 'float' and 'NoneType'

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.