使用Flux.jl进行图像分类

在PyTorch从事一个项目,这个项目创建一个深度学习模型,可以检测未知物种的疾病。

最近,决定在Julia中重建这个项目,并将其用作学习Flux.jl[1]的练习,这是Julia最流行的深度学习包(至少在GitHub上按星级排名)。

但在这样做的过程中,遇到了一些挑战,这些挑战在网上或文档中找不到好的例子。因此,决定写这篇文章,作为其他任何想在Flux做类似事情的人的参考资料。

这是给谁的?

因为Flux.jl(以下简称为“Flux”)是一个深度学习包,所以我主要为熟悉深度学习概念(如迁移学习)的读者编写这篇文章。

虽然在写这篇文章时也考虑到了Flux的一个半新手(比如我自己),但其他人可能会觉得这很有价值。只是要知道,写这篇文章并不是对Julia或通量的全面介绍或指导。为此,将分别参考其他资源,如官方的Julia和Flux文档。

最后,对PyTorch做了几个比较。了解本文观点并不需要有PyTorch的经验,但有PyTorch经验的人可能会觉得它特别有趣。

为什么是Julia?为什么选择Flux.jl?

如果你已经使用了Julia和/或Flux,你可能可以跳过本节。此外,许多其他人已经写了很多关于这个问题的帖子,所以我将简短介绍。

归根结底,我喜欢Julia。它在数值计算方面很出色,编程时真的很开心,而且速度很快。原生快速:不需要NumPy或其他底层C++代码的包装器。

至于为什么选择Flux,是因为它是Julia中最流行的深度学习框架,用纯Julia编写,可与Julia生态系统组合。

项目本身

好吧,既然我已经无耻地说服了Julia,现在是时候了解项目本身的信息了。

我使用了三个数据集——PlantVillage[2]、PlantLeaves[3]和PlantaeK[4]——涵盖了许多不同的物种。

我使用PlantVillage作为训练集,其他两个组合作为测试集。这意味着模型必须学习一些可以推广到未知物种的知识,因为测试集将包含未经训练的物种。

了解到这一点,我创建了三个模型:

  1. 使用ResNet迁移学习的基线

  2. 具有自定义CNN架构的孪生(又名暹罗)神经网络

  3. 具有迁移学习的孪生神经网络

本文的大部分内容将详细介绍处理数据、创建和训练模型的一些挑战和痛点。

处理数据

第一个挑战是数据集的格式错误。我不会在这里详细介绍如何对它们进行预处理,但最重要的是我创建了两个图像目录,即训练和测试。

这两个文件都填充了一长串图像,分别命名为img0.jpg、img1.jpg、imm2.jpg等。我还创建了两个CSV,一个用于训练集,一个为测试集,其中一列包含文件名,一列包含标签。

上述结构很关键,因为数据集的总容量超过10 GB,我电脑的内存肯定无法容纳,更不用说GPU的内存了。因此,我们需要使用DataLoader。(如果你曾经使用过PyTorch,你会很熟悉;这里的概念与PyTorch基本相同。)

为了在Flux中实现这一点,我们需要创建一个自定义结构来包装我们的数据集,以允许它批量加载数据。

为了让我们的自定义结构能够构造数据加载器,我们需要做的就是为类型定义两个方法:length和getindex。下面是我们将用于数据集的实现:

using Flux
using Images
using FileIO
using DataFrames
using Pipe

"""
    ImageDataContainer(labels_df, img_dir)
Implements the functions `length` and `getindex`, which are required to use ImageDataContainer
as an argument in a DataLoader for Flux.
"""
struct ImageDataContainer
    labels::AbstractVector
    filenames::AbstractVector{String}
    function ImageDataContainer(labels_df::DataFrame, img_dir::AbstractString)
        filenames = img_dir .* labels_df[!, 1] # first column should be the filenames
        labels = labels_df[!, 2] # second column should be the labels
        return new(labels, filenames)
    end
end

"Gets the number of observations for a given dataset."
function Base.length(dataset::ImageDataContainer)
    return length(dataset.labels)
end

