diff --git a/Chatbot/botm.png b/Chatbot/botm.png new file mode 100644 index 0000000000000000000000000000000000000000..c011ce5031e2e8d7329020c0c506c644726f19b0 Binary files /dev/null and b/Chatbot/botm.png differ diff --git a/Chatbot/user.png b/Chatbot/user.png new file mode 100644 index 0000000000000000000000000000000000000000..d40b6ed4ccde1f6f0fb9163df8e997b775226254 Binary files /dev/null and b/Chatbot/user.png differ diff --git a/Images/Glioma/0.jpg b/Images/Glioma/0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e61743b4e9246e3f3bb5ecd9897905764fdd816c Binary files /dev/null and b/Images/Glioma/0.jpg differ diff --git a/Images/Glioma/1.jpg b/Images/Glioma/1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..04ba42e5803584c4aed063532d74a626a8aabb18 Binary files /dev/null and b/Images/Glioma/1.jpg differ diff --git a/Images/Glioma/10.jpg b/Images/Glioma/10.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d2f57508da8d90f4a803b77672d8e87b7578ec7b Binary files /dev/null and b/Images/Glioma/10.jpg differ diff --git a/Images/Glioma/11.jpg b/Images/Glioma/11.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1328ba0d889859b49c55a4f9b9a5f1c1c0f9e282 Binary files /dev/null and b/Images/Glioma/11.jpg differ diff --git a/Images/Glioma/12.jpg b/Images/Glioma/12.jpg new file mode 100644 index 0000000000000000000000000000000000000000..39b6b5548b0651fb5571970618963e9ecf0f8b5d Binary files /dev/null and b/Images/Glioma/12.jpg differ diff --git a/Images/Glioma/2.jpg b/Images/Glioma/2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2e4393bbebd6f3d5cfd95565fa5b013db7b835a4 Binary files /dev/null and b/Images/Glioma/2.jpg differ diff --git a/Images/Glioma/3.jpg b/Images/Glioma/3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..229170fcb3a57c3233516f7475a28776dd975c0f Binary files /dev/null and b/Images/Glioma/3.jpg differ diff --git a/Images/Glioma/4.jpg b/Images/Glioma/4.jpg new file mode 100644 index 0000000000000000000000000000000000000000..0776fccea7edb035533a5c853a3696686df542ae Binary files /dev/null and b/Images/Glioma/4.jpg differ diff --git a/Images/Glioma/5.jpg b/Images/Glioma/5.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b2dabcb7c3a07ceb56a87097ab5e0cbd3638ec04 Binary files /dev/null and b/Images/Glioma/5.jpg differ diff --git a/Images/Glioma/6.jpg b/Images/Glioma/6.jpg new file mode 100644 index 0000000000000000000000000000000000000000..21e0a19bceb6e20bee09255bec684b09f0545c2e Binary files /dev/null and b/Images/Glioma/6.jpg differ diff --git a/Images/Glioma/7.jpg b/Images/Glioma/7.jpg new file mode 100644 index 0000000000000000000000000000000000000000..13ad90870c559d1b835ad7d7951a9c0305664542 Binary files /dev/null and b/Images/Glioma/7.jpg differ diff --git a/Images/Glioma/8.jpg b/Images/Glioma/8.jpg new file mode 100644 index 0000000000000000000000000000000000000000..462c80149beaa886c826a7f19ae1780406b949a3 Binary files /dev/null and b/Images/Glioma/8.jpg differ diff --git a/Images/Glioma/9.jpg b/Images/Glioma/9.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ae2b1b015e0d44273d9ca8fd405baad73d85ccfc Binary files /dev/null and b/Images/Glioma/9.jpg differ diff --git a/Images/Meningioma/0.jpg b/Images/Meningioma/0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..0f48d07b9db9a930f2a78621f8b9900052951bc2 Binary files /dev/null and b/Images/Meningioma/0.jpg differ diff --git a/Images/Meningioma/1.jpg b/Images/Meningioma/1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a75d54592ebc7a327d2767e74bce2c95e8f8ee55 Binary files /dev/null and b/Images/Meningioma/1.jpg differ diff --git a/Images/Meningioma/10.jpg b/Images/Meningioma/10.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4dcb23534147c37164e746f5fcea4d700e804291 Binary files /dev/null and b/Images/Meningioma/10.jpg differ diff --git a/Images/Meningioma/11.jpg b/Images/Meningioma/11.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a6741a4ba2be3536ed5c17993103d0eb47a71de0 Binary files /dev/null and b/Images/Meningioma/11.jpg differ diff --git a/Images/Meningioma/12.jpg b/Images/Meningioma/12.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2dd737e33d263c6ae7950f34ee4db37b852e7900 Binary files /dev/null and b/Images/Meningioma/12.jpg differ diff --git a/Images/Meningioma/2.jpg b/Images/Meningioma/2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4fdf4c8901bec3d06dbc71dea975347eb6634551 Binary files /dev/null and b/Images/Meningioma/2.jpg differ diff --git a/Images/Meningioma/3.jpg b/Images/Meningioma/3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..6243c5e485a6c79a10cef6751c74b9fc7b9abb57 Binary files /dev/null and b/Images/Meningioma/3.jpg differ diff --git a/Images/Meningioma/4.jpg b/Images/Meningioma/4.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a9dcc70f59a2c08d4bc67743d81eb57b43c0a982 Binary files /dev/null and b/Images/Meningioma/4.jpg differ diff --git a/Images/Meningioma/5.jpg b/Images/Meningioma/5.jpg new file mode 100644 index 0000000000000000000000000000000000000000..87c5f05b62eb8da5e9e24daf2d69c306cd92f907 Binary files /dev/null and b/Images/Meningioma/5.jpg differ diff --git a/Images/Meningioma/6.jpg b/Images/Meningioma/6.jpg new file mode 100644 index 0000000000000000000000000000000000000000..51d99c2b1aa731d219e9dc3e20295fcad2d70364 Binary files /dev/null and b/Images/Meningioma/6.jpg differ diff --git a/Images/Meningioma/7.jpg b/Images/Meningioma/7.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5173db5a887a7c41098444a5ab3074d922402d1f Binary files /dev/null and b/Images/Meningioma/7.jpg differ diff --git a/Images/Meningioma/8.jpg b/Images/Meningioma/8.jpg new file mode 100644 index 0000000000000000000000000000000000000000..52f9ce5d3d829f99b8d6bb71c90f01b26e0ad73d Binary files /dev/null and b/Images/Meningioma/8.jpg differ diff --git a/Images/Meningioma/9.jpg b/Images/Meningioma/9.jpg new file mode 100644 index 0000000000000000000000000000000000000000..09cfbdb01a5d940a67d6ecafc9f3e123c931ae4d Binary files /dev/null and b/Images/Meningioma/9.jpg differ diff --git a/Images/Pituitary/0.jpg b/Images/Pituitary/0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5caed195b3ac3fc04f3cfdb366ed622330e4d626 Binary files /dev/null and b/Images/Pituitary/0.jpg differ diff --git a/Images/Pituitary/1.jpg b/Images/Pituitary/1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..cb1c4b0236c7c8507a17da83b8d800fbf6f88cdf Binary files /dev/null and b/Images/Pituitary/1.jpg differ diff --git a/Images/Pituitary/10.jpg b/Images/Pituitary/10.jpg new file mode 100644 index 0000000000000000000000000000000000000000..f8481035e996d7a80ad1cb5a7ffc8b55d363d4d4 Binary files /dev/null and b/Images/Pituitary/10.jpg differ diff --git a/Images/Pituitary/11.jpg b/Images/Pituitary/11.jpg new file mode 100644 index 0000000000000000000000000000000000000000..fdb6878885e82ba50b2b92f6ef0aa6a21101202b Binary files /dev/null and b/Images/Pituitary/11.jpg differ diff --git a/Images/Pituitary/12.jpg b/Images/Pituitary/12.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d96a29ed63b31d042211a985431a69ba804a12b7 Binary files /dev/null and b/Images/Pituitary/12.jpg differ diff --git a/Images/Pituitary/2.jpg b/Images/Pituitary/2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4a528042b17c031b58c124f146414519a5b32699 Binary files /dev/null and b/Images/Pituitary/2.jpg differ diff --git a/Images/Pituitary/3.jpg b/Images/Pituitary/3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..deda7b3505ba4cdb4921f4a3418520e6b316a465 Binary files /dev/null and b/Images/Pituitary/3.jpg differ diff --git a/Images/Pituitary/4.jpg b/Images/Pituitary/4.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2ca6b2678172a4dfecf8de09c37fa52f50ff36e7 Binary files /dev/null and b/Images/Pituitary/4.jpg differ diff --git a/Images/Pituitary/5.jpg b/Images/Pituitary/5.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a566a4efa4d24072a1bd508adba1f24afb6b9d76 Binary files /dev/null and b/Images/Pituitary/5.jpg differ diff --git a/Images/Pituitary/6.jpg b/Images/Pituitary/6.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b5886bb2e549f1b8886ac33291b80042442ba4bf Binary files /dev/null and b/Images/Pituitary/6.jpg differ diff --git a/Images/Pituitary/7.jpg b/Images/Pituitary/7.jpg new file mode 100644 index 0000000000000000000000000000000000000000..dbe84fddaca8a79987c45de637aee2897a52346c Binary files /dev/null and b/Images/Pituitary/7.jpg differ diff --git a/Images/Pituitary/8.jpg b/Images/Pituitary/8.jpg new file mode 100644 index 0000000000000000000000000000000000000000..60e7c2978212b85d358e5c80a58032fc132e7d35 Binary files /dev/null and b/Images/Pituitary/8.jpg differ diff --git a/Images/Pituitary/9.jpg b/Images/Pituitary/9.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9525d3d161cdc2e2e757e459c20510051b485849 Binary files /dev/null and b/Images/Pituitary/9.jpg differ diff --git a/Images/Segmentation/image_with_mask_0.png b/Images/Segmentation/image_with_mask_0.png new file mode 100644 index 0000000000000000000000000000000000000000..9314ab3c9e21e084852223ecb0d56efc94033eb1 Binary files /dev/null and b/Images/Segmentation/image_with_mask_0.png differ diff --git a/Images/Segmentation/image_with_mask_1.png b/Images/Segmentation/image_with_mask_1.png new file mode 100644 index 0000000000000000000000000000000000000000..5cbd06038f04a736ba915fae847276af41a2f747 Binary files /dev/null and b/Images/Segmentation/image_with_mask_1.png differ diff --git a/Images/Segmentation/image_with_mask_10.png b/Images/Segmentation/image_with_mask_10.png new file mode 100644 index 0000000000000000000000000000000000000000..d0e00b9584122967a9de91a23ad9d617864e27cd Binary files /dev/null and b/Images/Segmentation/image_with_mask_10.png differ diff --git a/Images/Segmentation/image_with_mask_11.png b/Images/Segmentation/image_with_mask_11.png new file mode 100644 index 0000000000000000000000000000000000000000..92f8bc8738c6851a9a71f34690de05a4eb77ce8d Binary files /dev/null and b/Images/Segmentation/image_with_mask_11.png differ diff --git a/Images/Segmentation/image_with_mask_12.png b/Images/Segmentation/image_with_mask_12.png new file mode 100644 index 0000000000000000000000000000000000000000..0116585471d84ae7f371ebfb50f80abf50362f8b Binary files /dev/null and b/Images/Segmentation/image_with_mask_12.png differ diff --git a/Images/Segmentation/image_with_mask_13.png b/Images/Segmentation/image_with_mask_13.png new file mode 100644 index 0000000000000000000000000000000000000000..736d79b081e297e08f5706acbff7126f3ac67d08 Binary files /dev/null and b/Images/Segmentation/image_with_mask_13.png differ diff --git a/Images/Segmentation/image_with_mask_14.png b/Images/Segmentation/image_with_mask_14.png new file mode 100644 index 0000000000000000000000000000000000000000..aaa56de3f43fe4609d9518c1c14efc2ae2262cb1 Binary files /dev/null and b/Images/Segmentation/image_with_mask_14.png differ diff --git a/Images/Segmentation/image_with_mask_2.png b/Images/Segmentation/image_with_mask_2.png new file mode 100644 index 0000000000000000000000000000000000000000..12d784e50a69774057abcaeacf84620016970263 Binary files /dev/null and b/Images/Segmentation/image_with_mask_2.png differ diff --git a/Images/Segmentation/image_with_mask_3.png b/Images/Segmentation/image_with_mask_3.png new file mode 100644 index 0000000000000000000000000000000000000000..f7c83d2bed01c05b3dfaba3b2bade6d332f9e83d Binary files /dev/null and b/Images/Segmentation/image_with_mask_3.png differ diff --git a/Images/Segmentation/image_with_mask_4.png b/Images/Segmentation/image_with_mask_4.png new file mode 100644 index 0000000000000000000000000000000000000000..b2c13fbcbb89fa7cce459cb3a2c6fc2882da91aa Binary files /dev/null and b/Images/Segmentation/image_with_mask_4.png differ diff --git a/Images/Segmentation/image_with_mask_5.png b/Images/Segmentation/image_with_mask_5.png new file mode 100644 index 0000000000000000000000000000000000000000..de089a192c1e50c08bb2f5f6c8d06e1b489a477f Binary files /dev/null and b/Images/Segmentation/image_with_mask_5.png differ diff --git a/Images/Segmentation/image_with_mask_6.png b/Images/Segmentation/image_with_mask_6.png new file mode 100644 index 0000000000000000000000000000000000000000..e72165bcabf216764d07ec5fadd5aafd359f9e60 Binary files /dev/null and b/Images/Segmentation/image_with_mask_6.png differ diff --git a/Images/Segmentation/image_with_mask_7.png b/Images/Segmentation/image_with_mask_7.png new file mode 100644 index 0000000000000000000000000000000000000000..8f57fd4d9c13b3622a5add0af89ae2e2c6f5addf Binary files /dev/null and b/Images/Segmentation/image_with_mask_7.png differ diff --git a/Images/Segmentation/image_with_mask_8.png b/Images/Segmentation/image_with_mask_8.png new file mode 100644 index 0000000000000000000000000000000000000000..7268529e9d59db0f19f12ff15dca4f2eb147cab0 Binary files /dev/null and b/Images/Segmentation/image_with_mask_8.png differ diff --git a/Images/Segmentation/image_with_mask_9.png b/Images/Segmentation/image_with_mask_9.png new file mode 100644 index 0000000000000000000000000000000000000000..6401eab7b30d25bb06575d3d797f10b1d232851d Binary files /dev/null and b/Images/Segmentation/image_with_mask_9.png differ diff --git a/Images/Segmentation/original_image_0.png b/Images/Segmentation/original_image_0.png new file mode 100644 index 0000000000000000000000000000000000000000..c0c6f57d3a08b7bc8015a9ce7e2218967d6d726a Binary files /dev/null and b/Images/Segmentation/original_image_0.png differ diff --git a/Images/Segmentation/original_image_1.png b/Images/Segmentation/original_image_1.png new file mode 100644 index 0000000000000000000000000000000000000000..4bdd4078df5af85c3c5c2e690170b0e7bfd16220 Binary files /dev/null and b/Images/Segmentation/original_image_1.png differ diff --git a/Images/Segmentation/original_image_10.png b/Images/Segmentation/original_image_10.png new file mode 100644 index 0000000000000000000000000000000000000000..70218232067832bb19d688d131d221c05324c3a2 Binary files /dev/null and b/Images/Segmentation/original_image_10.png differ diff --git a/Images/Segmentation/original_image_11.png b/Images/Segmentation/original_image_11.png new file mode 100644 index 0000000000000000000000000000000000000000..dbc275fa79756be1f698051f66ee27700f200c35 Binary files /dev/null and b/Images/Segmentation/original_image_11.png differ diff --git a/Images/Segmentation/original_image_12.png b/Images/Segmentation/original_image_12.png new file mode 100644 index 0000000000000000000000000000000000000000..dde5a047f420c08ce0d215c5568295881f807ed5 Binary files /dev/null and b/Images/Segmentation/original_image_12.png differ diff --git a/Images/Segmentation/original_image_13.png b/Images/Segmentation/original_image_13.png new file mode 100644 index 0000000000000000000000000000000000000000..a5dc81aba5bd0e2117afef6a2586e9a99d5b83aa Binary files /dev/null and b/Images/Segmentation/original_image_13.png differ diff --git a/Images/Segmentation/original_image_14.png b/Images/Segmentation/original_image_14.png new file mode 100644 index 0000000000000000000000000000000000000000..49a88c3c58c4991a2a3d85041886235561994aa5 Binary files /dev/null and b/Images/Segmentation/original_image_14.png differ diff --git a/Images/Segmentation/original_image_2.png b/Images/Segmentation/original_image_2.png new file mode 100644 index 0000000000000000000000000000000000000000..7bc81c6ef3b4b126745df3a8541b47185de3fee5 Binary files /dev/null and b/Images/Segmentation/original_image_2.png differ diff --git a/Images/Segmentation/original_image_3.png b/Images/Segmentation/original_image_3.png new file mode 100644 index 0000000000000000000000000000000000000000..0ac1421dd4f24253b7085f4939eb67295674509e Binary files /dev/null and b/Images/Segmentation/original_image_3.png differ diff --git a/Images/Segmentation/original_image_4.png b/Images/Segmentation/original_image_4.png new file mode 100644 index 0000000000000000000000000000000000000000..3787aa308264521f6ed91cd8357a182434ce0b30 Binary files /dev/null and b/Images/Segmentation/original_image_4.png differ diff --git a/Images/Segmentation/original_image_5.png b/Images/Segmentation/original_image_5.png new file mode 100644 index 0000000000000000000000000000000000000000..ae1baaaeba4ece6ed8b20c396d7e7f806cecfe70 Binary files /dev/null and b/Images/Segmentation/original_image_5.png differ diff --git a/Images/Segmentation/original_image_6.png b/Images/Segmentation/original_image_6.png new file mode 100644 index 0000000000000000000000000000000000000000..51d02cc1513653a393a74e2bc8424c7f07bfd747 Binary files /dev/null and b/Images/Segmentation/original_image_6.png differ diff --git a/Images/Segmentation/original_image_7.png b/Images/Segmentation/original_image_7.png new file mode 100644 index 0000000000000000000000000000000000000000..78a0f8f1c8512e3e115504a29b24550096665777 Binary files /dev/null and b/Images/Segmentation/original_image_7.png differ diff --git a/Images/Segmentation/original_image_8.png b/Images/Segmentation/original_image_8.png new file mode 100644 index 0000000000000000000000000000000000000000..1f259e7c7a62bb8d2f79b31644b0a9911fefe652 Binary files /dev/null and b/Images/Segmentation/original_image_8.png differ diff --git a/Images/Segmentation/original_image_9.png b/Images/Segmentation/original_image_9.png new file mode 100644 index 0000000000000000000000000000000000000000..ac24c1145660e43607e9f9e0a8af497e16166a88 Binary files /dev/null and b/Images/Segmentation/original_image_9.png differ diff --git a/ResUNet.py b/ResUNet.py new file mode 100644 index 0000000000000000000000000000000000000000..471cff96af6a5309ca54bd5e79ea14237a12002a --- /dev/null +++ b/ResUNet.py @@ -0,0 +1,100 @@ +import os +import numpy as np +import pandas as pd +import cv2 + +import tensorflow as tf +from tensorflow.python.keras import Sequential +from tensorflow.keras import layers, optimizers +from tensorflow.keras.layers import * +from tensorflow.keras.models import Model +from tensorflow.keras.initializers import glorot_uniform +from tensorflow.keras.utils import plot_model +from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, LearningRateScheduler +import tensorflow.keras.backend as K + + +# lets create model now +def resblock(X, f): + ''' + function for creating res block + ''' + X_copy = X #copy of input + + # main path + X = Conv2D(f, kernel_size=(1,1), kernel_initializer='he_normal')(X) + X = BatchNormalization()(X) + X = Activation('relu')(X) + + X = Conv2D(f, kernel_size=(3,3), padding='same', kernel_initializer='he_normal')(X) + X = BatchNormalization()(X) + + # shortcut path + X_copy = Conv2D(f, kernel_size=(1,1), kernel_initializer='he_normal')(X_copy) + X_copy = BatchNormalization()(X_copy) + + # Adding the output from main path and short path together + X = Add()([X, X_copy]) + X = Activation('relu')(X) + + return X + +def upsample_concat(x, skip): + ''' + funtion for upsampling image + ''' + X = UpSampling2D((2,2))(x) + merge = Concatenate()([X, skip]) + + return merge + + +def load_model(): + + input_shape = (256,256,3) + X_input = Input(input_shape) #iniating tensor of input shape + + # Stage 1 + conv_1 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(X_input) + conv_1 = BatchNormalization()(conv_1) + conv_1 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv_1) + conv_1 = BatchNormalization()(conv_1) + pool_1 = MaxPool2D((2,2))(conv_1) + + # stage 2 + conv_2 = resblock(pool_1, 32) + pool_2 = MaxPool2D((2,2))(conv_2) + + # Stage 3 + conv_3 = resblock(pool_2, 64) + pool_3 = MaxPool2D((2,2))(conv_3) + + # Stage 4 + conv_4 = resblock(pool_3, 128) + pool_4 = MaxPool2D((2,2))(conv_4) + + # Stage 5 (bottle neck) + conv_5 = resblock(pool_4, 256) + + # Upsample Stage 1 + up_1 = upsample_concat(conv_5, conv_4) + up_1 = resblock(up_1, 128) + + # Upsample Stage 2 + up_2 = upsample_concat(up_1, conv_3) + up_2 = resblock(up_2, 64) + + # Upsample Stage 3 + up_3 = upsample_concat(up_2, conv_2) + up_3 = resblock(up_3, 32) + + # Upsample Stage 4 + up_4 = upsample_concat(up_3, conv_1) + up_4 = resblock(up_4, 16) + + # final output + out = Conv2D(1, (1,1), kernel_initializer='he_normal', padding='same', activation='sigmoid')(up_4) + + seg_model = Model(X_input, out) + + return seg_model \ No newline at end of file diff --git a/eff.py b/eff.py new file mode 100644 index 0000000000000000000000000000000000000000..14fcd0aac1a017255bb842ee813b9d2336228e37 --- /dev/null +++ b/eff.py @@ -0,0 +1,145 @@ +import os +import cv2 +import torch +import torchvision +from torch import nn +from torchvision import transforms +from PIL import Image + +class_names = ["Glioma Tumor", "Meningitis Tumor", "No Tumor", "Pituitary Tumor"] + +class CFG: + DEVICE = 'cpu' + NUM_DEVICES = torch.cuda.device_count() + NUM_WORKERS = os.cpu_count() + NUM_CLASSES = 4 + EPOCHS = 16 + BATCH_SIZE = 32 + LR = 0.001 + APPLY_SHUFFLE = True + SEED = 768 + HEIGHT = 224 + WIDTH = 224 + CHANNELS = 3 + IMAGE_SIZE = (224, 224, 3) + + +class EfficientNetV2Model(nn.Module): + def __init__(self, backbone_model, name='efficientnet-v2-large', + num_classes=CFG.NUM_CLASSES, device=CFG.DEVICE): + super(EfficientNetV2Model, self).__init__() + + self.backbone_model = backbone_model + self.device = device + self.num_classes = num_classes + self.name = name + + classifier = nn.Sequential( + nn.Flatten(), + nn.Dropout(p=0.2, inplace=True), + nn.Linear(in_features=1280, out_features=256, bias=True), + nn.GELU(), + nn.Dropout(p=0.2, inplace=True), + nn.Linear(in_features=256, out_features=num_classes, bias=False) + ).to(device) + + self._set_classifier(classifier) + + def _set_classifier(self, classifier:nn.Module) -> None: + self.backbone_model.classifier = classifier + + def forward(self, image): + return self.backbone_model(image) + + +def get_effiecientnetv2_model( + device: torch.device=CFG.NUM_CLASSES) -> nn.Module: + # Set the manual seeds + torch.manual_seed(CFG.SEED) + torch.cuda.manual_seed(CFG.SEED) + + # Get model weights + model_weights = ( + torchvision + .models + .EfficientNet_V2_L_Weights + .DEFAULT + ) + + # Get model and push to device + model = ( + torchvision.models.efficientnet_v2_l( + weights=model_weights + ) + ).to(device) + + # Freeze Model Parameters + for param in model.features.parameters(): + param.requires_grad = False + + return model + + +# Get EfficientNet v2 model +backbone_model = get_effiecientnetv2_model(CFG.DEVICE) + +efficientnetv2_params = { + 'backbone_model' : backbone_model, + 'name' : 'efficientnet-v2-large', + 'device' : CFG.DEVICE +} + +# Generate Model +efficientnet_model = EfficientNetV2Model(**efficientnetv2_params) + +efficientnet_model.load_state_dict( + torch.load('efficientnetV2.pth', map_location=torch.device('cpu')) +) + + +# def predict_eff(image_path): +# # Define the image transformation +# transform = transforms.Compose([ +# transforms.Resize((224, 224)), +# transforms.ToTensor() +# ]) + +# # Load and preprocess the image +# # image_path = "glioma.jpg" # Replace with the path to your image +# image = Image.open(image_path) +# input_tensor = transform(image) +# input_batch = input_tensor.unsqueeze(0).to("cuda") # Add batch dimension + +# # Perform inference +# with torch.no_grad(): +# output = efficientnet_model(input_batch).to("cuda") + +# # You can now use the 'output' tensor as needed (e.g., get predictions) +# # print(output) +# return output + + +def predict_eff(image_path): + # Define the image transformation + transform = transforms.Compose([ + transforms.Resize((224, 224)), + transforms.ToTensor() + ]) + + # Load and preprocess the image + # image_path = "glioma.jpg" # Replace with the path to your image + image = Image.open(image_path) + input_tensor = transform(image) + input_batch = input_tensor.unsqueeze(0).to(CFG.DEVICE) # Add batch dimension + + # Perform inference + with torch.no_grad(): + output = efficientnet_model(input_batch).to(CFG.DEVICE) + + # You can now use the 'output' tensor as needed (e.g., get predictions) + # print(output) + res = torch.softmax(output, dim=1) + + probs = {class_names[i]: float(res[0][i]) for i in range(len(class_names))} + + return probs \ No newline at end of file diff --git a/examples/Eff_ViT/Classification_0.jpg b/examples/Eff_ViT/Classification_0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8541b4a41cd938daa27110d6eed87a531bb34495 Binary files /dev/null and b/examples/Eff_ViT/Classification_0.jpg differ diff --git a/examples/Eff_ViT/Classification_1.jpg b/examples/Eff_ViT/Classification_1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..49af5b8ec23d986255f760b7aed7a7afdd5d54b0 Binary files /dev/null and b/examples/Eff_ViT/Classification_1.jpg differ diff --git a/examples/Eff_ViT/Classification_2.jpg b/examples/Eff_ViT/Classification_2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4fdf4c8901bec3d06dbc71dea975347eb6634551 Binary files /dev/null and b/examples/Eff_ViT/Classification_2.jpg differ diff --git a/examples/Eff_ViT/Classification_3.jpg b/examples/Eff_ViT/Classification_3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a9dcc70f59a2c08d4bc67743d81eb57b43c0a982 Binary files /dev/null and b/examples/Eff_ViT/Classification_3.jpg differ diff --git a/examples/ResUNet/0.jpg b/examples/ResUNet/0.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ada5266040a6f40bb5b7ccf2d1d46cb21ac177c1 Binary files /dev/null and b/examples/ResUNet/0.jpg differ diff --git a/examples/ResUNet/1.jpg b/examples/ResUNet/1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3dda59dff8e71a069b94d872ec2ffef45d468e7d Binary files /dev/null and b/examples/ResUNet/1.jpg differ diff --git a/examples/ResUNet/2.jpg b/examples/ResUNet/2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..593301bb500782863d0673ab125cf9d1653a87bd Binary files /dev/null and b/examples/ResUNet/2.jpg differ diff --git a/examples/ResUNet/3.jpg b/examples/ResUNet/3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8e47215e6a6d744cb801b46c313610f9bd086b81 Binary files /dev/null and b/examples/ResUNet/3.jpg differ diff --git a/examples/ResUNet/4.jpg b/examples/ResUNet/4.jpg new file mode 100644 index 0000000000000000000000000000000000000000..abba71a4d2809df20f6cfb55b1d6c71a1155e5c9 Binary files /dev/null and b/examples/ResUNet/4.jpg differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..5a866251cd000fbdd5a513d781eb2c5bbb41656e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +numpy +pandas +opencv-python +tensorflow +keras +torch +torchvision \ No newline at end of file diff --git a/vit.py b/vit.py new file mode 100644 index 0000000000000000000000000000000000000000..e6960d56598c3e2976e541e2ae41ea8b348291ca --- /dev/null +++ b/vit.py @@ -0,0 +1,109 @@ +import os +import torch +import torchvision +from torch import nn +from torchvision import transforms +from PIL import Image + +class CFG: + DEVICE = 'cpu' + NUM_DEVICES = torch.cuda.device_count() + NUM_WORKERS = os.cpu_count() + NUM_CLASSES = 4 + EPOCHS = 16 + BATCH_SIZE = 32 + LR = 0.001 + APPLY_SHUFFLE = True + SEED = 768 + HEIGHT = 224 + WIDTH = 224 + CHANNELS = 3 + IMAGE_SIZE = (224, 224, 3) + + +class VisionTransformerModel(nn.Module): + def __init__(self, backbone_model, name='vision-transformer', + num_classes=CFG.NUM_CLASSES, device=CFG.DEVICE): + super(VisionTransformerModel, self).__init__() + + self.backbone_model = backbone_model + self.device = device + self.num_classes = num_classes + self.name = name + + self.classifier = nn.Sequential( + nn.Flatten(), + nn.Dropout(p=0.2, inplace=True), + nn.Linear(in_features=1000, out_features=256, bias=True), + nn.GELU(), + nn.Dropout(p=0.2, inplace=True), + nn.Linear(in_features=256, out_features=num_classes, bias=False) + ).to(device) + + def forward(self, image): + vit_output = self.backbone_model(image) + return self.classifier(vit_output) + + +def get_vit_b32_model( + device: torch.device=CFG.NUM_CLASSES) -> nn.Module: + # Set the manual seeds + torch.manual_seed(CFG.SEED) + torch.cuda.manual_seed(CFG.SEED) + + # Get model weights + model_weights = ( + torchvision + .models + .ViT_L_32_Weights + .DEFAULT + ) + + # Get model and push to device + model = ( + torchvision.models.vit_l_32( + weights=model_weights + ) + ).to(device) + + # Freeze Model Parameters + for param in model.parameters(): + param.requires_grad = False + + return model + +# Get ViT model +vit_backbone = get_vit_b32_model(CFG.DEVICE) + +vit_params = { + 'backbone_model' : vit_backbone, + 'name' : 'ViT-L-B32', + 'device' : CFG.DEVICE +} + +# Generate Model +vit_model = VisionTransformerModel(**vit_params) + +vit_model.load_state_dict( + torch.load('vit_model.pth', map_location=torch.device('cpu')) +) + + +# Define the image transformation +transform = transforms.Compose([ + transforms.Resize((224, 224)), + transforms.ToTensor() +]) + + +def predict(image_path): + image = Image.open(image_path) + input_tensor = transform(image) + input_batch = input_tensor.unsqueeze(0).to(CFG.DEVICE) # Add batch dimension + + # Perform inference + with torch.no_grad(): + output = vit_model(input_batch).to(CFG.DEVICE) + + # You can now use the 'output' tensor as needed (e.g., get predictions) + return output \ No newline at end of file