CoolFace
Apppublic

pytorch/3D_ResNet

sourceHugging Faceupdated 5y agoView on Hugging Face
4likes
app.py106 linesDownload Raw Back to root
1import torch2# Choose the `slow_r50` model 3model = torch.hub.load('facebookresearch/pytorchvideo', 'slow_r50', pretrained=True)4import json5import urllib6from pytorchvideo.data.encoded_video import EncodedVideo7 8from torchvision.transforms import Compose, Lambda9from torchvision.transforms._transforms_video import (10    CenterCropVideo,11    NormalizeVideo,12)13from pytorchvideo.transforms import (14    ApplyTransformToKey,15    ShortSideScale,16    UniformTemporalSubsample17)18 19import gradio as gr20# Set to GPU or CPU21device = "cpu"22model = model.eval()23model = model.to(device)24json_url = "https://dl.fbaipublicfiles.com/pyslowfast/dataset/class_names/kinetics_classnames.json"25json_filename = "kinetics_classnames.json"26try: urllib.URLopener().retrieve(json_url, json_filename)27except: urllib.request.urlretrieve(json_url, json_filename)28with open(json_filename, "r") as f:29    kinetics_classnames = json.load(f)30 31# Create an id to label name mapping32kinetics_id_to_classname = {}33for k, v in kinetics_classnames.items():34    kinetics_id_to_classname[v] = str(k).replace('"', "")35side_size = 25636mean = [0.45, 0.45, 0.45]37std = [0.225, 0.225, 0.225]38crop_size = 25639num_frames = 840sampling_rate = 841frames_per_second = 3042 43# Note that this transform is specific to the slow_R50 model.44transform =  ApplyTransformToKey(45    key="video",46    transform=Compose(47        [48            UniformTemporalSubsample(num_frames),49            Lambda(lambda x: x/255.0),50            NormalizeVideo(mean, std),51            ShortSideScale(52                size=side_size53            ),54            CenterCropVideo(crop_size=(crop_size, crop_size))55        ]56    ),57)58 59# The duration of the input clip is also specific to the model.60clip_duration = (num_frames * sampling_rate)/frames_per_second61url_link = "https://dl.fbaipublicfiles.com/pytorchvideo/projects/archery.mp4"62video_path = 'archery.mp4'63try: urllib.URLopener().retrieve(url_link, video_path)64except: urllib.request.urlretrieve(url_link, video_path)65# Select the duration of the clip to load by specifying the start and end duration66# The start_sec should correspond to where the action occurs in the video67def inference(in_vid):68    start_sec = 069    end_sec = start_sec + clip_duration70 71    # Initialize an EncodedVideo helper class and load the video72    video = EncodedVideo.from_path(in_vid)73 74    # Load the desired clip75    video_data = video.get_clip(start_sec=start_sec, end_sec=end_sec)76 77    # Apply a transform to normalize the video input78    video_data = transform(video_data)79 80    # Move the inputs to the desired device81    inputs = video_data["video"]82    inputs = inputs.to(device)83    # Pass the input clip through the model84    preds = model(inputs[None, ...])85 86    # Get the predicted classes87    post_act = torch.nn.Softmax(dim=1)88    preds = post_act(preds)89    pred_classes = preds.topk(k=5).indices[0]90 91    # Map the predicted classes to the label names92    pred_class_names = [kinetics_id_to_classname[int(i)] for i in pred_classes]93    return "%s" % ", ".join(pred_class_names)94 95inputs = gr.inputs.Video(label="Input Video")96outputs = gr.outputs.Textbox(label="Top 5 predicted labels")97 98title = "3D RESNET"99description = "demo for 3D RESNET, Resnet Style Video classification networks pretrained on the Kinetics 400 dataset. To use it, simply upload your video, or click one of the examples to load them. Read more at the links below."100article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1812.03982'>SlowFast Networks for Video Recognition</a> | <a href='https://github.com/facebookresearch/pytorchvideo'>Github Repo</a></p>"101 102examples = [103    ['archery.mp4']104]105 106gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(enable_queue=True)