示例#1
0
def print_image(image_file, title, text, show_image=False, save_graph=False):
    img = load_img(image_file)
    format = img.format
    mode = img.mode
    (width, height) = img.size

    text = '%s - %s - %sx%s\n%s' % (format, mode, width, height, text)

    plt.figure(figsize=(10, 6))
    plt.imshow(img)
    plt.title(title)
    plt.text(
        s=text,
        fontsize=8,
        x=width - round(width * 0.025),
        y=round(width * 0.025),
        color='green',
        bbox=dict(facecolor='white', alpha=0.6, edgecolor='grey', boxstyle='round,pad=0.5'),
        horizontalalignment='right',
        verticalalignment='top'
    )

    # save graph
    if save_graph:
        graph_file = add_file_extension(add_file_extension(image_file, 'graph', True), 'png')
        plt.savefig(graph_file)

    # show graph
    if show_image:
        plt.show()
示例#2
0
    def rebuild_model_dict(self, config_file=None):
        model_file = self.get_data('model_file')

        # convert model file if needed (from other training system)
        if not os.path.isfile(model_file) and config_file is not None and os.path.isfile(config_file):
            self.set_data('config_file', config_file)
            model_file_tmp = add_file_extension(config_file, 'h5')
            if os.path.isfile(model_file_tmp):
                model_file = model_file_tmp
                self.set_data('config_file', config_file)

        if not os.path.isfile(model_file):
            raise AssertionError('model file "%s" was not found' % model_file)

        model_path = os.path.dirname(model_file)
        model_base = add_file_extension(os.path.basename(model_file), '*', True)
        model_files = []
        model_file_best = None
        model_file_best_val_acc = 0

        # search for all best models
        os.chdir(model_path)
        for file in glob.glob(model_base):
            output = re.search('\.([0-9]{1,})-([0-9]{1,}\.[0-9]{1,})\.', file, flags=re.IGNORECASE)
            if output is not None:
                epoch = int(output.group(1))
                val_acc = float(output.group(2))

                # build current model dict
                model_file_current = {
                    'val_acc': val_acc,
                    'epoch': epoch,
                    'model_file': '%s/%s' % (model_path, file)
                }

                # remember the best model
                if val_acc > model_file_best_val_acc:
                    model_file_best_val_acc = val_acc
                    model_file_best = model_file_current

                model_files.append(model_file_current)

        # set new model dict
        self.set_data('model_files', model_files)
        self.set_data('model_file_best', model_file_best)
    def POST_prediction_hook(self, argument, upload_data, models):
        # some configs
        show_image = False
        save_image = True

        # get model
        model = models[argument]['model']
        model_json_config = models[argument]['json_config']

        # load json
        self.config.load_json(model_json_config, True)

        # get file to evaluate
        evaluation_file = upload_data['upload_path']
        graph_file = add_file_extension(
            add_file_extension(evaluation_file, 'graph', True), PNG_EXTENSION)
        evaluation_file_web = upload_data['upload_path_web']
        graph_file_web = add_file_extension(
            add_file_extension(evaluation_file_web, 'graph', True),
            PNG_EXTENSION)

        self.start_timer('prediction')
        evaluation_data = self.evaluate_file(model, evaluation_file,
                                             show_image, save_image)
        self.finish_timer('prediction')

        prediction_overview_array = evaluation_data[
            'prediction_overview_array']
        prediction_class = evaluation_data['prediction_class']
        prediction_accuracy = evaluation_data['prediction_accuracy']

        return_value = {
            'evaluated_file': evaluation_file,
            'graph_file': graph_file,
            'evaluated_file_web': evaluation_file_web,
            'graph_file_web': graph_file_web,
            'prediction_overview_array': prediction_overview_array,
            'prediction_class': prediction_class,
            'prediction_accuracy': prediction_accuracy,
            'prediction_time': '%.4f' % self.get_timer('prediction')
        }

        return return_value
示例#4
0
    def build_data(self):
        add_transfer_learning_name = self.configs['data']['add_transfer_learning_name'] if 'add_transfer_learning_name' in self.configs['data'] else None

        if 'transfer_learning' in self.configs and add_transfer_learning_name:
            transfer_learning_model = self.gettl('transfer_learning_model').lower()
        else:
            transfer_learning_model = None

        data_files = [
            'model_file',
            'config_file',
            'best_model_file',
            'accuracy_file',
            'log_file',
            'csv_file'
        ]

        extension_wrapper = {
            'config_file': 'json',
            'model_file': 'h5',
            'best_model_file': 'best.{epoch:02d}-{val_accuracy:.2f}.h5',
            'accuracy_file': 'png',
            'log_file': 'log',
            'csv_file': 'csv'
        }

        for data_file_main in data_files:
            if data_file_main in self.configs['data']:
                if transfer_learning_model is not None:
                    self.set_data(
                        data_files[0],
                        add_file_extension(self.get_data(data_files[0]), transfer_learning_model, True)
                    )
                data_files.remove(data_file_main)

                for data_file in data_files:
                    self.set_data(
                        data_file,
                        add_file_extension(self.get_data(data_file_main), extension_wrapper[data_file])
                    )

            return None