### Download and Display Example Image Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Downloads an example coffee mug image from a URL and opens it using PIL. This image will be used to demonstrate object detection. ```python # Download an example image IM_URL = "https://cvbp-secondary.z19.web.core.windows.net/images/cvbp_cup.jpg" im_path = os.path.join(data_path(), "example.jpg") urllib.request.urlretrieve(IM_URL, im_path) im = Image.open(im_path) im ``` -------------------------------- ### Start Jupyter Notebook Server Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md Command to launch the Jupyter notebook server. Once started, users can access and run the notebooks from their web browser. ```bash jupyter notebook ``` -------------------------------- ### Setup Notebook Environment Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Configures the Jupyter notebook environment for automatic reloading of modules and inline display of plots. It imports essential Python libraries and specific modules from fastai and utils_cv for computer vision tasks. ```python %reload_ext autoreload %autoreload 2 %matplotlib inline # Regular python libraries import sys import io import os from pathlib import Path import time # fast.ai import fastai from fastai.vision import ( cnn_learner, DatasetType, ImageList, imagenet_stats, models, open_image ) from ipywebrtc import CameraStream, ImageRecorder from ipywidgets import HBox, VBox, Label, Layout, widgets, Widget import scrapbook as sb # # Computer Vision repository sys.path.extend(["..", "../.."]) # to access the utils_cv library from utils_cv.classification.data import Urls from utils_cv.classification.model import model_to_learner from utils_cv.common.data import unzip_url from utils_cv.common.gpu import which_processor, db_num_workers from utils_cv.similarity.metrics import compute_distances from utils_cv.similarity.model import compute_feature, compute_features_learner from utils_cv.similarity.plot import plot_distances print(f"Fast.ai: {fastai.__version__}") which_processor() ``` -------------------------------- ### Download and Prepare Example Image for Classification Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/00_webcam.ipynb Downloads a sample image from a URL and opens it using the `open_image` function. This prepares the image for prediction by the classification model. Requires `urllib`, `os`, and the `open_image` function. ```python # Download an example image IM_URL = "https://cvbp-secondary.z19.web.core.windows.net/images/cvbp_cup.jpg" urllib.request.urlretrieve(IM_URL, os.path.join(data_path(), "example.jpg")) im = open_image(os.path.join(data_path(), "example.jpg"), convert_mode='RGB') im ``` -------------------------------- ### Install 'utils_cv' Library using Pip Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md Alternative installation method using pip to install only the 'utils_cv' library from the GitHub repository. This method does not download the notebooks and does not create a new conda environment. ```bash pip install git+https://github.com/microsoft/ComputerVision.git@master#egg=utils_cv ``` -------------------------------- ### Provision VM using VM Builder Tool Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md This command initiates the VM Builder tool, located in the 'contrib' folder, to preconfigure a virtual machine with the necessary settings for the computer vision recipes repository. This tool is intended for Linux and Mac users. ```shell python contrib/vm_builder/vm_builder.py ``` -------------------------------- ### Load and Display Image for Retrieval (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Loads a query image from a specified path and displays it. This is the initial step for image retrieval from a file. ```python query_im = open_image(query_im_path, convert_mode='RGB') query_im ``` -------------------------------- ### Log Outputs for Testing (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Logs specific outputs like computed query features and distances using `sb.glue`. This is useful for verifying notebook execution during testing. ```python # Log some outputs which can be used during testing to verify correct notebook execution sb.glue("query_feature", list(query_feature)) sb.glue("distances", distances) ``` -------------------------------- ### Clone Repository and Create Conda Environment Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md Steps to clone the computervision-recipes repository and create a conda environment using the provided 'environment.yml' file. This sets up all necessary dependencies for running the notebooks. ```bash git clone https://github.com/Microsoft/computervision-recipes cd computervision-recipes conda env create -f environment.yml ``` -------------------------------- ### Setup Notebook Environment with Fast.ai Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/00_webcam.ipynb Configures the Jupyter notebook environment for automatic reloading of modules and sets up matplotlib for inline plotting. It also imports necessary libraries from fastai and other utility modules for computer vision tasks. ```python %reload_ext autoreload %autoreload 2 %matplotlib inline import sys sys.path.append("../../") import io import os import time import urllib.request import fastai from fastai.vision import models, open_image from ipywebrtc import CameraStream, ImageRecorder from ipywidgets import HBox, Label, Layout, Widget import scrapbook as sb from utils_cv.common.data import data_path from utils_cv.common.gpu import which_processor from utils_cv.classification.data import imagenet_labels from utils_cv.classification.model import IMAGENET_IM_SIZE, model_to_learner print(f"Fast.ai: {fastai.__version__}") which_processor() ``` -------------------------------- ### Start Webcam Inference and Display Widgets Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb This code snippet initializes webcam inference by setting a global flag `is_playing` to True and starting the `predict_webcam_frames` function in a separate thread. It also hides the image recorder widget for improved performance and attaches the `start` function to observe changes in the webcam's image value. ```python is_playing = False # Once prediciton started, hide image recorder widget for faster fps def start(_): global is_playing # Make sure this get called only once if not is_playing: w_imrecorder.layout.display = "none" is_playing = True Thread(target=predict_webcam_frames).start() w_imrecorder.image.observe(start, "value") ``` -------------------------------- ### Initialize Webcam and Image Retrieval Widgets (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Sets up webcam stream and image recording widgets using `ipywebrtc` for real-time image retrieval. It also initializes widgets for displaying results and labels. ```python # Webcam w_cam = CameraStream( constraints={ "facing_mode": "user", "audio": False, "video": {"width": IM_SIZE, "height": IM_SIZE}, }, layout=Layout(width=f"{IM_SIZE}px"), ) # Image recorder for taking a snapshot w_imrecorder = ImageRecorder(stream=w_cam, layout=Layout(padding="0 0 0 50px")) # Label widget to show our retrieval results w_label = Label(layout=Layout(padding="0 0 0 50px")) w_im = widgets.Image() ``` -------------------------------- ### Azure CLI Login and Subscription Setup Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/vmss_builder/README.md This snippet demonstrates how to log in to Azure CLI and set the active subscription. Ensure you have the Azure CLI installed and replace '{your-azure-subscription-id}' with your actual subscription ID. ```bash az login az account set -s {your-azure-subscription-id} ``` -------------------------------- ### Image Retrieval Example: Find Similar Images Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/02_state_of_the_art.ipynb Demonstrates how to find and display images most similar to a query image within the ranking set. It involves extracting the query image's feature, computing distances, and plotting the results. ```python # Get the DNN feature for the query image query_im_path = str(data_rank.train_ds.items[1]) query_feature = dnn_features[query_im_path] print(f"Query image path: {query_im_path}") print(f"Query feature dimension: {len(query_feature)}") assert len(query_feature) == EMBEDDING_DIM # Compute the distances between the query and all reference images distances = compute_distances(query_feature, dnn_features) plot_distances(distances, num_rows=1, num_cols=6, figsize=(15,5)) ``` -------------------------------- ### Load and Display Example Image (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/crowd_counting/crowdcounting/examples/tutorial.ipynb Loads an example image file using Pillow and displays it using Matplotlib. This step is crucial for visualizing the input before applying crowd counting models. ```python img = '../data/images/1.jpg' pil_im = Image.open(img)imshow(np.asarray(pil_im)) ``` -------------------------------- ### Display Webcam Retrieval Widgets (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Renders the initialized webcam, image recorder, and result display widgets in a vertical box layout for user interaction. ```python # Show widgets VBox([w_label, HBox([w_cam, w_imrecorder, w_im])]) ``` -------------------------------- ### Setup Webcam Stream and Image Recorder for Classification Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/00_webcam.ipynb Initializes a webcam stream using `ipywebrtc` and an `ImageRecorder` widget to capture snapshots. It also sets up a `Label` widget to display classification results. This requires `ipywebrtc` and `Layout` from `ipywidgets`. ```python # Webcam w_cam = CameraStream( constraints={ 'facing_mode': 'user', 'audio': False, 'video': { 'width': IMAGENET_IM_SIZE, 'height': IMAGENET_IM_SIZE } }, layout=Layout(width=f'{IMAGENET_IM_SIZE}px') ) # Image recorder for taking a snapshot w_imrecorder = ImageRecorder(stream=w_cam, layout=Layout(padding='0 0 0 50px')) # Label widget to show our classification results w_label = Label(layout=Layout(padding='0 0 0 50px')) ``` -------------------------------- ### Install Computer Vision Utilities Source: https://context7.com/microsoft/computervision-recipes/llms.txt Installs the `utils_cv` Python package directly via pip or clones the repository and sets up a conda environment for the full project. This provides access to the computer vision utilities and example notebooks. ```bash # Install utils_cv library only pip install git+https://github.com/microsoft/ComputerVision.git@master#egg=utils_cv # Or clone repository and create conda environment git clone https://github.com/Microsoft/computervision-recipes cd computervision-recipes conda env create -f environment.yml conda activate cv python -m ipykernel install --user --name cv --display-name "Python (cv)" ``` -------------------------------- ### Display Detection Results Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Prints the detailed detection results, including bounding box coordinates, labels, and confidence scores for each detected object in the image. ```python detections ``` -------------------------------- ### Setup Notebook Environment - Python Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/html_demo/JupyterCode/2_upload_ui.ipynb Configures the Jupyter notebook environment by ensuring that changes to libraries are loaded and that plots are displayed directly within the notebook. This is a standard setup for interactive Python development in notebooks. ```python # Ensure edits to libraries are loaded and plotting is shown in the notebook. %matplotlib inline %reload_ext autoreload %autoreload 2 ``` -------------------------------- ### Compute and Plot Image Distances (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Calculates the distances between the query image's feature vector and all reference image feature vectors. It then visualizes these distances. ```python # Compute the distances between the query and all reference images distances = compute_distances(query_feature, ref_features) plot_distances(distances, num_rows=1, num_cols=7, figsize=(15,5)) ``` -------------------------------- ### Install Tracking Libraries (Bash) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/tracking/README.md Installs necessary libraries for multi-object tracking in the 'cv' conda environment. Includes OpenCV, YACS, LAP, progress, cython_bbox, and motmetrics. ```bash activate cv conda install -c conda-forge opencv yacs lap progress pip install cython_bbox motmetrics ``` -------------------------------- ### Initialize VideoLearner Model Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb Initializes the VideoLearner model with a pre-trained 'kinetics' base model and specifies the sample length (number of frames). This prepares the model for action recognition tasks. ```python learner = VideoLearner( base_model="kinetics", sample_length=NUM_FRAMES, ) ``` -------------------------------- ### Get or Create Azure ML Workspace Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/20_azure_workspace_setup.ipynb This Python code attempts to retrieve an existing Azure Machine Learning workspace or create a new one if it doesn't exist. It utilizes the provided subscription ID, resource group, workspace name, and region. This operation requires appropriate Azure permissions and may fail if permissions are insufficient or if workspace creation limits are reached. ```python # Get or Create the workspace ws = Workspace(subscription_id=subscription_id, resource_group=resource_group, workspace_name=workspace_name, location=workspace_region) ``` -------------------------------- ### Setup Conda Environment Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/action_recognition/i3d/README.md This command sets up the necessary environment for the project using Conda. It first creates a new Conda environment from the 'environment.yaml' file and then activates the newly created 'i3d' environment. ```bash conda env create -f environment.yaml conda activate i3d ``` -------------------------------- ### Classify Webcam Frame and Retrieve Similar Image (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Defines a callback function to process frames from the webcam. It computes the DNN feature for the frame, retrieves the most similar image from the reference set, and displays the result. ```python def classify_frame(_): """ Classify an image snapshot by using a pretrained model """ # Once capturing started, remove the capture widget since we don't need it anymore if w_imrecorder.layout.display != "none": w_imrecorder.layout.display = "none" try: cam_im = open_image(io.BytesIO(w_imrecorder.image.value), convert_mode="RGB") # Compute DNN representation for the webcam frame featurizer.features = None _, ind, prob = learn.predict(cam_im) query_features = featurizer.features # Retrieve most similar image among the reference images similars = compute_topk_similar(query_features, ref_features) im_path, distance = similars[0] # Show result label and confidence w_label.value = ( f"Most similar image with L2 distance of {distance:0.3f}: {im_path}" ) w_im.value = open(im_path, "rb").read() except OSError: # If im_recorder doesn't have valid image data, skip it. pass # Taking the next snapshot programmatically w_imrecorder.recording = True # Register classify_frame as a callback. Will be called whenever image.value changes. w_imrecorder.image.observe(classify_frame, "value") ``` -------------------------------- ### Display Webcam and Classification Widgets Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/00_webcam.ipynb Renders the `CameraStream`, `ImageRecorder`, and `Label` widgets in a horizontal box layout, allowing users to interact with the webcam for real-time image classification. ```python # Show widgets HBox([w_cam, w_imrecorder, w_label]) ``` -------------------------------- ### Initialize Webcam Stream and Image Recorder (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb Sets up a webcam stream with specified constraints (e.g., resolution, facing mode) and an image recorder for capturing snapshots. This is used for real-time action prediction from a webcam feed. ```python # Webcam settings w_cam = CameraStream( constraints={ "facing_mode": "user", "audio": False, "video": {"width": 400, "height": 400}, }, layout=Layout(width="400px"), ) # Image recorder for taking a snapshot w_imrecorder = ImageRecorder( format="jpg", stream=w_cam, layout=Layout(padding="0 0 0 100px") ) ``` -------------------------------- ### Check CUDA Toolkit Version Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md Command to check the installed CUDA Toolkit version on the system. This is a system requirement for certain deep learning functionalities within the repository. ```bash nvcc --version ``` -------------------------------- ### Example SSH Tunnel for Jupyter Notebook Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md This is a specific example of the SSH tunneling command, forwarding local port 9999 to the remote port 8888, which is typically used by Jupyter notebooks. This allows access to the Jupyter notebook running on the VM via localhost:9999 in the local browser. ```shell ssh -L 9999:localhost:8888 @ ``` -------------------------------- ### Activate Conda Environment and Register Jupyter Kernel Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md Commands to activate the 'cv' conda environment and register it as a kernel for Jupyter notebooks. This allows notebooks to utilize the installed packages and Python environment. ```bash conda activate cv python -m ipykernel install --user --name cv --display-name "Python (cv)" ``` -------------------------------- ### Initialize Webcam Stream for Object Detection Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Sets up a webcam stream using the `ipywebrtc` library to capture video input. This stream will be used for real-time object detection within the notebook. ```python camera = CameraStream(constraints={'video': True, 'audio': False}) image_recorder = ImageRecorder(camera=camera) # Display the camera widget # Note: Jupyter widgets can be unstable. Refer to FAQ for troubleshooting. camera ``` -------------------------------- ### Check Azure ML SDK Version Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/20_azure_workspace_setup.ipynb This Python code snippet checks the installed version of the Azure ML SDK. It's essential for ensuring compatibility with Azure services and other SDK components. It requires the 'azureml-core' package to be installed. ```python # For automatic reloading of modified libraries %autoreload 2 import sys sys.path.extend(["..", "../.."]) # to access the utils_cv library # Azure import azureml.core from azureml.core import Workspace # Check core SDK version number print(f"Azure ML SDK Version: {azureml.core.VERSION}") ``` -------------------------------- ### Initialize Webcam and Image Recorder Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Sets up a camera stream with specific video constraints and an image recorder to capture frames. It also initializes an image widget to display detection results. This is a prerequisite for object detection from a live feed. ```python w_cam = CameraStream( constraints={ 'facing_mode': 'user', 'audio': False, 'video': { 'width': 200, 'height': 200 } }, layout=Layout(width='200px') ) # Image recorder for taking a snapshot w_imrecorder = ImageRecorder(stream=w_cam, layout=Layout(padding='0 0 0 50px')) # Label widget to show our object detection results w_im = widgets.Image(layout=Layout(width='200px')) ``` -------------------------------- ### Prepare Dataset and Configure Training Parameters with Python Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/02_state_of_the_art.ipynb This code block downloads a dataset for image retrieval, defines paths for training and testing data, and sets various hyperparameters for model training. It includes configurations for epochs, learning rates, batch size, image dimensions, dropout, architecture, and embedding dimension. ```python # Dataset data_root_dir = unzip_url(Urls.fridge_objects_retrieval_path, exist_ok = True) DATA_FINETUNE_PATH = os.path.join(data_root_dir, "train") DATA_RANKING_PATH = os.path.join(data_root_dir, "test") print("Image root directory: {}".format(data_root_dir)) # DNN configuration and learning parameters. Use more epochs to possibly improve accuracy. EPOCHS_HEAD = 6 #12 EPOCHS_BODY = 6 #12 HEAD_LEARNING_RATE = 0.01 BODY_LEARNING_RATE = 0.0001 BATCH_SIZE = 32 IM_SIZE = (224,224) DROPOUT = 0 ARCHITECTURE = models.resnet50 # Desired embedding dimension. Higher dimensions slow down retrieval but often provide better accuracy. EMBEDDING_DIM = 2048 assert EMBEDDING_DIM == 4096 or EMBEDDING_DIM <= 2048 ``` -------------------------------- ### Display Fast.ai Version and Processor Information Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/html_demo/JupyterCode/1_image_similarity_export.ipynb Prints the installed version of the Fast.ai library and identifies the available processor (CPU or GPU) for computations. This helps in verifying the environment setup and understanding the hardware being utilized. ```python print(f"Fast.ai version = {fastai.__version__}") which_processor() ``` -------------------------------- ### Load and Prepare Reference Images Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/00_webcam.ipynb Loads reference images for similarity comparison and prepares them using fastai's ImageList. It defines image size, specifies paths for query and reference images, and creates a DataBunch for model training or inference. The images are transformed and normalized using ImageNet statistics. ```python IM_SIZE = 300 # image size in pixels. Reduce to speed-up demo. # Set path to query and reference images im_path = unzip_url(Urls.fridge_objects_path, exist_ok=True) ref_im_path = os.path.join(Path(im_path) / "can") query_im_path = os.path.join(Path(im_path) / "can" / "1.jpg") print(f"Query image path = {query_im_path}") print(f"Reference images directory = {ref_im_path}") # Construct a DataBunch ref_data = ( ImageList.from_folder(ref_im_path) .split_none() .label_from_folder() .transform(tfms=None, size=IM_SIZE) .databunch(bs=2, num_workers = db_num_workers()) .normalize(imagenet_stats) ) ``` -------------------------------- ### Establish SSH Tunnel for Port Forwarding Source: https://github.com/microsoft/computervision-recipes/blob/staging/SETUP.md This command sets up an SSH tunnel to forward a local port to a remote port on a server. This is useful for accessing services running on a remote VM, such as Jupyter notebooks, from your local machine. ```shell ssh -L local_port:remote_address:remote_port @ ``` -------------------------------- ### Configure Action Recognition Parameters Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb Sets up configuration parameters for the action recognition model, including the number of frames per clip, image scaling and input size, sample video URL, file path for the video, prediction score threshold, and the averaging size for smoothing predictions. ```python NUM_FRAMES = 8 # 8 or 32. IM_SCALE = 128 # resize then crop INPUT_SIZE = 112 # input clip size: 3 x NUM_FRAMES x 112 x 112 # video sample to download sample_video_url = Urls.webcam_vid # file path to save video sample video_fpath = data_path() / "sample_video.mp4" # prediction score threshold SCORE_THRESHOLD = 0.01 # Averaging 5 latest clips to make video-level prediction (or smoothing) AVERAGING_SIZE = 5 ``` -------------------------------- ### Setup Notebook Environment and Imports (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/02_multilabel_classification.ipynb This code block sets up the notebook environment by reloading extensions and enabling inline plotting. It then imports necessary libraries from fastai, numpy, pandas, pathlib, and local utility modules for computer vision tasks. ```python # Ensure edits to libraries are loaded and plotting is shown in the notebook. %reload_ext autoreload %autoreload 2 %matplotlib inline import sys sys.path.append("../../") import warnings warnings.filterwarnings('ignore') import numpy as np import pandas as pd from pathlib import Path import scrapbook as sb # fastai import fastai from fastai.vision import ( models, ImageList, imagenet_stats, cnn_learner, partial ) # local modules from utils_cv.classification.model import ( TrainMetricsRecorder, hamming_accuracy, zero_one_accuracy, get_optimal_threshold, ) from utils_cv.classification.plot import plot_thresholds from utils_cv.classification.data import Urls from utils_cv.common.data import unzip_url from utils_cv.common.gpu import db_num_workers, which_processor print(f"Fast.ai version = {fastai.__version__}") which_processor() ``` -------------------------------- ### Run Experiment Sweep with CLI Acronyms Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/11_exploring_hyperparameters.ipynb A simplified version of the `scripts/sweep.py` command using acronyms for parameters. This command achieves the same experimental setup as the previous example but with a more concise syntax. The `--no-early-stopping` parameter is omitted as it is the default behavior. ```shell python scripts/sweep.py -lr 1e-3 1e-4 1e-5 -is 99 299 -e 10 -i -o lr_bs_test.csv ``` -------------------------------- ### Download Video File using Requests (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb Downloads a video file from a given URL using the 'requests' library and saves it to a specified file path. This is a prerequisite for processing the video locally. ```python r = requests.get(sample_video_url) open(video_fpath, 'wb').write(r.content) ``` -------------------------------- ### Initialize for Quantitative Evaluation Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/02_state_of_the_art.ipynb Initializes variables and sets up parameters for quantitatively evaluating image retrieval performance, specifically for the Recall@1 measure. It defines steps for selecting query images to manage computation time. ```python #init count = 0 labels = data_rank.train_ds.y im_paths = data_rank.train_ds.items assert len(labels) == len(im_paths) == len(dnn_features) # Use a subset of at least 500 images from the ranking set as query images. step = math.ceil(len(im_paths)/500.0) query_indices = range(len(im_paths))[::step] ``` -------------------------------- ### Create or Get Azure ML Workspace Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/20_azure_workspace_setup.ipynb This function creates a new Azure Machine Learning workspace if it doesn't exist or retrieves an existing one. It requires subscription ID, resource group, workspace name, and region as input. It handles Azure authentication and can fall back to Azure CLI credentials if direct authentication fails. ```python from utils_cv.common.azureml import get_or_create_workspace ws = get_or_create_workspace( subscription_id, resource_group, workspace_name, workspace_region) ``` -------------------------------- ### Import Libraries for Action Recognition Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb Imports necessary Python libraries for action recognition, including PyTorch, Torchvision, decord, ipywebrtc, and custom utilities from the computervision-recipes project. It also sets up the system path to access local utility modules. ```python # Regular Python libraries import sys from collections import deque # import io import requests import os from time import sleep, time from threading import Thread from IPython.display import Video # Third party tools import decord # import IPython.display # from ipywebrtc import CameraStream, ImageRecorder from ipywidgets import HBox, HTML, Layout, VBox, Widget, Label import numpy as np from PIL import Image import torch import torch.cuda as cuda import torch.nn as nn from torchvision.transforms import Compose # utils_cv sys.path.append("../../") from utils_cv.action_recognition.data import KINETICS, Urls from utils_cv.action_recognition.dataset import get_transforms from utils_cv.action_recognition.model import VideoLearner from utils_cv.action_recognition.references import transforms_video as transforms from utils_cv.common.gpu import system_info, torch_device from utils_cv.common.data import data_path system_info() ``` -------------------------------- ### Install Crowd Counting Dependencies Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/crowd_counting/README.md Installs necessary libraries for crowd counting by cloning the repository recursively and installing requirements.txt. Requires Python 3, Tensorflow 1.4.1+, and PyTorch. ```bash git clone --recursive git@github.com:microsoft/ComputerVision.git cd ComputerVision/contrib/crowd_counting/ pip install -r requirements.txt ``` -------------------------------- ### Initialize and View Default Parameters with ParameterSweeper (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/11_exploring_hyperparameters.ipynb Demonstrates how to create an instance of the ParameterSweeper and view its default training parameters. This is useful for understanding the initial configuration before making adjustments. ```python sweeper = ParameterSweeper() sweeper.parameters ``` -------------------------------- ### Build and Test Deformable Convolutional Networks Source: https://github.com/microsoft/computervision-recipes/blob/staging/utils_cv/tracking/references/fairmot/models/networks/DCNv2/README.md Scripts to build the Deformable Convolutional Networks V2 library and run example tests. The 'make.sh' script compiles the necessary components, while 'test.py' executes examples and performs gradient checks. ```bash ./make.sh # build python test.py # run examples and gradient check ``` -------------------------------- ### Configure VM User Environment Setup Script Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/vmss_builder/README.md This shell script is executed after the VMSS deployment. It clones the computer vision recipes repository, creates a conda environment, and generates multiple JupyterHub users with configurable usernames and passwords. Modify this script to set the desired number of users and their credentials before running the deployment. ```bash # Example configuration within vm_user_env_setup.sh # Set the number of users NUM_USERS=5 # Define usernames and passwords (or generate them) USERNAMES=("user1" "user2" "user3" "user4" "user5") PASSWORDS=("password1" "password2" "password3" "password4" "password5") # ... rest of the script to clone repo, create conda env, and add users ``` -------------------------------- ### Setup Azure ML Workspace Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/20_deployment_on_kubernetes.ipynb Retrieves or creates an Azure Machine Learning workspace. This function requires subscription ID, resource group, workspace name, and region. It prints workspace attributes upon successful creation or retrieval. ```python ws = get_or_create_workspace( subscription_id, resource_group, workspace_name, workspace_region ) # Print the workspace attributes print( "Workspace name: " + ws.name, "Workspace region: " + ws.location, "Subscription id: " + ws.subscription_id, "Resource group: " + ws.resource_group, sep="\n", ) ``` -------------------------------- ### Compile FairMOT DCNv2 Library (Bash) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/tracking/README.md Compiles the DCNv2 library required by FairMOT. This step is necessary for the tracking examples to function correctly. ```bash cd utils_cv/tracking/references/fairmot/models/networks/DCNv2 sh make.sh ``` -------------------------------- ### Stop Webcam and Preserve Detections Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Provides code to gracefully stop the webcam stream and close all associated widgets. It also demonstrates how to extract and preserve the detected bounding box information for later use or analysis. ```python # Stop the model and webcam Widget.close_all() ``` ```python # Preserve some of the notebook outputs detections = [ (x.label_idx, x.label_name, [(x.left, x.top), (x.right, x.bottom)]) for x in detections["det_bboxes"] ] sb.glue("detection_bounding_box", detections) ``` -------------------------------- ### Initialize Video Transformation Environment (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/10_video_transformation.ipynb Imports necessary libraries and sets up the environment for video transformations. Includes utilities for dataset handling, image processing, and GPU acceleration. ```python import sys sys.path.append("../../") import os import time import decord import matplotlib.pyplot as plt import numpy as np import warnings import shutil from sklearn.metrics import accuracy_score import torch import torch.cuda as cuda import torch.nn as nn import torchvision import urllib.request from utils_cv.action_recognition.dataset import VideoDataset, DEFAULT_MEAN, DEFAULT_STD from utils_cv.action_recognition.references.functional_video import denormalize from utils_cv.action_recognition.references.transforms_video import ( CenterCropVideo, NormalizeVideo, RandomCropVideo, RandomHorizontalFlipVideo, RandomResizedCropVideo, ResizeVideo, ToTensorVideo, ) from utils_cv.common.gpu import system_info from utils_cv.common.data import data_path system_info() warnings.filterwarnings('ignore') ``` -------------------------------- ### Import Libraries for Object Detection Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Imports necessary Python libraries for general tasks, IPython widgets, image manipulation, and torchvision for object detection. It also configures matplotlib backend for Windows and prints torchvision version and GPU information. ```python # Regular Python libraries import io import os import sys import time import urllib.request import matplotlib.pyplot as plt # IPython import scrapbook as sb from ipywebrtc import CameraStream, ImageRecorder from ipywidgets import HBox, Layout, widgets, Widget # Image from PIL import Image # TorchVision import torchvision from torchvision import transforms as T # utils_cv sys.path.append("../../") from utils_cv.common.data import data_path from utils_cv.common.gpu import which_processor, is_windows from utils_cv.detection.data import coco_labels from utils_cv.detection.model import DetectionLearner from utils_cv.detection.plot import PlotSettings, plot_boxes # Change matplotlib backend so that plots are shown for windows if is_windows(): plt.switch_backend('TkAgg') print(f"TorchVision: {torchvision.__version__}") which_processor() ``` -------------------------------- ### Stop All Widgets Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/00_webcam.ipynb Closes all active Jupyter widgets, including the webcam stream and any associated display elements, to clean up resources. ```python # Stop the model and webcam Widget.close_all() ``` -------------------------------- ### Configure Dataset and Model Parameters Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/01_training_and_evaluation_introduction.ipynb Sets up parameters for the image similarity task, including the dataset path, DNN architecture (ResNet-18), training epochs, learning rate, batch size, and image size. The 'Fridge Objects' dataset is downloaded and unzipped. ```python # Set dataset, model and evaluation parameters DATA_PATH = unzip_url(Urls.fridge_objects_path, exist_ok=True) # DNN configuration and learning parameters EPOCHS_HEAD = 4 EPOCHS_BODY = 12 LEARNING_RATE = 10* 1e-4 BATCH_SIZE = 16 ARCHITECTURE = models.resnet18 IM_SIZE = 300 ``` -------------------------------- ### Verify fast.ai Version Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/03_training_accuracy_vs_speed.ipynb This code snippet checks the installed version of the fast.ai library. It's a simple Python script that imports the library and prints its version number. ```python import fastai fastai.__version__ ``` -------------------------------- ### Start Docker Image Build Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/classification/21_deployment_on_azure_container_instances.ipynb Initiates the process of building a Docker image. This is a time-consuming operation, especially for the first build, and is essential for creating deployable environments. ```python # Since building the docker image for the first time requires a few minutes, let's start building the image ``` -------------------------------- ### Plot Detections on Image Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/detection/00_webcam.ipynb Visualizes the detected objects on the image by drawing bounding boxes around them. The bounding boxes are colored green for better visibility. ```python plot_boxes(im, detections["det_bboxes"], plot_settings=PlotSettings(rect_color=(0, 255, 0))) ``` -------------------------------- ### Predict Actions from Video File (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/00_webcam.ipynb Performs action recognition on a given video file using a pre-trained learner. It utilizes threading for non-blocking inference and allows filtering by target labels and setting score thresholds. ```python video = str(data_path()/'sample_video.mp4') learner.predict_video( video, LABELS, averaging_size=AVERAGING_SIZE, score_threshold=SCORE_THRESHOLD, target_labels=TARGET_LABELS, ) ``` -------------------------------- ### Download Pretrained I3D Models Source: https://github.com/microsoft/computervision-recipes/blob/staging/contrib/action_recognition/i3d/README.md This bash script downloads the pre-trained I3D models. These models are typically used as a starting point for fine-tuning on specific datasets like HMDB-51. ```bash bash download_models.sh ``` -------------------------------- ### Compute and Print Median Rank Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/similarity/01_training_and_evaluation_introduction.ipynb Calculates the median rank of the positive example across all comparative sets using NumPy. It also computes and prints the median rank and a random chance rank for comparison. ```python # Compute the median rank of the positive example over all comparative sets ranks = positive_image_ranks(comparative_sets) median_rank = np.median(ranks) random_rank = np.median([(len(cs.neg_im_paths)+1)/2.0 for cs in comparative_sets]) print(f"The positive example ranks {median_rank}, as a median, \ across our {len(ranks)} comparative sets. Random chance rank is {random_rank}") ``` -------------------------------- ### Download Sample Video (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/action_recognition/10_video_transformation.ipynb Downloads a sample video file from a specified URL and saves it to the local VIDEO_PATH. This is used for demonstrating video transformations. ```python url = ( "https://cvbp-secondary.z19.web.core.windows.net/datasets/action_recognition/drinking.mp4" ) with urllib.request.urlopen(url) as response, open(VIDEO_PATH, "wb") as out_file: shutil.copyfileobj(response, out_file) ``` -------------------------------- ### Initialize and Load FairMOT Model (Python) Source: https://github.com/microsoft/computervision-recipes/blob/staging/scenarios/tracking/02_mot_challenge.ipynb Initializes the tracking model using the TrackingLearner class and loads a pre-trained FairMOT model weights from a specified file path. This prepares the model for evaluation on the MOT Challenge dataset. ```python tracker = TrackingLearner(None, BASELINE_MODEL) ```