Как представить ndarray обученной сети в python? - PullRequest
0 голосов
/ 25 апреля 2019

Я пытаюсь использовать полностью согласованную нейронную сеть кафе (NN) в Python.Исходная модель / NN была реализована и обучена в Keras, а затем преобразована в модель caffe с использованием MMdnn .

Данные, которые я хочу представить в NN, представляют собой массив данных.Это должно протолкнуть это через сеть, а затем сделать прогноз класса на выходе.

Однако, когда я пытаюсь представить массив массивов 1-D в загруженной caffe-модели, я получаю следующееошибка:

File "/pythonpath/python3.7/site-packages/caffe/pycaffe.py", line 119, in _Net_forward
    outputs = set(self.outputs + blobs)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

Я выполнил поиск в Google и Stackoverflow, но не смог решить проблему. Не могли бы вы помочь, пожалуйста?

Описание того, что я сделал:

Я регулярно загружаю свою модель кафе по:

nn = caffe.Net('/model_path/model.prototxt',
               '/model_path/model.caffemodel',
               caffe.TEST)

Это дает мне следующий журнал (успех?):

WARNING: Logging before InitGoogleLogging() is written to STDERR
W0423 14:53:15.663930 11914 _caffe.cpp:139] DEPRECATION WARNING - deprecated use of Python interface
W0423 14:53:15.663944 11914 _caffe.cpp:140] Use this instead (with the named "weights" parameter):
W0423 14:53:15.663946 11914 _caffe.cpp:142] Net('/path/model.prototxt', 1, weights='/path/model.caffemodel')
I0423 14:53:15.665053 11914 net.cpp:51] Initializing net from parameters: 
state {
  phase: TEST
  level: 0
}
layer {
  name: "dense_1_input"
  type: "Input"
  top: "dense_1_input"
  input_param {
    shape {
      dim: 1
      dim: 40
    }
  }
}
.... more layers ...
layer {
  name: "output_activation"
  type: "Softmax"
  bottom: "output"
  top: "output_activation"
}
I0423 14:53:15.665112 11914 layer_factory.hpp:77] Creating layer dense_1_input
I0423 14:53:15.665118 11914 net.cpp:84] Creating Layer dense_1_input
I0423 14:53:15.665122 11914 net.cpp:380] dense_1_input -> dense_1_input
I0423 14:53:15.665140 11914 net.cpp:122] Setting up dense_1_input
I0423 14:53:15.665143 11914 net.cpp:129] Top shape: 1 40 (40)
I0423 14:53:15.665148 11914 net.cpp:137] Memory required for data: 160
.... more layers ...
I0423 14:53:15.665232 11914 layer_factory.hpp:77] Creating layer output_activation
I0423 14:53:15.665236 11914 net.cpp:84] Creating Layer output_activation
I0423 14:53:15.665239 11914 net.cpp:406] output_activation<- output
I0423 14:53:15.665242 11914 net.cpp:380] output_activation-> output_activation
I0423 14:53:15.665248 11914 net.cpp:122] Setting up output_activation
I0423 14:53:15.665251 11914 net.cpp:129] Top shape: 1 3 (3)
I0423 14:53:15.665254 11914 net.cpp:137] Memory required for data: 248
I0423 14:53:15.665256 11914 net.cpp:200] output_activation does not need backward computation.
.... more layers ...
I0423 14:53:15.665269 11914 net.cpp:242] This network produces output output_activation
I0423 14:53:15.665272 11914 net.cpp:255] Network initialization done.

Данные, которые я хочу представить NN, хранятся в массиве Numpy.Это дано модели caffe следующим образом:

# print data, its shape and type
print("Data:")
print(test_data)
print("Data shape:")
print(test_data.shape)
print("Data type:")
print(type(test_data))
print("Type of array elements:")
print(type(test_data[0][0]))


# forward data through caffe model
out = nn.forward(test_data)
pred_probas = out['prob']
print(pred_probas.argmax())

Приведенный выше код выдает этот журнал (ошибка):

Data:
    [[ 0.2655475   0.2655475   0.2655475   0.2655475   0.2655475   0.26516597
   0.26516597  0.26516597  0.26516597  0.26516597 -0.03401361 -0.04166667
  -0.03996599 -0.01870748 -0.01785714 -0.02636054 -0.0255102  -0.03401361
  -0.03231293 -0.0212585   0.02047792  0.02047792  0.02047792  0.02047792
   0.02047792  0.02047792  0.02319407  0.02319407  0.02319407  0.02594073
   0.          0.          0.          0.          0.          0.
   0.01176471  0.          0.          0.01189689]]
Data shape:
(1, 40)
Data type:
<class 'numpy.ndarray'>
Type of array elements:
<class 'numpy.float64'>


Traceback (most recent call last):
  File "/path/caffe_nn_test.py", line 41, in <module>
    out = nn.forward(test_data)
  File "//python3.7/site-packages/caffe/pycaffe.py", line 119, in _Net_forward
    outputs = set(self.outputs + blobs)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

Я бы оценил любую помощь по этому вопросу!Спасибо.

1 Ответ

1 голос
/ 26 апреля 2019

Нашел ответ, почему я не смог ввести массив numpy так, как пытался. Необходимо назначить его правому входному блоку сети (как я понял).

См. Вопрос об именах слоев.

Этот код работает:

# load net from files
nn = caffe.Net('/model_path/model.prototxt',
               '/model_path/model.caffemodel',
               caffe.TEST)

# test_data is a numpy array with the shape (1, 40)


# set input for neural network
nn.blobs['dense_1_input'] = test_data

# forward data through caffe model
out = nn.forward()

# get class prediction
pred_probas = out['output_activation']
print(pred_probas.argmax())

Надеюсь, это поможет любому, у кого такой же вопрос.

...