Hello again, I tried another model modification. I am using torchvision alexnet model
AlexNetImpl(
(features): torch::nn::Sequential(
(0): torch::nn::Conv2d(3, 64, kernel_size=[11, 11], stride=[4, 4], padding=[2, 2])
(1): torch::nn::Functional()
(2): torch::nn::Functional()
(3): torch::nn::Conv2d(64, 192, kernel_size=[5, 5], stride=[1, 1], padding=[2, 2])
(4): torch::nn::Functional()
(5): torch::nn::Functional()
(6): torch::nn::Conv2d(192, 384, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(7): torch::nn::Functional()
(8): torch::nn::Conv2d(384, 256, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(9): torch::nn::Functional()
(10): torch::nn::Conv2d(256, 256, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(11): torch::nn::Functional()
(12): torch::nn::Functional()
)
(classifier): torch::nn::Sequential(
(0): torch::nn::Dropout(p=0.5, inplace=false)
(1): torch::nn::Linear(in_features=9216, out_features=4096, bias=true)
(2): torch::nn::Functional()
(3): torch::nn::Dropout(p=0.5, inplace=false)
(4): torch::nn::Linear(in_features=4096, out_features=4096, bias=true)
(5): torch::nn::Functional()
(6): torch::nn::Linear(in_features=4096, out_features=1000, bias=true)
)
)
I load it with torch::load and modify last output layer nodes number in this way:
AlexNet model_from_torchvision;
torch::load(model_from_torchvision, "alexnet.pt");
model_from_torchvision.get()->named_modules()["classifier"]->unregister_module("6");
model_from_torchvision.get()->named_modules()["classifier"]->register_module("6", torch::nn::Linear(4096, 12));
The model seems to have been modified (12 nodes in the output layer):
AlexNetImpl(
(features): torch::nn::Sequential(
(0): torch::nn::Conv2d(3, 64, kernel_size=[11, 11], stride=[4, 4], padding=[2, 2])
(1): torch::nn::Functional()
(2): torch::nn::Functional()
(3): torch::nn::Conv2d(64, 192, kernel_size=[5, 5], stride=[1, 1], padding=[2, 2])
(4): torch::nn::Functional()
(5): torch::nn::Functional()
(6): torch::nn::Conv2d(192, 384, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(7): torch::nn::Functional()
(8): torch::nn::Conv2d(384, 256, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(9): torch::nn::Functional()
(10): torch::nn::Conv2d(256, 256, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(11): torch::nn::Functional()
(12): torch::nn::Functional()
)
(classifier): torch::nn::Sequential(
(0): torch::nn::Dropout(p=0.5, inplace=false)
(1): torch::nn::Linear(in_features=9216, out_features=4096, bias=true)
(2): torch::nn::Functional()
(3): torch::nn::Dropout(p=0.5, inplace=false)
(4): torch::nn::Linear(in_features=4096, out_features=4096, bias=true)
(5): torch::nn::Functional()
(6): torch::nn::Linear(in_features=4096, out_features=12, bias=true)
)
)
Now I simply save the model and then I load it into a new object.
torch::save(model_from_torchvision, "savedmodel.pt");
AlexNet model_tmp;
torch::load(model_tmp, "savedmodel.pt");
But model_tmp structure is not the modified one but the original one (with 1000 nodes in the output layer).
AlexNetImpl(
(features): torch::nn::Sequential(
(0): torch::nn::Conv2d(3, 64, kernel_size=[11, 11], stride=[4, 4], padding=[2, 2])
(1): torch::nn::Functional()
(2): torch::nn::Functional()
(3): torch::nn::Conv2d(64, 192, kernel_size=[5, 5], stride=[1, 1], padding=[2, 2])
(4): torch::nn::Functional()
(5): torch::nn::Functional()
(6): torch::nn::Conv2d(192, 384, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(7): torch::nn::Functional()
(8): torch::nn::Conv2d(384, 256, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(9): torch::nn::Functional()
(10): torch::nn::Conv2d(256, 256, kernel_size=[3, 3], stride=[1, 1], padding=[1, 1])
(11): torch::nn::Functional()
(12): torch::nn::Functional()
)
(classifier): torch::nn::Sequential(
(0): torch::nn::Dropout(p=0.5, inplace=false)
(1): torch::nn::Linear(in_features=9216, out_features=4096, bias=true)
(2): torch::nn::Functional()
(3): torch::nn::Dropout(p=0.5, inplace=false)
(4): torch::nn::Linear(in_features=4096, out_features=4096, bias=true)
(5): torch::nn::Functional()
(6): torch::nn::Linear(in_features=4096, out_features=1000, bias=true)
)
)
Why could this be?
It seems that any change I make from outside the model is not persistent and it is lost after saving.
Thanks.