Diff of /leukemia.py [000000] .. [48affa]

Switch to unified view

a b/leukemia.py
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Wed Mar  2 18:23:11 2022
4
5
@author: Rushi
6
"""
7
8
#api libraries
9
from flask import Flask, request, render_template
10
11
#predicting libraries
12
from tensorflow.keras import optimizers, preprocessing
13
import tensorflow_addons as tfa
14
import tensorflow as tf
15
from keras.models import model_from_json
16
import numpy as np
17
import os
18
19
20
app = Flask(__name__)
21
22
def get_model():
23
    global model
24
    DATA_PATH = "D:/RK/Marwadi University/Sem-8/Project/C-NMC_Leukemia/"
25
    os.chdir(DATA_PATH)
26
27
    json_file = open("model.json","r")
28
    loaded_model_json = json_file.read()
29
    json_file.close()
30
    model = model_from_json(loaded_model_json)
31
    model.load_weights("model.h5")
32
    LEARNING_RATE = 3e-5
33
    optimizer = optimizers.Adam(lr=LEARNING_RATE)
34
    model.compile(optimizer=optimizer, 
35
                  loss='binary_crossentropy', 
36
                  metrics=['accuracy',tfa.metrics.F1Score(num_classes=2, average='weighted')])
37
38
    
39
def load_image(img_path):
40
    
41
    img = tf.keras.preprocessing.image.load_img(img_path, target_size=(200, 200))
42
    img_tensor = tf.keras.preprocessing.image.img_to_array(img)                    # (height, width, channels)
43
    img_tensor = np.expand_dims(img_tensor, axis=0)         # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
44
    img_tensor /= 255
45
    
46
    return img_tensor
47
48
def predictions(img_path):
49
    
50
    new_image = load_image(img_path)
51
    
52
    preds = model.predict(new_image)
53
    
54
    prediction = np.argmax(preds)
55
    pct = "{:.2f}".format(np.max(preds)*100)
56
    if prediction == 1:
57
        return ["The Prediction of the sample is: ALL", pct]
58
    else:
59
        return ["The Prediction of the sample is: HEM", pct]
60
61
@app.route("/", methods=['GET','POST'])
62
63
def home():
64
    
65
    return render_template('home.html')
66
67
@app.route("/predict", methods = ['GET','POST'])
68
69
def predict():
70
    
71
    if request.method == 'POST':
72
        
73
        get_model()
74
        
75
        file = request.files['file']
76
        
77
        filename = file.filename
78
        
79
        file_path = os.path.join('static', filename)
80
        
81
        file.save(file_path)
82
        
83
        print(file_path)
84
        product = predictions(file_path)
85
        str1 = str(product[1])
86
        value = ("Prediction Confidence Percentage is: " + str1 + "%")
87
        print(value)
88
        
89
        return render_template('predict.html', user_image = file_path, product = product[0], value = value) 
90
91
if __name__ == "__main__":
92
    app.run()
93