Skip to content

Commit bd856c7

Browse files
No public description
PiperOrigin-RevId: 826567725
1 parent 222cd7f commit bd856c7

File tree

7 files changed

+37
-58
lines changed

7 files changed

+37
-58
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Top level package for milk pouch detection."""

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/classify_images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
CLASSIFICATION_DIR = "objects_for_classification"
4545

4646
# Path to the custom trained model for Image Classifier.
47-
IMAGE_CLASSIFIER_WEIGHTS = "vit/best_vit_model_epoch_131.pt"
47+
IMAGE_CLASSIFIER_WEIGHTS = "models/vit/best_vit_model_epoch_131.pt"
4848
CLASS_NAMES = ["dairy", "other"]
4949

5050

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/extract_objects.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@
2626
# See the License for the specific language governing permissions and
2727
# limitations under the License.
2828

29-
# Copyright 2025 The TensorFlow Authors. All Rights Reserved.
30-
#
31-
# Licensed under the Apache License, Version 2.0 (the "License");
32-
# you may not use this file except in compliance with the License.
33-
# You may obtain a copy of the License at
34-
#
35-
# http://www.apache.org/licenses/LICENSE-2.0
36-
#
37-
# Unless required by applicable law or agreed to in writing, software
38-
# distributed under the License is distributed on an "AS IS" BASIS,
39-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40-
# See the License for the specific language governing permissions and
41-
# limitations under the License.
42-
4329
"""Detects, segments, and saves objects from images in a directory.
4430
4531
This script initializes a computer vision pipeline to process images, identify
@@ -68,9 +54,9 @@
6854
warnings.filterwarnings("ignore", category=UserWarning)
6955

7056

