BryanHaley commited on
Commit
0e2fd23
·
verified ·
1 Parent(s): e8f34f1

Add MI-GAN 512 Places2 for Core ML

Browse files
Files changed (3) hide show
  1. migan/LICENSE +21 -0
  2. migan/MIGAN.mlpackage.zip +3 -0
  3. migan/convert.py +34 -0
migan/LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Picsart AI Research (PAIR)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
migan/MIGAN.mlpackage.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c279edffd937627477b716376cea8e51aa8ac42c1691c8b47365020b915903c8
3
+ size 24529826
migan/convert.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys, torch, torch.nn as nn, coremltools as ct
2
+ src, ckpt, out = sys.argv[1], sys.argv[2], sys.argv[3]
3
+ sys.path.insert(0, src)
4
+ from lib.model_zoo.migan_inference import Generator
5
+
6
+ class Wrapped(nn.Module):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.g = Generator(resolution=512)
10
+ self.g.load_state_dict(torch.load(ckpt, map_location="cpu"))
11
+ self.g.eval()
12
+ def forward(self, image, mask):
13
+ # image: -1..1 (from the ImageType scale); mask: 0..1, 1 = the hole.
14
+ known = (mask < 0.5).float()
15
+ x = torch.cat([known - 0.5, image * known], dim=1)
16
+ y = self.g(x)
17
+ return ((y * 0.5 + 0.5) * 255).clamp(0, 255)
18
+
19
+ m = Wrapped().eval()
20
+ ex = (torch.zeros(1, 3, 512, 512), torch.zeros(1, 1, 512, 512))
21
+ traced = torch.jit.trace(m, ex)
22
+ mlm = ct.convert(
23
+ traced,
24
+ inputs=[ct.ImageType(name="image", shape=(1, 3, 512, 512), scale=2 / 255, bias=[-1, -1, -1], color_layout=ct.colorlayout.RGB),
25
+ ct.ImageType(name="mask", shape=(1, 1, 512, 512), scale=1 / 255, color_layout=ct.colorlayout.GRAYSCALE)],
26
+ outputs=[ct.ImageType(name="output", color_layout=ct.colorlayout.RGB)],
27
+ minimum_deployment_target=ct.target.iOS17,
28
+ compute_precision=ct.precision.FLOAT32,
29
+ )
30
+ mlm.author = "Picsart AI Research (MI-GAN, MIT); Core ML conversion for PhotoWagon"
31
+ mlm.license = "MIT"
32
+ mlm.short_description = "MI-GAN 512 Places2: image and mask (white = hole) in, filled image out."
33
+ mlm.save(out)
34
+ print("saved", out)