RuntimeError: import torchvision

Kris_Kulivnyk: <p>I try to import torchvision in order to use a fedearted dataset from the tutorial  with a VGG (or any other existing model)-&gt; <a href="https://github.com/OpenMined/PySyft/blob/dev/examples/tutorials/advanced/Federated%20Dataset.ipynb" rel="nofollow noopener">https://github.com/OpenMined/PySyft/blob/dev/examples/tutorials/advanced/Federated%20Dataset.ipynb</a></p>
<p>and I got  <strong>RuntimeError :</strong></p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-15-bd04a42c539f&gt; in &lt;module&gt;
----&gt; 1 import torchvision
      2 model = torchvision.models.vgg11(pretrained=False)
      3 #model = torchvision.models.resnet101(pretrained=False)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/__init__.py in &lt;module&gt;
----&gt; 1 from torchvision import models
      2 from torchvision import datasets
      3 from torchvision import ops
      4 from torchvision import transforms
      5 from torchvision import utils

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/__init__.py in &lt;module&gt;
      9 from .shufflenetv2 import *
     10 from . import segmentation
---&gt; 11 from . import detection

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/detection/__init__.py in &lt;module&gt;
----&gt; 1 from .faster_rcnn import *
      2 from .mask_rcnn import *
      3 from .keypoint_rcnn import *

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/detection/faster_rcnn.py in &lt;module&gt;
     11 
     12 from .generalized_rcnn import GeneralizedRCNN
---&gt; 13 from .rpn import AnchorGenerator, RPNHead, RegionProposalNetwork
     14 from .roi_heads import RoIHeads
     15 from .transform import GeneralizedRCNNTransform

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/detection/rpn.py in &lt;module&gt;
      6 from torchvision.ops import boxes as box_ops
      7 
----&gt; 8 from . import _utils as det_utils
      9 
     10 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/detection/_utils.py in &lt;module&gt;
     72 
     73 
---&gt; 74 @torch.jit.script
     75 def encode_boxes(reference_boxes, proposals, weights):
     76     # type: (torch.Tensor, torch.Tensor, torch.Tensor) -&gt; torch.Tensor

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/jit/__init__.py in script(obj, optimize, _frames_up, _rcb)
    822     else:
    823         ast = get_jit_def(obj)
--&gt; 824         fn = torch._C._jit_script_compile(ast, _rcb, get_default_args(obj))
    825         # Forward docstrings
    826         fn.__doc__ = obj.__doc__

RuntimeError: 

for operator (Tensor 0) -&gt; Tensor:
expected a value of type Tensor for argument '0' but found (Tensor, Tensor, Tensor, Tensor)
    gt_heights = reference_boxes_y2 - reference_boxes_y1
    gt_ctr_x = reference_boxes_x1 + 0.5 * gt_widths
    gt_ctr_y = reference_boxes_y1 + 0.5 * gt_heights

    targets_dx = wx * (gt_ctr_x - ex_ctr_x) / ex_widths
    targets_dy = wy * (gt_ctr_y - ex_ctr_y) / ex_heights
    targets_dw = ww * torch.log(gt_widths / ex_widths)
    targets_dh = wh * torch.log(gt_heights / ex_heights)

    targets = torch.cat((targets_dx, targets_dy, targets_dw, targets_dh), dim=1)
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    return targets
:
    gt_heights = reference_boxes_y2 - reference_boxes_y1
    gt_ctr_x = reference_boxes_x1 + 0.5 * gt_widths
    gt_ctr_y = reference_boxes_y1 + 0.5 * gt_heights

    targets_dx = wx * (gt_ctr_x - ex_ctr_x) / ex_widths
    targets_dy = wy * (gt_ctr_y - ex_ctr_y) / ex_heights
    targets_dw = ww * torch.log(gt_widths / ex_widths)
    targets_dh = wh * torch.log(gt_heights / ex_heights)

    targets = torch.cat((targets_dx, targets_dy, targets_dw, targets_dh), dim=1)
              ~~~~~~~~~ &lt;--- HERE
    return targets
</code></pre>
<p>My code (basically the code from tutorial + imports in the end):</p>
<pre><code class="lang-auto">from torch.utils.data import Dataset
import syft as sy  
import torch

