Spaces:
Sleeping
Sleeping
| # Install required packages | |
| !pip install -q datasets transformers accelerate evaluate pillow | |
| # Complete Food-101 Training - Single Cell | |
| # 1. Setup | |
| from google.colab import userdata | |
| import os | |
| import torch | |
| from datasets import load_dataset | |
| from transformers import ( | |
| AutoImageProcessor, | |
| AutoModelForImageClassification, | |
| TrainingArguments, | |
| Trainer | |
| ) | |
| import evaluate | |
| import numpy as np | |
| # Setup HF token | |
| hf_token = userdata.get('HF_TOKEN') | |
| os.environ['HF_TOKEN'] = hf_token | |
| # Check GPU | |
| print(f"GPU Available: {torch.cuda.is_available()}") | |
| print(f"GPU Name: {torch.cuda.get_device_name(0)}\n") | |
| # 2. Load Dataset | |
| print("Loading Food-101 dataset...") | |
| dataset = load_dataset("food101") | |
| print(f"Train samples: {len(dataset['train']):,}") | |
| print(f"Test samples: {len(dataset['validation']):,}\n") | |
| # Get class names | |
| class_names = dataset['train'].features['label'].names | |
| print(f"Number of classes: {len(class_names)}\n") | |
| # 3. Setup Preprocessing | |
| model_name = "microsoft/resnet-50" | |
| processor = AutoImageProcessor.from_pretrained(model_name) | |
| def preprocess_dataset(example): | |
| image = example['image'].convert("RGB") | |
| inputs = processor(image, return_tensors="pt") | |
| return { | |
| 'pixel_values': inputs['pixel_values'].squeeze(0), | |
| 'labels': example['label'] | |
| } | |
| # 4. Split and Preprocess Data | |
| print("Splitting dataset...") | |
| train_val = dataset['train'].train_test_split(test_size=0.1, seed=42) | |
| print("Preprocessing train dataset (this takes 5-10 minutes)...") | |
| train_dataset = train_val['train'].map( | |
| preprocess_dataset, | |
| remove_columns=['image', 'label'], | |
| batched=False | |
| ) | |
| print("Preprocessing validation dataset...") | |
| val_dataset = train_val['test'].map( | |
| preprocess_dataset, | |
| remove_columns=['image', 'label'], | |
| batched=False | |
| ) | |
| print("Preprocessing test dataset...") | |
| test_dataset = dataset['validation'].map( | |
| preprocess_dataset, | |
| remove_columns=['image', 'label'], | |
| batched=False | |
| ) | |
| # Set format | |
| train_dataset.set_format(type='torch', columns=['pixel_values', 'labels']) | |
| val_dataset.set_format(type='torch', columns=['pixel_values', 'labels']) | |
| test_dataset.set_format(type='torch', columns=['pixel_values', 'labels']) | |
| print(f"\nTrain: {len(train_dataset):,}") | |
| print(f"Validation: {len(val_dataset):,}") | |
| print(f"Test: {len(test_dataset):,}\n") | |
| # 5. Load Model | |
| print("Loading model...") | |
| id2label = {i: label for i, label in enumerate(class_names)} | |
| label2id = {label: i for i, label in enumerate(class_names)} | |
| model = AutoModelForImageClassification.from_pretrained( | |
| model_name, | |
| num_labels=len(class_names), | |
| id2label=id2label, | |
| label2id=label2id, | |
| ignore_mismatched_sizes=True | |
| ) | |
| print(f"Model loaded: {model_name}") | |
| print(f"Total parameters: {sum(p.numel() for p in model.parameters()):,}\n") | |
| # 6. Setup Metrics | |
| accuracy_metric = evaluate.load("accuracy") | |
| f1_metric = evaluate.load("f1") | |
| def compute_metrics(eval_pred): | |
| predictions, labels = eval_pred | |
| predictions = np.argmax(predictions, axis=1) | |
| accuracy = accuracy_metric.compute(predictions=predictions, references=labels) | |
| f1 = f1_metric.compute(predictions=predictions, references=labels, average='weighted') | |
| return { | |
| 'accuracy': accuracy['accuracy'], | |
| 'f1': f1['f1'] | |
| } | |
| # 7. Training Arguments | |
| training_args = TrainingArguments( | |
| output_dir="./food101-resnet50", | |
| num_train_epochs=5, | |
| per_device_train_batch_size=64, | |
| per_device_eval_batch_size=64, | |
| learning_rate=2e-5, | |
| weight_decay=0.01, | |
| warmup_steps=500, | |
| logging_steps=100, | |
| eval_strategy="epoch", | |
| save_strategy="epoch", | |
| save_total_limit=2, | |
| load_best_model_at_end=True, | |
| metric_for_best_model="accuracy", | |
| greater_is_better=True, | |
| fp16=True, | |
| dataloader_num_workers=0, | |
| push_to_hub=True, | |
| hub_model_id="suchithnj12/food101-resnet50", | |
| hub_strategy="end", | |
| report_to="none" | |
| ) | |
| # 8. Create Trainer | |
| trainer = Trainer( | |
| model=model, | |
| args=training_args, | |
| train_dataset=train_dataset, | |
| eval_dataset=val_dataset, | |
| compute_metrics=compute_metrics | |
| ) | |
| # 9. Train | |
| print("Starting training (this takes 2-3 hours)...") | |
| print("-" * 60) | |
| train_result = trainer.train() | |
| print("\nTraining completed!") | |
| print(f"Final train loss: {train_result.metrics['train_loss']:.4f}") | |
| print(f"Training time: {train_result.metrics['train_runtime']/60:.2f} minutes\n") | |
| # 10. Evaluate on Test Set | |
| print("Evaluating on test set...") | |
| test_results = trainer.evaluate(test_dataset) | |
| print("\nTest Results:") | |
| print(f"Accuracy: {test_results['eval_accuracy']:.4f}") | |
| print(f"F1 Score: {test_results['eval_f1']:.4f}\n") | |
| # 11. Push to Hub | |
| print("Pushing model to HuggingFace Hub...") | |
| trainer.push_to_hub(commit_message="Food-101 ResNet-50 trained model") | |
| print(f"\nModel available at: https://huggingface.co/suchithnj12/food101-resnet50") | |
| print("Training pipeline completed successfully!") | |
| ingβbuilderβscript:β | |
| β4.20k/?β[00:00<00:00,β416kB/s] | |
| Downloadingβbuilderβscript:β | |
| β6.79k/?β[00:00<00:00,β645kB/s] | |
| Starting training (this takes 2-3 hours)... | |
| ------------------------------------------------------------ | |
| [5330/5330 1:56:41, Epoch 5/5] | |
| Epoch Training Loss Validation Loss Accuracy F1 | |
| 1 4.496600 4.458635 0.166337 0.154736 | |
| 2 3.814000 3.742320 0.298614 0.261986 | |
| 3 3.257200 3.191649 0.362508 0.330396 | |
| 4 2.935900 2.900200 0.397096 0.368665 | |
| 5 2.847600 2.818526 0.408317 0.380199 | |
| Training completed! | |
| Final train loss: 3.6109 | |
| Training time: 116.77 minutes | |
| Evaluating on test set... | |
| [395/395 05:23] | |
| Test Results: | |
| Accuracy: 0.4450 | |
| F1 Score: 0.4139 | |
| Pushing model to HuggingFace Hub... | |
| ProcessingβFilesβ(2β/β2)ββββββ:β100% | |
| β95.1MBβ/β95.1MB,β9.91MB/sββ | |
| NewβDataβUploadβββββββββββββββ:β100% | |
| β95.1MBβ/β95.1MB,β9.91MB/sββ | |
| ββ...esnet50/model.safetensors:β100% | |
| β95.1MBβ/β95.1MBββββββββββββ | |
| ββ...esnet50/training_args.bin:β100% | |
| β5.84kBβ/β5.84kBββββββββββββ | |
| Model available at: https://huggingface.co/suchithnj12/food101-resnet50 | |
| Training pipeline completed successfully! |