Python Keras/Tensorflow/h5py-键错误:“;Can';t打开属性(无法定位属性:';nb#u层';)”引用;

Python Keras/Tensorflow/h5py-键错误:“;Can';t打开属性(无法定位属性:';nb#u层';)”引用;,python,tensorflow,keras,h5py,Python,Tensorflow,Keras,H5py,我有以下代码片段: def save_bottleneck_features(): """builds the pretrained vgg16 model and runs it on our training and validation datasets""" datagen = ImageDataGenerator(rescale=1./255) # match the vgg16 architecture so we can load the pretrain

我有以下代码片段:

def save_bottleneck_features():
    """builds the pretrained vgg16 model and runs it on our training and validation datasets"""
    datagen = ImageDataGenerator(rescale=1./255)

    # match the vgg16 architecture so we can load the pretrained weights into this model
    model = Sequential()
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf", input_shape=(1022, 767, 3)))
    model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))

    model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

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

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

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

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

    # load VGG16 weights
    f = h5py.File(weights_path)

    for k in range(f.attrs['nb_layers']):
        if k >= len(model.layers):
            break
        g = f['layer_{}'.format(k)]
        weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
        model.layers[k].set_weights(weights)

    f.close()
    print('Model loaded.')

    generator = datagen.flow_from_directory(
            train_data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode=None,
            shuffle=False)
    bottleneck_features_train = model.predict_generator(generator, nb_train_samples)
    np.save(open('bottleneck_features_train.npy', 'wb'), bottleneck_features_train)

    generator = datagen.flow_from_directory(
            validation_data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode=None,
            shuffle=False)
    bottleneck_features_validation = model.predict_generator(generator, nb_validation_samples)
    np.save(open('bottleneck_features_validation.npy', 'wb'), bottleneck_features_validation)
我调用了
save\u瓶颈\u features()
函数,但是文件
瓶颈\u features\u validation.npy
似乎没有创建,因为我得到了下面的错误,还是与
h5
文件有关

Using TensorFlow backend.
Traceback (most recent call last):
  File "train.py", line 362, in <module>
    train_top_model()
  File "train.py", line 178, in train_top_model
    model.add(Flatten(input_shape=train_data.shape[1:]))
NameError: name 'train_data' is not defined

C:\Users\Abder-Rahman\Desktop\Testing\skin lesion detection\skin_lesion_classification\unaltered_classification>python train.py
Using TensorFlow backend.
train.py:99: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(data_format="channels_last", pool_size=(2, 2), input_shape=(1022, 767...)`
  model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf", input_shape=(1022, 767, 3)))
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "CountExtremelyRandomStats" device_type: "CPU"') for unknown op: CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "FinishedNodes" device_type: "CPU"') for unknown op: FinishedNodes
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "GrowTree" device_type: "CPU"') for unknown op: GrowTree
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ReinterpretStringToFloat" device_type: "CPU"') for unknown op: ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "SampleInputs" device_type: "CPU"') for unknown op: SampleInputs
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ScatterAddNdim" device_type: "CPU"') for unknown op: ScatterAddNdim
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNInsert" device_type: "CPU"') for unknown op: TopNInsert
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNRemove" device_type: "CPU"') for unknown op: TopNRemove
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TreePredictions" device_type: "CPU"') for unknown op: TreePredictions
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "UpdateFertileSlots" device_type: "CPU"') for unknown op: UpdateFertileSlots
train.py:102: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), name="conv1_1", activation="relu")`
  model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
train.py:104: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), name="conv1_2", activation="relu")`
  model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
train.py:108: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (3, 3), name="conv2_1", activation="relu")`
  model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
train.py:110: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (3, 3), name="conv2_2", activation="relu")`
  model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
train.py:114: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (3, 3), name="conv3_1", activation="relu")`
  model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
train.py:116: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (3, 3), name="conv3_2", activation="relu")`
  model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
train.py:118: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (3, 3), name="conv3_3", activation="relu")`
  model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
train.py:122: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name="conv4_1", activation="relu")`
  model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
train.py:124: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name="conv4_2", activation="relu")`
  model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
train.py:126: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name="conv4_3", activation="relu")`
  model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
train.py:130: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name="conv5_1", activation="relu")`
  model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
train.py:132: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name="conv5_2", activation="relu")`
  model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
train.py:134: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name="conv5_3", activation="relu")`
  model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
Traceback (most recent call last):
  File "train.py", line 361, in <module>
    save_bottleneck_features()
  File "train.py", line 140, in save_bottleneck_features
    for k in range(f.attrs['nb_layers']):
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.7.0\h5py\_objects.c:2853)
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.7.0\h5py\_objects.c:2811)
  File "C:\Python35\lib\site-packages\h5py\_hl\attrs.py", line 58, in __getitem__
    attr = h5a.open(self._id, self._e(name))
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.7.0\h5py\_objects.c:2853)
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.7.0\h5py\_objects.c:2811)
  File "h5py\h5a.pyx", line 77, in h5py.h5a.open (D:\Build\h5py\h5py-2.7.0\h5py\h5a.c:2350)