"Gets the i-th to j-th observations (including labels) for a given dataset."
function Base.getindex(dataset::ImageDataContainer, idxs::Union{UnitRange,Vector})
    batch_imgs = map(idx -> load(dataset.filenames[idx]), idxs)
    batch_labels = map(idx -> dataset.labels[idx], idxs)

    "Applies necessary transforms and reshapings to batches and loads them onto GPU to be fed into a model."
    function transform_batch(imgs, labels)
        # convert imgs to 256×256×3×64 array (Height×Width×Color×Number) of floats (values between 0.0 and 1.0)
        # arrays need to be sent to gpu inside training loop for garbage collector to work properly
        batch_X = @pipe hcat(imgs...) |> reshape(_, (HEIGHT, WIDTH, length(labels))) |> channelview |> permutedims(_, (2, 3, 1, 4))
        batch_y = @pipe labels |> reshape(_, (1, length(labels)))

        return (batch_X, batch_y)
    end

    return transform_batch(batch_imgs, batch_labels)
end

本质上,当Flux试图检索一批图像时,它会调用getindex(dataloader, i:i+batchsize),这在Julia中相当于dataloader[i:i+batchsize]。

因此,我们的自定义getindex函数获取文件名列表,获取适当的文件名,加载这些图像,然后将其处理并重新塑造为适当的HEIGHT × WIDTH × COLOR × NUMBER形状。标签也是如此。

然后,我们的训练、验证和测试数据加载器可以非常容易地完成:

using Flux: Data.DataLoader
using CSV
using DataFrames
using MLUtils

# dataframes containing filenames for images and corresponding labels
const train_df = DataFrame(CSV.File(dataset_dir * "train_labels.csv"))
const test_df = DataFrame(CSV.File(dataset_dir * "test_labels.csv"))

# ImageDataContainer wrappers for dataframes
# gives interface for getting the actual images and labels as tensors
const train_dataset = ImageDataContainer(train_df, train_dir)
const test_dataset = ImageDataContainer(test_df, test_dir)

# randomly sort train dataset into training and validation sets
const train_set, val_set = splitobs(train_dataset, at=0.7, shuffle=true)

const train_loader = DataLoader(train_set, batchsize=BATCH_SIZE, shuffle=true)
const val_loader = DataLoader(val_set, batchsize=BATCH_SIZE, shuffle=true)
const test_loader = DataLoader(test_dataset, batchsize=BATCH_SIZE)

制作模型

数据加载器准备就绪后,下一步是创建模型。首先是基于ResNet的迁移学习模型。事实证明,这项工作相对困难。

在Metalhead.jsl包中(包含用于迁移学习的计算机视觉Flux模型),创建具有预训练权重的ResNet18模型应该与model = ResNet(18; pretrain = true)一样简单。

然而,至少在编写本文时,创建预训练的模型会导致错误。这很可能是因为Metalhead.jsl仍在添加预训练的权重。

我终于在HuggingFace上找到了包含权重的.tar.gz文件:

https://huggingface.co/FluxML/resnet18

我们可以使用以下代码加载权重,并创建我们自己的自定义Flux模型:

using Flux
using Metalhead
using Pipe
using BSON

# load in saved params from bson
resnet = ResNet(18)
@pipe joinpath(@__DIR__, "resnet18.bson") |> BSON.load(_)[:model] |> Flux.loadmodel!(resnet, _)

# last element of resnet18 is a chain
# since we're removing the last element, we just want to recreate it, but with different number of classes
# probably a more elegant, less hard-coded way to do this, but whatever
baseline_model = Chain(
    resnet.layers[1:end-1],
    Chain(
        AdaptiveMeanPool((1, 1)),
        Flux.flatten,
        Dense(512 => N_CLASSES)
    )
)

(注意:如果有比这更优雅的方法来更改ResNet的最后一层,请告诉我。)

创建了预训练的迁移学习模型后,这只剩下两个孪生网络模型。然而,与迁移学习不同,我们必须学习如何手动创建模型。(如果你习惯了PyTorch,这就是Flux与PyTorch的不同之处。)

使用Flux文档和其他在线资源创建CNN相对容易。然而,Flux没有内置层来表示具有参数共享的Twin网络。它最接近的是平行层,它不使用参数共享。

然而,Flux在这里有关于如何创建自定义多个输入或输出层的文档。在我们的例子中,我们可以用来创建自定义Twin层的代码如下:

using Flux

"Custom Flux NN layer which will create twin network from `path` with shared parameters and combine their output with `combine`."
struct Twin{T,F}
    combine::F
    path::T
end

