Beispiel #1
0
    def __init__(self):
        #data_manager 생성
        self.data_manager = Data_manager()

        train_sentences = self.data_manager.get_train_sentences()
        train_tags = self.data_manager.get_train_tags()

        #학습 문장으로 word2_vec_embedder 생성
        self.word2vec_embedder = Word2vec_embedder(train_sentences)

        #word2vec 방식으로 embedding 된 vector_set
        with open('trainDataVecs.pkl', 'rb') as f:
            trainDataVecs = pickle.load(f)

        #embedding 된 vector_set과 정답지로 Sentiment_analysis 모델 생성
        self.sentiment_analysis_model = Sentiment_analysis_model(
            trainDataVecs, train_tags)
    def __init__(self, parent):
        self.parent = parent
        self.model = Model()
        self.view = View(parent)
        self.data_manager = Data_manager()
        self.view.setup()

        pub.subscribe(self.button_1_pressed, "BUTTON_1_PRESSED")
        pub.subscribe(self.button_2_pressed, "BUTTON_2_PRESSED")
        pub.subscribe(self.button_3_pressed, "BUTTON_3_PRESSED")
        pub.subscribe(self.load_image, "LOAD_IMAGE")
        pub.subscribe(self.image_loaded, "IMAGE_LOADED")
        pub.subscribe(self.barthel_data_sent, "BARTHEL_DATA_SENT")
        pub.subscribe(self.update_barthel, "UPDATE_BARTHEL")
        pub.subscribe(self.error_barthel, "ERROR_BARTHEL")
        pub.subscribe(self.emina_data_sent, "EMINA_DATA_SENT")
        pub.subscribe(self.update_emina, "UPDATE_EMINA")
        pub.subscribe(self.error_emina, "ERROR_EMINA")
        pub.subscribe(self.analyse_image, "ANALYSE_IMAGE")
        pub.subscribe(self.ask_mask_confirmation, "ASK_MASK_CONFIRMATION")
        pub.subscribe(self.pre_segmentation_confirmated, "PRE_SEGMENTATION_CONFIRMATED")
        pub.subscribe(self.segmentation_gui, "SEGMENTATION_GUI")
        pub.subscribe(self.roi_granulation, "ROI_GRANULATION")
        pub.subscribe(self.roi_necrosis, "ROI_NECROSIS")
        pub.subscribe(self.roi_slough, "ROI_SLOUGH")
        pub.subscribe(self.ask_roi_confirmation, "ASK_ROI_CONFIRMATION")
        pub.subscribe(self.roi_confirmated, "ROI_CONFIRMATED")
        pub.subscribe(self.ask_perimeter, "ASK_PERIMETER")
        pub.subscribe(self.update_perimeter_count, "UPDATE_PERIMETER_COUNT")
        pub.subscribe(self.update_granulation_count, "UPDATE_GRANULATION_COUNT")
        pub.subscribe(self.update_necrosis_count, "UPDATE_NECROSIS_COUNT")
        pub.subscribe(self.update_slough_count, "UPDATE_SLOUGH_COUNT")
        pub.subscribe(self.tot_ple_ko, "TOT_PLE_KO")
        pub.subscribe(self.data_ko, "DATA_KO")
        pub.subscribe(self.data_ok, "DATA_OK")
        pub.subscribe(self.flash_reduction, "FLASH_REDUCTION")
        pub.subscribe(self.flash_confirmated, "FLASH_CONFIRMATED")
        pub.subscribe(self.image_accepted, "IMG_ACCEPTED")
        pub.subscribe(self.data_files_ko, "DATA_FILES_KO")
        pub.subscribe(self.data_n_elements, "DATA_N_ELEMENTS")
        pub.subscribe(self.ask_image_i, "ASK_IMAGE_i")
        pub.subscribe(self.load_image_i, "IMAGE_LOAD_i")
        pub.subscribe(self.load_metadata_i, "METADATA_LOAD_i")
