当前位置: 首页 > news >正文

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

《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

相关文章:

  • GitCode 助力至善云学:构建智慧教育平台
  • 053 性能压测 单机锁 setnx
  • buu-[OGeek2019]babyrop-好久不见41
  • C++ 设计模式-状态模式
  • 在s32ds for platform平台debug编译能正常编译,但是切换到release编译时报错
  • DeepSeek vs ChatGPT:AI 领域的华山论剑,谁主沉浮?
  • Uniapp判断设备是安卓还是 iOS,并调用不同的方法
  • 了解大数据
  • 虚拟机的创建及配置
  • Lineageos 22.1(Android 15)Launcer简单调整初始化配置
  • Qt学习(六) 软件启动界面 ,注册表使用 ,QT绘图, 视图和窗口绘图,Graphics View绘图框架:简易CAD
  • 数据库索引:缺点与类型全解析
  • CSS 布局技术深度解析:从传统到现代的核心布局方案
  • Arm64架构CentOS7服务器搭建Fabric环境
  • RPC:分布式系统的通信桥梁
  • 毕业项目推荐:基于yolov8/yolov5/yolo11的番茄成熟度检测识别系统(python+卷积神经网络)
  • 华为S系列交换机安全加固解决方案
  • Secured Finance携手Axelar及Squid提升流动性,迎接USDFC主网
  • 宇树科技13家核心零部件供应商梳理!
  • chmod命令修改rwxr-x---只读权限为rwxr-xr-x
  • 复旦大学艺术馆开馆:以当代视角再看文科文脉
  • 特朗普公开“怼”库克:苹果不应在印度生产手机
  • 阿里上季度营收增7%:淘天营收创新高,AI产品营收连续七个季度三位数增长
  • 党建评:对违规宴饮等问题要坚决露头就打
  • 国台办:民进党当局刻意刁难大陆配偶,这是不折不扣的政治迫害
  • 沈阳一超市疑借领养名义烹食流浪狗,当地市监局:已收到多起投诉