RaniyaK commited on
Commit
028930f
·
verified ·
1 Parent(s): 99814b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -7
app.py CHANGED
@@ -1,10 +1,88 @@
1
- from flask import Flask, jsonify
 
 
 
 
 
 
2
 
3
- app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- @app.route('/api/data')
6
- def get_data():
7
- return jsonify({"message": "Hello from Flask!"})
 
 
 
 
 
 
 
 
8
 
9
- if __name__ == "__main__":
10
- app.run(port=3000) # Run on port 5000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import cv2
4
+ from sklearn.model_selection import train_test_split
5
+ from transformers import ViTForImageClassification, ViTFeatureExtractor
6
+ from transformers import Trainer, TrainingArguments
7
+ from datasets import load_dataset, Dataset
8
 
9
+ # Function to load images from specified folders
10
+ def load_images_from_folders(folders, label):
11
+ images = []
12
+ labels = []
13
+ for folder in folders:
14
+ for filename in os.listdir(folder):
15
+ if filename.lower().endswith(('.png', '.jpg', '.jpeg')): # Check for valid image extensions
16
+ img = cv2.imread(os.path.join(folder, filename), cv2.IMREAD_GRAYSCALE) # Read image as grayscale
17
+ if img is not None:
18
+ img = cv2.resize(img, (224, 224)) # Resize to 224x224 pixels
19
+ img = img.astype(np.float32) # Ensure the image is in float32 format
20
+ img /= 255.0 # Normalize to [0, 1]
21
+ images.append(img)
22
+ labels.append(label)
23
+ else:
24
+ print(f"Failed to load image: {filename}")
25
+ return images, labels
26
 
27
+ # Load normal and pneumonia images
28
+ normal_folders = [
29
+ os.path.join('chest-xray-pneumonia', 'chest_xray', 'test', 'NORMAL'),
30
+ os.path.join('chest-xray-pneumonia', 'chest_xray', 'train', 'NORMAL'),
31
+ os.path.join('chest-xray-pneumonia', 'chest_xray', 'val', 'NORMAL'),
32
+ ]
33
+ pneumonia_folders = [
34
+ os.path.join('chest-xray-pneumonia', 'chest_xray', 'test', 'PNEUMONIA'),
35
+ os.path.join('chest-xray-pneumonia', 'chest_xray', 'train', 'PNEUMONIA'),
36
+ os.path.join('chest-xray-pneumonia', 'chest_xray', 'val', 'PNEUMONIA'),
37
+ ]
38
 
39
+ normal_images, normal_labels = load_images_from_folders(normal_folders, 0)
40
+ pneumonia_images, pneumonia_labels = load_images_from_folders(pneumonia_folders, 1)
41
+
42
+ # Combine images and labels
43
+ images = normal_images + pneumonia_images
44
+ labels = normal_labels + pneumonia_labels
45
+
46
+ # Split the dataset into training and testing sets
47
+ X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42)
48
+
49
+ # Convert the dataset to a Hugging Face Dataset
50
+ train_dataset = Dataset.from_dict({"image": X_train, "label": y_train})
51
+ test_dataset = Dataset.from_dict({"image": X_test, "label": y_test})
52
+
53
+ # Load feature extractor and model
54
+ feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224-in21k')
55
+ model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224-in21k', num_labels=2)
56
+
57
+ # Preprocess the dataset
58
+ def preprocess_function(examples):
59
+ return feature_extractor(images=examples['image'], return_tensors="pt")
60
+
61
+ train_dataset = train_dataset.map(preprocess_function, batched=True)
62
+ test_dataset = test_dataset.map(preprocess_function, batched=True)
63
+
64
+ # Training arguments
65
+ training_args = TrainingArguments(
66
+ output_dir='./results',
67
+ evaluation_strategy='epoch',
68
+ learning_rate=2e-5,
69
+ per_device_train_batch_size=8,
70
+ per_device_eval_batch_size=8,
71
+ num_train_epochs=10,
72
+ weight_decay=0.01,
73
+ )
74
+
75
+ # Trainer
76
+ trainer = Trainer(
77
+ model=model,
78
+ args=training_args,
79
+ train_dataset=train_dataset,
80
+ eval_dataset=test_dataset,
81
+ )
82
+
83
+ # Train the model
84
+ trainer.train()
85
+
86
+ # Save the model
87
+ model.save_pretrained('./pneumonia_model_final')
88
+ print("Model saved as './pneumonia_model_final'")