pytorch-yolo-v3 icon indicating copy to clipboard operation
pytorch-yolo-v3 copied to clipboard

RuntimeError: invalid argument 2: size '[18 x 256 x 1 x 1]' is invalid for input with 4607 elements

Open MatthewLQM opened this issue 6 years ago • 19 comments

Hi, I'm trying to use your code to do some detection work. I'm successful while using yolov3.cfg and yolov3.weights. But when I change this cfg file and weights to my detection, error occurs. I only change the classes num to 1 and the error is here:

Traceback (most recent call last):
  File "D:/GitHub/Graduation/YOLO_v3_self/detect.py", line 109, in <module>
    model.load_weights(args.weightsfile)
  File "D:\GitHub\Graduation\YOLO_v3_self\darknet.py", line 425, in load_weights
    conv_weights = conv_weights.view_as(conv.weight.data)
  File "C:\software\Anaconda3\envs\my_dev\lib\site-packages\torch\tensor.py", line 230, in view_as
    return self.view(tensor.size())
RuntimeError: invalid argument 2: size '[18 x 256 x 1 x 1]' is invalid for input with 4607 elements at ..\src\TH\THStorage.c:41

Is there some solution? Thanks!

MatthewLQM avatar May 24 '18 06:05 MatthewLQM

In darknet.py, line374, change the value of the parameter "count" to 4, maybe it can work :)

zazada avatar Jun 06 '18 07:06 zazada

@zazada why change count to 4? I faced similar error. FYI, I have 17 classes. Kindly help please Thanks

munziliashali avatar Jun 07 '18 06:06 munziliashali

when changing the class num, remember to change the filters num in last conv before every YOLO layer. @munziliashali for your situation , you should change the filter num to 4+1+17 = 22

Brizel avatar Jun 08 '18 02:06 Brizel

Yes, I already did. My question is why change count to 4? Because it worked perfectly... So I wondered why... :)

munziliashali avatar Jun 08 '18 10:06 munziliashali

@munziliashali This has to do with the header file of the weights function. Very strange indeed since for the official YOLO v3 weights file supplied by Joseph Redmon, the header was 5 x 32 bits long. However, in YOLO v2, it was 4x32 bits long.

ayooshkathuria avatar Jun 11 '18 19:06 ayooshkathuria

@munziliashali . Can you confirm the implementation can work on custom trained file, (like on your 17 class dataset?).

ayooshkathuria avatar Jun 11 '18 19:06 ayooshkathuria

Dear @ayooshkathuria Yes, it worked! many thanks

munziliashali avatar Jun 21 '18 07:06 munziliashali

You can update load_weights function as follow to resolve problem: def load_weights(self, weightfile):

    #Open the weights file
    fp = open(weightfile, "rb")

    #The first 4 values are header information 
    # 1. Major version number
    # 2. Minor Version Number
    # 3. Subversion number 
    # 4. IMages seen 
    header = np.fromfile(fp, dtype = np.int32, count = 3)        
    if (header[0]*10+header[1] >=2) and (header[0] < 1000) and (header[1] < 1000):
        sub_header = np.fromfile(fp, dtype = np.int32, count = 2)
    else:
        sub_header = np.fromfile(fp, dtype = np.int32, count = 1)
    
    header = np.append(header,sub_header)

    self.header = torch.from_numpy(header)
    
    self.seen = self.header[3]
    
    #The rest of the values are the weights
    # Let's load them up
    weights = np.fromfile(fp, dtype = np.float32)

Since yolov3, the type of subversion number is size_t not int32

tungth07 avatar Jul 03 '18 06:07 tungth07

@tungth07 your answer is right. I have solved this problem, thanks

MatthewLQM avatar Jul 11 '18 16:07 MatthewLQM

Hello, I have a little problem. I set count=4, the model used is tiny-yolo, I have 6 kinds of data. When I was testing, only the first row of data in the names file showed, why?

