Files
colomi 0eb61f3e60 初始化换发型项目:3个微服务代码 + 部署脚本
包含:
- hair_service_sd: 换发型/换发色算法服务 (端口 8801)
- photo_service: LoRA 训练调度服务 (端口 32678)
- stable-diffusion-webui: SD WebUI 推理服务 (端口 57860)
- kohya_ss_home: 训练环境代码
- meidaojia: 监控测试脚本
- setup.sh: 一键部署脚本 (conda环境恢复 + 配置生成 + 完整性检查)
- start_all_services.sh: 启动3个服务
- configure.ini.template: 路径模板化 (BASE_DIR自动推导)
- conda_envs/py310.yml: py310 环境定义

大文件 (weights/, models/, data/, conda_envs/*.tar.gz 等) 通过 .gitignore 排除,
由网盘单独上传。
2026-07-11 18:11:49 +08:00

95 lines
3.3 KiB
Python

import sys
import os
import cv2
import torch
import torch.nn as nn
def op_name(op_name, m):
m.op_name = op_name
return m
def conv_bn_relu(name, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, momentum = 0.1, track_running_stats=True):
return nn.Sequential(
op_name(name, nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, False)),
op_name(name + '/bn', nn.BatchNorm2d(out_channels, momentum = momentum, track_running_stats = track_running_stats)),
op_name(name + '/relu', nn.ReLU(inplace=True)),
)
# Define a Basic resnet block
class BasicResnetBlock(nn.Module):
def __init__(self, name, in_channels, out_channels, kernel_size=3, stride=2, padding=1, direct_plus=False):
super(BasicResnetBlock, self).__init__()
self.op_name = name
self.conv_block = nn.Sequential(
conv_bn_relu(name=name + '/conv1', in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride, padding=padding),
conv_bn_relu(name=name + '/conv2', in_channels=out_channels, out_channels=out_channels,
kernel_size=3, stride=1, padding=1)
)
if direct_plus and (in_channels == out_channels) and (stride == 1):
self.sc_block = None
else:
self.sc_block = conv_bn_relu(name=name + '/sc_conv', in_channels=in_channels, out_channels=out_channels,
kernel_size=1, stride=stride, padding=0)
def forward(self, x):
if self.sc_block is None:
out = x + self.conv_block(x)
else:
out = self.conv_block(x) + self.sc_block(x)
return out
class Flatten(nn.Module):
def __init__(self, axis):
super(Flatten, self).__init__()
self.axis = axis
def forward(self, x):
assert self.axis == 1
x = x.reshape(x.shape[0], -1)
# x = x.view(-1, 1152)
return x
def flatten(name, axis):
return op_name(name, Flatten(axis))
def linear_bn_relu(name, in_features, out_features, momentum = 0.1, track_running_stats=True):
return nn.Sequential(
op_name(name + '/FC', nn.Linear(in_features, out_features)),
op_name(name + '/bn', nn.BatchNorm1d(out_features, momentum = momentum, track_running_stats = track_running_stats)),
op_name(name + '/relu', nn.ReLU(inplace=True)),
)
class LeftEye(nn.Module):
def __init__(self, name, in_channels, out_channels):
super(LeftEye, self).__init__()
self.op_name = name
op_list = []
op_list += [conv_bn_relu(name + '/first_conv', in_channels, 24, kernel_size = 5, stride = 2, padding = 2) ]
ch_num = [24, 32, 64, 96, 128]
op_list += [BasicResnetBlock(name + '/stage%d'%(i + 1), ch_num[i], ch_num[i+1]) for i in range(len(ch_num) - 1) ]
op_list += [flatten(name + '/flatten', 1),
linear_bn_relu(name + '/FC1', 1152, 256),
op_name(name + '/FC2', nn.Linear(256, out_channels))]
self.conv_block = nn.Sequential(*op_list)
def forward(self, x):
return self.conv_block(x)
def get_left_eye_symbol(symbol_name='BigResNet', input_nc = 3, output_nc = 17 * 2):
return LeftEye(symbol_name, input_nc, output_nc)
if __name__=='__main__':
pass