PengyiZhang / SlimYOLOv3

This page is for the SlimYOLOv3: Narrower, Faster and Better for UAV Real-Time Applications
1.14k stars 320 forks source link

How to control the number of channels of pruned model is multiple of 4 ? #29

Open ycxia opened 5 years ago

wjjouc commented 5 years ago

@ycxia have you completed the Channel Prunning? When I run: python yolov3/prune.py --cfg VisDrone2019/yolov3-spp3.cfg --weights yolov3-spp3_sparsity.weights the following problem occur: Traceback (most recent call last): File "prune.py", line 389, in opt.perlayer_ratio, File "prune.py", line 160, in test m = getattr(module, 'batchnorm%d' % i) # batch_norm layer File "/home/wjj/.local/lib/python3.5/site-packages/torch/nn/modules/module.py", line 535, in getattr type(self).name, name)) AttributeError: 'Sequential' object has no attribute 'batch_norm_0' Could you tell me how to solve this problem?

ycxia commented 5 years ago

@wjjouc you should use fcos or ktian08-hyp branch of ultralytics, not master.

varghesealex90 commented 5 years ago

@wjjouc @ycxia where you to fix the error. I tried ktian and fcos. I am getting error during training.

wjjouc commented 5 years ago

@varghesealex90 I have not fix the error.Can you tell me where to find ktian and fcos?

varghesealex90 commented 5 years ago

Dont use master. Git cloning to ultrlytics by default takes the master branch

cd into yolov3

git checkout ktian

git pull

and now you can use.

wjjouc commented 5 years ago

@varghesealex90 Thank you so much! I did as you said and run: $ cd yolov3 & git checkout ktian08-hyp & git pull $ python prune.py --cfg cfg/prune_0.5.cfg --weights weights/prune_0.5_final.weights but a new error arose as follow: prune done! pruned ratio 0.500 Done! Traceback (most recent call last): File "prune.py", line 389, in opt.perlayer_ratio, File "prune.py", line 326, in test img, ratiow, ratioh, padw, padh = letterbox(org_img, new_shape=[img_size,img_size], mode='rect') File "/home/wjj/my/yolov3/utils/datasets.py", line 308, in letterbox shape = img.shape[:2] # current shape [height, width] AttributeError: 'NoneType' object has no attribute 'shape'

How should I fix this error?

In addition,the command given by the author is: python yolov3/prune.py --cfg VisDrone2019/yolov3-spp3.cfg --data-cfg VisDrone2019/drone.data --weights yolov3-spp3_sparsity.weights --overall_ratio 0.5 --perlayer_ratio 0.1 but I dont seem to find the parameter "--data-cfg" in the prune.py?

Hwijune commented 5 years ago

@varghesealex90 Thank you so much! I did as you said and run: $ cd yolov3 & git checkout ktian08-hyp & git pull $ python prune.py --cfg cfg/prune_0.5.cfg --weights weights/prune_0.5_final.weights but a new error arose as follow: prune done! pruned ratio 0.500 Done! Traceback (most recent call last): File "prune.py", line 389, in opt.perlayer_ratio, File "prune.py", line 326, in test img, ratiow, ratioh, padw, padh = letterbox(org_img, new_shape=[img_size,img_size], mode='rect') File "/home/wjj/my/yolov3/utils/datasets.py", line 308, in letterbox shape = img.shape[:2] # current shape [height, width] AttributeError: 'NoneType' object has no attribute 'shape'

How should I fix this error?

In addition,the command given by the author is: python yolov3/prune.py --cfg VisDrone2019/yolov3-spp3.cfg --data-cfg VisDrone2019/drone.data --weights yolov3-spp3_sparsity.weights --overall_ratio 0.5 --perlayer_ratio 0.1 but I dont seem to find the parameter "--data-cfg" in the prune.py?

it's just test. match image path

varghesealex90 commented 5 years ago

@wjjouc I use my own data.

