File size: 5,206 Bytes
3404d44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# Run Experiment 2-A for all model types with proper conda environments
#
# Usage:
#   ./run_exp2a.sh           # Run all models
#   ./run_exp2a.sh qwen      # Run only Qwen
#   ./run_exp2a.sh nvila     # Run only NVILA
#   ./run_exp2a.sh molmo     # Run only Molmo

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_DIR="/data/shared/Qwen/experiments/exp2a_results"
SAMPLES=50  # Samples per category
SEED=42

echo "=============================================="
echo "Experiment 2-A: Embedding Space Analysis"
echo "=============================================="
echo "Output directory: $OUTPUT_DIR"
echo "Samples per category: $SAMPLES"
echo ""

# Initialize conda
source /root/miniconda3/etc/profile.d/conda.sh

# Parse arguments
RUN_QWEN=false
RUN_NVILA=false
RUN_MOLMO=false

if [ $# -eq 0 ]; then
    RUN_QWEN=true
    RUN_NVILA=true
    RUN_MOLMO=true
else
    for arg in "$@"; do
        case $arg in
            qwen) RUN_QWEN=true ;;
            nvila) RUN_NVILA=true ;;
            molmo) RUN_MOLMO=true ;;
            *) echo "Unknown model: $arg. Use qwen, nvila, or molmo." ;;
        esac
    done
fi

# Run for Qwen (no conda environment - deactivate all)
if [ "$RUN_QWEN" = true ]; then
    echo "=============================================="
    echo "Running Qwen2.5-VL experiments..."
    echo "=============================================="
    # Deactivate all conda environments to get to base system python
    conda deactivate 2>/dev/null || true
    conda deactivate 2>/dev/null || true
    conda deactivate 2>/dev/null || true

    # Use system python directly
    /root/miniconda3/bin/python "$SCRIPT_DIR/exp2a_embedding_analysis.py" \
        --model_type qwen \
        --scales vanilla 80k 400k 800k 2m \
        --output_dir "$OUTPUT_DIR" \
        --samples_per_category $SAMPLES \
        --seed $SEED
fi

# Run for NVILA (vila conda environment)
if [ "$RUN_NVILA" = true ]; then
    echo "=============================================="
    echo "Running NVILA experiments..."
    echo "=============================================="
    conda activate vila

    python "$SCRIPT_DIR/exp2a_embedding_analysis.py" \
        --model_type nvila \
        --scales vanilla 80k 400k 800k 2m \
        --output_dir "$OUTPUT_DIR" \
        --samples_per_category $SAMPLES \
        --seed $SEED

    conda deactivate
fi

# Run for Molmo (molmo conda environment)
if [ "$RUN_MOLMO" = true ]; then
    echo "=============================================="
    echo "Running Molmo experiments..."
    echo "=============================================="
    conda activate molmo

    python "$SCRIPT_DIR/exp2a_embedding_analysis.py" \
        --model_type molmo \
        --scales vanilla 80k 400k 800k \
        --output_dir "$OUTPUT_DIR" \
        --samples_per_category $SAMPLES \
        --seed $SEED

    conda deactivate
fi

echo ""
echo "=============================================="
echo "Experiments completed!"
echo "Results saved to: $OUTPUT_DIR"
echo "=============================================="

# Generate combined comparison plot (uses base python)
/root/miniconda3/bin/python - <<'EOF'
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

output_dir = "/data/shared/Qwen/experiments/exp2a_results"

# Collect all results
all_results = []
for model_type in ['qwen', 'nvila', 'molmo']:
    summary_path = os.path.join(output_dir, model_type, 'results_summary.csv')
    if os.path.exists(summary_path):
        df = pd.read_csv(summary_path)
        all_results.append(df)

if all_results:
    combined_df = pd.concat(all_results, ignore_index=True)
    combined_df.to_csv(os.path.join(output_dir, 'all_results_summary.csv'), index=False)

    # Create combined comparison plot
    pairs = ['sim_above_far', 'sim_under_close', 'sim_left_right']
    pair_labels = ['above-far', 'under-close', 'left-right']

    fig, axes = plt.subplots(1, 3, figsize=(18, 6))

    for ax, (pair, pair_label) in zip(axes, zip(pairs, pair_labels)):
        for model_type in ['qwen', 'nvila', 'molmo']:
            model_data = combined_df[combined_df['model'].str.startswith(model_type)]
            if not model_data.empty:
                scales = model_data['model'].str.replace(f'{model_type}_', '')
                values = model_data[pair].values
                ax.plot(scales, values, marker='o', label=model_type.upper(), linewidth=2)

        ax.set_title(f'{pair_label}', fontsize=12, fontweight='bold')
        ax.set_xlabel('Training Scale')
        ax.set_ylabel('Cosine Similarity')
        ax.legend()
        ax.set_ylim(0, 1)
        ax.axhline(y=0.5, color='gray', linestyle='--', alpha=0.5)
        ax.tick_params(axis='x', rotation=45)

    plt.suptitle('Spatial Concept Similarity Across Training Scales', fontsize=14, fontweight='bold')
    plt.tight_layout()
    plt.savefig(os.path.join(output_dir, 'all_models_comparison.png'), dpi=300, bbox_inches='tight')
    plt.close()

    print(f"\nCombined results saved to {output_dir}/all_results_summary.csv")
    print(f"Combined plot saved to {output_dir}/all_models_comparison.png")
else:
    print("No results found to combine.")
EOF