AttributeError: module 'cog' has no attribute 'Predictor'
tianmianjiang opened this issue · 4 comments
Hello, I have a problem. When I run the predict.py file, I get this error: AttributeError: module 'cog' has no attribute 'Predictor'. I've tried several things and it doesn't work. How can I solve this problem?
Thank you.
I dug into it a bit and the issue seems to be that they never pinned a version of cog
, and so their code was written for a super outdated version of it. The current version of cog
is 0.4.4, but from what I can tell the last version of cog
that's compatible with their code is version 0.0.20. Unfortunately, pypi only has the following versions of cog
: 0.4.4, 0.4.3, and 0.0.3. To install cog
at version 0.0.20, I ran the following:
pip install -e "git+https://github.com/replicate/cog.git@v0.0.20#egg=cog&subdirectory=python/"
Once I did this, I was able to import everything from predict.py successfully.
I dug into it a bit and the issue seems to be that they never pinned a version of
cog
, and so their code was written for a super outdated version of it. The current version ofcog
is 0.4.4, but from what I can tell the last version ofcog
that's compatible with their code is version 0.0.20. Unfortunately, pypi only has the following versions ofcog
: 0.4.4, 0.4.3, and 0.0.3. To installcog
at version 0.0.20, I ran the following:pip install -e "git+https://github.com/replicate/cog.git@v0.0.20#egg=cog&subdirectory=python/"
Once I did this, I was able to import everything from predict.py successfully.
Thank you very much indeed for your answer, I managed to solve the problem through your answer and help. After checking the information, I also found that the version of cog they were using was too low and caused a series of problems. So I used the latest version of cog and made the following changes to the code.
`class Predictor(BasePredictor):
def setup(self):
"""Load the model into memory to make running multiple predictions efficient"""
self.device = torch.device("cuda")
self.clip_model, self.preprocess = clip.load(
"ViT-B/32", device=self.device, jit=False
)
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
self.models = {}
self.prefix_length = 10
for key, weights_path in WEIGHTS_PATHS.items():
model = ClipCaptionModel(self.prefix_length)
model.load_state_dict(torch.load(weights_path, map_location=CPU))
model = model.eval()
model = model.to(self.device)
self.models[key] = model`
我深入研究了一下,问题似乎是他们从未固定过 的版本
cog
,因此他们的代码是为它的超级过时版本编写的。当前版本cog
是 0.4.4,但据我所知,cog
与其代码兼容的最后一个版本是 0.0.20 版本。不幸的是,pypi 只有以下版本cog
:0.4.4、0.4.3 和 0.0.3。要安装cog
版本 0.0.20,我运行了以下命令:pip install -e "git+https://github.com/replicate/cog.git@v0.0.20#egg=cog&subdirectory=python/"
完成此操作后,我就能够成功导入 Predict.py 中的所有内容。
非常感谢您的回答,通过您的回答和帮助我成功解决了问题。经过查资料,我还发现他们使用的cog版本太低,导致了一系列问题。所以我使用了最新版本的cog并对代码进行了以下更改。 `class Predictor(BasePredictor): def setup(self): """将模型加载到内存中以提高运行多个预测的效率""" self.device = torch.device("cuda") self.clip_model, self.preprocess = Clip.load( "ViT-B/32", device=self.device, jit=False ) self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
self.models = {} self.prefix_length = 10 for key, weights_path in WEIGHTS_PATHS.items(): model = ClipCaptionModel(self.prefix_length) model.load_state_dict(torch.load(weights_path, map_location=CPU)) model = model.eval() model = model.to(self.device) self.models[key] = model`
hi dude,I am still suffering from this problem, but I don't see any difference between the code you modified and the original one? Could you please give me some advice