ethereon / caffe-tensorflow

Caffe models in TensorFlow
Other
2.8k stars 1.03k forks source link

Problems for convert caffenet and alexnet #54

Open larsonwu0220 opened 8 years ago

larsonwu0220 commented 8 years ago

Hi All : When I run ./convert.py alexnet.prototxt --caffemodel alexnet.caffemodel --data-output-path=alexTensorflow.py after download alexnet model from Caffe, it appears error as following: How to modify the code ? Thanks

Traceback (most recent call last):
  File "./convert.py", line 60, in <module>
    main()
  File "./convert.py", line 56, in main
    args.phase)
  File "./convert.py", line 27, in convert
    transformer = TensorFlowTransformer(def_path, caffemodel_path, phase=phase)
  File "/home/larson/atensorflow_code/Caffe2Tensorflow/kaffe/tensorflow/transformer.py", line 221, in __init__
    self.load(def_path, data_path, phase)
  File "/home/larson/atensorflow_code/Caffe2Tensorflow/kaffe/tensorflow/transformer.py", line 227, in load
    graph = GraphBuilder(def_path, phase).build()
  File "/home/larson/atensorflow_code/Caffe2Tensorflow/kaffe/graph.py", line 140, in __init__
    self.load()
  File "/home/larson/atensorflow_code/Caffe2Tensorflow/kaffe/graph.py", line 146, in load
    text_format.Merge(def_file.read(), self.params)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 309, in Merge
    return MergeLines(text.split('\n'), message, allow_unknown_extension)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 346, in MergeLines
    _ParseOrMerge(lines, message, True, allow_unknown_extension)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 371, in _ParseOrMerge
    allow_unknown_extension)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 473, in _MergeField
    allow_unknown_extension)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 493, in _MergeField
    _MergeScalarField(tokenizer, message, field, allow_multiple_scalars)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 625, in _MergeScalarField
    value = tokenizer.ConsumeString()
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 901, in ConsumeString
    the_bytes = self.ConsumeByteString()
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 916, in ConsumeByteString
    the_list = [self._ConsumeSingleByteString()]
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 935, in _ConsumeSingleByteString
    raise self._ParseError('Expected string but found: %r' % (text,))
google.protobuf.text_format.ParseError: 9:9 : Expected string but found: 'Convolution'

The following is alexnet.prototxt

name: "CaffeNet"
input: "data"
input_dim: 1
input_dim: 3
input_dim: 227
input_dim: 227
layer {
  name: "conv1"
  type: Convolution
  bottom: "data"
  top: "conv1"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
  }
}
layer {
  name: "relu1"
  type: ReLU
  bottom: "conv1"
  top: "conv1"
}
layer {
  name: "pool1"
  type: Pooling
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layer {
  name: "norm1"
  type: LRN
  bottom: "pool1"
  top: "norm1"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layer {
  name: "conv2"
  type: Convolution
  bottom: "norm1"
  top: "conv2"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 256
    pad: 2
    kernel_size: 5
    group: 2
  }
}
layer {
  name: "relu2"
  type: ReLU
  bottom: "conv2"
  top: "conv2"
}
layer {
  name: "pool2"
  type: Pooling
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layer {
  name: "norm2"
  type: LRN
  bottom: "pool2"
  top: "norm2"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layer {
  name: "conv3"
  type: Convolution
  bottom: "norm2"
  top: "conv3"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 384
    pad: 1
    kernel_size: 3
  }
}
layer {
  name: "relu3"
  type: ReLU
  bottom: "conv3"
  top: "conv3"
}
layer {
  name: "conv4"
  type: Convolution
  bottom: "conv3"
  top: "conv4"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 384
    pad: 1
    kernel_size: 3
    group: 2
  }
}
layer {
  name: "relu4"
  type: ReLU
  bottom: "conv4"
  top: "conv4"
}
layer {
  name: "conv5"
  type: Convolution
  bottom: "conv4"
  top: "conv5"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 256
    pad: 1
    kernel_size: 3
    group: 2
  }
}
layer {
  name: "relu5"
  type: ReLU
  bottom: "conv5"
  top: "conv5"
}
layer {
  name: "pool5"
  type: Pooling
  bottom: "conv5"
  top: "pool5"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layer {
  name: "fc6"
  type: InnerProduct
  bottom: "pool5"
  top: "fc6"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 4096
  }
}
layer {
  name: "relu6"
  type: ReLU
  bottom: "fc6"
  top: "fc6"
}
layer {
  name: "fc7"
  type: InnerProduct
  bottom: "fc6"
  top: "fc7"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 4096
  }
}
layer {
  name: "relu7"
  type: ReLU
  bottom: "fc7"
  top: "fc7"
}
layer {
  name: "fc8_sbt"
  type: InnerProduct
  bottom: "fc7"
  top: "fc8_sbt"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 5
  }
}
layer {
  name: "prob"
  type: Softmax
  bottom: "fc8_sbt"
  top: "prob"
}
swearos commented 8 years ago

the extension of data-output-path is ‘npy’,not 'py'. so you need change '--data-output-path=alexTensorflow.py' to '--data-output-path=alexTensorflow.npy

ethereon commented 8 years ago

The prototxt looks invalid - I'd expect the types to be quoted (example).

shreyaspimpalgaonkar commented 6 years ago

Answer from :- https://github.com/ethereon/caffe-tensorflow/issues/39

The problem is with the version of Caffe that your prototxt file is written for. In more recent versions of Caffe, some syntax for prototxt files has changed. E.g. layers changed to layer or now the type of layers is a string. So to solve the issue you can simply modify the prototxt file so that it would be compatible with latest release of Caffe.

To do so, apply these changes:

layers -> layer CONVOLUTION -> "Convolution" POOLING -> "Pooling" RELU -> "ReLU" INNER_PRODUCT -> "InnerProduct" SOFTMAX -> "Softmax" DROPOUT -> "Dropout" LRN -> "LRN"