Giter Site home page Giter Site logo

caffemodel2pytorch's Introduction

Hi there 👋

I'm looking for a research internship or research engineer internship for summer/fall 2021 on Computer Vision / Speech / NLP / Deep Learning / Machine Learning.

My CV: https://github.com/vadimkantorov/vadimkantorov.com/releases/download/cv/cv_KANTOROV_Vadim_research.pdf

My LinkedIn: https://linkedin.com/in/vadimkantorov

My Scholar: https://scholar.google.com/citations?user=pEDJ-8cAAAAJ

My e-mail: [email protected]

I'm eligible for work in EU / Israel / Russia / Switzerland. Please reach out if you think I may be a good fit!

caffemodel2pytorch's People

Contributors

abhisheksambyal avatar kanezaki avatar penway avatar sebftw avatar vadimkantorov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

caffemodel2pytorch's Issues

google.protobuf.text_format.ParseError: 52:3 : Message type "caffe.LayerParameter" has no field named "bn_param".

Dear @vadimkantorov , when running your code I encounter the error:

google.protobuf.text_format.ParseError: 52:3 : Message type "caffe.LayerParameter" has no field named "bn_param".

It is raised in line of your script caffemodel2pytorch.py

I initialize the model as:

kwargs = {'caffe_proto' : 'https://raw.githubusercontent.com/BVLC/caffe/master/src/caffe/proto/caffe.proto',
         'phase'    : caffe.TEST, 
         'weights'  : './models/resnet200_anet_2016.caffemodel'
            }
prototxt='./models/resnet200_anet_2016_deploy.prototxt'

model = caffe.Net(prototxt, **kwargs)

You can download the prototxt and caffemodel files with the commands:

wget -O models/resnet200_anet_2016.caffemodel https://yjxiong.blob.core.windows.net/anet-2016/resnet200_anet_2016.caffemodel
wget -O models/resnet200_anet_2016_deploy.prototxt https://yjxiong.blob.core.windows.net/anet-2016/resnet200_anet_2016_deploy.prototxt

Could you please help me out with this issue?

Best,
Mattia

URL reading doensn't work in Python 3

Reading and writing the URL to fine does not work in python 3 because the file is in bytes

f.write((urlopen if 'http' in caffe_proto else open)(caffe_proto).read())

