# Finetune onnx model

**URL:** https://community.konduit.ai/t/finetune-onnx-model/2739
**Category:** SameDiff
**Created:** [August 2, 2023, 5:05pm UTC](https://community.konduit.ai/t/finetune-onnx-model/2739 "2023-08-02T17:05:53Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Booker](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/booker/32/987_2.png) [@Booker](https://community.konduit.ai/u/Booker)
#### Post date: [August 2, 2023, 5:05pm UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/1 "2023-08-02T17:05:54Z")

</div>

Can I retrain some layers in onnx model by SameDiff? Is there an example?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 3, 2023, 11:54am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/2 "2023-08-03T11:54:35Z")

</div>

@Booker yes we do. You would need to add an updater and loss function. Most onnx models will only have the feedforward. You can load it as is and just add these things when you’re done. Eg: loss + updater could be:

```auto
SDVariable loss = sd.loss.logLoss("loss", label, out);

                //Also set the training configuration:
                sd.setTrainingConfig(TrainingConfig.builder()
                        .updater(new Adam(0.01))
                        .weightDecay(1e-3, true)
                        .dataSetFeatureMapping("in") //features[0] -> "in" placeholder
                        .dataSetLabelMapping("label") //labels[0] -> "label" placeholder
                        .build());

```

sd would be a samediff instance you imported using the OnnxFrameworkImporter.

---

<div class="post-metadata">

### Author: ![Booker](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/booker/32/987_2.png) [@Booker](https://community.konduit.ai/u/Booker)
#### Post date: [August 4, 2023, 5:36am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/3 "2023-08-04T05:36:58Z")

</div>

Thanks. I should retrain entire model or one layer to get better result for my little data? how to frozen entire model and set specific layer to train?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 4, 2023, 8:31am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/4 "2023-08-04T08:31:37Z")

</div>

@Booker there aren’t really “layers” in samediff. It’s just ops. There are just variables. In terms of specific variables, you can just do:

SameDIff sd = …;  
sd = sd.freeze(true);

That will freeze all variables.

For leaving certain variables frozen, just set those to variables. You can see the code for that here:

> <https://github.com/deeplearning4j/deeplearning4j/blob/5edff97bd65ca836b3f725d14d2fd49a0c975d87/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/SameDiff.java#L6725>

By default everything should be constants since onnx imports as feedforward only.

---

<div class="post-metadata">

### Author: ![Booker](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/booker/32/987_2.png) [@Booker](https://community.konduit.ai/u/Booker)
#### Post date: [August 4, 2023, 8:51am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/5 "2023-08-04T08:51:41Z")

</div>

Powerfull! The next release is waiting for a year, when will release it?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 4, 2023, 9:31am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/6 "2023-08-04T09:31:23Z")

</div>

I had paid work that held that up for quite a while. Now that that’s done I’m wrapping up the cuda testing now. I’ve been cleaning up technical debt along the way. Don’t worry that’s the main item I’m working on atm.

---

<div class="post-metadata">

### Author: ![Booker](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/booker/32/987_2.png) [@Booker](https://community.konduit.ai/u/Booker)
#### Post date: [August 6, 2023, 1:53am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/7 "2023-08-06T01:53:21Z")

</div>

Unable to resolve attribute for name auto\_pad for node Conv for op type Conv  
Unable to resolve attribute for name dilations for node MaxPool for op type MaxPool  
Skipping input B on node /model.22/dfl/conv/Conv

These console logs exist when I import yolov8x.onnx, is it normal and can be ignore? Thanks.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 6, 2023, 3:58am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/8 "2023-08-06T03:58:09Z")

</div>

@Booker can you file an issue and link to the model so I can reproduce this and verify this will work in the current upcoming release?Thanks!

---

<div class="post-metadata">

### Author: ![Booker](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/booker/32/987_2.png) [@Booker](https://community.konduit.ai/u/Booker)
#### Post date: [August 6, 2023, 7:33am UTC](https://community.konduit.ai/t/finetune-onnx-model/2739/9 "2023-08-06T07:33:28Z")

</div>

> <https://github.com/deeplearning4j/deeplearning4j/issues/10021>
>
> Unable to resolve attribute for name auto\_pad for node Conv for op type Conv
> Un…able to resolve attribute for name dilations for node MaxPool for op type MaxPool
> Skipping input B on node /model.22/dfl/conv/Conv
> 
> These console logs exist when I import yolov8x.onnx, is it normal and can be ignore? Thanks.
> 
> model link:
> https://huggingface.co/spaces/Booker8/ll/resolve/main/yolov8x.zip

done.
