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

Add Cog configuration and scripts #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
*.caffemodel
*.mat
*.npy

10 changes: 10 additions & 0 deletions cog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
predict: "predict.py:Predictor"
build:
python_version: "3.8"
python_packages:
- "numpy==1.20.0"
- "opencv-python==4.5.1.48"
- "torch==1.8.0"
system_packages:
- "libgl1-mesa-dev"
- "libglib2.0-0"
47 changes: 47 additions & 0 deletions predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import tempfile
from pathlib import Path
import cv2
import numpy as np
import torch
import RRDBNet_arch as arch
import cog

model_path = (
"models/RRDB_ESRGAN_x4.pth"
)


class Predictor(cog.Predictor):
def setup(self):
if torch.cuda.is_available():
self.device = torch.device("cuda:0")
else:
self.device = torch.device("cpu")
print("Loading model...")
self.model = arch.RRDBNet(3, 3, 64, 23, gc=32)
self.model.load_state_dict(torch.load(model_path), strict=True)
self.model.eval()
self.model = self.model.to(self.device)

@cog.input("image", type=Path, help="Low-resolution input image")
def predict(self, image):
print("Reading input image...")
img = cv2.imread(str(image), cv2.IMREAD_COLOR)
img = img * 1.0 / 255
img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
img_LR = img.unsqueeze(0)
img_LR = img_LR.to(self.device)

print("Upscaling...")
with torch.no_grad():
output = (
self.model(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()
)
output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
output = (output * 255.0).round()
out_path = Path(tempfile.mkdtemp()) / "out.png"

print("Saving result...")
cv2.imwrite(str(out_path), output)

return out_path