llinguini's picture
Upload folder using huggingface_hub
d84a12f verified
import numpy as np
import json
import torch
# Load stats file
stats_file = "../meta/stats.json"
with open(stats_file, 'r') as f:
stats = json.load(f)
print("Keys in stats file:", list(stats.keys()))
# Check each field more thoroughly by simulating the torch conversion
problem_found = False
for key in stats:
print(f"\nExamining key: {key}")
for stat_type in ["mean", "std"]:
if stat_type in stats[key]:
value = stats[key][stat_type]
print(f" - {stat_type} type: {type(value)}")
try:
# Try converting to numpy array (this is what the code does)
numpy_val = np.array(value)
print(f" NumPy array shape: {numpy_val.shape}, dtype: {numpy_val.dtype}")
# Test if this can be converted to torch tensor
try:
torch_val = torch.from_numpy(numpy_val).to(dtype=torch.float32)
print(f" ✓ Successfully converted to torch tensor")
except Exception as e:
print(f" ✗ ERROR converting to torch tensor: {type(e).__name__}: {e}")
print(f" Value: {numpy_val}")
problem_found = True
except Exception as e:
print(f" ✗ ERROR converting to NumPy array: {type(e).__name__}: {e}")
print(f" Value: {value}")
problem_found = True
if not problem_found:
print("\nNo problems found in the stats file. The error might be happening in another part of the code.")
print("Try fixing the stats file with a preprocessing step:")
# Create a fixed stats file
fixed_stats = {}
for key in stats:
fixed_stats[key] = {}
for field, value in stats[key].items():
if field in ["mean", "std"]:
# Try to safely convert to float array
try:
# First convert to numpy array
arr = np.array(value)
# If object type, replace with safe values
if arr.dtype == np.dtype('O'):
arr = np.ones_like(arr, dtype=np.float32)
else:
# Make sure it's float32
arr = arr.astype(np.float32)
# Convert back to list for JSON
fixed_stats[key][field] = arr.tolist()
except Exception as e:
print(f"Fixing {key}.{field}: {e}")
# Just use a safe default
fixed_stats[key][field] = [1.0]
else:
# Keep other fields unchanged
fixed_stats[key][field] = value
# Save the fixed stats
with open("../meta/stats_fixed.json", 'w') as f:
json.dump(fixed_stats, f, indent=2)
print("Created fixed stats file: ../meta/stats_fixed.json")
print("You can replace the original stats.json with this file.")