Python 属性错误:';张量';对象没有属性';分配';在尝试迁移学习时
我的尝试基于 并尝试对猫和狗进行迁移学习,然后对“斑马和大象”进行迁移学习,但没有遇到任何问题。 我的示例的代码与教程几乎相同,并且有效:Python 属性错误:';张量';对象没有属性';分配';在尝试迁移学习时,python,keras,transfer-learning,Python,Keras,Transfer Learning,我的尝试基于 并尝试对猫和狗进行迁移学习,然后对“斑马和大象”进行迁移学习,但没有遇到任何问题。 我的示例的代码与教程几乎相同,并且有效: 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 =
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'
据我所知,有一些无用的行(例如,创建一个输入层,它在我最终使用的体系结构中没有使用)
一般来说,我认为这两种情况非常相似,我只做了一些更改,因为新问题有3个类,而不是2个
不幸的是,谷歌搜索这个错误并没有多大帮助。有人看到我犯的错误吗?首先,将
conv=Conv2D(512,(3,3),activation='relu',padding='same',name=“13”)()
改为conv=Conv2D(512,(3,3),activation='relu',padding='same',name=“13”)(bnorm)
。其次,检查包含图像的文件夹,以确定问题似乎是由图像\u生成器产生的。
。我用我的一个小数据集在我的机器上尝试了你的代码,它正在进行培训,没有任何问题(2和3个课程)。谢谢你的输入!我也从头重写了,我很困惑地发现似乎完全相同的代码现在正在工作:)也许这与你给出的第一个建议有关。
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)