# Why does ViTForMaskedImageModeling not construct the original image correctly?

**URL:** <https://discuss.huggingface.co/t/why-does-vitformaskedimagemodeling-not-construct-the-original-image-correctly/40125>\
**Category:** Beginners\
**Created:** [May 17, 2023, 4:49am UTC](https://discuss.huggingface.co/t/why-does-vitformaskedimagemodeling-not-construct-the-original-image-correctly/40125 "2023-05-17T04:49:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bassel1](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@Bassel1](https://discuss.huggingface.co/u/Bassel1)\
**Post date:** [May 17, 2023, 4:49am UTC](https://discuss.huggingface.co/t/why-does-vitformaskedimagemodeling-not-construct-the-original-image-correctly/40125/1 "2023-05-17T04:49:03Z")

</div>

I was trying to use masked image modeling in huggingface and I saw [ViTForMaskedImageModeling](https://huggingface.co/docs/transformers/model_doc/vit#transformers.ViTForMaskedImageModeling)  
in the documentation but I did not understand how it reconstructs the original image `loss, reconstructed_pixel_values = outputs.loss, outputs.reconstruction`  
also, it doesn’t reconstruct the original image correctly. It gives me noise.

```auto
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
model = ViTForMaskedImageModeling.from_pretrained("google/vit-base-patch16-224-in21k")

num_patches = (model.config.image_size // model.config.patch_size) ** 2
pixel_values = image_processor(images=image, return_tensors="pt").pixel_values
# create random boolean mask of shape (batch_size, num_patches)
bool_masked_pos = torch.randint(low=0, high=2, size=(1, num_patches)).bool()

outputs = model(pixel_values, bool_masked_pos=bool_masked_pos)
loss, reconstructed_pixel_values = outputs.loss, outputs.reconstruction

reconstructed_pixel_values = reconstructed_pixel_values.detach().numpy()
reconstructed_pixel_values = np.transpose(reconstructed_pixel_values[0], (1, 2, 0))

plt.imshow(reconstructed_pixel_values)
plt.show()

```

 ![Screen Shot 2023-05-17 at 7.31.51 AM](https://us1.discourse-cdn.com/hellohellohello/original/3X/3/0/30192ca43617456af3cdda22928f175642c36443.jpeg)
