Omkar1872 commited on
Commit
25c3d29
·
verified ·
1 Parent(s): 643a2a9

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py.py +72 -0
  2. brain_tumor_model.h5 +3 -0
  3. requirements.txt +4 -0
app.py.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.py
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/10HN0j7fMWCCHCZRup57O2eH-Acl7X0_5
8
+ """
9
+
10
+ from google.colab import files
11
+ uploaded = files.upload() # Select your zip file
12
+
13
+ import zipfile
14
+ import os
15
+
16
+ zip_path = "/content/archive (2).zip" # Update if named differently
17
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
18
+ zip_ref.extractall("brain_tumor_dataset")
19
+
20
+ os.listdir("brain_tumor_dataset")
21
+
22
+ import os
23
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
24
+ from tensorflow.keras.models import Sequential
25
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
26
+ from tensorflow.keras.optimizers import Adam
27
+
28
+ # Set paths
29
+ train_path = "brain_tumor_dataset" # Unzipped folder
30
+
31
+ # Preprocess data
32
+ datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
33
+
34
+ train_gen = datagen.flow_from_directory(
35
+ train_path, target_size=(150, 150), batch_size=32, class_mode='binary', subset='training')
36
+
37
+ val_gen = datagen.flow_from_directory(
38
+ train_path, target_size=(150, 150), batch_size=32, class_mode='binary', subset='validation')
39
+
40
+ # Build CNN
41
+ model = Sequential([
42
+ Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
43
+ MaxPooling2D(2, 2),
44
+ Conv2D(64, (3, 3), activation='relu'),
45
+ MaxPooling2D(2, 2),
46
+ Flatten(),
47
+ Dense(128, activation='relu'),
48
+ Dropout(0.3),
49
+ Dense(1, activation='sigmoid')
50
+ ])
51
+
52
+ model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
53
+ model.fit(train_gen, validation_data=val_gen, epochs=10)
54
+
55
+ # Save model
56
+ model.save("brain_tumor_model.h5")
57
+
58
+ import gradio as gr
59
+ import tensorflow as tf
60
+ import numpy as np
61
+ from PIL import Image
62
+
63
+ model = tf.keras.models.load_model("brain_tumor_model.h5")
64
+
65
+ def predict_tumor(img):
66
+ img = img.resize((150, 150))
67
+ img_array = np.expand_dims(np.array(img)/255.0, axis=0)
68
+ pred = model.predict(img_array)[0][0]
69
+ return "Tumor" if pred > 0.5 else "No Tumor"
70
+
71
+ gr.Interface(fn=predict_tumor, inputs=gr.Image(type="pil"), outputs="text", title="Brain Tumor Classifier").launch()
72
+
brain_tumor_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc017582097ebf2b0ac5834431e679691d6242822939908cb20f4d48d9f37fb8
3
+ size 127678168
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow
2
+ gradio
3
+ numpy
4
+ Pillow