0%

传统windows下面的文件,很多老的项目中文件编码有gb2312的,也有unicode的,在vscode中修改的话经常会出错,因为写了个小脚本,把所有非unicode编码的文件统一改为unicode编码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding:utf-8 -*-
__author__ = 'whx'

import os,sys
import chardet
import glob

def convert( filename, in_enc = "GBK", out_enc="UTF-8-SIG" ):
try:
content = open(filename, 'rb').read()
result = chardet.detect(content)#通过chardet.detect获取当前文件的编码格式串,返回类型为字典类型
coding = result.get('encoding')#获取encoding的值[编码格式]
if "UTF" not in coding and "utf" not in coding :#文件格式如果不是utf-8的时候,才进行转码
print ("转换 "+ coding + "to utf-8! " + filename)
new_content = content.decode( coding ).encode(out_enc)
open(filename, 'wb').write( new_content )
print (" done")
else:
print ("不用修改 "+coding + ":" +filename)
except Exception as ex:

print (" error",ex)



filesAspx = glob.glob("D:\source\project/**/*.aspx",recursive=True)
filesCs = glob.glob("D:\source\project/**/*.cs",recursive=True)

for file_path in filesAspx:
convert(file_path)

for file_path in filesAspx:
convert(filesCs)


老的项目中使用了1.6.x的jquery,需要升级其中的弹窗为layui,但是layui需要至少1.10.x,修改方式如下,将原来单行的jquery引用,改为保护多版本jquery,添加layui引用,最后恢复原来的jquery版本号,确保不影响原来其它jquery使用的地方。

1
2
<script src="../js/jquery-1.3.2.min.js" type="text/javascript"></script>

1
2
3
4
5
 <script type="text/javascript" src="../js/lib/jquery.js"></script>
<script type="text/javascript" src="../js/jquerynew.js"></script>
<script type="text/javascript" src="../js/layer/js/layer.js"></script>
<script type="text/javascript" >var $jq = jQuery.noConflict(true);console.log($().jquery);</script>

顺便做一个批量替换的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding:utf-8 -*-
#@Author: whx

import os
import re
import glob
#构建一个文本文件内容查找函数
def replace_text(file_path, search_str):
try:
with open(file_path, 'r', encoding='utf-8') as f ,open("%s.bak"%file_path, 'w', encoding='utf-8') as fw:
data = f.read()
res = re.findall(search_str, data)
if res :
newData = re.sub(search_str, r'<script type="text/javascript" src="\g<2>/jquery-\g<3>.js"></script>\n<script type="text/javascript" src="\g<2>/jquerynew.js"></script>\n<script type="text/javascript" src="\g<2>/layer.js"></script>\n<script type="text/javascript" >var $jq = jQuery.noConflict(true);console.log($().jquery);</script>' , data)
fw.write(newData)
f.close()
fw.close()
os.remove(file_path)
os.rename("%s.bak" % file_path, file_path)
else:
f.close()
fw.close()
os.remove("%s.bak" % file_path)

except UnicodeDecodeError:
with open(file_path, 'r', encoding='gbk') as f ,open("%s.bak"%file_path, 'w', encoding='gbk') as fw:
data = f.read()
res = re.findall(search_str, data)
if res :
newData = re.sub(search_str, r'<script type="text/javascript" src="\g<2>/jquery-\g<3>.js"></script>\n<script type="text/javascript" src="\g<2>/jquerynew.js"></script>\n<script type="text/javascript" src="\g<2>/layer.js"></script>\n<script type="text/javascript" >var $jq = jQuery.noConflict(true);console.log($().jquery);</script>' , data)
fw.write(newData)
f.close()
fw.close()
os.remove(file_path)
os.rename("%s.bak" % file_path, file_path)
else:
f.close()
fw.close()
os.remove("%s.bak" % file_path)
files = glob.glob("D:\source\project/**/*.aspx*",recursive=True)
search_str = "<script(.*)src=\"(.*)/jquery-(.*).js\"(.*)>(.*)</script>"
search_str_reg = re.compile(search_str,flags=re.IGNORECASE)

for file_path in files:
replace_text(file_path, search_str_reg )


s3 minio学习

直接采用docker方式运行,docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: '3'

services:
minio:
image: minio/minio
ports:
- "9000:9000"
- "9001:9001"
volumes:
- ./data:/data
- ./config:/root/.minio
environment:
MINIO_ROOT_USER: whx
MINIO_ROOT_PASSWORD: IamPasswod
command: server --console-address ":9001" /data

