Julia 基于Flux深度学习框架的cifar10数据集分类

目录

一、安装Julia

二、Flux简介

三、安装Flux和相关依赖库

四、cifar10项目下载

*五、cifar10数据集下载

六、开始训练


一、安装Julia

IDE是Atom,安装和使用教程为:Windows10 Atom安装和运行Julia的使用教程(详细)


二、Flux简介

1.Flux.jl是一个内置于Julia的机器学习框架。它与PyTorch有一些相似之处,就像大多数现代框架一样。

2.Flux是一种优雅的机器学习方法。 它是100%纯Julia堆栈形式,并在Julia的原生GPU和AD支持之上提供轻量级抽象。

3.Flux是一个用于机器学习的库。 它功能强大,具有即插即拔的灵活性,即内置了许多有用的工具,但也可以在需要的地方使用Julia语言的全部功能。

4.Flux遵循以下几个关键原则:

(1) Flux对于正则化嵌入等功能显式API相对较少。 相反,写下数学形式将起作用 ,并且速度很快

(2) 所有的知识和工具,从LSTM到GPU内核,都是简单的Julia代码。 如果有疑问的话,可以查看官方教程。 如果需要不同的函数块或者是功能模块,我们也可以轻松自己动手实现。

(3)Flux适用于Julia库,包括从数据帧和图像到差分方程求解器等等内容,因此我们也可以轻松构建集成Flux模型的复杂数据处理流水线。

5.Flux相关教程链接(FQ):https://fluxml.ai/Flux.jl/stable/

6.Flux模型代码示例链接:https://github.com/FluxML/model-zoo/


三、安装Flux和相关依赖库

1.打开julia控制台,或者打开Atom启动下方REPL的julia,先输入如下指令

using Pkg
 

                       

2.安装Flux

Pkg.add("Flux")
 

3.同理,安装依赖项Metalhead

Pkg.add("Metalhead")
 
Pkg.add("Images")
 
Pkg.add("Statistics")
 

一般安装了Metalhead也会自动帮你装上ImagesStatistics~


四、cifar10项目下载

1.下载model-zoo文件夹:https://github.com/FluxML/model-zoo/

2.cifar10.jlmodel-zoo-master\vision\cifar10

3.我们在Atom里打开这个项目,如下


*五、cifar10数据集下载

1.github上的model-zoo里cifar10的下载函数里面解压的方式Linux的,在

C:\Users\你的电脑用户名\.julia\packages\Metalhead\fYeSU\src\datasets\autodetect.jl


  
  1. function download(which)
  2. if which === ImageNet
  3. error("ImageNet is not automatiacally downloadable. See instructions in datasets/README.md")
  4. elseif which == CIFAR10
  5. local_path = joinpath(@__DIR__, "..","..",datasets, "cifar-10-binary.tar.gz")
  6. #print(local_path)
  7. dir_path = joinpath(@__DIR__,"..","..","datasets")
  8. if(!isdir(joinpath(dir_path, "cifar-10-batches-bin")))
  9. if(!isfile(local_path))
  10. Base.download("https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz", local_path)
  11. end
  12. run(`tar -xzvf $local_path -C $dir_path`)
  13. end
  14. else
  15. error("Download not supported for $(which)")
  16. end
  17. end

这意味着解压函数在windows10上是无效的,但是这并不影响我们在windows上的使用,我们只需要手动下载即可

2.下载地址:https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz

3.下载完成后请放到这个文件夹(其实是放到这里是为了配合Linux操作系统):

C:\Users\你的电脑用户名\.julia\packages\Metalhead\fYeSU\datasets

解压后的内容如下:

 注意:不放这里,你就等着报错报到死吧!!那就是无法找到cifar10数据集位置!!


六、开始训练

1.核心代码

