Closed bishalw closed 1 year ago
same problem
same problem
same problem
RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same
Encountered this error on M2
RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same
I'm running into the same issue on Mac M2 Max when using controlnet depth_leres++ and depth_leres preprocessors
Same error M1 Max
Same issue, M1
Same error M1 Pro
Same error M1 Max
you need to set: net.to(devices) then it will be ok
you need to set: net.to(devices) then it will be ok
More details?please
The error means that you put data in mps but sht net still in cpu, so you need to put net in mps also.
def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
devices=torch.device('mps')):
# """用多GPU进行模型训练"""
timer, num_batches = d2l.Timer(), len(train_iter)
animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
legend=['train loss', 'train acc', 'test acc'])
# net = nn.DataParallel(net, device_ids=devices).to(devices[0])
net.to(devices)
for epoch in range(num_epochs):
# 4个维度:储存训练损失,训练准确度,实例数,特点数
metric = d2l.Accumulator(4)
for i, (features, labels) in enumerate(train_iter):
timer.start()
l, acc = train_batch_ch13(
net, features, labels, loss, trainer, devices)
metric.add(l, acc, labels.shape[0], labels.numel())
timer.stop()
if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
animator.add(epoch + (i + 1) / num_batches,
(metric[0] / metric[2], metric[1] / metric[3],
None))
test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)
animator.add(epoch + 1, (None, None, test_acc))
print(f'loss {metric[0] / metric[2]:.3f}, train acc '
f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
f'{str(devices)}')
same error M1 Max
The error means that you put data in mps but sht net still in cpu, so you need to put net in mps also.
def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices=torch.device('mps')): # """用多GPU进行模型训练""" timer, num_batches = d2l.Timer(), len(train_iter) animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1], legend=['train loss', 'train acc', 'test acc']) # net = nn.DataParallel(net, device_ids=devices).to(devices[0]) net.to(devices) for epoch in range(num_epochs): # 4个维度:储存训练损失,训练准确度,实例数,特点数 metric = d2l.Accumulator(4) for i, (features, labels) in enumerate(train_iter): timer.start() l, acc = train_batch_ch13( net, features, labels, loss, trainer, devices) metric.add(l, acc, labels.shape[0], labels.numel()) timer.stop() if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1: animator.add(epoch + (i + 1) / num_batches, (metric[0] / metric[2], metric[1] / metric[3], None)) test_acc = d2l.evaluate_accuracy_gpu(net, test_iter) animator.add(epoch + 1, (None, None, test_acc)) print(f'loss {metric[0] / metric[2]:.3f}, train acc ' f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}') print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on ' f'{str(devices)}')
請問這些代碼要放在哪裡?
I've found the solution: stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/init.py
code: import cv2 import numpy as np import torch import os from modules import devices, shared from annotator.annotator_path import models_path from torchvision.transforms import transforms
from .leres.depthmap import estimateleres, estimateboost from .leres.multi_depth_model_woauxi import RelDepthModel from .leres.net_tools import strip_prefix_if_present
from .pix2pix.options.test_options import TestOptions from .pix2pix.models.pix2pix4depth_model import Pix2Pix4DepthModel
base_model_path = os.path.join(models_path, "leres") old_modeldir = os.path.dirname(os.path.realpath(file))
remote_model_path_leres = "https://huggingface.co/lllyasviel/Annotators/resolve/main/res101.pth" remote_model_path_pix2pix = "https://huggingface.co/lllyasviel/Annotators/resolve/main/latest_net_G.pth"
model = None pix2pixmodel = None
def unload_leres_model(): global model, pix2pixmodel if model is not None: model = model.cpu() if pix2pixmodel is not None: pix2pixmodel = pix2pixmodel.unload_network('G')
def apply_leres(input_image, thr_a, thr_b, boost=False): global model, pix2pixmodel
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('mps')
if model is None:
model_path = os.path.join(base_model_path, "res101.pth")
old_model_path = os.path.join(old_modeldir, "res101.pth")
if os.path.exists(old_model_path):
model_path = old_model_path
elif not os.path.exists(model_path):
from basicsr.utils.download_util import load_file_from_url
load_file_from_url(remote_model_path_leres, model_dir=base_model_path)
if torch.cuda.is_available():
checkpoint = torch.load(model_path)
else:
checkpoint = torch.load(model_path, map_location=torch.device('cpu'))
model = RelDepthModel(backbone='resnext101')
model.load_state_dict(strip_prefix_if_present(checkpoint['depth_model'], "module."), strict=True)
del checkpoint
if boost and pix2pixmodel is None:
pix2pixmodel_path = os.path.join(base_model_path, "latest_net_G.pth")
if not os.path.exists(pix2pixmodel_path):
from basicsr.utils.download_util import load_file_from_url
load_file_from_url(remote_model_path_pix2pix, model_dir=base_model_path)
opt = TestOptions().parse()
if not torch.cuda.is_available():
device = torch.device('mps') # 定義 device 變量
opt.gpu_ids = [] # cpu mode
pix2pixmodel = Pix2Pix4DepthModel(opt)
pix2pixmodel.save_dir = base_model_path
pix2pixmodel.load_networks('latest')
pix2pixmodel.eval()
model = model.to(device) # 使用正確的 device 變量
if devices.get_device_for("controlnet").type != 'mps':
model = model.to(devices.get_device_for("controlnet"))
assert input_image.ndim == 3
height, width, dim = input_image.shape
with torch.no_grad():
if boost:
depth = estimateboost(input_image, model, 0, pix2pixmodel, max(width, height))
else:
depth = estimateleres(input_image, model, width, height)
numbytes=2
depth_min = depth.min()
depth_max = depth.max()
max_val = (2**(8*numbytes))-1
if depth_max - depth_min > np.finfo("float").eps:
out = max_val * (depth - depth_min) / (depth_max - depth_min)
else:
out = np.zeros(depth.shape)
depth_image = out.astype("uint16")
depth_image = cv2.convertScaleAbs(depth_image, alpha=(255.0/65535.0))
if thr_a != 0:
thr_a = ((thr_a/100)*255)
depth_image = cv2.threshold(depth_image, thr_a, 255, cv2.THRESH_TOZERO)[1]
depth_image = cv2.bitwise_not(depth_image)
if thr_b != 0:
thr_b = ((thr_b/100)*255)
depth_image = cv2.threshold(depth_image, thr_b, 255, cv2.THRESH_TOZERO)[1]
return depth_image
I'm having the same error. But I'm only really a basic coder so I'm confused as to which code in which file needs to be updated. Could you explain with a bit more detail please? :)
I'm having the same error. But I'm only really a basic coder so I'm confused as to which code in which file needs to be updated. Could you explain with a bit more detail please? :)
NEW solution found:
open stable-diffusion-webui/webui-user.sh
in export COMMANDLINE_ARGS=
ADD --no-half
DONE!!!
Tried this on my M1. The --no-half commandline arg didn't make a difference, however changing the code in the leres __init__.py
did fix the issue. For those who don't get the code, just download the replacement file from this link, then navigate to
stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres
Rename __init__.py
to old__init__.py
or similar just in case you need to revert this. Then drop the downloaded version of __init__.py
into the folder. Restart Automatic1111 and you should be good to go, so long as you select the leres preprocessor for Controlnet.
Credit to @ga780586 for actually writing the code. I just implemented it and shared my updated file.
Thanks @ga780586 and @Minyall this actually worked! wish it was implemented into offical CN, as of v1.1.440 it's still not working out of the box
I'm having the same error. But I'm only really a basic coder so I'm confused as to which code in which file needs to be updated. Could you explain with a bit more detail please? :)
The way this question has been answered, makes it seem like the issue is specific to the model mentioned, but it isn't. Porting any model from CUDA to mps will most likely require this change. And in response, also for the sake of other coders who may be less familiar with various platforms, I think a generic answer for this would be, aside adding images and labels to device, also add your model in same way. Example below;
images=images.to(devc) labels=labels.to(devc) model = model.to(devc)
before doing; outputs = model(images)
@Minyall I do did as you said but the issue still remain. Why is that? Please help.
PS: This issue happens while I am using the “controlnet-canny” function.
在我的 M1 上尝试过这个。 --no-half 命令行参数没有什么区别,但是更改 leres 中的代码
__init__.py
确实解决了问题。对于那些没有获得代码的人,只需从此链接下载替换文件,然后导航到
stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres
重命名
__init__.py
为old__init__.py
或类似名称,以防万一您需要恢复此名称。然后将下载的版本放入__init__.py
文件夹中。重新启动Automatic1111,只要您为Controlnet 选择leres 预处理器,您就可以开始了。归功于@ga780586用于实际编写代码。我刚刚实现了它并分享了我更新的文件。
Thank you very much for solving my problem in the way you said
labels = labels.to(device) model=model.to(device) outputs = model(inputs).to(device) It will work
Is there an existing issue for this?
What happened?
RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same
This is the error I get when I try to use the depth_leres++ preprocessor.
Steps to reproduce the problem
What should have happened?
Should've gotten the depth map.
Commit where the problem happens
webui: 1.3.2 controlnet: 1.1.224
What browsers do you use to access the UI ?
Google Chrome
Command Line Arguments
This is what i currently used, but I've tried it without it as well.
List of enabled extensions
Console logs
Additional information
I ran this on M1 Pro Max