Code Monkey home page Code Monkey logo

Comments (5)

pammirato avatar pammirato commented on May 26, 2024 11

To change a numpy HxWxC array to CxHxW, and get the same behavior as if you called ToPILImage() and then ToTensor(), do

npimg = np.transpose(npimg,(2,0,1))

Doing a npimg.reshape() will not produce the same results

from vision.

alykhantejani avatar alykhantejani commented on May 26, 2024 2

I ran into this issue today too, I think a normalize parameter would be nice as sometimes the image loading function used before this would already put the data into [0,1].

@ClementPinard As a quick work-around you can call ToPILImage first in a Compose i.e.

transform = Compose([ToPILImage(), ToTensor()])

from vision.

el3ment avatar el3ment commented on May 26, 2024 2

@alykhantejani +1 on the normalize parameter.. even better would be for toTensor not to normalize at all.. isn't that what the Normalize transform is for? I got burned doing a Normalize transform with a ToTensor transform

from vision.

cuixing158 avatar cuixing158 commented on May 26, 2024 2

as @alykhantejani said ,another way to get the same effect is using numpy to convert,
assume your image is opencv format,
img_tensor = cv_img[:,:,::-1].transpose((2,0,1)).copy() # chw, RGB order,[0,255]
img_tensor = torch.from_numpy(img_tensor).float().div(255) # chw , FloatTensor type,[0,1]
img_tensor = img_tensor.unsqueeze(0) # nch*w

from vision.

soumith avatar soumith commented on May 26, 2024

fixed in master

from vision.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.