Spaces:
Running
Running
File size: 4,408 Bytes
929f451 ee8e678 929f451 774a99f 929f451 def8bfd 929f451 ac5c2a4 929f451 774a99f 929f451 774a99f 929f451 def8bfd 929f451 def8bfd 84aa40a ac5c2a4 def8bfd 929f451 ac5c2a4 def8bfd 929f451 ac5c2a4 def8bfd 929f451 ac5c2a4 def8bfd 929f451 ac5c2a4 929f451 ac5c2a4 929f451 ac5c2a4 929f451 774a99f 929f451 af03c71 929f451 774a99f 929f451 af03c71 774a99f 929f451 774a99f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import gradio as gr
import torch
from PIL import Image
import numpy as np
import random
from einops import rearrange
import matplotlib.pyplot as plt
from torchvision.transforms import v2
from model import MAE_ViT, MAE_Encoder, MAE_Decoder, MAE_Encoder_FeatureExtractor
path = [['images/cat.jpg'], ['images/dog.jpg'], ['images/horse.jpg'], ['images/airplane.jpg'], ['images/truck.jpg']]
device = torch.device("cpu")
model_name = "model/no_mode/vit-t-mae-pretrain.pt"
model_no_mode = torch.load(model_name, map_location='cpu')
model_no_mode.eval()
model_no_mode.to(device)
model_name = "model/bottom_256/vit-t-mae-pretrain.pt"
model_pca_mode = torch.load(model_name, map_location='cpu')
model_pca_mode.eval()
model_pca_mode.to(device)
transform = v2.Compose([
v2.Resize((32, 32)),
v2.ToTensor(),
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
# Load and Preprocess the Image
def load_image(image_path, transform):
img = Image.open(image_path).convert('RGB')
img = transform(img).unsqueeze(0) # Add batch dimension
return img
def show_image(img, title):
img = rearrange(img, "c h w -> h w c")
img = (img.cpu().detach().numpy() + 1) / 2 # Normalize to [0, 1]
plt.imshow(img)
plt.axis('off')
plt.title(title)
# Visualize a Single Image
def visualize_single_image_no_mode(image_path):
img = load_image(image_path, transform).to(device)
# Run inference
with torch.no_grad():
predicted_img, mask = model_no_mode(img)
# Convert the tensor back to a displayable image
# masked image
im_masked = img * (1 - mask)
# MAE reconstruction pasted with visible patches
im_paste = img * (1 - mask) + predicted_img * mask
# resize the image to 96 x 96
img = v2.functional.resize(img[0], (96, 96))
im_masked = v2.functional.resize(im_masked[0], (96, 96))
predicted_img = v2.functional.resize(predicted_img[0], (96, 96))
im_paste = v2.functional.resize(im_paste[0], (96, 96))
# make the plt figure larger
plt.figure(figsize=(18, 8))
plt.subplot(1, 4, 1)
show_image(img, "original")
plt.subplot(1, 4, 2)
show_image(im_masked, "masked")
plt.subplot(1, 4, 3)
show_image(predicted_img, "reconstruction")
plt.subplot(1, 4, 4)
show_image(im_paste, "reconstruction + visible")
plt.tight_layout()
# convert the plt figure to a numpy array
plt.savefig("output.png")
return np.array(plt.imread("output.png"))
def visualize_single_image_pca_mode(image_path):
img = load_image(image_path, transform).to(device)
# Run inference
with torch.no_grad():
predicted_img, mask = model_pca_mode(img)
# Convert the tensor back to a displayable image
# masked image
im_masked = img * (1 - mask)
# MAE reconstruction pasted with visible patches
im_paste = img * (1 - mask) + predicted_img * mask
# remove the batch dimension
im_masked = im_masked[0]
predicted_img = predicted_img[0]
im_paste = im_paste[0]
# make the plt figure larger
plt.figure(figsize=(18, 8))
plt.subplot(1, 4, 1)
show_image(img, "original")
plt.subplot(1, 4, 2)
show_image(im_masked, "masked")
plt.subplot(1, 4, 3)
show_image(predicted_img, "reconstruction")
plt.subplot(1, 4, 4)
show_image(im_paste, "reconstruction + visible")
plt.tight_layout()
# convert the plt figure to a numpy array
plt.savefig("output.png")
return np.array(plt.imread("output.png"))
inputs_image = [
gr.components.Image(type="filepath", label="Input Image"),
]
outputs_image = [
gr.components.Image(type="numpy", label="Output Image"),
]
inference_no_mode = gr.Interface(
fn=visualize_single_image_no_mode,
inputs=inputs_image,
outputs=outputs_image,
examples=path,
cache_examples = False,
title="MAE-ViT Image Reconstruction",
description="This is a demo of the MAE-ViT model for image reconstruction.",
)
inference_pca_mode = gr.Interface(
fn=visualize_single_image_pca_mode,
inputs=inputs_image,
outputs=outputs_image,
examples=path,
title="MAE-ViT Image Reconstruction",
description="This is a demo of the MAE-ViT model for image reconstruction.",
)
gr.TabbedInterface(
[inference_no_mode, inference_pca_mode],
tab_names=['Normal Mode', 'PCA Mode']
).queue().launch() |