cifar10.jl


  
  1. using Flux, Metalhead, Statistics
  2. using Flux: onehotbatch, onecold, crossentropy, throttle
  3. using Metalhead: trainimgs
  4. using Images: channelview
  5. using Statistics: mean
  6. using Base.Iterators: partition
  7. # VGG16 and VGG19 models
  8. vgg16() = Chain(
  9. Conv((3, 3), 3 => 64, relu, pad=(1, 1), stride=(1, 1)),
  10. BatchNorm(64),
  11. Conv((3, 3), 64 => 64, relu, pad=(1, 1), stride=(1, 1)),
  12. BatchNorm(64),
  13. x -> maxpool(x, (2, 2)),
  14. Conv((3, 3), 64 => 128, relu, pad=(1, 1), stride=(1, 1)),
  15. BatchNorm(128),
  16. Conv((3, 3), 128 => 128, relu, pad=(1, 1), stride=(1, 1)),
  17. BatchNorm(128),
  18. x -> maxpool(x, (2,2)),
  19. Conv((3, 3), 128 => 256, relu, pad=(1, 1), stride=(1, 1)),
  20. BatchNorm(256),
  21. Conv((3, 3), 256 => 256, relu, pad=(1, 1), stride=(1, 1)),
  22. BatchNorm(256),
  23. Conv((3, 3), 256 => 256, relu, pad=(1, 1), stride=(1, 1)),
  24. BatchNorm(256),
  25. x -> maxpool(x, (2, 2)),
  26. Conv((3, 3), 256 => 512, relu, pad=(1, 1), stride=(1, 1)),
  27. BatchNorm(512),
  28. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  29. BatchNorm(512),
  30. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  31. BatchNorm(512),
  32. x -> maxpool(x, (2, 2)),
  33. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  34. BatchNorm(512),
  35. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  36. BatchNorm(512),
  37. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  38. BatchNorm(512),
  39. x -> maxpool(x, (2, 2)),
  40. x -> reshape(x, :, size(x, 4)),
  41. Dense(512, 4096, relu),
  42. Dropout(0.5),
  43. Dense(4096, 4096, relu),
  44. Dropout(0.5),
  45. Dense(4096, 10),
  46. softmax) |> gpu
  47. vgg19() = Chain(
  48. Conv((3, 3), 3 => 64, relu, pad=(1, 1), stride=(1, 1)),
  49. BatchNorm(64),
  50. Conv((3, 3), 64 => 64, relu, pad=(1, 1), stride=(1, 1)),
  51. BatchNorm(64),
  52. x -> maxpool(x, (2, 2)),
  53. Conv((3, 3), 64 => 128, relu, pad=(1, 1), stride=(1, 1)),
  54. BatchNorm(128),
  55. Conv((3, 3), 128 => 128, relu, pad=(1, 1), stride=(1, 1)),
  56. BatchNorm(128),
  57. x -> maxpool(x, (2, 2)),
  58. Conv((3, 3), 128 => 256, relu, pad=(1, 1), stride=(1, 1)),
  59. BatchNorm(256),
  60. Conv((3, 3), 256 => 256, relu, pad=(1, 1), stride=(1, 1)),
  61. BatchNorm(256),
  62. Conv((3, 3), 256 => 256, relu, pad=(1, 1), stride=(1, 1)),
  63. BatchNorm(256),
  64. Conv((3, 3), 256 => 256, relu, pad=(1, 1), stride=(1, 1)),
  65. x -> maxpool(x, (2, 2)),
  66. Conv((3, 3), 256 => 512, relu, pad=(1, 1), stride=(1, 1)),
  67. BatchNorm(512),
  68. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  69. BatchNorm(512),
  70. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  71. BatchNorm(512),
  72. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  73. x -> maxpool(x, (2, 2)),
  74. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  75. BatchNorm(512),
  76. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  77. BatchNorm(512),
  78. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  79. BatchNorm(512),
  80. Conv((3, 3), 512 => 512, relu, pad=(1, 1), stride=(1, 1)),
  81. x -> maxpool(x, (2, 2)),
  82. x -> reshape(x, :, size(x, 4)),
  83. Dense(512, 4096, relu),
  84. Dropout(0.5),
  85. Dense(4096, 4096, relu),
  86. Dropout(0.5),
  87. Dense(4096, 10),
  88. softmax) |> gpu
  89. # Function to convert the RGB image to Float64 Arrays
  90. getarray(X) = Float32.(permutedims(channelview(X), (2, 3, 1)))
  91. # Fetching the train and validation data and getting them into proper shape
  92. X = trainimgs(CIFAR10)
  93. imgs = [getarray(X[i].img) for i in 1:50000]
  94. labels = onehotbatch([X[i].ground_truth.class for i in 1:50000],1:10)
  95. train = gpu.([(cat(imgs[i]..., dims = 4), labels[:,i]) for i in partition(1:49000, 100)])
  96. valset = collect(49001:50000)
  97. valX = cat(imgs[valset]..., dims = 4) |> gpu
  98. valY = labels[:, valset] |> gpu
  99. # Defining the loss and accuracy functions
  100. m = vgg16()
  101. loss(x, y) = crossentropy(m(x), y)
  102. accuracy(x, y) = mean(onecold(m(x), 1:10) .== onecold(y, 1:10))
  103. # Defining the callback and the optimizer
  104. evalcb = throttle(() -> @show(accuracy(valX, valY)), 10)
  105. opt = ADAM()
  106. # Starting to train models
  107. Flux.train!(loss, params(m), train, opt, cb = evalcb)
  108. # Fetch the test data from Metalhead and get it into proper shape.
  109. # CIFAR-10 does not specify a validation set so valimgs fetch the testdata instead of testimgs
  110. test = valimgs(CIFAR10)
  111. testimgs = [getarray(test[i].img) for i in 1:10000]
  112. testY = onehotbatch([test[i].ground_truth.class for i in 1:10000], 1:10) |> gpu
  113. testX = cat(testimgs..., dims = 4) |> gpu
  114. # Print the final accuracy
  115. @show(accuracy(testX, testY))

2.菜单栏里 Packages->Julia->Run File,可以在REPL里看到训练的效果,也就是最后一句代码展示准确度

              

3.至于如何放到GPU上训练,我们还需要下载CuArrays


  
  1. Using Pkg
  2. Pkg.add("CuArrays")

以及安装CUDA和cuDNN支持,具体细节看官方文档:https://fluxml.ai/Flux.jl/stable/gpu/#Installation-

文章来源: nickhuang1996.blog.csdn.net,作者:悲恋花丶无心之人,版权归原作者所有,如需转载,请联系作者。

原文链接:nickhuang1996.blog.csdn.net/article/details/89500786

(完)