Upload src/filtering/filter_data.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294#!/usr/bin/env python3"""Data Filtering Pipeline for GUI-Shift.
Filters K-step GUI Transition samples based on model-generated responses.- Discards samples where all N responses are entirely correct or incorrect- Keeps samples with mixed correctness (informative for learning)
From: GUI-Shift paper Section 3.3 (arXiv:2505.12493)"""
import argparseimport jsonimport osimport randomfrom pathlib import Pathfrom typing import List, Dict, Any, Tuple, Optional
import torchfrom transformers import AutoModelForVision2Seq, AutoProcessorfrom PIL import Image
def load_model_and_processor(model_path: str, device: str = "cuda"): """Load base VLM model and processor for filtering.""" processor = AutoProcessor.from_pretrained( model_path, trust_remote_code=True, ) model = AutoModelForVision2Seq.from_pretrained( model_path, torch_dtype=torch.bfloat16, device_map=device, trust_remote_code=True, ) model.eval() return model, processor
def generate_responses( model, processor, sample: Dict[str, Any], num_generations: int = 8, temperature: float = 0.9, max_new_tokens: int = 256,) -> List[str]: """Generate N candidate responses for a single sample.""" image_paths = sample.get("image_path", sample.get("image", [])) problem = sample["problem"] # Load images images = [] for img_path in image_paths: if isinstance(img_path, str) and os.path.exists(img_path): images.append(Image.open(img_path).convert("RGB")) # Build prompt messages = [ {"role": "user", "content": [{"type": "image"} for _ in images] + [{"type": "text", "text": problem}]} ] text = processor.apply_chat_template(messages, add_generation_prompt=True) inputs = processor( text=text, images=images, return_tensors="pt", padding=True, ).to(model.device) responses = [] with torch.no_grad(): for _ in range(num_generations): outputs = model.generate( **inputs, max_new_tokens=max_new_tokens, temperature=temperature, do_sample=True, num_return_sequences=1, ) # Decode response generated_text = processor.batch_decode( outputs[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True, )[0] responses.append(generated_text) return responses
def evaluate_action_correctness( response: str, ground_truth: Dict[str, Any],) -> float: """ Evaluate if a response action matches the ground truth. Returns 1.0 if correct, 0.0 if incorrect. """ import re # Extract action from response match = re.search(r'<answer>(.*?)</answer>', response, re.DOTALL) if not match: return 0.0 content = match.group(1).strip() try: pred_action = json.loads(content) except json.JSONDecodeError: return 0.0 gt_action = ground_truth if isinstance(ground_truth, dict) else json.loads(ground_truth) pred_type = pred_action.get("action_type", "") gt_type = gt_action.get("action_type", "") if pred_type != gt_type: return 0.0 # Check parameters based on action type if pred_type in ["click", "long_press"]: bbox = gt_action.get("bbox", [0, 0, 0, 0]) x = pred_action.get("x", 0) y = pred_action.get("y", 0) if bbox and len(bbox) >= 4: if bbox[0] <= x <= bbox[2] and bbox[1] <= y <= bbox[3]: return 1.0 # Fallback: check exact coordinates if "x" in gt_action and "y" in gt_action: tolerance = 20 if abs(x - gt_action["x"]) <= tolerance and abs(y - gt_action["y"]) <= tolerance: return 1.0 return 0.0 elif pred_type == "scroll": return 1.0 if pred_action.get("direction") == gt_action.get("direction") else 0.0 elif pred_type == "open_app": return 1.0 if pred_action.get("app_name") == gt_action.get("app_name") else 0.0 elif pred_type == "input_text": return 1.0 if pred_action.get("text") == gt_action.get("text") else 0.0 elif pred_type in ["navigate_back", "navigate_home", "wait"]: return 1.0 return 0.0
def filter_sample( responses: List[str], ground_truth: Dict[str, Any], threshold_all_correct: float = 1.0, threshold_all_incorrect: float = 0.0,) -> bool: """ Decide whether to keep a sample based on response correctness diversity. Returns True if sample should be KEPT (has mixed correctness), False if sample should be DISCARDED (all correct or all incorrect). """ scores = [evaluate_action_correctness(resp, ground_truth) for resp in responses] # Check if all responses are entirely correct if all(score >= threshold_all_correct for score in scores): return False # Too easy, discard # Check if all responses are entirely incorrect if all(score <= threshold_all_incorrect for score in scores): return False # Too hard, discard # Mixed correctness — informative for learning return True
def parse_ground_truth(sample: Dict[str, Any]) -> Dict[str, Any]: """Extract ground truth action from sample.""" if "ground_truth_action" in sample: return sample["ground_truth_action"] # Extract from solution in conversations solution = sample.get("solution", "") if isinstance(solution, str): import re match = re.search(r'<answer>(.*?)</answer>', solution, re.DOTALL) if match: try: return json.loads(match.group(1).strip()) except json.JSONDecodeError: pass return {}
def main(): parser = argparse.ArgumentParser(description="Filter K-step GUI Transition data") parser.add_argument("--input_file", type=str, required=True, help="Input JSONL file with K-step data") parser.add_argument("--output_file", type=str, required=True, help="Output filtered JSONL file") parser.add_argument("--model_path", type=str, required=True, help="Base VLM model for filtering") parser.add_argument("--num_generations", type=int, default=8, help="Number of generations per sample") parser.add_argument("--temperature", type=float, default=0.9, help="Sampling temperature") parser.add_argument("--max_new_tokens", type=int, default=256, help="Max tokens per generation") parser.add_argument("--device", type=str, default="cuda", help="Device for model inference") parser.add_argument("--seed", type=int, default=42, help="Random seed") args = parser.parse_args() random.seed(args.seed) torch.manual_seed(args.seed) print(f"Loading model from {args.model_path}...") model, processor = load_model_and_processor(args.model_path, args.device) print(f"Loading samples from {args.input_file}...") samples = [] with open(args.input_file, "r") as f: for line in f: if line.strip(): samples.append(json.loads(line)) print(f"Loaded {len(samples)} samples. Starting filtering...") kept_samples = [] discarded_easy = 0 discarded_hard = 0 for i, sample in enumerate(samples): print(f" Processing sample {i+1}/{len(samples)}...", end="\r") # Generate responses responses = generate_responses( model, processor, sample, num_generations=args.num_generations, temperature=args.temperature, max_new_tokens=args.max_new_tokens, ) # Get ground truth gt = parse_ground_truth(sample) if not gt: print(f"\n Warning: Could not parse ground truth for sample {sample.get('id', i)}") continue # Evaluate and filter scores = [evaluate_action_correctness(resp, gt) for resp in responses] if all(score >= 1.0 for score in scores): discarded_easy += 1 continue elif all(score <= 0.0 for score in scores): discarded_hard += 1 continue # Add correctness scores to sample metadata sample["filter_scores"] = scores sample["filter_mean_score"] = sum(scores) / len(scores) kept_samples.append(sample) print(f"\nFiltering complete!") print(f" Kept: {len(kept_samples)} samples") print(f" Discarded (too easy): {discarded_easy} samples") print(f" Discarded (too hard): {discarded_hard} samples") # Write filtered data os.makedirs(os.path.dirname(args.output_file) or ".", exist_ok=True) with open(args.output_file, "w") as f: for sample in kept_samples: f.write(json.dumps(sample, ensure_ascii=False) + "\n") print(f"Wrote filtered data to {args.output_file}") # Write statistics stats = { "input_file": args.input_file, "output_file": args.output_file, "model_path": args.model_path, "num_generations": args.num_generations, "total_samples": len(samples), "kept_samples": len(kept_samples), "discarded_easy": discarded_easy, "discarded_hard": discarded_hard, "keep_ratio": len(kept_samples) / len(samples) if samples else 0, } stats_file = args.output_file.replace(".jsonl", "_stats.json") with open(stats_file, "w") as f: json.dump(stats, f, indent=2) print(f"Wrote statistics to {stats_file}")
if __name__ == "__main__": main()src/filtering/filter_data.pyAdded+294