Skip to content

Instantly share code, notes, and snippets.

@takuma104
Created May 29, 2023 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuma104/aaedd400e21b37d1e4297fe483e90695 to your computer and use it in GitHub Desktop.
Save takuma104/aaedd400e21b37d1e4297fe483e90695 to your computer and use it in GitHub Desktop.
import json
import struct
import sys
from pprint import pprint
def parse_safetensors_header(file_path):
# modified from https://huggingface.co/docs/safetensors/metadata_parsing
with open(file_path, 'rb') as f:
header_bytes = f.read(8)
# Interpret the bytes as a little-endian unsigned 64-bit integer
length_of_header = struct.unpack('<Q', header_bytes)[0]
# Read length_of_header bytes starting from the 9th byte
header_bytes = f.read(length_of_header)
# Interpret the bytes as a JSON object
header = json.loads(header_bytes)
return header
if __name__ == '__main__':
file_path = sys.argv[1]
metadata = parse_safetensors_header(file_path)['__metadata__']
pprint(metadata)
@takuma104
Copy link
Author

Example: https://civitai.com/models/13239/light-and-shadow

{'ss_batch_size_per_device': '1',
 'ss_cache_latents': 'True',
 'ss_clip_skip': '2',
 'ss_color_aug': 'False',
 'ss_dataset_dirs': '{"6_JW": {"n_repeats": 6, "img_count": 18}}',
 'ss_enable_bucket': 'True',
 'ss_epoch': '20',
 'ss_flip_aug': 'False',
 'ss_full_fp16': 'False',
 'ss_gradient_accumulation_steps': '1',
 'ss_gradient_checkpointing': 'False',
 'ss_keep_tokens': 'None',
 'ss_learning_rate': '0.0001',
 'ss_lr_scheduler': 'cosine_with_restarts',
 'ss_lr_warmup_steps': '0',
 'ss_max_bucket_reso': '1024',
 'ss_max_token_length': '225',
 'ss_max_train_steps': '2160',
 'ss_min_bucket_reso': '256',
 'ss_mixed_precision': 'fp16',
 'ss_network_alpha': '64.0',
 'ss_network_dim': '128',
 'ss_network_module': 'networks.lora',
 'ss_new_sd_model_hash': 'fbcf965a62d9d82e935d3d17e97522c29f44550aa9e120a6886f19b578521ec5',
 'ss_num_batches_per_epoch': '108',
 'ss_num_epochs': '20',
 'ss_num_reg_images': '0',
 'ss_num_train_images': '108',
 'ss_output_name': 'JW',
 'ss_random_crop': 'False',
 'ss_reg_dataset_dirs': '{}',
 'ss_resolution': '(512, 640)',
 'ss_sd_model_hash': '6030dabe',
 'ss_sd_model_name': 'model.ckpt',
 'ss_seed': '1337',
 'ss_session_id': '2132880007',
 'ss_shuffle_caption': 'True',
 'ss_text_encoder_lr': '1e-05',
 'ss_total_batch_size': '1',
 'ss_training_comment': 'None',
 'ss_training_started_at': '1677387272.5684652',
 'ss_unet_lr': '0.0001',
 'ss_v2': 'False'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment