[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"tool-davidbau--rewriting":3,"similar-davidbau--rewriting":106},{"id":4,"github_repo":5,"name":6,"description_en":7,"description_zh":8,"ai_summary_zh":9,"readme_en":10,"readme_zh":11,"quickstart_zh":12,"use_case_zh":13,"hero_image_url":14,"owner_login":15,"owner_name":16,"owner_avatar_url":17,"owner_bio":18,"owner_company":19,"owner_location":20,"owner_email":21,"owner_twitter":15,"owner_website":22,"owner_url":23,"languages":24,"stars":53,"forks":54,"last_commit_at":55,"license":56,"difficulty_score":57,"env_os":58,"env_gpu":59,"env_ram":58,"env_deps":60,"category_tags":67,"github_topics":71,"view_count":79,"oss_zip_url":80,"oss_zip_packed_at":80,"status":81,"created_at":82,"updated_at":83,"faqs":84,"releases":105},8471,"davidbau\u002Frewriting","rewriting","Rewriting a Deep Generative Model, ECCV 2020 (oral).  Interactive tool to directly edit the rules of a GAN to synthesize scenes with objects added, removed, or altered.  Change StyleGANv2 to make extravagant eyebrows, or horses wearing hats.","Rewriting 是一款源自 MIT 与 Adobe 研究院的交互式 AI 工具，曾荣获 ECCV 2020 口头报告奖。它突破了传统图像编辑仅能修改单张图片的局限，允许用户直接调整预训练生成模型（如 StyleGANv2）的内部权重，从而从根本上改变模型生成图像的“规则”。\n\n通过 Rewriting，用户可以轻松实现向场景中添加物体（如给马戴上帽子）、移除特定元素（如去掉水印），甚至重塑物理逻辑（如让树木从塔楼中生长）。一旦模型规则被修改，即可无限合成符合新设定的全新图像，而无需重新收集数据或进行漫长的模型训练。\n\n这项技术的核心亮点在于其独特的“模型重写”理念：基于生成器权重充当线性关联记忆的假设，精准定位并编辑控制特定语义概念的神经元。这不仅为创意工作者提供了快速构建虚构世界模型的强大手段，也为研究人员深入探究深度学习网络如何组织知识提供了直观窗口。\n\nRewriting 特别适合希望探索生成式 AI 内部机制的研究人员、需要快速原型验证的开发者，以及渴望打破数据限制进行艺术创作的设计师。无论是想制造夸张的眉毛效果，还是构建现实中不存在的奇幻场景，它都能让用户的想象力直接转化","Rewriting 是一款源自 MIT 与 Adobe 研究院的交互式 AI 工具，曾荣获 ECCV 2020 口头报告奖。它突破了传统图像编辑仅能修改单张图片的局限，允许用户直接调整预训练生成模型（如 StyleGANv2）的内部权重，从而从根本上改变模型生成图像的“规则”。\n\n通过 Rewriting，用户可以轻松实现向场景中添加物体（如给马戴上帽子）、移除特定元素（如去掉水印），甚至重塑物理逻辑（如让树木从塔楼中生长）。一旦模型规则被修改，即可无限合成符合新设定的全新图像，而无需重新收集数据或进行漫长的模型训练。\n\n这项技术的核心亮点在于其独特的“模型重写”理念：基于生成器权重充当线性关联记忆的假设，精准定位并编辑控制特定语义概念的神经元。这不仅为创意工作者提供了快速构建虚构世界模型的强大手段，也为研究人员深入探究深度学习网络如何组织知识提供了直观窗口。\n\nRewriting 特别适合希望探索生成式 AI 内部机制的研究人员、需要快速原型验证的开发者，以及渴望打破数据限制进行艺术创作的设计师。无论是想制造夸张的眉毛效果，还是构建现实中不存在的奇幻场景，它都能让用户的想象力直接转化为可生成的现实规则。","# Rewriting a Deep Generative Model\n\nIn this paper, we ask if a deep network can be reprogrammed to follow different rules, by enabling a user to directly change the weights, instead of training with a data set.\n\n\n\u003Ctable>\u003Ctr>\u003Ctd>\u003Ca href=\"http:\u002F\u002Frewriting.csail.mit.edu\u002F\">\u003Cimg src='https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_61ba67cd3be5.gif'>\u003C\u002Fa>\u003Cbr>\nDirectly rewriting the weights of a StyleGANv2 to reprogram horses to have hats.\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftable>\n\n### What is model rewriting?\nWe present the task of *model rewriting*, which aims to add, remove, and alter the semantic and physical rules of a pre-trained deep network.  While modern image editing tools achieve a user-specified goal by manipulating individual input images, we enable a user to synthesize an unbounded number of new images by editing a generative model to carry out modified rules.\n\n\n### Why rewrite a model?\nThere are two reasons to want to rewrite a deep network directly:\n  1. To gain insight about how a deep network organizes its knowledge.\n  2. To enable creative users to quickly make novel models for which there is no existing data set.\n\nModel rewriting envisions a way to construct deep networks according to a user's intentions. Rather than limiting networks to imitating data that we already have, rewriting allows deep networks to model a world that follows new rules that a user wishes to have.\n\n\n[**Rewriting a Deep Generative Model**](https:\u002F\u002Frewriting.csail.mit.edu\u002F).\u003Cbr>\n[David Bau](https:\u002F\u002Fpeople.csail.mit.edu\u002Fdavidbau\u002Fhome\u002F), [Steven Liu](http:\u002F\u002Fpeople.csail.mit.edu\u002Fstevenliu\u002F), [Tongzhou Wang](https:\u002F\u002Fssnl.github.io\u002F), [Jun-Yan Zhu](https:\u002F\u002Fwww.cs.cmu.edu\u002F~junyanz\u002F), [Antonio Torralba](http:\u002F\u002Fweb.mit.edu\u002Ftorralba\u002Fwww\u002F). \u003Cbr>\nECCV 2020 (oral).\u003Cbr>\nMIT CSAIL and Adobe Research.\n\n\u003Ctable>\u003Ctr>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=i2_-zNqtEPk\" target=\"_blank\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_bd15c04b4a30.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>2 minute\u003Cbr>Video Preview\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002Fpaper\u002F\">\u003Cimg height=\"100\" width=\"78\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_5ec65e07398a.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>ECCV 2020\u003Cbr>Preprint\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002Fvideo\u002F\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_d1e737130267.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>ECCV 2020\u003Cbr>Talk Video\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002Fslides\u002Frewriting-slides.pptx\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_55728310baf1.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>Presentation\u003Cbr>Slides\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002F\" class=\"d-inline-block p-3 align-top\">\u003Cimg height=\"100\" width=\"78\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_13d35d9650c2.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>Website\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002Fdavidbau\u002Frewriting\u002Fblob\u002Fmaster\u002Fnotebooks\u002Frewriting-interface.ipynb\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_95c531cb975c.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>Demo Colab\u003Cbr>Notebook\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftable>\n\n\u003Cimg src='https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_0f283494d2c3.gif' width=\"800px\" \u002F>\n\nOur method rewrites the weights of a generator to change generative rules.\nInstead of editing individual images, our method edits the generator, so an infinite set of images can be potentially synthesized and manipulated using the altered rules.  Rules can be changed in various ways, such as *removing* patterns like watermarks, *adding* objects such as people, or *replacing* definitions such as making trees grow out of towers.\n\n\u003Cimg src='https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_9ed8c2053bfe.png' width=\"800px\" \u002F>\n\nOur method is based on the hypothesis that the weights of a generator act as linear associative memory. A layer stores a map between keys, which denote meaningful context, and values, which determine output.\n\n## Example Results\n\nThe results below show changes of a single rule within StyleGANv2.  In each case,\nfour examples chosen by the user (center of the top row) establish the context\nfor the rule begin rewritten, and the \"copy and paste\" examples (left and right of\ntop row) indicate how the user wishes to change the model.\n\nThe grid below shows pairs of outputs: for each pair, the first is the output of\nthe original unmodified StyleGANv2.  The second is the output of the modified\nStyleGANv2, applying the user's intention using our method.\n\n\u003Chr>\n\nFirst: changing the rule defining kids' eyebrows to make them look like a bushy mustache.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_ff57e24d61ed.png\" width=800>\n\n\u003Chr>\n\nAltering the rule for pointy tower tops to make them into trees.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_12398f93dc20.png\" width=800>\n\n\u003Chr>\n\nChanging the rule for tops of horses heads, to put hats on horses.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_6c67b653f92e.png\" width=800>\n\n\u003Chr>\n\nChanging frowns into smiles.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_cdd38f65adca.png\" width=800>\n\n\u003Chr>\n\nRemoving the main window in a building by changing the rule to draw a blank wall.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_c230ff53cb52.png\" width=800>\n\n## Tips\n\nThe code runs using PyTorch.\n\n* The method and interface can be found in `\u002Frewrite`\n* Notebooks are in `\u002Fnotebooks`: see `rewriting-interface.ipynb` for the demonstration UI.\n* Quantitative experiments in `\u002Fmetrics`, dissection utilities in `\u002Futils`.\n  Experiments from section 5.1 can be repoduced by `\u002Fexperiments.sh`,\n  and section 5.2 can be reproduced by `\u002Fwatermarks.sh`.  The experiment\n  in section 5.3 runs in ithe notebook `\u002Fnotebooks\u002Freflection-rule-change.ipynb`.\n\n## Setup\n\nIt's designed to use a recent version of PyTorch (1.4+) on python (3.6), using\ncuda 10.1 and cudnn 7.6.0.  Run `setup\u002Fsetup_renv.sh` to create a conda environment\nthat has the needed dependencies.\n\n## Rewriting your own models\n\nTo edit your own models, do the following:\n * Train your GAN using the standard StyleGAN v2 tensorflow release, found\n   at https:\u002F\u002Fgithub.com\u002FNVlabs\u002Fstylegan2\n * Convert your trained weights to pytorch using the `convert_weight.py` utility \n   by rosinality, found in https:\u002F\u002Fgithub.com\u002Frosinality\u002Fstylegan2-pytorch\n * Then those weight files can be directly loaded by our sequentialiized\n   StyleGAN port, as follows:\n```\n# Resolution (size) and style dimensionality (style_dim and n_mlp) are\n# the architecture dimensions as you trained them.  The truncation trick can be\n# applied here if desired (truncation=1.0 if not).\n# Note that mconv='seq' splits apart the optimized modulated convolution into\n# separate operations that the rewriter can examine the underlying\n# convolution directly.\nmodel = SeqStyleGAN2(size=256, style_dim=512, n_mlp=8, truncation=0.5, mconv='seq')\n\n# load the exponential moving average model weights, put it on the GPU.\nstate_dict = torch.load('your_model.pt')\nmodel.load_state_dict(state_dict['g_ema'], latent_avg=state_dict['latent_avg'])\nmodel.cuda()\n```\n * Create a `ganrewrite.SeqStyleGanRewriter` instance to edit your model\n```\nlayernum = 8 # or which ever layer you wish to modify\nsample_size = 1000 # a small sample of images for computing statistics\nzds = zdataset.z_dataset_for_model(model, size=sample_size)\ngw = SeqStyleGanRewriter(\n    model, zds, layernum,\n    cachedir='experiments')\n```\n * Finally, to specify and create changes, you can use the\n   `rewriteapp.GanRewriteApp` interface (assumes you are running\n   in a notebook.  This interface can be used to try edits\n   and save and load json files with stored model edit specifications.\n```\nsavedir = 'masks'\ninterface = rewriteapp.GanRewriteApp(gw, size=256, mask_dir=savedir, num_canvases=32)\n```\n * To bulk-generate images from an edited model (notebook UI not needed),\n   you can do the following, then sample the output of the modified model\n   as usual.  See `metrics\u002Fsample_edited.py` for an example.\n```\nsaved_edit = 'masks\u002Fmy_edit.json'\ngw.apply_edit(json.load(saved_edit), rank=1)\n```\n","# 重写深度生成模型\n\n在本文中，我们探讨是否可以通过让用户直接更改权重，而不是使用数据集进行训练，来重新编程深度网络以遵循不同的规则。\n\n\n\u003Ctable>\u003Ctr>\u003Ctd>\u003Ca href=\"http:\u002F\u002Frewriting.csail.mit.edu\u002F\">\u003Cimg src='https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_61ba67cd3be5.gif'>\u003C\u002Fa>\u003Cbr>\n直接重写StyleGANv2的权重，将马匹重新编程为戴帽子的样子。\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftable>\n\n### 什么是模型重写？\n我们提出了*模型重写*的任务，其目标是添加、移除以及改变预训练深度网络的语义和物理规则。虽然现代图像编辑工具通过操作单个输入图像来实现用户指定的目标，但我们允许用户通过编辑生成模型使其遵循修改后的规则，从而合成无限数量的新图像。\n\n\n### 为什么需要重写模型？\n希望直接重写深度网络的原因有两个：\n  1. 为了深入了解深度网络如何组织其知识。\n  2. 为了让富有创造力的用户能够快速创建没有现有数据集支持的新模型。\n\n模型重写设想了一种根据用户意图构建深度网络的方式。与其将网络局限于模仿我们已有的数据，不如通过重写使深度网络能够模拟一个遵循用户期望新规则的世界。\n\n\n[**重写深度生成模型**](https:\u002F\u002Frewriting.csail.mit.edu\u002F)。\u003Cbr>\n[大卫·鲍](https:\u002F\u002Fpeople.csail.mit.edu\u002Fdavidbau\u002Fhome\u002F)、[史蒂文·刘](http:\u002F\u002Fpeople.csail.mit.edu\u002Fstevenliu\u002F)、[王通舟](https:\u002F\u002Fssnl.github.io\u002F)、[朱俊彦](https:\u002F\u002Fwww.cs.cmu.edu\u002F~junyanz\u002F)、[安东尼奥·托拉尔巴](http:\u002F\u002Fweb.mit.edu\u002Ftorralba\u002Fwww\u002F)。\u003Cbr>\nECCV 2020（口头报告）。\u003Cbr>\nMIT CSAIL与Adobe Research。\n\n\u003Ctable>\u003Ctr>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=i2_-zNqtEPk\" target=\"_blank\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_bd15c04b4a30.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>2分钟\u003Cbr>视频预览\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002Fpaper\u002F\">\u003Cimg height=\"100\" width=\"78\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_5ec65e07398a.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>ECCV 2020\u003Cbr>预印本\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002Fvideo\u002F\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_d1e737130267.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>ECCV 2020\u003Cbr>演讲视频\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002Fslides\u002Frewriting-slides.pptx\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_55728310baf1.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>演示\u003Cbr>幻灯片\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Frewriting.csail.mit.edu\u002F\" class=\"d-inline-block p-3 align-top\">\u003Cimg height=\"100\" width=\"78\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_13d35d9650c2.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>网站\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\n\u003Ctd>\u003Ccenter>\u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002Fdavidbau\u002Frewriting\u002Fblob\u002Fmaster\u002Fnotebooks\u002Frewriting-interface.ipynb\" class=\"d-inline-block p-3 align-bottom\">\u003Cimg height=\"78\" width=\"136\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_95c531cb975c.png\" style=\"border:1px solid\" data-nothumb=\"\">\u003Cbr>演示Colab\u003Cbr>笔记本\u003C\u002Fa>\u003C\u002Fcenter>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftable>\n\n\u003Cimg src='https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_0f283494d2c3.gif' width=\"800px\" \u002F>\n\n我们的方法通过重写生成器的权重来改变生成规则。不同于对单张图像进行编辑，我们的方法直接编辑生成器，因此可以利用修改后的规则潜在地合成和操控无限多的图像。规则可以通过多种方式改变，例如*移除*水印等图案、*添加*人物等对象，或*替换*定义，比如让树木从塔楼上生长出来。\n\n\u003Cimg src='https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_9ed8c2053bfe.png' width=\"800px\" \u002F>\n\n我们的方法基于这样一个假设：生成器的权重充当线性联想记忆。每一层存储着键与值之间的映射关系，其中键表示有意义的上下文，而值则决定输出。\n\n## 示例结果\n\n以下结果展示了对StyleGANv2中单一规则的修改。在每种情况下，\n由用户选择的四个示例（顶部行中央）确立了要被重写的规则的上下文，\n而“复制并粘贴”的示例（顶部行左右两侧）则表明了用户希望如何改变模型。\n\n下图中的网格显示了成对的输出：对于每一对，第一个是原始未修改的StyleGANv2的输出，\n第二个则是应用了我们方法后，按照用户的意图修改过的StyleGANv2的输出。\n\n\u003Chr>\n\n首先：将儿童眉毛的规则改为看起来像浓密的小胡子。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_ff57e24d61ed.png\" width=800>\n\n\u003Chr>\n\n将尖顶塔楼的规则改为让它们长出树木。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_12398f93dc20.png\" width=800>\n\n\u003Chr>\n\n将马头顶部的规则改为给马戴上帽子。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_6c67b653f92e.png\" width=800>\n\n\u003Chr>\n\n将皱眉的表情改为微笑。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_cdd38f65adca.png\" width=800>\n\n\u003Chr>\n\n通过改变绘制空白墙的规则，移除建筑物中的主窗户。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_readme_c230ff53cb52.png\" width=800>\n\n## 使用提示\n\n代码使用PyTorch运行。\n\n* 方法和界面位于`\u002Frewrite`\n* 笔记本位于`\u002Fnotebooks`：请参阅`rewriting-interface.ipynb`以获取演示UI。\n* 定量实验位于`\u002Fmetrics`，剖析工具位于`\u002Futils`。\n  第5.1节的实验可以通过`\u002Fexperiments.sh`复现，\n  而第5.2节的实验可以通过`\u002Fwatermarks.sh`复现。第5.3节的实验则在`\u002Fnotebooks\u002Freflection-rule-change.ipynb`笔记本中运行。\n\n## 环境设置\n\n该代码设计用于在Python 3.6上运行最新版本的PyTorch (1.4+)，\n并搭配CUDA 10.1和cuDNN 7.6.0。运行`setup\u002Fsetup_renv.sh`即可创建包含所需依赖项的conda环境。\n\n## 重写您自己的模型\n\n要编辑您自己的模型，请按以下步骤操作：\n * 使用标准的StyleGAN v2 TensorFlow发布版训练您的GAN，该版本可在https:\u002F\u002Fgithub.com\u002FNVlabs\u002Fstylegan2找到。\n * 使用rosinality提供的`convert_weight.py`工具将您训练好的权重转换为PyTorch格式，\n   该工具可在https:\u002F\u002Fgithub.com\u002Frosinality\u002Fstylegan2-pytorch中找到。\n * 随后，这些权重文件可以直接加载到我们序列化的StyleGAN移植版本中，如下所示：\n```\n# 分辨率（size）和风格维度（style_dim和n_mlp）是您训练时所使用的架构尺寸。\n# 如果需要，此处也可以应用截断技巧（如果不使用，则设为truncation=1.0）。\n# 注意，mconv='seq'会将优化后的调制卷积拆分为独立的操作，\n# 这样重写器就可以直接检查底层的卷积。\nmodel = SeqStyleGAN2(size=256, style_dim=512, n_mlp=8, truncation=0.5, mconv='seq')\n\n# 加载指数移动平均模型权重，并将其放到 GPU 上。\nstate_dict = torch.load('your_model.pt')\nmodel.load_state_dict(state_dict['g_ema'], latent_avg=state_dict['latent_avg'])\nmodel.cuda()\n```\n * 创建一个 `ganrewrite.SeqStyleGanRewriter` 实例来编辑你的模型\n```\nlayernum = 8 # 或者你想要修改的任意层\nsample_size = 1000 # 用于计算统计量的一小批图像\nzds = zdataset.z_dataset_for_model(model, size=sample_size)\ngw = SeqStyleGanRewriter(\n    model, zds, layernum,\n    cachedir='experiments')\n```\n * 最后，要指定并创建修改，你可以使用 `rewriteapp.GanRewriteApp` 界面（假设你在 Jupyter Notebook 中运行）。该界面可用于尝试编辑，并保存和加载包含已存储模型编辑规范的 JSON 文件。\n```\nsavedir = 'masks'\ninterface = rewriteapp.GanRewriteApp(gw, size=256, mask_dir=savedir, num_canvases=32)\n```\n * 若要批量生成经过编辑后的模型输出的图像（无需 Jupyter Notebook 界面），可以执行以下操作，然后像往常一样对修改后的模型输出进行采样。示例请参见 `metrics\u002Fsample_edited.py`。\n```\nsaved_edit = 'masks\u002Fmy_edit.json'\ngw.apply_edit(json.load(saved_edit), rank=1)\n```","# Rewriting 快速上手指南\n\nRewriting 是一个用于直接修改预训练深度生成模型（如 StyleGANv2）权重的工具。它允许用户通过编辑模型规则来添加、移除或改变生成图像的语义特征（例如给马加上帽子、将塔顶变成树），而无需重新训练模型或准备新数据集。\n\n## 环境准备\n\n本项目基于 PyTorch 开发，推荐在 Linux 环境下运行。\n\n*   **操作系统**: Linux\n*   **Python 版本**: 3.6+\n*   **PyTorch 版本**: 1.4+\n*   **CUDA**: 10.1\n*   **cuDNN**: 7.6.0\n*   **依赖管理**: Conda (推荐)\n\n> **注意**：国内用户建议在创建 Conda 环境时配置清华或中科大镜像源以加速依赖下载。\n\n## 安装步骤\n\n1.  **克隆项目代码**\n    ```bash\n    git clone https:\u002F\u002Fgithub.com\u002Fdavidbau\u002Frewriting.git\n    cd rewriting\n    ```\n\n2.  **创建 Conda 环境并安装依赖**\n    项目提供了自动化脚本 `setup_renv.sh` 来配置所需的环境。\n    ```bash\n    bash setup\u002Fsetup_renv.sh\n    ```\n    *如果脚本执行失败，请手动激活环境并安装 requirements.txt 中的依赖。*\n\n3.  **验证安装**\n    激活环境后，尝试运行示例 Notebook 或检查 PyTorch 是否识别到 GPU：\n    ```bash\n    conda activate rewriting_env  # 环境名称可能因脚本而异，请根据实际输出调整\n    python -c \"import torch; print(torch.cuda.is_available())\"\n    ```\n\n## 基本使用\n\n本工具的核心流程是：加载预训练的 StyleGANv2 模型 -> 初始化重写器 -> 使用交互界面或代码应用编辑规则。\n\n### 1. 准备模型权重\n你需要一个标准的 StyleGANv2 模型（`.pt` 格式）。如果你只有 TensorFlow 版本的权重，请先使用 [rosinality\u002Fstylegan2-pytorch](https:\u002F\u002Fgithub.com\u002Frosinality\u002Fstylegan2-pytorch) 中的 `convert_weight.py` 进行转换。\n\n### 2. 加载模型与初始化重写器\n以下代码展示了如何加载模型并初始化重写接口（建议在 Jupyter Notebook 中运行）：\n\n```python\nimport torch\nfrom stylegan2 import SeqStyleGAN2  # 假设已导入项目内的序列化模型定义\nimport zdataset\nfrom ganrewrite import SeqStyleGanRewriter\nfrom rewriteapp import GanRewriteApp\n\n# 1. 初始化模型\n# 参数需与你训练时的架构一致 (size, style_dim, n_mlp)\n# mconv='seq' 是关键，它将卷积操作拆分以便重写器访问底层卷积\nmodel = SeqStyleGAN2(size=256, style_dim=512, n_mlp=8, truncation=0.5, mconv='seq')\n\n# 2. 加载权重\n# 加载 EMA (指数移动平均) 权重并移至 GPU\nstate_dict = torch.load('your_model.pt')\nmodel.load_state_dict(state_dict['g_ema'], latent_avg=state_dict['latent_avg'])\nmodel.cuda()\n\n# 3. 初始化重写器\nlayernum = 8          # 指定要修改的网络层数\nsample_size = 1000    # 用于计算统计信息的小样本图像数量\nzds = zdataset.z_dataset_for_model(model, size=sample_size)\n\ngw = SeqStyleGanRewriter(\n    model, zds, layernum,\n    cachedir='experiments'\n)\n\n# 4. 启动交互界面 (仅在 Notebook 中有效)\n# 该界面允许你选择源区域和目标区域来定义规则变化\nsavedir = 'masks'\ninterface = GanRewriteApp(gw, size=256, mask_dir=savedir, num_canvases=32)\n```\n\n### 3. 应用编辑并生成图像\n在交互界面中完成规则定义并保存为 JSON 文件后，可以通过代码批量应用编辑并生成新图像：\n\n```python\nimport json\n\n# 加载之前保存的编辑规则\nsaved_edit = 'masks\u002Fmy_edit.json'\n\n# 应用编辑到模型 (rank=1 表示应用主要编辑方向)\ngw.apply_edit(json.load(open(saved_edit)), rank=1)\n\n# 此时 model 已被修改，可像往常一样采样生成图像\n# 参考 metrics\u002Fsample_edited.py 查看完整的生成示例\n```\n\n### 快速体验\n如果你想直接体验效果而无需配置本地环境，可以使用官方提供的 Colab 演示：\n*   **Demo Colab Notebook**: [https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002Fdavidbau\u002Frewriting\u002Fblob\u002Fmaster\u002Fnotebooks\u002Frewriting-interface.ipynb](https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002Fdavidbau\u002Frewriting\u002Fblob\u002Fmaster\u002Fnotebooks\u002Frewriting-interface.ipynb)","一位游戏美术设计师需要为奇幻题材项目快速生成大量“头戴礼帽的骏马”概念图，以探索独特的角色造型风格。\n\n### 没有 rewriting 时\n- 必须收集成千上万张“戴帽子的马”的真实照片或合成图来重新训练模型，但这类特定组合的数据集在现实中几乎不存在。\n- 只能使用传统修图软件对生成的每一张图片进行手动 P 图，效率极低且无法保证光影和透视的自然统一。\n- 若尝试通过调整潜在向量（Latent Vector）来“碰运气”寻找戴帽子的马，往往会导致马匹姿态扭曲或帽子形状怪异，难以精确控制。\n- 每次修改需求（如换成牛仔帽）都需要重复上述繁琐过程，无法形成一套可复用的生成规则。\n\n### 使用 rewriting 后\n- 直接编辑预训练 StyleGANv2 模型的内部权重，将“帽子”的特征规则关联到“马头”区域，无需任何额外训练数据即可生效。\n- 一次性改写生成器规则后，可无限合成各种姿态、光照下自然佩戴礼帽的骏马，彻底摆脱单张图片的手动修饰。\n- 能够精准控制语义规则，确保帽子完美贴合马头结构，同时保持马匹原有的生物特征不变，避免产生畸形图像。\n- 只需微调权重映射，即可瞬间将规则从“礼帽”切换为“牛仔帽”或“花环”，极大提升了创意迭代的灵活性。\n\nrewriting 的核心价值在于让用户通过直接修改模型权重来定义新的生成规则，从而在无数据支持的情况下创造出遵循特定逻辑的无限新图像。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdavidbau_rewriting_bd15c04b.png","davidbau","David Bau","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fdavidbau_00c4740f.jpg","Northeastern professor in machine learning, interpretable deep networks, NLP and vision, ex-Googler. Believes that deep learning should be transparent.","Northeastern University","Boston","davidbau@northeastern.edu","http:\u002F\u002Fbaulab.info\u002F","https:\u002F\u002Fgithub.com\u002Fdavidbau",[25,29,33,37,41,45,49],{"name":26,"color":27,"percentage":28},"Python","#3572A5",66.2,{"name":30,"color":31,"percentage":32},"Jupyter Notebook","#DA5B0B",25.7,{"name":34,"color":35,"percentage":36},"Cuda","#3A4E3A",5.3,{"name":38,"color":39,"percentage":40},"C++","#f34b7d",1.4,{"name":42,"color":43,"percentage":44},"Shell","#89e051",0.6,{"name":46,"color":47,"percentage":48},"C","#555555",0.5,{"name":50,"color":51,"percentage":52},"HTML","#e34c26",0.3,535,77,"2025-09-17T08:25:42","MIT",4,"未说明","需要 NVIDIA GPU，CUDA 10.1，cuDNN 7.6.0",{"notes":61,"python":62,"dependencies":63},"该项目旨在直接重写预训练的 StyleGAN v2 模型权重。用户需先使用 TensorFlow 版本的 StyleGAN v2 训练模型，然后使用 rosinality 提供的工具将权重转换为 PyTorch 格式方可加载。官方提供了 setup\u002Fsetup_renv.sh 脚本用于创建包含所有依赖的 conda 环境。主要功能代码位于 \u002Frewrite 目录，演示界面在 \u002Fnotebooks\u002Frewriting-interface.ipynb。","3.6",[64,65,66],"torch>=1.4","stylegan2-pytorch (rosinality)","tensorflow (用于训练原始 StyleGAN v2 模型)",[68,69,70],"开发框架","图像","其他",[72,73,74,75,76,77,78],"vision","machine-learning","graphics","hci","gans","deep-learning","research",2,null,"ready","2026-03-27T02:49:30.150509","2026-04-17T21:44:53.507039",[85,90,95,100],{"id":86,"question_zh":87,"answer_zh":88,"source_url":89},37920,"在 Colab 上加载自定义模型时遇到 state_dict 缺失键（如 noises.noise_0）或层类型错误怎么办？","这是 StyleGAN2 端口的问题。首先，确保在构建 SeqStyleGAN2 模型时设置参数 mconv='seq'，这将调制卷积层拆分为序列步骤以允许重写算法编辑底层卷积。其次，加载 state_dict 后，尝试执行 g.cuda() 将模型移至 GPU。如果仍然报错，最新的代码更新已使 noises 和 latent_avg 变为可选参数，请确保使用最新版本的代码库。","https:\u002F\u002Fgithub.com\u002Fdavidbau\u002Frewriting\u002Fissues\u002F2",{"id":91,"question_zh":92,"answer_zh":93,"source_url":94},37921,"运行 rewriting-interface 时出现 'Permission denied: \u002Ftemp\u002Ftorch_extensions\u002Ffused' 错误如何解决？","这是由于 PyTorch 在共享系统上的一个 bug，\u002Ftemp\u002F 目录可能被其他用户创建且当前用户无权访问。可以通过设置环境变量 TORCH_EXTENSIONS_DIR 来更改临时目录位置以避免此问题。例如，在 .bashrc 中添加：export TORCH_EXTENSIONS_DIR=\"\u002Ftmp\u002F${USER}\u002Ftorch_extensions\"。","https:\u002F\u002Fgithub.com\u002Fdavidbau\u002Frewriting\u002Fissues\u002F3",{"id":96,"question_zh":97,"answer_zh":98,"source_url":99},37922,"可以在商业项目或个人笔记本中复用 labwidget 吗？许可证情况如何？","可以。维护者确认允许在注明出处（attribution）的前提下复用 labwidget 代码。维护者随后也承诺会在仓库中添加明确的许可证文件。","https:\u002F\u002Fgithub.com\u002Fdavidbau\u002Frewriting\u002Fissues\u002F1",{"id":101,"question_zh":102,"answer_zh":103,"source_url":104},37923,"如何理解“将特征补丁与邻近特征隔离渲染”的具体操作方法？","具体操作是：如果你希望生成一个孤立的补丁（例如 32x32），而在某一层 L 的特征图空间大小为 32x32，你应该将该层中除了目标补丁区域（例如 4x4）之外的所有其他特征值设置为 0。对其他相关层也执行类似操作，以获得最终的孤立输出结果。","https:\u002F\u002Fgithub.com\u002Fdavidbau\u002Frewriting\u002Fissues\u002F9",[],[107,118,126,135,143,152],{"id":108,"name":109,"github_repo":110,"description_zh":111,"stars":112,"difficulty_score":113,"last_commit_at":114,"category_tags":115,"status":81},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[116,68,69,117],"Agent","数据工具",{"id":119,"name":120,"github_repo":121,"description_zh":122,"stars":123,"difficulty_score":113,"last_commit_at":124,"category_tags":125,"status":81},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[68,69,116],{"id":127,"name":128,"github_repo":129,"description_zh":130,"stars":131,"difficulty_score":79,"last_commit_at":132,"category_tags":133,"status":81},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",159267,"2026-04-17T11:29:14",[68,116,134],"语言模型",{"id":136,"name":137,"github_repo":138,"description_zh":139,"stars":140,"difficulty_score":79,"last_commit_at":141,"category_tags":142,"status":81},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108322,"2026-04-10T11:39:34",[68,69,116],{"id":144,"name":145,"github_repo":146,"description_zh":147,"stars":148,"difficulty_score":79,"last_commit_at":149,"category_tags":150,"status":81},6121,"gemini-cli","google-gemini\u002Fgemini-cli","gemini-cli 是一款由谷歌推出的开源 AI 命令行工具，它将强大的 Gemini 大模型能力直接集成到用户的终端环境中。对于习惯在命令行工作的开发者而言，它提供了一条从输入提示词到获取模型响应的最短路径，无需切换窗口即可享受智能辅助。\n\n这款工具主要解决了开发过程中频繁上下文切换的痛点，让用户能在熟悉的终端界面内直接完成代码理解、生成、调试以及自动化运维任务。无论是查询大型代码库、根据草图生成应用，还是执行复杂的 Git 操作，gemini-cli 都能通过自然语言指令高效处理。\n\n它特别适合广大软件工程师、DevOps 人员及技术研究人员使用。其核心亮点包括支持高达 100 万 token 的超长上下文窗口，具备出色的逻辑推理能力；内置 Google 搜索、文件操作及 Shell 命令执行等实用工具；更独特的是，它支持 MCP（模型上下文协议），允许用户灵活扩展自定义集成，连接如图像生成等外部能力。此外，个人谷歌账号即可享受免费的额度支持，且项目基于 Apache 2.0 协议完全开源，是提升终端工作效率的理想助手。",100752,"2026-04-10T01:20:03",[151,116,69,68],"插件",{"id":153,"name":154,"github_repo":155,"description_zh":156,"stars":157,"difficulty_score":79,"last_commit_at":158,"category_tags":159,"status":81},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[151,68]]