title: How to load torch pretrained models in Python?
body: <p>I have a torch .t7 file of a binarized pretrained model of the AlexNet. I need to convert this .t7 file to a npz/h5 file so I can import it comfortably.</p>
  
  <p>It is the alexnet_XNOR by allenAI. </p>
  
  <p>This is the code being used to read the file: <a href="https://github.com/bshillingford/python-torchfile/blob/master/torchfile.py" rel="nofollow noreferrer">https://github.com/bshillingford/python-torchfile/blob/master/torchfile.py</a></p>
  
  <pre><code>import torchfile as T
  o = T.load('alexnet_XNOR.t7')
  </code></pre>
  
  <p>This is the error that shows up:</p>
  
  <pre><code>File "&lt;ipython-input-5-7ef550de1de7&gt;", line 1, in &lt;module&gt;
  runfile('C:/Users/yash/Desktop/importer.py', wdir='C:/Users/yash/Desktop')
  
  File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
  execfile(filename, namespace)
  
  File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
  exec(compile(f.read(), filename, 'exec'), namespace)
  
  File "C:/Users/yash/Desktop/importer.py", line 10, in &lt;module&gt;
  o = T.load('alexnet_XNOR.t7')
  
  File "C:\Users\yash\Desktop\torchreader.py", line 430, in load
  return reader.read_obj()
  
  File "C:\Users\yash\Desktop\torchreader.py", line 377, in read_obj
  obj._obj = self.read_obj()
  
  File "C:\Users\yash\Desktop\torchreader.py", line 394, in read_obj
  obj[k] = v
  
  TypeError: unhashable type: 'numpy.ndarray'
  </code></pre>
  
  <p>My attempt:
  I modified the torchreader and ran the main code (added print statements):</p>
  
  <pre><code>for _ in range(size):
              k = self.read_obj()
              v = self.read_obj()
              print(obj.shape)
              print(v.shape)
              obj[k] = v
              if self.use_list_heuristic:
                  if not isinstance(k, int) or k &lt;= 0:
                      keys_natural = False
                  elif isinstance(k, int):
                      key_sum += k
  </code></pre>
  
  <p>It said None for obj.shape, and for v.shape, it said:</p>
  
  <h2>    AttributeError: 'bytes' object has no attribute 'shape'</h2>
  
  <pre><code>print(obj)
  print("===========")
  print(v)
  </code></pre>
  
  <p>When i simply print obj and v using the above snippet code, I get this followed by simply the error i got initially:</p>
  
  <pre><code>{}
  =================
  b'torch.CudaTensor'
  {b'_type': b'torch.CudaTensor'}
  =================
  []
  {b'output': array([], dtype=float32), b'_type': b'torch.CudaTensor'}
  =================
  b'gradInput'
  {None: b'gradInput', b'output': array([], dtype=float32), b'_type': b'torch.CudaTensor'}
  =================
  [ 0.]
  </code></pre>
  
  <p>Can someone explain why this error is happening? I cannot understand the codebase itself as I have not worked with torch before.</p>
  
  <p>Thank you!</p>