Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

训练分割模型时出现的错误 #19

Open
leaozhun opened this issue Oct 8, 2024 · 1 comment
Open

训练分割模型时出现的错误 #19

leaozhun opened this issue Oct 8, 2024 · 1 comment

Comments

@leaozhun
Copy link

leaozhun commented Oct 8, 2024

Traceback (most recent call last):
File "tools/train.py", line 242, in
main()
File "tools/train.py", line 232, in main
train_detector(model,
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/mmdet/apis/train.py", line 244, in train_detector
runner.run(data_loaders, cfg.workflow)
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/mmcv/runner/epoch_based_runner.py", line 127, in run
epoch_runner(data_loaders[i], **kwargs)
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/mmcv/runner/epoch_based_runner.py", line 47, in train
for i, data_batch in enumerate(self.data_loader):
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 352, in iter
return self._get_iterator()
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 294, in _get_iterator
return _MultiProcessingDataLoaderIter(self)
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 827, in init
self._reset(loader, first_iter=True)
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 857, in _reset
self._try_put_index()
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1091, in _try_put_index
index = self._next_index()
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 427, in _next_index
return next(self._sampler_iter) # may raise StopIteration
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/torch/utils/data/sampler.py", line 227, in iter
for idx in self.sampler:
File "/media/dell/data1/miniconda3/envs/openpvsg/lib/python3.8/site-packages/mmdet/datasets/samplers/group_sampler.py", line 36, in iter
indices = np.concatenate(indices)
File "<array_function internals>", line 200, in concatenate
ValueError: need at least one array to concatenate
请问您对这样的错误有解决方案吗

@leaozhun
Copy link
Author

Hello,the previous error should be due to a bug in unzip_and_extract.py, and the modified code is attached here
import os, shutil
from tqdm import tqdm
import zipfile
import os
import cv2
import hashlib
from pathlib import Path
from multiprocessing import Pool

zip_dir = './data_zip/'
target_dir = './data/'

List all the zip files in the directory

zip_files = {
'Ego4D/ego4d_videos.zip': {
'target': 'ego4d/videos',
'md5': '9334c74f5c831c80774862afa9d3f7f0'
},
'Ego4D/ego4d_masks.zip': {
'target': 'ego4d/masks',
'md5': '218ce689e1e8284e25b50280a5d29612'
},
'EpicKitchen/epic_kitchen_videos.zip': {
'target': 'epic_kitchen/videos',
'md5': 'b791a71ef24b14721a7b5041190ba4a3'
},
'EpicKitchen/epic_kitchen_masks.zip': {
'target': 'epic_kitchen/masks',
'md5': '03757120075de23328a11e56660175f4'
},
'VidOR/vidor_videos.zip': {
'target': 'vidor/videos',
'md5': 'fcc1a6f54ef60aa16fab335a6270c960'
},
'VidOR/vidor_masks.zip': {
'target': 'vidor/masks',
'md5': '17bfa5ec13235d86273bc9d067776862'
},
}

def calculate_md5(file_path):
hash_md5 = hashlib.md5()
with open(file_path, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
hash_md5.update(chunk)
return hash_md5.hexdigest()

def makedir_if_not_exist(dir):
if not os.path.exists(dir):
os.makedirs(dir)

def process_video(video_path, save_root):
video_name = video_path.name.split('.')[0]
save_dir = os.path.join(save_root, video_name)
os.makedirs(save_dir, exist_ok=True)
cap = cv2.VideoCapture(str(video_path))
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
success = True
count = 0
while success and count < n_frames:
success, image = cap.read()
if success:
cv2.imwrite(os.path.join(save_dir, '{:04d}.png'.format(count)),
image)
count += 1

def extract_videos(video_root, save_root, num_processes=10):
video_root = Path(video_root)
videos_1 = list(video_root.rglob('.MP4'))
videos_2 = list(video_root.rglob('
.mp4'))
videos=videos_1+videos_2
with Pool(num_processes) as pool:
pool.starmap(process_video, [(video, save_root) for video in videos])

def unzip_files(file, destination):
with zipfile.ZipFile(file, 'r') as zip_ref:

    for member in zip_ref.infolist():
        filename = os.path.basename(member.filename)
        if not filename:
            continue
        file_path = os.path.join(destination, filename)
        if 'masks' in file.split('/')[-1]:
            # 获取图像所在文件夹的名称
            folder_name = Path(member.filename).parent.name
            file_path = os.path.join(destination, folder_name,filename)
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with zip_ref.open(member, 'r') as source, open(file_path,
                                                       'wb') as target:
            shutil.copyfileobj(source, target)

unzip files

for filename in zip_files:
zip_path = os.path.join(zip_dir, filename)
zip_info = zip_files[filename]

assert calculate_md5(
    zip_path) == zip_info['md5'], f'{zip_path} md5 mismatches!'

target_path = os.path.join(target_dir, zip_info['target'])
makedir_if_not_exist(target_path)
unzip_files(zip_path, target_path)
print(filename, 'unzipping completed!', flush=True)

if 'videos' in filename.split('/')[-1]:
    frame_path = target_path.replace('videos', 'frames')
    extract_videos(target_path, frame_path)
    print(frame_path, 'extraction completed!', flush=True)

copy pvsg.json to data/

shutil.copy('./data_zip/pvsg.json', './data/')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant