Spaces:
Build error
Build error
Download image_encoder.py from unthinkablee/xraydentt: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/unthinkablee/xraydentt/resolve/main/image_encoder.py
- Command line
-
hf download hf://spaces/unthinkablee/xraydentt/image_encoder.py
-
curl -L -o image_encoder.py https://huggingface.co/spaces/unthinkablee/xraydentt/resolve/main/image_encoder.py
1.86 kB
| import os | |
| import glob | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import pickle | |
| import torch.nn.functional as F | |
| import torch | |
| import torch.nn as nn | |
| # image encoder for the first input PX image | |
| # the output is concatanated with embedded point vector and propagated to MLP part. | |
| class UNET(nn.Module): | |
| def __init__( | |
| self, in_channels=1, out_channels=128, features=[64, 128, 256, 512], # 128, [64, 128, 256, 512] | |
| ): | |
| super(UNET, self).__init__() | |
| self.ups = nn.ModuleList() | |
| self.downs = nn.ModuleList() | |
| self.pool = nn.MaxPool2d(kernel_size=2, stride=2) | |
| # Down part of UNET | |
| for feature in features: | |
| self.downs.append(DoubleConv(in_channels, feature)) | |
| in_channels = feature | |
| # Up part of UNET | |
| for feature in reversed(features): | |
| self.ups.append( | |
| nn.ConvTranspose2d( | |
| feature*2, feature, kernel_size=2, stride=2, | |
| ) | |
| ) | |
| self.ups.append(DoubleConv(feature*2, feature)) | |
| self.bottleneck = DoubleConv(features[-1], features[-1]*2) | |
| self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1) | |
| def forward(self, x): | |
| skip_connections = [] | |
| for down in self.downs: | |
| x = down(x) | |
| skip_connections.append(x) | |
| x = self.pool(x) | |
| x = self.bottleneck(x) | |
| skip_connections = skip_connections[::-1] | |
| for idx in range(0, len(self.ups), 2): | |
| x = self.ups[idx](x) | |
| skip_connection = skip_connections[idx//2] | |
| if x.shape != skip_connection.shape: | |
| x = F.interpolate(x, skip_connection.shape[2:]) | |
| concat_skip = torch.cat((skip_connection, x), dim=1) | |
| x = self.ups[idx+1](concat_skip) | |
| return self.final_conv(x) |