-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for freezing pretrained vision model layers with regex (#3981)
- Loading branch information
1 parent
423a82a
commit 830c3f0
Showing
9 changed files
with
416 additions
and
5 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
examples/regex_freezing/ecd_freezing_with_regex_training.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import logging | ||
import os | ||
import shutil | ||
|
||
import pandas as pd | ||
import yaml | ||
from datasets import load_dataset | ||
|
||
from ludwig.api import LudwigModel | ||
|
||
""" | ||
To inspect model layers in the terminal, type: "ludwig collect_summary -pm resnet18" | ||
For some models, a HuggingFace Token will be necessary. | ||
Once you obtain one, use "export HUGGING_FACE_HUB_TOKEN="<api_token>"" in the terminal. | ||
""" | ||
|
||
dataset = load_dataset("beans") | ||
train_df = pd.DataFrame( | ||
{"image_path": [f"train_{i}.jpg" for i in range(len(dataset["train"]))], "label": dataset["train"]["labels"]} | ||
) | ||
test_df = pd.DataFrame( | ||
{"image_path": [f"test_{i}.jpg" for i in range(len(dataset["test"]))], "label": dataset["test"]["labels"]} | ||
) | ||
|
||
os.makedirs("train_images", exist_ok=True) | ||
os.makedirs("test_images", exist_ok=True) | ||
|
||
for i, img in enumerate(dataset["train"]["image"]): | ||
img.save(f"train_images/train_{i}.jpg") | ||
for i, img in enumerate(dataset["test"]["image"]): | ||
img.save(f"test_images/test_{i}.jpg") | ||
|
||
train_df["image_path"] = train_df["image_path"].apply(lambda x: os.path.join("train_images", x)) | ||
test_df["image_path"] = test_df["image_path"].apply(lambda x: os.path.join("test_images", x)) | ||
|
||
train_df.to_csv("beans_train.csv", index=False) | ||
test_df.to_csv("beans_test.csv", index=False) | ||
|
||
|
||
config = yaml.safe_load( | ||
r""" | ||
input_features: | ||
- name: image_path | ||
type: image | ||
encoder: | ||
type: resnet | ||
use_pretrained: true | ||
trainable: true | ||
output_features: | ||
- name: label | ||
type: category | ||
trainer: | ||
epochs: 1 | ||
batch_size: 5 | ||
layers_to_freeze_regex: '(layer1\.0\.*|layer2\.0\.*)' | ||
""" | ||
) | ||
|
||
model = LudwigModel(config, logging_level=logging.INFO) | ||
train_stats = model.train(dataset="beans_train.csv", skip_save_model=True) | ||
eval_stats, predictions, output_directory = model.evaluate(dataset="beans_test.csv") | ||
|
||
print("Training Statistics: ", train_stats) | ||
print("Evaluation Statistics: ", eval_stats) | ||
|
||
shutil.rmtree("train_images") | ||
shutil.rmtree("test_images") | ||
os.remove("beans_train.csv") | ||
os.remove("beans_test.csv") |
64 changes: 64 additions & 0 deletions
64
examples/regex_freezing/llm_freezing_with_regex_training.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
|
||
import yaml | ||
|
||
from ludwig.api import LudwigModel | ||
|
||
""" | ||
To inspect model layers in the terminal, type: "ludwig collect_summary -pm resnet18" | ||
For some models, a HuggingFace Token will be necessary. | ||
Once you obtain one, use "export HUGGING_FACE_HUB_TOKEN="<api_token>"" in the terminal. | ||
""" | ||
|
||
config_str = yaml.safe_load( | ||
r""" | ||
model_type: llm | ||
base_model: facebook/opt-350m | ||
adapter: | ||
type: lora | ||
prompt: | ||
template: | | ||
### Instruction: | ||
Generate a concise summary of the following text, capturing the main points and conclusions. | ||
### Input: | ||
{input} | ||
### Response: | ||
input_features: | ||
- name: prompt | ||
type: text | ||
preprocessing: | ||
max_sequence_length: 256 | ||
output_features: | ||
- name: output | ||
type: text | ||
preprocessing: | ||
max_sequence_length: 256 | ||
trainer: | ||
type: finetune | ||
layers_to_freeze_regex: (decoder\.layers\.22\.final_layer_norm\.*) | ||
learning_rate: 0.0001 | ||
batch_size: 5 | ||
gradient_accumulation_steps: 16 | ||
epochs: 1 | ||
learning_rate_scheduler: | ||
warmup_fraction: 0.01 | ||
preprocessing: | ||
sample_ratio: 0.1 | ||
generation: | ||
pad_token_id : 0 | ||
""" | ||
) | ||
|
||
model = LudwigModel(config=config_str, logging_level=logging.INFO) | ||
results = model.train(dataset="ludwig://alpaca") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.