model inference but self.training is save true #11285
Answered
by
morestart
morestart
asked this question in
code help: CV
-
i use
but when i load my model, i use debug mode find that the self.training is save True. self.model = CustomModel.load_from_checkpoint(model_path)
self.model.training = False i use above code change model.training status, but its not work this is my inference full code: class CustomModelInference:
def __init__(
self,
model_path: str,
conf_thres: float = 0.25,
iou_thres: float = 0.45,
max_det: int = 1000,
device: str = 'cuda:0',
need_classes: list | None = None
):
self.conf_thres = conf_thres
self.iou_thres = iou_thres
self.max_det = max_det
self.device = device
self.need_classes = need_classes
self.model = CustomModel.load_from_checkpoint(model_path)
self.model.training = False
self.model.to(device)
self.stride = int(self.model.stride.max())
self.names = self.model.names
self.imgsz = self.model.imgsz
@torch.no_grad()
def infer(self, img: np.ndarray):
imgsz = check_img_size(self.imgsz, s=self.stride)
cudnn.benchmark = True
img = letterbox(img, imgsz, stride=self.stride, auto=True)[0]
# img = np.stack(img, 0)
if len(img.shape) == 3:
img = img[None]
img = img[..., ::-1].transpose((0, 3, 1, 2))
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device)
img = img.float()
img = img / 255.0
out, train_out = self.model(img) |
Beta Was this translation helpful? Give feedback.
Answered by
morestart
Dec 31, 2021
Replies: 2 comments
-
fine, i know the answer, i have to set model.eval()...... |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
morestart
-
training mode is the default in pytorch, so this seems to be correct. import torch
class MyModel(torch.nn.Module):
pass
model = MyModel()
model.training
Out[14]: True |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fine, i know the answer, i have to set model.eval()......