'Python - Turn bytes object into tensor object for use in torch.load()
I have downloaded a tensor (.pt) object from my Google drive into my Python workspace, which is currently a bytes object. It needs to be a tensor object (X) to be used in the code torch.load(X):
The code to download the tensorflow object from my Google Drive is:
file_id2 = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
request = drive_service.files().get_media(fileId=file_id2)
gh = io.BytesIO()
downloader = MediaIoBaseDownload(gh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print ("Download %d%%." % int(status.progress() * 100))
X = gh.getvalue()
I have tried to find a way of getting the bytes object (X) into a tensor object, but I am not finding anything helpful
Is there a way of doing this? or a way of downloading the tensor object from google as a .pt file and not bytes.
Solution 1:[1]
I managed to solve it, so will leave this here in case anyone needs it in the future.
tensorObject = torch.load(io.BytesIO(X))
I had to pass the object as a BytesIO object (I thought it already was), and now it works :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ScoutEU |
