itstalmeez commited on
Commit
3e5d1b0
·
verified ·
1 Parent(s): b0a9e9d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +69 -0
README.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
3
+ from tensorflow.keras.models import Sequential
4
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
5
+ from google.colab import drive
6
+ drive.mount('/content/drive')
7
+ # Define constants
8
+ image_size = (150, 150)
9
+ batch_size = 32
10
+
11
+ # Data augmentation for the training set
12
+ train_datagen = ImageDataGenerator(
13
+ rescale=1./255,
14
+ shear_range=0.2,
15
+ zoom_range=0.2,
16
+ horizontal_flip=True
17
+ )
18
+ # Rescaling for the testing set
19
+ test_datagen = ImageDataGenerator(rescale=1./255)
20
+
21
+ # Load the training set
22
+ train_set = train_datagen.flow_from_directory(
23
+ '/content/drive/MyDrive/chest_xray/train',
24
+ target_size=image_size,
25
+ batch_size=batch_size,
26
+ class_mode='binary'
27
+ )
28
+
29
+ # Load the testing set
30
+ test_set = test_datagen.flow_from_directory(
31
+ '/content/drive/MyDrive/chest_xray/test',
32
+ target_size=image_size,
33
+ batch_size=batch_size,
34
+ class_mode='binary'
35
+ )
36
+ # Build the CNN model
37
+ model = Sequential()
38
+ model.add(Conv2D(32, (3, 3), input_shape=(image_size[0], image_size[1], 3), activation='relu'))
39
+ model.add(MaxPooling2D(pool_size=(2, 2)))
40
+ model.add(Conv2D(64, (3, 3), activation='relu'))
41
+ model.add(MaxPooling2D(pool_size=(2, 2)))
42
+ model.add(Flatten())
43
+ model.add(Dense(units=128, activation='relu'))
44
+ model.add(Dense(units=1, activation='sigmoid'))
45
+ # Compile the model
46
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
47
+ # Train the model
48
+ model.fit(train_set, epochs=10, validation_data=test_set)
49
+
50
+ # Save the model
51
+ model.save('pneumonia_model.h5')
52
+ # Evaluate the model on the testing set
53
+ accuracy = model.evaluate(test_set)[1]
54
+ print(f'Test Accuracy: {accuracy}')
55
+ # Make predictions on new images
56
+ def predict_image(file_path):
57
+ img = tf.keras.preprocessing.image.load_img(file_path, target_size=image_size)
58
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
59
+ img_array = tf.expand_dims(img_array, 0) # Create a batch
60
+
61
+ predictions = model.predict(img_array)
62
+ if predictions[0] > 0.5:
63
+ print("Prediction: Pneumonia")
64
+ else:
65
+ print("Prediction: Normal")
66
+
67
+ # Example usage:
68
+ image_path = "/content/drive/MyDrive/chest_xray/train/PNEUMONIA/BACTERIA-1033441-0001.jpeg"
69
+ predict_image(image_path)