File size: 3,873 Bytes
eb09c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Quick test to verify the tensor creation fix works.
This creates a simple test scenario to check if our fix resolves the padding issue.
"""

import sys
import tempfile
from pathlib import Path
import numpy as np
from PIL import Image

def create_simple_test_frames(num_frames=8):
    """Create simple test frames."""
    frames = []
    for i in range(num_frames):
        # Create a 224x224 RGB image with different colors per frame
        img_array = np.full((224, 224, 3), fill_value=(i * 30) % 255, dtype=np.uint8)
        frame = Image.fromarray(img_array, 'RGB')
        frames.append(frame)
    return frames

def test_tensor_creation():
    """Test the tensor creation with our fix."""
    print("πŸ§ͺ Testing Tensor Creation Fix")
    print("=" * 40)

    try:
        # Import required modules
        from transformers import AutoImageProcessor
        import torch
        print("βœ… Imports successful")

        # Load processor
        processor = AutoImageProcessor.from_pretrained("facebook/timesformer-base-finetuned-k400")
        print("βœ… Processor loaded")

        # Create test frames
        frames = create_simple_test_frames(8)
        print(f"βœ… Created {len(frames)} test frames")

        # Test our fix approach
        try:
            inputs = processor(images=frames, return_tensors="pt", padding=True)
            print(f"βœ… Tensor created successfully!")
            print(f"   Shape: {inputs['pixel_values'].shape}")
            print(f"   Dtype: {inputs['pixel_values'].dtype}")
            return True

        except Exception as e:
            print(f"❌ Primary approach failed: {e}")

            # Try fallback
            try:
                inputs = processor(images=[frames], return_tensors="pt", padding=True)
                print(f"βœ… Fallback approach worked!")
                print(f"   Shape: {inputs['pixel_values'].shape}")
                return True
            except Exception as e2:
                print(f"❌ Fallback also failed: {e2}")
                return False

    except Exception as e:
        print(f"❌ Test setup failed: {e}")
        return False

def test_prediction_pipeline():
    """Test the full prediction pipeline."""
    print("\n🎬 Testing Full Pipeline")
    print("=" * 40)

    try:
        from predict import predict_actions
        print("βœ… Import successful")

        # Create a temporary video file (simulate with images)
        with tempfile.TemporaryDirectory() as tmp_dir:
            tmp_path = Path(tmp_dir)

            # For this test, we'll create a simple video-like structure
            # Since we can't easily create a real video, we'll test the frame processing directly

            # This would normally be called by predict_actions with a real video file
            print("⚠️  Note: Full video test requires a real video file")
            print("   The tensor fix is now in place in predict.py")

        return True

    except Exception as e:
        print(f"❌ Pipeline test failed: {e}")
        return False

if __name__ == "__main__":
    print("πŸ”§ Quick Test Suite for Tensor Fix")
    print("=" * 50)

    # Test 1: Basic tensor creation
    test1_passed = test_tensor_creation()

    # Test 2: Pipeline integration
    test2_passed = test_prediction_pipeline()

    print("\nπŸ“Š Results:")
    print(f"   Tensor creation: {'βœ… PASSED' if test1_passed else '❌ FAILED'}")
    print(f"   Pipeline check: {'βœ… PASSED' if test2_passed else '❌ FAILED'}")

    if test1_passed:
        print("\nπŸŽ‰ The tensor creation fix appears to be working!")
        print("   You can now try uploading a video to the Streamlit app.")
    else:
        print("\nπŸ’₯ The fix may need more work. Check the error messages above.")

    print("\nπŸ’‘ Next step: Run 'streamlit run app.py' and test with a real video")