AttributeError:尝试进行转移学习时,“ Tensor”对象没有属性“ assign”

时间:2019-12-27 07:24:43

标签: python keras transfer-learning

我基于https://colab.research.google.com/github/google/eng-edu/blob/master/ml/pc/exercises/image_classification_part3.ipynb#scrollTo=BMXb913pbvFg的示例进行了尝试 并尝试对猫和狗的分类进行迁移学习,然后以“斑马和大象”为例进行了相同的学习,没有遇到任何问题。 我的示例代码与教程几乎相同,并且可以正常工作:

image_data_generator = ImageDataGenerator(rescale=1./255., validation_split=.2)
image_size = 128
IMAGE_WIDTH = image_size
IMAGE_HEIGHT = image_size
BATCH_SIZE=128
image_data_dir = "images"
seed = 1
image_generator = image_data_generator.flow_from_directory(
    image_data_dir,
    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
    batch_size=BATCH_SIZE,
    color_mode='rgb', 
    subset='training',
    seed = seed,
    class_mode='binary')
test_generator = image_data_generator.flow_from_directory(
    image_data_dir,
    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
    batch_size=BATCH_SIZE,
    color_mode='rgb', 
    subset='validation',
    seed = seed,
    class_mode='binary')

from keras.applications.vgg16 import VGG16
inputs = Input((image_size, image_size, 3))
vgg16 = VGG16(include_top=False, weights='imagenet', pooling=None, input_shape=(image_size, image_size, 3), classes=2 )

for layer in vgg16.layers:
    layer.trainable = False

for layer in vgg16.layers:
  print(layer.name, layer.trainable)

x = Flatten()(vgg16.get_layer('block5_pool').output)

x_ = BatchNormalization()(x)
x_ = Dense(128, activation='relu')(x_)
x_ = BatchNormalization()(x_)
x_ = Dense(64, activation='relu')(x_)
x_ = Dense(1, activation='sigmoid')(x_)

model = Model(vgg16.input, x_)

model.compile(loss='binary_crossentropy',
                  optimizer='Adam',
                  metrics=['binary_accuracy'])

model.summary()

model.fit_generator(image_generator,
                    steps_per_epoch=n/BATCH_SIZE, 
                    epochs=50,
                    validation_data=test_generator)

此代码没有遇到任何麻烦。 以下一个引发了错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-55354bac5e27> in <module>()
     43     validation_data=test_generator,
     44     validation_steps= 458 // BATCH_SIZE,
---> 45     epochs=90)

--> 228   return ref.assign(value, name=name)
    229 
    230 

AttributeError: 'Tensor' object has no attribute 'assign'

在这里:

image_data_generator = ImageDataGenerator(rescale=1./255., validation_split=.15)

image_data_dir = "processed/img/"
seed = 1 
IMAGE_SIZE = SIZE
IMAGE_WIDTH, IMAGE_HEIGHT = IMAGE_SIZE, IMAGE_SIZE

image_generator = image_data_generator.flow_from_directory(
    image_data_dir,
    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
    batch_size=BATCH_SIZE,
    class_mode="categorical", #could be "binary" for binary problems
    color_mode='rgb', 
    subset='training',
    seed = seed)

test_generator = image_data_generator.flow_from_directory(
    image_data_dir,
    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
    batch_size=BATCH_SIZE,
    class_mode="categorical", #could be "binary" for binary problems
    color_mode='rgb', 
    subset='validation',
    seed = seed)

layer1 = Input((SIZE, SIZE, 3))

vgg16 = VGG16(include_top=False, weights='imagenet', pooling=None, input_shape=(SIZE, SIZE, 3), classes=num_classes )

for layer in vgg16.layers:
    layer.trainable = False

bnorm = BatchNormalization(name="14")(vgg16.get_layer('block4_pool').output)

conv = Conv2D(512, (3, 3), activation='relu', padding='same',name="13")()
pool = MaxPooling2D(pool_size=(2, 2),name="15")(conv)
pool = BatchNormalization(name="16")(pool)

x = Flatten()(pool)

x_ = BatchNormalization()(x)
x_ = Dense(128, activation='relu')(x_)
x_ = BatchNormalization()(x_)
x_ = Dense(64, activation='relu')(x_)
x_ = Dense(num_classes, activation='softmax')(x_)

model = Model(vgg16.input, x_)

model.compile(loss='categorical_crossentropy',
                  optimizer='adadelta',
                  metrics=['categorical_accuracy'])

model.fit_generator( image_generator,
    steps_per_epoch= 2606 // BATCH_SIZE,
    validation_data=test_generator,
    validation_steps= 458 // BATCH_SIZE,
    epochs=90)

据我所知,有几行无用的内容(例如创建最终在体系结构中不使用的输入层。

通常,我认为这两种情况都非常相似,由于新问题具有3个类别而不是2个类别,因此我仅作了一些更改。

不幸的是,搜索该错误并没有太大帮助。有人看到我犯了一个错误吗?

1 个答案:

答案 0 :(得分:-1)

这在退化的情况下发生。可以说如果数组为空(即没有行,没有列,没有形状),就会发生这种情况。

解决方案-请确保您向喀拉拉邦或tf喂食的食物都不为空。