Spaces:
Runtime error
Runtime error
File size: 7,743 Bytes
062cf9b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | """
Fast Binary Dataset Creator - No Hanging!
Optimized version that skips the slow sample counting
"""
import torch
from torchvision import datasets, transforms
from torch.utils.data import Dataset, DataLoader
import os
import warnings
warnings.filterwarnings("ignore")
# UCF101 class names (in alphabetical order)
UCF101_CLASSES = [
'ApplyEyeMakeup', 'ApplyLipstick', 'Archery', 'BabyCrawling', 'BalanceBeam',
'BandMarching', 'BaseballPitch', 'Basketball', 'BasketballDunk', 'BenchPress',
'Biking', 'Billiards', 'BlowDryHair', 'BlowingCandles', 'BodyWeightSquats',
'Bowling', 'BoxingPunchingBag', 'BoxingSpeedBag', 'BreastStroke', 'BrushingTeeth',
'CleanAndJerk', 'CliffDiving', 'CricketBowling', 'CricketShot', 'CuttingInKitchen',
'Diving', 'Drumming', 'Fencing', 'FieldHockeyPenalty', 'FloorGymnastics',
'FrisbeeCatch', 'FrontCrawl', 'GolfSwing', 'Haircut', 'Hammering',
'HammerThrow', 'HandstandPushups', 'HandstandWalking', 'HeadMassage', 'HighJump',
'HorseRace', 'HorseRiding', 'HulaHoop', 'IceDancing', 'JavelinThrow',
'JugglingBalls', 'JumpingJack', 'JumpRope', 'Kayaking', 'Knitting',
'LongJump', 'Lunges', 'MilitaryParade', 'Mixing', 'MoppingFloor',
'Nunchucks', 'ParallelBars', 'PizzaTossing', 'PlayingCello', 'PlayingDaf',
'PlayingDhol', 'PlayingFlute', 'PlayingGuitar', 'PlayingPiano', 'PlayingSitar',
'PlayingTabla', 'PlayingViolin', 'PoleVault', 'PommelHorse', 'PullUps',
'Punch', 'PushUps', 'Rafting', 'RockClimbingIndoor', 'RopeClimbing',
'Rowing', 'SalsaSpin', 'ShavingBeard', 'Shotput', 'SkateBoarding',
'Skiing', 'Skijet', 'SkyDiving', 'SoccerJuggling', 'SoccerPenalty',
'StillRings', 'SumoWrestling', 'Surfing', 'Swing', 'TableTennisShot',
'TaiChi', 'TennisSwing', 'ThrowDiscus', 'TrampolineJumping', 'Typing',
'UnevenBars', 'VolleyballSpiking', 'WalkingWithDog', 'WallPushups', 'WritingOnBoard',
'YoYo'
]
class VideoTransform:
"""Fast video transform for binary classification"""
def __init__(self):
self.resize = transforms.Resize((112, 112))
self.normalize = transforms.Normalize(
mean=[0.43216, 0.394666, 0.37645],
std=[0.22803, 0.22145, 0.216989]
)
def __call__(self, clip):
if clip is None:
return torch.randn(3, 16, 112, 112)
if clip.dim() != 4:
return torch.randn(3, 16, 112, 112)
# Convert to proper format [C, T, H, W]
if clip.shape[3] == 3: # THWC format
clip = clip.permute(3, 0, 1, 2)
clip = clip.float() / 255.0
# Apply transforms to each frame
transformed_frames = []
for t in range(clip.size(1)):
frame = clip[:, t, :, :]
frame = self.resize(frame)
frame = self.normalize(frame)
transformed_frames.append(frame)
return torch.stack(transformed_frames, dim=1)
class FastBinaryActionDataset(Dataset):
"""
Fast Binary Classification Dataset - No Hanging!
Key optimizations:
- Skip slow sample counting
- Use estimated class weights
- Faster initialization
"""
def __init__(self, data_root, annotation_path, target_action="Basketball",
frames_per_clip=16, train=True):
self.target_action = target_action
self.target_class_idx = UCF101_CLASSES.index(target_action)
print(f"β‘ Creating FAST Binary Classifier: '{target_action}' vs All Others")
print(f"π Target class index: {self.target_class_idx}")
# Load full UCF101 dataset
print("π Loading UCF101 dataset (this may take a moment)...")
self.dataset = datasets.UCF101(
root=data_root,
annotation_path=annotation_path,
frames_per_clip=frames_per_clip,
step_between_clips=1,
train=train,
output_format="THWC"
)
self.transform = VideoTransform()
print(f"β
Dataset loaded: {len(self.dataset)} total samples")
# SKIP the slow sample counting - use estimates instead!
# Typical UCF101 has ~100 samples per class on average
estimated_target = 120 # Basketball has ~120 clips typically
estimated_others = len(self.dataset) - estimated_target
print(f"π Estimated samples:")
print(f" π {target_action}: ~{estimated_target}")
print(f" π« Others: ~{estimated_others}")
# Calculate estimated class weights
total = estimated_target + estimated_others
self.target_weight = total / (2 * estimated_target)
self.non_target_weight = total / (2 * estimated_others)
print(f"βοΈ Estimated weights - {target_action}: {self.target_weight:.3f}, Others: {self.non_target_weight:.3f}")
print("β‘ Fast dataset ready!")
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
try:
video, audio, original_label = self.dataset[idx]
# Convert to binary label: 1 = target action, 0 = all others
binary_label = 1 if original_label == self.target_class_idx else 0
return self.transform(video), binary_label
except Exception as e:
# Return dummy data on error
if idx < 5: # Only print first few errors
print(f"β οΈ Error loading sample {idx}: {e}")
return self.transform(None), 0
def get_fast_binary_dataloaders(data_root, annotation_path, target_action="Basketball",
batch_size=8, num_workers=0):
"""
Get fast binary classification dataloaders - No hanging!
"""
print(f"\nβ‘ Creating FAST Binary Datasets for: {target_action}")
print("=" * 50)
# Create datasets
train_dataset = FastBinaryActionDataset(
data_root, annotation_path, target_action=target_action, train=True
)
test_dataset = FastBinaryActionDataset(
data_root, annotation_path, target_action=target_action, train=False
)
# Create DataLoaders
train_loader = DataLoader(
train_dataset, batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=True
)
test_loader = DataLoader(
test_dataset, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=True
)
# Return estimated class weights
class_weights = torch.tensor([
train_dataset.non_target_weight, # Class 0 (not target)
train_dataset.target_weight # Class 1 (target)
])
print(f"\nπ¦ Fast DataLoaders created:")
print(f" β‘ Train batches: {len(train_loader)}")
print(f" β‘ Test batches: {len(test_loader)}")
print(f" βοΈ Class weights: {class_weights}")
return train_loader, test_loader, class_weights
if __name__ == "__main__":
# Test fast basketball binary classification
data_root = r"C:\ASH_PROJECT\data\UCF101"
annotation_path = r"C:\ASH_PROJECT\data\ucfTrainTestlist"
print("β‘ Testing FAST Basketball Binary Classification Dataset...")
train_loader, test_loader, class_weights = get_fast_binary_dataloaders(
data_root, annotation_path, target_action="Basketball", batch_size=4
)
# Test a few batches
print("\nπ Testing data loading...")
for i, (clips, labels) in enumerate(train_loader):
print(f"Batch {i+1}: clips={clips.shape}, labels={labels}")
if i >= 2: # Test only first 3 batches
break
print("β
Fast binary dataset test completed!") |