JaidedAI / EasyOCR

Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.
https://www.jaided.ai
Apache License 2.0
23.49k stars 3.08k forks source link

cannot import name '_accumulate' from 'torch._utils' #1243

Open lIkesimba9 opened 4 months ago

lIkesimba9 commented 4 months ago

I want finetune model, and when i use trainer.ipynb i get this error `ImportError Traceback (most recent call last) Cell In[1], line 4 2 import torch.backends.cudnn as cudnn 3 import yaml ----> 4 from train import train 5 from utils import AttrDict 6 import pandas as pd

File ~/pet/pcb/EasyOCR/trainer/train.py:15 12 import numpy as np 14 from utils import CTCLabelConverter, AttnLabelConverter, Averager ---> 15 from dataset import hierarchical_dataset, AlignCollate, Batch_Balanced_Dataset 16 from model import Model 17 from test import validation

File ~/pet/pcb/EasyOCR/trainer/dataset.py:13 11 import numpy as np 12 from torch.utils.data import Dataset, ConcatDataset, Subset ---> 13 from torch._utils import _accumulate 14 import torchvision.transforms as transforms 16 def contrast_grey(img):

ImportError: cannot import name '_accumulate' from 'torch._utils' (/home/simba9/anaconda3/envs/easyocr/lib/python3.9/site-packages/torch/_utils.py)`

mdolensk commented 4 months ago

Just ran into the same problem. Pytorch v2.3.0 no longer defines function _accumulate in torch/_utils.py. What worked for me was to patch the code from an older version, namely v2.2.0

def _accumulate(iterable, fn=lambda x, y: x + y):
    "Return running totals"
    # _accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # _accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    for element in it:
        total = fn(total, element)
        yield total

So, either find and downgrade to an older version of Pytorch such as v2.2.0 or use my workaround, which is:

  1. comment out the offending import statement in line 13
  2. append above code snippet to dataset.py
samthakur587 commented 3 months ago
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

use this torch version with 11.3 toolkit and remove the troch and torchvision from requirements and then install the req.