hook = sy.TorchHook(torch)  # &lt;-- NEW: hook PyTorch ie add extra functionalities to support Federated Learning
bob = sy.VirtualWorker(hook, id="bob")  # &lt;-- NEW: define remote worker bob
alice = sy.VirtualWorker(hook, id="alice")  # &lt;-- NEW: and alice



class Arguments():
    def __init__(self):
        self.batch_size = 64
        self.test_batch_size = 1000
        self.epochs = 10
        self.lr = 0.01
        self.momentum = 0.5
        self.no_cuda = False
        self.seed = 1
        self.log_interval = 10
        self.save_model = False

args = Arguments()

use_cuda = not args.no_cuda and torch.cuda.is_available()

torch.manual_seed(args.seed)

device = torch.device("cuda" if use_cuda else "cpu")

kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}


from scipy.io import loadmat
import matplotlib.pyplot as plt

def load_data(path):
    """ Helper function for loading a MAT-File"""
    data = loadmat(path)
    return data['X'], data['y']


train_data , train_labels = load_data("advanced/data/train_32x32.mat")
test_data , test_labels = load_data("advanced/data/test_32x32.mat")

# Transpose the image arrays
X_train, y_train = train_data.transpose((3,0,1,2)), train_labels[:,0]
X_test, y_test = test_data.transpose((3,0,1,2)), test_labels[:,0]

fig=plt.figure(figsize=(8, 8))
columns = 4
rows = 5
for i in range(1,columns*rows+1):
    fig.add_subplot(rows, columns, i)
    plt.imshow(X_train[i])
plt.show()


base=sy.BaseDataset(torch.from_numpy(X_train),torch.from_numpy(y_train))
base_federated=base.federate((bob, alice))
federated_train_loader = sy.FederatedDataLoader( # &lt;-- this is now a FederatedDataLoader 
                         base_federated,batch_size=args.batch_size)



def train(args, model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(federated_train_loader): # &lt;-- now it is a distributed dataset
        model.send(data.location) # &lt;-- NEW: send the model to the right location
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        model.get() # &lt;-- NEW: get the model back
        if batch_idx % args.log_interval == 0:
            loss = loss.get() # &lt;-- NEW: get the loss back
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * args.batch_size, len(train_loader) * args.batch_size, #batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))



import torchvision
import torch.optim as optim
model = torchvision.models.vgg11(pretrained=False)

model = model.to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr) # TODO momentum is not supported at the moment

print(model)
print(optimizer)

for epoch in range(1, args.epochs + 1):
    train(args, model, device, federated_train_loader, optimizer, epoch)
</code></pre>

Pranavan_Theivendira: <aside class="quote no-group" data-username="Kris_Kulivnyk" data-post="1" data-topic="51383">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kris_kulivnyk/48/14235_2.png" class="avatar"> Kris_Kulivnyk:</div>
<blockquote>
<p>targets = torch.cat((targets_dx, targets_dy, targets_dw, targets_dh), dim=1)</p>
</blockquote>
</aside>
<p>Hi,</p>
<p>Try this. Instead of device, use <code>device=device</code> in your code snippet. You have to change in the following line.</p>
<pre><code class="lang-auto">data, target = data.to(device), target.to(device)
</code></pre>
<p>It should be something as follows.</p>
<pre><code class="lang-auto">data, target = data.to(device=device), target.to(device=device)
</code></pre>
<p>Change other places as well if it is relevant. This may help.</p>
<p>Follow a similar issue here - <a href="https://github.com/pytorch/pytorch/issues/15478" class="inline-onebox" rel="noopener nofollow ugc">JIT Tensor .to not functioning, confusing error message · Issue #15478 · pytorch/pytorch · GitHub</a></p>
<p>Thanks</p>

Kris_Kulivnyk: <p>Hi Pranavan_Theivendira!</p>
<p>Unfortunately, it does not help…<br>
Instead, when I put imports in the header (code below), -&gt;</p>
<pre><code class="lang-auto">from torch.utils.data import Dataset
import syft as sy  
import torch
import torchvision
import torch.optim as optim
model = torchvision.models.vgg11(pretrained=False)

