Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Enable libPNG support #2379

Merged
merged 120 commits into from
Jul 2, 2020
Merged

PR: Enable libPNG support #2379

merged 120 commits into from
Jul 2, 2020

Conversation

andfoy
Copy link
Contributor

@andfoy andfoy commented Jul 1, 2020

This PR fixes the regressions introduced in #2301

setup.py Outdated

ext_modules = [
extension(
'torchvision._C',
sources,
include_dirs=include_dirs,
library_dirs=library_dirs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is not needed anymore

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it is, given that we changed include_dirs, good to double-check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be good to include if future functions require other libraries?

@codecov
Copy link

codecov bot commented Jul 1, 2020

Codecov Report

Merging #2379 into master will decrease coverage by 0.07%.
The diff coverage is 51.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2379      +/-   ##
==========================================
- Coverage   70.73%   70.66%   -0.08%     
==========================================
  Files          93       94       +1     
  Lines        7768     7799      +31     
  Branches     1199     1204       +5     
==========================================
+ Hits         5495     5511      +16     
- Misses       1910     1925      +15     
  Partials      363      363              
Impacted Files Coverage Δ
torchvision/io/__init__.py 100.00% <ø> (ø)
torchvision/io/image.py 51.61% <51.61%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa6af6d...84dad44. Read the comment docs.

@@ -6,6 +6,7 @@ dependencies:
- pytest-cov
- codecov
- pip
- libpng
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same is needed for Windows in https://github.com/pytorch/vision/blob/master/.circleci/unittest/windows/scripts/environment.yml ? Not sure why tests are not failing there

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests seem to be passing, although I'm not sure why we don't need to add libpng to https://github.com/pytorch/vision/blob/master/.circleci/unittest/windows/scripts/environment.yml as well.

Let's merge and see if all the builds pass on master.

@fmassa fmassa merged commit c1a99b7 into pytorch:master Jul 2, 2020
@fmassa fmassa mentioned this pull request Jul 2, 2020
fmassa added a commit that referenced this pull request Jul 2, 2020
fmassa added a commit that referenced this pull request Jul 2, 2020
image_link_flags = []

# Locating libPNG
libpng = distutils.spawn.find_executable('libpng-config')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this locally on a Windows box after doing conda install libpng and it returns None.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest changing this to pngfix. as it is more robust and is listed in the official page. http://www.linuxfromscratch.org/blfs/view/svn/general/libpng.html

Copy link
Contributor

@peterjc123 peterjc123 Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
libpng = distutils.spawn.find_executable('libpng-config')
libpng = distutils.spawn.find_executable('libpng-config')
pngfix = distutils.spawn.find_executable('pngfix')

png_version = parse_version(png_version)
if png_version >= parse_version("1.6.0"):
print('Building torchvision with PNG image support')
png_lib = subprocess.run([libpng, '--libdir'],
Copy link
Contributor

@peterjc123 peterjc123 Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I saw this. Unluckily, on Windows, libpng-config doesn't exist. But there is a CMake configuration. Reading that file, it seems that you'll only need to link against libpng.lib and set the corresponding include directory include\libpng16.

png_found = libpng is not None
image_macros += [('PNG_FOUND', str(int(png_found)))]
print('PNG found: {0}'.format(png_found))
if png_found:
Copy link
Contributor

@peterjc123 peterjc123 Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if png_found:
if png_found:
if libpng is not None:
# Unix / Mac
else:
# Windows
png_lib = os.path.join(os.path.dirname(os.path.dirname(pngfix)), 'lib')
png_include = os.path.join(os.path.dirname(os.path.dirname(pngfix)), 'include', 'libpng16')


# Locating libPNG
libpng = distutils.spawn.find_executable('libpng-config')
png_found = libpng is not None
Copy link
Contributor

@peterjc123 peterjc123 Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
png_found = libpng is not None
png_found = libpng is not None or pngfix is not None

// If we are in a Windows environment, we need to define
// initialization functions for the _custom_ops extension
#ifdef _WIN32
#if PY_MAJOR_VERSION < 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It is not no longer needed as PyTorch has dropped Python 2 support.

@@ -243,7 +359,9 @@ def run(self):

# Package info
packages=find_packages(exclude=('test',)),

package_data={
package_name: ['*.lib', '*.dylib', '*.so']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and .dll I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package_name: ['*.lib', '*.dylib', '*.so']
package_name: ['*.dll', '*.dylib', '*.so']


class ImageTester(unittest.TestCase):
def test_read_png(self):
for img_path in get_images(IMAGE_DIR, "png"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.png

self.assertEqual(img_lpng, img_pil)

def test_decode_png(self):
for img_path in get_images(IMAGE_DIR, "png"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.png

@peterjc123
Copy link
Contributor

@andfoy I made a few suggestions in this PR. Maybe you could take a look.

cp "$env_path/lib/libpng16.dylib" torchvision
else
# Include libPNG
cp "$bin_path/Library/lib/libpng.lib" torchvision
Copy link
Contributor

@peterjc123 peterjc123 Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cp "$bin_path/Library/lib/libpng.lib" torchvision
cp "$bin_path/Library/bin/libpng16.dll" torchvision

de-vri-es pushed a commit to fizyr-forks/torchvision that referenced this pull request Aug 4, 2020
* 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 <r.zenine@gmail.com>
de-vri-es pushed a commit to fizyr-forks/torchvision that referenced this pull request Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants