示例#1
0
    def __init__(self,
                 setting):
        super(ResNetForGeneratorModel, self).__init__(setting)

        self.setting = setting
        ch = SettingHandler(setting)
        input_nc         = int(setting['base']['numberOfInputImageChannels'])
        output_nc        = int(setting['base']['numberOfOutputImageChannels'])
        ngf              = int(setting['models']['resNet']['generator']['filterSize'])
        use_bias         = setting['models']['resNet']['generator']['useBias']
        self.gpu_ids     = ch.get_GPU_ID()
        self.pad_factory = PadFactory(setting)
        self.norm_layer  = NormalizeFactory(setting).create(
                                setting['models']['resNet']['generator']['normalizeLayer'])

        model = [nn.ReflectionPad2d(3),
                 nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias),
                 self.norm_layer(ngf),
                 nn.ReLU(True)
                ]

        n_downsampling = 2
        for i in range(n_downsampling):
            mult   = 2 ** i
            model += [nn.Conv2d(ngf * mult,
                                ngf * mult * 2,
                                kernel_size=3,
                                stride=2,
                                padding=1,
                                bias=use_bias),
                      self.norm_layer(ngf * mult * 2),
                      nn.ReLU(True)
                     ]
        mult = 2**n_downsampling

        for i in range(int(setting['models']['resNet']['generator']['nBlocks'])):
            model += [ResnetBlock(ngf * mult, setting)] 

        for i in range(n_downsampling):
            mult   = 2**(n_downsampling - i)
            model += [nn.ConvTranspose2d(ngf * mult, 
                                         int(ngf * mult / 2),
                                         kernel_size=3, 
                                         stride=2,
                                         padding=1,
                                         output_padding=1,
                                         bias=use_bias),
                      self.norm_layer(int(ngf * mult / 2)),
                      nn.ReLU(True)]

        model += [self.pad_factory.create(setting['models']['resNet']['generator']['paddingType'])(3)]
        model += [nn.Conv2d(ngf, output_nc, kernel_size=(7, 7), padding=0)]
        model += [nn.Tanh()]

        self.model = nn.Sequential(*model)
示例#2
0
    def __init__(self, settings):

        super(UnetForGeneratorModel, self).__init__(settings)
        self.settings = settings
        ch = SettingHandler(settings)
        self.gpu_ids = ch.get_GPU_ID()
        #self.pad_factory = PadFactory(settings)

        self.model = UnetBlock(0, settings)
        for i in range(
                1,
                int(settings['models']['unet']['generator']['numHierarchy'])):
            self.model = UnetBlock(i, settings, [self.model])
示例#3
0
    def __init__(self, settings=None, **kwargs):

        if settings is not None:
            self.is_data_parallel = settings['base']['isDataParallel']
            self.is_showmode_info = settings['ui']['base']['isShowModelInfo']
        else:
            self.is_data_parallel = False
            self.is_showmode_info = False

        self.settings = settings
        ch = SettingHandler(settings)
        self.gpu_ids = ch.get_GPU_ID()
        self.checkpoints_dir = ch.get_check_points_dir()

        self.loss_factory = lf.LossFactory(settings)
        self.optimizer_factory = OptimizerFactory(settings)
        self.show_model_lst = []

        self.model_factory = ModelFactory(settings)
示例#4
0
    def __init__(self, 
                 settings,
                 gpu_ids=[]):

        super(PatchGANModel, self).__init__(settings)


        # get global
        self.settings  = settings
        ch           = SettingHandler(settings)
        self.gpu_ids = ch.get_GPU_ID()
        norm_layer   = ch.get_norm_layer()

        n = 1
        if settings['base']['controller'] == 'pix2pix' or settings['base']['controller'] == 'pix2pixMulti':
            n = 2 # for image pooling
        input_nc = settings['models']['patchGANDiscriminator']['input_n'] * n

        use_bias = True
