question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transforms.Lambda fails on class functions

See original GitHub issue

transforms.Lambda appears to fail when a function that is passed to it is a class function:

import torchvision

class MyClass(object):

    def my_transform(self, img):
        return img

def my_transform(img):
    return img

t = MyClass()

print('Pure function works')
torchvision.transforms.Lambda(my_transform)
print('Class function fails')
torchvision.transforms.Lambda(t.my_transform)

Output:

Pure function works
Class function fails
Traceback (most recent call last):
  File "test_pytorch.py", line 17, in <module>
    torchvision.transforms.Lambda(t.my_transform)
  File "/braintree/home/qbilius/libs/miniconda3/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 279, in __init__
    assert isinstance(lambd, types.LambdaType)
AssertionError

My understanding it that isinstance(lambd, types.LambdaType) is not the best way to check if something is a function or not. Rather, callable or __call__ should be used.

I could submit a quick PR if that seems like a good solution.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
vfdev-5commented, Aug 8, 2018

Glad that you can adopt a more simple solution 😃

Concerning lambdas, I think the idea is similar to pythonic lambda expressions:

t = Lambda(lambda img: img**2)

More realistic example, can be found even in torchvision with ColorJitter, where lambdas allows to wrap a function with initialization:

t = Lambda(lambda img: F.adjust_contrast(img, contrast_factor))

I agree with you that this can look artificial.

Anyway, maybe maintainers could explain better. cc @fmassa .

PS: if you think that there is an answer to your question you can close the issue and reopen it if there’s still something missing.

0reactions
qbiliuscommented, Aug 9, 2018

Thanks for your kind help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS Lambda function errors in Java
This page describes how to view Lambda function invocation errors for the Java runtime using the Lambda console and the AWS CLI.
Read more >
c# - AWS Firehose transform Lambda function throws ...
I'm at a loss as to why or where this error is occurring. I know the Lambda function is returning the correct response,...
Read more >
How To Use Python Lambda Functions
The interpreter raises an error while parsing the code since assert is not a valid keyword in a lambda expression. Single Expression. In...
Read more >
Create an ETL solution using AWS Step Functions ...
AWS Glue is a fully managed Extract, Transform and Load (ETL) service that makes ... Create another Lambda function named crawler-activity-fail which sends ......
Read more >
December 13, 2018, 1:54pm
Apparently you are passing a tuple , while the method expects a PIL.Image . ... Lambda(lambda crops: torch.stack([transforms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found