Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1f219d

Browse files
Merge pull request avinashkranjan#927 from ayush-raj8/master
Malaria Detection
2 parents 1b51db4 + 569c0e7 commit b1f219d

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

‎Malaria/Images/cell1.jpg‎

13.7 KB
Loading[フレーム]

‎Malaria/Models/malaria.h5‎

17.6 MB
Binary file not shown.

‎Malaria/main.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# import important libraries
2+
import os
3+
from PIL import Image
4+
import tensorflow as tf
5+
import numpy as np
6+
7+
# Configuring user's pc so that tensorflow model built on other's pc can run
8+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
9+
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
10+
11+
# reading image
12+
val_path = input("Enter image path name eg.Images/cell1.jpg: ")
13+
img = Image.open(val_path)
14+
# confiiguring input image according to the model's requirement
15+
img = img.resize((36, 36))
16+
img = np.asarray(img)
17+
img = img.reshape((1, 36, 36, 3))
18+
img = img.astype(np.float64)
19+
model_path = "./Malaria/Models/malaria.h5"
20+
model = tf.keras.models.load_model(model_path)
21+
pred = np.argmax(model.predict(img)[0])
22+
if pred == 1:
23+
print("Infected Cell")
24+
else:
25+
print("Healthy Cell")

‎Malaria/model_training.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# importing the libraries for loading data and visualisation
2+
import os
3+
import cv2
4+
import numpy as np
5+
from PIL import Image
6+
# import for train-test-split
7+
from sklearn.model_selection import train_test_split
8+
# import for One Hot Encoding
9+
from keras.utils import to_categorical
10+
# importing libraries for Model
11+
from tensorflow.keras.models import Sequential
12+
from tensorflow.keras.layers import Conv2D, MaxPooling2D
13+
from tensorflow.keras.layers import Dense, Flatten, Dropout, BatchNormalization
14+
15+
# loading the data of images and setting their labels
16+
data = []
17+
labels = []
18+
19+
Parasitized = os.listdir("../input/malaria/cell_images/Parasitized/")
20+
21+
for a in Parasitized:
22+
try:
23+
imageP = cv2.imread("../input/malaria/cell_images/Parasitized/" + a)
24+
image_from_arrayP = Image.fromarray(imageP, 'RGB')
25+
size_imageP = image_from_arrayP.resize((36, 36))
26+
data.append(np.array(size_imageP))
27+
labels.append(0)
28+
except AttributeError:
29+
print("")
30+
31+
Uninfected = os.listdir("../input/malaria/cell_images/Uninfected/")
32+
33+
for b in Uninfected:
34+
try:
35+
imageU = cv2.imread("../input/malaria/cell_images/Uninfected/" + b)
36+
image_from_arrayU = Image.fromarray(imageU, 'RGB')
37+
size_imageU = image_from_arrayU.resize((36, 36))
38+
data.append(np.array(size_imageU))
39+
labels.append(1)
40+
except AttributeError:
41+
print("")
42+
43+
# Creating single numpy array of all the images and labels
44+
data1 = np.array(data)
45+
labels1 = np.array(labels)
46+
print('Cells : {} and labels : {}'.format(data1.shape, labels1.shape))
47+
48+
# lets shuffle the data and labels before splitting them into training and testing sets
49+
n = np.arange(data1.shape[0])
50+
np.random.shuffle(n)
51+
data2 = data1[n]
52+
labels2 = labels1[n]
53+
54+
# Splitting the dataset into the Training set and Test set
55+
X_train, X_valid, y_train,y_valid = train_test_split(data2,
56+
labels2, test_size=0.2, random_state=0)
57+
X_trainF = X_train.astype('float32')
58+
X_validF = X_valid.astype('float32')
59+
y_trainF = to_categorical(y_train)
60+
y_validF = to_categorical(y_valid)
61+
62+
classifier = Sequential()
63+
# CNN layers
64+
classifier.add(Conv2D(32, kernel_size=(3, 3),
65+
input_shape=(36, 36, 3), activation='relu'))
66+
classifier.add(MaxPooling2D(pool_size=(2, 2)))
67+
classifier.add(BatchNormalization(axis=-1))
68+
classifier.add(Dropout(0.5)) # Dropout prevents overfitting
69+
classifier.add(Conv2D(32, kernel_size=(3, 3),
70+
input_shape=(36, 36, 3), activation='relu'))
71+
classifier.add(MaxPooling2D(pool_size=(2, 2)))
72+
classifier.add(BatchNormalization(axis=-1))
73+
classifier.add(Dropout(0.5))
74+
classifier.add(Flatten())
75+
classifier.add(Dense(units=128, activation='relu'))
76+
classifier.add(BatchNormalization(axis=-1))
77+
classifier.add(Dropout(0.5))
78+
classifier.add(Dense(units=2, activation='softmax'))
79+
classifier.compile(optimizer='adam',
80+
loss='categorical_crossentropy', metrics=['accuracy'])
81+
history = classifier.fit(X_trainF, y_trainF,
82+
batch_size=120, epochs=15,
83+
verbose=1, validation_data=(X_validF, y_validF))
84+
classifier.summary()
85+
86+
y_pred = classifier.predict(X_validF)
87+
y_predF = np.argmax(y_pred, axis=1)
88+
y_valid_one = np.argmax(y_validF, axis=1)
89+
classifier.save("./Malaria/Models/malaria.h5")

‎Malaria/requirements.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
numpy==1.19.2
2+
pandas==1.1.2
3+
h5py==2.10.0
4+
tensorflow==2.4.1
5+
pillow==7.2.0
6+
Keras==2.2.5
7+
PIL=8.2.0
8+
opencv-python==4.5.1.48
9+
scikit-learn>=0.18

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /