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

how to get each picture's latent code? #36

Closed
alip7 opened this issue Nov 11, 2020 · 3 comments
Closed

how to get each picture's latent code? #36

alip7 opened this issue Nov 11, 2020 · 3 comments

Comments

@alip7
Copy link

alip7 commented Nov 11, 2020

I set the 'return_latents=True' in psp.py , but get TypeError :


Traceback (most recent call last):
File "/home/zhz1/anaconda3/envs/env_36/lib/python3.7/site-packages/PIL/Image.py", line 2751, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 1, 256), '|u1')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "scripts/inference.py", line 136, in
run()
File "scripts/inference.py", line 82, in run
result = tensor2im(result_batch[i])
File "./utils/common.py", line 23, in tensor2im
return Image.fromarray(var.astype('uint8'))
File "/home/zhz1/anaconda3/envs/env_36/lib/python3.7/site-packages/PIL/Image.py", line 2753, in fromarray
raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1, 1, 256), |u1

could you please tell me how to output each picture's latent code?

@yuval-alaluf
Copy link
Collaborator

Did you only make a change in psp.py?
You most likely got the error because you did not make all the necessary changes needed.

You should make the changes in inference.py instead. You should change the call

result_batch = net(inputs, randomize_noise=False, resize=opts.resize_outputs)

in inference.py to return a tuple as such:

images, result_latents = net(...,return_latents=True) 

Then, in inference.py in line 129 you should change

return result_batch

to

return images, result_latents

Finally, in line 73, you should change the line

result_batch = run_on_batch(input_cuda, net, opts)

to something like

result_batch, result_latents = run_on_batch(input_cuda, net, opts)

Now you have the latents of the batch and can save them if you wish.
In short, returning latents is very much possible with some minimal changes to the inference.py file.
I may have missed a small step, but the above changes should assist you in getting started. If you still encounter issues feel free to reply.

@alip7
Copy link
Author

alip7 commented Nov 12, 2020

Did you only make a change in psp.py?
You most likely got the error because you did not make all the necessary changes needed.

You should make the changes in inference.py instead. You should change the call

result_batch = net(inputs, randomize_noise=False, resize=opts.resize_outputs)

in inference.py to return a tuple as such:

images, result_latents = net(...,return_latents=True) 

Then, in inference.py in line 129 you should change

return result_batch

to

return images, result_latents

Finally, in line 73, you should change the line

result_batch = run_on_batch(input_cuda, net, opts)

to something like

result_batch, result_latents = run_on_batch(input_cuda, net, opts)

Now you have the latents of the batch and can save them if you wish.
In short, returning latents is very much possible with some minimal changes to the inference.py file.
I may have missed a small step, but the above changes should assist you in getting started. If you still encounter issues feel free to reply.

Thanks for your instruction and your great work! It helps a lot.
now I get the (18,512) of latent code for each picture.

@yuval-alaluf
Copy link
Collaborator

Awesome!

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

No branches or pull requests

2 participants