|
import numpy as np |
|
import time |
|
from tensorflow.keras.preprocessing import image |
|
import streamlit as st |
|
|
|
import tensorflow as tf |
|
gpus = tf.config.experimental.list_physical_devices('GPU') |
|
if gpus: |
|
try: |
|
for gpu in gpus: |
|
tf.config.experimental.set_memory_growth(gpu, True) |
|
except RuntimeError as e: |
|
|
|
print(e) |
|
|
|
|
|
model = tf.keras.models.load_model('best_resnet152_model.h5') |
|
|
|
class_names = {0: '1099_Div', 1: '1099_Int', 2: 'Non_Form', 3: 'w_2', 4: 'w_3'} |
|
|
|
|
|
|
|
|
|
@st.cache_resource |
|
def predict(pil_img): |
|
|
|
img_array = image.img_to_array(pil_img) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
img_array /= 255.0 |
|
|
|
|
|
|
|
start_time = time.time() |
|
predictions = model.predict(img_array) |
|
end_time = time.time() |
|
predicted_class_index = np.argmax(predictions, axis=1)[0] |
|
|
|
|
|
predicted_class_name = class_names[predicted_class_index] |
|
print("Predicted class:", predicted_class_name) |
|
print("Execution time: ", end_time - start_time) |
|
return predicted_class_name |