File size: 7,376 Bytes
4258b21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union

import torch
import torch.nn as nn
import torch.utils.checkpoint
from collections import OrderedDict
from .outputs import BaseOutput
from model.diffusers.models.unet_2d_condition import UNet2DConditionModel

all_feature_dic={}

def clear_feature_dic():
    global all_feature_dic
    all_feature_dic={}
    all_feature_dic["low"]=[]
    all_feature_dic["mid"]=[]
    all_feature_dic["high"]=[]
    all_feature_dic["highest"]=[]
def get_feature_dic():
    global all_feature_dic
    return all_feature_dic


class UNet2DConditionOutput(BaseOutput):
    """
    Args:
        sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Hidden states conditioned on `encoder_hidden_states` input. Output of last layer of model.
    """

    sample: torch.FloatTensor


class UNet2D(UNet2DConditionModel):


    def forward(
        self,
        sample: torch.FloatTensor,
        timestep: Union[torch.Tensor, float, int],
        encoder_hidden_states: torch.Tensor,
        return_dict: bool = True,
    ) -> Union[UNet2DConditionOutput, Tuple]:
        """r
        Args:
            sample (`torch.FloatTensor`): (batch, channel, height, width) noisy inputs tensor
            timestep (`torch.FloatTensor` or `float` or `int): (batch) timesteps
            encoder_hidden_states (`torch.FloatTensor`): (batch, channel, height, width) encoder hidden states
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`models.unet_2d_condition.UNet2DConditionOutput`] instead of a plain tuple.

        Returns:
            [`~models.unet_2d_condition.UNet2DConditionOutput`] or `tuple`:
            [`~models.unet_2d_condition.UNet2DConditionOutput`] if `return_dict` is True, otherwise a `tuple`. When
            returning a tuple, the first element is the sample tensor.
        """
        # 0. center input if necessary
        if self.config.center_input_sample:
            sample = 2 * sample - 1.0

        # 1. time
        timesteps = timestep
        if not torch.is_tensor(timesteps):
            timesteps = torch.tensor([timesteps], dtype=torch.long, device=sample.device)
        elif torch.is_tensor(timesteps) and len(timesteps.shape) == 0:
            timesteps = timesteps.to(dtype=torch.float32)
            timesteps = timesteps[None].to(device=sample.device)
        

#         if timesteps[0]==0:
#             flag_time=True
#         else:
#             flag_time=False
        
        if timesteps[0]==0 or timesteps[0]==1:
            flag_time=True
        else:
            flag_time=False    
            
        # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
        timesteps = timesteps.expand(sample.shape[0])

        t_emb = self.time_proj(timesteps)
        emb = self.time_embedding(t_emb)

        # 2. pre-process
        sample = self.conv_in(sample)
        
        if flag_time:
            reshape_h=sample.reshape(int(sample.size()[0]/2),int(sample.size()[1]*2),sample.size()[2],sample.size()[3])
            if reshape_h.size()[2]==8:
                all_feature_dic["low"].append(reshape_h)
            elif reshape_h.size()[2]==16:
                all_feature_dic["mid"].append(reshape_h)
            elif reshape_h.size()[2]==32:
                all_feature_dic["high"].append(reshape_h)
            elif reshape_h.size()[2]==64:
                all_feature_dic["highest"].append(reshape_h)
                    
        # 3. down
        down_block_res_samples = (sample,)
        for downsample_block in self.down_blocks:
            if hasattr(downsample_block, "attentions") and downsample_block.attentions is not None:
                sample, res_samples = downsample_block(
                    hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states
                )
            else:
                sample, res_samples = downsample_block(hidden_states=sample, temb=emb)

            if flag_time:
                for h in res_samples:
                    reshape_h=h.reshape(int(h.size()[0]/2),int(h.size()[1]*2),h.size()[2],h.size()[3])
#                     print(reshape_h.size()[2])
                    if reshape_h.size()[2]==8:
                        all_feature_dic["low"].append(reshape_h)
                    elif reshape_h.size()[2]==16:
                        all_feature_dic["mid"].append(reshape_h)
                    elif reshape_h.size()[2]==32:
                        all_feature_dic["high"].append(reshape_h)
                    elif reshape_h.size()[2]==64:
                        all_feature_dic["highest"].append(reshape_h)
            down_block_res_samples += res_samples

        # 4. mid
        sample = self.mid_block(sample, emb, encoder_hidden_states=encoder_hidden_states)
        if flag_time:
            reshape_h=sample.reshape(int(sample.size()[0]/2),int(sample.size()[1]*2),sample.size()[2],sample.size()[3])
            if reshape_h.size()[2]==8:
                all_feature_dic["low"].append(reshape_h)
            elif reshape_h.size()[2]==16:
                all_feature_dic["mid"].append(reshape_h)
            elif reshape_h.size()[2]==32:
                all_feature_dic["high"].append(reshape_h)
            elif reshape_h.size()[2]==64:
                all_feature_dic["highest"].append(reshape_h)
        
        # 5. up
        for upsample_block in self.up_blocks:
            res_samples = down_block_res_samples[-len(upsample_block.resnets) :]
            down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]

            if hasattr(upsample_block, "attentions") and upsample_block.attentions is not None:
                sample, up_samples = upsample_block(
                    hidden_states=sample,
                    temb=emb,
                    res_hidden_states_tuple=res_samples,
                    encoder_hidden_states=encoder_hidden_states,
                )
            else:
                sample, up_samples = upsample_block(hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples)
            
            if flag_time:
                for h in up_samples:
                    reshape_h=h.reshape(int(h.size()[0]/2),int(h.size()[1]*2),h.size()[2],h.size()[3])
#                     reshape_h=sample.reshape(int(sample.size()[0]/2),int(sample.size()[1]*2),sample.size()[2],sample.size()[3])
                    if reshape_h.size()[2]==8:
                        all_feature_dic["low"].append(reshape_h)
                    elif reshape_h.size()[2]==16:
                        all_feature_dic["mid"].append(reshape_h)
                    elif reshape_h.size()[2]==32:
                        all_feature_dic["high"].append(reshape_h)
                    elif reshape_h.size()[2]==64:
                        all_feature_dic["highest"].append(reshape_h)
                    
        # 6. post-process
        # make sure hidden states is in float32
        # when running in half-precision
        sample = self.conv_norm_out(sample.float()).type(sample.dtype)
        sample = self.conv_act(sample)
        sample = self.conv_out(sample)
        
        
        
        if not return_dict:
            return (sample,)

        return UNet2DConditionOutput(sample=sample)