然后nginx做反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
server {

listen 80;
server_name s3.wanghongxing.com;
client_max_body_size 20M;

root /data/www/s3.wanghongxing.com;
error_page 403 404 /404.php;
index index.html index.htm index.php;
try_files $uri /index.html;
error_log /var/log/nginx/nginx-error.s3.log;
access_log /var/log/nginx/nginx-access.s3.log;
charset "utf-8";
add_header X-Content-Type-Options nosniff;
add_header Content-Security-Policy "default-src 'self' 'unsafe-inline' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'none';style-src 'self' 'unsafe-inline'; child-src https:";
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000";


location / {
charset "utf8";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_connect_timeout 10000;
proxy_read_timeout 10000;
keepalive_timeout 6000;
#port_in_redirect off;
proxy_pass http://127.0.0.1:9000;
}

}

server {

listen 80;
server_name s3-console.wanghongxing.com;
client_max_body_size 20M;

root /data/www/s3.wanghongxing.com;
error_page 403 404 /404.php;
index index.html index.htm index.php;
try_files $uri /index.html;
error_log /var/log/nginx/nginx-error.s3.log;
access_log /var/log/nginx/nginx-access.s3.log;
charset "utf-8";
add_header X-Content-Type-Options nosniff;
add_header Content-Security-Policy "default-src 'self' 'unsafe-inline' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'none';style-src 'self' 'unsafe-inline'; child-src https:";
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000";


location / {
charset "utf8";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_connect_timeout 10000;
proxy_read_timeout 10000;
keepalive_timeout 6000;
#port_in_redirect off;
proxy_pass http://127.0.0.1:9001;
}

}

客户端学习

下载mc客户端

1
2
3
curl --progress-bar -O https://dl.min.io/client/mc/release/darwin-arm64/mc 
chmod +x mc

macos m1miniconda使用python2.7

conda create -n python27 python=2.7 anaconda

1
CONDA_SUBDIR=osx-64 conda create -n py27 python=2.7

系统安装docker,然后在执行如下命令

1
2
3
4
docker run -it --rm --name certbot \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/www:/www" \
certbot/certbot certonly

其中/var/lib/www是域名静态根目录,期间提示号域名的根目录的时候输入 /www

正常的话会将证书放到/etc/letsencrypt目录下。

macbook silicon 运行kettle(data-integration)

kettle在windows及intel电脑运行正常,但是在silicon芯片的macos上执行是提示:不支持arm64。搜索 发现解决方案: https://medium.com/@gabriella.mayang/how-to-install-and-open-pentaho-data-integration-on-macbook-m2-apple-silicon-4081c2875a02

Step 1: Download JDK and JRE

I tried JDK from Oracle but it failed. Now the only JDK that I am using and proven successful in opening PDI in my M2 is the one from Eclipse Temurin OpenJDK.

  1. Click here to download https://adoptium.net/temurin/releases/?variant=openjdk8&os=mac
  2. Choose JDK .pkg

Step 2: Download MacOSX X86 JAR

  1. Click here to download https://mega.nz/file/VFhgnS6I#s4zUK81rXGdXcpJMuzUvDOHoixZmAl4jyXd0yMU11F0
  2. Move the file to ‘libswt/osx64’

就是把下载的文件放到 data-integration/libswt/osx64 目录下。

Step 4: Create New Terminal Profile for Intel

注:这里是苹果原生的terminal,不是iterm。

  1. Open Terminal > Settings
  2. Click the ‘**+’** button to add new profile and name it as “Rosetta Intel”. You can choose another name but make sure the new profile name indicates the profile usage is for Intel-based apps
  3. Go to ‘Shell’ tab and paste this in the ‘Run Command’ field:
1
env /usr/bin/arch -x86_64 /bin/zsh --login

Uncheck ‘Run inside shell’. Uncheck the checkbox would prevent running the shell twice, which could bloat your environment variables since ~/.zshrc gets run twice

最后 JAVA_HOME

因为我用了多版本的jdk,所以需要单独设置一个java环境

1
2
export JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home

然后就成功了。

解决 访问http自动跳转到https的问题

1、先访问http域名,然后点击地址栏所测那个“不安全”的警告,弹出对话框中选择网站设置,

2024-03-04-09.37.38

2、然后再下面的不安全内容后面的下拉框中选择允许

2024-03-04-09.37.47

3、在地址栏输入: chrome://net-internals/#hsts

截屏2024-03-04 09.53.37

4、找到底部Delete domain security policies一栏,输入想处理的域名,点击delete。

截屏2024-03-04 09.53.51

5、搞定了,再次访问http域名不再自动跳转https了。

pytorch学习之路

pytorch包含以下组件

torch: 一个像numpy 向量库

torch.autograd 自动微分库,支持所有微分向量运算

torch.jit 一个编译栈,从pytorch代码来创建可序列化、可以优化的模型

torch.nn 神经元网络库,设计之初保持了最大化灵活性,深度继承了求导库

torch.multiprocessing 多处理,在torch 向量访问处理器时使用了魔术化的内存共享。在数据加载和疯狂的训练模型的时候非常有用。

