EasyOCR icon indicating copy to clipboard operation
EasyOCR copied to clipboard

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

Open lIkesimba9 opened this issue 10 months ago • 2 comments

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)`

lIkesimba9 avatar Apr 24 '24 19:04 lIkesimba9

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

mdolensk avatar Apr 25 '24 23:04 mdolensk

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.

samthakur587 avatar May 07 '24 16:05 samthakur587