Spaces:
Sleeping
Sleeping
Commit
·
ac5c2a4
1
Parent(s):
cbb2900
add multiple image plot
Browse files
app.py
CHANGED
@@ -28,7 +28,6 @@ transform = v2.Compose([
|
|
28 |
# Load and Preprocess the Image
|
29 |
def load_image(image_path, transform):
|
30 |
img = Image.open(image_path).convert('RGB')
|
31 |
-
# transform = Compose([ToTensor(), Normalize(0.5, 0.5), Resize((32, 32))])
|
32 |
img = transform(img).unsqueeze(0) # Add batch dimension
|
33 |
return img
|
34 |
|
@@ -36,6 +35,10 @@ def show_image(img, title):
|
|
36 |
img = rearrange(img, "c h w -> h w c")
|
37 |
img = (img.cpu().detach().numpy() + 1) / 2 # Normalize to [0, 1]
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# Visualize a Single Image
|
40 |
def visualize_single_image(image_path):
|
41 |
img = load_image(image_path, transform).to(device)
|
@@ -51,28 +54,28 @@ def visualize_single_image(image_path):
|
|
51 |
|
52 |
# MAE reconstruction pasted with visible patches
|
53 |
im_paste = img * (1 - mask) + predicted_img * mask
|
54 |
-
|
55 |
-
img = rearrange(img[0], "c h w -> h w c")
|
56 |
-
img = (img.cpu().detach().numpy() + 1) / 2 # Normalize to [0, 1]
|
57 |
|
58 |
-
#
|
59 |
-
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
71 |
-
# show_image(im_paste[0], "reconstruction + visible")
|
72 |
|
73 |
-
#
|
|
|
74 |
|
75 |
-
return
|
76 |
|
77 |
inputs_image = [
|
78 |
gr.components.Image(type="filepath", label="Input Image"),
|
|
|
28 |
# Load and Preprocess the Image
|
29 |
def load_image(image_path, transform):
|
30 |
img = Image.open(image_path).convert('RGB')
|
|
|
31 |
img = transform(img).unsqueeze(0) # Add batch dimension
|
32 |
return img
|
33 |
|
|
|
35 |
img = rearrange(img, "c h w -> h w c")
|
36 |
img = (img.cpu().detach().numpy() + 1) / 2 # Normalize to [0, 1]
|
37 |
|
38 |
+
plt.imshow(img)
|
39 |
+
plt.axis('off')
|
40 |
+
plt.title(title)
|
41 |
+
|
42 |
# Visualize a Single Image
|
43 |
def visualize_single_image(image_path):
|
44 |
img = load_image(image_path, transform).to(device)
|
|
|
54 |
|
55 |
# MAE reconstruction pasted with visible patches
|
56 |
im_paste = img * (1 - mask) + predicted_img * mask
|
|
|
|
|
|
|
57 |
|
58 |
+
# make the plt figure larger
|
59 |
+
plt.figure(figsize=(12, 4))
|
60 |
+
|
61 |
+
plt.subplot(1, 4, 1)
|
62 |
+
show_image(img[0], "original")
|
63 |
|
64 |
+
plt.subplot(1, 4, 2)
|
65 |
+
show_image(im_masked[0], "masked")
|
66 |
|
67 |
+
plt.subplot(1, 4, 3)
|
68 |
+
show_image(predicted_img[0], "reconstruction")
|
69 |
|
70 |
+
plt.subplot(1, 4, 4)
|
71 |
+
show_image(im_paste[0], "reconstruction + visible")
|
72 |
|
73 |
+
plt.tight_layout()
|
|
|
74 |
|
75 |
+
# convert the plt figure to a numpy array
|
76 |
+
plt.savefig("output.png")
|
77 |
|
78 |
+
return np.array(plt.imread("output.png"))
|
79 |
|
80 |
inputs_image = [
|
81 |
gr.components.Image(type="filepath", label="Input Image"),
|