pytorchでUnetで白黒画像のカラー化

pytorchでUnetで白黒画像をカラー化した

 

データセット

STL-10 dataset
pytorchでは簡単にロードできるので今回は"unlabeled"の10万枚の画像をトレーニングに使用した.

image = STL10(root="D:\datasets", split="unlabeled",
                  transform=transform, download=True)

Unet

f:id:busongames:20190509220952p:plain
https://arxiv.org/abs/1505.04597
デコーダーにエンコーダーの情報を伝える形のモデル. 見た目がUだからUnet.

class Net(nn.Module):
    def __init__(self, in_ch, out_ch):
        super(Net, self).__init__()
        self.inc = d_conv(in_ch, 64)
        self.down1 = down(64, 128)
        self.down2 = down(128, 256)
        self.down3 = down(256, 512)
        self.down4 = down(512, 512)
        self.up1 = up(1024, 256)
        self.up2 = up(512, 128)
        self.up3 = up(256, 64)
        self.up4 = up(128, 64)
        self.outc = nn.Conv2d(64, out_ch, 1)

    def forward(self, x):
        x1 = self.inc(x)
        x2 = self.down1(x1)
        x3 = self.down2(x2)
        x4 = self.down3(x3)
        x5 = self.down4(x4)
        x = self.up1(x5, x4)
        x = self.up2(x, x3)
        x = self.up3(x, x2)
        x = self.up4(x, x1)
        x = self.outc(x)
        return F.sigmoid(x)


class down(nn.Module):
    def __init__(self, in_ch, out_ch):
        super(down, self).__init__()
        self.mpconv = nn.Sequential(
            nn.MaxPool2d(2),
            d_conv(in_ch, out_ch)
        )

    def forward(self, x):
        x = self.mpconv(x)
        return x


class up(nn.Module):
    def __init__(self, in_ch, out_ch, bilinear=True):
        super(up, self).__init__()

        if bilinear:
            self.up = nn.Upsample(
                scale_factor=2, mode='bilinear', align_corners=True)
        else:
            self.up = nn.ConvTranspose2d(in_ch//2, in_ch//2, 2, stride=2)

        self.conv = d_conv(in_ch, out_ch)

    def forward(self, x1, x2):
        x1 = self.up(x1)

        diffY = x2.size()[2] - x1.size()[2]
        diffX = x2.size()[3] - x1.size()[3]

        x1 = F.pad(x1, (diffX // 2, diffX - diffX//2,
                        diffY // 2, diffY - diffY//2))

        x = torch.cat([x2, x1], dim=1)
        x = self.conv(x)
        return x


class d_conv(nn.Module):
    def __init__(self, in_ch, out_ch):
        super(d_conv, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_ch, out_ch, 3, padding=1),
            nn.BatchNorm2d(out_ch),
            nn.LeakyReLU(inplace=True),
            nn.Conv2d(out_ch, out_ch, 3, padding=1),
            nn.BatchNorm2d(out_ch),
            nn.LeakyReLU(inplace=True)
        )

    def forward(self, x):
        x = self.conv(x)
        return x

元論文では畳み込みにpaddingが入っていないがこれはミラーリングを加味したものなので今回は入れた. LeakyReLUを使ってるなら入力とか出力とか[-1,1]のほうがよかったなと今は思う.

その他

  • lossはF.smooth_l1_loss()
  • Tensorにしたりto(device)したりnumpy()したり細かいところ忘れない
  • batch_size大きすぎるとout of memory

学習に時間かかったけどkerasよりは全然早かった

結果

"test"のデータセットに対して予測した.

f:id:busongames:20190511174539p:plain
結果
左が元のグレースケール, 真ん中がGround truth, 右が出力.
すごくうまくいってるんですがもしかして"unlabeled"の画像から"test"画像とってます?
別の猫のデータセットをresizeして入力した結果.
f:id:busongames:20190511180849p:plain
ねこ
やっぱり訓練データに合うようになってました.....