ayooshkathuria / pytorch-yolo-v3

A PyTorch implementation of the YOLO v3 object detection algorithm
3.3k stars 1.06k forks source link

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

Open MatthewLQM opened 6 years ago

MatthewLQM commented 6 years ago

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!

zazada commented 6 years ago

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

munziliashali commented 6 years ago

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

Brizel commented 6 years ago

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

munziliashali commented 6 years ago

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

ayooshkathuria commented 6 years ago

@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 commented 6 years ago

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

munziliashali commented 6 years ago

Dear @ayooshkathuria Yes, it worked! many thanks

tungth07 commented 6 years ago

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

MatthewLQM commented 6 years ago

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

VioletDu commented 6 years ago

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?

LittleSunH commented 6 years ago

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训练的。

L-F-F-F commented 5 years ago

@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 的回答修改即可。

dukeeagle commented 5 years ago

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?

GunjanChourasia commented 5 years ago

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.

fourth-archive commented 5 years ago

@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



iamdaaniyaal commented 5 years ago

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?

TravisTorch commented 5 years ago

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. 求帮助,但感觉无力了

youngupup commented 3 years ago

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

rmutalik commented 3 years ago

[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!