ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.4k stars 16.27k forks source link

classifier.py #5085

Closed sekisek closed 3 years ago

sekisek commented 3 years ago

I am trying to train with classifier.py (successfully on mnist) I get an error in the first epoch with my dataset stack expects each tensor to be equal size, but got [3, 557, 1512] at entry 0 and [3, 619, 1573] at entry 1 or stack expects each tensor to be equal size, but got [3, 598, 1431] at entry 0 and [3, 692, 861] at entry 1 after reading I think it is because of the different image sizes in my dataset? but how do I solve that?

also when training for classification only, there is there no need to point to a data yaml file with the classes???? or does it take the classes from the folder structure?

glenn-jocher commented 3 years ago

@sekisek yes classifier.py assumes the class names from the classification dataset directory names.

sekisek commented 3 years ago

@glenn-jocher thanks for all the help!!! I trained it with classifier.py and have new weights. Do I now use regular detect.py? or the detect.py in the classifier branch? I get different errors with both....

python detect.py --source 1.jpg --weights classified_640.pt

in the branch I get File "D:\AI\Codnda\yolov5_classffier\utils\general.py", line 492, in non_max_suppression nc = prediction.shape[2] - 5 # number of classes IndexError: tuple index out of range

in the main: AttributeError: 'Conv' object has no attribute 'fuseforward'

can it be because of the wired nan in the training? 1212

sekisek commented 3 years ago

I think I solve it

glenn-jocher commented 3 years ago

@sekisek you can simply pass the classifiation models an image as a pytorch tensor or a batch of images in BCWH format, i.e. 1,3,640,640 shape.

sekisek commented 3 years ago

Hi @glenn-jocher

so I retrained and no nan but I get the same errors when trying to detect python detect.py --source 1.jpg --weights 640_x_colors.pt

detect.py in classification branch gives: image 1/1 D:\AI\Codnda\yolov5_classffier\1.jpg: Traceback (most recent call last): File "D:\AI\Codnda\yolov5_classffier\detect.py", line 184, in <module> detect(opt=opt) File "D:\AI\Codnda\yolov5_classffier\detect.py", line 71, in detect pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, opt.classes, opt.agnostic_nms, File "D:\AI\Codnda\yolov5_classffier\utils\general.py", line 492, in non_max_suppression nc = prediction.shape[2] - 5 # number of classes IndexError: tuple index out of range

and the detect on main branch gives: Traceback (most recent call last): File "D:\AI\Codnda\yolov5\detect.py", line 295, in <module> main(opt) File "D:\AI\Codnda\yolov5\detect.py", line 290, in main run(**vars(opt)) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context return func(*args, **kwargs) File "D:\AI\Codnda\yolov5\detect.py", line 78, in run model = attempt_load(weights, map_location=device) # load FP32 model File "D:\AI\Codnda\yolov5\models\experimental.py", line 94, in attempt_load ckpt = torch.load(attempt_download(w), map_location=map_location) # load File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\serialization.py", line 607, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\serialization.py", line 882, in _load result = unpickler.load() File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1130, in __getattr__ raise AttributeError("'{}' object has no attribute '{}'".format( AttributeError: 'Conv' object has no attribute 'fuseforward'

glenn-jocher commented 3 years ago

https://github.com/ultralytics/yolov5/issues/5085#issuecomment-938269732

sekisek commented 3 years ago

Hi @glenn-jocher I added out = F.interpolate(img, size=640) after img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 if img.ndimension() == 3: img = img.unsqueeze(0)

it does transform it to torch.Size([1, 3, 640, 640])

but still the same error Traceback (most recent call last): File "D:\AI\Codnda\yolov5_classffier\clif.py", line 205, in <module> detect(opt=opt) File "D:\AI\Codnda\yolov5_classffier\clif.py", line 92, in detect pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, opt.classes, opt.agnostic_nms, File "D:\AI\Codnda\yolov5_classffier\utils\general.py", line 492, in non_max_suppression nc = prediction.shape[2] - 5 # number of classes IndexError: tuple index out of range

any pointers?