homebrew-core/Formula/torchvision.rb

125 lines
4.7 KiB
Ruby

class Torchvision < Formula
include Language::Python::Virtualenv
desc "Datasets, transforms, and models for computer vision"
homepage "https://github.com/pytorch/vision"
url "https://github.com/pytorch/vision/archive/refs/tags/v0.14.1.tar.gz"
sha256 "ced67e1cf1f97e168cdf271851a4d0b6d382ab7936e7bcbb39aaa87239c324b6"
license "BSD-3-Clause"
livecheck do
url :stable
regex(/^v?(\d+(?:\.\d+)+)$/i)
end
bottle do
sha256 cellar: :any, arm64_ventura: "105fefd0fba873baa04bb18d258764f2c19381f8df4898275f4f011bb3cc5093"
sha256 cellar: :any, arm64_monterey: "1535824bd57631ff8233c97973adf6e5aa580d0480f1075a1abeff42a6a824f4"
sha256 cellar: :any, arm64_big_sur: "64468c7a188dc9e582ffbaa84f7676c0ab4429364ae0a4ff839c3b97666d5beb"
sha256 cellar: :any, ventura: "ab654adfe800fb5651a631818801a603b942ff24b691ec1d48cf03b88e4b8a22"
sha256 cellar: :any, monterey: "92dbfc9b11c8b410b8f3adfb3bff5bd1468865179e10ede6154a74be8d12bab2"
sha256 cellar: :any, big_sur: "d494ddeddff9c94d692dc0bbc65881ca5a6ecb544884d8122b0aa100e427f90e"
sha256 cellar: :any_skip_relocation, x86_64_linux: "7ee8ead5885d4f0e4cb5c4b9d1155e72111eb651998000cd8a5ab51654de055a"
end
depends_on "cmake" => :build
depends_on "ninja" => :build
depends_on "python@3.11" => [:build, :test]
depends_on "jpeg-turbo"
depends_on "libpng"
depends_on "numpy"
depends_on "pillow"
depends_on "python-typing-extensions"
depends_on "pytorch"
on_macos do
depends_on "libomp"
end
resource "certifi" do
url "https://files.pythonhosted.org/packages/37/f7/2b1b0ec44fdc30a3d31dfebe52226be9ddc40cd6c0f34ffc8923ba423b69/certifi-2022.12.7.tar.gz"
sha256 "35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3"
end
resource "charset-normalizer" do
url "https://files.pythonhosted.org/packages/96/d7/1675d9089a1f4677df5eb29c3f8b064aa1e70c1251a0a8a127803158942d/charset-normalizer-3.0.1.tar.gz"
sha256 "ebea339af930f8ca5d7a699b921106c6e29c617fe9606fa7baa043c1cdae326f"
end
resource "idna" do
url "https://files.pythonhosted.org/packages/8b/e1/43beb3d38dba6cb420cefa297822eac205a277ab43e5ba5d5c46faf96438/idna-3.4.tar.gz"
sha256 "814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"
end
resource "requests" do
url "https://files.pythonhosted.org/packages/a5/61/a867851fd5ab77277495a8709ddda0861b28163c4613b011bc00228cc724/requests-2.28.1.tar.gz"
sha256 "7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983"
end
resource "urllib3" do
url "https://files.pythonhosted.org/packages/c2/51/32da03cf19d17d46cce5c731967bf58de9bd71db3a379932f53b094deda4/urllib3-1.26.13.tar.gz"
sha256 "c083dd0dce68dbfbe1129d5271cb90f9447dea7d52097c6e0126120c521ddea8"
end
def install
system "cmake", "-S", ".", "-B", "build", *std_cmake_args
system "cmake", "--build", "build"
system "cmake", "--install", "build"
jpeg = Formula["jpeg-turbo"]
inreplace "setup.py",
"(jpeg_found, jpeg_conda, jpeg_include, jpeg_lib) = find_library(\"jpeglib\", vision_include)",
"(jpeg_found, jpeg_conda, jpeg_include, jpeg_lib) = (True, False, \"#{jpeg.include}\", \"#{jpeg.lib}\")"
inreplace "pyproject.toml",
'requires = ["setuptools", "torch", "wheel"]',
'requires = ["setuptools", "wheel"]'
virtualenv_install_with_resources using: "python@3.11"
pkgshare.install "examples"
end
test do
# test that C++ libraries are available
(testpath/"test.cpp").write <<~EOS
#include <assert.h>
#include <torch/script.h>
#include <torch/torch.h>
#include <torchvision/vision.h>
int main() {
auto& ops = torch::jit::getAllOperatorsFor(torch::jit::Symbol::fromQualString("torchvision::nms"));
assert(ops.size() == 1);
}
EOS
pytorch = Formula["pytorch"]
openmp_flags = if OS.mac?
libomp = Formula["libomp"]
%W[
-Xpreprocessor -fopenmp
-I#{libomp.opt_include}
-L#{libomp.opt_lib} -lomp
]
else
%w[-fopenmp]
end
system ENV.cxx, "-std=c++14", "test.cpp", "-o", "test", *openmp_flags,
"-I#{pytorch.opt_include}",
"-I#{pytorch.opt_include}/torch/csrc/api/include",
"-L#{pytorch.opt_lib}", "-ltorch", "-ltorch_cpu", "-lc10",
"-L#{lib}", "-ltorchvision"
system "./test"
# test that the `torchvision` Python module is available
cp test_fixtures("test.png"), "test.png"
system libexec/"bin/python", "-c", <<~EOS
import torch
import torchvision
t = torchvision.io.read_image("test.png")
assert isinstance(t, torch.Tensor)
EOS
end
end