标签: transfer-learning

添加的层必须是类Layer的实例。找到:<tensorflow.python.keras.engine.input_layer.InputLayer对象位于0x000001FA104CBB70>

我是机器学习的新手。我正在按照本教程对VGG16模型进行微调。

该模型可以使用以下代码很好地加载:

vgg_model = tensorflow.keras.applications.vgg16.VGG16()
Run Code Online (Sandbox Code Playgroud)

但出现此错误:

TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer object at 0x000001FA104CBB70>
Run Code Online (Sandbox Code Playgroud)

运行此代码时:

model = Sequential()
for layer in vgg_model.layers[:-1]:
    model.add(layer)
Run Code Online (Sandbox Code Playgroud)

依存关系:

  • Keras 2.2.3
  • Tensorflow 1.12.0
  • 张量流-gpu1.12.0
  • 的Python 3.6.0

我正在关注此博客,但我想使用VGG16。

任何帮助解决此问题的方法将不胜感激。非常感谢。

keras tensorflow transfer-learning vgg-net finetunning

5
推荐指数
2
解决办法
7701
查看次数

使用 Keras 应用程序中的模型,无需预先训练权重

Keras 应用程序提供了一些最流行的模型架构的实现,并在一些最流行的数据集上预训练了权重。这些预定义的模型对于类似于模型训练数据集的问题的迁移学习非常方便。

但是,如果我遇到一个非常不同的问题并且想要在新数据集上完全训练模型怎么办?如果我没有预训练的权重,如何使用应用程序中的模型根据我自己的数据集从头开始训练?

deep-learning keras transfer-learning

4
推荐指数
1
解决办法
2285
查看次数

迁移学习准确性差

我的任务是根据缺陷对种子进行分类。我有 7 个类的大约 14k 图像(它们的大小不相等,有些类有更多照片,有些类有更少)。我尝试从头开始训练 Inception V3,准确率约为 90%。然后我尝试使用带有 ImageNet 权重的预训练模型进行迁移学习。我inception_v3applications没有顶级 fc 层的情况下导入,然后在文档中添加了我自己的层。我以以下代码结束:

# Setting dimensions
img_width = 454
img_height = 227

###########################
# PART 1 - Creating Model #
###########################

# Creating InceptionV3 model without Fully-Connected layers
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape = (img_height, img_width, 3))

# Adding layers which will be fine-tunned
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(7, activation='softmax')(x)

# Creating final model
model = Model(inputs=base_model.input, outputs=predictions)

# …
Run Code Online (Sandbox Code Playgroud)

python machine-learning keras tensorflow transfer-learning

4
推荐指数
1
解决办法
8045
查看次数

PyTorch Transfer Learning教程的混淆矩阵和测试准确性

遵循Pytorch Transfer学习教程之后,我感兴趣的是仅报告训练和测试的准确性以及混淆矩阵(例如使用sklearn混淆矩阵)。我怎样才能做到这一点?当前的教程仅报告火车/ Val的准确性,我很难确定如何在其中合并sklearn混淆矩阵代码。链接到此处的原始教程:https : //pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

%matplotlib inline
from graphviz import Digraph
import torch
from torch.autograd import Variable
# Author: Sasank Chilamkurthy

from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy

plt.ion()
data_transforms = {
    'train': transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val': …
Run Code Online (Sandbox Code Playgroud)

python confusion-matrix scikit-learn pytorch transfer-learning

4
推荐指数
3
解决办法
4738
查看次数

NLP预训练模型(例如ELMo,Bert)的数据预处理

我计划根据手头的数据(由人们键入的注释)从头开始训练ELMo或Bert模型。我现在拥有的数据都是由不同的人键入的。拼写,格式和句子不一致都存在问题。阅读ELMo和Bert的论文后,我知道这两个模型都使用了很多类似Wikipedia的句子。我还没有找到Emlo或Bert模型的任何经过处理的训练样本或任何预处理教程。我的问题是:

  • Bert和ELMo模型是否具有标准的数据预处理步骤或标准的处理数据格式?
  • 根据我现有的脏数据,是否有任何方法可以对此数据进行预处理,以使生成的单词表示更为准确?

nlp machine-learning pre-trained-model transfer-learning natural-language-processing

4
推荐指数
1
解决办法
1734
查看次数

如何在 Keras 的迁移学习中使用 input_shape 和 input_tensor?

当我们在 Keras2 中进行迁移学习时,参数需要“input_shape”和“input_tensor”。但我只使用 input_tensor 而从未使用过 input_shape。我觉得只用input_tensor就够了,不知道什么时候用input_shape。我应该如何单独使用它们?

我同时使用了 input_tensor 和 input_shape 以及单独的值,并且仅采用 input_tensor 的值并忽略了 input_shape 。

vgg16_model = VGG16(include_top=False, weights='imagenet', 
                    input_tensor = Input(shape=(150, 150, 3)), 
                    input_shape=(224,224,3))

top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dense(1, activation='sigmoid'))
model = Model(input=vgg16_model.input, output=top_model(vgg16_model.output))

