Merge remote-tracking branch 'origin/CNN_0001'

This commit is contained in:
2026-03-10 19:41:58 -04:00
5 changed files with 400 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
import keras
from keras.src.legacy.preprocessing.image import ImageDataGenerator
from flask import Flask, render_template, send_file, request
from flask import Flask, render_template, send_file, request, jsonify
import traceback
import numpy as np
import tensorflow
import sys
@@ -12,6 +13,11 @@ import uuid
#init flaskapp
app = Flask(__name__)
@app.errorhandler(Exception)
def handle_exception(e):
traceback.print_exc()
return jsonify({"error": str(e)}), 500
# use this on image passed in to the API
def save_image(imgData):
filename=str(uuid.uuid4())+'.jpg'
@@ -69,11 +75,11 @@ def predict_vgg16():
else :
threshold_output = np.where(predictions > 0.5, 1, 0)
response=str(predictions)+'-->'+str(threshold_output)
print('/predict_vgg16:'+response)
return response
@app.route('/predict_resnet50', methods=['GET','POST'])
def predict_resnet50():
print('/predict_resnet50')
test_image=request.get_data()
test_image = PIL.Image.open(io.BytesIO(test_image))
test_image = test_image.convert('L')
@@ -86,13 +92,13 @@ def predict_resnet50():
else :
threshold_output = np.where(predictions > 0.5, 1, 0)
response=str(predictions)+'-->'+str(threshold_output)
print('/predict_resnet50:'+response)
return response
# New model updated in October 2024
# To Test : curl -X POST --data-binary @ADT_11_0_0_Training_BollingerBand_20191129_270d.jpg http://127.0.0.1:5000/predict_resnet50_20241024_270
@app.route('/predict_resnet50_20241024_270', methods=['GET','POST'])
def predict_resnet50_20241024_270():
print('/predict_resnet50_20241024_270')
test_image=request.get_data()
test_image = PIL.Image.open(io.BytesIO(test_image))
test_image = test_image.convert('L')
@@ -105,29 +111,36 @@ def predict_resnet50_20241024_270():
else :
threshold_output = np.where(predictions > 0.5, 1, 0)
response=str(predictions)+'-->'+str(threshold_output)
print('/predict_resnet50_20241024_270:'+response)
return response
# This version expects the image to be of the form (x,x,3).
# @app.route('/predict_resnet50B', methods=['GET','POST'])
# def predict_resnet50B():
# print('/predict_resnet50B')
# test_image=request.get_data()
# save_image(test_image)
# test_image = PIL.Image.open(io.BytesIO(test_image))
# test_array=keras.preprocessing.image.img_to_array(test_image)
# batch_test_array=np.array([test_array])
# predictions=resnet50b_model.predict(batch_test_array)
# if type(predictions) == list:
# average_prediction = sum(predictions)/len(predictions)
# threshold_output = np.where(average_prediction > 0.5, 1, 0)
# else :
# threshold_output = np.where(predictions > 0.5, 1, 0)
# response=str(predictions)+'-->'+str(threshold_output)
# return response
@tensorflow.function(reduce_retracing=True)
def infer_convnext(x):
return convnext_model(x, training=False)
# New model updated in February 2026 ConvNexT)
@app.route('/predict_convnext', methods=['GET','POST'])
def predict_convnext():
test_image=request.get_data()
test_image = PIL.Image.open(io.BytesIO(test_image))
# Convert grayscale → RGB
test_image = test_image.convert('RGB')
# Resize to match training size
if test_image.size != (224, 224):
test_image = test_image.resize((224, 224))
test_array=keras.preprocessing.image.img_to_array(test_image)
batch_test_array = np.expand_dims(test_array, axis=0)
# predictions=convnext_model.predict(batch_test_array)
# predictions = convnext_model(batch_test_array, training=False)
predictions = infer_convnext(batch_test_array)
threshold_output = np.where(predictions > 0.5, 1, 0)
response=str(predictions)+'-->'+str(threshold_output)
print('/predict_convnext:'+response)
return response
@app.route('/predict_lenet5', methods=['GET','POST'])
def predict_lenet5():
print('/predict_lenet5')
test_image=request.get_data()
test_image = PIL.Image.open(io.BytesIO(test_image))
test_image = test_image.convert('L')
@@ -140,6 +153,7 @@ def predict_lenet5():
else :
threshold_output = np.where(predictions > 0.5, 1, 0)
response=str(predictions)+'-->'+str(threshold_output)
print('/predict_lenet5:'+response)
return response
# This method is used to process an image through PIL and send it back to the client. The client can then used this processed image as part of the training data
@@ -172,9 +186,10 @@ if __name__ == '__main__':
print('Loading {model_name}'.format(model_name=resnet50_20241024_270_model_name))
resnet50_20241024_270_model = keras.models.load_model(resnet50_20241024_270_model_name)
# resnet50b_model_name='../Weights/resnet50B.h5'
# print('Loading {model_name}'.format(model_name=resnet50b_model_name))
# resnet50b_model = keras.models.load_model(resnet50b_model_name)
convnext_model_name='../Weights/convnext_20260228_90.h5.keras'
convnext_model_name=os.path.realpath(path + '/' + convnext_model_name)
print('Loading {model_name}'.format(model_name=convnext_model_name))
convnext_model=keras.models.load_model(convnext_model_name);
vgg16_model_name='../Weights/vggnet16.h5.keras'
vgg16_model_name = os.path.realpath(path + '/' + vgg16_model_name)