#        if type(norm_layer) == functools.partial:
#            use_bias = norm_layer.func == nn.InstanceNorm2d
#        else:
#            use_bias = norm_layer == nn.InstanceNorm2d

        
        print(settings['models'])
        kw         = int(settings['models']['patchGANDiscriminator']['kernelSize'])
        padw       = int(settings['models']['patchGANDiscriminator']['paddingSize'])
        ndf        = int(settings['models']['patchGANDiscriminator']['numberOfDiscriminatorFilters'])
        n_layers   = int(settings['models']['patchGANDiscriminator']['nLayers'])
        is_sigmoid = settings['models']['patchGANDiscriminator']['useSigmoid']


        sequence = [
                nn.Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw),
                nn.LeakyReLU(0.2, True)
        ]

        nf_mult = 1
        nf_mult_prev = 1
        for n in range(1, n_layers):
            nf_mult_prev = nf_mult
            nf_mult      = min(2**n, 8)
            sequence += [
                    nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
                              kernel_size=kw, stride=2, padding=padw, bias=use_bias),
                    norm_layer(ndf * nf_mult),
                    nn.LeakyReLU(0.2, True)
            ]

        nf_mult_prev = nf_mult
        nf_mult      = min(2**n_layers, 8)
        sequence += [
                nn.Conv2d(ndf * nf_mult_prev, 
                          ndf * nf_mult,
                          kernel_size=kw, 
                          stride=1,
                          padding=padw,
                          bias=use_bias),
                norm_layer(ndf * nf_mult),
                nn.LeakyReLU(0.2, True),
                nn.Conv2d(ndf * nf_mult, 
                     1, 
                     kernel_size=kw, 
                     stride=1, 
                     padding=padw)]

        if is_sigmoid:
            sequence += [nn.Sigmoid()]

        self.model = nn.Sequential(*sequence)
示例#5
0
    def __init__(self, settings):
        import aimaker.models.model_factory as mf
        import aimaker.loss.loss_factory as lf
        import aimaker.optimizers.optimizer_factory as of

        self.settings = settings
        ch = SettingHandler(settings)

        self.gpu_ids = ch.get_GPU_ID()
        self.checkpoints_dir = ch.get_check_points_dir()
        model_factory = mf.ModelFactory(settings)
        loss_factory = lf.LossFactory(settings)
        optimizer_factory = of.OptimizerFactory(settings)

        # for discriminator regularization
        self.pool_fake_A = ImagePool(
            int(settings['controllers']['cycleGAN']['imagePoolSize']))
        self.pool_fake_B = ImagePool(
            int(settings['controllers']['cycleGAN']['imagePoolSize']))

        name = settings['controllers']['cycleGAN']['generatorModel']
        self.netG_A = model_factory.create(name)
        self.netG_B = model_factory.create(name)
        if len(self.gpu_ids):
            self.netG_A = self.netG_A.cuda(self.gpu_ids[0])
            self.netG_B = self.netG_B.cuda(self.gpu_ids[0])

            name = settings['controllers']['cycleGAN']['discriminatorModel']
            self.netD_A = model_factory.create(name)
            self.netD_B = model_factory.create(name)
            if len(self.gpu_ids):
                self.netD_A = self.netD_A.cuda(self.gpu_ids[0])
                self.netD_B = self.netD_B.cuda(self.gpu_ids[0])

        self.loadModels()

        self.criterionGAN = loss_factory.create("GANLoss")
        self.criterionCycle = loss_factory.create(
            settings['controllers']['cycleGAN']['cycleLoss'])
        self.criterionIdt = loss_factory.create(
            settings['controllers']['cycleGAN']['idtLoss'])
        if len(self.gpu_ids):
            self.criterionGAN = self.criterionGAN.cuda(self.gpu_ids[0])
            self.criterionCycle = self.criterionCycle.cuda(self.gpu_ids[0])
            self.criterionIdt = self.criterionIdt.cuda(self.gpu_ids[0])

            # initialize optimizers
        self.optimizer_G = optimizer_factory.create(
            settings['controllers']['cycleGAN']['generatorOptimizer'])(
                it.chain(self.netG_A.parameters(),
                         self.netG_B.parameters()), settings)

        if settings['data']['base']['isTrain']:
            self.optimizer_D_A = optimizer_factory.create(
                settings['controllers']['cycleGAN']['D_AOptimizer'])(
                    self.netD_A.parameters(), settings)
            self.optimizer_D_B = optimizer_factory.create(
                settings['controllers']['cycleGAN']['D_BOptimizer'])(
                    self.netD_B.parameters(), settings)

        if settings['ui']['base']['isShowModelInfo']:
            self.showModel()