torch。utils 数据记在和其他功能

通常pytorch用作两个方面:

1、替代numpy来使用gpu

2、提供最大灵活性和速度的深度学习研究平台

动态神经元网络:使用磁带记录仪回放这种独特的方法来创建神经元网络。

pytorch使用反向模式自动微分技术,让我们经常零成本的改变网络行为;我们的灵感来自于这篇文章的几个研究报告。

这个技术不是pytorch独有的。这是目前为止最快速的实现。你可以为你疯狂的研究获得最快的速度和灵活性。

pytorch不是一个整体绑定到 c++框架的。他是深度集成到python中。你可以像使用numpy scipy scikit-learn一样自然的使用它。你可以写你自己全新神经元网络,使用你喜欢的库和包。我们的目标是不重复造轮子。

必要的经验

pytorch设计为只觉得、线性的思考、简单易用。当你执行一行代码,他被执行了。在现实世界中他不是异步视图。当你调试一行代码或者接收到错误信息和堆栈跟踪,理解他们是很直观的。堆栈调用之初你的代码在哪里被定义。我们希望你不要因为坏的堆栈调用、异步执行和不透明的执行引擎而花费时间调试你的代码。

快速和学习

pytorch有最小的框架成本。我们继承了加速库像intel MKL 和nvidia(cuDNN nccl)去最大化速度。最核心的,他核心的cpu gpu向量和神经元网络是成熟的并且经过多年测试的。

因此,pytorch是非常快的,无论执行小规模还是大规模的模型。

无痛扩展

安装

二进制,命令行通过conda 或者pip 安装 参照: https://pytorch.org/get-started/locally/

源代码安装

需要满足:python 3.8+;编译器全面支持C++17,像clang或者gcc(gcc 9.4.0或者最新)

强烈推荐安装conda环境。

如果想要编译cuda支持,从我们的支持矩阵里选择一个我们支持的版本select a supported version of CUDA from our support matrix。然后安装nvidia cuda ,nvidia cuDNN v8.5 + ,CUDA兼容的编译器。

如果要禁用CUDA,设置环境变量 USE_CUDA=0 .其它有用的环境变量在setup.py中。

安装依赖

Get the PyTorch Source

1
2
3
4
5
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
# if you are updating an existing checkout
git submodule sync
git submodule update --init --recursive

通用

1
2
3
conda install cmake ninja
# Run this command from the PyTorch directory after cloning the source code using the “Get the PyTorch Source“ section below
pip install -r requirements.txt

On Linux

1
2
3
4
5
6
7
conda install intel::mkl-static intel::mkl-include
# CUDA only: Add LAPACK support for the GPU if needed
conda install -c pytorch magma-cuda110 # or the magma-cuda* that matches your CUDA version from https://anaconda.org/pytorch/repo

# (optional) If using torch.compile with inductor/triton, install the matching version of triton
# Run from the pytorch directory after cloning
make triton

On MacOS

1
2
3
4
# Add this package on intel x86 processor machines only
conda install intel::mkl-static intel::mkl-include
# Add these packages if torch.distributed is needed
conda install pkg-config libuv

On Windows

1
2
3
4
conda install intel::mkl-static intel::mkl-include
# Add these packages if torch.distributed is needed.
# Distributed package support on Windows is a prototype feature and is subject to changes.
conda install -c conda-forge libuv=1.39

Install PyTorch

On macOS

1
python3 setup.py develop

20240219-sd模型

常见的模型有以下几类:

  • Base 模型: 就是最常见的 AI 文生图模型,输入一段文字指令,AI 会根据你的指令生成图片。较常见的有 Stable Diffusion v1.5,Stable Diffusion XL 等。
  • LoRA 模型: 你可以简单将其理解为一种滤镜模型。通过它能生成更特殊的图片,比如在 Workflow 中加载线稿 LoRA 模型,就可以生成线稿风格的图片。甚至还有一些 LoRA 模型能让 AI 生成特定样貌的人物,比如一些名人的 LoRA,加载这种模型后,就可以生成特定的名人。但需要注意,这种模型没法脱离 Base 模型使用,这就意味着你需要先下载 Base 模型,然后再下载对应的 LoRA 模型才行。
  • Inpainting 模型: 如果你想要修改某张图片,比如去掉某个物体,或者修复某个物体,那么你就需要使用 Inpainting 模型。这种模型输入一张图片,然后输入一个遮罩,AI 会根据遮罩修复图片。这个模型可以单独使用。
  • Upscale 模型: 如果你想将一张图片放大,那么你就需要使用 Upscale 模型。这种模型输入一张图片,然后输入一个倍数,AI 会根据倍数放大图片。这个模型也可以单独使用。
  • ControlNet 模型: 如果你想控制图片中某个物体出现的位置,或者人物的站姿,那么你就需要使用 ControlNet 模型。但需要注意这个模型和 LoRA 类似,不能脱离 Base 模型使用。
  • 图生视频模型: 这种模型输入一段文字指令,AI 会根据你的指令将图片转为视频。