I tried using .decode('utf-8) but there is conversion problem

When prototxt does not have operation in eltwise layer, it raises error

When prototxt does not have operation in eltwise layer, it raises error (ex: https://github.com/KaimingHe/deep-residual-networks/blob/master/prototxt/ResNet-50-deploy.prototxt).

In caffe.proto, default operation is set as SUM.

    caffe_proto = 'https://raw.githubusercontent.com/BVLC/caffe/master/src/caffe/proto/caffe.proto'
  File "./caffemodel2pytorch/caffemodel2pytorch.py", line 83, in __init__    module = module_constructor(param)
  File "./caffemodel2pytorch/caffemodel2pytorch.py", line 291, in <lambda>
    Eltwise = lambda param: [torch.mul, torch.add, torch.max][param['operation']]
KeyError: 'operation'

What mean of Skipping Layer??

Using your modules (caffemodel2pytorch),
I tested your sample code.


model = caffemodel2pytorch.Net(
    prototxt = 'caffemodel2pytorch/ubuntu14.04_bvlc_epoch_100.0/inception_v1_deploy.prototxt',
    weights = 'caffemodel2pytorch/ubuntu14.04_bvlc_epoch_100.0/inception_v1_iter_156251.caffemodel',
    caffe_proto = 'caffe/src/caffe/proto/caffe.proto'
    )
model.cuda()
model.eval()

torch.set_grad_enabled(False)

# make sure to have right procedure of image normalization and channel reordering
image = torch.autograd.Variable(torch.Tensor(8, 3, 224, 224).cuda())

# outputs dict of pytorch Variables
# in this example the dict contains the only key 'prob'
output_dict = model(data=image)

I met this message.

Skipping layer [data, Input, Input]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_3a/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_3b/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_4a/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_4b/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_4c/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_4d/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_4e/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_5a/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
Skipping layer [inception_5b/output, Concat, Concat]: not found in caffemodel2pytorch.modules dict
...
File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 489, in call
result = self.forward(*input, **kwargs)
File "caffemodel2pytorch/caffemodel2pytorch.py", line 122, in forward
assert name in variables, 'Variable [{}] does not exist. Pass it as a keyword argument or provide a layer which produces it.'.format(name)
AssertionError: Variable [inception_3a/output] does not exist. Pass it as a keyword argument or provide a layer which produces it.

FileNotFoundError: [Errno 2] No such file or directory: 'protoc': 'protoc'

I'm getting the following error while converting to pytorch/keras.

Traceback (most recent call last):
  File "/home/vmuser/anaconda3/envs/torch_old/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/vmuser/anaconda3/envs/torch_old/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/vmuser/Data/deblur/LP_deblur_cafee/caffemodel2pytorch/caffemodel2pytorch.py", line 386, in <module>
    net_param = initialize(args.caffe_proto).NetParameter()
  File "/home/vmuser/Data/deblur/LP_deblur_cafee/caffemodel2pytorch/caffemodel2pytorch.py", line 39, in initialize
    subprocess.check_call(['protoc', '--proto_path', os.path.dirname(local_caffe_proto), '--python_out', codegen_dir, local_caffe_proto])
  File "/home/vmuser/anaconda3/envs/torch_old/lib/python3.6/subprocess.py", line 306, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/home/vmuser/anaconda3/envs/torch_old/lib/python3.6/subprocess.py", line 287, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/home/vmuser/anaconda3/envs/torch_old/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/home/vmuser/anaconda3/envs/torch_old/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'protoc': 'protoc'

Run Caffe models using PyTorch as backend

i tried do this :
import torch
import caffemodel2pytorch

model = caffemodel2pytorch.Net(
prototxt = 'VGG_ILSVRC_16_layers_deploy.prototxt',
weights = 'VGG_ILSVRC_16_layers.caffemodel',
caffe_proto = 'https://github.com/intel/caffe/blob/master/src/caffe/proto/caffe.proto/')
model.cuda()
model.eval()

it just showed the error :

32         if caffe_pb2 is None:
 33                 local_caffe_proto = os.path.join(codegen_dir, os.path.basename(caffe_proto))

---> 34 with open(local_caffe_proto, 'w') as f:
35 mybytes = urlopen(caffe_proto).read()
36 mystr = mybytes.decode('ascii', 'ignore')

IsADirectoryError: [Errno 21] Is a directory: '/tmp/tmpdrkuoyvy/'

can you help me? thanks

3D support and Deconvolution

Hey Vadim,

In a fork I add 3D support and a Deconvolution layer. I put 2D layers and 3D layers in separate files. If you want I can make a pull request to pull it in this repo. Do you want that?

Best,

Dirk

pytorch2caffemodel

Dear sir :
recently I am researching how to covert pytorch model to caffemodel ,would you like to share a tool that can convert pytorch model to caffemodel?or can you give me some advice ?

Error in converting model with Fast RCNN

Got this error when trying to convert a model with prototext:

roi_pooling_param { pooled_w: 7 pooled_h: 7 spatial_scale: 0.0625 # 1/16 }

Here is the error message:
google.protobuf.text_format.Parse(open(prototxt).read(), self.net_param) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 697, in Parse allow_unknown_field=allow_unknown_field) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 769, in ParseLines return parser.ParseLines(lines, message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 822, in ParseLines self._ParseOrMerge(lines, message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 850, in _ParseOrMerge self._MergeField(tokenizer, message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 980, in _MergeField merger(tokenizer, message, field) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 1055, in _MergeMessageField self._MergeField(tokenizer, sub_message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 947, in _MergeField (message_descriptor.full_name, name)) google.protobuf.text_format.ParseError: 393:3 : Message type "caffe.LayerParameter" has no field named "roi_pooling_param".

AttributeError: module 'google.protobuf.descriptor' has no attribute '_message'

what is the version of the protobuf you use?
I just copied

import torch
from caffemodel2pytorch import caffemodel2pytorch

model = caffemodel2pytorch.Net(
	prototxt = 'mobilenet_deploy.prototxt',
	weights = 'mobilenet.caffemodel',
	caffe_proto = 'https://raw.githubusercontent.com/BVLC/caffe/master/src/caffe/proto/caffe.proto'
)
model.cuda()
model.eval()
torch.set_grad_enabled(False)

it just showed the error :

Traceback (most recent call last):
  File "test_torch.py", line 7, in <module>
    caffe_proto = 'https://raw.githubusercontent.com/BVLC/caffe/master/src/caffe
/proto/caffe.proto'
  File "C:\LPR\MobileNet-Caffe\caffemodel2pytorch\caffemodel2pytorch.py", line 7
1, in __init__
    self.net_param = initialize(caffe_proto).NetParameter()
  File "C:\LPR\MobileNet-Caffe\caffemodel2pytorch\caffemodel2pytorch.py", line 4
1, in initialize
    old_pool = google.protobuf.descriptor._message.default_pool
AttributeError: module 'google.protobuf.descriptor' has no attribute '_message'

how do i install it

I did it and got an error
import caffemodel2pytorch as caffe
caffe.set_mode_gpu()
caffe.set_device(0)

AttributeError Traceback (most recent call last)
in ()
1 get_ipython().magic('cd /content/caffemodel2pytorch')
2 import caffemodel2pytorch as caffe
----> 3 caffe.set_mode_gpu()
4 caffe.set_device(0)

AttributeError: module 'caffemodel2pytorch' has no attribute 'set_mode_gpu'

Attribute error

Im working on the project, micro-structural reconstruction using machine learning and im using the MCRpy and I'm having this attribute error.

import mcrpy as mcr
import numpy as np
from PIL import Image

Load the image using Pillow

image = Image.open('/content/drive/MyDrive/AlubronzeCuAl20v500.png')

Convert the image to a NumPy array

slice_2d = np.array(image)

Define the size of the 3D volume element

size_3d = (100, 100, 100)

Define the descriptors to use

descriptors = [mcr.Descriptor.TwoPointCorrelationFFT()]

Reconstruct the 3D volume element

volume_3d = mcr.reconstruct(slice_2d, size_3d, descriptors)

the error
AttributeError: module 'mcrpy' has no attribute 'Descriptor'

FileNotFoundError: [WinError 2]

When I run caffemodel,I got this error,Can you offer me some help?
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\caffemodel\caffemodel2pytorch\caffemodel2pytorch.py", line 383, in
net_param = initialize(args.caffe_proto).NetParameter()
File "C:\Users\Admin\Desktop\caffemodel\caffemodel2pytorch\caffemodel2pytorch.py", line 39, in initialize
subprocess.check_call(['protoc', '--proto_path', os.path.dirname(local_caffe_proto), '--python_out', codegen_dir, local_caffe_proto])
File "C:\Users\Admin\anaconda3\envs\python3.8\lib\subprocess.py", line 359, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Users\Admin\anaconda3\envs\python3.8\lib\subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Admin\anaconda3\envs\python3.8\lib\subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Admin\anaconda3\envs\python3.8\lib\subprocess.py", line 1327, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the specified file。(I've translated the Chinese version of the error.)

TypeError: expected str, bytes or os.PathLike object, not NoneType

Hi! I'm trying to run your example "Imitate pycaffe interface to help in porting", but an error pops up

Traceback (most recent call last):
File "py.py", line 9, in
net = caffe.Net('tracker.prototxt', caffe.TEST, weights = 'tracker.caffemodel')
File "/media/veracrypt1/work/examples/trackers/caffemodel2pytorch-master/caffemodel2pytorch.py", line 71, in init
self.net_param = initialize(caffe_proto).NetParameter()
File "/media/veracrypt1/work/examples/trackers/caffemodel2pytorch-master/caffemodel2pytorch.py", line 33, in initialize
local_caffe_proto = os.path.join(codegen_dir, os.path.basename(caffe_proto))
File "/usr/local/lib/python3.7/posixpath.py", line 146, in basename
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

caffe to pytorch model

Hi!
I am trying to use your tool to convert a caffe model to a pytorch model. Basically I have this .protxt file and this as a caffemodel. Would it be possible to use your tool and directly get a pytorch model which can be immediately used for prediction like its equivalent caffe model?

Thanks,
Arnab

downloaded caffe.proto cannot be read, saying FileNotFoundError, win10, python3

Traceback (most recent call last):
File "caffe2PytorchTry001.py", line 7, in
caffe_proto = 'https://raw.githubuserco
File "F:\Projects\VesselSupportClassify2
self.net_param = initialize(caffe_proto
File "F:\Projects\VesselSupportClassify2
subprocess.check_call(['protoc', '--pro
File "C:\Users\q\AppData\Local\Program
retcode = call(*popenargs, **kwargs)
File "C:\Users\q\AppData\Local\Program
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\q\AppData\Local\Program
restore_signals, start_new_session)
File "C:\Users\q\AppData\Local\Program
startupinfo)
FileNotFoundError: [WinError 2] system cannot find out this file
while the file do exists in temp file, but cannot be read

not work on win10, python 3

can you help me to solved this problem?

Add support for older caffe model

The original C3D model (used by the fantastic anomaly detection work of Waqas https://github.com/WaqasSultani/AnomalyDetectionCVPR2018) can be downloaded from here http://vlg.cs.dartmouth.edu/c3d/ (direct link: https://www.dropbox.com/s/vr8ckp0pxgbldhs/conv3d_deepnetA_sport1m_iter_1900000?dl=0 )

This caffe model, when python3 caffemodel2pytorch.py conv3d_deepnetA_sport1m_iter_1900000 -o conv3d_deepnetA_sport1m_iter_1900000.h5 errors out because net_param.layers contains blobs, but these blobs do not contain a data field, but many diff fields (one per weight)

I don't know why this is the case. I assume it is due to an older caffe version used

Please kindly add support for these kind of models as well

How to use converted-model (pt)??

Hello.
My caffemodel was changed pt-model (pytorch model) using your convertor.
But, I do not know how to use it.

I think that inference or training of pytorch should need two files.

  1. definition python code.
  2. pt model

If you can, please upload sample code (inference code and training)

docker image

Execuse me, could you provide a docker image to run the code?

TypeError: type() takes exactly 2 arguments (1 given)

python=2.7

File "../oicr-master/caffemodel2pytorch.py", line 354, in init_weight_bias
self.weight = nn.Parameter(weight.type_as(self.weight), requires_grad = self.weight.requires_grad)
File "../lib/python2.7/site-packages/torch/tensor.py", line 41, in type_as
return self.type(tensor.type())
TypeError: type() takes exactly 2 arguments (1 given)

What advice do you have?

In addition, what version of python and pytorch are you using?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.