Initial commit
This commit is contained in:
7
skills/transfer-learning-adapter/assets/README.md
Normal file
7
skills/transfer-learning-adapter/assets/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Assets
|
||||
|
||||
Bundled resources for transfer-learning-adapter skill
|
||||
|
||||
- [ ] example_config.json: Provides an example configuration file for adapting a pre-trained model to a new dataset.
|
||||
- [ ] model_architecture.png: A diagram illustrating the architecture of a commonly used pre-trained model.
|
||||
- [ ] data_preprocessing_example.py: A code snippet demonstrating how to preprocess the new dataset to be compatible with the pre-trained model.
|
||||
@@ -0,0 +1,158 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
This script demonstrates how to preprocess a new dataset for transfer learning.
|
||||
It focuses on ensuring compatibility with a pre-trained model, including
|
||||
handling image resizing, normalization, and label encoding.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
|
||||
def load_and_preprocess_images(image_dir, target_size=(224, 224), grayscale=False):
|
||||
"""
|
||||
Loads images from a directory, resizes them, and optionally converts them to grayscale.
|
||||
|
||||
Args:
|
||||
image_dir (str): Path to the directory containing the images.
|
||||
target_size (tuple): The desired size (width, height) of the images.
|
||||
grayscale (bool): Whether to convert images to grayscale.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a list of preprocessed image arrays and a list of corresponding filenames.
|
||||
Returns None, None if an error occurs.
|
||||
"""
|
||||
|
||||
images = []
|
||||
filenames = []
|
||||
try:
|
||||
for filename in os.listdir(image_dir):
|
||||
if filename.endswith(('.jpg', '.jpeg', '.png')):
|
||||
image_path = os.path.join(image_dir, filename)
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if grayscale:
|
||||
img = img.convert('L') # Convert to grayscale
|
||||
img = img.resize(target_size)
|
||||
img_array = np.array(img)
|
||||
|
||||
# Ensure images are 3-channel even if grayscale
|
||||
if grayscale and len(img_array.shape) == 2:
|
||||
img_array = np.stack([img_array] * 3, axis=-1)
|
||||
elif len(img_array.shape) == 2:
|
||||
img_array = np.stack([img_array] * 3, axis=-1)
|
||||
|
||||
|
||||
images.append(img_array)
|
||||
filenames.append(filename)
|
||||
except (IOError, OSError) as e:
|
||||
print(f"Error processing image {filename}: {e}")
|
||||
return images, filenames
|
||||
except OSError as e:
|
||||
print(f"Error accessing image directory {image_dir}: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
def normalize_images(images):
|
||||
"""
|
||||
Normalizes pixel values of images to the range [0, 1].
|
||||
|
||||
Args:
|
||||
images (list): A list of image arrays.
|
||||
|
||||
Returns:
|
||||
list: A list of normalized image arrays.
|
||||
"""
|
||||
normalized_images = [img / 255.0 for img in images]
|
||||
return normalized_images
|
||||
|
||||
|
||||
def encode_labels(labels):
|
||||
"""
|
||||
Encodes categorical labels into numerical values using LabelEncoder.
|
||||
|
||||
Args:
|
||||
labels (list): A list of categorical labels.
|
||||
|
||||
Returns:
|
||||
numpy.ndarray: An array of encoded labels.
|
||||
"""
|
||||
label_encoder = LabelEncoder()
|
||||
encoded_labels = label_encoder.fit_transform(labels)
|
||||
return encoded_labels
|
||||
|
||||
|
||||
def create_dataframe(images, labels, filenames):
|
||||
"""
|
||||
Creates a pandas DataFrame from the preprocessed images, labels, and filenames.
|
||||
|
||||
Args:
|
||||
images (list): A list of preprocessed image arrays.
|
||||
labels (numpy.ndarray): An array of encoded labels.
|
||||
filenames (list): A list of filenames.
|
||||
|
||||
Returns:
|
||||
pandas.DataFrame: A DataFrame containing the image data, labels, and filenames.
|
||||
"""
|
||||
df = pd.DataFrame({'image': images, 'label': labels, 'filename': filenames})
|
||||
return df
|
||||
|
||||
|
||||
def split_data(df, test_size=0.2, random_state=42):
|
||||
"""
|
||||
Splits the data into training and testing sets.
|
||||
|
||||
Args:
|
||||
df (pandas.DataFrame): The DataFrame containing the data.
|
||||
test_size (float): The proportion of the data to use for testing.
|
||||
random_state (int): The random state for reproducibility.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the training and testing DataFrames.
|
||||
"""
|
||||
train_df, test_df = train_test_split(df, test_size=test_size, random_state=random_state)
|
||||
return train_df, test_df
|
||||
|
||||
|
||||
def main(image_dir):
|
||||
"""
|
||||
Main function to demonstrate the data preprocessing steps.
|
||||
|
||||
Args:
|
||||
image_dir (str): Path to the directory containing the images.
|
||||
"""
|
||||
images, filenames = load_and_preprocess_images(image_dir)
|
||||
|
||||
if images is None or filenames is None:
|
||||
print("Error loading images. Exiting.")
|
||||
return
|
||||
|
||||
# Example labels (replace with your actual labels)
|
||||
labels = [filename.split('_')[0] for filename in filenames] # Assuming filename format: label_image_id.jpg
|
||||
encoded_labels = encode_labels(labels)
|
||||
|
||||
normalized_images = normalize_images(images)
|
||||
|
||||
df = create_dataframe(normalized_images, encoded_labels, filenames)
|
||||
|
||||
train_df, test_df = split_data(df)
|
||||
|
||||
print("Training DataFrame shape:", train_df.shape)
|
||||
print("Testing DataFrame shape:", test_df.shape)
|
||||
print("First 5 rows of training DataFrame:")
|
||||
print(train_df.head())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1:
|
||||
image_directory = sys.argv[1]
|
||||
main(image_directory)
|
||||
else:
|
||||
print("Please provide the image directory as a command-line argument.")
|
||||
print("Example: python data_preprocessing_example.py path/to/images")
|
||||
55
skills/transfer-learning-adapter/assets/example_config.json
Normal file
55
skills/transfer-learning-adapter/assets/example_config.json
Normal file
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"_comment": "Example configuration for transfer learning adaptation.",
|
||||
"model_name": "bert-base-uncased",
|
||||
"_comment": "Pre-trained model to adapt. Choose from Hugging Face model hub.",
|
||||
"dataset_name": "glue",
|
||||
"_comment": "Dataset to fine-tune on. Choose from Hugging Face datasets or specify a local path.",
|
||||
"dataset_subset": "mrpc",
|
||||
"_comment": "Specific subset of the dataset to use (if applicable).",
|
||||
"train_file": null,
|
||||
"_comment": "Optional path to a custom training data file. Overrides dataset_name and dataset_subset if provided.",
|
||||
"validation_file": null,
|
||||
"_comment": "Optional path to a custom validation data file. Overrides dataset_name and dataset_subset if provided.",
|
||||
"output_dir": "./adapted_model",
|
||||
"_comment": "Directory to save the adapted model and training logs.",
|
||||
"num_epochs": 3,
|
||||
"_comment": "Number of training epochs.",
|
||||
"learning_rate": 2e-5,
|
||||
"_comment": "Learning rate for the AdamW optimizer.",
|
||||
"batch_size": 32,
|
||||
"_comment": "Batch size for training and evaluation.",
|
||||
"weight_decay": 0.01,
|
||||
"_comment": "Weight decay for regularization.",
|
||||
"seed": 42,
|
||||
"_comment": "Random seed for reproducibility.",
|
||||
"max_length": 128,
|
||||
"_comment": "Maximum sequence length for input tokens. Truncate or pad sequences as needed.",
|
||||
"task_name": "text_classification",
|
||||
"_comment": "Type of task for which the model is being adapted. Options: text_classification, token_classification, question_answering, sequence_to_sequence.",
|
||||
"metric": "accuracy",
|
||||
"_comment": "Primary metric to evaluate performance. Options depend on the task. Common examples: accuracy, f1, rouge, bleu.",
|
||||
"gradient_accumulation_steps": 1,
|
||||
"_comment": "Number of steps to accumulate gradients before performing a backward/update pass.",
|
||||
"fp16": true,
|
||||
"_comment": "Whether to use 16-bit floating point precision (Mixed Precision Training).",
|
||||
"evaluation_strategy": "epoch",
|
||||
"_comment": "Evaluation strategy to adopt during training. Options: steps, epoch",
|
||||
"save_strategy": "epoch",
|
||||
"_comment": "Save strategy to adopt during training. Options: steps, epoch",
|
||||
"logging_steps": 100,
|
||||
"_comment": "Log every X updates steps.",
|
||||
"push_to_hub": false,
|
||||
"_comment": "Whether to push the adapted model to the Hugging Face Hub.",
|
||||
"hub_model_id": null,
|
||||
"_comment": "The name of the repository to keep in sync with the local adapted model. It can be a path to an existing repository on the Hub or a new one. Overrides the repository id in the Trainer's config.",
|
||||
"hub_token": null,
|
||||
"_comment": "The token to use when pushing the adapted model to the Hub.",
|
||||
"device": "cuda",
|
||||
"_comment": "Device (cpu, cuda) on which the code should be run.",
|
||||
"tokenizer_name": null,
|
||||
"_comment": "Optional tokenizer name to use. If not provided, the tokenizer associated with the model_name will be used.",
|
||||
"do_train": true,
|
||||
"_comment": "Whether to perform training.",
|
||||
"do_eval": true,
|
||||
"_comment": "Whether to perform evaluation."
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
(Binary file content for model_architecture.png - A placeholder image representing a typical deep learning model architecture suitable for transfer learning. This could be a simplified ResNet, VGG, or similar. The image should visually depict layers, connections, and the concept of freezing layers for transfer learning.)
|
||||
|
||||
(Image data would go here. A real PNG file would contain binary data defining the image.)
|
||||
|
||||
<!--
|
||||
Instructions for replacing this placeholder:
|
||||
|
||||
1. This file is a placeholder for a visual representation of a common deep learning model architecture.
|
||||
2. Use a tool like draw.io, Lucidchart, or similar to create a diagram.
|
||||
3. The diagram should clearly show:
|
||||
* Input layer
|
||||
* Multiple convolutional layers (or other relevant layer types)
|
||||
* Pooling layers (if applicable)
|
||||
* Fully connected layers (if applicable)
|
||||
* Output layer
|
||||
4. Highlight the layers that are typically frozen during transfer learning (e.g., the earlier convolutional layers). Use color or shading to differentiate these layers.
|
||||
5. Label the layers clearly.
|
||||
6. Save the diagram as a PNG file.
|
||||
7. Replace the placeholder binary data in this file with the actual PNG data. You can do this by opening the PNG file in a binary editor and copying the data, or by using a scripting language to read and write the binary data.
|
||||
|
||||
Example Architecture Considerations:
|
||||
|
||||
* ResNet: Shows residual connections and the concept of blocks.
|
||||
* VGG: Shows a deep stack of convolutional layers.
|
||||
* MobileNet: Focuses on efficient architectures.
|
||||
|
||||
The goal is to provide a visual aid to users understanding how transfer learning can be applied.
|
||||
-->
|
||||
Reference in New Issue
Block a user