From 5b06225339b5510a2585636b4389806999847f42 Mon Sep 17 00:00:00 2001 From: John Haddon Date: Sat, 9 Nov 2024 22:07:49 +0000 Subject: [PATCH] TensorToImage : Fix x/y mixup --- python/GafferMLTest/TensorToImageTest.py | 22 +++++++++++++++++++++- src/GafferML/TensorToImage.cpp | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/python/GafferMLTest/TensorToImageTest.py b/python/GafferMLTest/TensorToImageTest.py index 63c523a5293..b7a2b05d7a5 100644 --- a/python/GafferMLTest/TensorToImageTest.py +++ b/python/GafferMLTest/TensorToImageTest.py @@ -43,9 +43,10 @@ import Gaffer import GafferTest import GafferImage +import GafferImageTest import GafferML -class TensorToImageTest( GafferTest.TestCase ) : +class TensorToImageTest( GafferImageTest.ImageTestCase ) : def testNoInput( self ) : @@ -106,5 +107,24 @@ def testNonMatchingChannels( self ) : with self.assertRaisesRegex( RuntimeError, 'Invalid channel "G' ) : tensorToImage["out"].channelData( "G", imath.V2i( 0 ) ) + def testRoundTripWithImageToTensor( self ) : + + image = GafferImage.Checkerboard() + + imageToTensor = GafferML.ImageToTensor() + imageToTensor["image"].setInput( image["out"] ) + imageToTensor["channels"].setInput( image["out"]["channelNames"]) + + tensorToImage = GafferML.TensorToImage() + tensorToImage["tensor"].setInput( imageToTensor["tensor"] ) + tensorToImage["channels"].setInput( image["out"]["channelNames"]) + + self.assertImagesEqual( tensorToImage["out"], image["out"] ) + + imageToTensor["interleaveChannels"].setValue( True ) + tensorToImage["interleavedChannels"].setValue( True ) + + self.assertImagesEqual( tensorToImage["out"], image["out"] ) + if __name__ == "__main__": unittest.main() diff --git a/src/GafferML/TensorToImage.cpp b/src/GafferML/TensorToImage.cpp index d51ec9d77cf..3f2ec34716a 100644 --- a/src/GafferML/TensorToImage.cpp +++ b/src/GafferML/TensorToImage.cpp @@ -92,14 +92,14 @@ ImageShape imageShape( const Tensor *tensor, bool interleavedChannels ) if( interleavedChannels ) { return { - Box2i( V2i( 0 ), V2i( (int)shape[i], (int)shape[i+1] ) ), + Box2i( V2i( 0 ), V2i( (int)shape[i+1], (int)shape[i] ) ), (size_t)shape[i+2] }; } else { return { - Box2i( V2i( 0 ), V2i( (int)shape[i+1], (int)shape[i+2] ) ), + Box2i( V2i( 0 ), V2i( (int)shape[i+2], (int)shape[i+1] ) ), (size_t)shape[i] }; }