9.9 KiB
9.9 KiB
In [1]:
import torch
from torchvision import transforms
from PIL import Image
# Function to load and preprocess the image
def load_image_as_tensor(image_path, batch_size=1):
# Open the image using PIL
image = Image.open(image_path)
# Define the transformation pipeline
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(), # Convert PIL Image to tensor and scale to [0, 1]
# transforms.Lambda(lambda x: x.unsqueeze(0).repeat(batch_size, 1, 1, 1)) # Add batch dimension and repeat
])
# Apply the transformation
tensor = transform(image)
return tensorj:\AI\ComfyUI_windows_portable\python_embeded\Lib\site-packages\transformers\utils\hub.py:124: FutureWarning: Using `TRANSFORMERS_CACHE` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead. warnings.warn(
In [2]:
image = load_image_as_tensor("examples/006.jpg")In [3]:
image.shapeOut [3]:
torch.Size([3, 256, 256])
In [4]:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
scale_max = 2In [5]:
model_path = "models/anysr/anysr_edsr_500.pth"In [6]:
model_load = torch.load(model_path)['model']
model_args = model_load['args']['encoder_spec']['args']
pretrain_dict = model_load['sd']In [7]:
image_tensor = image.squeeze(dim=0).to(device)In [8]:
image_tensor.shapeOut [8]:
torch.Size([3, 256, 256])
In [9]:
from anysr_module.edsr_anysr import make_edsr
model = make_edsr(scale=2, **model_args)In [10]:
from anysr_module.anysr_model import replace
model_dict = model.state_dict()
model_dict = replace(pretrain_dict, model_dict)
model.load_state_dict(model_dict, strict=False)In [11]:
model = model.to(device)In [12]:
model.scale = 2
model.scale2 = 2In [14]:
image_tensor.shapeOut [14]:
torch.Size([3, 256, 256])
In [12]:
pred = model(
image_tensor.unsqueeze(dim=0).cuda(),
)In [16]:
pred.shapeOut [16]:
torch.Size([1, 256, 256, 256])
In [13]:
pred.shapeOut [13]:
torch.Size([1, 256, 256, 256])
In [14]:
reshaped_tensor = pred.reshape(1, 256, 256, 3, 9, 9)[1;31m---------------------------------------------------------------------------[0m [1;31mRuntimeError[0m Traceback (most recent call last) Cell [1;32mIn[14], line 1[0m [1;32m----> 1[0m reshaped_tensor [38;5;241m=[39m [43mpred[49m[38;5;241;43m.[39;49m[43mreshape[49m[43m([49m[38;5;241;43m1[39;49m[43m,[49m[43m [49m[38;5;241;43m256[39;49m[43m,[49m[43m [49m[38;5;241;43m256[39;49m[43m,[49m[43m [49m[38;5;241;43m3[39;49m[43m,[49m[43m [49m[38;5;241;43m9[39;49m[43m,[49m[43m [49m[38;5;241;43m9[39;49m[43m)[49m [1;31mRuntimeError[0m: shape '[1, 256, 256, 3, 9, 9]' is invalid for input of size 16777216
In [17]:
predOut [17]:
tensor([[[[-0.2023, -0.2465, -0.1883, ..., 0.0145, 0.0291, 0.0131],
[-0.2058, -0.3171, -0.2691, ..., 0.0181, 0.0362, 0.0219],
[-0.1518, -0.3128, -0.2633, ..., 0.0039, 0.0277, 0.0250],
...,
[-0.0476, 0.0612, 0.0954, ..., -0.1948, -0.2188, -0.1279],
[-0.0426, 0.0376, 0.0445, ..., -0.1631, -0.1892, -0.0890],
[ 0.0051, 0.0672, 0.0702, ..., -0.0010, -0.0048, 0.0314]],
[[-0.1818, -0.2641, -0.2487, ..., -0.1725, -0.1746, -0.1650],
[-0.0194, -0.1230, -0.1304, ..., -0.2200, -0.2187, -0.1621],
[-0.0632, -0.1623, -0.1519, ..., -0.2183, -0.2122, -0.1603],
...,
[-0.1712, -0.1563, -0.1632, ..., -0.1246, -0.1534, -0.1306],
[-0.2195, -0.2493, -0.2216, ..., -0.1596, -0.1834, -0.1382],
[-0.1708, -0.2002, -0.1785, ..., -0.1240, -0.1355, -0.1496]],
[[ 0.0384, -0.1395, -0.1434, ..., 0.0056, -0.0025, -0.0254],
[ 0.0819, -0.0770, -0.1296, ..., -0.0080, -0.0228, -0.0414],
[ 0.0429, -0.0804, -0.1248, ..., -0.0178, -0.0256, -0.0443],
...,
[ 0.1172, 0.0857, 0.0598, ..., -0.0916, -0.1025, -0.0719],
[ 0.0778, 0.0410, 0.0348, ..., -0.0644, -0.0767, -0.0587],
[ 0.0415, 0.0601, 0.0726, ..., 0.0271, 0.0418, 0.0895]],
...,
[[ 0.0076, -0.1249, -0.1511, ..., -0.0041, -0.0041, -0.0131],
[-0.1826, -0.2713, -0.2433, ..., 0.0897, 0.0770, 0.0489],
[-0.1696, -0.2405, -0.2122, ..., 0.0730, 0.0656, 0.0382],
...,
[ 0.0398, 0.0766, 0.1059, ..., -0.0038, -0.0599, -0.0317],
[ 0.0230, 0.0949, 0.1537, ..., -0.0311, -0.0921, -0.0622],
[ 0.0083, 0.0128, 0.0458, ..., -0.1552, -0.1739, -0.1277]],
[[ 0.1756, 0.2339, 0.2451, ..., 0.1755, 0.1732, 0.1503],
[ 0.0185, -0.0176, 0.0165, ..., 0.2317, 0.2341, 0.1946],
[-0.0086, -0.0330, 0.0083, ..., 0.2167, 0.2240, 0.1849],
...,
[ 0.1407, 0.1760, 0.2159, ..., 0.0839, 0.0529, 0.0533],
[ 0.1521, 0.2473, 0.3145, ..., 0.0638, 0.0380, 0.0335],
[ 0.1696, 0.2355, 0.2771, ..., 0.0139, -0.0056, 0.0147]],
[[-0.1895, -0.2206, -0.2065, ..., -0.0522, -0.0691, -0.0413],
[-0.1096, -0.1073, -0.1033, ..., -0.1391, -0.1615, -0.1086],
[-0.1121, -0.1327, -0.1424, ..., -0.1380, -0.1547, -0.1007],
...,
[ 0.0065, -0.0212, -0.0236, ..., -0.1827, -0.1751, -0.0828],
[-0.0189, -0.0623, -0.0892, ..., -0.1393, -0.1310, -0.0676],
[-0.0020, -0.0198, -0.0286, ..., -0.0580, -0.0464, -0.0142]]]],
device='cuda:0', grad_fn=<AddBackward0>)