Code Documentation¶
htr2hpc¶
API Client¶
- class htr2hpc.api_client.OCRModel(pk: int, name: str, file: str, file_size: int, job: str, owner: str, training: bool, versions: list, documents: list[int], accuracy_percent: float, training_accuracy: float, rights: str, can_share: bool, parent: str | None = None)[source]¶
-
whether model is sharable
- class htr2hpc.api_client.ResultsList(api: eScriptoriumAPIClient, result_type: str, count: int, next: str, previous: str, results: list)[source]¶
API list response.
- class htr2hpc.api_client.Task(pk: int, document: int, document_part: int, workflow_state: int, label: str, messages: str, queued_at: datetime, started_at: datetime, done_at: datetime, method: str, user: int)[source]¶
API response for a task result.
- class htr2hpc.api_client.Workflow(convert: str | None = None, segment: str | None = None, transcribe: str | None = None)[source]¶
Tasks¶
Views¶
Users¶
- htr2hpc.users.init_user(user: AbstractUser, user_info: Any) None[source]¶
pucas EXTRA_USER_INIT hook: make new CAS accounts inactive by default.
Existing accounts are left unchanged to preserve any intentional changes (e.g. is_active set by an admin). Admin and staff accounts created via the createcasuser command are activated by that command after this hook runs.
Context Processors¶
- htr2hpc.context_processors.CPU_COUNT = 2¶
number of logical CPUs in the system
- htr2hpc.context_processors.HOSTNAME = 'build-33801908-project-1186947-htr2hpc'¶
hostname for the local VM
- htr2hpc.context_processors.htr2hpc_version(request)[source]¶
Custom context processor to expose the htr2hpc package version.
Management Commands¶
- class htr2hpc.management.commands.cleanup_exports.Command(stdout=None, stderr=None, no_color=False, force_color=False)[source]¶
Training¶
- class htr2hpc.train.run.TrainingManager(base_url: str, api_token: str, work_dir: pathlib.Path, document_id: int, training_mode: str, model_name: str, num_workers: int, parts: intspan.core.intspan | None = None, model_id: int | None = None, task_report_id: int | None = None, update: htr2hpc.train.run.UpdateMode = <UpdateMode.NEVER: 0>, transcription_id: int | None = None, existing_data: bool = False, show_progress: bool = True, model_file: pathlib.Path = None, training_data_counts: dict | None = None, slurm_output: str = '', job_stats: str = '')[source]¶
- class htr2hpc.train.data.TrainingDataCounts(parts: int = 0, lines: int = 0, regions: int = 0)[source]¶
- htr2hpc.train.data.compile_data(segmentations, output_dir)[source]¶
Compile a list of kraken segmentation objects into a binary file for recognition training.
- htr2hpc.train.data.get_best_model(model_dir: Path, original_model: Path | None = None) Path | None[source]¶
Find the best model in the specified model_dir directory. By default, looks for a file named *_best.mlmodel. If no best model is found by filename, looks for best model based on accuracy score in kraken metadata. When original_model is specified, accuracy must be better than the original to be considered ‘best’.
- htr2hpc.train.data.get_model_file(api, model_id, training_type, output_dir)[source]¶
Download a model file from the eScriptorium and save it to the specified directory. Raises a ValueError if the model is not the specified training type. Returns a
pathlib.Pathto the downloaded file.
- htr2hpc.train.data.get_prelim_model(input_model: Path)[source]¶
Copies the input model to a file with suffix _prelim.mlmodel, then returns the path to that newly created file.
- htr2hpc.train.data.get_segmentation_data(api, document_details, part_id, image_dir, transcription_id=None) tuple[Segmentation, tuple][source]¶
Get a single document part from the eScriptorium API and generate a kraken segmentation object.
Returns a tuple of the segmentation object and the part details from the API, which includes image size needed for serialization. Includes transcription text when a transcription_id is specified.
- htr2hpc.train.data.serialize_segmentation(segmentation: Segmentation, part)[source]¶
Serialize a segmentation object as ALTO XML for use as training data. Requires kraken
Segmentationand part details returned by eScriptorum API.
- htr2hpc.train.data.split_segmentation(training_data_dir)[source]¶
Takes as input directory containing ALTO XML files and creates a train.txt and validate.txt file which define the train/validation split. This allows consistency across the multiple train tasks.
- htr2hpc.train.data.upload_best_model(api, model_dir: Path, model_type: str, model_id: int | None = None, original_model: Path | None = None) Path | None[source]¶
Upload the best model in the specified model directory to eScriptorium with the specified job type (Segment/Recognize). If a model id is specified, updates that model; otherwise creates a new model. Returns
pathlib.Pathobject for best model if found and successfully uploaded; otherwise returns None.
- htr2hpc.train.data.upload_models(api, model_dir: Path, model_type: str, show_progress=True) int[source]¶
Upload all model files in the specified model directory to eScriptorum with the specified job type (Segment/Recognize). Returns a count of the number of models created.
- htr2hpc.train.slurm.recognition_train(input_data_dir: Path, output_model: Path, input_model: Path | None = None, num_workers: int = 8, mem_per_cpu: str = '2G', training_time: timedelta = datetime.timedelta(seconds=900), epochs: int | None = None) int[source]¶
Run ketos recognition training as a slurm job. Returns the slurm job id for the queued job.
- htr2hpc.train.slurm.segtrain(input_data_dir: Path, output_model: Path, input_model: Path, num_workers: int = 8, mem_per_cpu: str = '4G', training_time: timedelta = datetime.timedelta(seconds=900), epochs: int | None = None) int[source]¶
Run ketos segmentation training as a slurm job. Returns the slurm job id for the queued job.
- htr2hpc.train.slurm.slurm_job_queue_status(job_id: int) str[source]¶
Use squeue to get the full-word status (i.e., PENDING or RUNNING) for a queued slurm job.
- htr2hpc.train.slurm.slurm_job_stats(job_id: int) str[source]¶
Use jobstats to get Slurm Job Statistics, to track resource usage
- htr2hpc.train.slurm.slurm_job_status(job_id: int) set[source]¶
Use sacct to get the status of a slurm job that is no longer queued. Returns a set of unique full-word statuses, reporting across all tasks for the job.
- htr2hpc.train.calculate.calc_cpu_mem(job_stats)[source]¶
Given a preliminary job_stats output, return recommended mem per cpu.
- htr2hpc.train.calculate.calc_full_duration(slurm_output, job_stats)[source]¶
Given a preliminary slurm job output, return duration estimate: Setup time, plus N times the average epoch plus 10% for wiggle room. Assumes the train task will take 50 epochs. N = 50 - count of completed epochs from prelim train task.
- htr2hpc.train.calculate.estimate_cpu_mem(training_data_size, training_mode)[source]¶
Use files in input data dir to come up with estimate of prelim mem per cpu.
- htr2hpc.train.calculate.estimate_duration(training_data_size, training_mode)[source]¶
Use files in input data dir to come up with estimate of prelim train duration.
- htr2hpc.train.calculate.slurm_get_avg_epoch(slurm_output)[source]¶
Return average epoch duration, in seconds
- htr2hpc.train.calculate.slurm_get_max_acc(slurm_output, training_mode)[source]¶
Return a tuple of (epoch #, accuracy) for the epoch with highest accuracy
Utility script to delete models from eScriptorim API, for easily cleaning up models created and uploaded for testing.
Requires an eScriptorium API token set as an environment variable in ESCRIPTORIUM_API_TOKEN.
Takes a base url for the eScriptorium instance and a model name prefix; will delete all models that start with the specified model name prefix.
usage:
python src/htr2hpc/train/rm_models.py https://test-htr.lib.princeton.edu/ model_prefix