-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
log loss per image #7278
Merged
Merged
log loss per image #7278
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import logging | ||
import math | ||
import os | ||
import os.path | ||
import random | ||
import shutil | ||
from pathlib import Path | ||
|
@@ -586,6 +587,7 @@ def main(args): | |
# Move unet, vae and text_encoder to device and cast to weight_dtype | ||
# The VAE is in float32 to avoid NaN losses. | ||
unet.to(accelerator.device, dtype=weight_dtype) | ||
|
||
if args.pretrained_vae_model_name_or_path is None: | ||
vae.to(accelerator.device, dtype=torch.float32) | ||
else: | ||
|
@@ -829,6 +831,7 @@ def tokenize_captions(examples, is_train=True): | |
) | ||
|
||
def preprocess_train(examples): | ||
fnames = [os.path.basename(image.filename) for image in examples[image_column]] | ||
images = [image.convert("RGB") for image in examples[image_column]] | ||
# image aug | ||
original_sizes = [] | ||
|
@@ -858,13 +861,14 @@ def preprocess_train(examples): | |
tokens_one, tokens_two = tokenize_captions(examples) | ||
examples["input_ids_one"] = tokens_one | ||
examples["input_ids_two"] = tokens_two | ||
examples["filenames"] = fnames | ||
return examples | ||
|
||
with accelerator.main_process_first(): | ||
if args.max_train_samples is not None: | ||
dataset["train"] = dataset["train"].shuffle(seed=args.seed).select(range(args.max_train_samples)) | ||
# Set the training transforms | ||
train_dataset = dataset["train"].with_transform(preprocess_train) | ||
train_dataset = dataset["train"].with_transform(preprocess_train, output_all_columns=True) | ||
|
||
def collate_fn(examples): | ||
pixel_values = torch.stack([example["pixel_values"] for example in examples]) | ||
|
@@ -879,6 +883,7 @@ def collate_fn(examples): | |
"input_ids_two": input_ids_two, | ||
"original_sizes": original_sizes, | ||
"crop_top_lefts": crop_top_lefts, | ||
"filenames": [example['filenames'] for example in examples] | ||
} | ||
|
||
# DataLoaders creation: | ||
|
@@ -1073,10 +1078,13 @@ def compute_time_ids(original_size, crops_coords_top_left): | |
loss = loss.mean(dim=list(range(1, len(loss.shape)))) * mse_loss_weights | ||
loss = loss.mean() | ||
|
||
for fname in batch['filenames']: | ||
accelerator.log({'loss_for_' + fname: loss}, step=global_step) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we accept a CLI argument for this? That will be nice no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, it's nice to have. |
||
# Gather the losses across all processes for logging (if we use distributed training). | ||
avg_loss = accelerator.gather(loss.repeat(args.train_batch_size)).mean() | ||
train_loss += avg_loss.item() / args.gradient_accumulation_steps | ||
|
||
|
||
# Backpropagate | ||
accelerator.backward(loss) | ||
if accelerator.sync_gradients: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have
os
module imported. I think we can reuse it.