|
| 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/cell-images-for-detecting-malaria/cell_images/Parasitized/") |
| 20 | + |
| 21 | +for a in Parasitized: |
| 22 | + try: |
| 23 | + imageP = cv2.imread("../input/cell-images-for-detecting-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/cell-images-for-detecting-malaria/cell_images/Uninfected/") |
| 32 | + |
| 33 | +for b in Uninfected: |
| 34 | + try: |
| 35 | + imageU = cv2.imread("../input/cell-images-for-detecting-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, labels2, test_size=0.2, random_state=0) |
| 56 | +X_trainF = X_train.astype('float32') |
| 57 | +X_validF = X_valid.astype('float32') |
| 58 | +# One Hot Encoding |
| 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), input_shape=(36, 36, 3), activation='relu')) |
| 65 | +classifier.add(MaxPooling2D(pool_size=(2, 2))) |
| 66 | +classifier.add(BatchNormalization(axis=-1)) |
| 67 | +classifier.add(Dropout(0.5)) # Dropout prevents overfitting |
| 68 | +classifier.add(Conv2D(32, kernel_size=(3, 3), input_shape=(36, 36, 3), activation='relu')) |
| 69 | +classifier.add(MaxPooling2D(pool_size=(2, 2))) |
| 70 | +classifier.add(BatchNormalization(axis=-1)) |
| 71 | +classifier.add(Dropout(0.5)) |
| 72 | +classifier.add(Flatten()) |
| 73 | +classifier.add(Dense(units=128, activation='relu')) |
| 74 | +classifier.add(BatchNormalization(axis=-1)) |
| 75 | +classifier.add(Dropout(0.5)) |
| 76 | +classifier.add(Dense(units=2, activation='softmax')) |
| 77 | +classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) |
| 78 | +history = classifier.fit(X_trainF, y_trainF, batch_size=120, epochs=15, verbose=1, validation_data=(X_validF, y_validF)) |
| 79 | +classifier.summary() |
| 80 | + |
| 81 | + |
| 82 | +y_pred = classifier.predict(X_validF) |
| 83 | +# Convert back to categorical values |
| 84 | +y_predF = np.argmax(y_pred, axis=1) |
| 85 | +y_valid_one = np.argmax(y_validF, axis=1) |
| 86 | +classifier.save("./Malaria/Models/malaria.h5") |
0 commit comments