KeyError: "Can't open attribute (Can't locate attribute: 'nb_layers')"
使用TensorFlow后端。
回溯(最近一次呼叫最后一次):
文件“train.py”,第362行,在
列车顶部模型()
文件“train.py”,第178行,在train\u top\u模型中
model.add(展平(input_shape=train_data.shape[1:]))
名称错误:未定义名称“列车数据”
C:\Users\Abder-Rahman\Desktop\Testing\skin disease detection\skin\u disease\u classification\unchanged\u classification>python train.py
使用TensorFlow后端。
train.py:99:UserWarning:更新对keras2api的`MaxPooling2D`调用:`MaxPooling2D(data\u format=“channels\u last”,pool\u size=(2,2),input\u shape=(1022767…)`
model.add(maxpoolg2d(池大小=(2,2),dim_ordery=“tf”,input_ushape=(1022,767,3)))
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:'BestSplits'设备类型:'cpu'),用于未知op:BestSplits
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:'CountExtremelyRandomStats“device_type:'cpu'),用于未知op:CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]用于未知op:FinishedNodes的OpKernel('op:'FinishedNodes'设备类型:'cpu')
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]用于未知op:GrowTree的OpKernel('op:'GrowTree'设备类型:'cpu')
E c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\kernel.cc:943]OpKernel('op:“ReinterpretStringToFloat”设备类型:“cpu”)用于未知op:ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:“SampleInputs”device\u type:'cpu'),用于未知op:SampleInputs
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:“ScatterAddNdim”device_type:'cpu'),用于未知op:ScatterAddNdim
E c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\u kernel.cc:943]OpKernel('op:“TopNInsert”device\u type:'cpu'),用于未知op:TopNInsert
E c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\u kernel.cc:943]OpKernel('op:TopNRemove“device\u type:'cpu'),用于未知op:TopNRemove
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:“TreePredictions”device\u type:'cpu'),用于未知op:TreePredictions
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel(“op:“UpdateFertileSlots”设备类型:“cpu”)用于未知op:UpdateFertileSlots
train.py:102:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(64,(3,3),name=“conv1_1”,activation=“relu”)`
添加(卷积2D(64,3,3,activation='relu',name='conv1_1'))
train.py:104:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(64,(3,3),name=“conv1_2”,activation=“relu”)`
添加(卷积2D(64,3,3,activation='relu',name='conv1_2'))
train.py:108:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(128,(3,3),name=“conv2_1”,activation=“relu”)`
添加(卷积2D(128,3,3,activation='relu',name='conv2_1'))
train.py:110:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(128,(3,3),name=“conv2_2”,activation=“relu”)`
添加(卷积2D(128,3,3,activation='relu',name='conv2_2'))
train.py:114:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(256,(3,3),name=“conv3_1”,activation=“relu”)`
添加(卷积2D(256,3,3,activation='relu',name='conv3_1'))
train.py:116:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(256,(3,3),name=“conv3_2”,activation=“relu”)`
添加(卷积2D(256,3,3,activation='relu',name='conv3_2'))
train.py:118:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(256,(3,3),name=“conv3_3”,activation=“relu”)`
添加(卷积2D(256,3,3,activation='relu',name='conv3_3'))
train.py:122:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(512,(3,3),name=“conv4_1”,activation=“relu”)`
添加(卷积2D(512,3,3,activation='relu',name='conv4_1'))
train.py:124:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(512,(3,3),name=“conv4_2”,activation=“relu”)`
添加(卷积2D(512,3,3,activation='relu',name='conv4_2'))
train.py:126:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(512,(3,3),name=“conv4_3”,activation=“relu”)`
添加(卷积2D(512,3,3,activation='relu',name='conv4_3'))
train.py:130:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(512,(3,3),name=“conv5_1”,activation=“relu”)`
添加(卷积2D(512,3,3,activation='relu',name='conv5_1'))
train.py:132:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(512,(3,3),name=“conv5_2”,activation=“relu”)`
添加(卷积2D(512,3,3,activation='relu',name='conv5_2'))
train.py:134:UserWarning:更新对keras2api的`Conv2D`调用:`Conv2D(512,(3,3),name=“conv5_3”,activation=“relu”)`
添加(卷积2D(512,3,3,activation='relu',name='conv5_3'))
回溯(最近一次呼叫最后一次):
文件“train.py”,第361行,在
保存\u瓶颈\u功能()
文件“train.py”,第140行,在save_bott中