Beispiel #3
0
    def update_model(self):
        #data_manager 생성
        self.data_manager = Data_manager()
        self.data_manager.update_train_data_set_from_report_data('report.txt')
        train_sentences = self.data_manager.get_train_sentences()
        train_tags = self.data_manager.get_train_tags()
        print(len(train_sentences))
        print(len(train_tags))

        # 학습 문장으로 word2_vec_embedder 생성
        self.word2vec_embedder.make_model(train_sentences)
        #word2vec 방식으로 embedding 된 vector_set
        trainDataVecs = word2vec_embbeding_module.getAvgFeatureVecs(
            train_sentences, self.word2vec_embedder.model,
            self.word2vec_embedder.num_features)
        with open('trainDataVecs.pkl', 'wb') as f:
            pickle.dump(trainDataVecs, f)
        #embedding 된 vector_set과 정답지로 Sentiment_analysis 모델 생성
        self.sentiment_analysis_model.set_x_train(trainDataVecs)
        self.sentiment_analysis_model.set_y_train(train_tags)
        self.sentiment_analysis_model.make_model()
Beispiel #4
0
class Sentiment_analysis:
    def __init__(self):
        #data_manager 생성
        self.data_manager = Data_manager()

        train_sentences = self.data_manager.get_train_sentences()
        train_tags = self.data_manager.get_train_tags()

        #학습 문장으로 word2_vec_embedder 생성
        self.word2vec_embedder = Word2vec_embedder(train_sentences)

        #word2vec 방식으로 embedding 된 vector_set
        with open('trainDataVecs.pkl', 'rb') as f:
            trainDataVecs = pickle.load(f)

        #embedding 된 vector_set과 정답지로 Sentiment_analysis 모델 생성
        self.sentiment_analysis_model = Sentiment_analysis_model(
            trainDataVecs, train_tags)

    def Sentiment_analysis_predict_pos_neg(self, sentence):
        # sentence, score = self.sentiment_analysis_model.predict_pos_neg_by_loaded_model(sentence,self.word2vec_embedder.model,self.word2vec_embedder.num_features)
        sentence, score = self.sentiment_analysis_model.predict_pos_neg(
            sentence, self.word2vec_embedder.model,
            self.word2vec_embedder.num_features)

        return sentence, score

    def update_model(self):
        #data_manager 생성
        self.data_manager = Data_manager()
        self.data_manager.update_train_data_set_from_report_data('report.txt')
        train_sentences = self.data_manager.get_train_sentences()
        train_tags = self.data_manager.get_train_tags()
        print(len(train_sentences))
        print(len(train_tags))

        # 학습 문장으로 word2_vec_embedder 생성
        self.word2vec_embedder.make_model(train_sentences)
        #word2vec 방식으로 embedding 된 vector_set
        trainDataVecs = word2vec_embbeding_module.getAvgFeatureVecs(
            train_sentences, self.word2vec_embedder.model,
            self.word2vec_embedder.num_features)
        with open('trainDataVecs.pkl', 'wb') as f:
            pickle.dump(trainDataVecs, f)
        #embedding 된 vector_set과 정답지로 Sentiment_analysis 모델 생성
        self.sentiment_analysis_model.set_x_train(trainDataVecs)
        self.sentiment_analysis_model.set_y_train(train_tags)
        self.sentiment_analysis_model.make_model()
Beispiel #5
0
sheet = "https://docs.google.com/spreadsheets/d/1ecgbu4-tBPrXJtZzMFKN1YQdBdJ9aE5tKWLGBdDhVE0/edit?usp=sharing"

# Instance ofg the google sheedt class
my_sheet = Google_sheets(sheet)

# Save the list of google sheet data
data = my_sheet.get_data()

# Path of the project
current_path = os.path.dirname(__file__)

# Path of the csv file with local data
csv_file_path = os.path.join(current_path, "data.csv")

