AttributeError: 'collections.OrderedDict' 对象没有属性 'predict'

时间:2021-04-30 16:27:18

标签: deep-learning pytorch conv-neural-network

作为一个新手和深度学习和 pytorch 的初学者,我不确定我应该给你们什么输入来回答我的问题。但我会尽力让你们明白我的问题。我已经使用 'model= torch.load('model/resnet18-5c106cde.pth')' 在 pytorch 中加载了一个模型。但它显示一个 AttributeError: 'collections.OrderedDict' object has no attribute 'predict', 当我使用命令 'prediction = model.predict(test_image)' 时。希望你们能理解我的问题,并提前致谢...

1 个答案:

答案 0 :(得分:0)

我猜您正在加载的检查点存储模型状态字典(模型的参数)而不是模型(模型的结构及其参数)。试试:

<div class="form-row d-flex justify-content-center align-items-center container">
                   <div class="form-group col-md-3">
                       <label for="inputPassword" id="fieldRegister">Senha:</label>
                       <input type="password" class="form-control" id="inputPassword" minLength="6" maxLength="8" 
                            placeholder="(Máx 6 a 8 dígitos)" name="senha" required
                            value={password}
                            onChange={e=> setPassword(e.target.value)}
                        />
                    </div>
                    <div class="form-group col-md-4">
                        <label for="inputPassword" id="fieldRegister">Confirmar senha:</label>
                        <input type="password" class="form-control" name="senha2" id="inputPassword2" minLength="6" maxLength="8" placeholder="Confirme sua senha" required/>
                    </div>
                    
                 </div>
                 <div class="form-row d-flex justify-content-center align-items-center container">
                    <button type="submit" id="button" class="btn btn-primary mb-3 col-md-2" onClick={validarSenha()}>Cadastrar</button>
                </div>

其中 model = resnet18(*args, **kwargs) model.load_state_dict(torch.load(PATH)) model.eval() 是模型检查点的路径。您需要将 PATH 声明为对象类的实例(声明模型结构),以便您可以加载检查点(仅参数,无结构)。所以你需要找到合适的类来为 resnet18 导入,可能是这样的:

model