hook = sy.TorchHook(torch)  # &lt;-- NEW: hook PyTorch ie add extra functionalities to support Federated Learning
bob = sy.VirtualWorker(hook, id="bob")  # &lt;-- NEW: define remote worker bob
alice = sy.VirtualWorker(hook, id="alice")  # &lt;-- NEW: and alice

class Arguments():
    def __init__(self):
        self.batch_size = 64
        self.test_batch_size = 1000
        self.epochs = 10
        self.lr = 0.01
        self.momentum = 0.5
        self.no_cuda = False
        self.seed = 1
        self.log_interval = 10
        self.save_model = False

args = Arguments()

use_cuda = not args.no_cuda and torch.cuda.is_available()

torch.manual_seed(args.seed)

device = torch.device("cuda" if use_cuda else "cpu")

kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

from scipy.io import loadmat
import matplotlib.pyplot as plt

def load_data(path):
    """ Helper function for loading a MAT-File"""
    data = loadmat(path)
    return data['X'], data['y']

train_data , train_labels = load_data("advanced/data/train_32x32.mat")
test_data , test_labels = load_data("advanced/data/test_32x32.mat")

print(train_data.shape)
print(train_labels.shape)


# Transpose the image arrays
X_train, y_train = train_data.transpose((3,0,1,2)), train_labels[:,0]
X_test, y_test = test_data.transpose((3,0,1,2)), test_labels[:,0]

print(X_train.shape)
print(y_train.shape)

fig=plt.figure(figsize=(8, 8))
columns = 4
rows = 5
for i in range(1,columns*rows+1):
    fig.add_subplot(rows, columns, i)
    plt.imshow(X_train[i])
plt.show()

base=sy.BaseDataset(torch.from_numpy(X_train),torch.from_numpy(y_train))
base_federated=base.federate((bob, alice))
federated_train_loader = sy.FederatedDataLoader( # &lt;-- this is now a FederatedDataLoader 
                         base_federated,batch_size=args.batch_size)