# Instance of the data manager class
my_data_manager = Data_manager(csv_file_path)

# Compare the local data and the new data. return new registers
new_data = my_data_manager.compare_data(data)

# Loop for each new register of data
for row in new_data:

    # EMAIL CONECTION

    # Local email account to send error message
    email_server = "*****@*****.**"
    password_server = "Superman2019"

    # Email information of the client
    email_user = row["email"]
Beispiel #6
0
    len(train_sentences)
    len(test_sentences)
    test_sentences[50000]
    test_label = [int(x) for x in test_label]
    # test_sentences[50000:50010]
    with open('trainDataVecs.pkl', 'wb') as f:
        pickle.dump(trainDataVecs, f)
    with open('testDataVecs.pkl', 'wb') as f:
        pickle.dump(testDataVecs, f)

    len(testDataVecs)

    sentiment_analysis.sentiment_analysis_model.model_evaluate(
        testDataVecs, test_label)
    sentiment_analysis.Sentiment_analysis_predict_pos_neg(test_text[1])[1]
    data_manager = Data_manager()
    type(test_label[0])
    TP = 0
    FP = 0
    FN = 0
    TN = 0
    for i in range(len(test_text)):
        result = sentiment_analysis.Sentiment_analysis_predict_pos_neg(
            test_text[i])[1]
        if (test_label_[i] == 1 and result >= 0):
            TP = TP + 1
        if (test_label_[i] == 1 and result < 0):
            FN = FN + 1
        if (test_label_[i] == 0 and result >= 0):
            FP = FP + 1
        if (test_label_[i] == 0 and result < 0):
