分类: 人工智能

  • flux模型Flux.1 Schnell和Flux. 1 Dev 有什么区别?怎么选择?

    在 AI 成像领域,Flux.1 凭借其创建高分辨率和真实图像的能力,取得了一个有趣的开端。在本文中,我们将总结 Flux.1 Schnell 和 Flux.1 Dev 之间的区别,这被认为是非常适合普通用户并受到广泛关注的两个版本。

     

    Flux.1 Schnell

    对于 Flux.1 Schnell,“Schnell”是“fast”的德语翻译,这意味着此版本专为希望获得快速一致结果的用户而设计,强调更高的性能和处理速度。

     

    • 处理速度
      • Flux.1 Schnell 专注于减少创建图像所花费的时间。非常适合需要快速见效的任务。
    • 资源管理
      • 由于它注重速度,该模型具有高效的资源管理,从而降低了功耗和处理时间。
    • 质量下降
      • 对于更高的速度,有时图像的细节或复杂性可能会有所减少,但对于那些想要快速图像而不需要最大分辨率的人来说,这可以被认为是一种有价值的交换。

     

    Flux.1 Dev

    Flux.1 Dev “Dev”代表“Developer”,意思是开发人员,它被认为是为那些想要深入使用 AI 模型的人设计的,例如开发人员、研究人员或需要根据要求定制模型以达到特定要求的人。

     

    • 开发特点
      • Flux.1 Dev 附带特定于开发人员的工具和功能,这些工具和功能提供对内部参数配置、模型测试和深入结果分析的访问。
    • 灵活性
      • 此模型旨在允许用户自定义 AI 功能以满足其特定需求。它在测试和开发方面具有高度的灵活性,非常适合那些想要研究或开发新功能的人。
    • 具体用途
      • Flux.1 Dev 是那些想要以自己的方式创建和自定义模型的人的最佳工具,这使得创建最高质量的图像并满足特定要求成为可能。

     

    如何选择?

    • 尽管 Flux.1 Schnell 和 Flux.1 Dev 是相同的基本工具,每个版本的设计和用途使它们适用于不同的应用程序。
    • 如果您是想要快速创建图像并且不想复杂配置 Flux.1 的普通用户,那么理论上 Schnell 是最合适的选择。
    • 但是,如果您是需要深入使用 AI 并需要高度灵活的工具的研究人员或开发人员,那么 Flux.1 Dev 将是进一步研究和开发的响应速度最快的选择。

    实际情况下,Flux.1 Dev 几乎总是更好的选择。Flux.1 Schnell的唯一作用可能是让人关注这个细节。

     

     

    官方下载链接

    https://huggingface.co/black-forest-labs

     

     

     

  • 本地部署大模型时,怎么选择合适的版本?

    显卡

    以通义千问为例。

    1. Qwen2.5-7B
      • 参数量:70亿(7B)
      • 显存需求:大约需要8-12GB VRAM(具体取决于优化技术)
    2. Qwen2.5-14B
      • 参数量:140亿(14B)
      • 显存需求:大约需要16-24GB VRAM(具体取决于优化技术)
    3. Qwen2.5-32B
      • 参数量:320亿(32B)
      • 显存需求:大约需要32-48GB VRAM(具体取决于优化技术)

     

    因为我的显卡是16GB 显存,所以Qwen2.5-7B是一个合适的选择。一般对于同一个大模型,发布者会推出很多不同参数的版本,比如0.5B,1B,3B等等,大家可以根据自己的显卡配置进行选择

    • Qwen2.5-7B:可以在16GB VRAM的显卡上运行良好,不会出现显存不足的情况。
    • Qwen2.5-14B:虽然理论上可以在16GB VRAM的显卡上运行,但可能会遇到显存不足的问题,特别是在进行复杂的推理任务时。如果使用一些显存优化技术(如混合精度训练、分批加载等),可以勉强运行,但体验可能不佳。
    • Qwen2.5-32B:这个版本显然不适合16GB VRAM的显卡,因为显存需求远超显卡容量。

     

    计算公式

    显存需求 ≈ 参数数量 × 精度字节 × 1.2(中间变量开销)

    • 参数数量 :
      • 模型的参数规模(如7B表示70亿参数)。
    • 精度字节 :
      • FP32(单精度浮点):4字节/参数
      • FP16(半精度浮点):2字节/参数
      • Int8(8-bit量化):1字节/参数
      • 4-bit量化:0.5字节/参数

     

    • 示例计算:
      • Qwen2.5-7B(FP16): 7B × 2字节 × 1.2 ≈ 14GB
      • Qwen2.5-14B(4-bit量化): 14B × 0.5字节 × 1.2 ≈ 8.4GB

    机器内存和大模型文件大小

    大模型文件最好选用gguf格式文件。

    大部分本地部署大模型的软件在使用大模型时,一般是将大模型加载到内存里,所以本地机器的内存大小也是需要考虑的,如果16G内存的电脑,仅加载模型文件就需要使用好几G,会导致机器运行卡顿。

     

     

     

  • ControlNet With SDXL Model

    怎么选择模型?

    Canny models

    Use the Canny ControlNet to copy the composition of an image.

    The Canny preprocessor detects edges in the control image. The Canny control model then conditions the denoising process to generate images with those edges.

    diffusers_xl_canny_full  (推荐, 速度比较慢, 但效果最好.)
    diffusers_xl_canny_mid
    diffusers_xl_canny_small
    kohya_controllllite_xl_canny_anime
    kohya_controllllite_xl_canny
    sai_xl_canny_128lora
    sai_xl_canny_256lora
    t2i-adapter_xl_canny
    t2i-adapter_diffusers_xl_canny

    Use diffusers_xl_canny_full if you are okay with its large size and lower speed.

    Use kohya_controllllite_xl_canny if you need a small and faster model and can accept a slight change in style.

    Use sai_xl_canny_128lora for a reasonable file size while changing the style less.

    The control weight parameter is critical to generating good images. Most models need it to be lower than 1.

     

    Depth models

    Use the ControlNet Depth model to copy the composition of an image. The usage is similar to Canny but the result is different.

    Here are the depth models we are going to study.

    • diffusers_xl_depth_full
    • diffusers_xl_depth_mid
    • diffusers_xl_depth_small
    • kohya_controllllite_xl_depth_anime
    • kohya_controllllite_xl_depth
    • sai_xl_depth_128lora
    • sai_xl_depth_256lora
    • sargezt_xl_depth
    • sargezt_xl_depth_faid_vidit
    • sargezt_xl_depth_zeed
    • t2i-adapter_diffusers_xl_depth_midas
    • t2i-adapter_diffusers_xl_depth_zoe

    A depth control model uses a depth map  to condition a Stable Diffusion model to generate an image that follows the depth information.A depth map can be extracted from an image using a preprocessor or created from scratch.

    diffusers_xl_depthsai_xl_depth, and t2i-adapter_diffusers_xl_depth models perform well despite their size differences. All are safe choices.

     

     

    Recolor models

    Use the recolor models to color an back-and-white photo.

    • sai_xl_recolor_128lora
    • sai_xl_recolor_256lora

    Both the 128 and 256 Recolor Control-Lora work well.

    Use the recolor_luminance preprocessor because it produces a brighter image matching human perception.

    Be careful in crafting the prompt and the negative prompt. It can have a big effect on recoloring. Use these SDXL style prompts as your starting point.

    You don’t need to use a refiner.

     

     

    Blur models

    Use the Blur model to recover a blurry image.

    • kohya_controllllite_xl_blur_anime
    • kohya_controllllite_xl_blur

    Alternatively, you can use blur_gaussian preprocessor to blur a clear image for testing.

    Of course, some image details are lost in the blur, so you should not expect to recover the same image.

     

     

    IP-adapter

    The Image Prompt Adapter (IP-adapter) lets you use an image prompt like MidJourney. Let’s use the original example from the ControlNet extension to illustrate what it does.

     

     

    OpenPose models

    The OpenPose ControlNet model is for copying a human pose but the outfit, background and anything else.

    Here are the OpenPose models available.

    • kohya_controllllite_xl_openpose_anime
    • kohya_controllllite_xl_openpose_anime_v2
    • t2i-adapter_xl_openpose
    • t2i-adapter_diffusers_xl_openpose
    • thibaud_xl_openpose
    • thibaud_xl_openpose_256lora

     

    [Major Update] sd-webui-controlnet 1.1.400 – Official writeup of SDXL ControlNet models for WebUI.

    Stabilityai/control-lora – An overview of Stability AI’s Control LoRA models.

    kohya-ss/controlnet-lllite – Model Card of ControlNet-LLLite.

    tencent-ailab/IP-Adapter – GitHub page of the Image Prompt adapter.

  • AI-ML-DL-NNs-用gensim学习word2vec

    了解Gensim

    Gensim(generate similarity)是一个简单高效的自然语言处理Python库,用于抽取文档的语义主题(semantic topics)。Gensim的输入是原始的、无结构的数字文本(纯文本),内置的算法包括Word2Vec,FastText,潜在语义分析(Latent Semantic Analysis,LSA),潜在狄利克雷分布(Latent Dirichlet Allocation,LDA)等,通过计算训练语料中的统计共现模式自动发现文档的语义结构。这些算法都是非监督的,这意味着不需要人工输入——仅仅需要一组纯文本语料。一旦发现这些统计模式后,任何纯文本(句子、短语、单词)就能采用语义表示简洁地表达。

     

    特点

      • Memory independence: 不需要一次性将整个训练语料读入内存,Gensim充分利用了Python内置的生成器(generator)和迭代器(iterator)用于流式数据处理,内存效率是Gensim设计目标之一。
      • Memory sharing: 训练好的模型可以持久化到硬盘,和重载到内存。多个进程之间可以共享相同的数据,减少了内存消耗。
      • 多种向量空间算法的高效实现: 包括Word2Vec,Doc2Vec,FastText,TF-IDF,LSA,LDA,随机映射等。
      • 支持多种数据结构
      • 基于语义表示的文档相似度查询

    核心概念

     

    corpus

    一组纯文本的集合,在Gensim中,语料有两个角色:

    1. 模型训练的输入。此时语料用于自动训练机器学习模型,如LsiModel,LdaModel,模型使用训练语料发现公共主题,初始化模型参数。因为Gensim聚焦于非监督模型,因此无需人工干预。
    2. Documents to organize。模型训练好后,可以用于从新文档(训练语料中没有出现过的)抽取主题。

     

    向量空间模型(vector space model,VSM)

    在向量空间模型中,每一篇文档被表示成一组特征。特征可以认为是问答对(question-answer pair),例如:

    1. How many times does the word splonge appear in the document? Zero.
    2. How many paragraphs does the document consist of? Two.
    3. How many fonts does the document use? Five.
    • 单词splonge在文档中出现过几次? 0
    • 文档包含几个段落? 2
    • 文档里包含几种字体? 5

     

    通常对于每一个question分配一个id,因此这篇文档可以表示成一系列的二元对:

    • (1, 0.0): 第1个问题, 答案是0.0
    • (2, 2.0): 第2个问题, 答案是2.0
    • (3, 5.0): 第3个问题, 答案是5.0

    如果我们事先知道所有的question,我们可以隐式的省略这些id只保留answer序列,简写成:

    • ( 0.0 , 2.0 , 5.0 )

    这组answer序列就可以被认为是一个向量,用于代表这篇文档。

    每一篇文档的questions都是相同的,因此在观察两个向量后,我们希望能够得到如下结论:两个向量很相似,因此原始文档一定也很相似。 当然,这个结论是否正确取决于questions选择的好坏。

    我们最常用的词袋模型就是一种向量空间模型,question是词汇表中的词w i w_iwi是否出现在文档中,因此用词袋模型表示文档,向量的维度等于词汇表中单词的数量V

    sparse vector

    为了节约空间,在Gensim中省略了所有值为0.0的元素,例如,对于上面的向量( 0.0 , 2.0 , 5.0 ) (0.0,2.0,5.0),我们只需要写[ ( 2 , 2.0 ) , ( 3 , 5.0 ) ] ,向量中每一个元素是一个二元元组(feature_id, feature_value)。如果某个特征在稀疏表示中缺省,可以很自然的认为其值为0.0。

    streamed corpus

    Gensim没有规定任何指定的数据格式,语料是一组稀疏向量序列。例如:[ [ ( 2 , 2.0 ) , ( 3 , 5.0 ) ]]是一个包含两篇文档的简单语料,两篇文档被表示成两个稀疏向量,第一个有两个非零元素,第二个有一个非零元素。这个例子中,我们将语料用Python中list表示,但是Gensim并没有规定语料必须表示成list,Numpy中array,Pandas中dataframe或者其他任何对象,迭代时,将依次产生这些稀疏向量。这个灵活性允许我们创建自己的语料类,直接从硬盘、网络、数据库……中流式产生稀疏向量。

    model, transformation

    Gensim中用model指代将一篇文档转换(transform)为另一种形式的模型代码以及相关参数,因为文档被表示成向量,因此model可以认为是从一个向量空间到另一个向量空间的变换,这个变换的参数是从训练数据中学习得到的。训练好的models可以被持久化到硬盘,之后在重载回来,无论是在新的训练文档中继续训练还是用于转换一篇文档。Gensim实现了很多模型,比如:Word2Vec,LsiModel,LdaModel,FastText等,具体的可以参考Gensim API。

     

    一个例子

    给定语料包含9篇文档,12个特征:

    • 9篇文档, 9个内层list
    • 12个特征, 12个特征id
    corpus=[[(0, 1.0), (1, 1.0), (2,1.0)],
            [(2, 1.0), (3, 1.0), (4, 1.0), (5, 1.0), (6, 1.0), (8, 1.0)],
            [(1, 1.0), (3, 1.0), (4, 1.0), (7, 1.0)],
            [(0, 1.0), (4, 2.0), (7, 1.0)],
            [(3, 1.0), (5, 1.0), (6, 1.0)],
            [(9, 1.0)],
            [(9, 1.0), (10, 1.0)],
            [(9, 1.0), (10, 1.0), (11, 1.0)],
            [(8, 1.0), (10, 1.0), (11, 1.0)]]

    接下来,我们初始化一个转换:

    from gensim import corpora, models, similarities
    
    tfidf = models.TfidfModel(corpus)
    

    我们采用tfidf模型训练这个转换,即原始bag-of-words向量→ \rightarrowtfidf向量,训练好的模型存储在变量tfidf中,接下来我们用这个tfidf模型将一篇新的文档vec转换为tfidf向量:

    vec = [(0, 1), (4, 1)]
    print(tfidf[vec])

    得到的结果是[ ( 0 , 0.80752440244407231 ) , ( 4 , 0.58983416267400446 ) ] [(0, 0.80752440244407231)]

    同样,我们可以利用语料的tfidf向量训练一个相似度计算模型:

    index = similarities.SparseMatrixSimilarity(tfidf[corpus], num_features=12)
    

    然后计算文档vec与语料每一篇文档的相似度:

    sims = index[tfidf[vec]]
    print(list(enumerate(sims)))

    [(0,0.4662244),(1,0.19139354),(2,0.24600551),(3,0.82094586),(4,0.0),(5,0.0),(6,0.0),(7,0.0),(8,0.0)],表示vec与第一篇文档有46.6%的相似度,与第二篇文档有19.1%的相似度……

     

    gensim的处理流程也可以分为三步:首先将文档表示成特征向量,然后利用训练语料训练转换模型,最后将训练好的模型应用到新文档上。

     

  • Python 环境问题

    Pycharm

    Anaconda

    Conda

    创建Conda虚拟环境

    • 指定python版本和环境名称
      • conda create -n env_name python=3.8
    • 指定python版本, 环境名称, 环境安装路径
      • conda create –prefix=C:/ProgramData/Anaconda3/envs/ENV_spider python=3.9
    • 激活所安装的环境
      • conda activate env_name
    • 在当前激活的环境里安装包
      • conda install package_name
    • 退出当前激活的环境
      • conda deactivate

    PIP

     

    Jupyter

    安装内核

    • 激活/切换到要添加的虚拟环境, 确认是否安装 ipykernel
      • python -m ipykernel –version
    • 如果没有,则安装
      • python -m pip install ipykernel
    • 为 jupyter 添加内核
      • python -m ipykernel install –name env_name

    查看已安装内核

    • jupyter kernelspec list
    (ENV_machinelearning) λ jupyter kernelspec list
    Available kernels:
      %s    %s python3             C:\ProgramData\Anaconda3\envs\ENV_machinelearning\share\jupyter\kernels\python3
      %s    %s pycharm-78712231    C:\Users\Jin\AppData\Roaming\jupyter\kernels\pycharm-78712231
      %s    %s pycharm-ae39b4c1    C:\Users\Jin\AppData\Roaming\jupyter\kernels\pycharm-ae39b4c1
      %s    %s env_machinelearning C:\ProgramData\jupyter\kernels\env_machinelearning

     

    删除 jupyter 内核

    • jupyter kernelspec remove kernelname