blocklearning 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blocklearning/__init__.py +1 -0
- blocklearning/__main__.py +6 -0
- blocklearning/analysis.py +133 -0
- blocklearning/anomaly.py +148 -0
- blocklearning/blocks.py +601 -0
- blocklearning/cli.py +46 -0
- blocklearning/codegen.py +1012 -0
- blocklearning/core.py +861 -0
- blocklearning/data/bearing_uottawa.SOURCE.txt +6 -0
- blocklearning/data/bearing_uottawa.npz +0 -0
- blocklearning/data/concrete.SOURCE.txt +4 -0
- blocklearning/data/concrete.npz +0 -0
- blocklearning/data/digits32.SOURCE.txt +4 -0
- blocklearning/data/digits32.npz +0 -0
- blocklearning/data/ecg_mitdb.SOURCE.txt +6 -0
- blocklearning/data/ecg_mitdb.npz +0 -0
- blocklearning/data/hydraulic.SOURCE.txt +6 -0
- blocklearning/data/hydraulic.npz +0 -0
- blocklearning/data/pcb_det.SOURCE.txt +6 -0
- blocklearning/data/pcb_det.npz +0 -0
- blocklearning/data/pneumatic_seg.SOURCE.txt +8 -0
- blocklearning/data/pneumatic_seg.npz +0 -0
- blocklearning/data/pneumonia28.SOURCE.txt +13 -0
- blocklearning/data/pneumonia28.npz +0 -0
- blocklearning/data/steel_plates.SOURCE.txt +5 -0
- blocklearning/data/steel_plates.npz +0 -0
- blocklearning/data/steel_power.SOURCE.txt +3 -0
- blocklearning/data/steel_power.npz +0 -0
- blocklearning/data/steelblast.SOURCE.txt +4 -0
- blocklearning/data/steelblast.npz +0 -0
- blocklearning/data.py +1158 -0
- blocklearning/detect.py +222 -0
- blocklearning/forecast.py +204 -0
- blocklearning/graph.py +602 -0
- blocklearning/jobs.py +501 -0
- blocklearning/labeling.py +1294 -0
- blocklearning/labeling_sources.py +217 -0
- blocklearning/layers.py +615 -0
- blocklearning/losses.py +199 -0
- blocklearning/multi.py +175 -0
- blocklearning/nn_extra.py +643 -0
- blocklearning/predict.py +344 -0
- blocklearning/pretrained/LICENSE-mnv3s.txt +202 -0
- blocklearning/pretrained/mnv3s.SOURCE.txt +6 -0
- blocklearning/pretrained/mnv3s.bin +0 -0
- blocklearning/pretrained/mnv3s.json +1 -0
- blocklearning/pretrained.py +56 -0
- blocklearning/project.py +120 -0
- blocklearning/segment.py +173 -0
- blocklearning/server.py +116 -0
- blocklearning/static/app.js +1986 -0
- blocklearning/static/boot.js +51 -0
- blocklearning/static/demo.jpg +0 -0
- blocklearning/static/demo.webm +0 -0
- blocklearning/static/engine.js +1326 -0
- blocklearning/static/graph.js +1902 -0
- blocklearning/static/index.html +301 -0
- blocklearning/static/label.html +158 -0
- blocklearning/static/label.js +1040 -0
- blocklearning/static/labels.js +227 -0
- blocklearning/static/projects.js +516 -0
- blocklearning/static/store.js +259 -0
- blocklearning/static/style.css +844 -0
- blocklearning/static/train-worker.js +41 -0
- blocklearning/static/web.js +602 -0
- blocklearning/tasks.py +351 -0
- blocklearning/web.py +412 -0
- blocklearning/weblabel.py +316 -0
- blocklearning-0.1.0.dist-info/METADATA +66 -0
- blocklearning-0.1.0.dist-info/RECORD +74 -0
- blocklearning-0.1.0.dist-info/WHEEL +4 -0
- blocklearning-0.1.0.dist-info/entry_points.txt +2 -0
- blocklearning-0.1.0.dist-info/licenses/LICENSE +25 -0
- blocklearning-0.1.0.dist-info/licenses/src/blocklearning/pretrained/LICENSE-mnv3s.txt +202 -0
|
@@ -0,0 +1,1902 @@
|
|
|
1
|
+
// BlockLearning 모델 편집기: 층(상자)을 선으로 이은 그래프. 자리는 자동으로 위→아래 (갈래는 옆으로, 건너뛰는 선은 옆으로 돌아감).
|
|
2
|
+
// 그래프 JSON(v2): {version: 2, nodes: [{id, type, ...설정, inputs: [id...]}]}. 입력 노드 id "input", Repeat 묶음 안의 입력 "in".
|
|
3
|
+
// 앞부분은 순수 함수(자동 배치·편집 동작·글자)라 node로 시험하고, 뒷부분(mount)이 화면을 그린다.
|
|
4
|
+
(function () { // 감싸서 이름이 전역으로 새지 않게 (app.js와 같은 전역 공간을 씀)
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const BOX_W = 236, BOX_H = 50, MERGE_W = 104, MERGE_H = 30, GAP_Y = 34, GAP_X = 30, DUMMY_W = 14, HEAD = 30, PAD = 16,
|
|
8
|
+
MARGIN = 24, IN_W = 14, IN_H = 10, ACT_D = 64, SNAP = 48, GAP_H = 60;
|
|
9
|
+
|
|
10
|
+
// ---- 블록 목록 (왼쪽): 범주 → 블록. 같은 종류에 설정만 다른 것(예: Causal Attention)도 따로 둠
|
|
11
|
+
const CATS = [["dense", "완전연결 (Dense)"], ["conv", "합성곱 (Conv)"], ["pool", "줄이기·모으기 (Pooling)"], ["up", "키우기 (Upsampling)"],
|
|
12
|
+
["rnn", "순환 (RNN)"], ["attn", "어텐션 (Attention)"], ["tf", "Transformer 부품"], ["norm", "정규화 (Normalization)"],
|
|
13
|
+
["act", "활성화 (Activation)"], ["reg", "과적합 막기 (Regularization)"], ["merge", "합치기·나누기"], ["shape", "모양 바꾸기"],
|
|
14
|
+
["prep", "입력 변환"], ["aug", "증강 (학습 때만)"], ["pre", "사전학습 (고정)"], ["pack", "묶음 (자주 쓰는 조합)"]];
|
|
15
|
+
const COLORS = { input: "gray", dense: "gray", conv: "teal", pool: "teal", up: "teal", rnn: "coral", attn: "blue", tf: "blue", norm: "amber",
|
|
16
|
+
act: "purple", reg: "gray", merge: "add", shape: "gray", prep: "pink", aug: "pink", pre: "blue", out: "coral", pack: "gray" };
|
|
17
|
+
// 블록이 받는 모양: seq (채널, 길이) · img (채널, 높이, 너비) · sp (둘 다) · any
|
|
18
|
+
const RANK = { conv: "seq", pool: "seq", global_avg: "seq", last_step: "seq", conv2d: "img", pool2d: "img", global_avg2d: "img",
|
|
19
|
+
lstm: "seq", gru: "seq", simple_rnn: "seq", se: "sp", dw_conv: "seq", sep_conv: "seq", max_pool: "seq", global_max: "seq", first_step: "seq",
|
|
20
|
+
conv_t: "seq", cls_token: "seq", dw_conv2d: "img", sep_conv2d: "img", max_pool2d: "img", global_max2d: "img", conv_t2d: "img",
|
|
21
|
+
patch_embed: "img", to_seq: "img", rand_rotate: "img", rand_zoom: "img", rand_stretch: "seq", spectrogram: "seq",
|
|
22
|
+
adaptive_pool: "sp", attn_pool: "sp", upsample: "sp", pixel_shuffle: "sp", eca: "sp", cbam: "sp", spatial_attn: "sp", mha: "sp",
|
|
23
|
+
linear_attn: "sp", nonlocal: "sp", cross_attn: "sp", pos_enc: "sp", groupnorm: "sp", instancenorm: "sp", spatial_dropout: "sp",
|
|
24
|
+
permute: "sp", crop: "sp", pad: "sp", rand_flip: "sp", rand_shift: "sp", embedding: "sp", mnv3s: "img" };
|
|
25
|
+
const TWO_D = { conv: "conv2d", pool: "pool2d", global_avg: "global_avg2d", dw_conv: "dw_conv2d", sep_conv: "sep_conv2d", max_pool: "max_pool2d",
|
|
26
|
+
global_max: "global_max2d", conv_t: "conv_t2d" };
|
|
27
|
+
const ONE_D = Object.fromEntries(Object.entries(TWO_D).map(([a, b]) => [b, a]));
|
|
28
|
+
const P = (cat, name, desc, make) => ({ cat, name, desc, make });
|
|
29
|
+
const PALETTE = {
|
|
30
|
+
dense: P("dense", "Dense", "모든 값을 섞어 정한 개수(units)로 (Linear)", () => ({ type: "dense", units: 16 })),
|
|
31
|
+
glu: P("dense", "GLU", "반은 값, 반은 문(σ): 값 × σ(문). 위치마다 채널에", () => ({ type: "glu", units: 16, gate: "sigmoid" })),
|
|
32
|
+
"glu:swiglu": P("dense", "SwiGLU", "GLU의 문을 SiLU로 (LLaMA 등 최신 LLM의 FFN)", () => ({ type: "glu", units: 16, gate: "silu" })),
|
|
33
|
+
conv: P("conv", "Conv1D", "짧은 구간의 모양(패턴)을 찾아 채널(filter)로 나눔", () => ({ type: "conv", channels: 16, kernel: 3, stride: 1 })),
|
|
34
|
+
"conv:causal": P("conv", "Causal Conv1D", "과거만 보는 Conv1D (왼쪽만 채움). WaveNet·TCN", () => ({ type: "conv", channels: 16, kernel: 3, stride: 1, padding: "causal" })),
|
|
35
|
+
conv2d: P("conv", "Conv2D", "작은 네모(3×3 등) 필터로 그림의 획·모서리 같은 모양을 찾음", () => ({ type: "conv2d", channels: 8, kernel: 3, stride: 1 })),
|
|
36
|
+
dw_conv: P("conv", "DepthwiseConv1D", "채널마다 따로 필터 하나 (채널은 안 섞음, 가벼움)", () => ({ type: "dw_conv", kernel: 3, multiplier: 1 })),
|
|
37
|
+
dw_conv2d: P("conv", "DepthwiseConv2D", "채널마다 따로 3×3 필터 (MobileNet)", () => ({ type: "dw_conv2d", kernel: 3, multiplier: 1 })),
|
|
38
|
+
sep_conv: P("conv", "SeparableConv1D", "Depthwise + 1칸 Conv: 같은 일을 훨씬 적은 가중치로", () => ({ type: "sep_conv", channels: 16, kernel: 3 })),
|
|
39
|
+
sep_conv2d: P("conv", "SeparableConv2D", "Depthwise 3×3 + 1×1 Conv (Xception·MobileNet)", () => ({ type: "sep_conv2d", channels: 16, kernel: 3 })),
|
|
40
|
+
pool: P("pool", "AvgPool1D", "이웃 값을 평균 내어 길이를 줄임", () => ({ type: "pool", size: 2 })),
|
|
41
|
+
max_pool: P("pool", "MaxPool1D", "이웃 중 가장 큰 값만 남겨 길이를 줄임 (가장 흔한 풀링)", () => ({ type: "max_pool", size: 2 })),
|
|
42
|
+
pool2d: P("pool", "AvgPool2D", "2×2 칸을 평균 내어 그림 크기를 절반으로", () => ({ type: "pool2d", size: 2 })),
|
|
43
|
+
max_pool2d: P("pool", "MaxPool2D", "2×2 칸 중 가장 큰 값만 남겨 그림을 절반으로", () => ({ type: "max_pool2d", size: 2 })),
|
|
44
|
+
global_avg: P("pool", "GlobalAvgPool1D", "채널마다 모든 시점을 평균 내어 값 하나로", () => ({ type: "global_avg" })),
|
|
45
|
+
global_max: P("pool", "GlobalMaxPool1D", "채널마다 모든 시점 중 가장 큰 값 (어딘가에 한 번이라도 나타났나)", () => ({ type: "global_max" })),
|
|
46
|
+
global_avg2d: P("pool", "GlobalAvgPool2D", "채널마다 그림 전체를 평균 내어 값 하나로", () => ({ type: "global_avg2d" })),
|
|
47
|
+
global_max2d: P("pool", "GlobalMaxPool2D", "채널마다 그림 전체에서 가장 큰 값", () => ({ type: "global_max2d" })),
|
|
48
|
+
adaptive_pool: P("pool", "AdaptivePool", "입력 크기와 상관없이 정한 크기(예: 4)로 줄임", () => ({ type: "adaptive_pool", size: 4, mode: "avg" })),
|
|
49
|
+
attn_pool: P("pool", "AttentionPool", "위치마다 점수를 배워 중요한 곳을 더 반영해 모음", () => ({ type: "attn_pool" })),
|
|
50
|
+
last_step: P("pool", "LastTimestep", "채널마다 마지막 시점 값만 (TCN식 분류)", () => ({ type: "last_step" })),
|
|
51
|
+
first_step: P("pool", "FirstTimestep", "채널마다 첫 시점 값만 (CLS token 꺼내기)", () => ({ type: "first_step" })),
|
|
52
|
+
upsample: P("up", "Upsample", "크기를 2배로 (가까운 값 복사 또는 선형 보간)", () => ({ type: "upsample", mode: "nearest" })),
|
|
53
|
+
pixel_shuffle: P("up", "PixelShuffle", "채널을 칸으로 옮겨 2배로 (채널은 2·4로 나뉨)", () => ({ type: "pixel_shuffle" })),
|
|
54
|
+
conv_t: P("up", "ConvTranspose1D", "배우면서 길이를 2배로 키우는 Conv", () => ({ type: "conv_t", channels: 16, kernel: 2, stride: 2 })),
|
|
55
|
+
conv_t2d: P("up", "ConvTranspose2D", "배우면서 그림을 2배로 키우는 Conv (U-Net·생성)", () => ({ type: "conv_t2d", channels: 8, kernel: 2, stride: 2 })),
|
|
56
|
+
simple_rnn: P("rnn", "SimpleRNN", "가장 단순한 순환 층", () => ({ type: "simple_rnn", hidden: 16, layers: 1 })),
|
|
57
|
+
lstm: P("rnn", "LSTM", "시점을 차례로 읽으며 기억", () => ({ type: "lstm", hidden: 16, layers: 1 })),
|
|
58
|
+
gru: P("rnn", "GRU", "LSTM보다 가벼운 순환 층", () => ({ type: "gru", hidden: 16, layers: 1 })),
|
|
59
|
+
"lstm:bi": P("rnn", "Bidirectional LSTM", "앞→뒤, 뒤→앞으로 둘 다 읽어 이어 붙임", () => ({ type: "lstm", hidden: 16, layers: 1, bidirectional: true })),
|
|
60
|
+
"gru:bi": P("rnn", "Bidirectional GRU", "앞→뒤, 뒤→앞으로 둘 다 읽는 GRU", () => ({ type: "gru", hidden: 16, layers: 1, bidirectional: true })),
|
|
61
|
+
se: P("attn", "SE block", "채널마다 중요도를 배워 곱함 (Squeeze-and-Excitation)", () => ({ type: "se", ratio: 4, act: "relu" })),
|
|
62
|
+
eca: P("attn", "ECA", "SE의 가벼운 판: 이웃 채널끼리만 봄", () => ({ type: "eca", kernel: 3 })),
|
|
63
|
+
cbam: P("attn", "CBAM", "무엇을(채널) + 어디를(공간) 볼지 차례로", () => ({ type: "cbam", ratio: 4, kernel: 7 })),
|
|
64
|
+
spatial_attn: P("attn", "SpatialAttention", "어디를 볼지 지도를 만들어 곱함", () => ({ type: "spatial_attn", kernel: 7 })),
|
|
65
|
+
mha: P("attn", "MultiHeadAttention", "위치끼리 서로 보고 섞음 (Self-attention, Transformer의 핵심)", () => ({ type: "mha", heads: 2 })),
|
|
66
|
+
"mha:causal": P("attn", "Causal Attention", "앞 위치만 봄 (GPT식, 다음 값 맞히기)", () => ({ type: "mha", heads: 2, causal: true })),
|
|
67
|
+
cross_attn: P("attn", "CrossAttention", "입력 둘: 첫째(질문)가 둘째(참고)의 어디를 볼지", () => ({ type: "cross_attn", heads: 2 })),
|
|
68
|
+
linear_attn: P("attn", "LinearAttention", "길이에 비례하는 가벼운 어텐션 (긴 입력용)", () => ({ type: "linear_attn", heads: 2 })),
|
|
69
|
+
nonlocal: P("attn", "NonLocal", "먼 칸끼리의 관계를 모아 더함 (이미지 어텐션)", () => ({ type: "nonlocal" })),
|
|
70
|
+
pos_enc: P("tf", "PositionalEncoding", "위치마다 sin·cos 값을 더해 순서를 알려 줌", () => ({ type: "pos_enc", mode: "sin" })),
|
|
71
|
+
"pos_enc:learned": P("tf", "PositionalEncoding (학습형)", "위치마다 더할 값을 배움 (BERT·ViT)", () => ({ type: "pos_enc", mode: "learned" })),
|
|
72
|
+
"mha:rope": P("tf", "Attention + RoPE", "Q·K를 위치만큼 돌려 순서를 앎 (최신 LLM)", () => ({ type: "mha", heads: 2, pos: "rope" })),
|
|
73
|
+
patch_embed: P("tf", "PatchEmbedding", "그림을 조각으로 잘라 조각마다 벡터로 (ViT 입구)", () => ({ type: "patch_embed", patch: 4, dim: 32 })),
|
|
74
|
+
cls_token: P("tf", "CLS token", "줄 맨 앞에 전체를 대표할 자리 하나를 붙임", () => ({ type: "cls_token" })),
|
|
75
|
+
bn: P("norm", "BatchNorm", "묶음(배치) 기준으로 값 크기를 고르게", () => ({ type: "bn" })),
|
|
76
|
+
layernorm: P("norm", "LayerNorm", "위치마다 채널 값을 고르게 (Transformer)", () => ({ type: "layernorm" })),
|
|
77
|
+
rmsnorm: P("norm", "RMSNorm", "LayerNorm의 가벼운 판 (최신 LLM)", () => ({ type: "rmsnorm" })),
|
|
78
|
+
groupnorm: P("norm", "GroupNorm", "채널을 묶어서 (배치가 작을 때)", () => ({ type: "groupnorm", groups: 4 })),
|
|
79
|
+
instancenorm: P("norm", "InstanceNorm", "샘플·채널마다 따로 (스타일 변환)", () => ({ type: "instancenorm" })),
|
|
80
|
+
dropout: P("reg", "Dropout", "학습 때 일부 값을 무작위로 꺼서 과적합을 막음", () => ({ type: "dropout", rate: 0.2 })),
|
|
81
|
+
spatial_dropout: P("reg", "SpatialDropout", "채널을 통째로 끔 (Conv 뒤에)", () => ({ type: "spatial_dropout", rate: 0.1 })),
|
|
82
|
+
drop_path: P("reg", "DropPath", "Residual 길을 통째로 건너뜀 (Residual 안 마지막에)", () => ({ type: "drop_path", rate: 0.1 })),
|
|
83
|
+
gaussian_noise: P("reg", "GaussianNoise", "학습 때 작은 잡음을 더함", () => ({ type: "gaussian_noise", std: 0.1 })),
|
|
84
|
+
add: P("merge", "Add (+)", "들어온 값들을 더함 (모양이 같아야 함). 잔차", () => ({ type: "add" })),
|
|
85
|
+
concat: P("merge", "Concat", "채널 방향으로 이어 붙임 (길이가 같아야 함)", () => ({ type: "concat" })),
|
|
86
|
+
multiply: P("merge", "Multiply (×)", "원소마다 곱함 (문·가중치 적용)", () => ({ type: "multiply" })),
|
|
87
|
+
average: P("merge", "Average", "원소마다 평균", () => ({ type: "average" })),
|
|
88
|
+
maximum: P("merge", "Maximum", "원소마다 큰 값", () => ({ type: "maximum" })),
|
|
89
|
+
slice: P("merge", "Split (채널 나누기)", "채널 일부만 꺼냄 (둘 이상 꺼내면 나누기)", () => ({ type: "slice", start: 0, count: 4 })),
|
|
90
|
+
flatten: P("shape", "Flatten", "한 줄로 펼침 (Dense 앞)", () => ({ type: "flatten" })),
|
|
91
|
+
reshape: P("shape", "Reshape", "값 전체를 (채널, 나머지) 줄로 다시 묶음", () => ({ type: "reshape", channels: 4 })),
|
|
92
|
+
permute: P("shape", "Transpose", "줄은 채널↔길이, 그림은 높이↔너비", () => ({ type: "permute" })),
|
|
93
|
+
to_seq: P("shape", "ToSequence", "그림 (C, H, W) → 칸들의 줄 (C, H·W). 어텐션 앞", () => ({ type: "to_seq" })),
|
|
94
|
+
crop: P("shape", "Crop", "가장자리를 잘라냄", () => ({ type: "crop", amount: 1 })),
|
|
95
|
+
pad: P("shape", "Pad", "가장자리에 0을 채움", () => ({ type: "pad", amount: 1 })),
|
|
96
|
+
embedding: P("prep", "Embedding", "값을 구간 번호로 바꿔 번호마다 벡터로 (시계열 토큰)", () => ({ type: "embedding", bins: 32, dim: 16 })),
|
|
97
|
+
spectrogram: P("prep", "Spectrogram", "신호 → 시간·주파수 그림 (그 뒤엔 Conv2D)", () => ({ type: "spectrogram", n_fft: 32, hop: 8, mode: "log" })),
|
|
98
|
+
"spectrogram:mel": P("prep", "Mel spectrogram", "사람 귀처럼 낮은 소리를 촘촘히 (음성)", () => ({ type: "spectrogram", n_fft: 64, hop: 16, mode: "mel", n_mels: 16 })),
|
|
99
|
+
"spectrogram:mfcc": P("prep", "MFCC", "mel을 DCT로 줄인 음성 특징", () => ({ type: "spectrogram", n_fft: 64, hop: 16, mode: "mfcc", n_mels: 32, n_mfcc: 13 })),
|
|
100
|
+
rand_flip: P("aug", "RandomFlip", "학습 때 반씩 뒤집음 (그림 좌우·신호 시간)", () => ({ type: "rand_flip", dir: "h" })),
|
|
101
|
+
rand_rotate: P("aug", "RandomRotation", "학습 때 그림을 조금씩 돌림", () => ({ type: "rand_rotate", degrees: 15 })),
|
|
102
|
+
rand_shift: P("aug", "RandomShift", "학습 때 조금씩 옮김", () => ({ type: "rand_shift", frac: 0.1 })),
|
|
103
|
+
rand_zoom: P("aug", "RandomZoom", "학습 때 그림을 조금 확대·축소", () => ({ type: "rand_zoom", frac: 0.1 })),
|
|
104
|
+
rand_scale: P("aug", "RandomScale", "학습 때 값 전체 세기를 조금 바꿈", () => ({ type: "rand_scale", frac: 0.2 })),
|
|
105
|
+
rand_stretch: P("aug", "RandomStretch", "학습 때 신호를 시간 방향으로 늘이고 줄임", () => ({ type: "rand_stretch", frac: 0.1 })),
|
|
106
|
+
mnv3s: P("pre", "MobileNetV3-Small", "ImageNet으로 미리 학습한 특징 1024개 (가중치 고정)", () => ({ type: "mnv3s", size: 128 })),
|
|
107
|
+
};
|
|
108
|
+
const ACT_LIST = [["relu", "ReLU", "음수는 0 (가장 많이 씀)"], ["leaky_relu", "LeakyReLU", "음수도 조금 흘림"], ["gelu", "GELU", "부드러운 ReLU (Transformer)"],
|
|
109
|
+
["silu", "SiLU / Swish", "x·σ(x) (EfficientNet·LLM)"], ["elu", "ELU", "음수 쪽이 부드럽게 −1로"], ["mish", "Mish", "부드럽고 매끈한 ReLU 계열"],
|
|
110
|
+
["softplus", "Softplus", "부드러운 ReLU"], ["sigmoid", "Sigmoid", "0~1로"], ["tanh", "Tanh", "−1~1로"], ["selu", "SELU", "스스로 정규화되는 활성화"],
|
|
111
|
+
["relu6", "ReLU6", "0~6으로 자름 (모바일)"], ["hardswish", "HardSwish", "가벼운 Swish (MobileNetV3)"], ["hardsigmoid", "HardSigmoid", "가벼운 Sigmoid"],
|
|
112
|
+
["softmax", "Softmax", "채널 합이 1이 되게"]];
|
|
113
|
+
for (const [k, name, desc] of ACT_LIST) PALETTE[k === "relu" ? "act" : `act:${k}`] = P("act", name, desc, () => ({ type: "act", kind: k }));
|
|
114
|
+
PALETTE.prelu = P("act", "PReLU", "LeakyReLU의 기울기를 배움", () => ({ type: "prelu" }));
|
|
115
|
+
// 종류마다 이름·설명·색 (palette 대표 항목에서)
|
|
116
|
+
const TYPES = {};
|
|
117
|
+
for (const [id, p] of Object.entries(PALETTE)) {
|
|
118
|
+
const t = p.make().type;
|
|
119
|
+
if (!TYPES[t] || !id.includes(":")) TYPES[t] = { cat: p.cat, name: t === "act" ? "Activation" : p.name, desc: p.desc, make: p.make };
|
|
120
|
+
}
|
|
121
|
+
TYPES.act.desc = "꺾임(비선형)을 넣음. 잘 모르겠으면 ReLU";
|
|
122
|
+
TYPES.output = { cat: "out", name: "Output", desc: "classification이면 클래스 수, regression이면 값 하나", make: () => ({ type: "output" }) };
|
|
123
|
+
const ACT_NAMES = Object.fromEntries(ACT_LIST.map(([k, n]) => [k, n]));
|
|
124
|
+
const RNN_NAMES = { lstm: "LSTM", gru: "GRU", simple_rnn: "SimpleRNN" };
|
|
125
|
+
const ACTS = ACT_LIST.map(([k, n]) => [k, k === "relu" ? "ReLU (추천)" : n]);
|
|
126
|
+
const MERGES = ["add", "concat", "multiply", "average", "maximum"];
|
|
127
|
+
const TWO_INPUT = ["cross_attn"];
|
|
128
|
+
// 왼쪽 목록은 데이터 종류와 상관없이 모든 층 (직접 꾸밀 수 있게). 잘 안 맞는 층은 흐리게 한마디와 함께
|
|
129
|
+
const TABULAR_OK = ["dense", "glu", "act", "prelu", "bn", "layernorm", "rmsnorm", "dropout", "gaussian_noise", "drop_path", "add", "concat",
|
|
130
|
+
"multiply", "average", "maximum", "slice", "flatten", "output"];
|
|
131
|
+
function fitNote(t, input) {
|
|
132
|
+
const r = RANK[t];
|
|
133
|
+
if (input === "image") return r === "seq" && !["conv", "pool", "global_avg"].includes(t) ? "줄(시계열) 모양에 씀 — 그림은 ToSequence 뒤에" : "";
|
|
134
|
+
if (r === "img") return t === "conv2d" || t === "pool2d" || t === "global_avg2d" ? "이미지 데이터용 (Spectrogram 뒤에는 됨)" : "그림 모양에 씀 (Spectrogram 뒤에는 됨)";
|
|
135
|
+
if (input === "tabular" && !TABULAR_OK.includes(t)) return "표 데이터엔 보통 안 씀 (열 순서를 시간처럼 다룸)";
|
|
136
|
+
return "";
|
|
137
|
+
}
|
|
138
|
+
function paletteTypes(input) {
|
|
139
|
+
const all = Object.keys(PALETTE);
|
|
140
|
+
const typeOf = (id) => PALETTE[id].make().type;
|
|
141
|
+
return all.filter((t) => !fitNote(typeOf(t), input)).concat(all.filter((t) => fitNote(typeOf(t), input)));
|
|
142
|
+
}
|
|
143
|
+
function chooseHeads(C) { return [4, 2, 1].find((h) => C % h === 0 && C / h >= 2) || 1; }
|
|
144
|
+
|
|
145
|
+
// ---- 묶음: 자주 쓰는 조합. make(C, rank, L) → {nodes (입력은 "SRC"), exit}. C: 넣는 자리로 들어오는 채널 수
|
|
146
|
+
const PACKS = [
|
|
147
|
+
{ id: "conv_bn_relu", name: "Conv–BN–ReLU", desc: "가장 흔한 합성곱 단위", ranks: [2, 3] },
|
|
148
|
+
{ id: "resnet", name: "ResNet 기본 블록", desc: "Conv–BN–ReLU–Conv–BN + 지름길 더하기 → ReLU", ranks: [2, 3] },
|
|
149
|
+
{ id: "bottleneck", name: "ResNet Bottleneck", desc: "1×1로 줄였다가 3×3 → 1×1로 되돌리고 지름길 (깊은 ResNet)", ranks: [2, 3] },
|
|
150
|
+
{ id: "se_resnet", name: "SE-ResNet 블록", desc: "ResNet 블록 + SE 채널 주의", ranks: [2, 3] },
|
|
151
|
+
{ id: "inception", name: "Inception 모듈", desc: "1·3·5칸 필터를 나란히 써서 이어 붙임", ranks: [2, 3] },
|
|
152
|
+
{ id: "mobilenet", name: "MobileNet Inverted Residual", desc: "1×1로 4배 넓힘 → Depthwise → 1×1로 좁힘 + 지름길", ranks: [2, 3] },
|
|
153
|
+
{ id: "dense", name: "DenseNet Dense 블록", desc: "층마다 앞의 모든 값을 이어 붙여 다음으로", ranks: [2, 3] },
|
|
154
|
+
{ id: "convnext", name: "ConvNeXt 블록", desc: "Depthwise 7칸 → LayerNorm → 4배 넓힘·GELU → 되돌림 + DropPath 지름길", ranks: [2, 3] },
|
|
155
|
+
{ id: "unet", name: "U-Net 한 단계", desc: "줄였다가(MaxPool) 키우고(Upsample) 같은 크기의 앞 값과 이어 붙임", ranks: [2, 3] },
|
|
156
|
+
{ id: "transformer", name: "Transformer Encoder 블록", desc: "LayerNorm → Self-attention + 지름길 → LayerNorm → FFN(4배·GELU) + 지름길", ranks: [2, 3] },
|
|
157
|
+
{ id: "decoder", name: "Transformer Decoder 블록 (GPT식)", desc: "Encoder 블록과 같고 어텐션이 앞만 봄 (Causal)", ranks: [2] },
|
|
158
|
+
{ id: "mixer", name: "MLP-Mixer 블록", desc: "위치끼리 섞는 MLP(Transpose) + 채널끼리 섞는 MLP, 각각 지름길", ranks: [2] },
|
|
159
|
+
{ id: "vit", name: "ViT 입구", desc: "PatchEmbedding → CLS token → PositionalEncoding(학습형)", ranks: [3] },
|
|
160
|
+
{ id: "mlp", name: "MLP 블록 (Residual)", desc: "LayerNorm → 넓힘·GELU → 되돌림 + 지름길", ranks: [1, 2, 3] },
|
|
161
|
+
];
|
|
162
|
+
function makePack(id, C, rank, L) {
|
|
163
|
+
const nodes = [];
|
|
164
|
+
let n = 0;
|
|
165
|
+
const two = rank === 3;
|
|
166
|
+
const add = (type, ins, kw = {}) => {
|
|
167
|
+
const t = two && TWO_D[type] ? TWO_D[type] : type, node = Object.assign({ id: `p${++n}`, type: t, inputs: ins }, kw);
|
|
168
|
+
if (t === "conv2d" && node.kernel > 7) node.kernel = 7;
|
|
169
|
+
if (rank === 1 && (type === "conv")) { node.type = "dense"; node.units = node.channels; delete node.channels; delete node.kernel; delete node.stride; }
|
|
170
|
+
nodes.push(node);
|
|
171
|
+
return node.id;
|
|
172
|
+
};
|
|
173
|
+
const conv = (ins, ch, k = 3) => add("conv", [ins], { channels: Math.max(1, ch), kernel: k, stride: 1 });
|
|
174
|
+
const c = Math.max(4, C || 16), q = Math.max(1, Math.round(c / 4));
|
|
175
|
+
let x = "SRC", s;
|
|
176
|
+
switch (id) {
|
|
177
|
+
case "conv_bn_relu": x = add("act", [add("bn", [conv("SRC", c)])], { kind: "relu" }); break;
|
|
178
|
+
case "resnet": case "se_resnet": {
|
|
179
|
+
s = add("bn", [conv(add("act", [add("bn", [conv("SRC", c)])], { kind: "relu" }), c)]);
|
|
180
|
+
if (id === "se_resnet") s = add("se", [s], { ratio: Math.min(4, c), act: "relu" });
|
|
181
|
+
x = add("act", [add("add", ["SRC", s])], { kind: "relu" });
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
case "bottleneck": {
|
|
185
|
+
s = add("act", [add("bn", [conv("SRC", q, 1)])], { kind: "relu" });
|
|
186
|
+
s = add("act", [add("bn", [conv(s, q, 3)])], { kind: "relu" });
|
|
187
|
+
s = add("bn", [conv(s, c, 1)]);
|
|
188
|
+
x = add("act", [add("add", ["SRC", s])], { kind: "relu" });
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case "inception": {
|
|
192
|
+
const b = Math.max(4, Math.round(c / 2));
|
|
193
|
+
const b1 = conv("SRC", b, 1), b2 = conv(conv("SRC", b, 1), b, 3), b3 = conv(conv("SRC", b, 1), b, 5);
|
|
194
|
+
x = add("act", [add("concat", [b1, b2, b3])], { kind: "relu" });
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
case "mobilenet": {
|
|
198
|
+
s = add("act", [add("bn", [conv("SRC", c * 4, 1)])], { kind: "relu6" });
|
|
199
|
+
s = add("act", [add("bn", [add("dw_conv", [s], { kernel: 3, multiplier: 1 })])], { kind: "relu6" });
|
|
200
|
+
s = add("bn", [conv(s, c, 1)]);
|
|
201
|
+
x = add("add", ["SRC", s]);
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "dense": {
|
|
205
|
+
const g = 8;
|
|
206
|
+
const l1 = conv(add("act", [add("bn", ["SRC"])], { kind: "relu" }), g, 3), c1 = add("concat", ["SRC", l1]);
|
|
207
|
+
const l2 = conv(add("act", [add("bn", [c1])], { kind: "relu" }), g, 3);
|
|
208
|
+
x = add("concat", [c1, l2]);
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "convnext": {
|
|
212
|
+
s = add("layernorm", [add("dw_conv", ["SRC"], { kernel: 7, multiplier: 1 })]);
|
|
213
|
+
s = add("drop_path", [conv(add("act", [conv(s, c * 4, 1)], { kind: "gelu" }), c, 1)], { rate: 0.1 });
|
|
214
|
+
x = add("add", ["SRC", s]);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "unet": {
|
|
218
|
+
const skip = add("act", [conv("SRC", c)], { kind: "relu" });
|
|
219
|
+
const mid = add("act", [conv(add("max_pool", [skip], { size: 2 }), c * 2)], { kind: "relu" });
|
|
220
|
+
x = add("act", [conv(add("concat", [add("upsample", [mid], { mode: "nearest" }), skip]), c)], { kind: "relu" });
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
case "transformer": case "decoder": {
|
|
224
|
+
const kw = { heads: chooseHeads(c) };
|
|
225
|
+
if (id === "decoder") kw.causal = true;
|
|
226
|
+
const a = add("add", ["SRC", add("mha", [add("layernorm", ["SRC"])], kw)]);
|
|
227
|
+
const f = conv(add("act", [conv(add("layernorm", [a]), c * 4, 1)], { kind: "gelu" }), c, 1);
|
|
228
|
+
x = add("add", [a, f]);
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
case "mixer": {
|
|
232
|
+
const tok = Math.max(2, L || 16);
|
|
233
|
+
const t1 = add("permute", [add("layernorm", ["SRC"])]);
|
|
234
|
+
const t2 = add("permute", [conv(add("act", [conv(t1, tok * 2, 1)], { kind: "gelu" }), tok, 1)]);
|
|
235
|
+
const a = add("add", ["SRC", t2]);
|
|
236
|
+
const f = conv(add("act", [conv(add("layernorm", [a]), c * 4, 1)], { kind: "gelu" }), c, 1);
|
|
237
|
+
x = add("add", [a, f]);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
case "vit": x = add("pos_enc", [add("cls_token", [add("patch_embed", ["SRC"], { patch: 4, dim: 32 })])], { mode: "learned" }); break;
|
|
241
|
+
case "mlp": {
|
|
242
|
+
const f = conv(add("act", [conv(add("layernorm", ["SRC"]), c * 2, 1)], { kind: "gelu" }), c, 1);
|
|
243
|
+
x = add("add", ["SRC", f]);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
default: throw new Error("모르는 묶음: " + id);
|
|
247
|
+
}
|
|
248
|
+
return { nodes, exit: x };
|
|
249
|
+
}
|
|
250
|
+
// 묶음을 선 from → to 사이에 넣음 → {graph, ids}
|
|
251
|
+
function insertPack(g0, from, to, pack) {
|
|
252
|
+
const g = clone(g0), f = findNode(g, to);
|
|
253
|
+
if (!f || !insOf(f.node).includes(from)) return { graph: g0, ids: [] };
|
|
254
|
+
const mk = idMaker(g), map = {};
|
|
255
|
+
for (const n of pack.nodes) map[n.id] = mk();
|
|
256
|
+
const added = pack.nodes.map((n) => Object.assign({}, n, { id: map[n.id], inputs: n.inputs.map((x) => (x === "SRC" ? from : map[x])) }));
|
|
257
|
+
replaceInput(f.node, from, map[pack.exit]);
|
|
258
|
+
f.list.splice(f.list.indexOf(f.node), 0, ...added);
|
|
259
|
+
return { graph: g, ids: added.map((n) => n.id) };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ------------------------------------------------------------ 순수 함수: 그래프 다루기
|
|
263
|
+
const clone = (x) => JSON.parse(JSON.stringify(x));
|
|
264
|
+
const isMerge = (n) => n && MERGES.includes(n.type);
|
|
265
|
+
const maxIns = (n) => (isMerge(n) ? Infinity : n && TWO_INPUT.includes(n.type) ? 2 : 1); // 받을 수 있는 입력 수
|
|
266
|
+
const insOf = (n) => (n && Array.isArray(n.inputs) ? n.inputs : []);
|
|
267
|
+
const nodesOf = (g) => (g && Array.isArray(g.nodes) ? g.nodes : []);
|
|
268
|
+
|
|
269
|
+
function eachNode(nodes, fn, parent = null) { // 모든 노드 (묶음 안 포함): fn(node, list, parent)
|
|
270
|
+
for (const n of nodes || []) {
|
|
271
|
+
if (!n || typeof n !== "object") continue;
|
|
272
|
+
fn(n, nodes, parent);
|
|
273
|
+
if (n.body && Array.isArray(n.body.nodes)) eachNode(n.body.nodes, fn, n);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function findNode(g, id) { // → {node, list, parent} 또는 null
|
|
277
|
+
let f = null;
|
|
278
|
+
eachNode(nodesOf(g), (n, list, parent) => { if (n.id === id && !f) f = { node: n, list, parent }; });
|
|
279
|
+
return f;
|
|
280
|
+
}
|
|
281
|
+
const entryOf = (parent) => (parent ? "in" : "input");
|
|
282
|
+
function newId(g) {
|
|
283
|
+
let m = 0;
|
|
284
|
+
eachNode(nodesOf(g), (n) => { const r = /^n(\d+)$/.exec(n.id || ""); if (r) m = Math.max(m, Number(r[1])); });
|
|
285
|
+
return "n" + (m + 1);
|
|
286
|
+
}
|
|
287
|
+
function idMaker(g) { // 같은 편집 안에서 여러 개 만들 때
|
|
288
|
+
let m = Number(newId(g).slice(1));
|
|
289
|
+
return () => "n" + m++;
|
|
290
|
+
}
|
|
291
|
+
const users = (list, id) => list.filter((n) => insOf(n).includes(id));
|
|
292
|
+
function ancestors(list, id) { // id에 닿는(위쪽) 모든 노드 id
|
|
293
|
+
const map = new Map(list.map((n) => [n.id, n])), out = new Set(), stack = [id];
|
|
294
|
+
while (stack.length) {
|
|
295
|
+
const k = stack.pop();
|
|
296
|
+
if (out.has(k)) continue;
|
|
297
|
+
out.add(k);
|
|
298
|
+
const n = map.get(k);
|
|
299
|
+
if (n) stack.push(...insOf(n));
|
|
300
|
+
}
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
function replaceInput(n, from, to) { // n.inputs에서 from을 to로 (첫 번째만)
|
|
304
|
+
const i = insOf(n).indexOf(from);
|
|
305
|
+
if (i >= 0) n.inputs[i] = to;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function insertOnEdge(g0, from, to, node) {
|
|
309
|
+
const g = clone(g0), f = findNode(g, to);
|
|
310
|
+
if (!f || !insOf(f.node).includes(from)) return g;
|
|
311
|
+
const n = Object.assign({}, node, { id: node.id || newId(g), inputs: [from] });
|
|
312
|
+
replaceInput(f.node, from, n.id);
|
|
313
|
+
f.list.splice(f.list.indexOf(f.node), 0, n);
|
|
314
|
+
return g;
|
|
315
|
+
}
|
|
316
|
+
function connect(g0, from, to) { // → {graph, error}
|
|
317
|
+
const g = clone(g0), f = findNode(g, to);
|
|
318
|
+
if (!f || f.node.type === "input") return { graph: g0, error: "Input에는 선을 이을 수 없습니다" };
|
|
319
|
+
const entry = entryOf(f.parent);
|
|
320
|
+
if (from !== entry && !f.list.some((n) => n.id === from)) return { graph: g0, error: "같은 묶음 안의 층끼리만 이을 수 있습니다" };
|
|
321
|
+
const src = f.list.find((n) => n.id === from);
|
|
322
|
+
if (src && src.type === "output") return { graph: g0, error: "출력 뒤에는 이을 수 없습니다" };
|
|
323
|
+
if (from === to || ancestors(f.list, from).has(to)) return { graph: g0, error: "아래에서 위로는 이을 수 없습니다 (선이 돌게 됨)" };
|
|
324
|
+
const t = f.node;
|
|
325
|
+
if (insOf(t).includes(from)) return { graph: g0, error: "이미 이어져 있습니다" };
|
|
326
|
+
if (isMerge(t)) t.inputs = insOf(t).concat([from]); // 합치기 상자: 입력을 더함
|
|
327
|
+
else if (maxIns(t) === 2) t.inputs = insOf(t).length < 2 ? insOf(t).concat([from]) : [insOf(t)[0], from]; // 입력 둘: 둘째를 바꿈
|
|
328
|
+
else t.inputs = [from]; // 보통 층: 기존 선을 새 선으로 바꿈
|
|
329
|
+
return { graph: g, error: null };
|
|
330
|
+
}
|
|
331
|
+
// 출력 동그라미를 빈 곳에 놓고 층을 고름: from 뒤에 이어진 새 층 → {graph, id}
|
|
332
|
+
function addConnected(g0, from, node, pos) {
|
|
333
|
+
const g = clone(g0), f = findNode(g, from);
|
|
334
|
+
if (!f) return { graph: g0, id: null };
|
|
335
|
+
const n = Object.assign({}, node, { id: newId(g), inputs: [from] });
|
|
336
|
+
if (validPos(pos)) n.pos = [Math.max(0, Math.round(pos[0])), Math.max(0, Math.round(pos[1]))];
|
|
337
|
+
f.list.splice(f.list.indexOf(f.node) + 1, 0, n);
|
|
338
|
+
return { graph: g, id: n.id };
|
|
339
|
+
}
|
|
340
|
+
// 입력 동그라미를 빈 곳에 놓고 층을 고름: to 앞을 채우는 새 층 (합치기 상자면 입력을 더함) → {graph, id}
|
|
341
|
+
function addFeeding(g0, to, node, pos) {
|
|
342
|
+
const g = clone(g0), f = findNode(g, to);
|
|
343
|
+
if (!f || f.node.type === "input") return { graph: g0, id: null };
|
|
344
|
+
const n = Object.assign({}, node, { id: newId(g), inputs: [] });
|
|
345
|
+
if (validPos(pos)) n.pos = [Math.max(0, Math.round(pos[0])), Math.max(0, Math.round(pos[1]))];
|
|
346
|
+
f.list.splice(f.list.indexOf(f.node), 0, n);
|
|
347
|
+
f.node.inputs = isMerge(f.node) || (maxIns(f.node) === 2 && insOf(f.node).length < 2) ? insOf(f.node).concat([n.id]) : [n.id];
|
|
348
|
+
return { graph: g, id: n.id };
|
|
349
|
+
}
|
|
350
|
+
function removeNode(g0, id) {
|
|
351
|
+
const f0 = findNode(g0, id);
|
|
352
|
+
if (!f0 || f0.node.type === "input" || f0.node.type === "output") return g0;
|
|
353
|
+
const g = clone(g0), f = findNode(g, id), rep = insOf(f.node)[0];
|
|
354
|
+
for (const u of users(f.list, id)) {
|
|
355
|
+
u.inputs = u.inputs.map((x) => (x === id ? rep : x)).filter((x) => x !== undefined);
|
|
356
|
+
if (isMerge(u)) u.inputs = u.inputs.filter((x, i) => u.inputs.indexOf(x) === i);
|
|
357
|
+
}
|
|
358
|
+
f.list.splice(f.list.indexOf(f.node), 1);
|
|
359
|
+
return g;
|
|
360
|
+
}
|
|
361
|
+
function removeEdge(g0, from, to) {
|
|
362
|
+
const g = clone(g0), f = findNode(g, to);
|
|
363
|
+
if (!f) return g0;
|
|
364
|
+
const i = insOf(f.node).indexOf(from);
|
|
365
|
+
if (i >= 0) f.node.inputs.splice(i, 1);
|
|
366
|
+
return g;
|
|
367
|
+
}
|
|
368
|
+
// 고른 노드들이 "입구 하나·출구 하나"인 묶음인지: → {source, exit, list} 또는 null
|
|
369
|
+
function singleEntryExit(g, ids) {
|
|
370
|
+
if (!ids || !ids.length) return null;
|
|
371
|
+
const fs = ids.map((id) => findNode(g, id));
|
|
372
|
+
if (fs.some((f) => !f || f.node.type === "input" || f.node.type === "output") || fs.some((f) => f.list !== fs[0].list)) return null;
|
|
373
|
+
const list = fs[0].list, sel = new Set(ids);
|
|
374
|
+
const ext = new Set();
|
|
375
|
+
for (const f of fs) for (const x of insOf(f.node)) if (!sel.has(x)) ext.add(x);
|
|
376
|
+
if (ext.size !== 1) return null;
|
|
377
|
+
const outside = list.filter((n) => !sel.has(n.id));
|
|
378
|
+
const exits = ids.filter((id) => outside.some((n) => insOf(n).includes(id)));
|
|
379
|
+
const sinks = ids.filter((id) => !list.some((n) => sel.has(n.id) && insOf(n).includes(id)));
|
|
380
|
+
if (exits.length > 1 || sinks.length !== 1 || (exits.length === 1 && exits[0] !== sinks[0])) return null;
|
|
381
|
+
return { source: [...ext][0], exit: sinks[0], list, parent: fs[0].parent };
|
|
382
|
+
}
|
|
383
|
+
function wrapResidual(g0, ids, shapes) {
|
|
384
|
+
const s0 = singleEntryExit(g0, ids);
|
|
385
|
+
if (!s0) return g0;
|
|
386
|
+
const g = clone(g0), s = singleEntryExit(g, ids), mk = idMaker(g);
|
|
387
|
+
const a = (shapes || {})[s.source], b = (shapes || {})[s.exit];
|
|
388
|
+
let sc = s.source;
|
|
389
|
+
const scNode = [];
|
|
390
|
+
if (a && b && JSON.stringify(a) !== JSON.stringify(b)) {
|
|
391
|
+
sc = mk();
|
|
392
|
+
if (b.length === 1) scNode.push({ id: sc, type: "dense", units: b[0], inputs: [s.source] });
|
|
393
|
+
else {
|
|
394
|
+
const ratio = a.length === b.length ? Math.round(a[1] / b[1]) : 1;
|
|
395
|
+
scNode.push({ id: sc, type: b.length === 3 ? "conv2d" : "conv", channels: b[0], kernel: 1, stride: ratio === 2 ? 2 : 1,
|
|
396
|
+
inputs: [s.source] });
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const add = { id: mk(), type: "add", inputs: [s.exit, sc] };
|
|
400
|
+
for (const u of s.list) if (!ids.includes(u.id)) if (u.inputs) u.inputs = u.inputs.map((x) => (x === s.exit ? add.id : x));
|
|
401
|
+
const at = s.list.findIndex((n) => n.id === s.exit) + 1;
|
|
402
|
+
s.list.splice(at, 0, ...scNode, add);
|
|
403
|
+
return g;
|
|
404
|
+
}
|
|
405
|
+
function wrapRepeat(g0, ids) {
|
|
406
|
+
if (!singleEntryExit(g0, ids)) return g0;
|
|
407
|
+
const g = clone(g0), s = singleEntryExit(g, ids), id = newId(g);
|
|
408
|
+
const body = s.list.filter((n) => ids.includes(n.id)).map((n) => Object.assign({}, n, { inputs: insOf(n).map((x) => (x === s.source ? "in" : x)) }));
|
|
409
|
+
const rep = { id, type: "repeat", times: 2, inputs: [s.source], body: { nodes: body } };
|
|
410
|
+
const at = s.list.findIndex((n) => ids.includes(n.id));
|
|
411
|
+
for (const u of s.list) if (!ids.includes(u.id)) if (u.inputs) u.inputs = u.inputs.map((x) => (x === s.exit ? id : x));
|
|
412
|
+
for (let i = s.list.length - 1; i >= 0; i--) if (ids.includes(s.list[i].id)) s.list.splice(i, 1);
|
|
413
|
+
s.list.splice(at, 0, rep);
|
|
414
|
+
return g;
|
|
415
|
+
}
|
|
416
|
+
function unwrap(g0, id) {
|
|
417
|
+
const f0 = findNode(g0, id);
|
|
418
|
+
if (!f0 || f0.node.type !== "repeat" || !f0.node.body) return g0;
|
|
419
|
+
const g = clone(g0), f = findNode(g, id), r = f.node, src = insOf(r)[0];
|
|
420
|
+
const body = r.body.nodes || [];
|
|
421
|
+
const sinks = body.filter((n) => !body.some((m) => insOf(m).includes(n.id)));
|
|
422
|
+
const exit = sinks.length ? sinks[sinks.length - 1].id : src;
|
|
423
|
+
const back = body.map((n) => Object.assign({}, n, { inputs: insOf(n).map((x) => (x === "in" ? src : x)) }));
|
|
424
|
+
for (const u of f.list) {
|
|
425
|
+
if (u === r || !u.inputs) continue;
|
|
426
|
+
u.inputs = u.inputs.map((x) => (x === id ? exit : x));
|
|
427
|
+
if (isMerge(u)) u.inputs = u.inputs.filter((x, i) => u.inputs.indexOf(x) === i); // 몸체가 비었으면 같은 값 둘이 됨
|
|
428
|
+
}
|
|
429
|
+
f.list.splice(f.list.indexOf(r), 1, ...back);
|
|
430
|
+
return g;
|
|
431
|
+
}
|
|
432
|
+
function splitBranch(g0, ids, newNode) { // 같은 입구에서 갈래 하나를 더 만들고 Concat
|
|
433
|
+
if (!singleEntryExit(g0, ids)) return g0;
|
|
434
|
+
const g = clone(g0), s = singleEntryExit(g, ids), mk = idMaker(g);
|
|
435
|
+
const lane = Object.assign({}, newNode, { id: mk(), inputs: [s.source] });
|
|
436
|
+
const cat = { id: mk(), type: "concat", inputs: [s.exit, lane.id] };
|
|
437
|
+
for (const u of s.list) if (!ids.includes(u.id)) if (u.inputs) u.inputs = u.inputs.map((x) => (x === s.exit ? cat.id : x));
|
|
438
|
+
s.list.splice(s.list.findIndex((n) => n.id === s.exit) + 1, 0, lane, cat);
|
|
439
|
+
return g;
|
|
440
|
+
}
|
|
441
|
+
function copyNodes(g, ids) {
|
|
442
|
+
const s = singleEntryExit(g, ids);
|
|
443
|
+
if (!s) return null;
|
|
444
|
+
return { nodes: clone(s.list.filter((n) => ids.includes(n.id))), source: s.source, exit: s.exit };
|
|
445
|
+
}
|
|
446
|
+
function pasteAfter(g0, id, clip) { // → {graph, ids}
|
|
447
|
+
const f0 = findNode(g0, id);
|
|
448
|
+
if (!clip || !f0 || f0.node.type === "output") return { graph: g0, ids: [] };
|
|
449
|
+
const g = clone(g0), f = findNode(g, id), mk = idMaker(g), map = {};
|
|
450
|
+
eachNode(clip.nodes, (n) => { map[n.id] = mk(); });
|
|
451
|
+
const remap = (nodes, top) => nodes.map((n) => {
|
|
452
|
+
const m = Object.assign({}, n, { id: map[n.id], inputs: insOf(n).map((x) => (top && x === clip.source ? id : map[x] || x)) });
|
|
453
|
+
if (n.body) m.body = { nodes: remap(n.body.nodes || [], false) };
|
|
454
|
+
return m;
|
|
455
|
+
});
|
|
456
|
+
const added = remap(clone(clip.nodes), true);
|
|
457
|
+
const exit = map[clip.exit];
|
|
458
|
+
for (const u of users(f.list, id)) replaceInput(u, id, exit);
|
|
459
|
+
f.list.splice(f.list.indexOf(f.node) + 1, 0, ...added);
|
|
460
|
+
return { graph: g, ids: added.map((n) => n.id) };
|
|
461
|
+
}
|
|
462
|
+
function setParam(g0, id, key, value) {
|
|
463
|
+
const g = clone(g0), f = findNode(g, id);
|
|
464
|
+
if (f) f.node[key] = value;
|
|
465
|
+
return g;
|
|
466
|
+
}
|
|
467
|
+
// ---- 화면 위치 (pos: 판 위 왼쪽 위 [x, y], 맨 위 층위 상자만. 모델에는 영향 없음)
|
|
468
|
+
const validPos = (p) => Array.isArray(p) && p.length === 2 && p.every((v) => typeof v === "number" && isFinite(v));
|
|
469
|
+
function setPos(g0, id, pos) {
|
|
470
|
+
const g = clone(g0), f = findNode(g, id);
|
|
471
|
+
if (f && validPos(pos)) f.node.pos = [Math.round(pos[0]), Math.round(pos[1])]; // 음수도 됨 (희망 위치, 배치가 판 안으로 맞춤)
|
|
472
|
+
return g;
|
|
473
|
+
}
|
|
474
|
+
function clearPos(g0) {
|
|
475
|
+
const g = clone(g0);
|
|
476
|
+
eachNode(nodesOf(g), (n) => { delete n.pos; delete n.shift; });
|
|
477
|
+
return g;
|
|
478
|
+
}
|
|
479
|
+
const stripPos = clearPos;
|
|
480
|
+
// 줄바꿈한 줄 옮기기 (보기용): 그 줄의 상자 하나에 흐름 방향 거리(px, 0 이상)를 적음. 0이면 줄 첫머리 그대로
|
|
481
|
+
function setShift(g0, id, v) {
|
|
482
|
+
const g = clone(g0), f = findNode(g, id);
|
|
483
|
+
if (!f) return g;
|
|
484
|
+
const s = Math.max(0, Math.round(Number(v) || 0));
|
|
485
|
+
if (s > 0) f.node.shift = s; else delete f.node.shift;
|
|
486
|
+
return g;
|
|
487
|
+
}
|
|
488
|
+
// 상자 크기 (보기용, 모델과 상관없음): 고른 상자들에 한 번에. null이면 기본 크기
|
|
489
|
+
const SIZE_MIN = [60, 24], SIZE_MAX = [600, 400];
|
|
490
|
+
function setSize(g0, ids, size) {
|
|
491
|
+
const g = clone(g0);
|
|
492
|
+
for (const id of ids) {
|
|
493
|
+
const f = findNode(g, id);
|
|
494
|
+
if (!f || f.node.type === "repeat") continue;
|
|
495
|
+
if (!size) { delete f.node.size; continue; }
|
|
496
|
+
f.node.size = [0, 1].map((k) => Math.max(SIZE_MIN[k], Math.min(SIZE_MAX[k], Math.round(Number(size[k]) || 0))));
|
|
497
|
+
}
|
|
498
|
+
return g;
|
|
499
|
+
}
|
|
500
|
+
function stripView(g0) { // 모델 비교용: 화면용 값(위치·크기·줄바꿈)을 뺀 그래프
|
|
501
|
+
const g = clone(g0);
|
|
502
|
+
eachNode(nodesOf(g), (n) => { delete n.pos; delete n.size; delete n.brk; delete n.shift; });
|
|
503
|
+
return g;
|
|
504
|
+
}
|
|
505
|
+
// 줄바꿈 (보기용): 이 상자부터 다음 줄(가로면 아래, 세로면 오른쪽)로
|
|
506
|
+
function setBreak(g0, id, on) {
|
|
507
|
+
const g = clone(g0), f = findNode(g, id);
|
|
508
|
+
if (f) { if (on) f.node.brk = true; else delete f.node.brk; }
|
|
509
|
+
return g;
|
|
510
|
+
}
|
|
511
|
+
function addFree(g0, node, pos) { // 선과 먼 곳에 놓음: 아직 안 이어진 상자
|
|
512
|
+
const g = clone(g0), n = Object.assign({}, node, { id: node.id || newId(g), inputs: [] });
|
|
513
|
+
if (validPos(pos)) n.pos = [Math.max(0, Math.round(pos[0])), Math.max(0, Math.round(pos[1]))];
|
|
514
|
+
g.nodes.push(n);
|
|
515
|
+
return g;
|
|
516
|
+
}
|
|
517
|
+
function moveNode(g0, id, from, to) { // 놓인 상자를 다른 선(from → to) 사이로 옮김 → {graph, error}
|
|
518
|
+
const f = findNode(g0, id), t = findNode(g0, to);
|
|
519
|
+
if (!f || f.node.type === "input" || f.node.type === "output") return { graph: g0, error: "Input·Output은 옮길 수 없습니다" };
|
|
520
|
+
if (from === id || to === id) return { graph: g0, error: "제자리입니다" };
|
|
521
|
+
if (!t || !insOf(t.node).includes(from)) return { graph: g0, error: "그 선을 찾을 수 없습니다" };
|
|
522
|
+
if (t.list !== f.list) return { graph: g0, error: "묶음 안팎으로는 옮길 수 없습니다 (같은 묶음 안의 선으로)" };
|
|
523
|
+
const node = clone(f.node);
|
|
524
|
+
delete node.pos;
|
|
525
|
+
return { graph: insertOnEdge(removeNode(g0, id), from, to, node), error: null };
|
|
526
|
+
}
|
|
527
|
+
const SHORT_ACT = { selu: "SELU", relu6: "ReLU6", hardswish: "HSwish", hardsigmoid: "HSigm", softmax: "Softmax",
|
|
528
|
+
relu: "ReLU", sigmoid: "Sigmoid", tanh: "Tanh", leaky_relu: "LReLU",
|
|
529
|
+
gelu: "GELU", silu: "SiLU", elu: "ELU", mish: "Mish", softplus: "Softplus" };
|
|
530
|
+
function shortLabel(b) { return b.type === "act" ? SHORT_ACT[b.kind] || "Act" : label(b); }
|
|
531
|
+
// 점 p에서 선분 a-b까지 거리
|
|
532
|
+
function segDist(p, a, b) {
|
|
533
|
+
const dx = b[0] - a[0], dy = b[1] - a[1], L2 = dx * dx + dy * dy;
|
|
534
|
+
const t = L2 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / L2)) : 0;
|
|
535
|
+
return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dy);
|
|
536
|
+
}
|
|
537
|
+
// 끌어 놓을 선: SNAP(48px) 안에서 가장 가까운 선. skip(e) → true면 빼기
|
|
538
|
+
function nearestEdge(edges, p, maxDist = SNAP, skip = null) {
|
|
539
|
+
let best = null;
|
|
540
|
+
for (const e of edges) {
|
|
541
|
+
if (e.kind === "exit" || (skip && skip(e))) continue;
|
|
542
|
+
for (let i = 0; i + 1 < e.points.length; i++) {
|
|
543
|
+
const d = segDist(p, e.points[i], e.points[i + 1]);
|
|
544
|
+
if (d <= maxDist && (!best || d < best.d)) best = { e, d };
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return best ? best.e : null;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// 되돌리기 기록: push(바꾸기 전 상태), undo(지금) → 이전 상태 또는 null, redo(지금) → 다음 상태 또는 null
|
|
551
|
+
function history(limit = 100) {
|
|
552
|
+
const back = [], fwd = [];
|
|
553
|
+
return {
|
|
554
|
+
push(state) { back.push(clone(state)); if (back.length > limit) back.shift(); fwd.length = 0; },
|
|
555
|
+
undo(cur) { if (!back.length) return null; fwd.push(clone(cur)); return back.pop(); },
|
|
556
|
+
redo(cur) { if (!fwd.length) return null; back.push(clone(cur)); return fwd.pop(); },
|
|
557
|
+
canUndo: () => back.length > 0, canRedo: () => fwd.length > 0,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function label(b, summary) {
|
|
562
|
+
switch (b.type) {
|
|
563
|
+
case "input": return "Input";
|
|
564
|
+
case "conv": return `Conv1D · ${b.channels} filters · k=${b.kernel}` + (b.stride === 2 ? " · stride 2" : "") +
|
|
565
|
+
((b.dilation || 1) > 1 ? ` · dilation ${b.dilation}` : "") + (b.padding === "valid" ? " · valid" : b.padding === "causal" ? " · causal" : "") +
|
|
566
|
+
((b.groups || 1) > 1 ? ` · groups ${b.groups}` : "");
|
|
567
|
+
case "last_step": return "LastTimestep";
|
|
568
|
+
case "se": return `SE block · ratio ${b.ratio}`;
|
|
569
|
+
case "dropout": return `Dropout · ${b.rate}`;
|
|
570
|
+
case "flatten": return "Flatten";
|
|
571
|
+
case "pool": return `AvgPool1D · pool ${b.size}`;
|
|
572
|
+
case "conv2d": return `Conv2D · ${b.channels} filters · ${b.kernel}×${b.kernel}` + (b.stride === 2 ? " · stride 2" : "") +
|
|
573
|
+
((b.dilation || 1) > 1 ? ` · dilation ${b.dilation}` : "") + ((b.groups || 1) > 1 ? ` · groups ${b.groups}` : "") +
|
|
574
|
+
(b.padding === "valid" ? " · valid" : "");
|
|
575
|
+
case "pool2d": return `AvgPool2D · pool ${b.size || 2}`;
|
|
576
|
+
case "global_avg2d": return "GlobalAvgPool2D";
|
|
577
|
+
case "global_avg": return "GlobalAvgPool1D";
|
|
578
|
+
case "bn": return "BatchNorm";
|
|
579
|
+
case "act": return ACT_NAMES[b.kind] || "Activation";
|
|
580
|
+
case "dense": return `Dense · ${b.units} units`;
|
|
581
|
+
case "add": return "Add (+)";
|
|
582
|
+
case "concat": return "Concat";
|
|
583
|
+
case "lstm": case "gru": case "simple_rnn": {
|
|
584
|
+
const n = b.layers || 1;
|
|
585
|
+
return `${b.bidirectional ? "Bi" : ""}${RNN_NAMES[b.type]} · ${b.hidden} units` + (n > 1 ? ` × ${n} layers` : "") +
|
|
586
|
+
(b.return_sequences ? " · 줄 그대로" : "");
|
|
587
|
+
}
|
|
588
|
+
case "repeat": return `Repeat × ${b.times}`;
|
|
589
|
+
case "prelu": return "PReLU";
|
|
590
|
+
case "output":
|
|
591
|
+
if (b.head === "distance") return "Output · 정상과의 거리";
|
|
592
|
+
if (!summary) return "Output";
|
|
593
|
+
if (summary.task === "multi" && summary.targets) return `Output · 정답 ${summary.targets.length}개 (${summary.targets.map((t) => t.name).join(" · ")})`.slice(0, 60);
|
|
594
|
+
if (summary.task !== "classification") return "Output · regression";
|
|
595
|
+
{
|
|
596
|
+
const names = summary.classes.join(" · "); // 클래스가 많으면 상자를 넘지 않게 개수만
|
|
597
|
+
return `Output · classification (${names.length > 24 ? `${summary.classes.length} classes` : names})`;
|
|
598
|
+
}
|
|
599
|
+
default: return LABELS[b.type] ? LABELS[b.type](b) : TYPES[b.type] ? TYPES[b.type].name : String(b.type);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
const LABELS = {
|
|
603
|
+
glu: (b) => `${b.gate === "silu" ? "SwiGLU" : "GLU"} · ${b.units} units`,
|
|
604
|
+
dw_conv: (b) => `DepthwiseConv1D · k=${b.kernel || 3}` + ((b.multiplier || 1) > 1 ? ` · ×${b.multiplier}` : ""),
|
|
605
|
+
dw_conv2d: (b) => `DepthwiseConv2D · ${b.kernel || 3}×${b.kernel || 3}` + ((b.multiplier || 1) > 1 ? ` · ×${b.multiplier}` : ""),
|
|
606
|
+
sep_conv: (b) => `SeparableConv1D · ${b.channels} filters · k=${b.kernel || 3}`,
|
|
607
|
+
sep_conv2d: (b) => `SeparableConv2D · ${b.channels} filters · ${b.kernel || 3}×${b.kernel || 3}`,
|
|
608
|
+
max_pool: (b) => `MaxPool1D · pool ${b.size || 2}`, max_pool2d: () => "MaxPool2D · pool 2",
|
|
609
|
+
adaptive_pool: (b) => `Adaptive${(b.mode || "avg") === "max" ? "Max" : "Avg"}Pool · ${b.size || 4}`,
|
|
610
|
+
upsample: (b) => `Upsample ×2 · ${(b.mode || "nearest") === "linear" ? "linear" : "nearest"}`,
|
|
611
|
+
conv_t: (b) => `ConvTranspose1D · ${b.channels} filters · k=${b.kernel || 2}` + ((b.stride || 2) === 2 ? " · ×2" : ""),
|
|
612
|
+
conv_t2d: (b) => `ConvTranspose2D · ${b.channels} filters · ${b.kernel || 2}×${b.kernel || 2}` + ((b.stride || 2) === 2 ? " · ×2" : ""),
|
|
613
|
+
eca: (b) => `ECA · k=${b.kernel || 3}`, cbam: (b) => `CBAM · ratio ${b.ratio || 4}`, spatial_attn: (b) => `SpatialAttention · k=${b.kernel || 7}`,
|
|
614
|
+
mha: (b) => `${b.causal ? "Causal " : ""}Attention · ${b.heads || 2} heads` + (b.pos === "rope" ? " · RoPE" : ""),
|
|
615
|
+
linear_attn: (b) => `LinearAttention · ${b.heads || 2} heads`, cross_attn: (b) => `CrossAttention · ${b.heads || 2} heads`,
|
|
616
|
+
pos_enc: (b) => `PositionalEncoding · ${(b.mode || "sin") === "learned" ? "학습형" : "sin·cos"}`,
|
|
617
|
+
patch_embed: (b) => `PatchEmbedding · ${b.patch || 4}×${b.patch || 4} → ${b.dim || 32}`,
|
|
618
|
+
groupnorm: (b) => `GroupNorm · ${b.groups || 4} groups`,
|
|
619
|
+
spatial_dropout: (b) => `SpatialDropout · ${b.rate ?? 0.1}`, drop_path: (b) => `DropPath · ${b.rate ?? 0.1}`,
|
|
620
|
+
gaussian_noise: (b) => `GaussianNoise · ${b.std ?? 0.1}`,
|
|
621
|
+
multiply: () => "Multiply (×)", average: () => "Average", maximum: () => "Maximum",
|
|
622
|
+
slice: (b) => `Split · 채널 ${b.start || 0}–${(b.start || 0) + b.count - 1}`,
|
|
623
|
+
reshape: (b) => `Reshape · ${b.channels} 채널`, crop: (b) => `Crop · ${b.amount || 1}`, pad: (b) => `Pad · ${b.amount || 1}`,
|
|
624
|
+
embedding: (b) => `Embedding · ${b.bins || 32} 구간 → ${b.dim || 16}`,
|
|
625
|
+
spectrogram: (b) => `${{ log: "Spectrogram", mel: "Mel spectrogram", mfcc: "MFCC" }[b.mode || "log"]} · n_fft ${b.n_fft || 32}`,
|
|
626
|
+
rand_flip: (b) => `RandomFlip · ${{ h: "좌우", v: "상하", hv: "좌우·상하" }[b.dir || "h"]}`,
|
|
627
|
+
rand_rotate: (b) => `RandomRotation · ±${b.degrees || 15}°`, rand_shift: (b) => `RandomShift · ±${b.frac ?? 0.1}`,
|
|
628
|
+
rand_zoom: (b) => `RandomZoom · ±${b.frac ?? 0.1}`, rand_scale: (b) => `RandomScale · ±${b.frac ?? 0.2}`,
|
|
629
|
+
rand_stretch: (b) => `RandomStretch · ±${b.frac ?? 0.1}`,
|
|
630
|
+
mnv3s: (b) => `MobileNetV3-Small · ${b.out === "map" ? "특징 지도" : `${b.size || 128}×${b.size || 128}`} · ${b.train_last ? `끝 ${b.train_last}블록 학습` : "고정"}`,
|
|
631
|
+
};
|
|
632
|
+
// 설정 칸: [종류, 키, 이름, …]
|
|
633
|
+
const num = (k, lab, min, max) => ["num", k, lab, min, max];
|
|
634
|
+
const sel = (k, lab, opts) => ["sel", k, lab, opts];
|
|
635
|
+
const yes = (k, lab) => ["sel", k, lab, [[false, "끔"], [true, "켬"]]];
|
|
636
|
+
const vals = (...xs) => xs.map((v) => [v, String(v)]);
|
|
637
|
+
const RATE = sel("rate", "Rate (끄는 비율)", vals(0.05, 0.1, 0.2, 0.3, 0.5));
|
|
638
|
+
const FRAC = sel("frac", "세기 (±비율)", vals(0.05, 0.1, 0.2, 0.3));
|
|
639
|
+
const HEADS = sel("heads", "Heads (나눠 보는 수, 채널의 약수)", vals(1, 2, 4, 8));
|
|
640
|
+
const FIELDS = {
|
|
641
|
+
glu: [num("units", "Units (출력 채널 수)", 1, 1024), sel("gate", "문(gate)", [["sigmoid", "Sigmoid (GLU)"], ["silu", "SiLU (SwiGLU)"]])],
|
|
642
|
+
dw_conv: [sel("kernel", "Kernel size", vals(1, 3, 5, 7, 9, 11, 15)), num("multiplier", "Multiplier (채널마다 필터 수)", 1, 8)],
|
|
643
|
+
dw_conv2d: [sel("kernel", "Kernel size", [1, 3, 5, 7].map((v) => [v, `${v}×${v}`])), num("multiplier", "Multiplier (채널마다 필터 수)", 1, 8)],
|
|
644
|
+
sep_conv: [num("channels", "Filters", 1, 256), sel("kernel", "Kernel size", vals(1, 3, 5, 7, 9, 11, 15))],
|
|
645
|
+
sep_conv2d: [num("channels", "Filters", 1, 256), sel("kernel", "Kernel size", [1, 3, 5, 7].map((v) => [v, `${v}×${v}`]))],
|
|
646
|
+
max_pool: [sel("size", "Pool size", vals(2, 4))],
|
|
647
|
+
adaptive_pool: [num("size", "출력 크기", 1, 64), sel("mode", "방법", [["avg", "평균 (Avg)"], ["max", "최댓값 (Max)"]])],
|
|
648
|
+
upsample: [sel("mode", "방법", [["nearest", "nearest (가까운 값 복사)"], ["linear", "linear (선형 보간)"]])],
|
|
649
|
+
conv_t: [num("channels", "Filters", 1, 256), sel("kernel", "Kernel size", vals(1, 2, 3, 4, 5)), sel("stride", "Stride", [[2, "2 (길이 2배)"], [1, "1 (그대로)"]])],
|
|
650
|
+
conv_t2d: [num("channels", "Filters", 1, 256), sel("kernel", "Kernel size", vals(1, 2, 3, 4, 5)), sel("stride", "Stride", [[2, "2 (크기 2배)"], [1, "1 (그대로)"]])],
|
|
651
|
+
eca: [sel("kernel", "Kernel size (이웃 채널 수)", vals(3, 5, 7))],
|
|
652
|
+
cbam: [sel("ratio", "Ratio (채널 ÷ ratio)", vals(2, 4, 8, 16)), sel("kernel", "공간 kernel size", vals(3, 5, 7))],
|
|
653
|
+
spatial_attn: [sel("kernel", "Kernel size", vals(3, 5, 7))],
|
|
654
|
+
mha: [HEADS, yes("causal", "Causal (앞만 봄, GPT식)"), sel("pos", "위치 정보", [["none", "없음 (PositionalEncoding을 따로)"], ["rope", "RoPE (Q·K를 위치만큼 돌림)"]])],
|
|
655
|
+
linear_attn: [HEADS], cross_attn: [HEADS],
|
|
656
|
+
pos_enc: [sel("mode", "방법", [["sin", "sin·cos (고정)"], ["learned", "학습형"]])],
|
|
657
|
+
patch_embed: [sel("patch", "조각 크기", [2, 4, 8].map((v) => [v, `${v}×${v}`])), num("dim", "Dim (조각마다 값 수)", 1, 256)],
|
|
658
|
+
groupnorm: [sel("groups", "Groups (채널의 약수)", vals(1, 2, 4, 8, 16, 32))],
|
|
659
|
+
spatial_dropout: [RATE], drop_path: [RATE],
|
|
660
|
+
gaussian_noise: [sel("std", "Std (잡음 크기)", vals(0.01, 0.05, 0.1, 0.2, 0.5))],
|
|
661
|
+
slice: [num("start", "시작 채널 (0부터)", 0, 1024), num("count", "채널 수", 1, 1024)],
|
|
662
|
+
reshape: [num("channels", "채널 수 (값 전체 개수의 약수)", 1, 65536)],
|
|
663
|
+
crop: [num("amount", "양쪽에서 자를 칸", 1, 64)], pad: [num("amount", "양쪽에 채울 칸", 1, 64)],
|
|
664
|
+
embedding: [sel("bins", "구간 수", vals(8, 16, 32, 64, 128, 256)), num("dim", "Dim (구간마다 값 수)", 1, 256)],
|
|
665
|
+
spectrogram: [sel("mode", "종류", [["log", "Spectrogram (log 크기)"], ["mel", "Mel spectrogram"], ["mfcc", "MFCC"]]),
|
|
666
|
+
sel("n_fft", "창 길이 (n_fft)", vals(8, 16, 32, 64, 128, 256)), sel("hop", "hop (창을 옮기는 칸)", vals(1, 2, 4, 8, 16, 32, 64, 128)),
|
|
667
|
+
sel("n_mels", "mel 칸 수", vals(8, 16, 32, 64)), sel("n_mfcc", "MFCC 개수", vals(8, 13, 20, 40))],
|
|
668
|
+
rand_flip: [sel("dir", "방향 (그림)", [["h", "좌우"], ["v", "상하"], ["hv", "좌우·상하"]])],
|
|
669
|
+
rand_rotate: [num("degrees", "최대 각도 (±도)", 1, 180)],
|
|
670
|
+
mnv3s: [sel("out", "출력", [["vector", "특징 1024개 (분류·이상 탐지)"], ["map", "특징 지도 (탐지: 8픽셀마다 한 칸)"]]),
|
|
671
|
+
sel("size", "입력 크기 (특징 1024개일 때)", [64, 96, 128, 160, 224].map((v) => [v, `${v}×${v}`])),
|
|
672
|
+
sel("train_last", "함께 학습할 끝 블록 (미세조정)", [[0, "없음 (모두 고정, 빠름)"], [1, "끝 1개"], [2, "끝 2개"], [3, "끝 3개"]])],
|
|
673
|
+
rand_shift: [FRAC], rand_zoom: [FRAC], rand_scale: [FRAC], rand_stretch: [FRAC],
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
// ------------------------------------------------------------ 순수 함수: 자동 배치
|
|
677
|
+
const validSize = (s) => Array.isArray(s) && s.length === 2 && s.every((v) => typeof v === "number" && v > 0 && isFinite(v));
|
|
678
|
+
const NARROW = [132, 66], MERGE_NW = 80; // 가로 방향 기본: 좁고 조금 높게 (옆으로 덜 길어짐)
|
|
679
|
+
const sizeOfDefault = (n, dir) => {
|
|
680
|
+
if (validSize(n.size)) return n.type === "act" ? [Math.min(...n.size), Math.min(...n.size)] : n.size.slice();
|
|
681
|
+
if (n.type === "act") return [ACT_D, ACT_D];
|
|
682
|
+
if (isMerge(n)) return [dir === "h" ? MERGE_NW : MERGE_W, MERGE_H];
|
|
683
|
+
return dir === "h" ? NARROW.slice() : [BOX_W, BOX_H];
|
|
684
|
+
};
|
|
685
|
+
// 글자 폭 어림 (한글 등은 2칸)
|
|
686
|
+
const textUnits = (t) => [...String(t)].reduce((s, ch) => s + (ch.charCodeAt(0) > 0x2e80 ? 2 : 1), 0);
|
|
687
|
+
function fitText(t, w) { // 폭 w(px)에 맞게 줄이고 '…'
|
|
688
|
+
const max = Math.floor((w - 14) / 6.5);
|
|
689
|
+
if (textUnits(t) <= max) return t;
|
|
690
|
+
let out = "";
|
|
691
|
+
for (const ch of String(t)) { if (textUnits(out + ch) > max - 1) break; out += ch; }
|
|
692
|
+
return out + "…";
|
|
693
|
+
}
|
|
694
|
+
// 상자 안 이름: 넓으면 한 줄, 좁으면 '종류' / '설정' 두 줄 (넘치면 '…')
|
|
695
|
+
function labelLines(n, summary, w) {
|
|
696
|
+
const full = label(n, summary);
|
|
697
|
+
if (w >= 190) return [full];
|
|
698
|
+
const i = full.indexOf(" · ");
|
|
699
|
+
return i < 0 ? [fitText(full, w)] : [fitText(full.slice(0, i), w), fitText(full.slice(i + 3), w)];
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// 한 층위(맨 위 또는 묶음 몸체)를 배치. 좌표는 이 층위의 (0, 0)에서.
|
|
703
|
+
// 층 배치(위→아래)를 늘 지킨다: 같은 층 상자는 겹치지 않게 옆으로 밀고, 선은 층 사이로만 지나가 짧고 아래로만 간다.
|
|
704
|
+
// 사용자가 옮긴 위치(pos)는 '희망': 가로는 그 자리(겹치면 옆으로 비킴), 세로는 그 높이에 맞는 층으로 내려감(위 상자보다 위로는 못 감).
|
|
705
|
+
function layoutScope(nodes, entryId, sizeOf, opts = {}) {
|
|
706
|
+
const GY = opts.dir === "h" ? GAP_H : GAP_Y; // 층 사이 간격 (가로는 동그라미·화살표가 보이게 넓게)
|
|
707
|
+
const list = (nodes || []).filter((n) => n && typeof n === "object" && typeof n.id === "string");
|
|
708
|
+
const items = new Map(); // id → {id, node, w, h, ax, layer, preds: [], succ: [], dummy}
|
|
709
|
+
const inner = new Map(); // repeat id → 안쪽 배치
|
|
710
|
+
const add = (id, node, w, h, ax) => items.set(id, { id, node, w, h, ax: ax === undefined ? w / 2 : ax, layer: 0, preds: [], succ: [], dummy: !node });
|
|
711
|
+
if (entryId === "in") add("in", null, IN_W, IN_H), (items.get("in").entry = true);
|
|
712
|
+
for (const n of list) {
|
|
713
|
+
if (items.has(n.id)) continue;
|
|
714
|
+
if (n.type === "repeat") {
|
|
715
|
+
const L = layoutScope(n.body && n.body.nodes, "in", sizeOf, opts);
|
|
716
|
+
inner.set(n.id, L);
|
|
717
|
+
const en = L.entries[0];
|
|
718
|
+
if (opts.dir === "h") { // 가로 방향: (돌려 계산하는 좌표에서) 머리글은 옆축 앞쪽
|
|
719
|
+
const cross = L.width + 2 * PAD + HEAD, w = Math.max(BOX_H, cross), off = HEAD + PAD + (w - cross) / 2;
|
|
720
|
+
L.off = [off, PAD];
|
|
721
|
+
add(n.id, n, w, L.height + 2 * PAD, en ? off + en.x + en.w / 2 : w / 2);
|
|
722
|
+
} else {
|
|
723
|
+
const w = Math.max(BOX_W, L.width + 2 * PAD); // 가로 기준: 묶음 안 입구 (줄이 곧게 이어지게)
|
|
724
|
+
L.off = [(w - L.width) / 2, HEAD];
|
|
725
|
+
add(n.id, n, w, L.height + HEAD + PAD, en ? L.off[0] + en.x + en.w / 2 : w / 2);
|
|
726
|
+
}
|
|
727
|
+
} else {
|
|
728
|
+
const [w, h] = sizeOf(n);
|
|
729
|
+
add(n.id, n, w, h);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
const realIns = (it) => (it.node ? [...new Set(insOf(it.node))].filter((x) => items.has(x) && x !== it.id) : []);
|
|
733
|
+
const hint = (it) => (it.node && validPos(it.node.pos) ? it.node.pos : null);
|
|
734
|
+
const out = list.find((n) => n.type === "output");
|
|
735
|
+
// 1. 층 번호: 가장 긴 경로 (순환은 끊음). lower(id): 이 층보다 위로는 안 감
|
|
736
|
+
const assign = (lower) => {
|
|
737
|
+
const memo = new Map(), visiting = new Set();
|
|
738
|
+
const layerOf = (id) => {
|
|
739
|
+
if (memo.has(id)) return memo.get(id);
|
|
740
|
+
if (visiting.has(id)) return 0;
|
|
741
|
+
visiting.add(id);
|
|
742
|
+
let v = lower(id);
|
|
743
|
+
for (const p of realIns(items.get(id))) v = Math.max(v, layerOf(p) + 1);
|
|
744
|
+
visiting.delete(id);
|
|
745
|
+
memo.set(id, v);
|
|
746
|
+
return v;
|
|
747
|
+
};
|
|
748
|
+
for (const id of items.keys()) items.get(id).layer = layerOf(id);
|
|
749
|
+
if (out) {
|
|
750
|
+
const mx = Math.max(0, ...[...items.values()].filter((it) => it.id !== out.id).map((it) => it.layer));
|
|
751
|
+
if (items.get(out.id).layer <= mx) items.get(out.id).layer = mx + 1;
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
assign(() => 0);
|
|
755
|
+
// 2. 세로 희망: 자연 배치의 줄 높이로 '몇 번째 줄'인지 정해 그 층 아래로만
|
|
756
|
+
if ([...items.values()].some((it) => hint(it) && hint(it)[1] > 0)) {
|
|
757
|
+
const top = rowTops([...items.values()]);
|
|
758
|
+
const pitch = BOX_H + GY;
|
|
759
|
+
const layerAt = (y) => {
|
|
760
|
+
let l = 0;
|
|
761
|
+
while (l + 1 < top.length && top[l + 1] - GY / 2 <= y) l++;
|
|
762
|
+
const last = top.length - 1;
|
|
763
|
+
return l === last && y > top[last] + pitch ? last + Math.round((y - top[last]) / pitch) : l;
|
|
764
|
+
};
|
|
765
|
+
assign((id) => { const h = hint(items.get(id)); return h ? layerAt(h[1]) : 0; });
|
|
766
|
+
}
|
|
767
|
+
// 3. 선과 보이지 않는 점(dummy): 두 층 이상 건너는 선은 사이 층마다 자리를 차지해 상자와 겹치지 않음
|
|
768
|
+
const edges = [];
|
|
769
|
+
for (const it of [...items.values()]) {
|
|
770
|
+
if (it.dummy) continue;
|
|
771
|
+
for (const u of realIns(it)) {
|
|
772
|
+
const a = items.get(u), chainIds = [u];
|
|
773
|
+
if (it.layer > a.layer + 1) {
|
|
774
|
+
for (let k = a.layer + 1; k < it.layer; k++) {
|
|
775
|
+
const d = `${u}>${it.id}#${k}`;
|
|
776
|
+
add(d, null, DUMMY_W, 0);
|
|
777
|
+
items.get(d).layer = k;
|
|
778
|
+
chainIds.push(d);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
chainIds.push(it.id);
|
|
782
|
+
for (let k = 0; k + 1 < chainIds.length; k++) {
|
|
783
|
+
const p = items.get(chainIds[k]), q = items.get(chainIds[k + 1]);
|
|
784
|
+
if (q.layer > p.layer) { p.succ.push(q.id); q.preds.push(p.id); }
|
|
785
|
+
}
|
|
786
|
+
edges.push({ from: u, to: it.id, chain: chainIds });
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
// 4. 층 안 순서: 깊이 우선(첫 입력을 따라감) → 무게중심 정렬
|
|
790
|
+
const maxL = Math.max(0, ...[...items.values()].map((it) => it.layer));
|
|
791
|
+
const rows = Array.from({ length: maxL + 1 }, () => []);
|
|
792
|
+
const placed = new Set();
|
|
793
|
+
const prio = (p, q) => { // p의 다음 중 q의 우선순위: 첫 입력 직선 < 다른 직선 < 건너뛰기
|
|
794
|
+
const qi = items.get(q);
|
|
795
|
+
if (qi.dummy) return 2;
|
|
796
|
+
return insOf(qi.node)[0] === p ? 0 : 1;
|
|
797
|
+
};
|
|
798
|
+
const visit = (id) => {
|
|
799
|
+
if (placed.has(id)) return;
|
|
800
|
+
placed.add(id);
|
|
801
|
+
const it = items.get(id);
|
|
802
|
+
rows[it.layer].push(id);
|
|
803
|
+
[...it.succ].sort((a, b) => prio(id, a) - prio(id, b)).forEach(visit);
|
|
804
|
+
};
|
|
805
|
+
[...items.values()].filter((it) => !it.preds.length).forEach((it) => visit(it.id));
|
|
806
|
+
for (const it of items.values()) visit(it.id);
|
|
807
|
+
const pos = new Map();
|
|
808
|
+
const index = () => rows.forEach((r) => r.forEach((id, i) => pos.set(id, i)));
|
|
809
|
+
index();
|
|
810
|
+
const bary = (ids) => (ids.length ? ids.reduce((s, x) => s + pos.get(x), 0) / ids.length : null);
|
|
811
|
+
const sortRow = (row, key) => {
|
|
812
|
+
const k = row.map((id, i) => [id, key(id), i]);
|
|
813
|
+
k.forEach((x) => { if (x[1] === null) x[1] = pos.get(x[0]); });
|
|
814
|
+
k.sort((a, b) => a[1] - b[1] || a[2] - b[2]);
|
|
815
|
+
row.splice(0, row.length, ...k.map((x) => x[0]));
|
|
816
|
+
};
|
|
817
|
+
for (let iter = 0; iter < 4; iter++) {
|
|
818
|
+
for (let l = 1; l <= maxL; l++) sortRow(rows[l], (id) => bary(items.get(id).preds));
|
|
819
|
+
index();
|
|
820
|
+
for (let l = maxL - 1; l >= 0; l--) sortRow(rows[l], (id) => bary(items.get(id).succ));
|
|
821
|
+
index();
|
|
822
|
+
}
|
|
823
|
+
// 5. 가로: 각 상자를 첫 입력(또는 희망 위치)의 가운데에 맞추고, 겹치면 오른쪽으로 밂
|
|
824
|
+
const firstPred = (it) => {
|
|
825
|
+
if (it.dummy) return it.preds[0];
|
|
826
|
+
const p0 = insOf(it.node)[0];
|
|
827
|
+
return it.preds.find((p) => p === p0 || p.startsWith(`${p0}>${it.id}#`)) || it.preds[0];
|
|
828
|
+
};
|
|
829
|
+
// 희망 위치가 있으면 줄마다 '가려는 가로 위치'(희망 위치 또는 첫 입력의 가운데) 순으로 세운 뒤 놓음:
|
|
830
|
+
// 따라가는 상자와 건너뛰는 선이 줄마다 같은 쪽에 있어 선이 엇갈리지 않음
|
|
831
|
+
const place = (useHints, auto) => {
|
|
832
|
+
const cx = new Map();
|
|
833
|
+
rows.forEach((r) => {
|
|
834
|
+
const wantOf = (id) => {
|
|
835
|
+
const it = items.get(id), h = useHints && hint(it), f = firstPred(it);
|
|
836
|
+
return h ? h[0] + it.ax : f !== undefined && cx.has(f) ? cx.get(f) : null;
|
|
837
|
+
};
|
|
838
|
+
if (useHints) {
|
|
839
|
+
const w = new Map(r.map((id) => [id, wantOf(id)]));
|
|
840
|
+
const k = r.map((id, i) => [id, w.get(id) !== null ? w.get(id) : auto.get(id), i]);
|
|
841
|
+
k.sort((a, b) => a[1] - b[1] || a[2] - b[2]);
|
|
842
|
+
r.splice(0, r.length, ...k.map((x) => x[0]));
|
|
843
|
+
}
|
|
844
|
+
let cursor = -Infinity;
|
|
845
|
+
for (const id of r) {
|
|
846
|
+
const it = items.get(id), want = wantOf(id);
|
|
847
|
+
const c = Math.max(want !== null ? want : it.ax, cursor + GAP_X + it.ax);
|
|
848
|
+
cx.set(id, c);
|
|
849
|
+
cursor = c + it.w - it.ax;
|
|
850
|
+
}
|
|
851
|
+
});
|
|
852
|
+
return cx;
|
|
853
|
+
};
|
|
854
|
+
let cx = place(false);
|
|
855
|
+
if ([...items.values()].some((it) => hint(it))) {
|
|
856
|
+
cx = place(true, cx);
|
|
857
|
+
index();
|
|
858
|
+
}
|
|
859
|
+
// 6. 세로: 줄마다 가장 높은 상자 기준
|
|
860
|
+
const rowTop = rowTops([...items.values()]), rowH = rows.map((r) => Math.max(0, ...r.map((id) => items.get(id).h)));
|
|
861
|
+
const minX = Math.min(0, ...[...items.values()].map((it) => cx.get(it.id) - it.ax));
|
|
862
|
+
const box = (it) => ({ x: cx.get(it.id) - it.ax - minX, y: rowTop[it.layer] + (rowH[it.layer] - it.h) / 2, w: it.w, h: it.h, ax: it.ax });
|
|
863
|
+
const res = { nodes: [], groups: [], edges: [], entries: [], width: 0, height: 0 };
|
|
864
|
+
const entryAt = new Map(); // 묶음 id → 묶음 안 입구 가운데
|
|
865
|
+
for (const it of items.values()) {
|
|
866
|
+
const b = box(it);
|
|
867
|
+
if (it.entry) { res.entries.push({ x: b.x, y: b.y, w: b.w, h: b.h, anc: [] }); continue; }
|
|
868
|
+
if (it.dummy) continue;
|
|
869
|
+
res.nodes.push(Object.assign({ id: it.id, layer: it.layer, ox: -minX, oy: 0, anc: [] }, b)); // ox: 희망 위치(pos)의 기준점 (왼쪽 끝 보정 포함)
|
|
870
|
+
if (inner.has(it.id)) {
|
|
871
|
+
const L = inner.get(it.id), ox = b.x + L.off[0], oy = b.y + L.off[1];
|
|
872
|
+
const up = (o) => ({ anc: [it.id].concat(o.anc || []) }); // 바깥 묶음부터 안쪽 묶음까지
|
|
873
|
+
res.groups.push({ id: it.id, x: b.x, y: b.y, w: b.w, h: b.h, anc: [] });
|
|
874
|
+
for (const n of L.nodes) res.nodes.push(Object.assign({}, n, { x: n.x + ox, y: n.y + oy, ox: n.ox + ox, oy: n.oy + oy, inGroup: n.inGroup || it.id }, up(n)));
|
|
875
|
+
for (const gr of L.groups) res.groups.push(Object.assign({}, gr, { x: gr.x + ox, y: gr.y + oy }, up(gr)));
|
|
876
|
+
for (const e of L.edges) res.edges.push(Object.assign({}, e, { points: e.points.map(([px, py]) => [px + ox, py + oy]) }, up(e)));
|
|
877
|
+
L.entries.forEach((en, k) => {
|
|
878
|
+
const m = Object.assign({}, en, { x: en.x + ox, y: en.y + oy }, up(en));
|
|
879
|
+
res.entries.push(m);
|
|
880
|
+
if (k === 0) entryAt.set(it.id, [m.x + m.w / 2, m.y + m.h / 2]);
|
|
881
|
+
});
|
|
882
|
+
const body = (it.node.body && it.node.body.nodes) || []; // 출구(몸체에서 아무도 안 쓰는 노드) → 묶음 아래 가운데
|
|
883
|
+
const sinks = body.filter((n) => n && !body.some((m) => insOf(m).includes(n.id)));
|
|
884
|
+
const ex = sinks.length && L.nodes.find((n) => n.id === sinks[sinks.length - 1].id);
|
|
885
|
+
if (ex) {
|
|
886
|
+
const sx = ex.x + ox + ex.ax, sy = ex.y + oy + ex.h, gx = b.x + b.ax, gb = b.y + b.h;
|
|
887
|
+
const pts = sx === gx ? [[sx, sy], [gx, gb]] : [[sx, sy], [sx, gb - PAD / 2], [gx, gb - PAD / 2], [gx, gb]];
|
|
888
|
+
res.edges.push({ from: ex.id, to: it.id, kind: "exit", points: pts, anc: [it.id] });
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
// 7. 선: 아래로 → 줄 사이 높이에서 가로 → 아래로 (dummy를 따라 옆으로 돌아감)
|
|
893
|
+
for (const e of edges) {
|
|
894
|
+
const its = e.chain.map((id) => items.get(id));
|
|
895
|
+
const pts = [];
|
|
896
|
+
const first = box(its[0]);
|
|
897
|
+
pts.push([first.x + first.ax, first.y + first.h]);
|
|
898
|
+
if (its[its.length - 1].layer <= its[0].layer) { // 순환 등: 바로 잇기
|
|
899
|
+
const t = box(its[its.length - 1]);
|
|
900
|
+
pts.push([t.x + t.ax, t.y]);
|
|
901
|
+
} else {
|
|
902
|
+
for (let k = 1; k < its.length; k++) {
|
|
903
|
+
const prev = its[k - 1], cur = its[k], b = box(cur), midY = rowTop[prev.layer] + rowH[prev.layer] + GY / 2;
|
|
904
|
+
const en = !cur.dummy && entryAt.get(cur.id);
|
|
905
|
+
const x = en ? en[0] : b.x + b.ax;
|
|
906
|
+
pts.push([pts[pts.length - 1][0], midY], [x, midY]);
|
|
907
|
+
pts.push(cur.dummy ? [x, rowTop[cur.layer] + rowH[cur.layer]] : en ? [x, en[1]] : [x, b.y]);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
const uniq = pts.filter((p, i) => i === 0 || p[0] !== pts[i - 1][0] || p[1] !== pts[i - 1][1]);
|
|
911
|
+
const clean = uniq.filter((p, i) => i === 0 || i === uniq.length - 1 || // 일직선 가운데 점 빼기
|
|
912
|
+
!((uniq[i - 1][0] === p[0] && p[0] === uniq[i + 1][0]) || (uniq[i - 1][1] === p[1] && p[1] === uniq[i + 1][1])));
|
|
913
|
+
res.edges.push({ from: e.from, to: e.to, points: clean });
|
|
914
|
+
}
|
|
915
|
+
const xs = [0], ys = [0];
|
|
916
|
+
for (const o of res.nodes.concat(res.groups, res.entries)) { xs.push(o.x + o.w); ys.push(o.y + o.h); }
|
|
917
|
+
for (const e of res.edges) for (const [x, y] of e.points) { xs.push(x); ys.push(y); }
|
|
918
|
+
res.width = Math.max(...xs);
|
|
919
|
+
res.height = Math.max(...ys);
|
|
920
|
+
return res;
|
|
921
|
+
|
|
922
|
+
function rowTops(its) { // 층마다 위쪽 y (가장 높은 상자 기준 + 간격)
|
|
923
|
+
const L = Math.max(0, ...its.map((it) => it.layer)), h = Array(L + 1).fill(0);
|
|
924
|
+
for (const it of its) h[it.layer] = Math.max(h[it.layer], it.h);
|
|
925
|
+
const t = [];
|
|
926
|
+
let y = 0;
|
|
927
|
+
h.forEach((v, l) => { t[l] = y; y += v + GY; });
|
|
928
|
+
return t;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// 선 그리기: 꺾인선(ortho) 또는 꺾이는 곳을 둥글게(curve)
|
|
933
|
+
function edgePath(pts, style) {
|
|
934
|
+
if (style !== "curve" || pts.length < 3) return pts.map((p, i) => `${i ? "L" : "M"}${p[0]} ${p[1]}`).join(" ");
|
|
935
|
+
let d = `M${pts[0][0]} ${pts[0][1]}`;
|
|
936
|
+
for (let i = 1; i < pts.length - 1; i++) {
|
|
937
|
+
const [a, b, c] = [pts[i - 1], pts[i], pts[i + 1]];
|
|
938
|
+
const r = Math.min(16, Math.hypot(b[0] - a[0], b[1] - a[1]) / 2, Math.hypot(c[0] - b[0], c[1] - b[1]) / 2);
|
|
939
|
+
const u = (q) => { const L2 = Math.hypot(q[0] - b[0], q[1] - b[1]) || 1; return [b[0] + (q[0] - b[0]) * r / L2, b[1] + (q[1] - b[1]) * r / L2]; };
|
|
940
|
+
const p1 = u(a), p2 = u(c);
|
|
941
|
+
d += ` L${p1[0]} ${p1[1]} Q${b[0]} ${b[1]} ${p2[0]} ${p2[1]}`;
|
|
942
|
+
}
|
|
943
|
+
const z = pts[pts.length - 1];
|
|
944
|
+
return d + ` L${z[0]} ${z[1]}`;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// 줄바꿈: 흐름축(이 좌표계의 y)을 층 경계에서 끊어 다음 줄을 옆축(x)으로 옮김. 직접(brk) 또는 자동(wrap: 한 줄 최대 길이).
|
|
948
|
+
// 줄 사이를 잇는 선은 '되돌아가는 선'(나가서 줄 사이 길로, 다음 줄 첫머리 앞에서 들어감)으로 다시 그림
|
|
949
|
+
const WRAP_GAP = 44, ROW_PAD = 30; // ROW_PAD: 줄 첫머리 앞 손잡이 자리
|
|
950
|
+
function wrapRows(L, g, wrapLen) {
|
|
951
|
+
const top = L.nodes.filter((n) => !n.anc.length);
|
|
952
|
+
const range = new Map();
|
|
953
|
+
for (const n of top) {
|
|
954
|
+
const r = range.get(n.layer) || [Infinity, -Infinity];
|
|
955
|
+
range.set(n.layer, [Math.min(r[0], n.y), Math.max(r[1], n.y + n.h)]);
|
|
956
|
+
}
|
|
957
|
+
const layers = [...range.keys()].sort((a, b) => a - b);
|
|
958
|
+
if (layers.length < 2) return L;
|
|
959
|
+
const brk = new Set();
|
|
960
|
+
for (const n of nodesOf(g)) {
|
|
961
|
+
const t = n && n.brk && top.find((x) => x.id === n.id);
|
|
962
|
+
if (t && t.layer !== layers[0]) brk.add(t.layer);
|
|
963
|
+
}
|
|
964
|
+
if (wrapLen > 0) { // 자동: 한 줄이 길이를 넘으면 그 층부터 다음 줄
|
|
965
|
+
let start = range.get(layers[0])[0];
|
|
966
|
+
for (const l of layers.slice(1)) {
|
|
967
|
+
if (brk.has(l)) start = range.get(l)[0];
|
|
968
|
+
else if (range.get(l)[1] - start > wrapLen) { brk.add(l); start = range.get(l)[0]; }
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
if (!brk.size) return L;
|
|
972
|
+
const segOfLayer = (l) => [...brk].filter((b) => b <= l).length;
|
|
973
|
+
const seg = new Map(top.map((n) => [n.id, segOfLayer(n.layer)]));
|
|
974
|
+
const segOf = (o) => seg.get(o.anc && o.anc.length ? o.anc[0] : o.id);
|
|
975
|
+
const nSeg = brk.size + 1;
|
|
976
|
+
const crossing = (e) => !(e.anc && e.anc.length) && segOf({ id: e.from }) !== segOf({ id: e.to }) && seg.has(e.from) && seg.has(e.to);
|
|
977
|
+
// 줄마다 흐름 시작과 옆축 범위
|
|
978
|
+
const S = Array.from({ length: nSeg }, () => ({ y0: Infinity, x0: Infinity, x1: -Infinity }));
|
|
979
|
+
const grow = (k, x0, x1, y0) => { const s = S[k]; s.x0 = Math.min(s.x0, x0); s.x1 = Math.max(s.x1, x1); if (y0 !== undefined) s.y0 = Math.min(s.y0, y0); };
|
|
980
|
+
for (const n of L.nodes) grow(segOf(n), n.x, n.x + n.w, n.anc.length ? undefined : n.y);
|
|
981
|
+
for (const o of L.groups.concat(L.entries)) grow(segOf(o), o.x, o.x + o.w);
|
|
982
|
+
for (const e of L.edges) if (!crossing(e) && segOf({ id: e.from, anc: e.anc }) !== undefined) for (const [x] of e.points) grow(segOf({ id: e.from, anc: e.anc }), x, x);
|
|
983
|
+
// 줄마다 옮긴 거리: 그 줄 상자 중 shift가 있는 것(층 순서로 처음), 없으면 줄 첫 상자가 기준
|
|
984
|
+
const gnode = new Map();
|
|
985
|
+
for (const n of nodesOf(g)) if (n && n.id) gnode.set(n.id, n);
|
|
986
|
+
const segTop = Array.from({ length: nSeg }, () => []);
|
|
987
|
+
for (const n of top) if (segOf(n) !== undefined) segTop[segOf(n)].push(n);
|
|
988
|
+
segTop.forEach((a) => a.sort((p, q) => p.layer - q.layer || p.x - q.x));
|
|
989
|
+
const shiftOfNode = (n) => { const m = n && gnode.get(n.id); return m && Number(m.shift) > 0 ? Number(m.shift) : 0; };
|
|
990
|
+
const anchor = segTop.map((a) => a.find((n) => shiftOfNode(n) > 0) || a[0]);
|
|
991
|
+
const moved = anchor.map(shiftOfNode);
|
|
992
|
+
let cross = 0;
|
|
993
|
+
const shift = S.map((s, k) => {
|
|
994
|
+
const d = [cross - s.x0, S[0].y0 - s.y0 + ROW_PAD + moved[k]];
|
|
995
|
+
cross += s.x1 - s.x0 + WRAP_GAP;
|
|
996
|
+
return d;
|
|
997
|
+
});
|
|
998
|
+
L.rows = S.map((s, k) => ({ k, anchor: anchor[k] ? anchor[k].id : null, shift: moved[k],
|
|
999
|
+
c0: s.x0 + shift[k][0], c1: s.x1 + shift[k][0], f0: S[0].y0 + ROW_PAD + moved[k] }));
|
|
1000
|
+
const mv = (o, k) => { const [dx, dy] = shift[k]; o.x += dx; o.y += dy; if (o.ox !== undefined) { o.ox += dx; o.oy += dy; } };
|
|
1001
|
+
for (const n of L.nodes) mv(n, segOf(n));
|
|
1002
|
+
for (const o of L.groups.concat(L.entries)) mv(o, segOf(o));
|
|
1003
|
+
const lanes = new Map();
|
|
1004
|
+
for (const e of L.edges) {
|
|
1005
|
+
if (crossing(e)) continue;
|
|
1006
|
+
const k = segOf({ id: e.from, anc: e.anc });
|
|
1007
|
+
if (k === undefined) continue;
|
|
1008
|
+
e.points = e.points.map(([x, y]) => [x + shift[k][0], y + shift[k][1]]);
|
|
1009
|
+
}
|
|
1010
|
+
for (const e of L.edges) { // 줄 사이 선: 나가서 → 줄 사이 길 → 다음 줄 첫머리 앞 → 들어감
|
|
1011
|
+
if (!crossing(e)) continue;
|
|
1012
|
+
const a = L.nodes.find((n) => n.id === e.from && !n.anc.length), b = L.nodes.find((n) => n.id === e.to && !n.anc.length);
|
|
1013
|
+
const en = L.entries.find((x) => x.anc && x.anc.length === 1 && x.anc[0] === e.to);
|
|
1014
|
+
const s = [a.x + a.ax, a.y + a.h], t = en ? [en.x + en.w / 2, en.y + en.h / 2] : [b.x + b.ax, b.y];
|
|
1015
|
+
const kb = segOf(b), used = lanes.get(kb) || 0;
|
|
1016
|
+
lanes.set(kb, used + 1);
|
|
1017
|
+
const lane = shift[kb][0] + S[kb].x0 - WRAP_GAP / 2 + ((used % 5) - 2) * 6;
|
|
1018
|
+
const out = s[1] + 12, inn = t[1] - 12 - (used % 3) * 4;
|
|
1019
|
+
e.points = [s, [s[0], out], [lane, out], [lane, inn], [t[0], inn], t];
|
|
1020
|
+
e.wrap = true;
|
|
1021
|
+
}
|
|
1022
|
+
const xs = [0], ys = [0];
|
|
1023
|
+
for (const o of L.nodes.concat(L.groups, L.entries)) { xs.push(o.x + o.w); ys.push(o.y + o.h); }
|
|
1024
|
+
for (const e of L.edges) for (const [x, y] of e.points) { xs.push(x); ys.push(y); }
|
|
1025
|
+
L.width = Math.max(...xs);
|
|
1026
|
+
L.height = Math.max(...ys);
|
|
1027
|
+
return L;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
// opts.edge: "ortho"(꺾인선, 기본) | "curve"(곡선). opts.dir: "v"(위→아래, 기본) | "h"(왼쪽→오른쪽). opts.wrap: 자동 줄바꿈 길이(px)
|
|
1031
|
+
// 가로는 x·y를 바꿔 같은 배치(겹침 없음·짧은 화살표)를 계산한 뒤 돌려놓음. ax는 늘 '옆축' 기준점(세로면 가로, 가로면 세로)
|
|
1032
|
+
function layoutGraph(g, sizeOf = null, opts = {}) {
|
|
1033
|
+
opts = opts || {};
|
|
1034
|
+
const h = opts.dir === "h";
|
|
1035
|
+
sizeOf = sizeOf || ((n) => sizeOfDefault(n, h ? "h" : "v"));
|
|
1036
|
+
const swapPos = (nodes) => (nodes || []).map((n) => {
|
|
1037
|
+
if (!n || typeof n !== "object") return n;
|
|
1038
|
+
const m = Object.assign({}, n);
|
|
1039
|
+
if (validPos(n.pos)) m.pos = [n.pos[1], n.pos[0]];
|
|
1040
|
+
if (n.body && Array.isArray(n.body.nodes)) m.body = Object.assign({}, n.body, { nodes: swapPos(n.body.nodes) });
|
|
1041
|
+
return m;
|
|
1042
|
+
});
|
|
1043
|
+
const L = wrapRows(layoutScope(h ? swapPos(nodesOf(g)) : nodesOf(g), "input", h ? (n) => sizeOf(n).slice().reverse() : sizeOf, opts),
|
|
1044
|
+
g, opts.wrap ? opts.wrap - 2 * MARGIN - ROW_PAD : 0);
|
|
1045
|
+
const mv = (o) => Object.assign({}, o, { x: o.x + MARGIN, y: o.y + MARGIN }, o.ox !== undefined ? { ox: o.ox + MARGIN, oy: o.oy + MARGIN } : {});
|
|
1046
|
+
const t = (o) => (h ? Object.assign({}, o, { x: o.y, y: o.x, w: o.h, h: o.w }, o.ox !== undefined ? { ox: o.oy, oy: o.ox } : {}) : o);
|
|
1047
|
+
const res = {
|
|
1048
|
+
nodes: L.nodes.map(mv).map(t),
|
|
1049
|
+
groups: L.groups.map(mv).map(t),
|
|
1050
|
+
edges: L.edges.map((e) => Object.assign({}, e, { points: e.points.map(([x, y]) => (h ? [y + MARGIN, x + MARGIN] : [x + MARGIN, y + MARGIN])) })),
|
|
1051
|
+
entries: L.entries.map(mv).map(t),
|
|
1052
|
+
width: (h ? L.height : L.width) + 2 * MARGIN, height: (h ? L.width : L.height) + 2 * MARGIN, dir: h ? "h" : "v",
|
|
1053
|
+
// 줄바꿈한 줄들: 손잡이 자리 (x, y = 줄 첫머리 앞, 옆축 가운데)와 기준 상자·옮긴 거리
|
|
1054
|
+
rows: (L.rows || []).map((r) => {
|
|
1055
|
+
const f = r.f0 + MARGIN, c = (r.c0 + r.c1) / 2 + MARGIN;
|
|
1056
|
+
return { k: r.k, anchor: r.anchor, shift: r.shift, x: h ? f : c, y: h ? c : f };
|
|
1057
|
+
}),
|
|
1058
|
+
};
|
|
1059
|
+
res.nodes.sort((a, b) => a.anc.length - b.anc.length || a.layer - b.layer || (h ? a.x - b.x || a.y - b.y : a.y - b.y || a.x - b.x));
|
|
1060
|
+
return res;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// 고른 상자들을 바로 위 상자(첫 입력)와 가운데를 맞춤 → 화살표가 수직. 위에 있는 것부터 차례로
|
|
1064
|
+
function alignToParent(g0, ids, opts) {
|
|
1065
|
+
let g = g0;
|
|
1066
|
+
const h = opts && opts.dir === "h", flow = (n) => (h ? n.x : n.y);
|
|
1067
|
+
const ys = layoutGraph(g, undefined, opts).nodes;
|
|
1068
|
+
const order = ids.filter((id) => ys.some((n) => n.id === id)).sort((a, b) => flow(ys.find((n) => n.id === a)) - flow(ys.find((n) => n.id === b)));
|
|
1069
|
+
for (const id of order) {
|
|
1070
|
+
const L = layoutGraph(g, undefined, opts), n = L.nodes.find((x) => x.id === id), f = findNode(g, id);
|
|
1071
|
+
const src = f && insOf(f.node)[0];
|
|
1072
|
+
if (!n || src === undefined) continue;
|
|
1073
|
+
let pc; // 위(가로면 왼쪽) 상자의 옆축 가운데
|
|
1074
|
+
if (src === "in") {
|
|
1075
|
+
const grp = n.anc[n.anc.length - 1], en = L.entries.find((x) => x.anc[x.anc.length - 1] === grp);
|
|
1076
|
+
if (!en) continue;
|
|
1077
|
+
pc = h ? en.y + en.h / 2 : en.x + en.w / 2;
|
|
1078
|
+
} else {
|
|
1079
|
+
const p = L.nodes.find((x) => x.id === src);
|
|
1080
|
+
if (!p) continue;
|
|
1081
|
+
pc = (h ? p.y : p.x) + p.ax;
|
|
1082
|
+
}
|
|
1083
|
+
// 흐름축은 0 = 바로 다음 층 (짧은 화살표)
|
|
1084
|
+
g = setPos(g, id, h ? [0, pc - n.ax - n.oy] : [pc - n.ax - n.ox, 0]);
|
|
1085
|
+
}
|
|
1086
|
+
return g;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
// ------------------------------------------------------------ 화면
|
|
1090
|
+
const LOOSE = ["입력이 연결되지 않음", "출력까지 이어지지 않음", "합칠 입력이 2개 이상"]; // 아직 선을 안 이은 것
|
|
1091
|
+
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c]));
|
|
1092
|
+
const fmtShape = (s) => (!s ? "" : `(${s.join(", ")})`);
|
|
1093
|
+
// 이미지면 기본 Conv를 Conv2D로
|
|
1094
|
+
// 넣는 자리의 모양(rank: 2 줄 · 3 그림, 모르면 데이터 종류)에 맞춰 1D ↔ 2D 층을 바꿈
|
|
1095
|
+
function forInput(n, input, rank) {
|
|
1096
|
+
const r = rank || (input === "image" ? 3 : 2);
|
|
1097
|
+
if (r === 3 && TWO_D[n.type]) {
|
|
1098
|
+
const t = TWO_D[n.type];
|
|
1099
|
+
if (t === "conv2d") return { type: "conv2d", channels: n.channels, kernel: 3, stride: 1 };
|
|
1100
|
+
if (t === "pool2d" || t === "max_pool2d") return { type: t, size: 2 };
|
|
1101
|
+
const m = Object.assign({}, n, { type: t });
|
|
1102
|
+
if (m.kernel > 7) m.kernel = 7;
|
|
1103
|
+
return m;
|
|
1104
|
+
}
|
|
1105
|
+
if (r === 2 && ONE_D[n.type] && rank) return Object.assign({}, n, { type: ONE_D[n.type] });
|
|
1106
|
+
return n;
|
|
1107
|
+
}
|
|
1108
|
+
const makeOf = (id) => PALETTE[id].make();
|
|
1109
|
+
|
|
1110
|
+
function mount(els, opts) {
|
|
1111
|
+
// els: {canvas, palette, settings}; opts: {onChange(graph), getSummary(), getInput(), onHint(msg), onHistory(canUndo, canRedo)}
|
|
1112
|
+
let graph = { version: 2, nodes: [{ id: "input", type: "input" }] }, sel = new Set(), selEdge = null, shapes = {}, issues = {}, L = null;
|
|
1113
|
+
let dropEdge = null, clip = null, edgeStyle = "ortho", flowDir = "v", autoWrap = false;
|
|
1114
|
+
let zoom = 1; // 판만 확대·축소 (페이지 전체가 아니라): SVG 크기만 바꾸고 좌표(viewBox)는 그대로
|
|
1115
|
+
// 자동 줄바꿈 길이: 보이는 판의 흐름 방향 길이 (판 좌표로)
|
|
1116
|
+
const lopts = () => ({ edge: edgeStyle, dir: flowDir,
|
|
1117
|
+
wrap: autoWrap ? (flowDir === "h" ? els.canvas.clientWidth : els.canvas.clientHeight) / zoom - 30 : 0 });
|
|
1118
|
+
const hist = history();
|
|
1119
|
+
const input = () => (opts.getInput ? opts.getInput() : "timeseries");
|
|
1120
|
+
const hint = (msg) => opts.onHint && opts.onHint(msg);
|
|
1121
|
+
const emit = () => opts.onChange(clone(graph));
|
|
1122
|
+
function set(g) { // 대표 모델 바꾸기 등: 되돌리기 가능
|
|
1123
|
+
if (nodesOf(graph).length > 1 && JSON.stringify(g) !== JSON.stringify(graph)) hist.push(graph);
|
|
1124
|
+
graph = clone(g); sel = new Set(); selEdge = null; draw();
|
|
1125
|
+
}
|
|
1126
|
+
function get() { return clone(graph); }
|
|
1127
|
+
function change(g, newSel) { // newSel: id 목록 또는 undefined(그대로)
|
|
1128
|
+
if (JSON.stringify(g) === JSON.stringify(graph)) return draw();
|
|
1129
|
+
hist.push(graph);
|
|
1130
|
+
graph = g;
|
|
1131
|
+
issues = {}; // 예전 그래프의 문제 표시는 바로 지움 (새 검사 결과가 오면 다시)
|
|
1132
|
+
if (newSel !== undefined) { sel = new Set(newSel); selEdge = null; }
|
|
1133
|
+
for (const id of [...sel]) if (!findNode(graph, id)) sel.delete(id);
|
|
1134
|
+
draw(); emit();
|
|
1135
|
+
}
|
|
1136
|
+
function undo() { const p = hist.undo(graph); if (p) { graph = p; sel = new Set(); selEdge = null; draw(); emit(); } return !!p; }
|
|
1137
|
+
function redo() { const p = hist.redo(graph); if (p) { graph = p; sel = new Set(); selEdge = null; draw(); emit(); } return !!p; }
|
|
1138
|
+
function decorate(checked) {
|
|
1139
|
+
shapes = (checked && checked.shapes) || {};
|
|
1140
|
+
issues = {};
|
|
1141
|
+
for (const i of (checked && checked.issues) || []) (issues[i.node] = issues[i.node] || []).push(i);
|
|
1142
|
+
draw();
|
|
1143
|
+
}
|
|
1144
|
+
function select(id) { sel = new Set(id ? [id] : []); selEdge = null; draw(); }
|
|
1145
|
+
const selIds = () => [...sel].filter((id) => findNode(graph, id));
|
|
1146
|
+
const outputNode = () => nodesOf(graph).find((n) => n.type === "output");
|
|
1147
|
+
|
|
1148
|
+
function draw() {
|
|
1149
|
+
const summary = opts.getSummary();
|
|
1150
|
+
L = layoutGraph(graph, undefined, lopts());
|
|
1151
|
+
const top = (o) => (o.anc && o.anc.length ? ` data-anc="${esc(o.anc.join(" "))}"` : "");
|
|
1152
|
+
// 판: 보이는 칸을 다 채우고, 오른쪽·아래로 여유를 둬서 어디로든 끌어 둘 수 있게
|
|
1153
|
+
const W = Math.max(L.width + 240, (els.canvas.clientWidth - 24) / zoom), H = Math.max(L.height + 240, (els.canvas.clientHeight - 24) / zoom); // 판 안쪽 여백(8px×2) 빼고
|
|
1154
|
+
let s = `<svg viewBox="0 0 ${W} ${H}" width="${Math.round(W * zoom)}" height="${Math.round(H * zoom)}" class="flow-svg">` +
|
|
1155
|
+
`<defs><marker id="fa" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">` +
|
|
1156
|
+
`<path d="M2 1L8 5L2 9" fill="none" stroke="context-stroke" stroke-width="1.6" stroke-linecap="round"/></marker></defs>`;
|
|
1157
|
+
for (const gr of L.groups) {
|
|
1158
|
+
const n = findNode(graph, gr.id).node, on = sel.has(gr.id), bad = issues[gr.id];
|
|
1159
|
+
s += `<g class="fg${on ? " sel" : ""}${bad ? " bad" : ""}" data-node="${esc(gr.id)}"${top(gr)}>` +
|
|
1160
|
+
`<rect x="${gr.x}" y="${gr.y}" width="${gr.w}" height="${gr.h}" rx="12" class="grp"/>` +
|
|
1161
|
+
`<rect x="${gr.x}" y="${gr.y}" width="${gr.w}" height="${HEAD - 4}" rx="12" class="grp-head"><title>묶음 머리를 끌면 묶음째 옮김</title></rect>` +
|
|
1162
|
+
`<text x="${gr.x + 12}" y="${gr.y + 18}" class="glabel">${esc(label(n))}${shapes[gr.id] ? " " + esc(fmtShape(shapes[gr.id])) : ""}${bad ? " ⚠ " + esc(bad[0].message) : ""}</text>` +
|
|
1163
|
+
port(L.nodes.find((n) => n.id === gr.id) || gr) + `</g>`;
|
|
1164
|
+
}
|
|
1165
|
+
for (const e of L.edges) {
|
|
1166
|
+
const d = edgePath(e.points, edgeStyle);
|
|
1167
|
+
if (e.kind === "exit") { s += `<path d="${d}" class="edge exit"${top(e)}/>`; continue; } // 묶음 출구 → 묶음 아래
|
|
1168
|
+
const to = findNode(graph, e.to), skip = to && insOf(to.node)[0] !== e.from;
|
|
1169
|
+
const on = selEdge && selEdge.from === e.from && selEdge.to === e.to;
|
|
1170
|
+
const key = esc(`${e.from}>${e.to}`);
|
|
1171
|
+
const into = L.groups.some((gr) => gr.id === e.to); // 묶음으로 들어가는 선은 안쪽 입구 점까지 이어짐 (화살촉 없음)
|
|
1172
|
+
s += `<path d="${d}" class="edge${skip ? " skip" : ""}${on ? " sel" : ""}"${into ? "" : ' marker-end="url(#fa)"'} data-from="${esc(e.from)}" data-to="${esc(e.to)}"${top(e)}/>` +
|
|
1173
|
+
`<path d="${d}" class="edge-hit" data-edge="${key}"${top(e)}/>`;
|
|
1174
|
+
}
|
|
1175
|
+
for (const en of L.entries) s += `<circle cx="${en.x + en.w / 2}" cy="${en.y + en.h / 2}" r="4" class="entry"${top(en)}><title>묶음의 입력</title></circle>`;
|
|
1176
|
+
for (const r of L.rows || []) { // 줄바꿈한 줄마다 손잡이: 끌면 그 줄을 흐름 방향(가로면 좌우)으로 옮김
|
|
1177
|
+
if (!r.anchor) continue;
|
|
1178
|
+
const hz = L.dir === "h", gw = hz ? 14 : 44, gh = hz ? 44 : 14, gx = hz ? r.x - 24 : r.x - 22, gy = hz ? r.y - 22 : r.y - 24;
|
|
1179
|
+
s += `<g class="row-grip" data-row="${r.k}" data-anchor="${esc(r.anchor)}" data-shift="${r.shift}">` +
|
|
1180
|
+
`<title>끌어서 ${hz ? "이 줄을 좌우" : "이 열을 위아래"}로 옮기기 (두 번 누르면 맨 ${hz ? "왼쪽" : "위"}으로)</title>` +
|
|
1181
|
+
`<rect x="${gx}" y="${gy}" width="${gw}" height="${gh}" rx="4"/>` +
|
|
1182
|
+
`<text x="${gx + gw / 2}" y="${gy + gh / 2}">${hz ? "⇔" : "⇕"}</text></g>`;
|
|
1183
|
+
}
|
|
1184
|
+
for (const bx of L.nodes) {
|
|
1185
|
+
if (L.groups.some((gr) => gr.id === bx.id)) continue;
|
|
1186
|
+
const n = findNode(graph, bx.id).node, on = sel.has(bx.id);
|
|
1187
|
+
// 아직 선을 안 이은 것뿐이면 오류가 아니라 '선을 이어 주세요' (회색 점선)
|
|
1188
|
+
const loose = issues[bx.id] && issues[bx.id].every((i) => LOOSE.some((w) => i.message.includes(w)));
|
|
1189
|
+
const bad = issues[bx.id] && (loose ? [{ message: "선을 이어 주세요" }] : issues[bx.id]);
|
|
1190
|
+
const T = TYPES[n.type] || {}, cls = `fb${on ? " sel" : ""}${loose ? " loose" : bad ? " bad" : ""}`;
|
|
1191
|
+
const head = `<g class="${cls}" data-node="${esc(bx.id)}"${top(bx)}>`, cx = bx.x + bx.w / 2, cy = bx.y + bx.h / 2;
|
|
1192
|
+
if (isMerge(n)) {
|
|
1193
|
+
s += head + `<title>${esc(bad ? bad[0].message : fmtShape(shapes[bx.id]))}</title>` +
|
|
1194
|
+
`<rect x="${bx.x}" y="${bx.y}" width="${bx.w}" height="${bx.h}" rx="15" class="box c-add"/>` +
|
|
1195
|
+
`<text x="${cx}" y="${cy}" class="t1">${esc(label(n))}${bad ? " ⚠" : ""}</text>` + port(bx) + `</g>`;
|
|
1196
|
+
continue;
|
|
1197
|
+
}
|
|
1198
|
+
if (n.type === "act") { // 활성화는 원
|
|
1199
|
+
s += head + `<title>${esc(label(n))}${bad ? " — " + esc(bad[0].message) : ""}</title>` +
|
|
1200
|
+
`<circle cx="${cx}" cy="${cy}" r="${bx.w / 2}" class="box c-purple"/>` +
|
|
1201
|
+
`<text x="${cx}" y="${cy - 7}" class="t1">${esc(shortLabel(n))}${bad ? " ⚠" : ""}</text>` +
|
|
1202
|
+
`<text x="${cx}" y="${cy + 10}" class="t3">${esc(fmtShape(shapes[bx.id]))}</text>` + port(bx) + `</g>`;
|
|
1203
|
+
continue;
|
|
1204
|
+
}
|
|
1205
|
+
const color = n.type === "input" ? "gray" : COLORS[T.cat] || "gray", narrow = bx.w < 190;
|
|
1206
|
+
const inShape = summary && fmtShape(summary.image ? [1].concat(summary.image) : [1, summary.length]);
|
|
1207
|
+
const subFull = n.type === "input"
|
|
1208
|
+
? (!summary ? "데이터를 먼저 불러오세요" : narrow ? inShape : `${inShape} channels × ${summary.image ? "height × width" : "length"}`)
|
|
1209
|
+
: bad ? bad[0].message : fmtShape(shapes[bx.id]);
|
|
1210
|
+
const lines = labelLines(n, summary, bx.w), sub = narrow ? fitText(subFull, bx.w) : subFull;
|
|
1211
|
+
const warn = bad && !loose ? " ⚠" : "";
|
|
1212
|
+
const text = lines.length === 1
|
|
1213
|
+
? `<text x="${cx}" y="${cy - 8}" class="t1">${esc(lines[0])}${warn}</text><text x="${cx}" y="${cy + 9}" class="t2">${esc(sub)}</text>`
|
|
1214
|
+
: `<text x="${cx}" y="${cy - 16}" class="t1">${esc(lines[0])}${warn}</text><text x="${cx}" y="${cy}" class="t2b">${esc(lines[1])}</text>` +
|
|
1215
|
+
`<text x="${cx}" y="${cy + 16}" class="t2">${esc(sub)}</text>`;
|
|
1216
|
+
const brkMark = n.brk ? `<text x="${bx.x + 8}" y="${bx.y + 11}" class="brk">↵</text>` : ""; // 여기서 줄바꿈
|
|
1217
|
+
s += head + `<title>${esc(label(n, summary))} — ${esc(subFull)}</title>` + brkMark +
|
|
1218
|
+
`<rect x="${bx.x}" y="${bx.y}" width="${bx.w}" height="${bx.h}" rx="10" class="box c-${color}"/>` + text + port(bx) + `</g>`;
|
|
1219
|
+
}
|
|
1220
|
+
for (const bx of L.nodes) { // 고른 상자 크기 손잡이 (끌면 고른 상자 모두 같은 크기로)
|
|
1221
|
+
if (!sel.has(bx.id) || L.groups.some((gr) => gr.id === bx.id)) continue;
|
|
1222
|
+
s += `<rect class="rsz" data-rsz="${esc(bx.id)}" x="${bx.x + bx.w - 7}" y="${bx.y + bx.h - 7}" width="11" height="11" rx="2"><title>끌어서 크기 바꾸기 (고른 상자 모두)</title></rect>`;
|
|
1223
|
+
}
|
|
1224
|
+
s += `<rect id="band" class="band" style="display:none"/><path id="drop-edge" class="drop" style="display:none" fill="none"/>` +
|
|
1225
|
+
`<line id="wire" class="wire" style="display:none"/><g id="snap"></g>`;
|
|
1226
|
+
els.canvas.innerHTML = s + "</svg>";
|
|
1227
|
+
drawSettings();
|
|
1228
|
+
if (opts.onHistory) opts.onHistory(hist.canUndo(), hist.canRedo());
|
|
1229
|
+
}
|
|
1230
|
+
function port(bx) { // 소켓: 위 = 입력, 아래 = 출력. 끌어서 다른 상자에 놓으면 선, 빈 곳에 놓으면 층 고르기
|
|
1231
|
+
const f = findNode(graph, bx.id), t = f ? f.node.type : "", a = bx.ax === undefined ? (L.dir === "h" ? bx.h : bx.w) / 2 : bx.ax;
|
|
1232
|
+
const [ix, iy, ox, oy] = L.dir === "h" ? [bx.x, bx.y + a, bx.x + bx.w, bx.y + a] : [bx.x + a, bx.y, bx.x + a, bx.y + bx.h];
|
|
1233
|
+
let s = "";
|
|
1234
|
+
if (t !== "input") s += `<circle cx="${ix}" cy="${iy}" r="5" class="port in" data-inport="${esc(bx.id)}"><title>입력: 끌어서 위 상자에 놓으면 선을 이음 (빈 곳에 놓으면 앞에 넣을 층을 고름)</title></circle>`;
|
|
1235
|
+
if (t !== "output") s += `<circle cx="${ox}" cy="${oy}" r="5" class="port" data-port="${esc(bx.id)}"><title>출력: 끌어서 아래 상자에 놓으면 선을 이음 (빈 곳에 놓으면 이어서 만들 층을 고름)</title></circle>`;
|
|
1236
|
+
return s;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
function field(lab, inp) { return `<label class="fld"><span>${lab}</span>${inp}</label>`; }
|
|
1240
|
+
function numberInput(k, v, min, max) { return `<input type="number" data-k="${k}" value="${esc(v)}" min="${min}" max="${max}" step="1">`; }
|
|
1241
|
+
function selectInput(k, v, options) {
|
|
1242
|
+
return `<select data-k="${k}">${options.map(([val, txt]) => `<option value="${val}"${String(v) === String(val) ? " selected" : ""}>${esc(txt)}</option>`).join("")}</select>`;
|
|
1243
|
+
}
|
|
1244
|
+
const KEYS = `<details class="keys"><summary>조작 방법</summary><p class="muted"><b>마우스만으로:</b> 왼쪽 목록에서 끌어 판에 놓기 (선 근처면 그 사이로) · 상자를 끌어 옮기기 (선 근처면 그 사이로, 여러 개 고른 채 끌면 함께) · ` +
|
|
1245
|
+
`상자 위·아래 동그라미를 끌어 다른 상자에 놓으면 선 (기존 선은 바뀜), 빈 곳에 놓으면 층을 골라 바로 이어서 만듦 · <b>오른쪽 클릭</b>: 복사·삭제·감싸기·정렬·줄바꿈(세로면 열바꿈)·선 지우기·층 넣기<br>` +
|
|
1246
|
+
`키보드도 됨: ` +
|
|
1247
|
+
`여러 개 고르기: 빈 곳에서 끌어서 네모, 또는 Shift+클릭 · Ctrl+C 복사 · Ctrl+X 잘라내기 · Ctrl+V 붙여넣기 · Ctrl+D 복제 · Delete 삭제 · Ctrl+Z 되돌리기 · Ctrl+Y 다시 하기 · Esc 선택 해제</p></details>`;
|
|
1248
|
+
// 처음 보는 사람에게 덜 중요한 칸은 접어 둠 (기본값이 아니면 펼침)
|
|
1249
|
+
const adv = (inner, open) => (inner ? `<details class="adv"${open ? " open" : ""}><summary>고급 설정</summary>${inner}</details>` : "");
|
|
1250
|
+
|
|
1251
|
+
function drawSettings() {
|
|
1252
|
+
const summary = opts.getSummary();
|
|
1253
|
+
if (selEdge) {
|
|
1254
|
+
const a = findNode(graph, selEdge.from), b = findNode(graph, selEdge.to);
|
|
1255
|
+
els.settings.innerHTML = `<h4>선</h4><p class="muted">${esc(a ? label(a.node, summary) : "묶음 입력")} → ${esc(b ? label(b.node, summary) : "")}</p>` +
|
|
1256
|
+
`<div class="acts"><button data-a="del-edge" class="danger">선 지우기</button></div>` + KEYS;
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
const ids = selIds();
|
|
1260
|
+
if (!ids.length) {
|
|
1261
|
+
els.settings.innerHTML = KEYS;
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
if (ids.length > 1) {
|
|
1265
|
+
const ok = !!singleEntryExit(graph, ids);
|
|
1266
|
+
els.settings.innerHTML = `<h4>${ids.length}개 선택</h4>` +
|
|
1267
|
+
(ok ? `<p class="muted">입구 하나·출구 하나인 묶음입니다</p>` : `<p class="muted">감싸기·복사는 입구 하나·출구 하나로 이어진 층들만 됩니다</p>`) +
|
|
1268
|
+
`<div class="acts"><button data-a="wrap-res"${ok ? "" : " disabled"}>Residual로 감싸기</button><button data-a="wrap-rep"${ok ? "" : " disabled"}>Repeat로 감싸기</button>` +
|
|
1269
|
+
`<button data-a="split"${ok ? "" : " disabled"}>갈래로 나누기</button><button data-a="copy"${ok ? "" : " disabled"}>복사</button>` +
|
|
1270
|
+
`<button data-a="dup"${ok ? "" : " disabled"}>복제</button><button data-a="del" class="danger">삭제</button></div>` + sizeFields(ids) + KEYS;
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1273
|
+
const b = findNode(graph, ids[0]).node, T = TYPES[b.type] || {};
|
|
1274
|
+
if (b.type === "input") {
|
|
1275
|
+
els.settings.innerHTML = `<h4>Input</h4><p class="muted">② 데이터 단계에서 불러온 데이터입니다${summary ? ` (length ${summary.length})` : ""}.</p>` + KEYS;
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
let f = "";
|
|
1279
|
+
if (b.type === "conv") f = field("Filters (채널 수: 많을수록 정확, 느림)", numberInput("channels", b.channels, 1, 256)) +
|
|
1280
|
+
field("Kernel size (한 번에 보는 칸 수)", selectInput("kernel", b.kernel, [1, 3, 5, 7, 9, 11, 15].map((v) => [v, v]))) +
|
|
1281
|
+
adv(field("Stride", selectInput("stride", b.stride || 1, [[1, "1"], [2, "2 (길이 절반)"]])) +
|
|
1282
|
+
field("Dilation (칸 사이를 띄워 넓게 봄, TCN)", selectInput("dilation", b.dilation || 1, [1, 2, 4, 8, 16, 32].map((v) => [v, v]))) +
|
|
1283
|
+
field("Padding", selectInput("padding", b.padding || "same", [["same", "same (길이 그대로)"], ["valid", "valid (가장자리만큼 줄어듦)"], ["causal", "causal (과거만 봄)"]])) +
|
|
1284
|
+
field("Groups (filters·들어오는 채널을 나누는 수, 같으면 depthwise)", numberInput("groups", b.groups || 1, 1, 256)),
|
|
1285
|
+
(b.stride || 1) !== 1 || (b.dilation || 1) !== 1 || (b.padding || "same") !== "same" || (b.groups || 1) !== 1);
|
|
1286
|
+
if (b.type === "pool") f = field("Pool size", selectInput("size", b.size, [[2, "2"], [4, "4"]]));
|
|
1287
|
+
if (b.type === "conv2d") f = field("Filters (채널 수: 많을수록 정확, 느림)", numberInput("channels", b.channels, 1, 256)) +
|
|
1288
|
+
field("Kernel size (한 번에 보는 네모 크기)", selectInput("kernel", b.kernel, [1, 3, 5, 7].map((v) => [v, `${v}×${v}`]))) +
|
|
1289
|
+
adv(field("Stride", selectInput("stride", b.stride || 1, [[1, "1"], [2, "2 (크기 절반)"]])) +
|
|
1290
|
+
field("Dilation (칸 사이를 띄워 넓게 봄)", selectInput("dilation", b.dilation || 1, [1, 2, 4, 8].map((v) => [v, v]))) +
|
|
1291
|
+
field("Groups (같으면 depthwise)", numberInput("groups", b.groups || 1, 1, 256)) +
|
|
1292
|
+
field("Padding", selectInput("padding", b.padding || "same", [["same", "same (크기 그대로)"], ["valid", "valid (가장자리만큼 줄어듦)"]])),
|
|
1293
|
+
(b.stride || 1) !== 1 || (b.padding || "same") !== "same" || (b.dilation || 1) !== 1 || (b.groups || 1) !== 1);
|
|
1294
|
+
if (b.type === "act") f = field("Function", selectInput("kind", b.kind, ACTS));
|
|
1295
|
+
if (b.type === "se") f = field("Ratio (줄이는 비율: 채널 ÷ ratio)", selectInput("ratio", b.ratio, [2, 4, 8, 16].map((v) => [v, v]))) +
|
|
1296
|
+
adv(field("안쪽 activation", selectInput("act", b.act || "relu", [["relu", "ReLU"], ["sigmoid", "Sigmoid"], ["tanh", "Tanh"]])), (b.act || "relu") !== "relu");
|
|
1297
|
+
if (b.type === "dropout") f = field("Rate (끄는 비율)", selectInput("rate", b.rate, [0.1, 0.2, 0.3, 0.5].map((v) => [v, v])));
|
|
1298
|
+
if (b.type === "dense") f = field("Units (출력 개수)", numberInput("units", b.units, 1, 1024));
|
|
1299
|
+
if (b.type === "lstm" || b.type === "gru" || b.type === "simple_rnn") f = field("Units (hidden size)", numberInput("hidden", b.hidden, 1, 256)) +
|
|
1300
|
+
field("Layers", numberInput("layers", b.layers || 1, 1, 3)) +
|
|
1301
|
+
field("Bidirectional (앞→뒤·뒤→앞 둘 다)", selectInput("bidirectional", !!b.bidirectional, [[false, "끔"], [true, "켬"]])) +
|
|
1302
|
+
field("Return sequences (줄 그대로 내보내기: 뒤에 순환·어텐션·Conv를 쌓을 때)", selectInput("return_sequences", !!b.return_sequences, [[false, "끔 (마지막 값만)"], [true, "켬"]]));
|
|
1303
|
+
if (FIELDS[b.type]) {
|
|
1304
|
+
const d = TYPES[b.type] ? TYPES[b.type].make() : {};
|
|
1305
|
+
f = FIELDS[b.type].map(([kind, k, lab, a1, a2]) => field(lab, kind === "num" ? numberInput(k, b[k] ?? d[k], a1, a2) : selectInput(k, b[k] ?? d[k], a1))).join("");
|
|
1306
|
+
}
|
|
1307
|
+
if (TWO_INPUT.includes(b.type)) f += `<div class="fld"><span>들어오는 선 (첫째 = 질문, 둘째 = 참고)</span><ol class="ins">${insOf(b).map((x, i) => {
|
|
1308
|
+
const src = findNode(graph, x);
|
|
1309
|
+
return `<li>${esc(src ? label(src.node, summary) : x)} <span class="muted">${esc(fmtShape(shapes[x]))}</span> <button data-a="in-del" data-i="${i}" title="이 선 지우기">×</button></li>`;
|
|
1310
|
+
}).join("")}</ol></div>`;
|
|
1311
|
+
if (b.type === "repeat") f = field("Repeat count", numberInput("times", b.times, 1, 10));
|
|
1312
|
+
if (isMerge(b)) {
|
|
1313
|
+
f = field("합치는 방법", selectInput("type", b.type, [["add", "Add (+): 더하기, 모양이 같아야 함"], ["concat", "Concat: 채널로 이어 붙이기"],
|
|
1314
|
+
["multiply", "Multiply (×): 원소마다 곱하기"], ["average", "Average: 원소마다 평균"], ["maximum", "Maximum: 원소마다 큰 값"]])) +
|
|
1315
|
+
`<div class="fld"><span>들어오는 선 (Concat은 이 순서로 붙임)</span><ol class="ins">${insOf(b).map((x, i) => {
|
|
1316
|
+
const src = findNode(graph, x);
|
|
1317
|
+
return `<li>${esc(src ? label(src.node, summary) : x === "in" ? "묶음 입력" : x)} <span class="muted">${esc(fmtShape(shapes[x]))}</span> <button data-a="in-del" data-i="${i}" title="이 선 지우기">×</button></li>`;
|
|
1318
|
+
}).join("")}</ol></div>`;
|
|
1319
|
+
}
|
|
1320
|
+
const isOutput = b.type === "output";
|
|
1321
|
+
const look = opts.onPreview ? `<div class="acts"><button data-a="preview" title="학습 샘플 하나가 이 층을 지나면 어떤 값·모양이 되는지 오른쪽 아래에 보여 줌">미리보기: 샘플이 이 층을 지나면</button></div>` : "";
|
|
1322
|
+
const btns = look + (isOutput ? "" : `<div class="acts"><button data-a="copy">복사</button><button data-a="dup">복제</button>`) + (isOutput ? "" :
|
|
1323
|
+
`<button data-a="del" class="danger">삭제</button>` + (b.type === "repeat" ? `<button data-a="unwrap">묶음 풀기</button>` : "") +
|
|
1324
|
+
`<button data-a="wrap-res">Residual로 감싸기</button><button data-a="wrap-rep">Repeat로 감싸기</button><button data-a="split">갈래로 나누기</button></div>`);
|
|
1325
|
+
const bad = issues[b.id] || [];
|
|
1326
|
+
els.settings.innerHTML = `<h4>${esc(label(b, summary))}</h4><p class="muted">${esc(T.desc || (b.type === "repeat" ? "안의 층을 여러 번 쌓음 (가중치는 따로)" : ""))}</p>${f}${btns}` +
|
|
1327
|
+
sizeFields(ids) +
|
|
1328
|
+
bad.map((i) => `<p class="err">${esc(i.message)}<br>→ ${esc(i.hint)}</p>`).join("") +
|
|
1329
|
+
(isOutput ? `<p class="muted">분류·회귀는 ① 과제에서 고릅니다. 출력 개수는 데이터에 맞춰 정해집니다.</p>` : "") + KEYS;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function sizeFields(ids) { // 상자 크기 (보기용): 고른 상자 모두에 한 번에
|
|
1333
|
+
const boxes = ids.map((id) => L.nodes.find((n) => n.id === id)).filter((n) => n && !L.groups.some((gr) => gr.id === n.id));
|
|
1334
|
+
if (!boxes.length) return "";
|
|
1335
|
+
return `<details class="adv size-fld"><summary>상자 크기 (보기용${boxes.length > 1 ? ` · ${boxes.length}개 모두` : ""})</summary>` +
|
|
1336
|
+
`<div class="row"><label>가로 <input type="number" data-size="w" value="${esc(Math.round(boxes[0].w))}" min="${SIZE_MIN[0]}" max="${SIZE_MAX[0]}" step="10"></label>` +
|
|
1337
|
+
`<label>세로 <input type="number" data-size="h" value="${esc(Math.round(boxes[0].h))}" min="${SIZE_MIN[1]}" max="${SIZE_MAX[1]}" step="5"></label>` +
|
|
1338
|
+
`<button data-a="size-reset">기본 크기로</button></div><span class="muted">고른 상자 오른쪽 아래 네모를 끌어도 됨</span></details>`;
|
|
1339
|
+
}
|
|
1340
|
+
let openCat = null, search = ""; // 처음에는 모든 범주를 접어 둠
|
|
1341
|
+
function packNote(p, kind) {
|
|
1342
|
+
if (kind === "image" && !p.ranks.includes(3)) return "줄(시계열) 모양에 씀 — 그림은 ToSequence 뒤에";
|
|
1343
|
+
if (kind !== "image" && !p.ranks.includes(2) && !p.ranks.includes(1)) return "그림 모양에 씀";
|
|
1344
|
+
return "";
|
|
1345
|
+
}
|
|
1346
|
+
function itemHtml(id, cat, name, desc, note) {
|
|
1347
|
+
return `<div class="pitem p-${cat}${note ? " off" : ""}" data-t="${esc(id)}" title="끌어서 판 위에 놓기 (선 근처면 그 선 사이로)">` +
|
|
1348
|
+
`<b>${esc(name)}</b><span>${esc(desc)}</span>${note ? `<em class="fit">${esc(note)}</em>` : ""}</div>`;
|
|
1349
|
+
}
|
|
1350
|
+
function drawPalette() {
|
|
1351
|
+
if (!els.palette.querySelector(".psearch")) {
|
|
1352
|
+
els.palette.innerHTML = `<input class="psearch" type="search" placeholder="블록 찾기 (예: attention, LSTM, norm)" aria-label="블록 찾기"><div class="plist"></div>`;
|
|
1353
|
+
const box = els.palette.querySelector(".psearch");
|
|
1354
|
+
box.addEventListener("input", () => { search = box.value.trim().toLowerCase(); drawPalette(); });
|
|
1355
|
+
box.addEventListener("mousedown", (e) => e.stopPropagation());
|
|
1356
|
+
}
|
|
1357
|
+
const kind = input(), all = paletteTypes(kind), list = els.palette.querySelector(".plist");
|
|
1358
|
+
const items = (cat) => cat === "pack"
|
|
1359
|
+
? PACKS.map((p) => ["pack:" + p.id, "pack", p.name, p.desc, packNote(p, kind)])
|
|
1360
|
+
: all.filter((id) => PALETTE[id].cat === cat).map((id) => [id, cat, PALETTE[id].name, PALETTE[id].desc, fitNote(makeOf(id).type, kind)]);
|
|
1361
|
+
let h = "";
|
|
1362
|
+
if (search) {
|
|
1363
|
+
const hits = CATS.flatMap(([cat, cname]) => items(cat).filter((it) => (it[2] + " " + it[3] + " " + cname).toLowerCase().includes(search)));
|
|
1364
|
+
h = hits.length ? hits.map((it) => itemHtml(...it)).join("") : `<p class="muted">'${esc(search)}'에 맞는 블록이 없습니다</p>`;
|
|
1365
|
+
} else {
|
|
1366
|
+
for (const [cat, name] of CATS) {
|
|
1367
|
+
const its = items(cat), on = openCat === cat;
|
|
1368
|
+
h += `<button type="button" class="pcat-btn p-${cat}${on ? " on" : ""}" data-cat="${cat}" aria-expanded="${on}"><span>${esc(name)}</span><em>${its.length}</em></button>`;
|
|
1369
|
+
if (on) h += `<div class="pitems">${its.map((it) => itemHtml(...it)).join("")}</div>`;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
list.innerHTML = h;
|
|
1373
|
+
}
|
|
1374
|
+
els.palette.addEventListener("click", (e) => {
|
|
1375
|
+
const b = e.target.closest("[data-cat]");
|
|
1376
|
+
if (!b) return;
|
|
1377
|
+
openCat = openCat === b.dataset.cat ? null : b.dataset.cat;
|
|
1378
|
+
drawPalette();
|
|
1379
|
+
});
|
|
1380
|
+
const paletteName = (id) => (id.startsWith("pack:") ? (PACKS.find((p) => "pack:" + p.id === id) || {}).name : PALETTE[id].name);
|
|
1381
|
+
const rankAt = (id) => (id === "input" || id === "in" ? (shapes[id] || []).length || (input() === "image" ? 3 : 2) : (shapes[id] || []).length || 0);
|
|
1382
|
+
function dropPack(id, edge) { // 묶음 넣기: 선 위면 그 사이, 아니면 Output 바로 앞
|
|
1383
|
+
let from, to;
|
|
1384
|
+
if (edge) { from = edge.from; to = edge.to; } else {
|
|
1385
|
+
const out = outputNode();
|
|
1386
|
+
if (!out || !insOf(out).length) return hint("묶음은 선 위에 놓으세요");
|
|
1387
|
+
from = insOf(out)[0]; to = out.id;
|
|
1388
|
+
}
|
|
1389
|
+
const sh = from === "input" ? (opts.getSummary() ? (opts.getSummary().image ? [1].concat(opts.getSummary().image) : [1, opts.getSummary().length]) : null) : shapes[from];
|
|
1390
|
+
const pk = PACKS.find((p) => "pack:" + p.id === id), rank = sh ? sh.length : (input() === "image" ? 3 : 2);
|
|
1391
|
+
let pack;
|
|
1392
|
+
try { pack = makePack(pk.id, sh ? sh[0] : 16, rank, sh && sh.length === 2 ? sh[1] : 0); } catch (err) { return hint(String(err.message || err)); }
|
|
1393
|
+
const r = insertPack(graph, from, to, pack);
|
|
1394
|
+
change(r.graph, r.ids);
|
|
1395
|
+
hint(`'${pk.name}'을(를) 넣었습니다${edge ? "" : " (Output 바로 앞)"}. 상자를 눌러 설정을 바꿀 수 있습니다`);
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// ---------------- 편집 동작
|
|
1399
|
+
function removeSel() {
|
|
1400
|
+
if (selEdge) return change(removeEdge(graph, selEdge.from, selEdge.to), []);
|
|
1401
|
+
let g = graph;
|
|
1402
|
+
for (const id of selIds()) g = removeNode(g, id);
|
|
1403
|
+
if (g === graph) return hint("Input·Output은 지울 수 없습니다");
|
|
1404
|
+
change(g, []);
|
|
1405
|
+
}
|
|
1406
|
+
function copySel(cut) {
|
|
1407
|
+
const ids = selIds().filter((id) => { const n = findNode(graph, id).node; return n.type !== "input" && n.type !== "output"; });
|
|
1408
|
+
if (!ids.length) return hint("복사할 층을 먼저 고르세요");
|
|
1409
|
+
const c = copyNodes(graph, ids);
|
|
1410
|
+
if (!c) return hint("입구 하나·출구 하나로 이어진 층들만 복사할 수 있습니다");
|
|
1411
|
+
clip = c;
|
|
1412
|
+
if (cut) { let g = graph; for (const id of ids) g = removeNode(g, id); change(g, []); }
|
|
1413
|
+
hint(`${ids.length}개 ${cut ? "잘라냄" : "복사함"} · Ctrl+V로 붙여넣기 (고른 상자 바로 뒤)`);
|
|
1414
|
+
}
|
|
1415
|
+
function paste(c) {
|
|
1416
|
+
if (!c) return hint("붙여넣을 것이 없습니다 (먼저 Ctrl+C)");
|
|
1417
|
+
const ids = selIds();
|
|
1418
|
+
const s = ids.length > 1 ? singleEntryExit(graph, ids) : null;
|
|
1419
|
+
const after = s ? s.exit : ids.length === 1 ? ids[0] : (insOf(outputNode())[0] || "input");
|
|
1420
|
+
const r = pasteAfter(graph, after, c);
|
|
1421
|
+
if (!r.ids.length) return hint("출력 뒤에는 붙일 수 없습니다");
|
|
1422
|
+
change(r.graph, r.ids);
|
|
1423
|
+
}
|
|
1424
|
+
function act(a, extra) {
|
|
1425
|
+
const ids = selIds();
|
|
1426
|
+
if (a === "preview") return opts.onPreview && opts.onPreview(ids[0]);
|
|
1427
|
+
if (a === "del-edge" || a === "del") return removeSel();
|
|
1428
|
+
if (a === "copy") return copySel(false);
|
|
1429
|
+
if (a === "dup") { const c = copyNodes(graph, ids); return c ? paste(c) : hint("입구 하나·출구 하나로 이어진 층들만 복제할 수 있습니다"); }
|
|
1430
|
+
if (a === "unwrap") return change(unwrap(graph, ids[0]), []);
|
|
1431
|
+
if (a === "size-reset") return change(setSize(graph, ids, null), ids);
|
|
1432
|
+
if (a === "in-del") { const n = findNode(graph, ids[0]).node; return change(removeEdge(graph, insOf(n)[Number(extra.i)], n.id)); }
|
|
1433
|
+
if (!singleEntryExit(graph, ids)) return hint("입구 하나·출구 하나로 이어진 층들만 감쌀 수 있습니다");
|
|
1434
|
+
if (a === "wrap-res") return change(wrapResidual(graph, ids, Object.assign({ input: summaryShape() }, shapes)), []);
|
|
1435
|
+
if (a === "wrap-rep") return change(wrapRepeat(graph, ids), []);
|
|
1436
|
+
if (a === "split") return change(splitBranch(graph, ids, forInput({ type: "conv", channels: 8, kernel: 5, stride: 1 }, input())), []);
|
|
1437
|
+
}
|
|
1438
|
+
function summaryShape() {
|
|
1439
|
+
const sm = opts.getSummary();
|
|
1440
|
+
return sm ? (sm.image ? [1].concat(sm.image) : [1, sm.length]) : null;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
// ---------------- 마우스로 끌기: 왼쪽 목록 → 판, 상자 옮기기, 선 잇기, 네모 고르기
|
|
1444
|
+
// 선 근처(48px)에 놓으면 그 선 사이로 들어가고, 아니면 놓은 자리에 둠 (판 위 자유 배치)
|
|
1445
|
+
function svgPoint(e) {
|
|
1446
|
+
const svg = els.canvas.querySelector("svg");
|
|
1447
|
+
if (!svg || !svg.getScreenCTM()) return null;
|
|
1448
|
+
const pt = svg.createSVGPoint();
|
|
1449
|
+
pt.x = e.clientX;
|
|
1450
|
+
pt.y = e.clientY;
|
|
1451
|
+
const q = pt.matrixTransform(svg.getScreenCTM().inverse());
|
|
1452
|
+
return [q.x, q.y];
|
|
1453
|
+
}
|
|
1454
|
+
const overCanvas = (e) => {
|
|
1455
|
+
const r = els.canvas.getBoundingClientRect();
|
|
1456
|
+
return e.clientX >= r.left && e.clientX <= r.right && e.clientY >= r.top && e.clientY <= r.bottom;
|
|
1457
|
+
};
|
|
1458
|
+
function showDrop(edge) {
|
|
1459
|
+
dropEdge = edge;
|
|
1460
|
+
const p = els.canvas.querySelector("#drop-edge");
|
|
1461
|
+
if (!p) return;
|
|
1462
|
+
if (!edge) { p.style.display = "none"; return; }
|
|
1463
|
+
p.style.display = "";
|
|
1464
|
+
p.setAttribute("d", edge.points.map((q, i) => `${i ? "L" : "M"}${q[0]} ${q[1]}`).join(" "));
|
|
1465
|
+
}
|
|
1466
|
+
let drag = null, band = null, wire = null, skipClick = false, ghost = null;
|
|
1467
|
+
function ignoreNextClick() { // 끌기 직후 브라우저가 보내는 click 하나만 버림 (안 오면 바로 풀림)
|
|
1468
|
+
skipClick = true;
|
|
1469
|
+
setTimeout(() => { skipClick = false; }, 0);
|
|
1470
|
+
}
|
|
1471
|
+
function movedGraph(d, p) { // 끈 상자들을 판 위 그 자리로 옮긴 그래프 (희망 위치)
|
|
1472
|
+
let g = graph;
|
|
1473
|
+
for (const id of d.ids) {
|
|
1474
|
+
const bx = L.nodes.find((n) => n.id === id);
|
|
1475
|
+
if (bx) g = setPos(g, id, [bx.x + p[0] - d.x0 - bx.ox, bx.y + p[1] - d.y0 - bx.oy]);
|
|
1476
|
+
}
|
|
1477
|
+
return g;
|
|
1478
|
+
}
|
|
1479
|
+
function showSnap(g, ids) { // 놓으면 실제로 들어갈 자리 (겹치지 않게 맞춘 뒤)
|
|
1480
|
+
const el = els.canvas.querySelector("#snap");
|
|
1481
|
+
if (!el) return;
|
|
1482
|
+
if (!g) { el.innerHTML = ""; return; }
|
|
1483
|
+
const L2 = layoutGraph(g, undefined, lopts());
|
|
1484
|
+
el.innerHTML = L2.nodes.filter((n) => ids.includes(n.id))
|
|
1485
|
+
.map((n) => `<rect x="${n.x}" y="${n.y}" width="${n.w}" height="${n.h}" rx="${n.w === n.h ? n.w / 2 : 10}" class="snap"/>`).join("");
|
|
1486
|
+
}
|
|
1487
|
+
function setGhost(text, e) {
|
|
1488
|
+
if (!ghost) { ghost = document.createElement("div"); ghost.className = "drag-ghost"; document.body.appendChild(ghost); }
|
|
1489
|
+
ghost.textContent = text;
|
|
1490
|
+
ghost.style.left = `${e.clientX + 14}px`;
|
|
1491
|
+
ghost.style.top = `${e.clientY + 10}px`;
|
|
1492
|
+
}
|
|
1493
|
+
function clearGhost() { if (ghost) ghost.remove(); ghost = null; els.canvas.classList.remove("dragging"); }
|
|
1494
|
+
|
|
1495
|
+
els.canvas.addEventListener("dblclick", (e) => { // 줄 손잡이 두 번: 그 줄을 맨 앞(왼쪽·위)으로
|
|
1496
|
+
const rg = e.target.closest("[data-row]");
|
|
1497
|
+
if (rg && Number(rg.dataset.shift) > 0) change(setShift(graph, rg.dataset.anchor, 0));
|
|
1498
|
+
});
|
|
1499
|
+
els.palette.addEventListener("mousedown", (e) => {
|
|
1500
|
+
const it = e.target.closest("[data-t]");
|
|
1501
|
+
if (!it || e.button !== 0) return;
|
|
1502
|
+
drag = { kind: "palette", type: it.dataset.t, el: it, x0: e.clientX, y0: e.clientY, moved: false };
|
|
1503
|
+
e.preventDefault(); // 글자 고르기 막기
|
|
1504
|
+
});
|
|
1505
|
+
els.canvas.addEventListener("mousedown", (e) => {
|
|
1506
|
+
if (e.button !== 0) return;
|
|
1507
|
+
const p = svgPoint(e);
|
|
1508
|
+
const rz = e.target.closest("[data-rsz]");
|
|
1509
|
+
if (rz && p) {
|
|
1510
|
+
const bx = L.nodes.find((n) => n.id === rz.dataset.rsz);
|
|
1511
|
+
drag = { kind: "size", id: bx.id, x0: p[0], y0: p[1], bx, ids: selIds().filter((id) => !L.groups.some((gr) => gr.id === id)) };
|
|
1512
|
+
e.preventDefault();
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
const rg = e.target.closest("[data-row]");
|
|
1516
|
+
if (rg && p) {
|
|
1517
|
+
drag = { kind: "row", anchor: rg.dataset.anchor, shift0: Number(rg.dataset.shift) || 0, x0: p[0], y0: p[1], base: graph, moved: false };
|
|
1518
|
+
e.preventDefault();
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
const ip = e.target.closest("[data-inport]");
|
|
1522
|
+
if (ip && p) { wire = { to: ip.dataset.inport, x0: p[0], y0: p[1] }; e.preventDefault(); return; }
|
|
1523
|
+
const pt = e.target.closest("[data-port]");
|
|
1524
|
+
if (pt && p) { wire = { from: pt.dataset.port, x0: p[0], y0: p[1] }; e.preventDefault(); return; }
|
|
1525
|
+
const g = e.target.closest("[data-node]");
|
|
1526
|
+
if (g && p) {
|
|
1527
|
+
const id = g.dataset.node, isGroup = g.classList.contains("fg");
|
|
1528
|
+
if (isGroup && !e.target.closest(".grp-head, .glabel")) return; // 묶음은 머리로만 끌기 (안쪽 빈 곳은 네모 고르기 없음)
|
|
1529
|
+
const ids = sel.has(id) && sel.size > 1 ? selIds() : [id];
|
|
1530
|
+
drag = { kind: "node", id, ids, x0: p[0], y0: p[1], moved: false };
|
|
1531
|
+
e.preventDefault();
|
|
1532
|
+
return;
|
|
1533
|
+
}
|
|
1534
|
+
if (e.target.closest("[data-edge]")) return;
|
|
1535
|
+
if (p) band = { x0: p[0], y0: p[1], x1: p[0], y1: p[1], moved: false };
|
|
1536
|
+
});
|
|
1537
|
+
document.addEventListener("mousemove", (e) => {
|
|
1538
|
+
if (drag && drag.kind === "palette") {
|
|
1539
|
+
drag.moved = drag.moved || Math.abs(e.clientX - drag.x0) + Math.abs(e.clientY - drag.y0) > 4;
|
|
1540
|
+
if (!drag.moved) return;
|
|
1541
|
+
els.canvas.classList.add("dragging");
|
|
1542
|
+
const p = overCanvas(e) && L ? svgPoint(e) : null;
|
|
1543
|
+
const edge = p ? nearestEdge(L.edges, p) : null;
|
|
1544
|
+
if (edge !== dropEdge) showDrop(edge);
|
|
1545
|
+
const pk = drag.type.startsWith("pack:");
|
|
1546
|
+
setGhost(`${paletteName(drag.type)} — ${!p ? "판 위에 놓으세요" : edge ? "이 선 사이에 넣기" : pk ? "Output 바로 앞에 넣기" : "여기에 두기 (선은 나중에 이음)"}`, e);
|
|
1547
|
+
return;
|
|
1548
|
+
}
|
|
1549
|
+
if (drag && drag.kind === "row") { // 줄 옮기기: 놓기 전까지는 보기만 (되돌리기 기록은 놓을 때 한 번)
|
|
1550
|
+
const p = svgPoint(e);
|
|
1551
|
+
if (!p) return;
|
|
1552
|
+
const d = flowDir === "h" ? p[0] - drag.x0 : p[1] - drag.y0;
|
|
1553
|
+
drag.moved = drag.moved || Math.abs(d) > 3;
|
|
1554
|
+
if (!drag.moved) return;
|
|
1555
|
+
drag.g = setShift(drag.base, drag.anchor, drag.shift0 + d);
|
|
1556
|
+
graph = drag.g;
|
|
1557
|
+
draw();
|
|
1558
|
+
graph = drag.base;
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
if (drag && drag.kind === "size") {
|
|
1562
|
+
const p = svgPoint(e), el = els.canvas.querySelector("#snap");
|
|
1563
|
+
if (!p || !el) return;
|
|
1564
|
+
const w = Math.max(SIZE_MIN[0], drag.bx.w + p[0] - drag.x0), h = Math.max(SIZE_MIN[1], drag.bx.h + p[1] - drag.y0);
|
|
1565
|
+
drag.size = [w, h];
|
|
1566
|
+
el.innerHTML = `<rect x="${drag.bx.x}" y="${drag.bx.y}" width="${w}" height="${h}" rx="10" class="snap"/>`;
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
if (drag && drag.kind === "node") {
|
|
1570
|
+
const p = svgPoint(e);
|
|
1571
|
+
if (!p) return;
|
|
1572
|
+
const dx = p[0] - drag.x0, dy = p[1] - drag.y0;
|
|
1573
|
+
drag.moved = drag.moved || Math.abs(dx) + Math.abs(dy) > 5;
|
|
1574
|
+
if (!drag.moved) return;
|
|
1575
|
+
const id = drag.id, ids = drag.ids;
|
|
1576
|
+
const hit = (el) => ids.includes(el.dataset.node) || (el.dataset.anc || "").split(" ").some((a) => ids.includes(a));
|
|
1577
|
+
for (const el of els.canvas.querySelectorAll("[data-node], [data-anc]")) el.setAttribute("transform", hit(el) ? `translate(${dx} ${dy})` : "");
|
|
1578
|
+
const n = findNode(graph, id).node, fixed = ids.length > 1 || n.type === "input" || n.type === "output"; // 여럿이면 자리만 옮김
|
|
1579
|
+
const edge = fixed ? null : nearestEdge(L.edges, p, SNAP, (ed) => ed.from === id || ed.to === id || (ed.anc || []).includes(id));
|
|
1580
|
+
if (edge !== dropEdge) showDrop(edge);
|
|
1581
|
+
showSnap(edge ? null : movedGraph(drag, p), ids);
|
|
1582
|
+
return;
|
|
1583
|
+
}
|
|
1584
|
+
if (wire) {
|
|
1585
|
+
const p = svgPoint(e), w = els.canvas.querySelector("#wire");
|
|
1586
|
+
if (!p || !w) return;
|
|
1587
|
+
w.style.display = "";
|
|
1588
|
+
w.setAttribute("x1", wire.x0); w.setAttribute("y1", wire.y0); w.setAttribute("x2", p[0]); w.setAttribute("y2", p[1]);
|
|
1589
|
+
return;
|
|
1590
|
+
}
|
|
1591
|
+
if (!band) return;
|
|
1592
|
+
const p = svgPoint(e), r = els.canvas.querySelector("#band");
|
|
1593
|
+
if (!p || !r) return;
|
|
1594
|
+
band.x1 = p[0]; band.y1 = p[1];
|
|
1595
|
+
band.moved = band.moved || Math.abs(band.x1 - band.x0) + Math.abs(band.y1 - band.y0) > 6;
|
|
1596
|
+
if (!band.moved) return;
|
|
1597
|
+
r.style.display = "";
|
|
1598
|
+
r.setAttribute("x", Math.min(band.x0, band.x1)); r.setAttribute("y", Math.min(band.y0, band.y1));
|
|
1599
|
+
r.setAttribute("width", Math.abs(band.x1 - band.x0)); r.setAttribute("height", Math.abs(band.y1 - band.y0));
|
|
1600
|
+
});
|
|
1601
|
+
document.addEventListener("mouseup", (e) => {
|
|
1602
|
+
if (drag && drag.kind === "palette") {
|
|
1603
|
+
const d = drag, edge = dropEdge;
|
|
1604
|
+
drag = null;
|
|
1605
|
+
clearGhost();
|
|
1606
|
+
showDrop(null);
|
|
1607
|
+
if (!d.moved) { // 누르기만 함: 안내
|
|
1608
|
+
d.el.classList.add("nudge");
|
|
1609
|
+
setTimeout(() => d.el.classList.remove("nudge"), 600);
|
|
1610
|
+
return hint("층은 끌어서 판 위에 놓으세요 (선 근처면 그 선 사이로 들어감)");
|
|
1611
|
+
}
|
|
1612
|
+
const p = overCanvas(e) ? svgPoint(e) : null;
|
|
1613
|
+
if (!p) return;
|
|
1614
|
+
if (d.type.startsWith("pack:")) return dropPack(d.type, edge);
|
|
1615
|
+
const node = forInput(makeOf(d.type), input(), edge ? rankAt(edge.from) : 0), nid = newId(graph);
|
|
1616
|
+
if (edge) {
|
|
1617
|
+
change(insertOnEdge(graph, edge.from, edge.to, Object.assign({ id: nid }, node)), [nid]);
|
|
1618
|
+
if (isMerge(node)) hint("합치기 상자는 입력이 2개 이상이어야 합니다: 다른 상자의 동그라미를 끌어 이 상자에 놓으세요");
|
|
1619
|
+
else if (maxIns(node) === 2) hint("입력이 둘인 상자입니다: 참고할 다른 상자의 동그라미를 끌어 이 상자에 놓으세요 (첫째 = 질문, 둘째 = 참고)");
|
|
1620
|
+
else if (/^rand_/.test(node.type)) hint("증강 블록은 Input 바로 아래에 두세요 (학습 때만 그림·신호를 바꿈)");
|
|
1621
|
+
} else {
|
|
1622
|
+
const [w, h] = sizeOfDefault(node, flowDir), o = L.nodes.find((n) => !n.anc.length) || { ox: 0, oy: 0 };
|
|
1623
|
+
change(addFree(graph, Object.assign({ id: nid }, node), [p[0] - w / 2 - o.ox, p[1] - h / 2 - o.oy]), [nid]);
|
|
1624
|
+
hint("아직 이어지지 않은 상자입니다: 위 상자 아래의 동그라미를 끌어 이 상자에 놓으세요");
|
|
1625
|
+
}
|
|
1626
|
+
return;
|
|
1627
|
+
}
|
|
1628
|
+
if (drag && drag.kind === "row") {
|
|
1629
|
+
const d = drag;
|
|
1630
|
+
drag = null;
|
|
1631
|
+
ignoreNextClick();
|
|
1632
|
+
if (!d.moved || !d.g) return; // 누르기만 함 (두 번 누르기가 같은 손잡이에 가도록 다시 그리지 않음)
|
|
1633
|
+
return change(d.g);
|
|
1634
|
+
}
|
|
1635
|
+
if (drag && drag.kind === "size") {
|
|
1636
|
+
const d = drag;
|
|
1637
|
+
drag = null;
|
|
1638
|
+
ignoreNextClick();
|
|
1639
|
+
if (!d.size) return draw();
|
|
1640
|
+
return change(setSize(graph, d.ids, d.size), d.ids);
|
|
1641
|
+
}
|
|
1642
|
+
if (drag && drag.kind === "node") {
|
|
1643
|
+
const d = drag, edge = dropEdge;
|
|
1644
|
+
drag = null;
|
|
1645
|
+
showDrop(null);
|
|
1646
|
+
if (!d.moved) return; // 누르기만 함: click에서 고름
|
|
1647
|
+
ignoreNextClick();
|
|
1648
|
+
const p = svgPoint(e);
|
|
1649
|
+
if (edge) {
|
|
1650
|
+
const r = moveNode(graph, d.id, edge.from, edge.to);
|
|
1651
|
+
if (r.error) { hint(r.error); return draw(); }
|
|
1652
|
+
return change(r.graph, [d.id]);
|
|
1653
|
+
}
|
|
1654
|
+
if (!p) return draw();
|
|
1655
|
+
return change(movedGraph(d, p), d.ids); // 판 위 그 자리로 (겹치면 옆으로, 세로는 층에 맞춤)
|
|
1656
|
+
}
|
|
1657
|
+
if (wire) {
|
|
1658
|
+
const w = wire, out = w.from !== undefined;
|
|
1659
|
+
wire = null;
|
|
1660
|
+
ignoreNextClick();
|
|
1661
|
+
const target = e.target.closest && e.target.closest("[data-node]");
|
|
1662
|
+
const ww = els.canvas.querySelector("#wire");
|
|
1663
|
+
if (ww) ww.style.display = "none";
|
|
1664
|
+
const p = svgPoint(e);
|
|
1665
|
+
if (target) { // 상자에 놓음: 선 잇기 (보통 층은 기존 선을 바꿈)
|
|
1666
|
+
const tid = target.dataset.node;
|
|
1667
|
+
if (tid === (out ? w.from : w.to)) return;
|
|
1668
|
+
const r = out ? connect(graph, w.from, tid) : connect(graph, tid, w.to);
|
|
1669
|
+
if (r.error) return hint(r.error);
|
|
1670
|
+
return change(r.graph, [out ? tid : w.to]);
|
|
1671
|
+
}
|
|
1672
|
+
if (!p || !overCanvas(e) || Math.hypot(p[0] - w.x0, p[1] - w.y0) < 12) return;
|
|
1673
|
+
layerMenu(e, (t) => { // 빈 곳에 놓음: 층을 골라 바로 이어서 만듦
|
|
1674
|
+
const node = forInput(makeOf(t), input(), out ? rankAt(w.from) : 0), [nw, nh] = sizeOfDefault(node, flowDir);
|
|
1675
|
+
const ref = L.nodes.find((n) => n.id === (out ? w.from : w.to)) || { ox: 0, oy: 0 };
|
|
1676
|
+
const pos = [p[0] - nw / 2 - ref.ox, p[1] - nh / 2 - ref.oy];
|
|
1677
|
+
const r = out ? addConnected(graph, w.from, node, pos) : addFeeding(graph, w.to, node, pos);
|
|
1678
|
+
if (r.id) change(r.graph, [r.id]);
|
|
1679
|
+
});
|
|
1680
|
+
return;
|
|
1681
|
+
}
|
|
1682
|
+
if (!band) return;
|
|
1683
|
+
const b = band;
|
|
1684
|
+
band = null;
|
|
1685
|
+
const r = els.canvas.querySelector("#band");
|
|
1686
|
+
if (r) r.style.display = "none";
|
|
1687
|
+
if (!b.moved) return;
|
|
1688
|
+
ignoreNextClick(); // 끈 뒤 따라오는 click이 선택을 지우지 않게
|
|
1689
|
+
const x0 = Math.min(b.x0, b.x1), x1 = Math.max(b.x0, b.x1), y0 = Math.min(b.y0, b.y1), y1 = Math.max(b.y0, b.y1);
|
|
1690
|
+
sel = new Set(L.nodes.filter((n) => n.x >= x0 && n.y >= y0 && n.x + n.w <= x1 && n.y + n.h <= y1).map((n) => n.id)
|
|
1691
|
+
.filter((id) => { const n = findNode(graph, id); return n && n.node.type !== "input" && n.node.type !== "output"; }));
|
|
1692
|
+
// 묶음째 고른 경우 안쪽 노드는 빼기
|
|
1693
|
+
for (const gr of L.groups) if (sel.has(gr.id)) for (const n of L.nodes) if (n.inGroup === gr.id) sel.delete(n.id);
|
|
1694
|
+
selEdge = null;
|
|
1695
|
+
draw();
|
|
1696
|
+
});
|
|
1697
|
+
els.canvas.addEventListener("click", (e) => {
|
|
1698
|
+
if (skipClick) { skipClick = false; return; }
|
|
1699
|
+
const ed = e.target.closest("[data-edge]");
|
|
1700
|
+
if (ed) { const [from, to] = ed.dataset.edge.split(">"); sel = new Set(); selEdge = { from, to }; return draw(); }
|
|
1701
|
+
const g = e.target.closest("[data-node]");
|
|
1702
|
+
if (!g) return select(null);
|
|
1703
|
+
const id = g.dataset.node;
|
|
1704
|
+
if (e.shiftKey) { if (sel.has(id)) sel.delete(id); else sel.add(id); selEdge = null; return draw(); }
|
|
1705
|
+
select(id);
|
|
1706
|
+
});
|
|
1707
|
+
// ---------------- 오른쪽 클릭 메뉴, 층 고르기 메뉴 (키보드 없이)
|
|
1708
|
+
const menu = document.createElement("div");
|
|
1709
|
+
menu.id = "ctx-menu";
|
|
1710
|
+
menu.hidden = true;
|
|
1711
|
+
document.body.appendChild(menu);
|
|
1712
|
+
let menuItems = [];
|
|
1713
|
+
function openMenu(e, items, cls = "") {
|
|
1714
|
+
menuItems = items;
|
|
1715
|
+
menu.className = `ctx-menu ${cls}`;
|
|
1716
|
+
menu.innerHTML = items.map((it, i) => (it.sep ? "<hr>" : it.title ? `<div class="ctx-title">${esc(it.title)}</div>`
|
|
1717
|
+
: `<button data-i="${i}"${it.disabled ? " disabled" : ""}${it.danger ? ' class="danger"' : ""}>${esc(it.label)}</button>`)).join("");
|
|
1718
|
+
menu.hidden = false;
|
|
1719
|
+
menu.style.left = `${Math.max(4, Math.min(e.clientX, innerWidth - menu.offsetWidth - 8))}px`;
|
|
1720
|
+
menu.style.top = `${Math.max(4, Math.min(e.clientY, innerHeight - menu.offsetHeight - 8))}px`;
|
|
1721
|
+
}
|
|
1722
|
+
const closeMenu = () => { menu.hidden = true; };
|
|
1723
|
+
menu.addEventListener("click", (ev) => {
|
|
1724
|
+
const b = ev.target.closest("[data-i]");
|
|
1725
|
+
if (!b || b.disabled) return;
|
|
1726
|
+
const it = menuItems[Number(b.dataset.i)];
|
|
1727
|
+
closeMenu();
|
|
1728
|
+
it.fn();
|
|
1729
|
+
});
|
|
1730
|
+
document.addEventListener("mousedown", (ev) => { if (!menu.hidden && !menu.contains(ev.target)) closeMenu(); }, true);
|
|
1731
|
+
document.addEventListener("keydown", (ev) => { if (ev.key === "Escape") closeMenu(); });
|
|
1732
|
+
els.canvas.addEventListener("scroll", closeMenu);
|
|
1733
|
+
function layerMenu(e, pick) {
|
|
1734
|
+
const kind = input(), all = paletteTypes(kind), items = [];
|
|
1735
|
+
for (const [cat, name] of CATS) {
|
|
1736
|
+
const ts = all.filter((t) => PALETTE[t].cat === cat);
|
|
1737
|
+
if (!ts.length) continue;
|
|
1738
|
+
items.push({ title: name });
|
|
1739
|
+
for (const t of ts) items.push({ label: PALETTE[t].name + (fitNote(makeOf(t).type, kind) ? " · 잘 안 맞음" : ""), fn: () => pick(t) });
|
|
1740
|
+
}
|
|
1741
|
+
openMenu(e, items, "layers");
|
|
1742
|
+
}
|
|
1743
|
+
els.canvas.addEventListener("contextmenu", (e) => {
|
|
1744
|
+
e.preventDefault();
|
|
1745
|
+
const p = svgPoint(e);
|
|
1746
|
+
if (!p) return;
|
|
1747
|
+
const g = e.target.closest("[data-node]"), ed = e.target.closest("[data-edge]");
|
|
1748
|
+
if (g) {
|
|
1749
|
+
const id = g.dataset.node, n = findNode(graph, id).node, fixed = n.type === "input" || n.type === "output";
|
|
1750
|
+
if (!sel.has(id)) { sel = new Set([id]); selEdge = null; draw(); }
|
|
1751
|
+
const many = selIds().length > 1;
|
|
1752
|
+
return openMenu(e, [
|
|
1753
|
+
{ title: many ? `${selIds().length}개 고름` : label(n, opts.getSummary()) },
|
|
1754
|
+
{ label: many ? "위 상자와 가운데 맞추기 (고른 것 모두)" : "위 상자와 가운데 맞추기", fn: () => autoLayout(), disabled: n.type === "input" },
|
|
1755
|
+
...(!n.anc && L.nodes.some((x) => x.id === id && !x.anc.length) && n.type !== "input"
|
|
1756
|
+
? [{ label: flowDir === "h" ? (n.brk ? "줄바꿈 풀기" : "여기서 줄바꿈 (아래 줄로)") : (n.brk ? "열바꿈 풀기" : "여기서 열바꿈 (오른쪽 열로)"),
|
|
1757
|
+
fn: () => change(setBreak(graph, id, !n.brk), [id]) }] : []),
|
|
1758
|
+
{ label: "복사", fn: () => copySel(false), disabled: fixed },
|
|
1759
|
+
{ label: "복제 (바로 뒤에)", fn: () => act("dup"), disabled: fixed },
|
|
1760
|
+
{ label: "이 뒤에 붙여넣기", fn: () => paste(clip), disabled: !clip || n.type === "output" },
|
|
1761
|
+
{ sep: true },
|
|
1762
|
+
{ label: "Residual로 감싸기", fn: () => act("wrap-res"), disabled: fixed },
|
|
1763
|
+
{ label: "Repeat로 감싸기", fn: () => act("wrap-rep"), disabled: fixed },
|
|
1764
|
+
{ label: "갈래로 나누기", fn: () => act("split"), disabled: fixed || input() === "tabular" },
|
|
1765
|
+
...(n.type === "repeat" ? [{ label: "묶음 풀기", fn: () => act("unwrap") }] : []),
|
|
1766
|
+
{ sep: true },
|
|
1767
|
+
{ label: "삭제", fn: () => removeSel(), danger: true, disabled: fixed },
|
|
1768
|
+
]);
|
|
1769
|
+
}
|
|
1770
|
+
if (ed) {
|
|
1771
|
+
const [from, to] = ed.dataset.edge.split(">");
|
|
1772
|
+
sel = new Set(); selEdge = { from, to }; draw();
|
|
1773
|
+
return openMenu(e, [
|
|
1774
|
+
{ title: "선" },
|
|
1775
|
+
{ label: "이 선에 층 넣기…", fn: () => layerMenu(e, (t) => {
|
|
1776
|
+
const nid = newId(graph);
|
|
1777
|
+
change(insertOnEdge(graph, from, to, Object.assign({ id: nid }, forInput(makeOf(t), input(), rankAt(from)))), [nid]);
|
|
1778
|
+
}) },
|
|
1779
|
+
{ label: "선 지우기", fn: () => change(removeEdge(graph, from, to), []), danger: true },
|
|
1780
|
+
]);
|
|
1781
|
+
}
|
|
1782
|
+
select(null);
|
|
1783
|
+
openMenu(e, [
|
|
1784
|
+
{ title: "판" },
|
|
1785
|
+
{ label: "여기에 층 추가…", fn: () => layerMenu(e, (t) => {
|
|
1786
|
+
const node = forInput(makeOf(t), input()), [nw, nh] = sizeOfDefault(node, flowDir), o = L.nodes.find((n) => !n.anc.length) || { ox: 0, oy: 0 };
|
|
1787
|
+
const nid = newId(graph);
|
|
1788
|
+
change(addFree(graph, Object.assign({ id: nid }, node), [p[0] - nw / 2 - o.ox, p[1] - nh / 2 - o.oy]), [nid]);
|
|
1789
|
+
}) },
|
|
1790
|
+
{ label: "붙여넣기", fn: () => paste(clip), disabled: !clip },
|
|
1791
|
+
{ label: "전체 자동 정렬", fn: () => autoLayout() },
|
|
1792
|
+
]);
|
|
1793
|
+
});
|
|
1794
|
+
|
|
1795
|
+
els.settings.addEventListener("change", (e) => {
|
|
1796
|
+
if (e.target.dataset.size) { // 크기 칸: 고른 상자 모두
|
|
1797
|
+
const w = Number(els.settings.querySelector('[data-size="w"]').value), h = Number(els.settings.querySelector('[data-size="h"]').value);
|
|
1798
|
+
return change(setSize(graph, selIds(), [w, h]), selIds());
|
|
1799
|
+
}
|
|
1800
|
+
const k = e.target.dataset.k, ids = selIds();
|
|
1801
|
+
if (!k || ids.length !== 1) return;
|
|
1802
|
+
const raw = e.target.value;
|
|
1803
|
+
const v = raw === "true" ? true : raw === "false" ? false : raw !== "" && !isNaN(Number(raw)) ? Number(raw) : raw; // same·relu 같은 글자 값은 그대로
|
|
1804
|
+
change(setParam(graph, ids[0], k, v));
|
|
1805
|
+
});
|
|
1806
|
+
els.settings.addEventListener("click", (e) => {
|
|
1807
|
+
const a = e.target.dataset.a;
|
|
1808
|
+
if (a) act(a, e.target.dataset);
|
|
1809
|
+
});
|
|
1810
|
+
document.addEventListener("keydown", (e) => {
|
|
1811
|
+
if (!els.canvas.offsetParent || /INPUT|SELECT|TEXTAREA/.test(document.activeElement.tagName)) return; // 모델 화면에서만
|
|
1812
|
+
const ctrl = e.ctrlKey || e.metaKey, k = e.key.toLowerCase();
|
|
1813
|
+
let done = true;
|
|
1814
|
+
if ((e.key === "Delete" || e.key === "Backspace") && (sel.size || selEdge)) removeSel();
|
|
1815
|
+
else if (ctrl && k === "z" && !e.shiftKey) { if (!undo()) hint("더 되돌릴 것이 없습니다"); }
|
|
1816
|
+
else if (ctrl && (k === "y" || (k === "z" && e.shiftKey))) { if (!redo()) hint("다시 할 것이 없습니다"); }
|
|
1817
|
+
else if (ctrl && k === "c") copySel(false);
|
|
1818
|
+
else if (ctrl && k === "x") copySel(true);
|
|
1819
|
+
else if (ctrl && k === "v") paste(clip);
|
|
1820
|
+
else if (ctrl && k === "d") act("dup");
|
|
1821
|
+
else if (ctrl && k === "a") { sel = new Set(nodesOf(graph).filter((n) => n.type !== "input" && n.type !== "output").map((n) => n.id)); selEdge = null; draw(); }
|
|
1822
|
+
else if (e.key === "Escape") select(null);
|
|
1823
|
+
else done = false;
|
|
1824
|
+
if (done) e.preventDefault();
|
|
1825
|
+
});
|
|
1826
|
+
// ---------------- 판 확대·축소: Ctrl(⌘)+휠·트랙패드 집기·두 손가락 집기는 판에서만 (페이지는 그대로), 단추·Ctrl+0/+/−
|
|
1827
|
+
const ZMIN = 0.2, ZMAX = 2;
|
|
1828
|
+
function setZoom(z, at) { // at: 화면 좌표 [x, y] — 그 점 아래의 판 위치가 그대로 있게
|
|
1829
|
+
z = Math.min(ZMAX, Math.max(ZMIN, Math.round(z * 100) / 100));
|
|
1830
|
+
if (z === zoom) return;
|
|
1831
|
+
const r = els.canvas.getBoundingClientRect(), cx = at ? at[0] : r.left + r.width / 2, cy = at ? at[1] : r.top + r.height / 2;
|
|
1832
|
+
const p = svgPoint({ clientX: cx, clientY: cy });
|
|
1833
|
+
zoom = z;
|
|
1834
|
+
draw();
|
|
1835
|
+
const svg = els.canvas.querySelector("svg");
|
|
1836
|
+
if (p && svg) {
|
|
1837
|
+
const b = svg.getBoundingClientRect();
|
|
1838
|
+
els.canvas.scrollLeft += b.left + p[0] * zoom - cx;
|
|
1839
|
+
els.canvas.scrollTop += b.top + p[1] * zoom - cy;
|
|
1840
|
+
}
|
|
1841
|
+
if (opts.onZoom) opts.onZoom(zoom);
|
|
1842
|
+
}
|
|
1843
|
+
function fitZoom() { // 모델 전체가 보이게 (100%보다 키우지는 않음)
|
|
1844
|
+
if (!L) return;
|
|
1845
|
+
const z = Math.min(1, (els.canvas.clientWidth - 40) / (L.width + 40), (els.canvas.clientHeight - 40) / (L.height + 40));
|
|
1846
|
+
zoom = Math.min(ZMAX, Math.max(ZMIN, Math.floor(z * 100) / 100));
|
|
1847
|
+
draw();
|
|
1848
|
+
els.canvas.scrollLeft = 0;
|
|
1849
|
+
els.canvas.scrollTop = 0;
|
|
1850
|
+
if (opts.onZoom) opts.onZoom(zoom);
|
|
1851
|
+
}
|
|
1852
|
+
els.canvas.addEventListener("wheel", (e) => {
|
|
1853
|
+
if (!(e.ctrlKey || e.metaKey)) return; // 그냥 휠은 판 스크롤
|
|
1854
|
+
e.preventDefault(); // 브라우저 확대 대신 판만
|
|
1855
|
+
setZoom(zoom * Math.exp(-Math.max(-60, Math.min(60, e.deltaY)) / 300), [e.clientX, e.clientY]);
|
|
1856
|
+
}, { passive: false });
|
|
1857
|
+
let pinch = null; // 터치: 두 손가락 집기
|
|
1858
|
+
const tdist = (t) => Math.hypot(t[0].clientX - t[1].clientX, t[0].clientY - t[1].clientY);
|
|
1859
|
+
els.canvas.addEventListener("touchstart", (e) => { if (e.touches.length === 2) pinch = { d: tdist(e.touches), z: zoom }; }, { passive: true });
|
|
1860
|
+
els.canvas.addEventListener("touchmove", (e) => {
|
|
1861
|
+
if (!pinch || e.touches.length !== 2) return;
|
|
1862
|
+
e.preventDefault();
|
|
1863
|
+
const t = e.touches;
|
|
1864
|
+
setZoom(pinch.z * tdist(t) / pinch.d, [(t[0].clientX + t[1].clientX) / 2, (t[0].clientY + t[1].clientY) / 2]);
|
|
1865
|
+
}, { passive: false });
|
|
1866
|
+
els.canvas.addEventListener("touchend", (e) => { if (e.touches.length < 2) pinch = null; });
|
|
1867
|
+
document.addEventListener("keydown", (e) => {
|
|
1868
|
+
if (!els.canvas.offsetParent || !(e.ctrlKey || e.metaKey) || /INPUT|SELECT|TEXTAREA/.test(document.activeElement.tagName)) return;
|
|
1869
|
+
if (e.key === "=" || e.key === "+") setZoom(zoom * 1.2);
|
|
1870
|
+
else if (e.key === "-" || e.key === "_") setZoom(zoom / 1.2);
|
|
1871
|
+
else if (e.key === "0") setZoom(1);
|
|
1872
|
+
else return;
|
|
1873
|
+
e.preventDefault();
|
|
1874
|
+
});
|
|
1875
|
+
|
|
1876
|
+
drawPalette();
|
|
1877
|
+
draw();
|
|
1878
|
+
function autoLayout() { // 고른 상자가 있으면 바로 위 상자와 가운데 맞춤, 없으면 전체 자동 정렬
|
|
1879
|
+
const ids = selIds().filter((id) => findNode(graph, id).node.type !== "input");
|
|
1880
|
+
if (ids.length) { change(alignToParent(graph, ids, lopts()), ids); return hint(`고른 ${ids.length}개를 바로 ${flowDir === "h" ? "왼쪽" : "위"} 상자와 가운데 맞췄습니다`); }
|
|
1881
|
+
change(clearPos(graph));
|
|
1882
|
+
hint("자동 정렬했습니다 (판 위에 옮겨 둔 위치를 지움). 상자를 고르고 누르면 그 상자만 위 상자와 가운데 맞춤");
|
|
1883
|
+
}
|
|
1884
|
+
function setEdgeStyle(v) { edgeStyle = v === "curve" ? "curve" : "ortho"; draw(); }
|
|
1885
|
+
function setWrap(on) { autoWrap = !!on; draw(); }
|
|
1886
|
+
function setFlowDir(v, fromUser) { // 방향을 바꾸면 옮겨 둔 위치는 지움 (축이 바뀌어 뜻이 달라짐, 되돌리기 됨)
|
|
1887
|
+
flowDir = v === "h" ? "h" : "v";
|
|
1888
|
+
let had = false;
|
|
1889
|
+
eachNode(nodesOf(graph), (n) => { if (n.pos) had = true; });
|
|
1890
|
+
if (fromUser && had) { change(clearPos(graph)); hint("방향을 바꿔 옮겨 둔 위치를 지웠습니다 (되돌리기로 살릴 수 있음)"); }
|
|
1891
|
+
else draw();
|
|
1892
|
+
}
|
|
1893
|
+
window.addEventListener("resize", () => { if (els.canvas.offsetParent) draw(); });
|
|
1894
|
+
return { set, get, decorate, select, redraw: draw, redrawPalette: drawPalette, undo, redo, autoLayout, setEdgeStyle, setFlowDir, setWrap,
|
|
1895
|
+
zoomIn: () => setZoom(zoom * 1.2), zoomOut: () => setZoom(zoom / 1.2), zoomReset: () => setZoom(1), zoomFit: fitZoom, getZoom: () => zoom };
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
const API = { PALETTE, PACKS, makePack, insertPack, RANK, mount, fmtShape, forInput, labelLines, setBreak, setShift, alignToParent, edgePath, addConnected, addFeeding, setSize, stripView, setPos, clearPos, stripPos, addFree, moveNode, shortLabel, nearestEdge, TYPES, ACT_NAMES, CATS, paletteTypes, fitNote, label, layoutGraph, insertOnEdge, connect, removeNode, removeEdge,
|
|
1899
|
+
singleEntryExit, wrapResidual, wrapRepeat, unwrap, splitBranch, copyNodes, pasteAfter, setParam, newId, findNode, history, clone };
|
|
1900
|
+
if (typeof module !== "undefined") module.exports = API;
|
|
1901
|
+
if (typeof window !== "undefined") window.StudioGraph = API;
|
|
1902
|
+
})();
|