# Start Training Function Changelog

## [2024-04-09] - Simplified Training Approach

### Changed
- Simplified training process to use a single consistent approach
- Now always using console datasets with AutoCreate=True for test dataset creation
- Removed complex conditional logic and multiple retry approaches
- Clearer error messages for common configuration issues

### Technical Notes
- Function no longer attempts to use manifest files directly for training
- Removed dataset detection and fallback mechanisms
- This approach requires initial setup in the AWS Console:
  1. Create a project in AWS Rekognition Custom Labels console
  2. Upload initial training images and label them
  3. The Lambda function will automatically handle test dataset creation
- For projects with existing test datasets, the AutoCreate parameter is ignored by AWS
- This provides a more predictable and reliable training process

## [2024-04-09] - Improved Test Dataset Detection

### Changed
- Enhanced test dataset handling for console-based projects:
  - Added detection for existing test datasets in the console
  - Only uses AutoCreate=True when no test dataset exists
  - Automatically retries with AutoCreate=True if initial attempt fails
  - Provides clearer error messages when test dataset handling fails

### Technical Notes
- Uses describe_project API to check for existing test datasets
- Logs whether a dedicated test dataset was found
- Added two-step retry approach for handling test datasets:
  1. First attempt uses existing test dataset configuration
  2. If that fails, retry with AutoCreate=True
- This ensures compatibility with both manual test datasets and auto-split approaches

## [2024-04-09] - Fixed Test Dataset Handling

### Fixed
- Resolved error when no test dataset exists in console projects
- Added proper error message with instructions for creating test datasets
- Improved error handling for test dataset scenarios
- Included TestingData with AutoCreate parameter in all training attempts

### Technical Notes
- The function now explicitly sets TestingData.AutoCreate=True when using console datasets
- Added specific error detection for "test dataset doesn't exist" errors
- Updated error messages to provide clear instructions for users
- Preserves AutoCreate setting during fallback retry attempts

## [2024-04-09] - Added Support for Console Datasets

### Fixed
- Resolved error when trying to train projects that already have console datasets
- Added intelligent detection of whether a project has console datasets
- Implemented automatic fallback to using console datasets when manifest file approach fails
- Added detailed logging to track which training approach is being used

### Technical Notes
- The function now checks `describe_project` to detect if the project has console datasets
- If datasets are detected, it doesn't specify `TrainingData` or `TestingData` parameters
- If an "already has associated datasets" error occurs, it retries without the manifest parameters
- This makes the function compatible with both external manifest files and console datasets

## [2024-04-08] - Project ARN Format Fix

### Changed
- Now using SSM Parameters instead of environment variables for sensitive credentials
- Added capability to read configuration from both environment variables and SSM parameters
- Enhanced security by storing Supabase credentials in SSM Parameter Store
- Modified initialization process to securely fetch credentials at runtime

### Technical Notes
- Function now checks SSM for parameters at:
  - `/datafy-rekognition-stack/supabase-url`
  - `/datafy-rekognition-stack/supabase-key`
  - `/datafy-rekognition-stack/training-bucket` (fallback)
  - `/datafy-rekognition-stack/project-name` (fallback)
- Parameters can be encrypted using AWS KMS (SecureString type)
- Supabase integration gracefully degrades if credentials are not available

## [2024-04-08] - Manual Execution Process

### Description
- The `start_training` function is designed to be run manually, not triggered by S3 events
- This function initiates the training process for a Rekognition Custom Labels model
- It validates the manifest file, starts training, and monitors the process

### Configuration
- Function requires the following environment variables:
  - `TRAINING_BUCKET`: S3 bucket containing the training data
  - `PROJECT_ARN`: ARN of the Rekognition Custom Labels project
  - `SUPABASE_URL`: URL for Supabase integration (optional)
  - `SUPABASE_KEY`: API key for Supabase integration (optional)

### Process Flow
1. Validates the manifest file in the training bucket
2. Generates a version name based on current timestamp
3. Starts the Rekognition training job
4. Stores metadata in Supabase (if configured)
5. Polls for training completion
6. Evaluates the model when training completes
7. Updates metadata with final status and evaluation results

### Invocation
- Function should be invoked manually using AWS CLI, SDK, or Console
- Not triggered automatically by S3 events (unlike the `create_manifest` function)
- Example AWS CLI invocation:
  ```bash
  aws lambda invoke --function-name Rekognition-StartTrainingFunction response.json
  ```

### Notes
- Training can take several hours to complete
- Function has a default timeout of 15 minutes (900 seconds)
- Polling process continues up to the function timeout
- For longer training sessions, consider implementing a Step Function

## [2024-04-07] - Metadata Storage Enhancement

### Added
- Integration with Supabase for storing training metadata
- Added `store_training_metadata` function to record:
  - Version name and training timestamp
  - Project version ARN
  - Training status and manifest file location
  - Evaluation metrics when available

### Changed
- Enhanced training process to include proper status polling
- Added model evaluation with F1 score assessment
- Improved error handling and logging

### Technical Notes
- Supabase integration is optional
- Function will skip metadata storage if Supabase credentials are not provided
- Default evaluation threshold is F1 score >= 0.85 