Skip to content

Commit c1a99b7

Browse files
andfoyr-zenine
andauthored
PR: Enable libPNG support (#2379)
* Add libpng requirement into conda recipe * Try to install libjpeg-turbo * Add PNG reading capabilities * Remove newline * Add image extension to compilation instructions * Include png functions as part of the main library * Update CMakeLists * Detect if building on conda-build * Debug * More debug messages * Print globbed libreries * Print globbed libreries * Point to correct PNG path * Remove libJPEG preventively * Debug extension loading * Link libpng explicitly * Link with PNG * Add PNG reading capabilities * Add libpng requirement into conda recipe * Try to install libjpeg-turbo * Remove newline * Add image extension to compilation instructions * Include png functions as part of the main library * Update CMakeLists * Detect if building on conda-build * Debug * More debug messages * Print globbed libreries * Print globbed libreries * Point to correct PNG path * Remove libJPEG preventively * Debug extension loading * Link libpng explicitly * Link with PNG * Install libpng on conda-based wheel distributions * Add -y flag * Add -y flag to yum * Locate LibPNG on windows conda * Remove empty else * Copy libpng16.so * Copy dylib on Mac * Improve check on Windows * Try to install ninja using conda on windows * Use libpng on Windows * Package lib on windows wheel * Point library to the correct place * Include binaries as part of wheel * Copy libpng.so on linux * Look for png.h on Windows when using conda-build * Do not skip png tests on Mac/Win * Restore libjpeg-turbo * Install jpeg-turbo on wheel distributions * Install libjpeg-turbo from conda-forge on wheel distributions * Do not pull av on conda-build * Add pillow disclaimer * Vendors libjpeg-turbo 2.0.4 * Merge JPEG work * Remove submodules * Regenerate circle config * Fix style issues * Fix C++ style issues * More style corrections * Add JPEG-turbo to linking libraries * More style corrections * More style corrections * More style corrections * Install libjpeg-turbo-devel * Install libturbo-jpeg on typing pipeline * Update Circle template * Windows and Unix turbojpeg have the same linking name * Install turbojpeg-devel instead of libjpeg-turbo * Copy TurboJPEG binaries to wheel * Move test image * Move back test image * Update JPEG test path * Remove dot from extension * Move image functions to extension * Use stdout arg in subprocess * Disable image extension if libpng or turbojpeg are not found * Append libpng stdout * Prevent list appending on lists * Minor path correction * Minor error correction * Add linking flags * Style issues correction * Address minor review corrections * Refactor library search * Restore access index * Fix JPEG tests * Update libpng version in Travis * Add -y flag * Remove dot * Update libpng using apt * Check libpng version * Change libturbojpeg binary * Update import * Change call * Restore av in conda recipe * Minor error correction * Remove unused comment in travis.yml * Update README * Fix missing links * Remove fixes for 16.04 * Remove JPEG-related code * Remove installation references to turbojpeg * Remove further references to turbojpeg * Fix c++ style issues * Fix c++ style issues * Fix libpng-config include flag parsing * Remove conda-forge * Remove include dirs from main extension * Do not pass extra include and library paths to main torchvision extension * Add libpng to environment.yml * Remove inexistent imports * Add instructions regarding environment variables to README Co-authored-by: Ryad ZENINE <[email protected]>
1 parent fa6af6d commit c1a99b7

File tree

19 files changed

+402
-10
lines changed

19 files changed

+402
-10
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ jobs:
107107
- checkout
108108
- run:
109109
command: |
110+
sudo apt-get update -y
110111
pip install --user --progress-bar off numpy mypy
111112
pip install --user --progress-bar off --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
112113
pip install --user --progress-bar off --editable .

.circleci/config.yml.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ jobs:
107107
- checkout
108108
- run:
109109
command: |
110+
sudo apt-get update -y
110111
pip install --user --progress-bar off numpy mypy
111112
pip install --user --progress-bar off --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
112113
pip install --user --progress-bar off --editable .

.circleci/unittest/linux/scripts/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- pytest-cov
77
- codecov
88
- pip
9+
- libpng
910
- ca-certificates
1011
- pip:
1112
- future

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ htmlcov
2121
*.swo
2222
gen.yml
2323
.mypy_cache
24+
.vscode/
25+
*.orig

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313

1414
before_install:
1515
- sudo apt-get update
16+
- sudo apt-get install -y libpng-dev
1617
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
1718
- bash miniconda.sh -b -p $HOME/miniconda
1819
- export PATH="$HOME/miniconda/bin:$PATH"

CMakeLists.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@ if(WITH_CUDA)
1111
endif()
1212

1313
find_package(Python3 COMPONENTS Development)
14+
1415
find_package(Torch REQUIRED)
16+
find_package(PNG REQUIRED)
17+
1518

1619
file(GLOB HEADERS torchvision/csrc/*.h)
17-
file(GLOB OPERATOR_SOURCES torchvision/csrc/cpu/*.h torchvision/csrc/cpu/*.cpp torchvision/csrc/*.cpp)
20+
file(GLOB IMAGE_HEADERS torchvision/csrc/cpu/image/*.h)
21+
file(GLOB IMAGE_SOURCES torchvision/csrc/cpu/image/*.cpp)
22+
file(GLOB OPERATOR_SOURCES torchvision/csrc/cpu/*.h torchvision/csrc/cpu/*.cpp ${IMAGE_HEADERS} ${IMAGE_SOURCES} ${HEADERS} torchvision/csrc/*.cpp)
1823
if(WITH_CUDA)
1924
file(GLOB OPERATOR_SOURCES ${OPERATOR_SOURCES} torchvision/csrc/cuda/*.h torchvision/csrc/cuda/*.cu)
2025
endif()
2126
file(GLOB MODELS_HEADERS torchvision/csrc/models/*.h)
2227
file(GLOB MODELS_SOURCES torchvision/csrc/models/*.h torchvision/csrc/models/*.cpp)
2328

24-
add_library(${PROJECT_NAME} SHARED ${MODELS_SOURCES} ${OPERATOR_SOURCES})
25-
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} Python3::Python)
29+
add_library(${PROJECT_NAME} SHARED ${MODELS_SOURCES} ${OPERATOR_SOURCES} ${IMAGE_SOURCES})
30+
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} ${PNG_LIBRARY} Python3::Python)
31+
# target_link_libraries(${PROJECT_NAME} PRIVATE ${PNG_LIBRARY} Python3::Python)
2632
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME TorchVision)
2733

2834
target_include_directories(${PROJECT_NAME} INTERFACE
29-
$<BUILD_INTERFACE:${HEADERS}>
35+
$<BUILD_INTERFACE:${HEADERS}:${PNG_INCLUDE_DIR}>
3036
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
3137

3238
include(GNUInstallDirs)
@@ -61,7 +67,7 @@ install(FILES
6167
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cpu)
6268
if(WITH_CUDA)
6369
install(FILES
64-
torchvision/csrc/cuda/vision_cuda.h
70+
torchvision/csrc/cuda/vision_cuda.h
6571
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cuda)
6672
endif()
6773
install(FILES ${MODELS_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/models)

README.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ Torchvision currently supports the following image backends:
7878

7979
* `accimage`_ - if installed can be activated by calling :code:`torchvision.set_image_backend('accimage')`
8080

81+
* `libpng`_ - can be installed via conda :code:`conda install libpng` or any of the package managers for debian-based and RHEL-based Linux distributions.
82+
83+
**Notes:** ``libpng`` must be available at compilation time in order to be available. Make sure that it is available on the standard library locations,
84+
otherwise, add the include and library paths in the environment variables ``TORCHVISION_INCLUDE`` and ``TORCHVISION_LIBRARY``, respectively.
85+
86+
.. _libpng : http://www.libpng.org/pub/png/libpng.html
8187
.. _Pillow : https://python-pillow.org/
8288
.. _Pillow-SIMD : https://github.com/uploadcare/pillow-simd
8389
.. _accimage: https://github.com/pytorch/accimage
8490

8591
C++ API
8692
=======
87-
TorchVision also offers a C++ API that contains C++ equivalent of python models.
93+
TorchVision also offers a C++ API that contains C++ equivalent of python models.
8894

8995
Installation From source:
9096

@@ -94,7 +100,7 @@ Installation From source:
94100
cd build
95101
# Add -DWITH_CUDA=on support for the CUDA if needed
96102
cmake ..
97-
make
103+
make
98104
make install
99105
100106
Once installed, the library can be accessed in cmake (after properly configuring ``CMAKE_PREFIX_PATH``) via the :code:`TorchVision::TorchVision` target:

packaging/build_wheel.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ setup_wheel_python
1010
pip_install numpy pyyaml future ninja
1111
setup_pip_pytorch_version
1212
python setup.py clean
13+
14+
# Copy binaries to be included in the wheel distribution
15+
if [[ "$(uname)" == Darwin || "$OSTYPE" == "msys" ]]; then
16+
python_exec="$(which python)"
17+
bin_path=$(dirname $python_exec)
18+
env_path=$(dirname $bin_path)
19+
if [[ "$(uname)" == Darwin ]]; then
20+
# Include LibPNG
21+
cp "$env_path/lib/libpng16.dylib" torchvision
22+
else
23+
# Include libPNG
24+
cp "$bin_path/Library/lib/libpng.lib" torchvision
25+
fi
26+
else
27+
# Include LibPNG
28+
cp "/usr/lib64/libpng.so" torchvision
29+
fi
30+
1331
if [[ "$OSTYPE" == "msys" ]]; then
1432
IS_WHEEL=1 "$script_dir/windows/internal/vc_env_helper.bat" python setup.py bdist_wheel
1533
else

packaging/pkg_helpers.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ setup_wheel_python() {
170170
conda env remove -n "env$PYTHON_VERSION" || true
171171
conda create -yn "env$PYTHON_VERSION" python="$PYTHON_VERSION"
172172
conda activate "env$PYTHON_VERSION"
173+
# Install libPNG from Anaconda (defaults)
174+
conda install libpng -y
173175
else
176+
# Install native CentOS libPNG
177+
yum install -y libpng-devel
174178
case "$PYTHON_VERSION" in
175179
2.7)
176180
if [[ -n "$UNICODE_ABI" ]]; then

packaging/torchvision/conda_build_config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
channel_sources:
2+
- defaults
3+
14
blas_impl:
25
- mkl # [x86_64]
36
c_compiler:

0 commit comments

Comments
 (0)