Skip to content

Commit d719428

Browse files
author
guoyongzhi
committed
optimizing trainepoch_gen! logic
1 parent af1649a commit d719428

5 files changed

Lines changed: 22 additions & 23 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "WordCloud"
22
uuid = "6385f0a0-cb03-45b6-9089-4e0acc74b26b"
33
authors = ["guoyongzhi <momoshanghan@163.com>"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ wordcloud in Julia
88
* [x] 引入动量加速训练
99
* [x] 分代调整以优化性能
1010
* [x] 控制字体大小和填充密度的策略
11-
* [x] 重新放置、旋转和缩放的策略
11+
* [x] 重新放置和缩放的策略
1212
* [x] 文字颜色和方向
1313
* [ ] 并行计算
1414

@@ -27,7 +27,7 @@ paint(wc, "qianziwen.png")
2727
```julia
2828
wc = wordcloud(
2929
process(open("res/alice.txt"), stopwords=WordCloud.stopwords_en ["said"]),
30-
maskimg = loadmask("res/alice_mask.png", color="#faeef8"),
30+
mask = loadmask("res/alice_mask.png", color="#faeef8"),
3131
colors = (WordCloud.colorschemes[:Set1_5].colors..., ),
3232
angles = (0, 90),
3333
filling_rate = 0.6) |> generate

src/WordCloud.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ include("qtree.jl")
55
include("rendering.jl")
66
include("nlp.jl")
77
using .Render
8+
using .QTree
89
using .NLP
910
include("train.jl")
1011
include("interface.jl")

src/interface.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mutable struct wordcloud
5353
words
5454
weights
5555
imgs
56-
maskimg
56+
mask
5757
qtrees
5858
maskqtree
5959
params::Dict{Symbol,Any}
@@ -72,10 +72,10 @@ angles = [0, 22, 4, 1, 100, 10, ......] #use entries sequentially in cycle
7272
filling_rate = 0.5
7373
border = 1
7474
### mask kargs
75-
maskimg = loadmask("res/heart.jpg", 256, 256) #see doc of `loadmask`
76-
maskimg = loadmask("res/heart.jpg", color="red", ratio=2) #see doc of `loadmask`
77-
maskimg = shape(ellipse, 800, 600, color="white", bgcolor=(0,0,0,0)) #see doc of `shape`
78-
transparentcolor = ARGB32(0,0,0,0) #set the transparent color in maskimg
75+
mask = loadmask("res/heart.jpg", 256, 256) #see doc of `loadmask`
76+
mask = loadmask("res/heart.jpg", color="red", ratio=2) #see doc of `loadmask`
77+
mask = shape(ellipse, 800, 600, color="white", bgcolor=(0,0,0,0)) #see doc of `shape`
78+
transparentcolor = ARGB32(0,0,0,0) #set the transparent color in mask
7979
"""
8080
wordcloud(wordsweights::Tuple; kargs...) = wordcloud(wordsweights...; kargs...)
8181
wordcloud(counter::AbstractDict; kargs...) = wordcloud(keys(counter)|>collect, values(counter)|>collect; kargs...)
@@ -102,7 +102,7 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
102102
angles = angles[si]
103103
params[:angles] = angles
104104
params[:font] = font
105-
if !haskey(params, :maskimg)
105+
if !haskey(params, :mask)
106106
maskcolor = "white"
107107
try
108108
# maskcolor = RGB(1,1,1) - RGB(sum(colors_o)/length(colors_o)) #补色
@@ -116,14 +116,14 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
116116
@show "colors sum failed",colors_o
117117
maskcolor = "black"
118118
end
119-
maskimg = randommask(maskcolor)
119+
mask = randommask(maskcolor)
120120
transparentcolor = get(params, :transparentcolor, ARGB(1, 1, 1, 0)) |> parsecolor
121121
else
122-
maskimg = params[:maskimg]
122+
mask = params[:mask]
123123
end
124-
transparentcolor = get(params, :transparentcolor, maskimg[1]) |> parsecolor
125-
maskimg, maskqtree, groundsize, groundoccupied = preparebackground(maskimg, transparentcolor)
126-
# params[:maskimg] = maskimg
124+
transparentcolor = get(params, :transparentcolor, mask[1]) |> parsecolor
125+
mask, maskqtree, groundsize, groundoccupied = preparebackground(mask, transparentcolor)
126+
# params[:mask] = mask
127127
# params[:maskqtree] = maskqtree
128128
params[:groundsize] = groundsize
129129
params[:groundoccupied] = groundoccupied
@@ -142,7 +142,7 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
142142
params[:border] = border
143143
params[:font] = font
144144
placement!(deepcopy(maskqtree), qtrees)
145-
wordcloud(words, weights, imgs, maskimg, qtrees, maskqtree, params)
145+
wordcloud(words, weights, imgs, mask, qtrees, maskqtree, params)
146146
end
147147

148148
function getposition(wc)
@@ -152,7 +152,7 @@ function getposition(wc)
152152
end
153153

154154
function paint(wc::wordcloud, args...; kargs...)
155-
resultpic = convert.(ARGB32, wc.maskimg)#.|>ARGB32
155+
resultpic = convert.(ARGB32, wc.mask)#.|>ARGB32
156156
overlay!(resultpic, wc.imgs, getposition(wc))
157157
if !(isempty(args) && isempty(kargs))
158158
resultpic = imresize(resultpic, args...; kargs...)

src/train.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ function trainepoch_gen!(qtrees, mask; optimiser=(t, Δ)->Δ./4, nearlevel=-4, q
182182
return nsp
183183
end
184184
if length(nearpool) == 0 return nsp end
185-
# @show length(nearpool)
186-
for ni in 1:min(10, 4length(indpairs)÷length(nearpool))
185+
# @show "#",length(nearpool)
186+
for ni in 1 : 2length(indpairs)÷length(nearpool) #the loop cost should not exceed 2length(indpairs)
187187
empty!(collpool)
188188
for (i1, i2) in nearpool |> shuffle!
189189
cp = collision_bfs_rand(getqt(i1), getqt(i2))
@@ -192,9 +192,9 @@ function trainepoch_gen!(qtrees, mask; optimiser=(t, Δ)->Δ./4, nearlevel=-4, q
192192
push!(collpool, (i1, i2))
193193
end
194194
end
195-
if length(collpool) == 0 return nsp end
196195
# @show length(collpool)
197-
for ci in 1:min(10, 4length(nearpool)÷length(collpool))
196+
if ni > length(collpool) break end # loop only when there are enough collisions
197+
for ci in 1 : 2length(nearpool)÷length(collpool) #the loop cost should not exceed 2length(nearpool)
198198
nsp2 = 0
199199
for (i1, i2) in collpool |> shuffle!
200200
cp = collision_bfs_rand(getqt(i1), getqt(i2))
@@ -204,9 +204,7 @@ function trainepoch_gen!(qtrees, mask; optimiser=(t, Δ)->Δ./4, nearlevel=-4, q
204204
nsp2 += 1
205205
end
206206
end
207-
if nsp2 == 0
208-
return nsp
209-
end
207+
if ci > nsp2 break end # loop only when there are enough collisions
210208
end
211209
# @show length(indpairs),length(nearpool),collpool
212210
end

0 commit comments

Comments
 (0)