AI 生图模型需要大量的计算资源,如果你的电脑配置不够,那么即使你下载了模型,也可能无法运行,或者运行起来会很慢,生成一张图片可能要 30 ~ 40 秒。按照前面提到的类型,不同类型的模型对电脑配置的要求也不同。

对配置影响最大的应该是 Base、Inpainting 还有图生视频模型。目前,市面上没有一个官方统一的 AI 模型运行配置要求,但根据我的经验:

  • Base & Inpainting 模型:
    • 如果你是 Windows 电脑,显存小于 6G,或者是 M 系列的 MacBook 内存小于 16G,我会推荐你使用 Stable Diffusion v1.5 或者基于此模型微调后的模型。
    • 如果你的电脑配置比较好,显存大于 8G,或者 M 系列的 MacBook 内存大于 16G,我则会推荐你使用 Stable Diffusion XL 或者基于此模型微调后的模型。
  • 如果你想使用图生视频模型,或者图片生视频模型,那么你的显存最好大于 16G,且最好使用 Nvidia 的显卡。不然基本很难将模型运行起来。

如果你是初学者,我会推荐你使用 DreamShaper 这个模型,它有基于 SDv1.5 的 Base 模型,也有基于 SDXL 的版本。同时配套也很不错。有 Inpainting 模型,也有 LCM 模型等。而且生成的效果也很不错。

DreamShaper 系列模型

如果只推荐一个模型的话,我会推荐 DreamShaper,首先它的成像质量不差,整体质感有点像 Midjourney。

其次,它的 SD v1.5 版本模型大小也不是很大,甚至比基础模型还要小一点点。

最后,也是我最喜欢的一点,它的配套模型比较丰富。有 LCM、Inpainting 模型,还有基于 SDXL Turbo 微调的模型。 对于电脑配置较差的朋友,我推荐你们使用 SD v1.5 的模型,即它的 V8.0 版本。

DreamShaper XL

https://civitai.com/models/112902/dreamshaper-xl

Realistic 风格模型

如果你主要是以生成人像为主,那么 Realistic 风格模型是一个不错的选择。他们生成的人像质量比较高,且看起来更加真实。以下是两个基于 SD v1.5 微调的模型。

https://civitai.com/models/43331/majicmix-realistic

一般你会看到两种后缀的模型:

  • safetensors:这种模型一般用的是 numpy 格式保存,这就意味着它只保存了张量数据,没有任何代码,加载这类文件会更安全和更快。
  • ckpt:这种文件是序列化过的,这意味着它们可能会包含一些恶意代码,加载这类模型就可能会带来安全风险。

所以在上述的案例中,我会推荐你下载 safetensors 格式的模型。

另外,我建议你在搜索模型的时候,需要看看是不是该模型的官方发的,一般我会看模型的下载数,一般下载数越多的模型,越有可能是官方发的。

参照 diffusers api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
conda create -n dfu python=3.10.13
conda activate dfu


pip install diffusers --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

pip install transformers==4.27.0 accelerate==0.12.0 invisible_watermark -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

conda install jupyter


pip install diffusers transformers scipy ftfy

pip install git+https://github.com/huggingface/accelerate

1
2
3
4
5
6
7
#读取diffuers库
from diffusers import StableDiffusionPipeline

#初始化SD模型,加载预训练权重
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
#使用GPU加速
pipe.to("mps")
1
2
3
4
5
6
7
8
9
10
11
#接下来,我们就可以运行pipeline了
prompt = "a photograph of an astronaut riding a horse"
# Number of denoising steps
steps = 25

# Scale for classifier-free guidance
CFG = 7.5
image = pipe(prompt, guidance_scale=CFG, height=512, width=768, num_inference_steps=steps).images[0]

# 由于没有固定seed,每次运行代码,我们都会得到一个不同的图片。
display(image)
20240218-whx
1
2
3
4
5
6
7
8
9
prompt = "city future, 8k, exploration, cinematic, realistic, unreal engine, hyper detailed, volumetric light, moody cinematic epic concept art, realistic matte painting, hyper photorealistic"
negative_prompt ="lowres, bad anatomy, text, error, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts,signature, watermark, username, blurry, artist name"
steps = 20

result1 = pipe(prompt=prompt,negative_prompt=negative_prompt, guidance_scale=CFG, height=512, width=768, num_inference_steps=steps)
image1 = result1.images[0]

# 由于没有固定seed,每次运行代码,我们都会得到一个不同的图片。
display(image1)
202402181931-001