# define the forward pass of the Twin layer
# feeds both inputs, X, through the same path (i.e., shared parameters)
# and combines their outputs
Flux.@functor Twin
(m::Twin)(Xs::Tuple) = m.combine(map(X -> m.path(X), Xs)...)

首先请注意,它以一个简单的结构Twin开头,包含两个字段:combine和path。path是我们的两个图像输入将经过的网络,而combine是在最后将输出组合在一起的函数。

使用Flux.@functor告诉Flux将我们的结构像一个常规的Flux层一样对待。(m::Twin)(Xs::Tuple) = m.combine(map(X -> m.path(X), Xs)…)定义了前向传递,其中元组Xs中的所有输入X都通过path馈送,然后所有输出都通过combine。

要使用自定义CNN架构创建Twin网络,我们可以执行以下操作:

using Flux

twin_model = Twin(
    # this layer combines the outputs of the twin CNNs
    Flux.Bilinear((32,32) => 1),
    
    # this is the architecture that forms the path of the twin network
    Chain(
        # layer 1
        Conv((5,5), 3 => 18, relu),
        MaxPool((3,3), stride=3),
        
        # layer 2
        Conv((5,5), 18 => 36, relu),
        MaxPool((2,2), stride=2),
        
        # layer 3
        Conv((3,3), 36 => 72, relu),
        MaxPool((2,2), stride=2),
        Flux.flatten,
        
        # layer 4
        Dense(19 * 19 * 72 => 64, relu),
        
        # Dropout(0.1),
        # output layer
        Dense(64 => 32, relu)
    )
)

在本例中,我们实际上使用Flux.Biliner层作为组合,这实质上创建了一个连接到两个独立输入的输出层。上面,两个输入是路径的输出,即自定义CNN架构。或者,我们可以以某种方式使用hcat或vcat作为组合,然后在最后添加一个Dense层,但这个解决方案似乎更适合这个问题。

现在,要使用ResNet创建Twin网络,我们可以执行以下操作:

using Flux
using Metalhead
using Pipe
using BSON

# load in saved params from bson
resnet = ResNet(18)
@pipe joinpath(@__DIR__, "resnet18.bson") |> BSON.load(_)[:model] |> Flux.loadmodel!(resnet, _)

# create twin resnet model
twin_resnet = Twin(
    Flux.Bilinear((32,32) => 1),
    Chain(
        resnet.layers[1:end-1],
        Chain(
            AdaptiveMeanPool((1, 1)),
            Flux.flatten,
            Dense(512 => 32)
        )
    )
)

请注意,我们如何使用与之前相同的技巧,并使用Flux.双线性层作为组合,并使用与之前类似的技巧来使用预训练的ResNet作为路径。

训练时间

现在我们的数据加载器和模型准备就绪,剩下的就是训练了。通常,在Flux中,可以使用一个简单的一行代码,@epochs 2 Flux.train!(loss, ps, dataset, opt),但我们确实有一些定制的事情要做。

首先,非孪生网络的训练循环:

using Flux
using Flux: Losses.logitbinarycrossentropy
using CUDA
using ProgressLogging
using Pipe
using BSON

"Stores the history through all the epochs of key training/validation performance metrics."
mutable struct TrainingMetrics
    val_acc::Vector{AbstractFloat}
    val_loss::Vector{AbstractFloat}

    TrainingMetrics(n_epochs::Integer) = new(zeros(n_epochs), zeros(n_epochs))
end