71-
GROUNDING_DINO_WEIGHTS = "grounding_dino/groundingdino_swint_ogc.pth"
72-
GROUNDING_DINO_CONFIG = "grounding_dino/GroundingDINO_SwinT_OGC.py"
73-
SAM2_WEIGHTS = "sam2/sam2.1_hiera_large.pt"
57+
GROUNDING_DINO_WEIGHTS = "models/grounding_dino/groundingdino_swint_ogc.pth"
58+
GROUNDING_DINO_CONFIG = "models/grounding_dino/GroundingDINO_SwinT_OGC.py"
59+
SAM2_WEIGHTS = "models/sam2/sam2.1_hiera_large.pt"
7460
SAM2_CONFIG = "configs/sam2.1/sam2.1_hiera_l.yaml"
7561
TEXT_PROMPT = "packets"
7662
INPUT_DIR = "input_images"

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/models/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,3 @@
2828
- sam2_model/: SAM2 weights
2929
- image_classifier_model/: Fine-tuned ViT classifier weights
3030
"""
31-
32-
from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src.models import classification
33-
from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src.models import detection_segmentation
34-
from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src.models import llm

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/models/detection_segmentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from sam2 import sam2_image_predictor
2525
import torch
2626

27-
from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection import models_utils
27+
from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src import models_utils
2828

2929
# Suppress common warnings for a cleaner console output.
3030
warnings.filterwarnings('ignore', category=UserWarning)

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/run_pipeline.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fi
2323
# --- Run pipeline ---
2424
echo "✅ Activating virtual environment..."
2525
source myenv/bin/activate
26+
cd milk_pouch_project
2627

2728
# List the image files in the GCS path.
2829
# NOTE: Adjust the grep pattern if other image types are expected.

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/setup.sh

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,31 @@
44
# using a model ensemble approach of bounding box detection, segmentation,
55
# and classification. The classification model is a pre-trained VIT model.
66

7-
# The script performs the following steps:
8-
# It performs the following steps:
9-
# 1. Installs system-level dependencies.
10-
# 2. Creates a Python virtual environment.
11-
# 3. Installs PyTorch, GroundingDINO, SAM2, and other Python packages.
12-
# 4. Creates project directories and downloads required model checkpoints.
13-
# 5. Fetches the custom trained VIT classifier model.
14-
# 6. Fetches the scripts to run the pipeline.
15-
167
# Exit immediately if a command exits with a non-zero status.
178
set -o errexit
189
# Treat unset variables as an error when substituting.
1910
set -o nounset
2011
# Pipes fail if any command in the pipe fails.
2112
set -o pipefail
2213

23-
# --- 1. Install System Dependencies ---
2414
echo "🔹 Starting: Install System Dependencies"
2515
apt-get update
2616
apt-get install -y python3-venv python3-pip lsof curl
2717
echo "✅ Finished: Install System Dependencies"
2818
echo "-----"
2919

30-
# --- 2. Creating Virtual Environment ---
3120
echo "🔹 Starting: Create Virtual Environment"
3221
python3.10 -m venv myenv
3322
source myenv/bin/activate
3423
echo "✅ Finished: Create Virtual Environment"
3524
echo "-----"
3625

37-
# --- 3. Install Compatible Torch version ---
3826
echo "🔹 Starting: Install Torch, Torchvision, Torchaudio"
3927
pip uninstall -y torch torchvision torchaudio > /dev/null 2>&1 || true
4028
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
4129
echo "✅ Finished: Install Torch, Torchvision, Torchaudio"
4230
echo "-----"
4331

44-
# --- 4. Install Grounding DINO ---
4532
echo "🔹 Starting: Install Grounding DINO"
4633
git clone https://github.com/IDEA-Research/GroundingDINO.git
4734
cd GroundingDINO
@@ -50,7 +37,6 @@ cd ..
5037
echo "✅ Finished: Install Grounding DINO"
5138
echo "-----"
5239

53-
# --- 5. Install SAM2 and Required Packages ---
5440
echo "🔹 Starting: Install SAM2 and Required Python Packages"
5541
pip install --no-cache-dir \
5642
opencv-python \
@@ -63,15 +49,13 @@ pip install --no-cache-dir \
6349
echo "✅ Finished: Install SAM2 and Required Python Packages"
6450
echo "-----"
6551

66-
# --- 6. Set Up Project Directories ---
6752
echo "🔹 Starting: Create Project Directory Structure"
6853
mkdir -p milk_pouch_project/models/sam2
6954
mkdir -p milk_pouch_project/models/grounding_dino
7055
mkdir -p milk_pouch_project/models/vit
7156
echo "✅ Finished: Create Project Directory Structure"
7257
echo "-----"
7358

74-
# --- 7. Download Model Checkpoints ---
7559
echo "🔹 Starting: Download SAM2 Checkpoint"
7660
wget -P ./milk_pouch_project/models/sam2 https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_large.pt
7761
echo "✅ Finished: Download SAM2 Checkpoint"
@@ -88,28 +72,25 @@ wget -P ./milk_pouch_project/models/vit https://storage.googleapis.com/tf_model_
8872
echo "✅ Finished: Download Image Classifier Model"
8973
echo "-----"
9074

91-
echo "Download the required files locally and modify the imports."
92-
curl -sS -o milk_pouch_project/models/detection_segmentation.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/models/detection_segmentation.py
93-
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src import models_utils|import ../models_utils|g' detection_segmentation.py
94-
95-
curl -sS -o milk_pouch_project/classify_images.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/classify_images.py
96-
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src.models import classification|from models import classification|g' classify_images.py
75+
echo "🔹 Starting: Clone Required Files from TensorFlow Models Repo"
76+
git clone --depth 1 --filter=blob:none --sparse https://github.com/tensorflow/models.git temp_tf_models
77+
cd temp_tf_models
78+
git sparse-checkout set official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src
79+
cd ..
80+
cp -r "temp_tf_models/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src"/* milk_pouch_project/
81+
rm -rf temp_tf_models
82+
echo "✅ Finished: Clone Required Files from TensorFlow Models Repo"
83+
echo "-----"
9784

98-
curl -sS -o milk_pouch_project/extract_objects.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/extract_objects.py
99-
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src.models import detection_segmentation|from models import detection_segmentation|g' extract_objects.py
100-
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src import batched_io|import batched_io|g' extract_objects.py
101-
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src import coco_annotation_writer|import coco_annotation_writer|g' extract_objects.py
85+
echo "🔹 Starting: Modify Imports for Local Project Structure"
86+
find milk_pouch_project -type f -name "*.py" -exec sed -i \
87+
's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src.models import |from models import |g' {} +
88+
find milk_pouch_project -type f -name "*.py" -exec sed -i \
89+
's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection.src import |import |g' {} +
10290

103-
curl -sS -o milk_pouch_project/models/classification.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/models/classification.py
104-
curl -sS -o milk_pouch_project/models/llm.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/models/llm.py
105-
curl -sS -o milk_pouch_project/models_utils.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/models_utils.py
106-
curl -sS -o milk_pouch_project/batched_io.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/batched_io.py
107-
curl -sS -o milk_pouch_project/run_pipeline.sh https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/src/run_pipeline.sh
91+
echo "✅ Finished: Modify Imports for Loscal Project Structure"
10892
echo "Files downloaded and modified successfully!"
93+
echo "-----"
10994

110-
# --- Completion ---
11195
echo "🎉🎉🎉 Environment setup complete! 🎉🎉🎉"
11296
echo "-----"
113-
114-
# Deactivate the virtual environment
115-
deactivate

0 commit comments

Comments
 (0)