Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
"""tumor-classification-using-keras.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/1EgMc5_zGbuWuvrsGd2sg2bVfXYUQXanx | |
# Import Statements | |
""" | |
from zipfile import ZipFile | |
file_name = "brain_tumor_dataset_kaggle.zip" | |
with ZipFile(file_name,'r') as zip: | |
zip.extractall() | |
print("done") | |
import streamlit as st | |
import os | |
import keras | |
from keras.models import Sequential | |
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization | |
from PIL import Image | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import OneHotEncoder | |
encoder = OneHotEncoder() | |
encoder.fit([[0], [1]]) | |
# 0 - Tumor | |
# 1 - Normal | |
""" | |
1. data list for storing image data in numpy array form | |
2. paths list for storing paths of all images | |
3. result list for storing one hot encoded form of target class whether normal or tumor""" | |
# This cell updates result list for images with tumor | |
data = [] | |
paths = [] | |
result = [] | |
for r, d, f in os.walk(r'../content/brain_tumor_dataset/yes'): #r-read, d-directory, f-file | |
for file in f: | |
if '.jpg' in file: | |
paths.append(os.path.join(r, file)) | |
for path in paths: | |
img = Image.open(path) | |
img = img.resize((128,128)) | |
img = np.array(img) | |
if(img.shape == (128,128,3)): | |
data.append(np.array(img)) | |
result.append(encoder.transform([[0]]).toarray()) | |
# This cell updates result list for images without tumor | |
paths = [] | |
for r, d, f in os.walk(r"../content/brain_tumor_dataset/no"): | |
for file in f: | |
if '.jpg' in file: | |
paths.append(os.path.join(r, file)) | |
for path in paths: | |
img = Image.open(path) | |
img = img.resize((128,128)) | |
img = np.array(img) | |
if(img.shape == (128,128,3)): | |
data.append(np.array(img)) | |
result.append(encoder.transform([[1]]).toarray()) | |
data = np.array(data) | |
data.shape | |
result = np.array(result) | |
result = result.reshape(-1,128,128,3) | |
from sklearn.model_selection import train_test_split | |
x_train,x_test,y_train,y_test = train_test_split(data, result, test_size=0.1, shuffle=True, random_state=0) | |
"""# Model Building | |
Batch normalization is a technique for training very deep neural networks that standardizes the inputs to a layer for each mini-batch. This has the effect of stabilizing the learning process and dramatically reducing the number of training epochs required to train deep networks. | |
""" | |
model = Sequential() | |
model.add(Conv2D(32, kernel_size=(2, 2), input_shape=(128, 128, 3), padding = 'Same')) | |
model.add(Conv2D(32, kernel_size=(2, 2), activation ='relu', padding = 'Same')) | |
model.add(BatchNormalization()) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Conv2D(64, kernel_size = (2,2), activation ='relu', padding = 'Same')) | |
model.add(Conv2D(64, kernel_size = (2,2), activation ='relu', padding = 'Same')) | |
model.add(Conv2D(64, kernel_size = (2,2), activation ='relu', padding = 'Same')) | |
model.add(Conv2D(64, kernel_size = (2,2), activation ='relu', padding = 'Same')) | |
model.add(BatchNormalization()) | |
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(2, activation='softmax')) | |
model.compile(loss = "categorical_crossentropy", optimizer='Adamax') | |
print(model.summary()) | |
y_train.shape | |
history = model.fit(x_train, y_train, epochs = 30, batch_size = 40, verbose = 1,validation_data = (x_test, y_test)) | |
"""# Plotting Losses""" | |
plt.plot(history.history['loss']) | |
plt.plot(history.history['val_loss']) | |
plt.title('Model Loss') | |
plt.ylabel('Loss') | |
plt.xlabel('Epoch') | |
plt.legend(['Test', 'Validation'], loc='upper right') | |
plt.show() | |
"""# Just Checking the Model""" | |
def names(number): | |
if number==0: | |
return 'Its a Tumor' | |
else: | |
return 'No, Its not a tumor' | |
from matplotlib.pyplot import imshow | |
img = Image.open(r"../content/brain_tumor_dataset/no/11 no.jpg") | |
x = np.array(img.resize((128,128))) | |
x = x.reshape(1,128,128,3) | |
res = model.predict_on_batch(x) | |
classification = np.where(res == np.amax(res))[1][0] | |
imshow(img) | |
print(str(res[0][classification]*100) + '% Confidence This Is A ' + names(classification)) | |
from matplotlib.pyplot import imshow | |
img = Image.open(r"../content/brain_tumor_dataset/no/18 no.jpg") | |
x = np.array(img.resize((128,128))) | |
x = x.reshape(1,128,128,3) | |
res = model.predict_on_batch(x) | |
classification = np.where(res == np.amax(res))[1][0] | |
imshow(img) | |
print(str(res[0][classification]*100) + '% Confidence This Is A ' + names(classification)) | |
"""# Thats It !!""" |