0

I´m having problem creating a program to be used in google codelabs. The goal is to import a ".pth" file convert it to ".onnx" model and trhen download it from the google codelabs directory as in the ones existent in "/content/upload"

I need to use google codelabs because i don´t have pyhon installed on windows.

The code i did was:

Step1 - (Ok so far. No errors)

# Preparations
!pip install onnx

# Import the necessary libraries.
import os
import shutil

import torch
import onnx

# Import the TensorFlow library.

import tensorflow as tf

#import os
from google.colab import files
#import shutil

Step2 - (Ok so far. No errors) - Import File. This is where it shows up a button to import the ".pth" file

# Import File
# Define the upload and result folders.

upload_folder = 'upload'
result_folder = 'results'

# Delete the upload and result folders if they exist.

if os.path.isdir(upload_folder):
    shutil.rmtree(upload_folder)
if os.path.isdir(result_folder):
    shutil.rmtree(result_folder)

# Create the upload and result folders.

os.mkdir(upload_folder)
os.mkdir(result_folder)

# Upload the images.
uploaded = files.upload()
for filename in uploaded.keys():
  dst_path = os.path.join(upload_folder, filename)

Step3 - (Ok so far. No errors) - Convert the loaded/imported file

# Convert
def convert_torch_to_onnx(model_path, output_filename="model.onnx"):
    # Load the Torch model.
    model = torch.load(dst_path)

    # Convert the Torch model to ONNX.
    onnx_model = torch.onnx.export(model, None, output_filename)

    # Save the ONNX model.
    onnx.save_model(onnx_model, output_filename)

    # Get the path of the ONNX model.
    onnx_path = os.path.join(upload_folder, output_filename)

Step4 - (his is where it is getting error. I cannot make it export the converted ".onnx" file to the proper directory

print(onnx_path) # just to see where it stored the coverted onnx file

# Define the path of the output file.
output_filename = "/content/output.onnx"

# Print the path to the screen.
print(f'move {output_filename} to {onnx_path}')

# Move the ONNX model to the new location.
shutil.move(output_filename, '/content/upload')

I dont understand where the error is. I tried earlier a simple way such as

    print(f'move {output_filename} to {onnx_path}')
    shutil.move(output_filename, onnx_path)

But still had errors.

Previously i did that without convertn and it was correctly put the file to /content/upload But now i´m having an error.

Previously, (without convert) it was sucessifully uploading the file only doing this:

# Define the upload and result folders.

upload_folder = 'upload'
result_folder = 'results'

# Delete the upload and result folders if they exist.

if os.path.isdir(upload_folder):
    shutil.rmtree(upload_folder)
if os.path.isdir(result_folder):
    shutil.rmtree(result_folder)

# Create the upload and result folders.

os.mkdir(upload_folder)
os.mkdir(result_folder)

# Upload the images.
uploaded = files.upload()
for filename in uploaded.keys():
  dst_path = os.path.join(upload_folder, filename)
   print(f'move {filename} to {dst_path}')
  shutil.move(filename, dst_path)

What´s wrong ? Why can´t i convert teh file to onnx and then use the same method as in print(f'move {filename} to {dst_path}') shutil.move(filename, dst_path)

???

Boris Lux.
  • 11
  • 1

0 Answers0