|
| 1 | +# TopOpt |
| 2 | + |
| 3 | +<a href="https://aistudio.baidu.com/projectdetail/6956236" class="md-button md-button--primary" style>AI Studio快速体验</a> |
| 4 | + |
| 5 | +=== "模型训练命令" |
| 6 | + |
| 7 | + ``` sh |
| 8 | + # linux |
| 9 | + wget https://paddle-org.bj.bcebos.com/paddlescience/datasets/topopt/top_dataset.h5 -P ./datasets/ |
| 10 | + # windows |
| 11 | + # curl https://paddle-org.bj.bcebos.com/paddlescience/datasets/topopt/top_dataset.h5 --output ./datasets/top_dataset.h5 |
| 12 | + python topopt.py |
| 13 | + ``` |
| 14 | + |
| 15 | +=== "模型评估命令" |
| 16 | + |
| 17 | + ``` sh |
| 18 | + # linux |
| 19 | + wget https://paddle-org.bj.bcebos.com/paddlescience/datasets/topopt/top_dataset.h5 -P ./datasets/ |
| 20 | + # windows |
| 21 | + # curl https://paddle-org.bj.bcebos.com/paddlescience/datasets/topopt/top_dataset.h5 --output ./datasets/top_dataset.h5 |
| 22 | + python topopt.py 'mode=eval' 'EVAL.pretrained_model_path_dict={"Uniform": "https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/uniform_pretrained.pdparams", "Poisson5": "https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/poisson5_pretrained.pdparams", "Poisson10": "https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/poisson10_pretrained.pdparams", "Poisson30": "https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/poisson30_pretrained.pdparams"}' |
| 23 | + ``` |
| 24 | + |
| 25 | +## 1. 背景简介 |
| 26 | + |
| 27 | +拓扑优化 (Topolgy Optimization) 是一种数学方法,针对给定的一组负载、边界条件和约束,在给定的设计区域内,以最大化系统性能为目标优化材料的分布。这个问题很有挑战性因为它要求解决方案是二元的,即应该说明设计区域的每个部分是否存在材料或不存在。这种优化的一个常见例子是在给定总重量和边界条件下最小化物体的弹性应变能。随着20世纪汽车和航空航天工业的发展,拓扑优化已经将应用扩展到很多其他学科:如流体、声学、电磁学、光学及其组合。SIMP (Simplied Isotropic Material with Penalization) 是目前广泛传播的一种简单而高效的拓扑优化求解方法。它通过对材料密度的中间值进行惩罚,提高了二元解的收敛性。 |
| 28 | + |
| 29 | + |
| 30 | +## 2. 问题定义 |
| 31 | + |
| 32 | +拓扑优化问题: |
| 33 | + |
| 34 | +$$ |
| 35 | +\begin{aligned} |
| 36 | +& \underset{\mathbf{x}}{\text{min}} \quad && c(\mathbf{u}(\mathbf{x}), \mathbf{x}) = \sum_{j=1}^{N} E_{j}(x_{j})\mathbf{u}_{j}^{\intercal}\mathbf{k}_{0}\mathbf{u}_{j} \\ |
| 37 | +& \text{s.t.} \quad && V(\mathbf{x})/V_{0} = f_{0} \\ |
| 38 | +& \quad && \mathbf{K}\mathbf{U} = \mathbf{F} \\ |
| 39 | +& \quad && x_{j} \in \{0, 1\}, \quad j = 1,...,N |
| 40 | +\end{aligned} |
| 41 | +$$ |
| 42 | + |
| 43 | +其中:$x_{j}$ 是材料分布 (material distribution);$c$ 指可塑性 (compliance);$\mathbf{u}_{j}$ 是 element displacement vector;$\mathbf{k}_{0}$ 是 element stiffness matrix for an element with unit Youngs modulu;$\mathbf{U}$, $\mathbf{F}$ 是 global displacement and force vectors;$\mathbf{K}$ 是 global stiffness matrix;$V(\mathbf{x})$, $V_{0}$ 是材料体积和设计区域的体积;$f_{0}$ 是预先指定的体积比。 |
| 44 | + |
| 45 | +## 3. 问题求解 |
| 46 | + |
| 47 | +实际求解上述问题时为做简化,会把最后一个约束条件换成连续的形式:$x_{j} \in [0, 1], \quad j = 1,...,N$。 常见的优化算法是 SIMP 算法,它是一种基于梯度的迭代法,并对非二元解做惩罚:$E_{j}(x_{j}) = E_{\text{min}} + x_{j}^{p}(E_{0} - E_{\text{min}})$,这里我们不对 SIMP 算法做过多展开。由于利用 SIMP 方法, 求解器只需要进行初始的 $N_{0}$ 次迭代就可以得到与结果的最终结果非常相近的基本视图,本案例希望通过将 SIMP 的第 $N_{0}$ 次初始迭代结果与其对应的梯度信息作为 Unet 的输入,预测 SIMP 的100次迭代步骤后给出的优化解。 |
| 48 | + |
| 49 | +### 3.1 数据集准备 |
| 50 | + |
| 51 | +下载的数据集为整理过的合成数据,整理后的格式为 `"iters": shape = (10000, 100, 40, 40)`,`"target": shape = (10000, 1, 40, 40)` |
| 52 | + |
| 53 | +- 10000 - 随机生成问题的个数 |
| 54 | + |
| 55 | +- 100 - SIMP 迭代次数 |
| 56 | + |
| 57 | +- 40 - 图像高度 |
| 58 | + |
| 59 | +- 40 - 图像宽度 |
| 60 | + |
| 61 | +数据集地址请存储于 `./datasets/top_dataset.h5` |
| 62 | + |
| 63 | +生成训练集:原始代码利用所有的10000问题生成训练数据。 |
| 64 | + |
| 65 | +``` py linenums="68" |
| 66 | +--8<-- |
| 67 | +examples/topopt/functions.py:68:101 |
| 68 | +--8<-- |
| 69 | +``` |
| 70 | + |
| 71 | +``` py linenums="37" |
| 72 | +--8<-- |
| 73 | +examples/topopt/topopt.py:37:46 |
| 74 | +--8<-- |
| 75 | +``` |
| 76 | + |
| 77 | +### 3.2 模型构建 |
| 78 | + |
| 79 | +经过 SIMP 的 $N_{0}$ 次初始迭代步骤得到的图像 $I$ 可以看作是模糊了的最终结构。由于最终的优化解给出的图像 $I^*$ 并不包含中间过程的信息,因此 $I^*$ 可以被解释为图像 $I$ 的掩码。于是 $I \rightarrow I^*$ 这一优化过程可以看作是二分类的图像分割或者前景-背景分割过程,因此构建 Unet 模型进行预测,具体网络结构如图所示: |
| 80 | + |
| 81 | + |
| 82 | +``` py linenums="87" |
| 83 | +--8<-- |
| 84 | +examples/topopt/topopt.py:87:89 |
| 85 | +--8<-- |
| 86 | +``` |
| 87 | + |
| 88 | +详细的模型代码在 `examples/topopt/topoptmodel.py` 中。 |
| 89 | + |
| 90 | + |
| 91 | +### 3.3 参数设定 |
| 92 | + |
| 93 | +根据论文以及原始代码给出以下训练参数: |
| 94 | + |
| 95 | +``` yaml linenums="49" |
| 96 | +--8<-- |
| 97 | +examples/topopt/conf/topopt.yaml:49:54 |
| 98 | +--8<-- |
| 99 | +``` |
| 100 | + |
| 101 | +``` py linenums="33" |
| 102 | +--8<-- |
| 103 | +examples/topopt/topopt.py:33:36 |
| 104 | +--8<-- |
| 105 | +``` |
| 106 | + |
| 107 | + |
| 108 | +### 3.4 data transform |
| 109 | + |
| 110 | +根据论文以及原始代码给出以下自定义的 data transform 代码,包括随机水平或垂直翻转和随机90度旋转,对 input 和 label 同时 transform: |
| 111 | + |
| 112 | +``` py linenums="102" |
| 113 | +--8<-- |
| 114 | +examples/topopt/functions.py:102:133 |
| 115 | +--8<-- |
| 116 | +``` |
| 117 | + |
| 118 | + |
| 119 | +### 3.5 约束构建 |
| 120 | + |
| 121 | +在本案例中,我们采用监督学习方式进行训练,所以使用监督约束 `SupervisedConstraint`,代码如下: |
| 122 | + |
| 123 | +``` py linenums="47" |
| 124 | +--8<-- |
| 125 | +examples/topopt/topopt.py:47:73 |
| 126 | +--8<-- |
| 127 | +``` |
| 128 | + |
| 129 | +`SupervisedConstraint` 的第一个参数是监督约束的读取配置,配置中 `"dataset"` 字段表示使用的训练数据集信息,其各个字段分别表示: |
| 130 | + |
| 131 | +1. `name`: 数据集类型,此处 `"NamedArrayDataset"` 表示分 batch 顺序读取的 `np.ndarray` 类型的数据集; |
| 132 | +2. `input`: 输入变量字典:`{"input_name": input_dataset}`; |
| 133 | +3. `label`: 标签变量字典:`{"label_name": label_dataset}`; |
| 134 | +4. `transforms`: 数据集预处理配,其中 `"FunctionalTransform"` 为用户自定义的预处理方式。 |
| 135 | + |
| 136 | +读取配置中 `"batch_size"` 字段表示训练时指定的批大小,`"sampler"` 字段表示 dataloader 的相关采样配置。 |
| 137 | + |
| 138 | +第二个参数是损失函数,这里使用[自定义损失](#381),通过 `cfg.vol_coeff` 确定损失公式中 $\beta$ 对应的值。 |
| 139 | + |
| 140 | +第三个参数是约束条件的名字,方便后续对其索引。此次命名为 `"sup_constraint"`。 |
| 141 | + |
| 142 | +在约束构建完毕之后,以我们刚才的命名为关键字,封装到一个字典中,方便后续访问。 |
| 143 | + |
| 144 | + |
| 145 | +### 3.6 采样器构建 |
| 146 | + |
| 147 | +原始数据第二维有100个通道,对应的是 SIMP 算法 100 次的迭代结果,本案例模型目标是用 SIMP 中间某一步的迭代结果直接预测 SIMP 算法100步迭代后最终的优化求解结果,这里需要构建一个通道采样器,用来将输入模型数据的第二维按一定的概率分布随机抽取某一通道或直接指定某一通道,再输入网络进行训练或推理。本案例将采样步骤放入模型的 forward 方法中。 |
| 148 | + |
| 149 | +``` py linenums="23" |
| 150 | +--8<-- |
| 151 | +examples/topopt/functions.py:23:67 |
| 152 | +--8<-- |
| 153 | +``` |
| 154 | + |
| 155 | +``` py linenums="77" |
| 156 | +--8<-- |
| 157 | +examples/topopt/topopt.py:77:79 |
| 158 | +--8<-- |
| 159 | +``` |
| 160 | + |
| 161 | +### 3.7 优化器构建 |
| 162 | + |
| 163 | +训练过程会调用优化器来更新模型参数,此处选择 `Adam` 优化器。 |
| 164 | + |
| 165 | +``` py linenums="90" |
| 166 | +--8<-- |
| 167 | +examples/topopt/topopt.py:90:94 |
| 168 | +--8<-- |
| 169 | +``` |
| 170 | + |
| 171 | +### 3.8 loss和metric构建 |
| 172 | + |
| 173 | +#### 3.8.1 loss构建 |
| 174 | +损失函数为 confidence loss + beta * volume fraction constraints: |
| 175 | + |
| 176 | +$$ |
| 177 | +\mathcal{L} = \mathcal{L}_{\text{conf}}(X_{\text{true}}, X_{\text{pred}}) + \beta * \mathcal{L}_{\text{vol}}(X_{\text{true}}, X_{\text{pred}}) |
| 178 | +$$ |
| 179 | + |
| 180 | +confidence loss 是 binary cross-entropy: |
| 181 | + |
| 182 | +$$ |
| 183 | +\mathcal{L}_{\text{conf}}(X_{\text{true}}, X_{\text{pred}}) = -\frac{1}{NM}\sum_{i=1}^{N}\sum_{j=1}^{M}\left[X_{\text{true}}^{ij}\log(X_{\text{pred}}^{ij}) + (1 - X_{\text{true}}^{ij})\log(1 - X_{\text{pred}}^{ij})\right] |
| 184 | +$$ |
| 185 | + |
| 186 | +volume fraction constraints: |
| 187 | + |
| 188 | +$$ |
| 189 | +\mathcal{L}_{\text{vol}}(X_{\text{true}}, X_{\text{pred}}) = (\bar{X}_{\text{pred}} - \bar{X}_{\text{true}})^2 |
| 190 | +$$ |
| 191 | + |
| 192 | +loss 构建代码如下: |
| 193 | + |
| 194 | +``` py linenums="260" |
| 195 | +--8<-- |
| 196 | +examples/topopt/topopt.py:260:273 |
| 197 | +--8<-- |
| 198 | +``` |
| 199 | + |
| 200 | +#### 3.8.2 metric构建 |
| 201 | +本案例原始代码选择 Binary Accuracy 和 IoU 进行评估: |
| 202 | + |
| 203 | +$$ |
| 204 | +\text{Bin. Acc.} = \frac{w_{00}+w_{11}}{n_{0}+n_{1}} |
| 205 | +$$ |
| 206 | + |
| 207 | +$$ |
| 208 | +\text{IoU} = \frac{1}{2}\left[\frac{w_{00}}{n_{0}+w_{10}} + \frac{w_{11}}{n_{1}+w_{01}}\right] |
| 209 | +$$ |
| 210 | + |
| 211 | +其中 $n_{0} = w_{00} + w_{01}$ , $n_{1} = w_{10} + w_{11}$ ,$w_{tp}$ 表示实际是 $t$ 类且被预测为 $p$ 类的像素点的数量 |
| 212 | +metric 构建代码如下: |
| 213 | + |
| 214 | +``` py linenums="274" |
| 215 | +--8<-- |
| 216 | +examples/topopt/topopt.py:274:316 |
| 217 | +--8<-- |
| 218 | +``` |
| 219 | + |
| 220 | + |
| 221 | +### 3.9 模型训练 |
| 222 | + |
| 223 | +本案例根据采样器的不同选择共有四组子案例,案例参数如下: |
| 224 | + |
| 225 | +``` yaml linenums="29" |
| 226 | +--8<-- |
| 227 | +examples/topopt/conf/topopt.yaml:29:31 |
| 228 | +--8<-- |
| 229 | +``` |
| 230 | + |
| 231 | +训练代码如下: |
| 232 | + |
| 233 | +``` py linenums="74" |
| 234 | +--8<-- |
| 235 | +examples/topopt/topopt.py:74:110 |
| 236 | +--8<-- |
| 237 | +``` |
| 238 | + |
| 239 | + |
| 240 | +### 3.10 评估模型 |
| 241 | + |
| 242 | +对四个训练好的模型,分别使用不同的通道采样器 (原始数据的第二维对应表示的是 SIMP 算法的 100 步输出结果,统一取原始数据第二维的第 5,10,15,20,...,80 通道以及对应的梯度信息作为新的输入构建评估数据集) 进行评估,每次评估时只取 `cfg.EVAL.num_val_step` 个 bacth 的数据,计算它们的平均 Binary Accuracy 和 IoU 指标;同时评估结果需要与输入数据本身的阈值判定结果 (0.5作为阈值) 作比较。具体代码请参考[完整代码](#4) |
| 243 | + |
| 244 | + |
| 245 | +#### 3.10.1 评估器构建 |
| 246 | +为应用 PaddleScience API,此处在每一次评估时构建一个评估器 SupervisedValidator 进行评估: |
| 247 | + |
| 248 | +``` py linenums="215" |
| 249 | +--8<-- |
| 250 | +examples/topopt/topopt.py:215:242 |
| 251 | +--8<-- |
| 252 | +``` |
| 253 | + |
| 254 | +评估器配置与 [约束构建](#35) 的设置类似,读取配置中 `"num_workers":0` 表示单线程读取;评价指标 `"metric"` 为自定义评估指标,包含 Binary Accuracy 和 IoU。 |
| 255 | + |
| 256 | + |
| 257 | +### 3.11 评估结果可视化 |
| 258 | + |
| 259 | +使用 `ppsci.utils.misc.plot_curve()` 方法直接绘制 Binary Accuracy 和 IoU 的结果: |
| 260 | + |
| 261 | +``` py linenums="182" |
| 262 | +--8<-- |
| 263 | +examples/topopt/topopt.py:182:192 |
| 264 | +--8<-- |
| 265 | +``` |
| 266 | + |
| 267 | + |
| 268 | +## 4. 完整代码 |
| 269 | + |
| 270 | +``` py linenums="1" title="topopt.py" |
| 271 | +--8<-- |
| 272 | +examples/topopt/topopt.py |
| 273 | +--8<-- |
| 274 | +``` |
| 275 | + |
| 276 | +``` py linenums="1" title="functions.py" |
| 277 | +--8<-- |
| 278 | +examples/topopt/functions.py |
| 279 | +--8<-- |
| 280 | +``` |
| 281 | + |
| 282 | +``` py linenums="1" title="topoptmodel.py" |
| 283 | +--8<-- |
| 284 | +examples/topopt/topoptmodel.py |
| 285 | +--8<-- |
| 286 | +``` |
| 287 | + |
| 288 | + |
| 289 | +## 5. 结果展示 |
| 290 | + |
| 291 | +下图展示了4个模型分别在16组不同的评估数据集上的表现,包括 Binary Accuracy 以及 IoU 这两种指标。其中横坐标代表不同的评估数据集,例如:横坐标 $i$ 表示由原始数据第二维的第 $5\cdot(i+1)$ 个通道及其对应梯度信息构建的评估数据集;纵坐标为评估指标。`thresholding` 对应的指标可以理解为 benchmark。 |
| 292 | + |
| 293 | +<figure markdown> |
| 294 | + { loading=lazy } |
| 295 | + <figcaption>Binary Accuracy结果</figcaption> |
| 296 | +</figure> |
| 297 | + |
| 298 | +<figure markdown> |
| 299 | + { loading=lazy } |
| 300 | + <figcaption>IoU结果</figcaption> |
| 301 | +</figure> |
| 302 | + |
| 303 | + |
| 304 | +用表格表示上图指标为: |
| 305 | + |
| 306 | +| bin_acc | eval_dataset_ch_5 | eval_dataset_ch_10 | eval_dataset_ch_15 | eval_dataset_ch_20 | eval_dataset_ch_25 | eval_dataset_ch_30 | eval_dataset_ch_35 | eval_dataset_ch_40 | eval_dataset_ch_45 | eval_dataset_ch_50 | eval_dataset_ch_55 | eval_dataset_ch_60 | eval_dataset_ch_65 | eval_dataset_ch_70 | eval_dataset_ch_75 | eval_dataset_ch_80 | |
| 307 | +| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | |
| 308 | +| Poisson5 | 0.9471 | 0.9619 | 0.9702 | 0.9742 | 0.9782 | 0.9801 | 0.9803 | 0.9825 | 0.9824 | 0.9837 | 0.9850 | 0.9850 | 0.9870 | 0.9863 | 0.9870 | 0.9872 | |
| 309 | +| Poisson10 | 0.9457 | 0.9703 | 0.9745 | 0.9798 | 0.9827 | 0.9845 | 0.9859 | 0.9870 | 0.9882 | 0.9880 | 0.9893 | 0.9899 | 0.9882 | 0.9899 | 0.9905 | 0.9904 | |
| 310 | +| Poisson30 | 0.9257 | 0.9595 | 0.9737 | 0.9832 | 0.9828 | 0.9883 | 0.9885 | 0.9892 | 0.9901 | 0.9916 | 0.9924 | 0.9925 | 0.9926 | 0.9929 | 0.9937 | 0.9936 | |
| 311 | +| Uniform | 0.9410 | 0.9673 | 0.9718 | 0.9727 | 0.9818 | 0.9824 | 0.9826 | 0.9845 | 0.9856 | 0.9892 | 0.9892 | 0.9907 | 0.9890 | 0.9916 | 0.9914 | 0.9922 | |
| 312 | + |
| 313 | +| iou | eval_dataset_ch_5 | eval_dataset_ch_10 | eval_dataset_ch_15 | eval_dataset_ch_20 | eval_dataset_ch_25 | eval_dataset_ch_30 | eval_dataset_ch_35 | eval_dataset_ch_40 | eval_dataset_ch_45 | eval_dataset_ch_50 | eval_dataset_ch_55 | eval_dataset_ch_60 | eval_dataset_ch_65 | eval_dataset_ch_70 | eval_dataset_ch_75 | eval_dataset_ch_80 | |
| 314 | +| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | |
| 315 | +| Poisson5 | 0.8995 | 0.9267 | 0.9421 | 0.9497 | 0.9574 | 0.9610 | 0.9614 | 0.9657 | 0.9655 | 0.9679 | 0.9704 | 0.9704 | 0.9743 | 0.9730 | 0.9744 | 0.9747 | |
| 316 | +| Poisson10 | 0.8969 | 0.9424 | 0.9502 | 0.9604 | 0.9660 | 0.9696 | 0.9722 | 0.9743 | 0.9767 | 0.9762 | 0.9789 | 0.9800 | 0.9768 | 0.9801 | 0.9813 | 0.9810 | |
| 317 | +| Poisson30 | 0.8617 | 0.9221 | 0.9488 | 0.9670 | 0.9662 | 0.9769 | 0.9773 | 0.9786 | 0.9803 | 0.9833 | 0.9850 | 0.9853 | 0.9855 | 0.9860 | 0.9875 | 0.9873 | |
| 318 | +| Uniform | 0.8887 | 0.9367 | 0.9452 | 0.9468 | 0.9644 | 0.9655 | 0.9659 | 0.9695 | 0.9717 | 0.9787 | 0.9787 | 0.9816 | 0.9784 | 0.9835 | 0.9831 | 0.9845 | |
| 319 | + |
| 320 | +## 参考文献 |
| 321 | +* [Sosnovik I, & Oseledets I. Neural networks for topology optimization](https://arxiv.org/pdf/1709.09578) |
| 322 | +* [原始代码](https://github.com/ISosnovik/nn4topopt/blob/master/) |
0 commit comments