def get_model(protos_per_class=1): inputs = Input(shape=input_shape) diss = TangentDistance(linear_factor=None, squared_dissimilarity=True, projected_atom_shape=12, signal_output='signals') caps = Capsule(prototype_distribution=(protos_per_class, 10)) caps.add( InputModule(signal_shape=(-1, np.prod(input_shape)), trainable=False, init_diss_initializer='zeros')) caps.add(diss) caps.add(SqueezeRouting()) caps.add(NearestCompetition()) output = caps(inputs)[1] # pre-train the model over 10000 random digits idx = np.random.randint(0, len(x_train) - 1, (min(10000, len(x_train)), )) pre_train_model = Model(inputs=inputs, outputs=diss.input[0]) diss_input = pre_train_model.predict(x_train[idx, :], batch_size=batch_size) diss.pre_training(diss_input, y_train[idx], capsule_inputs_are_equal=True) # define model and return model = Model(inputs, output) return model
def get_model(): inputs = Input(shape=input_shape) # get the dissimilarity either GLVQ, GTLVQ, or GMLVQ if args.mode == 'glvq': diss = MinkowskiDistance(linear_factor=None, squared_dissimilarity=True, signal_output='signals') elif args.mode == 'gtlvq': diss = RestrictedTangentDistance(linear_factor=None, squared_dissimilarity=True, signal_output='signals', projected_atom_shape=1) elif args.mode == 'gmlvq': # get identity matrices for the matrix initialization as we do not # use the standard routine of anysma for the initialization matrix_init = np.repeat(np.expand_dims(np.eye(2), 0), repeats=np.sum(protos_per_class), axis=0) matrix_init.astype(K.floatx()) diss = OmegaDistance(linear_factor=None, squared_dissimilarity=True, signal_output='signals', matrix_scope='local', matrix_constraint='OmegaNormalization', matrix_initializer=lambda x: matrix_init) # define capsule network caps = Capsule(prototype_distribution=protos_per_class) caps.add( InputModule(signal_shape=(-1, np.prod(input_shape)), trainable=False, init_diss_initializer='zeros')) caps.add(diss) caps.add(SqueezeRouting()) caps.add(NearestCompetition()) output = caps(inputs)[1] # pre-train the model and overwrite the standard initialization matrix # for GMLVQ if args.mode == 'gmlvq': _, matrices = diss.get_weights() pre_train_model = Model(inputs=inputs, outputs=diss.input[0]) diss_input = pre_train_model.predict(x_train, batch_size=batch_size) diss.pre_training(diss_input, y_train, capsule_inputs_are_equal=True) if args.mode == 'gmlvq': # set identity matrices centers, _ = diss.get_weights() diss.set_weights([centers, matrices]) # define model and return model = Model(inputs, output) return model
def get_model(protos_per_class=1): inputs = Input(shape=input_shape) diss = OmegaDistance(linear_factor=None, squared_dissimilarity=True, matrix_scope='global', matrix_constraint='OmegaNormalization', signal_output='signals', matrix_initializer='identity') caps = Capsule(prototype_distribution=(protos_per_class, 10)) caps.add(InputModule(signal_shape=(-1, np.prod(input_shape)), trainable=False, init_diss_initializer='zeros')) caps.add(diss) caps.add(SqueezeRouting()) caps.add(NearestCompetition()) output = caps(inputs)[1] # pre-train the model over 10000 random digits # skip the svd for GMLVQ _, matrix = diss.get_weights() idx = np.random.randint(0, len(x_train) - 1, (min(10000, len(x_train)),)) pre_train_model = Model(inputs=inputs, outputs=diss.input[0]) diss_input = pre_train_model.predict(x_train[idx, :], batch_size=batch_size) diss.pre_training(diss_input, y_train[idx], capsule_inputs_are_equal=True) # set identity matrices centers, _ = diss.get_weights() diss.set_weights([centers, matrix]) # define model and return model = Model(inputs, output) return model
def get_model(): inputs = Input(shape=input_shape) diss = TangentDistance(linear_factor=None, squared_dissimilarity=True, projected_atom_shape=15, signal_output='signals') # compute the prototype distribution proto_distrib = list(np.minimum( np.ceil(np.sum(y_train, 0) / 100).astype('int'), 5)) # class 'Buildings-Grass-Trees-Drives' proto_distrib[-2] = 2 # class 'Corn-mintill' proto_distrib[2] = 4 print('proto_distrib: ' + str(proto_distrib)) # define capsule network caps = Capsule(prototype_distribution=proto_distrib) caps.add(InputModule(signal_shape=(-1, np.prod(input_shape)), trainable=False, init_diss_initializer='zeros')) caps.add(diss) caps.add(SqueezeRouting()) caps.add(NearestCompetition()) output = caps(inputs)[1] # pre-train the model pre_train_model = Model(inputs=inputs, outputs=diss.input[0]) diss_input = pre_train_model.predict(x_train, batch_size=batch_size) diss.pre_training(diss_input, y_train, capsule_inputs_are_equal=True) # define model and return model = Model(inputs, output) return model