class Controller:
    """
    A class used to handle requests

    ...

    Attributes
    ----------
    parent : Tk
        root window
    model : Model
        a class to process and manage data structures
    view : View
        a class for data representation and user interaction
    data_manager : Data_manager
        a class used to manage the files's data reading/writing

    Methods
    -------
    button_1_pressed()
        Prints that button_1 from view has been pressed.
    button_2_pressed()
        Prints that button_2 from view has been pressed and calls the function.
        load_data from Data_manager.
    button_3_pressed(data)
        Prints that button_3 from view has been pressed.
        Calls the functions to check metadata and images before saving.
    load_image()
        Calls the function to load a pressure injury's image.
        WARNING: For optimal visualization, images must be '560x390'
    image_loaded(image_tk)
        Defines a Pressure_img object and saves its image.
        Calls the update_image from View to update the label with the loaded image.
        Calls the function from View to show "process" button.
    barthel_data_sent(data)
        Calls the Model function to calculate barthel from data sent.
    emina_data_sent(data)
        Calls the Model function to calculate emina from data sent.
    update_barthel(data)
        Calls the update_barthel from View to update the Scale with the value.
    update_emina(data)
        Calls the update_emina from View to update the Scale with the value.
    error_barthel()
        View request to check barthel fields.
    error_emina()
        View request to check emina fields.
    analyse_image()
        Checks if Pressure_img has been processed and calls processing function if not.
    ask_mask_confirmation(img_cv2_mask, scale_factor)
        Calls the View function to ask user confirmation about a mask.
    ask_roi_confirmation(img_cv2_mask, scale_factor, tissue, scale_factor)
        Calls the View function to ask user confirmation about a roi.
    pre_segmentation_confirmated(img_imgtk_mask, img_cv2_mask)
        Calls the Pressure_img function for the first image segmentation.
    segmentation_gui(img_imgtk_mask, img_cv2_mask)
        Updates the Pressure_img mask image.
        Calls the View function to show GUI for img segmentation to the user.
    roi_granulation()
        Updates the Pressure_img mask image
        Calls the Pressure_img function to allow the user to select the granulation roi.
    roi_necrosis()
        Updates the Pressure_img mask image
        Calls the Pressure_img function to allow the user to select the necrosis roi.
    roi_slough()
        Updates the Pressure_img mask image
        Calls the Pressure_img function to allow the user to select the slough roi.
    roi_confirmated()
        Calls the Pressure_img function to update granulation/necrosis/slough fields.
    ask_perimeter()
        Checks if perimeter has been cropped and calls the Pressure_img function if not.
    update_perimeter_count()
        Updates the View of perimeter label.
    update_granulation_count(number)
        Updates the View of granulation's label with the number of roi's already selected.
    update_necrosis_count(number)
        Updates the View of necrosis's label with the number of roi's already selected.
    update_slough_count(number)
        Updates the View of slough's label with the number of roi's already selected.
    tot_ple_ko()
        Calls a View function to warn the user that all metadata fields must be filled.
    data_ko(error)
        Calls a View function to warn the user what field has the wrong input data.
    data_ok()
        Sets Pressure_img loaded boolean to False.
        Calls View function to reset loaded image label.
        Calls the function to save all the data entered by the user.
    flash_reduction(img_cv2_mask)
        Checks if flash reduction has been called and calls
        Pressure_img function to reduce flash if not.
        Calls the View function to ask user confirmation.
    flash_confirmated(img_cv2_flash)
        Updates Pressure_img mask and flash_reduced boolean.
        Calls the View function to update image label.
    image_accepted()
        Updates Pressure_img processed boolean to True.
    save_data_file()
        Calls the Data_manager function to save user introduced data and images.
    data_files_ko()
        Calls the View function to warn the user about a file manager error.
    data_n_elements(num)
        Calls the View function to update the element's label counter with a new value.
    load_image_i(image_tk)
        Calls the View function to load the image_tk to the p2_label_img.
    load_metadata_i(metadata)
        Calls the View function to load the metadata to the p2_label_metadata.
    ask_image_i(i)
        Calls the Data_manager functions to read and load the image and metadata "i".
    """

    def __init__(self, parent):
        self.parent = parent
        self.model = Model()
        self.view = View(parent)
        self.data_manager = Data_manager()
        self.view.setup()

        pub.subscribe(self.button_1_pressed, "BUTTON_1_PRESSED")
        pub.subscribe(self.button_2_pressed, "BUTTON_2_PRESSED")
        pub.subscribe(self.button_3_pressed, "BUTTON_3_PRESSED")
        pub.subscribe(self.load_image, "LOAD_IMAGE")
        pub.subscribe(self.image_loaded, "IMAGE_LOADED")
        pub.subscribe(self.barthel_data_sent, "BARTHEL_DATA_SENT")
        pub.subscribe(self.update_barthel, "UPDATE_BARTHEL")
        pub.subscribe(self.error_barthel, "ERROR_BARTHEL")
        pub.subscribe(self.emina_data_sent, "EMINA_DATA_SENT")
        pub.subscribe(self.update_emina, "UPDATE_EMINA")
        pub.subscribe(self.error_emina, "ERROR_EMINA")
        pub.subscribe(self.analyse_image, "ANALYSE_IMAGE")
        pub.subscribe(self.ask_mask_confirmation, "ASK_MASK_CONFIRMATION")
        pub.subscribe(self.pre_segmentation_confirmated, "PRE_SEGMENTATION_CONFIRMATED")
        pub.subscribe(self.segmentation_gui, "SEGMENTATION_GUI")
        pub.subscribe(self.roi_granulation, "ROI_GRANULATION")
        pub.subscribe(self.roi_necrosis, "ROI_NECROSIS")
        pub.subscribe(self.roi_slough, "ROI_SLOUGH")
        pub.subscribe(self.ask_roi_confirmation, "ASK_ROI_CONFIRMATION")
        pub.subscribe(self.roi_confirmated, "ROI_CONFIRMATED")
        pub.subscribe(self.ask_perimeter, "ASK_PERIMETER")
        pub.subscribe(self.update_perimeter_count, "UPDATE_PERIMETER_COUNT")
        pub.subscribe(self.update_granulation_count, "UPDATE_GRANULATION_COUNT")
        pub.subscribe(self.update_necrosis_count, "UPDATE_NECROSIS_COUNT")
        pub.subscribe(self.update_slough_count, "UPDATE_SLOUGH_COUNT")
        pub.subscribe(self.tot_ple_ko, "TOT_PLE_KO")
        pub.subscribe(self.data_ko, "DATA_KO")
        pub.subscribe(self.data_ok, "DATA_OK")
        pub.subscribe(self.flash_reduction, "FLASH_REDUCTION")
        pub.subscribe(self.flash_confirmated, "FLASH_CONFIRMATED")
        pub.subscribe(self.image_accepted, "IMG_ACCEPTED")
        pub.subscribe(self.data_files_ko, "DATA_FILES_KO")
        pub.subscribe(self.data_n_elements, "DATA_N_ELEMENTS")
        pub.subscribe(self.ask_image_i, "ASK_IMAGE_i")
        pub.subscribe(self.load_image_i, "IMAGE_LOAD_i")
        pub.subscribe(self.load_metadata_i, "METADATA_LOAD_i")

    def button_1_pressed(self):
        """
        Prints that button_1 from view has been pressed.
        """

        print("controller - Botó 1!")

    def button_2_pressed(self):
        """
        Prints that button_2 from view has been pressed and calls the function.
        load_data from Data_manager.
        """

        print("controller - Botó 2!")
        self.data_manager.load_data()

    def button_3_pressed(self, data):
        """
        Prints that button_3 from view has been pressed.
        Calls the functions to check metadata and images before saving.

        Parameters
        ----------
        data : list
           a list with all the metadata field's information written by the user
        """

        print("controller - Botó 3!")
        try:
            if self.pressure_img.loaded:
                if self.pressure_img.processed:
                    self.model.getData(data)
                else:
                    self.view.popupmsg("És necessari processar la imatge.")
            else:
                self.view.popupmsg("És necessari carregar una imatge")
        except:
            self.view.popupmsg("És necessari carregar una imatge")

    def load_image(self):
        """
        Calls the function to load a pressure injury's image.
        WARNING: For optimal visualization, images must be '560x390'
        """

        print("controller - carregar imatge")
        self.pressure_img.path = self.model.carregar_imatge()

    def image_loaded(self, image_tk):
        """
        Defines a Pressure_img object and saves its image.
        Calls the update_image from View to update the label with the loaded image.
        Calls the function from View to show "process" button.

        Parameters
        ----------
        image_tk : PIL Image
           image ready to be loaded in a label
        """

        self.pressure_img = Pressure_img()
        self.pressure_img.img_origin = self.model.img_original
        self.pressure_img.loaded = True
        self.view.update_image(image_tk)
        self.view.botoImg()

    def barthel_data_sent(self, data):
        """
         Calls the Model function to calculate barthel from data sent.

         Parameters
        ----------
        data : list
           list with all the field's values selected by the user
        """

        print("controller - barthel_data_sent!")
        self.model.calculateBarthel(data)

    def emina_data_sent(self, data):
        """
         Calls the Model function to calculate emina from data sent.

         Parameters
        ----------
        data : list
           list with all the field's values selected by the user
        """

        print("controller - emina_data_sent!")
        self.model.calculateEmina(data)

    def update_barthel(self, data):
        """
        Calls the View function update_barthel to update barthel value.

        Parameters
        ----------
        data : int
            value of the barthel scale
       """

        print("controller - update_barthel")
        self.view.update_barthel(data)

    def update_emina(self, data):
        """
        Calls the View function update_emina to update emina value.

        Parameters
        ----------
        data : int
            value of the emina scale
       """

        print("controller - update_emina")
        self.view.update_emina(data)

    def error_barthel(self):
        """
       View request to check barthel fields.
       """

        print("controller - error barthel")
        self.view.popupmsg("S'han d'omplir tots els camps")

    def error_emina(self):
        """
        View request to check emina fields.
        """

        print("controller - error emina")
        self.view.popupmsg("S'han d'omplir tots els camps")

    def analyse_image(self):
        """
        Checks if Pressure_img has been processed and calls processing function if not.
        """

        print("controller - analyse_image!")
        if self.pressure_img.processed == False:
            self.pressure_img.crop_image(self.pressure_img.path)
        else:
            self.view.popupmsg("La imatge ja ha estat processada.")

    def ask_mask_confirmation(self, img_cv2_mask, scale_factor):
        """
        Calls the View function to ask user confirmation about an image's mask.

        Parameters
        ----------
        img_cv2_mask : image cv2
           image that requires user confirmation
        scale_factor : int
           image resize value (default = 100)
        """

        print("controller - ask_mask_confirmation!")
        try:
            self.view.ask_mask_confirmation(img_cv2_mask, scale_factor)
        except:
            self.view.popupmsg("Alguna cosa ha fallat. Torna-ho a intentar!")

    def ask_roi_confirmation(self, img_cv2_mask, img_cv2_roi, tissue, scale_factor):
        """
        Calls the View function to ask user confirmation about a image's roi.

        Parameters
        ----------
        img_cv2_mask : image cv2
           image before cropping roi
        img_cv2_roi : image cv2
           image that requires user confirmation
        tissue : String
            tissue of the roi
        scale_factor : int
           image resize value (default = 100)
        """

        print("controller - ask_roi_confirmation!")
        try:
            self.view.ask_roi_confirmation(img_cv2_mask, img_cv2_roi, tissue, scale_factor)
        except:
            self.view.popupmsg("Alguna cosa ha fallat. Torna-ho a intentar!")

    def pre_segmentation_confirmated(self, img_imgtk_mask, img_cv2_mask):
        """
        Calls the Pressure_img function for the first image segmentation.

        Parameters
        ----------
        img_imgtk_mask : PIL Image
           image before cropping roi
        img_cv2_mask : image cv2
           image that requires user confirmation
        """
        print("controller - pre_segmentation_confirmated!")
        scale_factor = 100
        self.pressure_img.begin_segmentation(img_imgtk_mask=img_imgtk_mask, img_cv2_mask=img_cv2_mask, scale_factor= scale_factor)

    def segmentation_gui(self, img_imgtk_mask, img_cv2_mask):
        """
        Updates the Pressure_img mask image.

        Parameters
        ----------
        img_imgtk_mask : PIL Image
           image before cropping roi
        img_cv2_mask : image cv2
           image that requires user confirmation
        """
        print("controller - segmentation_gui!")
        self.pressure_img.close_all()
        self.pressure_img.mask = img_cv2_mask
        self.view.segmentation_gui(img_imgtk_mask, img_cv2_mask)

    def roi_granulation(self):
        """
        Updates the Pressure_img mask image
        Calls the Pressure_img function to allow the user to select the granulation roi.
        """

        print("controller - roi_granulation!")
        img_cv2_mask = self.pressure_img.mask
        self.pressure_img.roi_crop(img_cv2_mask, "Granulation")

    def roi_necrosis(self):
        """
        Updates the Pressure_img mask image
        Calls the Pressure_img function to allow the user to select the necrosis roi.
        """

        print("controller - roi_necrosis!")
        img_cv2_mask = self.pressure_img.mask
        self.pressure_img.roi_crop(img_cv2_mask, "Necrosis")

    def roi_slough(self):
        """
        Updates the Pressure_img mask image
        Calls the Pressure_img function to allow the user to select the slough roi.
        """
        print("controller - roi_slough!")
        img_cv2_mask = self.pressure_img.mask
        self.pressure_img.roi_crop(img_cv2_mask, "Slough")

    def roi_confirmated(self, img_cv2_roi, tissue):
        """
        Calls the Pressure_img function to update granulation/necrosis/slough fields.

        Parameters
        ----------
        img_cv2_roi : image cv2
           roi selected by the user
        tissue : String
            tissue of the roi
        """
        print("controller - roi_confirmated!")
        self.pressure_img.save_mask_data(img_cv2_roi, tissue)

    def ask_perimeter(self):
        """
        Checks if perimeter has been cropped and calls the Pressure_img function if not.
        """

        print("controller - ask_perimeter!")
        img_cv2_mask = self.pressure_img.mask
        if self.pressure_img.perimetre_done:
            self.view.popupmsg("El perímetre ja ha estat seleccionat")
        else:
            self.pressure_img.roi_crop(img_cv2_mask, "Perimeter")

    def update_perimeter_count(self):
        """
        Updates the View of perimeter label.
        """

        print("controller - update_perimeter_count!")
        self.view.update_perimeter_count()

    def update_granulation_count(self, number):
        """
        Updates the View of granulation's label with the number of roi's already selected.

        Parameters
        ----------
        number : int
           number of granulation rois already selected
        """
        print("controller - update_granulation_count!")
        self.view.update_granulation_count(number)

    def update_necrosis_count(self, number):
        """
        Updates the View of necrosis's label with the number of roi's already selected.

        Parameters
        ----------
        number : int
           number of necrosis rois already selected
        """

        print("controller - update_necrosis_count!")
        self.view.update_necrosis_count(number)

    def update_slough_count(self, number):
        """
        Updates the View of slough's label with the number of roi's already selected.

        Parameters
        ----------
        number : int
           number of slough rois already selected
        """

        print("controller - update_slough_count!")
        self.view.update_slough_count(number)

    def tot_ple_ko(self):
        """
        Calls a View function to warn the user that all metadata fields must be filled.
        """

        print("controller - tot_ple_ko!")
        self.view.popupmsg("S'han d'omplir tots els camps")

    def data_ko(self, error):
        """
        Calls a View function to warn the user what field has the wrong input data.

        Parameters
        ----------
        error : list
           wrong input data field's name
        """

        self.view.show_error(error)
        print("controller - data_ko")

    def data_ok(self):
        """
        Sets Pressure_img loaded boolean to False.
        Calls View function to reset loaded image label.
        Calls the function to save all the data entered by the user.
        """

        print("controller - data_ok")
        self.pressure_img.loaded = False
        self.view.reset_view()
        try:
            self.save_data_file()
            self.view.popupmsg("Procés finalitzat amb èxit. Prem OK per continuar.")
        except:
            self.view.popupmsg("Error de gestió de fitxers.")

    def flash_reduction(self, img_cv2_mask):
        """
        Checks if flash reduction has been called and calls
        Pressure_img function to reduce flash if not.
        Calls the View function to ask user confirmation.

        Parameters
        ----------
        img_cv2_mask : image cv2
           image selected by user to reduce its flash
        """

        print("controller - flash_reduction!")
        if self.pressure_img.flash_reduced == False:
            try:
                img_flash_reduced = self.pressure_img.flash_reduction(img_cv2_mask)
                self.view.ask_flash_confirmation(img_cv2_mask, img_flash_reduced)
            except:
                self.view.popupmsg("Alguna cosa ha fallat. Torna-ho a intentar!")
        else:
            self.view.popupmsg("Ja s'ha aplicat la reducció.")

    def flash_confirmated(self, img_cv2_flash):
        """
        Updates Pressure_img mask and flash_reduced boolean.
        Calls the View function to update image label.

        Parameters
        ----------
        img_cv2_mask : image cv2
           image selected and validated by user to reduce its flash
        """

        print("controller - flash_confirmated!")
        self.pressure_img.flash_reduced = True
        self.pressure_img.mask = img_cv2_flash
        self.pressure_img.img = img_cv2_flash.copy()
        self.view.update_flash_label(img_cv2_flash)

    def image_accepted(self):
        """
        Updates Pressure_img processed boolean to True.
        """

        print("controller - image_accepted!")
        self.pressure_img.processed = True

    def save_data_file(self):
        """
        Calls the Data_manager function to save user introduced data and images.
        """

        print("controller - save_data_file")
        self.data_manager.save_data(self.model.metadata, self.pressure_img)

    def data_files_ko(self):
        """
        Calls the View function to warn the user about a file manager error.
        """

        print("controller - data_files_ko")
        self.view.popupmsg("Error de gestió dels fitxers.")

    def data_n_elements(self, num):
        """
        Calls the View function to update the element's label counter with a new value.

        Parameters
        ----------
        num : int
           number of images found in the storage directory
        """

        print("controller - data_n_elements")
        self.view.update_data_n_elements(num)

    def load_image_i(self, img_tk):
        """
        Calls the View function to load the image_tk to the p2_label_img.

        Parameters
        ----------
        img_tk : PIL Image
           image ready to be loaded to a label
        """

        print("controller - load_img_i")
        self.view.load_image_i(img_tk)

    def load_metadata_i(self, metadata):
        """
        Calls the View function to load the metadata to the p2_label_metadata.

        Parameters
        ----------
        metadata : JSON Object
           data sent to be loaded into a label
        """

        print("controller - load_metadata_i")
        self.view.load_metadata_i(metadata)

    def ask_image_i(self, i):
        """
        Calls the Data_manager functions to read and load the image and metadata "i".

        Parameters
        ----------
        i : int
           id of the image and metadata that have to be read
        """

        print("controller - ask_img_i")
        self.data_manager.load_img_i(i)
        self.data_manager.load_metadata_i(i)
