Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Required constant "version_number" was not found in the model file.

Discussion in 'ML-Agents' started by GoldenHippoInTw, Jun 13, 2022.

  1. GoldenHippoInTw

    GoldenHippoInTw

    Joined:
    Oct 31, 2019
    Posts:
    1
    I train a model via pytorch and convert it into .onnx
    But when I imported the .onnx to Unity
    It shows Required constant "version_number" was not found in the model file.

    Screenshot_4.jpg
    Then I use WinML to see what I'm missing
    I see this on the model that working on Unity

    Screenshot_5.jpg

    But my model is missing it
    Screenshot_6.jpg

    How could I add those outputs in my model?

    Here's my code on pytorch side.

    Code (Boo):
    1. class Network(nn.Module):
    2.     def __init__(self,version_number):
    3.         super(Network, self).__init__()
    4.        
    5.         self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=5, stride=1, padding=1)
    6.         self.bn1 = nn.BatchNorm2d(12)
    7.         self.conv2 = nn.Conv2d(in_channels=12, out_channels=12, kernel_size=5, stride=1, padding=1)
    8.         self.bn2 = nn.BatchNorm2d(12)
    9.         self.pool = nn.MaxPool2d(2,2)
    10.         self.conv4 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=5, stride=1, padding=1)
    11.         self.bn4 = nn.BatchNorm2d(24)
    12.         self.conv5 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=5, stride=1, padding=1)
    13.         self.bn5 = nn.BatchNorm2d(24)
    14.         self.fc1 = nn.Linear(24*10*10, 10)
    15.  
    16.     def forward(self, input ):
    17.         output = F.relu(self.bn1(self.conv1(input)))    
    18.         output = F.relu(self.bn2(self.conv2(output)))    
    19.         output = self.pool(output)                      
    20.         output = F.relu(self.bn4(self.conv4(output)))    
    21.         output = F.relu(self.bn5(self.conv5(output)))    
    22.         output = output.view(-1, 24*10*10)
    23.         output = self.fc1(output)
    24.  
    25.         return output
    26.  
    27. # Instantiate a neural network model
    28. model = Network()
    29.  
    30. print(model)
    31.  
    32. # Define the loss function with Classification Cross-Entropy loss and an optimizer with Adam optimizer
    33. loss_fn = nn.CrossEntropyLoss()
    34. optimizer = Adam(model.parameters(), lr=0.001, weight_decay=0.0001)
    35.  
    36.     #Function to Convert to ONNX
    37. def Convert_ONNX():
    38.  
    39.     # set the model to inference mode
    40.     model.eval()
    41.  
    42.     # Let's create a dummy input tensor
    43.     dummy_input = torch.randn(2, 3, 32, 32, requires_grad=True)
    44.  
    45.     inputNames = [ "actual_input_1" ]
    46.     outputNames = [ "output1" ]
    47.  
    48.     # Export the model  
    49.     torch.onnx.export(model,         # model being run
    50.          dummy_input,       # model input (or a tuple for multiple inputs)
    51.          "ImageClassifier.onnx",       # where to save the model
    52.          export_params=True,  # store the trained parameter weights inside the model file
    53.          opset_version=10,    # the ONNX version to export the model to
    54.          do_constant_folding=True,  # whether to execute constant folding for optimization
    55.          input_names = inputNames,   # the model's input names
    56.          output_names = outputNames # the model's output names
    57.          )
    58.     print(" ")
    59.     print('Model has been converted to ONNX')
    60.  
    61.  
     
    Airmouse likes this.
  2. vagerant

    vagerant

    Joined:
    Dec 4, 2015
    Posts:
    2
    I have the same question.Please tell me if someone knows the answer.Thanks!