Upload 4 files
Browse files
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2024 Dhinesh kumar A
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
---
|
|
|
|
| 1 |
+
# vit
|
| 2 |
+
multiple sclerosis it can classify
|
|
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from flask import Flask, render_template, request, redirect, url_for
|
| 4 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from werkzeug.utils import secure_filename
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__, static_folder="static", template_folder="templates")
|
| 9 |
+
|
| 10 |
+
# Load feature extractor and model
|
| 11 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(
|
| 12 |
+
'google/vit-base-patch16-224-in21k'
|
| 13 |
+
)
|
| 14 |
+
model = ViTForImageClassification.from_pretrained(
|
| 15 |
+
'google/vit-base-patch16-224-in21k', num_labels=4
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Path to custom trained weights
|
| 19 |
+
model_path = r'C:\Users\Dhinesh kumar A\Downloads\vit-main\vit-main\vit_multiple_sclerosis_final.pth'
|
| 20 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 21 |
+
model.eval()
|
| 22 |
+
|
| 23 |
+
# Class mapping
|
| 24 |
+
class_mapping = {
|
| 25 |
+
0: 'Control-Axial',
|
| 26 |
+
1: 'Control-Sagittal',
|
| 27 |
+
2: 'MS-Axial',
|
| 28 |
+
3: 'MS-Sagittal'
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# Upload folder
|
| 32 |
+
UPLOAD_FOLDER = os.path.join(app.static_folder, "uploads")
|
| 33 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.route('/')
|
| 37 |
+
def index():
|
| 38 |
+
return render_template('index.html')
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@app.route('/predict', methods=['GET', 'POST'])
|
| 42 |
+
def upload_and_predict():
|
| 43 |
+
if request.method == 'POST':
|
| 44 |
+
if 'file' not in request.files or request.files['file'].filename == '':
|
| 45 |
+
return redirect(request.url)
|
| 46 |
+
|
| 47 |
+
file = request.files['file']
|
| 48 |
+
filename = secure_filename(file.filename)
|
| 49 |
+
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 50 |
+
file.save(filepath)
|
| 51 |
+
|
| 52 |
+
# Preprocess image
|
| 53 |
+
image = Image.open(filepath).convert('RGB')
|
| 54 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 55 |
+
pixel_values = inputs['pixel_values']
|
| 56 |
+
|
| 57 |
+
# Inference
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
outputs = model(pixel_values=pixel_values)
|
| 60 |
+
logits = outputs.logits
|
| 61 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 62 |
+
predicted_class = class_mapping[predicted_class_idx]
|
| 63 |
+
|
| 64 |
+
# Serve uploaded image via /static/uploads
|
| 65 |
+
image_url = url_for('static', filename=f'uploads/{filename}')
|
| 66 |
+
|
| 67 |
+
return render_template(
|
| 68 |
+
'result.html',
|
| 69 |
+
prediction=predicted_class,
|
| 70 |
+
uploaded_image=image_url
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return render_template('upload.html')
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
if __name__ == '__main__':
|
| 77 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
Pillow
|
| 5 |
+
numpy
|
| 6 |
+
lime
|
| 7 |
+
scikit-image
|
| 8 |
+
matplotlib
|
| 9 |
+
Werkzeug
|