def train(args, model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(federated_train_loader): # &lt;-- now it is a distributed dataset
        model.send(data.location) # &lt;-- NEW: send the model to the right location
        data, target = data.to(device=device), target.to(device=device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        model.get() # &lt;-- NEW: get the model back
        if batch_idx % args.log_interval == 0:
            loss = loss.get() # &lt;-- NEW: get the loss back
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * args.batch_size, len(train_loader) * args.batch_size, #batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))


model = model.to(device=device)
optimizer = optim.SGD(model.parameters(), lr=args.lr) # TODO momentum is not supported at the moment

print(model)
print(optimizer)

for epoch in range(1, args.epochs + 1):
    train(args, model, device, federated_train_loader, optimizer, epoch)
</code></pre>
<p>I have another kind of error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
PureTorchTensorFoundError                 Traceback (most recent call last)
~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/tensors/interpreters/native.py in handle_func_command(cls, command)
    259             new_args, new_kwargs, new_type, args_type = syft.frameworks.torch.hook_args.hook_function_args(
--&gt; 260                 cmd, args, kwargs, return_args_type=True
    261             )

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in hook_function_args(attr, args, kwargs, return_args_type)
    156         # Try running it
--&gt; 157         new_args = hook_args(args)
    158 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;lambda&gt;(x)
    350 
--&gt; 351     return lambda x: f(lambdas, x)
    352 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in seven_fold(lambdas, args, **kwargs)
    558     return (
--&gt; 559         lambdas[0](args[0], **kwargs),
    560         lambdas[1](args[1], **kwargs),

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;lambda&gt;(i)
    328         # Last if not, rule is probably == 1 so use type to return the right transformation.
--&gt; 329         else lambda i: forward_func[type(i)](i)
    330         for a, r in zip(args, rules)  # And do this for all the args / rules provided

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;lambda&gt;(i)
     55     if hasattr(i, "child")
---&gt; 56     else (_ for _ in ()).throw(PureTorchTensorFoundError),
     57     torch.nn.Parameter: lambda i: i.child

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;genexpr&gt;(.0)
     55     if hasattr(i, "child")
---&gt; 56     else (_ for _ in ()).throw(PureTorchTensorFoundError),
     57     torch.nn.Parameter: lambda i: i.child

PureTorchTensorFoundError: 

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-16-448339389201&gt; in &lt;module&gt;
      1 for epoch in range(1, args.epochs + 1):
----&gt; 2     train(args, model, device, federated_train_loader, optimizer, epoch)

&lt;ipython-input-14-cab4c7130012&gt; in train(args, model, device, train_loader, optimizer, epoch)
      5         data, target = data.to(device=device), target.to(device=device)
      6         optimizer.zero_grad()
----&gt; 7         output = model(data)
      8         loss = F.nll_loss(output, target)
      9         loss.backward()

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--&gt; 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/vgg.py in forward(self, x)
     40 
     41     def forward(self, x):
---&gt; 42         x = self.features(x)
     43         x = self.avgpool(x)
     44         x = x.view(x.size(0), -1)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--&gt; 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
     90     def forward(self, input):
     91         for module in self._modules.values():
---&gt; 92             input = module(input)
     93         return input
     94 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--&gt; 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
    336                             _pair(0), self.dilation, self.groups)
    337         return F.conv2d(input, self.weight, self.bias, self.stride,
--&gt; 338                         self.padding, self.dilation, self.groups)
    339 
    340 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook.py in overloaded_func(*args, **kwargs)
    715             cmd_name = f"{attr.__module__}.{attr.__name__}"
    716             command = (cmd_name, None, args, kwargs)
--&gt; 717             response = TorchTensor.handle_func_command(command)
    718             return response
    719 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/tensors/interpreters/native.py in handle_func_command(cls, command)
    268             new_command = (cmd, None, new_args, new_kwargs)
    269             # Send it to the appropriate class and get the response
--&gt; 270             response = new_type.handle_func_command(new_command)
    271             # Put back the wrappers where needed
    272             response = syft.frameworks.torch.hook_args.hook_response(

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/pointers/object_pointer.py in handle_func_command(cls, command)
     86 
     87         # Send the command
---&gt; 88         response = owner.send_command(location, command)
     89 
     90         return response

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in send_command(self, recipient, message, return_ids)
    425 
    426         try:
--&gt; 427             ret_val = self.send_msg(codes.MSGTYPE.CMD, message, location=recipient)
    428         except ResponseSignatureError as e:
    429             ret_val = None

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in send_msg(self, msg_type, message, location)
    221 
    222         # Step 2: send the message and wait for a response
--&gt; 223         bin_response = self._send_msg(bin_message, location)
    224 
    225         # Step 3: deserialize the response

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/virtual.py in _send_msg(self, message, location)
      8 class VirtualWorker(BaseWorker, FederatedClient):
      9     def _send_msg(self, message: bin, location: BaseWorker) -&gt; bin:
---&gt; 10         return location._recv_msg(message)
     11 
     12     def _recv_msg(self, message: bin) -&gt; bin:

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/virtual.py in _recv_msg(self, message)
     11 
     12     def _recv_msg(self, message: bin) -&gt; bin:
---&gt; 13         return self.recv_msg(message)
     14 
     15     @staticmethod

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in recv_msg(self, bin_message)
    252             print(f"worker {self} received {sy.codes.code2MSGTYPE[msg_type]} {contents}")
    253         # Step 1: route message to appropriate function
--&gt; 254         response = self._message_router[msg_type](contents)
    255 
    256         # Step 2: Serialize the message to simple python objects

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in execute_command(self, message)
    383                 command = getattr(command, path)
    384 
--&gt; 385             response = command(*args, **kwargs)
    386 
    387         # some functions don't return anything (such as .backward())

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook.py in overloaded_func(*args, **kwargs)
    715             cmd_name = f"{attr.__module__}.{attr.__name__}"
    716             command = (cmd_name, None, args, kwargs)
--&gt; 717             response = TorchTensor.handle_func_command(command)
    718             return response
    719 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/tensors/interpreters/native.py in handle_func_command(cls, command)
    285             # in the execute_command function
    286             if isinstance(args, tuple):
--&gt; 287                 response = eval(cmd)(*args, **kwargs)
    288             else:
    289                 response = eval(cmd)(args, **kwargs)

RuntimeError: Given groups=1, weight of size 64 3 3 3, expected input[64, 32, 32, 3] to have 3 channels, but got 32 channels instead

</code></pre>
<p>Any idea of how to solve this?</p>

Pranavan_Theivendira: <p>Hi,</p>
<p>There is some problem with the input that you feed. In pytorch, we feed input as <strong>BxCxHxW</strong>. I think in your case, you are feeding channel as the last dimension. The input should be <strong>input[64, 32, 32, 3] to have 3 channels, but got 32 channels instead</strong>. I think the input should be <strong>[64,3,32,32]</strong>. You can do this by following line.</p>
<pre><code class="lang-auto">X = X.permute((0,3,1,2))
</code></pre>
<p>Here X is the input.</p>
<p>Thanks</p>

Kris_Kulivnyk: <p>Thank you Pranavan. It helps to solve the issue with input, but in meantime I receive a new problem</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
PureTorchTensorFoundError                 Traceback (most recent call last)
~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/tensors/interpreters/native.py in handle_func_command(cls, command)
    259             new_args, new_kwargs, new_type, args_type = syft.frameworks.torch.hook_args.hook_function_args(
--&gt; 260                 cmd, args, kwargs, return_args_type=True
    261             )

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in hook_function_args(attr, args, kwargs, return_args_type)
    156         # Try running it
--&gt; 157         new_args = hook_args(args)
    158 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;lambda&gt;(x)
    350 
--&gt; 351     return lambda x: f(lambdas, x)
    352 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in seven_fold(lambdas, args, **kwargs)
    558     return (
--&gt; 559         lambdas[0](args[0], **kwargs),
    560         lambdas[1](args[1], **kwargs),

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;lambda&gt;(i)
    328         # Last if not, rule is probably == 1 so use type to return the right transformation.
--&gt; 329         else lambda i: forward_func[type(i)](i)
    330         for a, r in zip(args, rules)  # And do this for all the args / rules provided

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;lambda&gt;(i)
     55     if hasattr(i, "child")
---&gt; 56     else (_ for _ in ()).throw(PureTorchTensorFoundError),
     57     torch.nn.Parameter: lambda i: i.child

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook_args.py in &lt;genexpr&gt;(.0)
     55     if hasattr(i, "child")
---&gt; 56     else (_ for _ in ()).throw(PureTorchTensorFoundError),
     57     torch.nn.Parameter: lambda i: i.child

PureTorchTensorFoundError: 

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-4-87b3b466f80f&gt; in &lt;module&gt;
     93 
     94 for epoch in range(1, args.epochs + 1):
---&gt; 95     train(args, model, device, federated_train_loader, optimizer, epoch)

&lt;ipython-input-4-87b3b466f80f&gt; in train(args, model, device, train_loader, optimizer, epoch)
     74         data, target = data.to(device=device), target.to(device=device)
     75         optimizer.zero_grad()
---&gt; 76         output = model(data)
     77         loss = F.nll_loss(output, target)
     78         loss.backward()

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--&gt; 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torchvision/models/vgg.py in forward(self, x)
     40 
     41     def forward(self, x):
---&gt; 42         x = self.features(x)
     43         x = self.avgpool(x)
     44         x = x.view(x.size(0), -1)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--&gt; 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
     90     def forward(self, input):
     91         for module in self._modules.values():
---&gt; 92             input = module(input)
     93         return input
     94 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--&gt; 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
    336                             _pair(0), self.dilation, self.groups)
    337         return F.conv2d(input, self.weight, self.bias, self.stride,
--&gt; 338                         self.padding, self.dilation, self.groups)
    339 
    340 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook.py in overloaded_func(*args, **kwargs)
    715             cmd_name = f"{attr.__module__}.{attr.__name__}"
    716             command = (cmd_name, None, args, kwargs)
--&gt; 717             response = TorchTensor.handle_func_command(command)
    718             return response
    719 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/tensors/interpreters/native.py in handle_func_command(cls, command)
    268             new_command = (cmd, None, new_args, new_kwargs)
    269             # Send it to the appropriate class and get the response
--&gt; 270             response = new_type.handle_func_command(new_command)
    271             # Put back the wrappers where needed
    272             response = syft.frameworks.torch.hook_args.hook_response(

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/pointers/object_pointer.py in handle_func_command(cls, command)
     86 
     87         # Send the command
---&gt; 88         response = owner.send_command(location, command)
     89 
     90         return response

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in send_command(self, recipient, message, return_ids)
    425 
    426         try:
--&gt; 427             ret_val = self.send_msg(codes.MSGTYPE.CMD, message, location=recipient)
    428         except ResponseSignatureError as e:
    429             ret_val = None

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in send_msg(self, msg_type, message, location)
    221 
    222         # Step 2: send the message and wait for a response
--&gt; 223         bin_response = self._send_msg(bin_message, location)
    224 
    225         # Step 3: deserialize the response

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/virtual.py in _send_msg(self, message, location)
      8 class VirtualWorker(BaseWorker, FederatedClient):
      9     def _send_msg(self, message: bin, location: BaseWorker) -&gt; bin:
---&gt; 10         return location._recv_msg(message)
     11 
     12     def _recv_msg(self, message: bin) -&gt; bin:

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/virtual.py in _recv_msg(self, message)
     11 
     12     def _recv_msg(self, message: bin) -&gt; bin:
---&gt; 13         return self.recv_msg(message)
     14 
     15     @staticmethod

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in recv_msg(self, bin_message)
    252             print(f"worker {self} received {sy.codes.code2MSGTYPE[msg_type]} {contents}")
    253         # Step 1: route message to appropriate function
--&gt; 254         response = self._message_router[msg_type](contents)
    255 
    256         # Step 2: Serialize the message to simple python objects

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/workers/base.py in execute_command(self, message)
    383                 command = getattr(command, path)
    384 
--&gt; 385             response = command(*args, **kwargs)
    386 
    387         # some functions don't return anything (such as .backward())

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/hook/hook.py in overloaded_func(*args, **kwargs)
    715             cmd_name = f"{attr.__module__}.{attr.__name__}"
    716             command = (cmd_name, None, args, kwargs)
--&gt; 717             response = TorchTensor.handle_func_command(command)
    718             return response
    719 

~/miniconda3/envs/pysyft/lib/python3.7/site-packages/syft/frameworks/torch/tensors/interpreters/native.py in handle_func_command(cls, command)
    285             # in the execute_command function
    286             if isinstance(args, tuple):
--&gt; 287                 response = eval(cmd)(*args, **kwargs)
    288             else:
    289                 response = eval(cmd)(args, **kwargs)

RuntimeError: _thnn_conv2d_forward not supported on CPUType for Byte

</code></pre>
<p>Do you have any ideas how to solve this kind of problems?</p>

Pranavan_Theivendira: <aside class="quote no-group" data-username="Kris_Kulivnyk" data-post="5" data-topic="51383">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kris_kulivnyk/48/14235_2.png" class="avatar"> Kris_Kulivnyk:</div>
<blockquote>
<p>_thnn_conv2d_forward not supported on CPUType for Byte</p>
</blockquote>
</aside>
<p>Hi,</p>
<p>This is a type issue. You can do the following to change your input to other type.</p>
<pre><code class="lang-auto">X = X.float()
</code></pre>
<p>Here <code>X</code> is the input. Hope this should help.</p>
<p>Thanks</p>

Adrian_Rojas: <p>Hi, I’m facing the same problem I thought that could be something in my code so I removed everything and just keep the imports as follows:</p>
<pre><code class="lang-auto">%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import matplotlib.pyplot as plt

import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import models
</code></pre>
<p>and I still got <strong>RuntimeError:</strong></p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-37-eda6ee908992&gt; in &lt;module&gt;
      5 from torch import optim
      6 import torch.nn.functional as F
----&gt; 7 from torchvision import models
      8 # Use GPU if it's available

~\Anaconda3\envs\pysyft\lib\site-packages\torchvision\__init__.py in &lt;module&gt;
----&gt; 1 from torchvision import models
      2 from torchvision import datasets
      3 from torchvision import ops
      4 from torchvision import transforms
      5 from torchvision import utils

~\Anaconda3\envs\pysyft\lib\site-packages\torchvision\models\__init__.py in &lt;module&gt;
      9 from .shufflenetv2 import *
     10 from . import segmentation
---&gt; 11 from . import detection

~\Anaconda3\envs\pysyft\lib\site-packages\torchvision\models\detection\__init__.py in &lt;module&gt;
----&gt; 1 from .faster_rcnn import *
      2 from .mask_rcnn import *
      3 from .keypoint_rcnn import *

~\Anaconda3\envs\pysyft\lib\site-packages\torchvision\models\detection\faster_rcnn.py in &lt;module&gt;
     11 
     12 from .generalized_rcnn import GeneralizedRCNN
---&gt; 13 from .rpn import AnchorGenerator, RPNHead, RegionProposalNetwork
     14 from .roi_heads import RoIHeads
     15 from .transform import GeneralizedRCNNTransform

~\Anaconda3\envs\pysyft\lib\site-packages\torchvision\models\detection\rpn.py in &lt;module&gt;
      6 from torchvision.ops import boxes as box_ops
      7 
----&gt; 8 from . import _utils as det_utils
      9 
     10 

~\Anaconda3\envs\pysyft\lib\site-packages\torchvision\models\detection\_utils.py in &lt;module&gt;
     72 
     73 
---&gt; 74 @torch.jit.script
     75 def encode_boxes(reference_boxes, proposals, weights):
     76     # type: (torch.Tensor, torch.Tensor, torch.Tensor) -&gt; torch.Tensor

~\Anaconda3\envs\pysyft\lib\site-packages\torch\jit\__init__.py in script(obj, optimize, _frames_up, _rcb)
    822     else:
    823         ast = get_jit_def(obj)
--&gt; 824         fn = torch._C._jit_script_compile(ast, _rcb, get_default_args(obj))
    825         # Forward docstrings
    826         fn.__doc__ = obj.__doc__

RuntimeError: 

for operator (Tensor 0) -&gt; Tensor:
expected a value of type Tensor for argument '0' but found (Tensor, Tensor, Tensor, Tensor)
    gt_heights = reference_boxes_y2 - reference_boxes_y1
    gt_ctr_x = reference_boxes_x1 + 0.5 * gt_widths
    gt_ctr_y = reference_boxes_y1 + 0.5 * gt_heights

    targets_dx = wx * (gt_ctr_x - ex_ctr_x) / ex_widths
    targets_dy = wy * (gt_ctr_y - ex_ctr_y) / ex_heights
    targets_dw = ww * torch.log(gt_widths / ex_widths)
    targets_dh = wh * torch.log(gt_heights / ex_heights)

    targets = torch.cat((targets_dx, targets_dy, targets_dw, targets_dh), dim=1)
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    return targets
:
    gt_heights = reference_boxes_y2 - reference_boxes_y1
    gt_ctr_x = reference_boxes_x1 + 0.5 * gt_widths
    gt_ctr_y = reference_boxes_y1 + 0.5 * gt_heights

    targets_dx = wx * (gt_ctr_x - ex_ctr_x) / ex_widths
    targets_dy = wy * (gt_ctr_y - ex_ctr_y) / ex_heights
    targets_dw = ww * torch.log(gt_widths / ex_widths)
    targets_dh = wh * torch.log(gt_heights / ex_heights)

    targets = torch.cat((targets_dx, targets_dy, targets_dw, targets_dh), dim=1)
              ~~~~~~~~~ &lt;--- HERE
    return targets


</code></pre>

ptrblck: <p>Did you update <code>PyTorch</code> and <code>torchvision</code> recently or just one of these?<br>
Based on your code snippet, it looks like you’re getting this error just after importing <code>torchvision.models</code>.<br>
In the recent stable PyTorch version (<code>1.2.0</code>) a lot of changes were made to the JIT, so that I would also update torchvision to <code>0.4.0</code>.</p>

John_Deterious: <p>I updated to 1.2<br>
I get error when importing torchvision<br>
It is very long error, the last line is:</p>
<p>ImportError: DLL load failed: The specified module could not be found.</p>
<p>It sounds like torchvision that comes with 1.2 is of version 0.4<br>
I don’t know why. This for windows, installation via conda</p>

ptrblck: <p>Have a look at <a href="https://discuss.pytorch.org/t/pytorch-1-2-windows/52959/11">this post</a> and <a href="https://github.com/pytorch/pytorch/issues/24141#issuecomment-520207774" rel="nofollow noopener">this one</a> where <a class="mention" href="/u/peterjc123">@peterjc123</a> posted the windows binaries, which should be uploaded soon (or already are).</p>