def __init__(self, opt): BaseModel.__init__(self, opt) self.net_names = ['G'] self.net_G = Unet(inchannel=3, outchannel=3, ndf=64, enc_blocks=1, dec_blocks=1, depth=3, concat=True, bilinear=self.opt.bilinear, norm_layer='LN') if self.isTrain: self.loss_names = ['G_ad_gen', 'D', 'G', 'G_style', 'G_scene'] self.net_names += ['D'] self.net_D = ResDiscriminator(ndf=32, img_f=128, layers=4, use_spect=True) self.GANloss = AdversarialLoss('lsgan').to(self.device) self.Styleloss = vgg_style_loss().to(self.device) self.sceneloss = scene_loss(self.opt.scenepath).to(self.device) self.optim_G = torch.optim.Adam(self.net_G.parameters(), lr=opt.lr, betas=(0.0, 0.999)) self.optim_D = torch.optim.Adam(self.net_D.parameters(), lr=opt.lr * opt.lrg2d, betas=(0.0, 0.999))
def cnn(base_model_name, input_shape, learning_rate, feature_dims): """It creates a convolutional network model using the input as a base model. Arguments: base_model_name {string} -- A string containing the name of a base model. input_shape {(int, int, int))} -- A tuple to indicate the shape of inputs. learning_rate {float} -- A float value to control speed of learning. feature_dims {int} -- An integer indicating the dimensions of the feature vector. Returns: {A Model object} -- A keras model. """ #Base model base_model = BaseModel(base_model_name, input_shape) #Model model = base_model.cnn(feature_dims) #Create an optimizer object adam_optimizer = Adam(lr = learning_rate) #Compile the model model.compile(loss = 'categorical_crossentropy', optimizer = adam_optimizer, metrics = ['categorical_accuracy']) model.summary() return model
def validate_base_model_creation(self, feature_model_name, module_path): #Arrange base_model = BaseModel(feature_model_name, input_shape) #Act & Assert with mock_patch(module_path) as base_mock: base_model.base_model() base_mock.assert_called_once()
def cnn_validate_base_model_creation(self, feature_model_name, module_path): #Arrange base_model = BaseModel(feature_model_name, input_shape) #Act & Assert with mock_patch(module_path) as base_mock: base_model._prepare_model = MagicMock() base_model.cnn(dimensions) base_mock.assert_called_once()
def test_cnn_model(self): #Arrange base_model = BaseModel('inceptionv3', input_shape) #Act model = base_model.cnn(dimensions) #Assert self.assertIsNotNone(model) self.assertTrue(model.layers[-1].name.startswith( LayerSpecification.layer_prefixes[LayerType.Dense][1])) self.assertTrue(model.layers[-2].name.startswith( LayerSpecification.layer_prefixes[LayerType.Dropout][1]))
def __init__(self, opt): BaseModel.__init__(self, opt) self.net_names = ['E_s', 'E_e', 'G_se'] self.net_E_s = Encoder_S(n_downsample=3, ndf=64, norm_layer='LN') self.net_E_e = Encoder_E(inc=3, n_downsample=4, outc=self.opt.dimen_e, ndf=64, usekl=opt.kl) self.net_G_se = Decoder_SE(s_inc=self.net_E_s.outc, e_inc=self.opt.dimen_e, n_upsample=self.net_E_s.n_downsample, norm_layer='LN') if self.isTrain: self.l1loss = nn.L1Loss() self.set_Adam_optims(self.net_names) self.Styleloss = vgg_style_loss().to(self.device) self.loss_names = ['kl', 'style', 'recon', 'ident', 'total']
def siamese_triplet(base_model_name, input_shape, learning_rate, feature_dims): """It creates a siamese triplet model using the input as a base model. Arguments: base_model_name {string} -- A string containing the name of a base model. input_shape {(int, int, int))} -- A tuple to indicate the shape of inputs. learning_rate {float} -- A float value to control speed of learning. feature_dims {int} -- An integer indicating the dimensions of the feature vector. Returns: {A Model object} -- A keras model. """ #Layer name guidances anchor_name_guidance = 'Anchor' positive_name_guidance = 'Positive' negative_name_guidance = 'Negative' #Base model handler base_model = BaseModel(base_model_name, input_shape) #Feature models feature_model = base_model.base_model() #Siamese inputs anchor_input = Input(shape = input_shape, name = anchor_name_guidance) positive_input = Input(shape = input_shape, name = positive_name_guidance) negative_input = Input(shape = input_shape, name = negative_name_guidance) #Feature vectors anchor_features = feature_model(anchor_input) positive_features = feature_model(positive_input) negative_features = feature_model(negative_input) #Loss layer X = Lambda(triplet_loss, output_shape = (1, ))([anchor_features, positive_features, negative_features]) #Create an optimizer object adam_optimizer = Adam(lr = learning_rate) model = Model(inputs = [anchor_input, positive_input, negative_input], outputs = [X], name = 'Siamese Triplet') model.compile(loss = 'mae', optimizer = adam_optimizer, metrics = ['accuracy']) model.summary() return model
def __init__(self, opt): BaseModel.__init__(self, opt) self.net_names = ['E_s', 'E_e', 'D_se'] self.net_E_s = S_Encoder() self.net_E_e = E_Encoder(outc=self.opt.dimen_e, ndf=64, usekl=True) self.net_D_se = SE_Decoder(e_inc=self.opt.dimen_e) if self.isTrain: self.l1loss = nn.L1Loss() self.Styleloss = vgg_style_loss().to(self.device) self.loss_names = ['kl', 'style', 'recon', 'ident', 'total'] self.optim_E_s = torch.optim.Adam(self.net_E_s.parameters(), lr=opt.lr, betas=(0.0, 0.999)) self.optim_E_e = torch.optim.Adam(self.net_E_e.parameters(), lr=opt.lr, betas=(0.0, 0.999)) self.optim_D_se = torch.optim.Adam(self.net_D_se.parameters(), lr=opt.lr, betas=(0.0, 0.999))
def __init__(self, opt): BaseModel.__init__(self, opt) self.net_names = ['G'] self.net_G = Unet(inchannel=3, outchannel=3, ndf=64, enc_blocks=2, dec_blocks=2, depth=3, bilinear=self.opt.bilinear, norm_layer='LN') if self.isTrain: self.loss_names = ['G_ad_gen', 'D', 'G', 'G_style', 'G_scene'] self.net_names += ['D'] self.net_D = ResDiscriminator(ndf=32, img_f=128, layers=4, use_spect=True) self.GANloss = AdversarialLoss('lsgan').to(self.device) self.Styleloss = vgg_style_loss().to(self.device) self.sceneloss = scene_loss('./checkpoints/net_E_s.path').to( self.device) self.set_Adam_optims(self.net_names)
def test_invalid_base_model_name(self): #Arrange Act & Assert with self.assertRaises(ValueError): base_model = BaseModel('nanana', input_shape) base_model.base_model()
def siamese_network(base_model_name, input_shape, learning_rate, feature_dims): """It creates a siamese network model using the input as a base model. Arguments: base_model_name {string} -- A string containing the name of a base model. input_shape {(int, int, int))} -- A tuple to indicate the shape of inputs. learning_rate {float} -- A float value to control speed of learning. feature_dims {int} -- An integer indicating the dimensions of the feature vector. Returns: {A Model object} -- A keras model. """ #Layer name guidances anchor_name_guidance = 'Anchor' sample_name_guidance = 'Sample' #Base model handler base_model = BaseModel(base_model_name, input_shape) #Feature models feature_model = base_model.base_model() #Siamese inputs anchor_input = Input(shape = input_shape, name = anchor_name_guidance) sample_input = Input(shape = input_shape, name = sample_name_guidance) #Feature vectors anchor_features = feature_model(anchor_input) sample_features = feature_model(sample_input) lambda_product = Lambda(lambda x : x[0] * x[1])([anchor_features, sample_features]) lambda_add = Lambda(lambda x : x[0] + x[1])([anchor_features, sample_features]) lambda_abs = Lambda(lambda x : K.abs(x[0] - x[1]))([anchor_features, sample_features]) lambda_eucledian_dist = Lambda(lambda x: K.square(x))(lambda_abs) #Layer specifications layer_specifications = [ #Concatenate and reshape lambda outputs LayerSpecification(LayerType.Concatenate), LayerSpecification(LayerType.Reshape, (4, feature_model.output_shape[1], 1)), #Convolution layer #1 LayerSpecification(LayerType.Conv2D, 32, (4, 1), activation = 'relu', padding = 'valid'), LayerSpecification(LayerType.Reshape, (feature_model.output_shape[1], 32, 1)), #Convolution layer #2 LayerSpecification(LayerType.Conv2D, 1, (1, 32), activation = 'linear', padding = 'valid'), #Flatten LayerSpecification(LayerType.Flatten), #Output unit LayerSpecification(LayerType.Dense, 1, activation = 'sigmoid', use_bias = True) ] #Model specification model_specification = ModelSpecification(layer_specifications) #Output X = model_specification.get_specification([lambda_product, lambda_add, lambda_abs, lambda_eucledian_dist]) #Create an optimizer object adam_optimizer = Adam(lr = learning_rate) model = Model(inputs = [anchor_input, sample_input], outputs = [X], name = 'Siamese Model') model.compile(loss = 'binary_crossentropy', optimizer = adam_optimizer, metrics = ['accuracy']) model.summary() return model