VioletDu avatar Jul 12 '18 09:07 VioletDu

Could you tell me clearly which parameters have you changed? I change the value of the parameter "count" to 4,but it didn't work. @MatthewLQM 你可以明确的告诉我你都改了哪些参数吗,我改了count但还是报错,我的.weights文件是在darknet下用yolov3训练的。

LittleSunH avatar Aug 01 '18 01:08 LittleSunH

@LittleSunH In your .cfg file,you should change the "classes=" of lines 610, 696, and 783 to your own. The "filters=" of lines 603, 689, and 776 are changed to (classes+5)*3. Then in darknet.py, the function "load_weights()" can be modified according to the @tungth07 's answer. 你要把你的cfg文件 610、696、783行的classes改成你自己的,603、689、776行的filters改成(classes+5)*3。darknet.py中load_weights()函数按照 @tungth07 的回答修改即可。

L-F-F-F avatar Nov 08 '18 11:11 L-F-F-F

I'm still getting a similar issue but with my shape being set to [4,2,85,26,26], with count = 5 and the desired output being 689520. I need to multiply one of my values (be it grid, anchors, or something else) by a factor of 3/2, but everything I try seems to blow up train.py. Any suggestions?

dukeeagle avatar Feb 20 '19 01:02 dukeeagle

i am facing this issue conv_weights = conv_weights.view_as(conv.weight.data) RuntimeError: shape '[1024, 512, 3, 3]' is invalid for input of size 4242442 number of classes = 2 i used the method by @tungth07 but its not working.

GunjanChourasia avatar Apr 05 '19 12:04 GunjanChourasia

@MatthewLQM @zazada @munziliashali @Brizel @this YOLOv3 tutorial may help you: https://github.com/ultralytics/yolov3/wiki/Train-Custom-Data

The accompanying repository works on MacOS, Windows and Linux, includes multigpu and multithreading, performs inference on images, videos, webcams, and an iOS app. It also tests to slightly higher mAPs than darknet, including on the latest YOLOv3-SPP.weights (60.7 COCO mAP), and offers the ability to train custom datasets from scratch to darknet performance, all using PyTorch :) https://github.com/ultralytics/yolov3



fourth-archive avatar Apr 11 '19 17:04 fourth-archive

I have tried all of the above solution but none of them are working. I get this error - model.load_weights("yolov3.weights") conv_weights = conv_weights.view_as(conv.weight.data) RuntimeError: shape '[128, 64, 3, 3]' is invalid for input of size 7067

If anyone has solved this issue please do let me know.

@ayooshkathuria Can you help with this issue?

iamdaaniyaal avatar Apr 18 '19 17:04 iamdaaniyaal

I have tried all of the above solution but none of them are working. I get this error - File "/home/travis/pytorch-yolo-v3-master/darknet.py", line 403, in load_weights self.seen = self.header[3]

IndexError: index 3 is out of bounds for dimension 0 with size 3 and .weight file is default(yolov3.cfg),accoding @tungth07 solution,but its not success. 求帮助,但感觉无力了

TravisTorch avatar Jun 04 '19 02:06 TravisTorch

i used the method by @tungth07 but its not working. 请问您怎么解决的?

youngupup avatar Dec 03 '20 05:12 youngupup

[18 x 256 x 1 x 1] = 4608 elements. The input has 4607 elements. There is 1 element missing.

If you go into the save_weights function in darknet2pytorch.py and comment out the code where it reduces the len(self.blocks) by 1, you will get this code, which should match the number of elements:

def save_weights(self, outfile, cutoff=0):
    # if cutoff <= 0:
    #     cutoff = len(self.blocks) - 1

By the way, I am actually using pytorch-YOLOv4, but this thread helped me fix my issues, so maybe this will still apply for YOLOv3.

Hope this helps!

rmutalik avatar Apr 23 '21 13:04 rmutalik