Beispiel #8
0
def main():
    # set up our command line arguments
    parser = argparse.ArgumentParser(
        description=
        'Set arguments for training of pytorch image recognotion model')
    parser.add_argument('data_directory',
                        type=str,
                        help='select directory for images to train on')
    parser.add_argument('--save_dir',
                        type=str,
                        default='model_checkpoint.pth',
                        help='select directory for saving of model')
    parser.add_argument('--arch',
                        type=str,
                        default='densenet121',
                        help='select architecture')
    parser.add_argument('--learning_rate',
                        type=float,
                        default='.003',
                        help='select learning rate')
    parser.add_argument(
        '--hidden_units',
        type=int,
        default='256',
        help='select number of hidden units for each of 3 layers')
    parser.add_argument('--epochs',
                        type=int,
                        default=5,
                        help='set number of epochs for training')
    parser.add_argument('--dropout',
                        type=float,
                        default=.2,
                        help='change of dropout')
    parser.add_argument(
        '--print_every',
        type=int,
        default=10,
        help='How often should the model print out training logs')
    parser.add_argument(
        '--output_size',
        type=int,
        default=102,
        help=
        'How many categories should it predict, should be equal to training data'
    )
    parser.add_argument('--gpu',
                        type=bool,
                        default=False,
                        help='Should pytorch use a gpu for better performance')

    # gets our arguments from the command line
    in_arg = parser.parse_args()

    # instantiates the data_manager class
    data_manager = Data_manager()

    # gets our dataloaders from the data_manager class, using our arg input as the directory
    dataloaders = data_manager.get_dataloaders(in_arg.data_directory)

    # creates a Model class
    model = Model()

    # creates a new model to train on
    model.create_model(in_arg.arch, in_arg.hidden_units, in_arg.output_size,
                       in_arg.dropout)

    # sends our model to the gpu if requested
    model.use_gpu(in_arg.gpu)

    # trains our model
    model.train(in_arg.epochs,
                dataloaders,
                learning_rate=in_arg.learning_rate,
                print_every_step=in_arg.print_every)

    # tests our trained models accuracy
    model.validate(dataloaders)

    # saves our trained model for use later
    model.save_model(data_manager.data_datasets['train'].class_to_idx,
                     in_arg.save_dir)