《Keras 3 :使用 Vision Transformers 进行物体检测》:此文为AI自动翻译

news/2025/2/25 17:36:30

《Keras 3 :使用 Vision Transformers 进行物体检测》

作者:Karan V. Dave
创建日期:2022 年 3 月 27
日最后修改时间:2023 年 11 月 20
日描述:使用 Vision Transformer 进行对象检测的简单 Keras 实现。

(i) 此示例使用 Keras 3

 在 Colab 中查看 

 GitHub 源


介绍

Alexey Dosovitskiy 等人的文章 Vision Transformer (ViT) 架构。 表明直接应用于图像序列的纯 transformer 补丁可以在对象检测任务中表现良好。

在这个 Keras 示例中,我们实现了一个对象检测 ViT 我们在加州理工学院 101 数据集上对其进行训练,以检测给定图像中的飞机。


导入和设置

import os

os.environ["KERAS_BACKEND"] = "jax"  # @param ["tensorflow", "jax", "torch"]


import numpy as np
import keras
from keras import layers
from keras import ops
import matplotlib.pyplot as plt
import numpy as np
import cv2
import os
import scipy.io
import shutil

准备数据集

我们使用加州理工学院 101 数据集。

# Path to images and annotations
path_images = "./101_ObjectCategories/airplanes/"
path_annot = "./Annotations/Airplanes_Side_2/"

path_to_downloaded_file = keras.utils.get_file(
    fname="caltech_101_zipped",
    origin="https://data.caltech.edu/records/mzrjq-6wc02/files/caltech-101.zip",
    extract=True,
    archive_format="zip",  # downloaded file format
    cache_dir="/",  # cache and extract in current directory
)
download_base_dir = os.path.dirname(path_to_downloaded_file)

# Extracting tar files found inside main zip file
shutil.unpack_archive(
    os.path.join(download_base_dir, "caltech-101", "101_ObjectCategories.tar.gz"), "."
)
shutil.unpack_archive(
    os.path.join(download_base_dir, "caltech-101", "Annotations.tar"), "."
)

# list of paths to images and annotations
image_paths = [
    f for f in os.listdir(path_images) if os.path.isfile(os.path.join(path_images, f))
]
annot_paths = [
    f for f in os.listdir(path_annot) if os.path.isfile(os.path.join(path_annot, f))
]

image_paths.sort()
annot_paths.sort()

image_size = 224  # resize input images to this size

images, targets = [], []

# loop over the annotations and images, preprocess them and store in lists
for i in range(0, len(annot_paths)):
    # Access bounding box coordinates
    annot = scipy.io.loadmat(path_annot + annot_paths[i])["box_coord"][0]

    top_left_x, top_left_y = annot[2], annot[0]
    bottom_right_x, bottom_right_y = annot[3], annot[1]

    image = keras.utils.load_img(
        path_images + image_paths[i],
    )
    (w, h) = image.size[:2]

    # resize images
    image = image.resize((image_size, image_size))

    # convert image to array and append to list
    images.append(keras.utils.img_to_array(image))

    # apply relative scaling to bounding boxes as per given image and append to list
    targets.append(
        (
            float(top_left_x) / w,
            float(top_left_y) / h,
            float(bottom_right_x) / w,
            float(bottom_right_y) / h,
        )
    )

# Convert the list to numpy array, split to train and test dataset
(x_train), (y_train) = (
    np.asarray(images[: int(len(images) * 0.8)]),
    np.asarray(targets[: int(len(targets) * 0.8)]),
)
(x_test), (y_test) = (
    np.asarray(images[int(len(images

http://www.niftyadmin.cn/n/5865772.html

相关文章

Helix——Figure 02发布的通用人形机器人控制VLA:不用微调即可做多个任务的快与慢双系统,让两个机器人协作干活(含清华HiRT详解)

前言 过去一周,我花了很大的心思、力气,把deepseek的GRPO、MLA算法的代码解析通透,比如GRPO与PPO的详细对比,再比如MLA中,图片 公式 代码的一一对应,详见此专栏《火爆全球的DeepSeek系列模型》 2.20日晚&…

11_17日项目笔记——制作“全屏播放页面”

创建项目: 项目需求:要实现的页面效果 使用相对布局(Relative): 所需图片资源需要请点击我https://download.csdn.net/download/m0_73992525/90009094?spm1001.2014.3001.5503 修改默认启动页面 此时应用启动默认加载…

go:运行第一个go语言程序

1.如何创建go语言编辑界面 2.案例一实现简单打印“hello worlg”: package main import "fmt" func main() { for i : 0; i < 10; { if i < 0 { continue } fmt.Println("hello world") i } } 运行结果&#xff1a; PS D:\demo2> go mod ini…

DeepSeek 助力 Vue 开发:打造丝滑的滚动动画(Scroll Animations)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

elementUI方案汇总

1&#xff1a;el-table 设置固定列&#xff0c;横向滚动条在固定列的位置上无法滚动的问题 问题原因&#xff1a;固定列将下方的滚动条盖住了&#xff0c;无法触发滚动条的滚动。 解决方法&#xff1a;改变固定列的样式&#xff0c;给固定列设置下边距&#xff0c;下边距的大小…

【Godot4.3】自定义圆角容器

概述 Godot控件想要完全实现现代UI风格&#xff0c;需要进行大量的自定义组件设计。本篇就依托于笔者自己对现代UI设计中的圆角面板元素模仿来制作圆角容器组件。 圆角容器 圆角元素在现代的扁平UI设计中非常常见&#xff0c;在Godot中可以通过改进PanelContainer来或者自定…

【IEEE出版,往届会后3个月EI检索 | 西华大学主办 | 中英文期刊、SCI期刊推荐】第四届能源、电力与电气国际学术会议(ICEPET 2025)

第四届能源、电力与电气国际学术会议&#xff08;ICEPET 2025&#xff09;由西华大学主办&#xff0c;西华大学能源与动力工程学院、西华大学电气与电子信息学院、西华大学航空航天学院、流体及动力机械教育部重点实验室、流体机械及工程四川省重点实验室、四川省水电能源动力装…

2025年02月24日Github流行趋势

项目名称&#xff1a;mastra 项目地址url&#xff1a;https://github.com/mastra-ai/mastra 项目语言&#xff1a;TypeScript 历史star数&#xff1a;5735 今日star数&#xff1a;1140 项目维护者&#xff1a;adeleke5140, abhiaiyer91, TheIsrael1, adeniyii, Joshuafolorunsh…