2017-09-16 3 views
0

Theano와 Keras를 사용하고 아래 명령을 사용하여 .h5 파일에서 VGG Net의 가중치를로드하려고합니다."AttributeError : 'module'객체에 'ifelse'속성이 없습니다."

VGG 네트 모델의 정의 :

def VGG_16(weights_path=None): 
    model = Sequential() 
    model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) 
    model.add(Convolution2D(64, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(64, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 

    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(128, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(128, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 

    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(256, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(256, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(256, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 

    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 

    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(ZeroPadding2D((1,1))) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 

    model.add(Flatten()) 
    model.add(Dense(4096, activation='relu')) 
    model.add(Dropout(0.5)) 
    model.add(Dense(4096, activation='relu')) 
    model.add(Dropout(0.5)) 
    model.add(Dense(1000, activation='softmax')) 

    if weights_path: 
     model.load_weights(weights_path) 

    return model 

는 아래의 명령

model = VGG_16('vgg16_weights_th_dim_ordering_th_kernels.h5') 

를 사용하여 가중치를로드하는 시도와 오류로 하나 아래를 가지고 :

'AttributeError Traceback (most recent call last) 
<ipython-input-3-e815cc7d5738> in <module>() 
     1 #model = VGG_16('vgg16_weights_tf_dim_ordering_tf_kernels.h5') 
----> 2 model = VGG_16('vgg16_weights_th_dim_ordering_th_kernels.h5') 
     3 #sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) 
     4 #model.compile(optimizer=sgd, loss='categorical_crossentropy') 

<ipython-input-2-f9b05d09c080> in VGG_16(weights_path) 
    39  model.add(Flatten()) 
    40  model.add(Dense(4096, activation='relu')) 
---> 41  model.add(Dropout(0.5)) 
    42  model.add(Dense(4096, activation='relu')) 
    43  model.add(Dropout(0.5)) 

c:\users\sekhar\onedrive\insofe\classes\week17\for_keras\keras-master\keras\models.pyc in add(self, layer) 
    330     output_shapes=[self.outputs[0]._keras_shape]) 
    331   else: 
--> 332    output_tensor = layer(self.outputs[0]) 
    333    if isinstance(output_tensor, list): 
    334     raise TypeError('All layers in a Sequential model ' 

c:\users\sekhar\onedrive\insofe\classes\week17\for_keras\keras-master\keras\engine\topology.pyc in __call__(self, x, mask) 
    570   if inbound_layers: 
    571    # This will call layer.build() if necessary. 
--> 572    self.add_inbound_node(inbound_layers, node_indices, tensor_indices) 
    573    # Outputs were already computed when calling self.add_inbound_node. 
    574    outputs = self.inbound_nodes[-1].output_tensors 

c:\users\sekhar\onedrive\insofe\classes\week17\for_keras\keras-master\keras\engine\topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices) 
    633   # creating the node automatically updates self.inbound_nodes 
    634   # as well as outbound_nodes on inbound layers. 
--> 635   Node.create_node(self, inbound_layers, node_indices, tensor_indices) 
    636 
    637  def get_output_shape_for(self, input_shape): 

c:\users\sekhar\onedrive\insofe\classes\week17\for_keras\keras-master\keras\engine\topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices) 
    164 
    165   if len(input_tensors) == 1: 
--> 166    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0])) 
    167    output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0])) 
    168    # TODO: try to auto-infer shape 

c:\users\sekhar\onedrive\insofe\classes\week17\for_keras\keras-master\keras\layers\core.pyc in call(self, x, mask) 
    108    def dropped_inputs(): 
    109     return K.dropout(x, self.p, noise_shape, seed=self.seed) 
--> 110    x = K.in_train_phase(dropped_inputs, lambda: x) 
    111   return x 
    112 

c:\users\sekhar\onedrive\insofe\classes\week17\for_keras\keras-master\keras\backend\theano_backend.pyc in in_train_phase(x, alt) 
    1166  if callable(alt): 
    1167   alt = alt() 
-> 1168  x = theano.ifelse.ifelse(_LEARNING_PHASE, x, alt) 
    1169  x._uses_learning_phase = True 
    1170  return x 

AttributeError: 'module' object has no attribute 'ifelse' 

것은 무엇일까 이 문제에 대한 가능한 해결책?

제 친구 중 한 명은 아나콘다와 테아 노를 다시 설치하는 것 외에 다른 대안이 없다고 말합니다. 제발.

+0

어떤 Keras 및 Theano 버전을 사용하고 있습니까? –

+0

Keras 버전은 1.2.1이고 Theano 버전은 0.10.0beta2입니다. – user8494391

답변

2

버전이 아마 Keras의 버전이 너무 새로운 기능입니다. theano를 0.9.x로 다운 그레이드하고 Keras를 2.0 이상으로 업그레이드해야합니다. 그러면 완벽하게 작동합니다.

2

단순히보십시오 :

import theano 
print theano.ifelse 

그것은 당신의 theano 설치가 가장 가능성이 잘못된 오류를 표시하고 다시 설치해야합니다.

예 출력 theano의

<module 'theano.ifelse' from '/usr/local/lib/python2.7/dist-packages/theano/ifelse.pyc'> 
3

theano_backend 파일로 이동하십시오. 행에서

:

x = theano.ifelse.ifelse(training, x, alt) 

덮어 쓰기 :

x = ifelse.ifelse(training, x, alt) 

그리고 아직도 theano_backend 파일 :

추가 : 영어로

from theano import ifelse 

죄송합니다.

2

케라를 업그레이드하면 효과가 있습니다.

비슷한 문제가 있습니다. pip install keras

다음과 같은 버전 조합을 사용하여 케라를 업그레이드하십시오.

1.0.1 2.1.3