Views
No views yet
1<!-- After Hugging Face Login install these libraries -->
2<!-- !pip install torchsr
3!pip install diffusers[training] -->
4from torchsr.models import edsr
5from diffusers import DDPMPipeline
6import torch
7from PIL import Image
8import torchvision.transforms as transforms
9
10device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
12# Load the butterfly pipeline
13butterfly_pipeline = DDPMPipeline.from_pretrained(
14 "Ketansomewhere/Lung_Ultrasound_Diffusion_720p"
15).to(device)
16
17# Create 1(can be n in principle) images
18images = butterfly_pipeline(batch_size=1).images
19
20# Load the pre-trained EDSR model
21model = edsr(scale=4, pretrained=True).to(device)
22
23upscaled_images = []
24
25for img in images:
26 # Convert to tensor and add batch dimension
27 img_tensor = transforms.ToTensor()(img).unsqueeze(0).to(device)
28
29 # Upscale the image
30 upscaled_img_tensor = model(img_tensor)
31
32 # Remove batch dimension and convert back to PIL image
33 upscaled_img = transforms.ToPILImage()(upscaled_img_tensor.squeeze(0).cpu())
34
35 # Add to list of upscaled images
36 upscaled_images.append(upscaled_img)
37
38# Function to make a grid of images
39def make_grid(images, size=720):
40 """Given a list of PIL images, stack them together into a line for easy viewing"""
41 output_im = Image.new("RGB", (size * len(images), size))
42 for i, im in enumerate(images):
43 output_im.paste(im.resize((size, size)), (i * size, 0))
44 return output_im
45
46# View the result
47make_grid(upscaled_images)
48