gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
857 stars 143 forks source link

'Tensor' object has no attribute '_keras_shape' #97

Open Guocode opened 5 years ago

Guocode commented 5 years ago
# create pytorch model

from pytorch2keras import pytorch_to_keras
from torch.autograd import Variable

from models.mobilenet import PoseEstimationWithMobileNet

model = PoseEstimationWithMobileNet(num_class=10)

# create input tensor
import numpy as np
import torch
input_np = np.random.uniform(-1, 1, (1, 3, 224, 224)).astype(np.float32)

input_var = Variable(torch.FloatTensor(input_np))

# pytorch2keras.converter.pytorch_to_keras()
# we should specify shape of the input tensor
k_model = pytorch_to_keras(model, input_var, [(3, 224, 224)], verbose=True)

print(k_model)

Environment (please complete the following information):

Error info is following

DEBUG:onnx2keras:... found all, continue
Using TensorFlow backend.
Traceback (most recent call last):
  File "/data_sdc/gzh/tmp/gesture_recog/utils/pth2keras.py", line 20, in <module>
    k_model = pytorch_to_keras(model, input_var, [(3, 224, 224)], verbose=True)
  File "/root/anaconda3/envs/py36v/lib/python3.6/site-packages/pytorch2keras/converter.py", line 73, in pytorch_to_keras
    verbose=verbose, change_ordering=change_ordering)
  File "/root/anaconda3/envs/py36v/lib/python3.6/site-packages/onnx2keras/converter.py", line 174, in onnx_to_keras
    keras_names
  File "/root/anaconda3/envs/py36v/lib/python3.6/site-packages/onnx2keras/operation_layers.py", line 133, in convert_reduce_mean
    layers[node_name].set_shape(layers[node_name]._keras_shape)
AttributeError: 'Tensor' object has no attribute '_keras_shape'

My model is simply the official torchvision mobilenetv2, what's the problem here?

jiangzhubo commented 5 years ago

i met this problem too, dont know what going go

jiangzhubo commented 5 years ago

@Guocode just direct change '_keras_shape' to '.shape' ,then will be fine, i solve it

tarunnith commented 3 years ago

from keras import backend as K

replace filters = init._keras_shape[channel_axis]

with

filters = init.shape[channel_axis]

deevyankar commented 3 years ago

@Guocode just direct change '_keras_shape' to '.shape' ,then will be fine, i solve it

how to change in resnet.py?