def pre_screen(img, display_img): """ Performs inference and displays target image with result. Args: - img -> type:Image -> fastai wrapper for pixel image. - display_img -> type:str -> location of static image being analyzed. """ with st.spinner('Wait for it...'): time.sleep(3) model = load_learner('models/', file="e-covidnet.pkl") if torch.cuda.is_available(): model.model.load_state_dict(torch.load('models/e-covidnet.pth')) else: model.model.load_state_dict( torch.load('models/e-covidnet.pth', map_location=torch.device("cpu"))) pred_class = model.predict(img)[0] pred_prob = round(torch.max(model.predict(img)[2]).item() * 100) if str(pred_class) == 'COVID-19': st.success("COVID-19 with the probability of " + str(pred_prob) + '%.') st.image(display_img, width=300) elif str(pred_class) == 'pneumonia': result = st.success("Viral Pneumonia with the probability of " + str(pred_prob) + '%.') st.image(display_img, width=300) elif str(pred_class) == 'normal': st.success("Normal with the probability of " + str(pred_prob) + '%.') st.image(display_img, width=300)
def predict(img): with st.spinner('Please wait..'): time.sleep(3) model = load_learner('.') pred_class,pred_idx,outputs = model.predict(img) pred_prob = round(torch.max(model.predict(img)[2]).item()*100) st.write('## This is ',str(pred_class).capitalize(),' with probablity of ',pred_prob,'%')
def predict(img, display_img): # Display the test image st.image(display_img, use_column_width=True) # Temporarily displays a message while executing with st.spinner('Using model 1...'): time.sleep(3) # Load model and make prediction model = load_learner('models/', 'export.pkl') pred_class = model.predict(img)[0] # get the predicted class pred_prob = round(torch.max(model.predict(img)[2]).item() * 100) # get the max probability # Display the prediction if str(pred_class) == '0-4': st.success("You are 0-4 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '5-9': st.success("You are 5-9 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '10-14': st.success("You are 10-14 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '15-19': st.success("You are 15-19 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '20-24': st.success("You are 20-24 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '25-29': st.success("You are 25-29 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '30-34': st.success("You are 30-34 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '35-39': st.success("You are 35-39 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '40-44': st.success("You are 40-44 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '45-49': st.success("You are 45-49 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '50-54': st.success("You are 50-54 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '55-59': st.success("You are 55-59 with the probability of " + str(pred_prob) + '%.') elif str(pred_class) == '60-64': st.success("You are 60-64 with the probability of " + str(pred_prob) + '%.') else: st.success("You are 65+ with the probability of " + str(pred_prob) + '%.')
def predict(img, display_img): st.image(display_img, use_column_width=True) with st.spinner('Wait for it...'): time.sleep(5) model = load_learner('model/images/') pred_class = model.predict(img)[0] pred_prob = (torch.max(model.predict(img)[2]).item() * 100) if str(pred_class) == 'poorichole': st.success("This is Poori Chole. Probability -> " + str(pred_prob) + '%.') else: st.success("This is Samosa. Probability -> " + str(pred_prob) + '%.')
def predict(img, display_img): # Display the test image st.image(display_img, use_column_width=True) # Temporarily displays a message while executing with st.spinner('Wait for it...'): time.sleep(3) # Load model and make prediction model = load_learner('model/data/train/') pred_class = model.predict(img)[0] pred_prob = round(torch.max(model.predict(img)[2]).item() * 100) # Display the prediction if str(pred_class) == 'mtp': st.success("This is Son Tung M-TP with the probability of " + str(pred_prob) + '%.') else: st.success("This is G-Dragon with the probability of " + str(pred_prob) + '%.')
def get_prediction(image_tensor): hasil = model.predict(image_tensor) return str(hasil[0]), torch.max(hasil[2]).item() * 100
def predict(img, display_img): # display test image st.image(display_img, use_column_width=True) # temporary message with st.spinner('Thinking...'): time.sleep(3) # load trained model model = load_learner('models/', 'WatchProj_classifier.pkl') # prediction pred_class = model.predict(img)[0] # confidence percentage pred_prob = round(torch.max(model.predict(img)[2]).item() * 100) if str(pred_class) == 'rolex': st.success("I am " + str(pred_prob) + '%' " confident this is a Rolex.") if str(pred_class) == 'audemarspiguet': st.success("I am " + str(pred_prob) + '%' " confident this is an Audemars Piguet.") if str(pred_class) == 'breitling': st.success("I am " + str(pred_prob) + '%' " confident this is an Breitling.") if str(pred_class) == 'iwc': st.success("I am " + str(pred_prob) + '%' " confident this is an IWC.") if str(pred_class) == 'jaegerlecoultre': st.success("I am " + str(pred_prob) + '%' " confident this is a Jaegerlecoultre.") if str(pred_class) == 'omega': st.success("I am " + str(pred_prob) + '%' " confident this is an Omega.") if str(pred_class) == 'panerai': st.success("I am " + str(pred_prob) + '%' " confident this is a Panerai.") if str(pred_class) == 'patekphilippe': st.success("I am " + str(pred_prob) + '%' " confident this is a Patek Philippe.") if str(pred_class) == 'cartier': st.success("I am " + str(pred_prob) + '%' " confident this is a Cartier.") if str(pred_class) == 'gucci': st.success("I am " + str(pred_prob) + '%' " confident this is a Gucci.") if str(pred_class) == 'seiko': st.success("I am " + str(pred_prob) + '%' " confident this is a Seiko.") if str(pred_class) == 'movado': st.success("I am " + str(pred_prob) + '%' " confident this is a Movado.") if str(pred_class) == 'zenith': st.success("I am " + str(pred_prob) + '%' " confident this is an Zenith.")
from utils import get_databunch from make_vgg_resnet import VGG_ResNet if __name__ == "__main__": datasetdir = os.path.join(os.path.dirname(__file__), './kuzu_mnist') datasetdir = os.path.abspath(datasetdir) # Load dataset databunch = get_databunch(datasetdir) # Create VGG + ResNet model learn = Learner(databunch, VGG_ResNet(), metrics=accuracy) # Load learn.load('vgg_resnet_model_with_norm') # Validate loss, acc = learn.validate() print('val_loss: {}, val_acc: {}'.format(loss, acc)) mat = np.zeros((10, 10)) for data in databunch.valid_ds: images, labels = data images = images.reshape((1, 1, 28, 28)) outputs = learn.model(images) _, predicted = torch.max(outputs, 1) predicted = int(predicted) mat[labels][predicted] += 1 print(mat)