"Trains given model for a given number of epochs and saves the model that performs best on the validation set."
function train!(model, n_epochs::Integer, filename::String)
    model = model |> gpu
    optimizer = ADAM()
    params = Flux.params(model[end]) # transfer learning, so only training last layers

    metrics = TrainingMetrics(n_epochs)

    # zero init performance measures for epoch
    epoch_acc = 0.0
    epoch_loss = 0.0

    # so we can automatically save the model with best val accuracy
    best_acc = 0.0

    # X and y are already in the right shape and on the gpu
    # if they weren't, Zygote.jl would throw a fit because it needs to be able to differentiate this function
    loss(X, y) = logitbinarycrossentropy(model(X), y)

    @info "Beginning training loop..."
    for epoch_idx ∈ 1:n_epochs
        @info "Training epoch $(epoch_idx)..."
        # train 1 epoch, record performance
        @withprogress for (batch_idx, (imgs, labels)) ∈ enumerate(train_loader)
            X = @pipe imgs |> gpu |> float32.(_)
            y = @pipe labels |> gpu |> float32.(_)

            gradients = gradient(() -> loss(X, y), params)
            Flux.Optimise.update!(optimizer, params, gradients)

            @logprogress batch_idx / length(enumerate(train_loader))
        end

        # reset variables
        epoch_acc = 0.0
        epoch_loss = 0.0

        @info "Validating epoch $(epoch_idx)..."
        # val 1 epoch, record performance
        @withprogress for (batch_idx, (imgs, labels)) ∈ enumerate(val_loader)
            X = @pipe imgs |> gpu |> float32.(_)
            y = @pipe labels |> gpu |> float32.(_)

            # feed through the model to create prediction
            ŷ = model(X)

            # calculate the loss and accuracy for this batch, add to accumulator for epoch results
            batch_acc = @pipe ((((σ.(ŷ) .> 0.5) .* 1.0) .== y) .* 1.0) |> cpu |> reduce(+, _)
            epoch_acc += batch_acc
            batch_loss = logitbinarycrossentropy(ŷ, y)
            epoch_loss += (batch_loss |> cpu)

            @logprogress batch_idx / length(enumerate(val_loader))
        end
        # add acc and loss to lists
        metrics.val_acc[epoch_idx] = epoch_acc / length(val_set)
        metrics.val_loss[epoch_idx] = epoch_loss / length(val_set)

        # automatically save the model every time it improves in val accuracy
        if metrics.val_acc[epoch_idx] >= best_acc
            @info "New best accuracy: $(metrics.val_acc[epoch_idx])! Saving model out to $(filename).bson"
            BSON.@save joinpath(@__DIR__, "$(filename).bson")
            best_acc = metrics.val_acc[epoch_idx]
        end
    end

    return model, metrics
end

这里有很多要解开的东西,但本质上这做了一些事情:

  1. 它创建了一个结构,用于跟踪我们想要的任何验证度量。在这种情况下是每个epoch的损失和精度。

  2. 它只选择要训练的最后一层参数。如果我们愿意,我们可以训练整个模型,但这在计算上会更费力。这是不必要的,因为我们使用的是预训练的权重。

  3. 对于每个epoch,它都会遍历要训练的训练集的所有批次。然后,它计算整个验证集(当然是成批的)的准确性和损失。如果提高了epoch的验证精度,则可以保存模型。如果没有,它将继续到下一个时代。

请注意,我们可以在这里做更多的工作,例如,提前停止,但以上内容足以了解大致情况。

接下来,Twin网络的训练循环非常相似,但略有不同:

using Flux
using Flux: Losses.logitbinarycrossentropy
using CUDA
using ProgressLogging
using Pipe
using BSON