wjjouc commented 5 years ago

@varghesealex90 I want to train my own data.But the issue still occurs whether I use my own data or the VisDrone data.The problem that bothers me is the path of the data.In order to set the path to data, should I modify the VisDrone2019/drone.data or prune.py? If it is prune.py, how and where should I modify it?

varghesealex90 commented 5 years ago

I am not sure about this @wjjouc since I have othered to try. What worries me more are:

1) After running prune. py, we get a folder with the pruned cfg and pruned.pt 2) I converted the pruned.pt to pruned.weights 3) I used prue.cfg and pruned.weights in darknet and compute mAP. I get 0 %. 4) However after training for quite sometime, (I HAD TO TRAIN FOR DAYS), I was able to achieve same mAP as yolov3-spp3 before pruning

wjjouc commented 5 years ago

@varghesealex90 I have not achieve run prune.py ,so I did not compute mAP. Maybe you could try to use test.py in ultralytics/pytorch to compute mAP if the MAP is 0% when using darknet, I think.

turboxin commented 5 years ago

hi @ycxia,may I ask have you solved the problem of controling the number of channels of pruned model as multiple of 4? Thanks a lot!

wjjouc commented 5 years ago

@hwijune The problem that occurs when running prune.py still bothers me: shape = img.shape[:2] # current shape [height, width] AttributeError: 'NoneType' object has no attribute 'shape' How and where to set or modify the path to images or data in prune.py? Or which files need to modify? Is prune.py , datasets.py or other files need to modify?I use some of the COCO data to finish Sparsity Training, and want to use those datas to prune model.

ycxia commented 5 years ago

@turboxin , I control the number of remaing channels as multiple of 4 through parameter of mask and Merge_masks. mask is easy to change, add the following codes after mask = weight_copy.gt(thresh).float().cuda(),

if (int(torch.sum(mask))%4) !=0: remainchannels = int(torch.sum(mask))%4) , sorted_index_weights = torch.sort(weight_copy,descending=True) mask[sorted_index_weights[:remain_channels-(remain_channels%4)+4]]=1.

But Merge_masks is not easy to change. It involves merging mask of multiple layers. I changed 0 to 1 of Merge_masks randomly so that torch.sum(Merge_masks)%4 ==0. Obviously this is not a good idea.

wjjouc commented 5 years ago

@ycxia I use fcos branch of ultralytics to run the command : python prune.py but a new issue occurs :

File "prune.py", line 390, in opt.perlayer_ratio, File "prune.py", line 327, in test img, ratiow, ratioh, padw, padh = letterbox(org_img, new_shape=[img_size,img_size], mode='rect') File "/home/gc/4-images/9.4/yolov3/utils/datasets.py", line 308, in letterbox shape = img.shape[:2] # current shape [height, width] AttributeError: 'NoneType' object has no attribute 'shape'

Can you help me ?

turboxin commented 5 years ago

hi @ycxia thank you very much for sharing!

Arc2019 commented 4 years ago

@ycxia I use fcos branch of ultralytics to run the command : python prune.py but a new issue occurs :

File "prune.py", line 390, in opt.perlayer_ratio, File "prune.py", line 327, in test img, ratiow, ratioh, padw, padh = letterbox(org_img, new_shape=[img_size,img_size], mode='rect') File "/home/gc/4-images/9.4/yolov3/utils/datasets.py", line 308, in letterbox shape = img.shape[:2] # current shape [height, width] AttributeError: 'NoneType' object has no attribute 'shape'

Can you help me ?

@wjjouc were you able to solve this issue? I get the same error!

MuhammadAsadJaved commented 4 years ago

@ycxia I use fcos branch of ultralytics to run the command : python prune.py but a new issue occurs :

