Update README.md
Browse files
README.md
CHANGED
|
@@ -56,27 +56,33 @@ def preprocess(image_path, ref_size=512):
|
|
| 56 |
image = Image.open(image_path).convert("RGB")
|
| 57 |
w, h = image.size
|
| 58 |
|
|
|
|
| 59 |
scale = ref_size / max(h, w)
|
| 60 |
new_w, new_h = int(w * scale), int(h * scale)
|
| 61 |
image_resized = image.resize((new_w, new_h), Image.BILINEAR)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
transform = transforms.Compose([
|
| 64 |
transforms.ToTensor(),
|
| 65 |
transforms.Normalize(mean=[0.5, 0.5, 0.5],
|
| 66 |
std=[0.5, 0.5, 0.5])
|
| 67 |
])
|
| 68 |
|
| 69 |
-
tensor = transform(
|
| 70 |
return tensor, (w, h)
|
| 71 |
|
| 72 |
# Run inference
|
| 73 |
-
inp, original_size = preprocess("
|
| 74 |
|
| 75 |
import torch
|
| 76 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 77 |
|
| 78 |
model.to(device)
|
| 79 |
-
inp.to(device)
|
| 80 |
|
| 81 |
with torch.no_grad():
|
| 82 |
semantic, detail, matte = model(inp, True)
|
|
|
|
| 56 |
image = Image.open(image_path).convert("RGB")
|
| 57 |
w, h = image.size
|
| 58 |
|
| 59 |
+
# Resize while maintaining aspect ratio
|
| 60 |
scale = ref_size / max(h, w)
|
| 61 |
new_w, new_h = int(w * scale), int(h * scale)
|
| 62 |
image_resized = image.resize((new_w, new_h), Image.BILINEAR)
|
| 63 |
|
| 64 |
+
# Create a new blank image of ref_size x ref_size and paste the resized image
|
| 65 |
+
# This will pad the image to ref_size x ref_size
|
| 66 |
+
padded_image = Image.new("RGB", (ref_size, ref_size), (0, 0, 0)) # Black padding
|
| 67 |
+
padded_image.paste(image_resized, ((ref_size - new_w) // 2, (ref_size - new_h) // 2))
|
| 68 |
+
|
| 69 |
transform = transforms.Compose([
|
| 70 |
transforms.ToTensor(),
|
| 71 |
transforms.Normalize(mean=[0.5, 0.5, 0.5],
|
| 72 |
std=[0.5, 0.5, 0.5])
|
| 73 |
])
|
| 74 |
|
| 75 |
+
tensor = transform(padded_image).unsqueeze(0)
|
| 76 |
return tensor, (w, h)
|
| 77 |
|
| 78 |
# Run inference
|
| 79 |
+
inp, original_size = preprocess("/content/portrait.jpg")
|
| 80 |
|
| 81 |
import torch
|
| 82 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 83 |
|
| 84 |
model.to(device)
|
| 85 |
+
inp = inp.to(device) # Assign the tensor to the device
|
| 86 |
|
| 87 |
with torch.no_grad():
|
| 88 |
semantic, detail, matte = model(inp, True)
|