0% found this document useful (0 votes)
3 views3 pages

braintumorcodewithnpz

The document contains a Python script for processing lung X-ray images, training various machine learning models, and evaluating their performance. It includes functions for converting images to a compressed format, preparing data for machine learning, training models like SVM, Random Forest, XGBoost, and an artificial neural network, and visualizing the results. Additionally, it provides functionality to classify a single image using the trained model.

Uploaded by

team work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

braintumorcodewithnpz

The document contains a Python script for processing lung X-ray images, training various machine learning models, and evaluating their performance. It includes functions for converting images to a compressed format, preparing data for machine learning, training models like SVM, Random Forest, XGBoost, and an artificial neural network, and visualizing the results. Additionally, it provides functionality to classify a single image using the trained model.

Uploaded by

team work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import os

import numpy as np
from PIL import Image
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
import matplotlib.pyplot as plt

def images_to_npz(data_dir, img_size=(64,64), save_path='lung_data.npz'):


images = []
labels = []
classes = sorted([d for d in os.listdir(data_dir) if
os.path.isdir(os.path.join(data_dir, d))])
print(f"Found classes: {classes}")

for cls in classes:


cls_dir = os.path.join(data_dir, cls)
for f in os.listdir(cls_dir):
fpath = os.path.join(cls_dir, f)
try:
with Image.open(fpath) as img:
img = img.convert('RGB')
img = img.resize(img_size)
img_array = np.array(img) / 255.0
images.append(img_array)
labels.append(cls)
except Exception as e:
print(f"Skipping {fpath}: {e}")

images = np.array(images)
labels = np.array(labels)
le = LabelEncoder()
labels_encoded = le.fit_transform(labels)
np.savez_compressed(save_path, images=images, labels=labels_encoded,
classes=le.classes_)
print(f"Saved {len(images)} images and labels to {save_path}")

def load_data(npz_path):
data = np.load(npz_path)
return data['images'], data['labels'], data['classes']

def prepare_ml_data(X):
return X.reshape(X.shape[0], -1)

def create_ann(input_shape, num_classes):


model = Sequential([
Flatten(input_shape=input_shape),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dense(num_classes, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model

def train_and_evaluate(X_train, X_test, y_train, y_test, classes):


results = {}

svm = SVC(kernel='rbf')
svm.fit(X_train, y_train)
svm_preds = svm.predict(X_test)
results['SVM'] = accuracy_score(y_test, svm_preds)
print(f"SVM accuracy: {results['SVM']:.4f}")

rf = RandomForestClassifier(n_estimators=100)
rf.fit(X_train, y_train)
rf_preds = rf.predict(X_test)
results['Random Forest'] = accuracy_score(y_test, rf_preds)
print(f"Random Forest accuracy: {results['Random Forest']:.4f}")

xgb = XGBClassifier(use_label_encoder=False, eval_metric='logloss')


xgb.fit(X_train, y_train)
xgb_preds = xgb.predict(X_test)
results['XGBoost'] = accuracy_score(y_test, xgb_preds)
print(f"XGBoost accuracy: {results['XGBoost']:.4f}")

ann = create_ann(input_shape=X_train.shape[1:], num_classes=len(classes))


ann.fit(X_train.reshape(-1, 64, 64, 3), y_train, epochs=10, batch_size=32,
verbose=2)
loss, acc = ann.evaluate(X_test.reshape(-1, 64, 64, 3), y_test, verbose=0)
results['ANN'] = acc
print(f"ANN accuracy: {acc:.4f}")

return results, ann

def plot_results(results):
plt.figure(figsize=(8,5))
plt.bar(results.keys(), results.values(),
color=['blue','green','red','purple'])
plt.ylim(0,1)
plt.title('Model Accuracy Comparison')
plt.ylabel('Accuracy')
for i, v in enumerate(results.values()):
plt.text(i, v + 0.01, f"{v:.2f}", ha='center')
plt.show()

def classify_image(img_path, model, classes, img_size=(64,64)):


img = Image.open(img_path).convert('RGB').resize(img_size)
img_arr = np.array(img) / 255.0
input_arr = np.expand_dims(img_arr, axis=0)
preds = model.predict(input_arr)
class_idx = np.argmax(preds)
confidence = preds[0][class_idx]
print(f"Predicted class: {classes[class_idx]}, Confidence:
{confidence:.2f}")

if __name__ == '__main__':
dataset_path = 'lung_dataset' # Change this to your lung dataset path
npz_file = 'lung_data.npz'
image_size = (64, 64)

print("Converting images to .npz format...")


images_to_npz(dataset_path, img_size=image_size, save_path=npz_file)

print("Loading dataset...")
X, y, classes = load_data(npz_file)

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
stratify=y, random_state=42)

X_train_flat = prepare_ml_data(X_train)
X_test_flat = prepare_ml_data(X_test)

print("Training and evaluating models...")


results, ann_model = train_and_evaluate(X_train_flat, X_test_flat,
y_train, y_test, classes)

print("Plotting comparison chart...")


plot_results(results)

# Classify single image - update with your test image path


test_img_path = 'test_lung_xray.jpg'
print("Classifying single image...")
classify_image(test_img_path, ann_model, classes, image_size)

You might also like