File "prune.py", line 390, in opt.perlayer_ratio, File "prune.py", line 327, in test img, ratiow, ratioh, padw, padh = letterbox(org_img, new_shape=[img_size,img_size], mode='rect') File "/home/gc/4-images/9.4/yolov3/utils/datasets.py", line 308, in letterbox shape = img.shape[:2] # current shape [height, width] AttributeError: 'NoneType' object has no attribute 'shape'

Can you help me ?

@wjjouc were you able to solve this issue? I get the same error!

+1

MuhammadAsadJaved commented 4 years ago

I am not sure about this @wjjouc since I have othered to try. What worries me more are:

  1. After running prune. py, we get a folder with the pruned cfg and pruned.pt
  2. I converted the pruned.pt to pruned.weights
  3. I used prue.cfg and pruned.weights in darknet and compute mAP. I get 0 %.
  4. However after training for quite sometime, (I HAD TO TRAIN FOR DAYS), I was able to achieve same mAP as yolov3-spp3 before pruning

Hi, it is same for me. I got prune.cfg and .pt and convert .pt to .weights and run demo on darknet repo. but it is not detecting anything. is it same for you?

joel5638 commented 4 years ago

@MuhammadAsadJaved Hi Javed. I have pruned weight using ktian branch. But the weights are not detecting while running the inference. Did you find any fix?

Arc2019 commented 4 years ago

You will have to retrain the pruned weights, to regain the performance loss due to pruning.

joel5638 commented 4 years ago

@Arc2019 okay. So you’re saying we’ d have train the pruned weight like normal training from the last epoch. But wouldn’t the size of the weight increase if we do so?

Pengyizhang Should have mentioned To retrain the whole pruned model again.

MuhammadAsadJaved commented 4 years ago

Hi, please retrain pruned weights for a few thousand epochs. It will not effect the size .

On Wed, Apr 1, 2020 at 8:04 PM Joel Prabhod notifications@github.com wrote:

@MuhammadAsadJaved https://github.com/MuhammadAsadJaved Hi Javed. I have pruned weight using ktian branch. But the weights are not detecting while running the inference. Did you find any fix?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/PengyiZhang/SlimYOLOv3/issues/29#issuecomment-607209019, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG4GR5GDDEJNHNU6NY24R4TRKMUWNANCNFSM4IMDOIKA .

Arc2019 commented 4 years ago

@Arc2019 okay. So you’re saying we’ d have train the pruned weight like normal training from the last epoch. But wouldn’t the size of the weight increase if we do so?

Pengyizhang Should have mentioned To retrain the whole pruned model again.

Yes, you will have to retrain it. The paper also mentions it clearly in the figure, as the "fine-tuning" step. Once we have the pruned weights, then we retrain and continue doing so in iterative steps till the required performance is achieved. For my application, I repeated the process for 3 cycles.

joel5638 commented 4 years ago

@MuhammadAsadJaved @Arc2019 thanks guys.

So il have to retrain the prune weights through the same ktian-hyp branch using train.py?

so that would be

python3 train.py --cfg cfg/prune-spp.cfg --weights weights/prune_0.5.weights --data data/custom.data --epochs 500

is the --epoch arg correct?

MuhammadAsadJaved commented 4 years ago

Yes.

On Thu, Apr 2, 2020 at 5:32 PM Joel Prabhod notifications@github.com wrote:

@MuhammadAsadJaved https://github.com/MuhammadAsadJaved @Arc2019 https://github.com/Arc2019 thanks guys.

So il have to retrain the prune weights through the same ktian-hyp branch using train.py?

so that would be

python3 train.py --cfg cfg/prune-spp.cfg --weights weights/prune_0.5.weights --data data/custom.data --epochs 500

is the --epoch arg correct?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/PengyiZhang/SlimYOLOv3/issues/29#issuecomment-607732633, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG4GR5GVQTYGGOSJACPQ3WDRKRLUHANCNFSM4IMDOIKA .

joel5638 commented 4 years ago

@MuhammadAsadJaved @Arc2019 and also when i run sparsity.py noting happens. I just executes with no output.

is that how it should be executed?