Skip to content

Commit

Permalink
optimizing trainepoch_gen! logic
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyongzhi committed Aug 26, 2020
1 parent af1649a commit d719428
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "WordCloud"
uuid = "6385f0a0-cb03-45b6-9089-4e0acc74b26b"
authors = ["guoyongzhi <[email protected]>"]
version = "0.2.0"
version = "0.2.1"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ wordcloud in Julia
* [x] 引入动量加速训练
* [x] 分代调整以优化性能
* [x] 控制字体大小和填充密度的策略
* [x] 重新放置、旋转和缩放的策略
* [x] 重新放置和缩放的策略
* [x] 文字颜色和方向
* [ ] 并行计算

Expand All @@ -27,7 +27,7 @@ paint(wc, "qianziwen.png")
```julia
wc = wordcloud(
process(open("res/alice.txt"), stopwords=WordCloud.stopwords_en ["said"]),
maskimg = loadmask("res/alice_mask.png", color="#faeef8"),
mask = loadmask("res/alice_mask.png", color="#faeef8"),
colors = (WordCloud.colorschemes[:Set1_5].colors..., ),
angles = (0, 90),
filling_rate = 0.6) |> generate
Expand Down
1 change: 1 addition & 0 deletions src/WordCloud.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include("qtree.jl")
include("rendering.jl")
include("nlp.jl")
using .Render
using .QTree
using .NLP
include("train.jl")
include("interface.jl")
Expand Down
26 changes: 13 additions & 13 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mutable struct wordcloud
words
weights
imgs
maskimg
mask
qtrees
maskqtree
params::Dict{Symbol,Any}
Expand All @@ -72,10 +72,10 @@ angles = [0, 22, 4, 1, 100, 10, ......] #use entries sequentially in cycle
filling_rate = 0.5
border = 1
### mask kargs
maskimg = loadmask("res/heart.jpg", 256, 256) #see doc of `loadmask`
maskimg = loadmask("res/heart.jpg", color="red", ratio=2) #see doc of `loadmask`
maskimg = shape(ellipse, 800, 600, color="white", bgcolor=(0,0,0,0)) #see doc of `shape`
transparentcolor = ARGB32(0,0,0,0) #set the transparent color in maskimg
mask = loadmask("res/heart.jpg", 256, 256) #see doc of `loadmask`
mask = loadmask("res/heart.jpg", color="red", ratio=2) #see doc of `loadmask`
mask = shape(ellipse, 800, 600, color="white", bgcolor=(0,0,0,0)) #see doc of `shape`
transparentcolor = ARGB32(0,0,0,0) #set the transparent color in mask
"""
wordcloud(wordsweights::Tuple; kargs...) = wordcloud(wordsweights...; kargs...)
wordcloud(counter::AbstractDict; kargs...) = wordcloud(keys(counter)|>collect, values(counter)|>collect; kargs...)
Expand All @@ -102,7 +102,7 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
angles = angles[si]
params[:angles] = angles
params[:font] = font
if !haskey(params, :maskimg)
if !haskey(params, :mask)
maskcolor = "white"
try
# maskcolor = RGB(1,1,1) - RGB(sum(colors_o)/length(colors_o)) #补色
Expand All @@ -116,14 +116,14 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
@show "colors sum failed",colors_o
maskcolor = "black"
end
maskimg = randommask(maskcolor)
mask = randommask(maskcolor)
transparentcolor = get(params, :transparentcolor, ARGB(1, 1, 1, 0)) |> parsecolor
else
maskimg = params[:maskimg]
mask = params[:mask]
end
transparentcolor = get(params, :transparentcolor, maskimg[1]) |> parsecolor
maskimg, maskqtree, groundsize, groundoccupied = preparebackground(maskimg, transparentcolor)
# params[:maskimg] = maskimg
transparentcolor = get(params, :transparentcolor, mask[1]) |> parsecolor
mask, maskqtree, groundsize, groundoccupied = preparebackground(mask, transparentcolor)
# params[:mask] = mask
# params[:maskqtree] = maskqtree
params[:groundsize] = groundsize
params[:groundoccupied] = groundoccupied
Expand All @@ -142,7 +142,7 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
params[:border] = border
params[:font] = font
placement!(deepcopy(maskqtree), qtrees)
wordcloud(words, weights, imgs, maskimg, qtrees, maskqtree, params)
wordcloud(words, weights, imgs, mask, qtrees, maskqtree, params)
end

function getposition(wc)
Expand All @@ -152,7 +152,7 @@ function getposition(wc)
end

function paint(wc::wordcloud, args...; kargs...)
resultpic = convert.(ARGB32, wc.maskimg)#.|>ARGB32
resultpic = convert.(ARGB32, wc.mask)#.|>ARGB32
overlay!(resultpic, wc.imgs, getposition(wc))
if !(isempty(args) && isempty(kargs))
resultpic = imresize(resultpic, args...; kargs...)
Expand Down
12 changes: 5 additions & 7 deletions src/train.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function trainepoch_gen!(qtrees, mask; optimiser=(t, Δ)->Δ./4, nearlevel=-4, q
return nsp
end
if length(nearpool) == 0 return nsp end
# @show length(nearpool)
for ni in 1:min(10, 4length(indpairs)÷length(nearpool))
# @show "#",length(nearpool)
for ni in 1 : 2length(indpairs)÷length(nearpool) #the loop cost should not exceed 2length(indpairs)
empty!(collpool)
for (i1, i2) in nearpool |> shuffle!
cp = collision_bfs_rand(getqt(i1), getqt(i2))
Expand All @@ -192,9 +192,9 @@ function trainepoch_gen!(qtrees, mask; optimiser=(t, Δ)->Δ./4, nearlevel=-4, q
push!(collpool, (i1, i2))
end
end
if length(collpool) == 0 return nsp end
# @show length(collpool)
for ci in 1:min(10, 4length(nearpool)÷length(collpool))
if ni > length(collpool) break end # loop only when there are enough collisions
for ci in 1 : 2length(nearpool)÷length(collpool) #the loop cost should not exceed 2length(nearpool)
nsp2 = 0
for (i1, i2) in collpool |> shuffle!
cp = collision_bfs_rand(getqt(i1), getqt(i2))
Expand All @@ -204,9 +204,7 @@ function trainepoch_gen!(qtrees, mask; optimiser=(t, Δ)->Δ./4, nearlevel=-4, q
nsp2 += 1
end
end
if nsp2 == 0
return nsp
end
if ci > nsp2 break end # loop only when there are enough collisions
end
# @show length(indpairs),length(nearpool),collpool
end
Expand Down

0 comments on commit d719428

Please sign in to comment.