How to load the prebuilt Resnet models weight ?(or any other prebuilt models)

(kerry.cho) #1

Can you please let me know how to load the pretrained models using c++ frontend? I am trying to load
the resnet model using C++ front end as follows.

	// replacement for the last layer
	torch::nn::Linear lin(2048, 2); // the last layer of resnet, which you want to replace, has dimensions 512x1000

	std::shared_ptr<torch::jit::script::Module> resnet;
	resnet = torch::jit::load("../../PretrainModel/resnet101.pt");

	torch::optim::Adam optimizer(lin->parameters(), torch::optim::AdamOptions(starting_lr).weight_decay(1e-4));
	resnet->to(device);
	lin->to(device);

	for (size_t epoch = 1; epoch <= kNumberOfEpochs; ++epoch)
	{
		double running_loss = 0;
		double running_corrects = 0;

		lin->train();

		clock_t s = (int)std::clock();

		for (const auto& batch : *train_loader)
		{
			std::vector<torch::jit::IValue> input;
			auto data = batch.data.to(device), targets = batch.target.to(device);
			input.push_back(data);
			// some training loop
			torch::Tensor out = resnet->forward(input).toTensor().squeeze();
			out = lin(out);
			auto pred = out.argmax(1);
	
			torch::Tensor loss = torch::nll_loss(out.log_softmax(1), targets);
			optimizer.zero_grad();
			loss.backward();
			optimizer.step();

			running_loss += loss.template item<float>();
			running_corrects += pred.eq(targets).sum().template item<int64_t>();
		}

		clock_t e = (int)std::clock();

		std::printf
		(
			"\rTrain Epoch: %ld [%5d] Loss: %.4f acc: %.4f processing time : %0.3f s",
			epoch,
			train_dataset_size, running_loss / train_dataset_size, running_corrects / train_dataset_size, (float)(e - s) / CLOCKS_PER_SEC);

}

But I want to create a new model using the values ​​of all the parameters.
How to do it ?

Thank you.