Skip to content

Instantly share code, notes, and snippets.

@hereismari
Created May 20, 2019 20:04
Show Gist options
  • Save hereismari/dea4fad2fe3fc381c6f06c41a9c791da to your computer and use it in GitHub Desktop.
Save hereismari/dea4fad2fe3fc381c6f06c41a9c791da to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:root:Torch was already hooked... skipping hooking process\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Setting up Sandbox...\n",
"Done!\n"
]
}
],
"source": [
"import sys\n",
"import json\n",
"import requests\n",
"import random\n",
"\n",
"import torch as th\n",
"from torch import nn\n",
"import syft as sy\n",
"\n",
"sy.create_sandbox(globals(), verbose=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torchvision\n",
"\n",
"# An instance of your model.\n",
"model = torchvision.models.resnet18()\n",
"\n",
"# An example input you would normally provide to your model's forward() method.\n",
"example = torch.rand(1, 3, 224, 224)\n",
"\n",
"# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.\n",
"traced_script_module = torch.jit.trace(model, example)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"import io\n",
"buffer = io.BytesIO()\n",
"torch.jit.save(traced_script_module, buffer)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"buffer_output = buffer.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b'PK\\x03\\x04\\x00\\x00\\x08\\x08\\x00\\x00'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"buffer_output"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"other_buffer = io.BytesIO(buffer_output)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"x = torch.jit.load(other_buffer)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0.0008, -0.0082, 0.0355, ..., 0.0083, -0.0411, 0.0243],\n",
" [ 0.0008, -0.0082, 0.0355, ..., 0.0083, -0.0411, 0.0243],\n",
" [ 0.0008, -0.0082, 0.0355, ..., 0.0083, -0.0411, 0.0243],\n",
" ...,\n",
" [ 0.0008, -0.0082, 0.0355, ..., 0.0083, -0.0411, 0.0243],\n",
" [ 0.0008, -0.0082, 0.0355, ..., 0.0083, -0.0411, 0.0243],\n",
" [ 0.0008, -0.0082, 0.0355, ..., 0.0083, -0.0411, 0.0243]],\n",
" grad_fn=<DifferentiableGraphBackward>)"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x(th.ones((64, 3, 7, 7)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment