sigmoidneuron123 commited on
Commit
f7837d4
·
verified ·
1 Parent(s): e85cb0c

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +55 -0
model.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import nn
2
+ from transformers import PreTrainedModel
3
+ from transformers import PretrainedConfig
4
+ from torchvision import transforms
5
+ from transformers.models.auto.configuration_auto import CONFIG_MAPPING
6
+ from transformers.models.auto.modeling_auto import MODEL_MAPPING
7
+
8
+ class ImageToImageConfig(PretrainedConfig):
9
+ model_type = "upscaleing"
10
+
11
+ def __init__(self, in_channels=3, out_channels=3,in_resolution=256,out_resolution=768, **kwargs):
12
+ super().__init__(**kwargs)
13
+ self.in_channels = in_channels
14
+ self.out_channels = out_channels
15
+ self.in_resolution = in_resolution
16
+ self.out_resolution = out_resolution
17
+
18
+ class ImageToImageModel(PreTrainedModel):
19
+ config_class = ImageToImageConfig
20
+
21
+ def __init__(self, config):
22
+ super().__init__(config)
23
+ self.model = nn.Sequential(
24
+ nn.Conv2d(in_channels=config.in_channels, out_channels=64, kernel_size=5, padding=2),
25
+ nn.ReLU(),
26
+ nn.Conv2d(in_channels=64, out_channels=32, kernel_size=3, padding=1),
27
+ nn.ReLU(),
28
+ nn.Conv2d(in_channels=32, out_channels=3 * 3 * 3, kernel_size=3, padding=1),
29
+ nn.PixelShuffle(3)
30
+ )
31
+ self.transform1 = transforms.Compose(
32
+ transforms=(
33
+ transforms.Resize((config.in_resolution,config.in_resolution)),
34
+ transforms.ToTensor()
35
+ )
36
+ )
37
+ self.transform2 = transforms.Compose(
38
+ transforms=(
39
+ transforms.ToPILImage(),
40
+ transforms.Resize((config.out_resolution,config.out_resolution))
41
+ )
42
+ )
43
+
44
+ def forward(self, image):
45
+ x = self.transform1(image)
46
+ x = self.model(x)
47
+ x = self.transform2(x)
48
+ return x
49
+
50
+ CONFIG_MAPPING.register("upscaleing", ImageToImageConfig)
51
+ MODEL_MAPPING.register(ImageToImageConfig, ImageToImageModel)
52
+
53
+ config = ImageToImageConfig()
54
+ model = ImageToImageModel(config)
55
+ model.save_pretrained("AIupscaleing")