"Trains given twin model for a given number of epochs and saves the model that performs best on the validation set."
function train!(model::Twin, n_epochs::Integer, filename::String; is_resnet::Bool=false)
    model = model |> gpu
    optimizer = ADAM()
    params = is_resnet ? Flux.params(model.path[end:end], model.combine) : Flux.params(model) # if custom CNN, need to train all params

    metrics = TrainingMetrics(n_epochs)

    # zero init performance measures for epoch
    epoch_acc = 0.0
    epoch_loss = 0.0

    # so we can automatically save the model with best val accuracy
    best_acc = 0.0

    # X and y are already in the right shape and on the gpu
    # if they weren't, Zygote.jl would throw a fit because it needs to be able to differentiate this function
    loss(Xs, y) = logitbinarycrossentropy(model(Xs), y)

    @info "Beginning training loop..."
    for epoch_idx ∈ 1:n_epochs
        @info "Training epoch $(epoch_idx)..."
        # train 1 epoch, record performance
        @withprogress for (batch_idx, ((imgs₁, labels₁), (imgs₂, labels₂))) ∈ enumerate(zip(train_loader₁, train_loader₂))
            X₁ = @pipe imgs₁ |> gpu |> float32.(_)
            y₁ = @pipe labels₁ |> gpu |> float32.(_)

            X₂ = @pipe imgs₂ |> gpu |> float32.(_)
            y₂ = @pipe labels₂ |> gpu |> float32.(_)

            Xs = (X₁, X₂)
            y = ((y₁ .== y₂) .* 1.0) # y represents if both images have the same label

            gradients = gradient(() -> loss(Xs, y), params)
            Flux.Optimise.update!(optimizer, params, gradients)

            @logprogress batch_idx / length(enumerate(train_loader₁))
        end

        # reset variables
        epoch_acc = 0.0
        epoch_loss = 0.0

        @info "Validating epoch $(epoch_idx)..."
        # val 1 epoch, record performance
        @withprogress for (batch_idx, ((imgs₁, labels₁), (imgs₂, labels₂))) ∈ enumerate(zip(val_loader₁, val_loader₂))
            X₁ = @pipe imgs₁ |> gpu |> float32.(_)
            y₁ = @pipe labels₁ |> gpu |> float32.(_)

            X₂ = @pipe imgs₂ |> gpu |> float32.(_)
            y₂ = @pipe labels₂ |> gpu |> float32.(_)

            Xs = (X₁, X₂)
            y = ((y₁ .== y₂) .* 1.0) # y represents if both images have the same label

            # feed through the model to create prediction
            ŷ = model(Xs)

            # calculate the loss and accuracy for this batch, add to accumulator for epoch results
            batch_acc = @pipe ((((σ.(ŷ) .> 0.5) .* 1.0) .== y) .* 1.0) |> cpu |> reduce(+, _)
            epoch_acc += batch_acc
            batch_loss = logitbinarycrossentropy(ŷ, y)
            epoch_loss += (batch_loss |> cpu)

            @logprogress batch_idx / length(enumerate(val_loader))
        end
        # add acc and loss to lists
        metrics.val_acc[epoch_idx] = epoch_acc / length(val_set)
        metrics.val_loss[epoch_idx] = epoch_loss / length(val_set)

        # automatically save the model every time it improves in val accuracy
        if metrics.val_acc[epoch_idx] >= best_acc
            @info "New best accuracy: $(metrics.val_acc[epoch_idx])! Saving model out to $(filename).bson"
            BSON.@save joinpath(@__DIR__, "$(filename).bson")
            best_acc = metrics.val_acc[epoch_idx]
        end
    end

    return model, metrics
end

首先注意,我们使用了一个同名函数train!,但具有稍微不同的函数签名。这允许Julia根据我们正在训练的网络类型来分配正确的功能。

还要注意,Twin ResNet模型冻结其预训练的参数,而我们训练所有Twin自定义CNN参数。

除此之外,训练循环的其余部分基本相同,只是我们必须使用两个训练数据加载器和两个验证数据加载器。这些为我们提供了两个输入和每批两组标签,我们将其适当地输入到Twin模型中。

最后,请注意,Twin模型预测两个输入图像是否具有相同的标签,而常规非Twin网络仅直接预测标签。

这样,为所有三个模型的测试集构建测试循环应该不会太难。因为这篇文章的目的是要解决我在网上找不到例子的主要痛点,所以我将把测试部分作为练习留给读者。

最后

最大的挑战是缩小从相对简单的示例到更先进的技术之间的差距,而这些技术缺乏示例。但这也揭示了Julia的优势:因为它本身就很快,所以搜索包的源代码以找到答案通常非常容易。

有几次,我发现自己在浏览Flux源代码,以了解一些东西是如何工作的。每一次我都能非常轻松快速地找到答案。我不确定我是否有勇气为PyTorch尝试类似的东西。

另一个挑战是Metalhead.jsl的不成熟状态,这在Julia生态系统中肯定不是独一无二的,因为它的功能不完整。

最后一个想法是,我发现Flux非常有趣和优雅……一旦我掌握了它的窍门。我肯定会在未来与Flux一起进行更深入的学习。

感谢阅读!

参考引用

[1] M. Innes, Flux: Elegant Machine Learning with Julia (2018), Journal of Open Source Software

[2] Arun Pandian J. and G. Gopal, Data for: Identification of Plant Leaf Diseases Using a 9-layer Deep Convolutional Neural Network (2019), Mendeley Data

[3] S. S. Chouhan, A. Kaul, and U. P. Singh, A Database of Leaf Images: Practice towards Plant Conservation with Plant Pathology (2019), Mendely Data

[4] V. P. Kour and S. Arora, PlantaeK: A leaf database of native plants of Jammu and Kashmir (2019), Mendeley Data

☆ END ☆

如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 woshicver」,每日朋友圈更新一篇高质量博文。

扫描二维码添加小编↓

4191d1d1b24ea973f0cdeaccc2973c4d.jpeg

猜你喜欢

转载自blog.csdn.net/woshicver/article/details/130120301
今日推荐