model.summary()
Run Code Online (Sandbox Code Playgroud)
Layer (type)                 Output Shape              Param #   
================================================================
input_6 (InputLayer)         (None, 150, 150, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 150, 150, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 150, 150, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 75, 75, 64)        0         
_________________________________________________________________
block2_conv......
Run Code Online (Sandbox Code Playgroud)

我预计这段代码中会出现一些错误,但是没有错误,并且该模型可以接受 (150, …

python machine-learning deep-learning keras transfer-learning

4
推荐指数
1
解决办法
3137
查看次数

AttributeError: 'tuple' 对象在尝试使用 keras 进行迁移学习时没有属性 'layer'

我正在尝试使用 keras 模型进行迁移学习,但一直坚持向模型添加新层。我试过下面的代码:

prev_model = load_model('final_model.h5') # loading the previously saved model.

new_model = Sequential()
new_model.add(prev_model)
new_model.add(Dense(256,activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(1,activation='sigmoid'))
Run Code Online (Sandbox Code Playgroud)

但得到:

TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Flatten object at 0x00000000B74364A8>
Run Code Online (Sandbox Code Playgroud)

每当我使用.add()添加图层时都会发生这种情况。

然后我发现

number_of_layers_to_freeze = 10
vgg_model = VGG16(include_top=False)
for i in range(number_of_layers_to_freeze):
    vgg_model.layers[i].trainable = False
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)

model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)
Run Code Online (Sandbox Code Playgroud)

在其他职位。但它导致

 AttributeError: 'tuple' object has no attribute 'layer'
Run Code Online (Sandbox Code Playgroud)

我目前正在使用

keras 2.2.5 
tensorflow-gpu 1.14.0
Run Code Online (Sandbox Code Playgroud)

是不是版本冲突导致的?


完整回溯 :(AttributeError: …

python keras tensorflow transfer-learning

4
推荐指数
1
解决办法
3300
查看次数

如何解决“No Algorithm Worked”Keras 错误?

我尝试在 Keras 中开发 FCN-16 模型。我用类似的 FCN-16 模型权重初始化了权重。

def FCN8 (nClasses, input_height=256, input_width=256):

    ## input_height and width must be devisible by 32 because maxpooling with filter size = (2,2) is operated 5 times,
    ## which makes the input_height and width 2^5 = 32 times smaller
    assert input_height % 32 == 0
    assert input_width % 32 == 0
    IMAGE_ORDERING = "channels_last"

    img_input = Input(shape=(input_height, input_width, 3))  ## Assume 224,224,3

    ## Block 1
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1', data_format=IMAGE_ORDERING)(
        img_input) …
Run Code Online (Sandbox Code Playgroud)

computer-vision deep-learning keras tensorflow transfer-learning

4
推荐指数
5
解决办法
9262
查看次数

Tensorflow 中哪些层受到 dropout 层的影响?

考虑迁移学习,以便在 keras/tensorflow 中使用预训练模型。对于每个旧层,trained参数设置false为使其权重在训练期间不会更新,而最后一层已被新层替换,并且必须对这些层进行训练。特别是添加了两个带有神经元和 relu 激活函数的全连接隐藏层5121024在这些层之后,使用 Dropout 层rate 0.2。这意味着在每个训练时期,20%神经元都会被随机丢弃。

该 dropout 层影响哪些层?它是否会影响所有网络,包括已layer.trainable=false设置的预训练层,还是仅影响新添加的层?或者它只影响前一层(即具有1024神经元的一层)?

换句话说,在每个时期因 dropout 而关闭的神经元属于哪一层?

import os

from tensorflow.keras import layers
from tensorflow.keras import Model
  
from tensorflow.keras.applications.inception_v3 import InceptionV3

local_weights_file = 'weights.h5'

pre_trained_model = InceptionV3(input_shape = (150, 150, 3), 
                                include_top = False, 
                                weights = None)

pre_trained_model.load_weights(local_weights_file)

for layer in pre_trained_model.layers:
  layer.trainable = False
  
# pre_trained_model.summary()

last_layer = pre_trained_model.get_layer('mixed7')
last_output = last_layer.output

# Flatten the output …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow dropout transfer-learning

4
推荐指数
1
解决办法
4890
查看次数

如何为 BERT 准备文本 - 出现错误

我正在尝试学习 BERT 进行文本分类。我在准备使用 BERT 的数据时发现一些问题。

从我的数据集中,我将情绪和评论分为:

X = df['sentiments']
y = df['reviews'] #it contains four different class of reviews
Run Code Online (Sandbox Code Playgroud)

下一个,

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
train_encodings = tokenizer(X_train, truncation=True, padding=True, max_length=512) 
Run Code Online (Sandbox Code Playgroud)

这是我收到错误的地方:

X = df['sentiments']
y = df['reviews'] #it contains four different class of reviews
Run Code Online (Sandbox Code Playgroud)

当我尝试将 X 转换为列表并使用它时,出现另一个错误:

TypeError: TextEncodeInput must be Union[TextInputSequence, Tuple[InputSequence, InputSequence]]
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下问题出在哪里吗?之前我遵循了 20 个新闻数据集的教程,结果很有效。但现在当我在另一个项目中使用它时,它不起作用,我感到很难过。

谢谢。

nlp python-3.x transfer-learning bert-language-model

4
推荐指数
1
解决办法
6770
查看次数