Ssd.pytorch: When I run train.py for about 40 iters, it got an error and the program break

Created on 1 Apr 2018  ·  12Comments  ·  Source: amdegroot/ssd.pytorch

File "train.py", line 255, in
train()
File "train.py", line 165, in train
images, targets = next(batch_iterator)
File "/home/chenzw/anaconda3/envs/tensor3/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 271, in next
raise StopIteration
StopIteration

Most helpful comment

I made the following revise:

  1. epoch_size = len(data_loader) instead of len(dataset) // args.batch_size
  2. add the try clause when load train data:
    try:
    images, targets = next(batch_iterator)
    except StopIteration:
    batch_iterator = iter(data_loader)
    images, targets = next(batch_iterator)
    except Exception as e:
    print("Loading data Exception:", e)

All 12 comments

Wow,another error
File "train.py", line 255, in
train()
File "train.py", line 165, in train
images, targets = next(batch_iterator)
File "/home/chenzw/anaconda3/envs/tensor/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 271, in __next__
raise StopIteration
StopIteration

I have same problem

iter 510 || Loss: 8.8698 || Traceback (most recent call last):
File "train.py", line 257, in
train()
File "train.py", line 184, in train
'append', epoch_size)
File "train.py", line 122, in update_vis_plot
update=True
File "/home/zhenghe/.local/lib/python3.5/site-packages/visdom/__init__.py", line 206, in result
return fn(args, *kwargs)
File "/home/zhenghe/.local/lib/python3.5/site-packages/visdom/__init__.py", line 769, in line
update=update, name=name)
File "/home/zhenghe/.local/lib/python3.5/site-packages/visdom/__init__.py", line 206, in result
return fn(args, *kwargs)
File "/home/zhenghe/.local/lib/python3.5/site-packages/visdom/__init__.py", line 613, in scatter
assert win is not None
AssertionError

Because when then iter run over one whole epoch, it can't go on iterating from the start point automatically.

here solved this problem

The reason is as ShoufaChen explained.

And I notice that there is another way to handle this problem. I train this network in VOC datasets and I read the config.py in data folder,it expected to iter 120000 times. For the entire datasets it can iter iter_datasets = len(dataset) / batchSize times,to achieve our goal,say,120000,we need to repeat epoch_size = 120000 / iter_datasets,In order to simplify the design, I change the code like this:

iter_datasets = len(dataset) // args.batch_size
epoch_size = cfg['max_iter'] // iter_datasets
for epoch in range(0, epoch_size):
    for i_batch, (images, targets) in enumerate(data_loader):

And notice that this would break down your visdom output and I just turn off visdom,Another thing you should notice is that you should change your code to print infomation(loss, accuracy, time...) that you are interested in.Ok, for visdom,I also notice that you need to have a globle viz at the beginning of train() function for your draw code to use viz if you flag --visdom True .


Hope this helps!

''''''''''''''''''''''''''''''''''''''''''''''
zhuyu72
you can copy another train.py(maybe your_train.py) and change some code like this:

    iter_datasets = len(dataset) // args.batch_size
    epoch_size = cfg['max_iter'] // iter_datasets

    for iteration in range(0, epoch_size):
        for i_batch, (images, targets) in enumerate(data_loader):
            if args.visdom and iteration != 0 and (iteration % epoch_size == 0):
                update_vis_plot(epoch, loc_loss, conf_loss, epoch_plot, None,
                                'append', epoch_size)
                # reset epoch loss counters
                loc_loss = 0
                conf_loss = 0

where should i place the code? @HosinPrime

what I changed has solved this problem.
You can change the code in
for iteration in range(args.start_iter, cfg['max_iter']):
use try...except to judge if next() rease StopIteration,and in except reload data.

I made the following revise:

  1. epoch_size = len(data_loader) instead of len(dataset) // args.batch_size
  2. add the try clause when load train data:
    try:
    images, targets = next(batch_iterator)
    except StopIteration:
    batch_iterator = iter(data_loader)
    images, targets = next(batch_iterator)
    except Exception as e:
    print("Loading data Exception:", e)

@HosinPrime 你好,我在修改了batch_size,并且没有修改网络的情况下进行训练,但是训练得到的损失很差,请问这是什么原因?

@HosinPrime 你好,我在修改了batch_size,并且没有修改网络的情况下进行训练,但是训练得到的损失很差,请问这是什么原因?
可能是batch_size太小了 影响训练结果

@HosinPrime 你好,我在修改了batch_size,并且没有修改网络的情况下进行训练,但是训练得到的损失很差,请问这是什么原因?
可能是batch_size太小了 影响训练结果

我将batch_size修改为了16进行训练,损失还是无法减小

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kouyichi picture kouyichi  ·  5Comments

chienyiwang picture chienyiwang  ·  3Comments

DHZS picture DHZS  ·  5Comments

sxyxf66 picture sxyxf66  ·  4Comments

FostorHUNT picture FostorHUNT  ·  4Comments