import torch import math import torch.optim as optim import gc #torch.cuda.emptycache()…
- User
import torch import math import torch.optim as optim import gc
#torch.cuda.empty_cache() #gc.collect()
class ResidualAttentionTranspose(nn.Module): def init(self, dim): super(ResidualAttentionTranspose, self).init() self.linear1 = ResidualLinear(dim, dim) self.attenton = nn.MultiheadAttention(dim, 8, batch_first=True)
codedef forward(self, x): shape = x.shape x = x.flatten(2).transpose(1, 2) x = self.linear1(x) attn_output, attn_weights = self.attenton(x, x, x) return (x + attn_output).view(shape)class ResidualAttentionFlatten(nn.Module): def init(self, dim): super(ResidualAttentionFlatten, self).init() self.linear1 = ResidualLinear(dim, dim) self.attenton = nn.MultiheadAttention(dim, 8, batch_first=True)
codedef forward(self, x): shape = x.shape x = x.flatten(2) x = self.linear1(x) attn_output, attn_weights = self.attenton(x, x, x) return (x + attn_output).view(shape)class PrintShape(nn.Module): def init(self): super(PrintShape, self).init()
codedef forward(self, x): print(x.shape) return xencoder = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=64, kernel_size=16, stride=4, padding=6), #PrintShape(), #ResidualAttentionFlatten(64), nn.Conv2d( in_channels=64, out_channels=128, kernel_size=3, stride=2, padding=1), #PrintShape(), #ResidualAttentionFlatten(32*32), nn.Conv2d( in_channels=128, out_channels=256, kernel_size=3, stride=2, padding=1), #PrintShape(), ResidualAttentionFlatten(1024), nn.Conv2d( in_channels=256, out_channels=512, kernel_size=3, stride=2, padding=1), #PrintShape(), ResidualAttentionFlatten(256), ).to('cuda')
#linear = ResidualAttention(768) decoder = nn.Sequential( nn.ConvTranspose2d(in_channels=512, out_channels=256, kernel_size=4, stride=2, padding=1), #ResidualAttentionFlatten(1024), #PrintShape(), #ResidualAttentionTranspose(256), nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size=4, stride=2, padding=1), #PrintShape(), #ResidualAttentionTranspose(128), nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=4, stride=2, padding=1), #ResidualAttentionTranspose(32), nn.ConvTranspose2d(in_channels=64, out_channels=32, kernel_size=4, stride=2, padding=1 ), #ResidualAttentionTranspose(16), nn.ConvTranspose2d(in_channels=32, out_channels=3, kernel_size=4, stride=2, padding=1 ), #nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=4, stride=2, padding=1 ),
- Assistant
- User
- Assistant