Dirry525 commited on
Commit
212a3ca
·
verified ·
1 Parent(s): fb47378

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +60 -0
README.md ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - diffusers
6
+ - class-conditional-image-generation
7
+ - diffusion-models-class
8
+ ---
9
+
10
+
11
+
12
+ # This model is a diffusion model for generate number.
13
+
14
+ ## Usage
15
+
16
+ ```python
17
+ # 定义模型
18
+ class ClassConditionalUnet(nn.Module):
19
+ def __init__(self, num_classes=10, class_emb_size=4):
20
+ super().__init__()
21
+ # 将数字所属的类别映射到一个长度为class_emb_size的特征向量
22
+ self.class_emb = nn.Embedding(num_classes, class_emb_size)
23
+ # self.model就是一个不带条件的unet模型,在这里给他添加额外的输入通道,用于接收条件信息
24
+ self.model = UNet2DModel(
25
+ sample_size=28, #生成的图像是28*28
26
+ in_channels=1 + class_emb_size, #加入额外的输入通道
27
+ out_channels=1, # 输入单通道黑白数字图
28
+ layers_per_block=2, # 设置一个unet模块有多少个残差连接层
29
+ block_out_channels=(32, 64, 64),
30
+ down_block_types=(
31
+ "DownBlock2D", #普通的ResNet下采样模块
32
+ "AttnDownBlock2D", #含有spatial self-attention的下采样和模块
33
+ "AttnDownBlock2D",
34
+
35
+ ),
36
+ up_block_types=(
37
+ "AttnUpBlock2D", #含有spatial self-attention的ResNet上采样模块
38
+ "AttnUpBlock2D",
39
+ "UpBlock2D",
40
+ ),
41
+ )
42
+
43
+ def forward(self, x, t, class_labels):
44
+ bs, ch, w, h = x.shape
45
+ class_cond = self.class_emb(class_labels) # 将类别映射为向量形式
46
+ class_cond = class_cond.view(bs, class_cond.shape[1], 1, 1).expand(bs, class_cond.shape[1], w,
47
+ h) # 拓展张量形状
48
+ net_input = torch.cat((class_cond, x), dim=1)
49
+ return self.model(net_input, t).sample
50
+ ckpt = torch.load("class_cond_unet.pth", map_location="cpu")
51
+
52
+ model = ClassConditionalUnet(
53
+ num_classes=ckpt["num_classes"],
54
+ class_emb_size=ckpt["class_emb_size"]
55
+ )
56
+
57
+ model.load_state_dict(ckpt["model_state_dict"])
58
+ model.eval()
59
+ noise_scheduler = DDPMScheduler.from_pretrained("Dirry525/class_num_generator")
60
+ ```