def test_rod_task(): with open('samples/sample_letter.txt', 'r') as f: raw_letter = f.read() assert (len(raw_letter) > 0) prep_letter = clean_text(raw_letter) prep_letter = tokenize(prep_letter) prep_letter = filter_text(prep_letter) selected_model = 'fasttext' selected_task = 'rod_task' selected_threshold = 8 logging.info('Testing risk of death task..') try: os.makedirs('data/pickles/') except: pass controller = ExplainerController(selected_model, selected_task, 'config/model_config.yml', 'config/data_config.yml') controller.download_model_files() ft_model, tokenizer, risk_grps = controller.load_model_files() predictor = Predictor( tokenizer, ft_model, risk_grps, controller.model_config['explainers'][selected_task]['class_names'], threshold=selected_threshold) explainer = Explainer( tokenizer, ft_model, controller.model_config['explainers'][selected_task]['class_names'], predictor, threshold=selected_threshold) prep_letter = explainer.preprocess_set([prep_letter], puncts, odd_chars, contraction_mapping) letter_pred_dict = predictor.extract_set_preds([prep_letter]) letter_df = predictor.preds_to_df(letter_pred_dict, raw_letter) evaluator = Evaluator(explainer, predictor, letter_df) lime_df = evaluator.lime_eval_predictions() lime_df = evaluator.lime_extract_explainer_stats(lime_df) full_df = evaluator.sr_eval_predictions(lime_df) full_df = evaluator.sr_extract_stats(full_df) assert (len(full_df.iloc[0].Explanations) > 0 and len(full_df.iloc[0].S_Top_Pos_Ranks.items()) > 0) assert (full_df.iloc[0].Risk > 0 and full_df.iloc[0].Risk <= 10)
def __init__(self): self.progress = None self.window = Tk() self.init_window() self.predictor = Predictor()
class Window: components = UIComponent() def __init__(self): self.progress = None self.window = Tk() self.init_window() self.predictor = Predictor() def init_window(self): self.window.title("COVID-19 Protocol") self.window.geometry("840x950") Window.components.browse_file_label = Label( self.window, text="Please select the file to work on") Window.components.browse_file_label.place(x=20, y=20) Window.components.browse_btn = Button(self.window, text="Browse", command=self.read_file) Window.components.browse_btn.place(x=650, y=47) Window.components.file_path_read_only = Text(self.window, height=1, width=60) Window.components.file_path_read_only.place(x=20, y=50) Window.components.file_path_read_only.configure(state=DISABLED) Window.components.file_path_read_only.bind( "<1>", lambda event: self.components.file_path_read_only.focus_set()) # meter to pixels scene width scene_label = Label( self.window, text="Please enter the scene width of the image in meters") scene_label.place(x=20, y=80) Window.components.scene_width_text = Text(self.window, height=1, width=10) Window.components.scene_width_text.place(x=400, y=80) # covid protocol distance in meters threshold_label = Label( self.window, text="Please enter covid protocol threshold distance") threshold_label.place(x=20, y=130) Window.components.threshold_distance_text = Text(self.window, height=1, width=10) Window.components.threshold_distance_text.place(x=400, y=130) # Start Window.components.start_btn = Button(self.window, text="Start", command=self.start_model) Window.components.start_btn.place(x=350, y=170) # Canvas for result Window.components.canvas = Canvas(self.window, width=820, height=720) Window.components.canvas.place(x=10, y=220) def start(self): self.window.mainloop() def read_file(self): file_path = filedialog.askopenfilename(parent=self.window) Window.components.file_path_read_only.configure(state=NORMAL) Window.components.file_path_read_only.delete('1.0', END) Window.components.file_path_read_only.insert('1.0', file_path) Window.components.file_path_read_only.configure(state=DISABLED) return file_path def display_image(self, img_path): h, w, d = cv2.imread(img_path).shape if w > 820: h = 820 * h / w w = 820 if h > 720: w = 720 * w / h h = 720 img = Image.open(img_path) img = img.resize((int(w), int(h)), Image.ANTIALIAS) Window.components.canvas.res_img = img = ImageTk.PhotoImage(img) Window.components.canvas.create_image( (0, 0), anchor=NW, image=Window.components.canvas.res_img) Window.components.canvas.image = Window.components.canvas.res_img def validate_input(self, file_path, scene_width, threshold_distance): if FileUtils.get_file_type( file_path) == FileUtils.FILE_TYPE_NOT_SUPPORTED: messagebox.showerror("Input Error", "File should be an image or video!") return False if not self.validate_number(scene_width): messagebox.showerror("Input Error", "Scene width should be a number!") return False if not self.validate_number(threshold_distance): messagebox.showerror("Input Error", "Threshold distance should be a number!") return False return True def validate_number(self, val): if val.isdigit(): return True try: float(val) return True except ValueError: return False def start_model(self): Window.components.file_path_read_only.configure(state=NORMAL) file_path = Window.components.file_path_read_only.get("1.0", END).strip() Window.components.file_path_read_only.configure(state=DISABLED) scene_width = Window.components.scene_width_text.get("1.0", END) threshold_dist = Window.components.threshold_distance_text.get( "1.0", END) if self.validate_input(file_path, scene_width, threshold_dist): if FileUtils.get_file_type(file_path) == FileUtils.FILE_TYPE_IMAGE: self.predict_image(file_path, float(scene_width), float(threshold_dist)) elif FileUtils.get_file_type( file_path) == FileUtils.FILE_TYPE_VIDEO: self.predict_video(file_path, float(scene_width), float(threshold_dist)) def predict_image(self, file_path, scene_width, threshold_distance): img = self.predictor.predict_image(file_path, scene_width, threshold_distance) print('\nFinish prediction\n') img_name = FileUtils.get_out_image_path(file_path) print('Output image in ' + img_name) ImageUtils.save_image(img_name, img) self.display_image(img_name) def predict_video(self, file_path, scene_width, threshold_distance): video = self.predictor.predict_video(file_path, scene_width, threshold_distance) messagebox.showinfo("Video Result", "Your result video is under this path: " + video)
def home(): """Renders the home page. Gets the input model parameters from the forms and runs the predictions with the selected config. Returns the results (statistics, explanation highlights) in a JSON format, processed by AJAX. """ if request.method == 'GET': return render_template( 'index.html', year=datetime.now().year, ) raw_letter = request.form['md-form-text'] selected_model = request.form['model-select'] selected_task = request.form['task-select'] selected_threshold = request.form['threshold-select'] if selected_threshold == 'Default': selected_threshold = 8 if selected_model == 'fasttext' else 10 prep_letter = clean_text(raw_letter) prep_letter = tokenize(prep_letter) prep_letter = filter_text(prep_letter) #### 2. Download model files controller = ExplainerController(selected_model, selected_task, 'config/model_config.yml', 'config/data_config.yml') controller.download_model_files() ft_model, tokenizer, risk_grps = controller.load_model_files() #### 3. Initialize predictor and explainer predictor = Predictor( tokenizer, ft_model, risk_grps, controller.model_config['explainers'][selected_task]['class_names'], threshold=selected_threshold) explainer = Explainer( tokenizer, ft_model, controller.model_config['explainers'][selected_task]['class_names'], predictor, threshold=selected_threshold) #### 4. Extract predictions prep_letter = explainer.preprocess_set([prep_letter], puncts, odd_chars, contraction_mapping) letter_pred_dict = predictor.extract_set_preds([prep_letter]) letter_df = predictor.preds_to_df(letter_pred_dict, raw_letter) #### 5. Evaluate with LIME and Sentence Ranking evaluator = Evaluator(explainer, predictor, letter_df) lime_df = evaluator.lime_eval_predictions() lime_df = evaluator.lime_extract_explainer_stats(lime_df) full_df = evaluator.sr_eval_predictions(lime_df) full_df = evaluator.sr_extract_stats(full_df) top_positive = "" top_negative = "" top_positive_score = 0.0 top_negative_score = 0.0 for i in range(len(full_df.iloc[0].Explanations)): if full_df.iloc[0].Explanations[i][1] > 0: top_positive = full_df.iloc[0].Explanations[i][0] top_positive_score = full_df.iloc[0].Explanations[i][1] break for i in range(len(full_df.iloc[0].Explanations)): if full_df.iloc[0].Explanations[i][1] < 0: top_negative = full_df.iloc[0].Explanations[i][0] top_negative_score = full_df.iloc[0].Explanations[i][1] break #full_df.to_csv('samples/sample_df.csv') logging.info('Explanation complete.. generating results.') conf_score = float(full_df.iloc[0].Confidence) if float( full_df.iloc[0].Confidence) >= 0.5 else ( 1 - float(full_df.iloc[0].Confidence)) session['risk_score'] = str(full_df.iloc[0].Risk) session['risk_per'] = str(round(full_df.iloc[0].Risk_per * 100, 2)) session['risk_conf'] = str(round(conf_score * 100, 2)) session['m_exp_score'] = str(round(full_df.iloc[0].Mean_exp_score, 4)) session['m_sent_r_score'] = str( round(full_df.iloc[0].Mean_Rank_Sent_Score, 4)) session['t_pos_exp'] = str(top_positive) session['t_neg_exp'] = str(top_negative) session['t_pos_exp_s'] = str(round(top_positive_score, 4)) session['t_neg_exp_s'] = str(round(top_negative_score, 4)) session['t_pos_sent_exp_keys'] = [ el[0] for el in full_df.iloc[0].S_Top_Pos_Ranks.items() ] session['t_neg_sent_exp_keys'] = [ el[0] for el in full_df.iloc[0].S_Top_Neg_Ranks.items() ] session['t_pos_sent_exp_vals'] = [ round(el[1], 4) for el in full_df.iloc[0].S_Top_Pos_Ranks.items() ] session['t_neg_sent_exp_vals'] = [ round(el[1], 4) for el in full_df.iloc[0].S_Top_Neg_Ranks.items() ] session['explanations'] = json.dumps([ (el[0], el[1]) for el in full_df.iloc[0].Explanations ]) session['risk_str'] = 'High risk' if full_df.iloc[0].Risk >= int( selected_threshold) else 'Low risk' if selected_model == '1d_cnn': session['model_name'] = '1D-Conv-LSTM' else: session['model_name'] = 'Fasttext' if selected_task == 'rod_task': session['task_name'] = 'Risk of death estimation (1yr)' else: session['task_name'] = 'Risk of hospital readmission (30d)' #session['raw_letter'] = full_df.iloc[0].Raw_Text return jsonify(list(session.items()))
# weights if args.weights is None and args.mode == 1: raise RuntimeError("Predicting without loaded weights") # output dimension if args.weights is not None and args.mode != 0: states = torch.load(args.weights) try: if int(states['output_dim']) != out_dim: raise RuntimeError("Output dim = {}, but was trained with dim = {}.".format( out_dim, int(states['output_dim']))) except KeyError: raise RuntimeError("Incorrect weights loaded.") # network model = Predictor(input_dim=in_dim, output_dim=out_dim, batch_size=args.batch_size, model_path=args.weights) if opts['use_cuda']: model = model.cuda() # training mode if to_train: for param in model.parameters(): param.requires_grad = True print("... Training network with time frames: {} -> {}".format(in_dim, out_dim)) with open('./models/nn_log.txt', 'a') as f: f.write('=== New run ===\n') # partition into training and validation sets split = np.ceil(len(data) * opts['train_per']).astype(int)