class Am(): """docstring for Amodel.""" def __init__(self, args): self.vocab_size = args.vocab_size self.gpu_nums = args.gpu_nums self.lr = args.lr self.is_training = args.is_training self._model_init() if self.is_training: self._ctc_init() self.opt_init() def _model_init(self): self.inputs = Input(name='the_inputs', shape=(None, 200, 1)) self.h1 = cnn_cell(32, self.inputs) self.h2 = cnn_cell(64, self.h1) self.h3 = cnn_cell(128, self.h2) self.h4 = cnn_cell(128, self.h3, pool=False) self.h5 = cnn_cell(128, self.h4, pool=False) # 200 / 8 * 128 = 3200 self.h6 = Reshape((-1, 3200))(self.h5) self.h6 = Dropout(0.2)(self.h6) self.h7 = dense(256)(self.h6) self.h7 = Dropout(0.2)(self.h7) self.outputs = dense(self.vocab_size, activation='softmax')(self.h7) self.model = Model(inputs=self.inputs, outputs=self.outputs) self.model.summary() def _ctc_init(self): self.labels = Input(name='the_labels', shape=[None], dtype='float32') self.input_length = Input(name='input_length', shape=[1], dtype='int64') self.label_length = Input(name='label_length', shape=[1], dtype='int64') self.loss_out = Lambda(ctc_lambda, output_shape=(1,), name='ctc')\ ([self.labels, self.outputs, self.input_length, self.label_length]) self.ctc_model = Model(inputs=[ self.labels, self.inputs, self.input_length, self.label_length ], outputs=self.loss_out) def opt_init(self): opt = Adam(lr=self.lr, beta_1=0.9, beta_2=0.999, decay=0.01, epsilon=10e-8) if self.gpu_nums > 1: self.ctc_model = multi_gpu_model(self.ctc_model, gpus=self.gpu_nums) self.ctc_model.compile(loss={ 'ctc': lambda y_true, output: output }, optimizer=opt, metrics=['accuracy']) self.ctc_model.summary()
def test_fit_octave(self): inputs = Input(shape=(32, 3)) high, low = OctaveConv1D(13, kernel_size=3, octave=4)(inputs) high, low = MaxPool1D()(high), MaxPool1D()(low) conv = OctaveConv1D(5, kernel_size=3, octave=4, ratio_out=0.0)([high, low]) flatten = Flatten()(conv) outputs = Dense(units=2, activation='softmax')(flatten) model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy') model.summary(line_length=200) self._test_fit(model)
def test_make_dual_lambda(self): inputs = Input(shape=(32, 32, 3)) conv = OctaveConv2D(13, kernel_size=3)(inputs) pool = OctaveConvDual()(conv, lambda: MaxPool2D()) conv = OctaveConv2D(7, kernel_size=3)(pool) pool = OctaveConvDual()(conv, lambda: MaxPool2D()) conv = OctaveConv2D(5, kernel_size=3, ratio_out=0.0)(pool) flatten = Flatten()(conv) outputs = Dense(units=2, activation='softmax')(flatten) model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy') model.summary(line_length=200) self._test_fit(model)
def test_fit_channels_first(self): inputs = Input(shape=(3, 32, 32)) high, low = OctaveConv2D(13, kernel_size=3, data_format='channels_first')(inputs) high, low = MaxPool2D(data_format='channels_first')(high), MaxPool2D(data_format='channels_first')(low) high, low = OctaveConv2D(7, kernel_size=3, data_format='channels_first')([high, low]) high, low = MaxPool2D(data_format='channels_first')(high), MaxPool2D(data_format='channels_first')(low) conv = OctaveConv2D(5, kernel_size=3, ratio_out=0.0, data_format='channels_first')([high, low]) flatten = Flatten()(conv) outputs = Dense(units=2, activation='softmax')(flatten) model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy') model.summary(line_length=200) self._test_fit(model, data_format='channels_first')
def get_model(model_name='c2_net', train_mode=True): input = Input(shape=(RESNET_SIZE, RESNET_SIZE, 3)) if model_name == 'c1_net': x = get_custom_model(input, train_mode=train_mode) elif model_name == 'c2_net': x = get_custom_model2(input, train_mode=train_mode) else: x = get_resnet_transfer_model(input, train_mode=train_mode, freeze_reznet=True) if USE_6POSE is True: out = Dense(6, activation='softmax', name='pose_dense_ouptut', trainable=train_mode)(x) else: out = Dense(3, activation=None, name='pose_dense_ouptut', trainable=train_mode)(x) model = Model(inputs=input, outputs=out) if USE_ADAM_OPT is True: optimizer = tf.compat.v1.train.AdamOptimizer( learning_rate=0.0001 ) # Adam(lr=0.05) tf.compat.v1.train.AdamOptimizer(learning_rate=0.05) else: optimizer = tf.compat.v1.train.MomentumOptimizer(learning_rate=0.001, momentum=0.3) if train_mode: model.compile(optimizer, loss='mse', metrics=['accuracy', 'mae', custom_acc ]) # mse -> mean sqare error | 'accuracy' else: model.compile( optimizer, loss='mae', metrics=['accuracy', 'mae', custom_acc] ) # mse -> mean sqare error | 'accuracy' | mae -> mean absolute error model.summary() return model
class DeepAgent: """ This algorithm is trying to use a DQN agent that learns himself just given a gym. After quite some trouble with various error messages, this now at least runs and trains. It does not yet achieve good results. Best result: ??? """ def __init__(self, shape, action_count: int): super().__init__() inp = Input(shape=shape) flat = Flatten()(inp) # Activation: relu, sigmoid, ... hidden1 = Dense(256, activation='relu')(flat) hidden2 = Dense(64, activation='relu')(hidden1) hidden3 = Dense(16, activation='relu')(hidden2) output = Dense(action_count, activation='softmax')(hidden3) self.model = Model(inputs=inp, outputs=output) print(self.model.summary()) self.memory = SequentialMemory(limit=50000, window_length=WINDOW_LENGTH) self.policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=1., value_min=.1, value_test=.05, nb_steps=1000) self.callbacks = self.build_callbacks("msnake") self.dqn = DQNAgent(model=self.model, nb_actions=action_count, memory=self.memory, nb_steps_warmup=50, target_model_update=1e-2, policy=self.policy) Adam._name = "fix_bug" # https://github.com/keras-rl/keras-rl/issues/345 # Metrics: mae, mse, accuracy # LR: learning rate self.dqn.compile(Adam(lr=1e-5), metrics=['mse']) def build_callbacks(self, env_name): callbacks = [] checkpoint_weights_filename = 'dqn_' + env_name + '_weights_{step}.h5f' callbacks += [ ModelIntervalCheckpoint(checkpoint_weights_filename, interval=5000) ] log_filename = 'dqn_{}_log.json'.format(env_name) callbacks += [FileLogger(log_filename, interval=100)] return callbacks
class ChessModel: """ The model which can be trained to take observations of a game of chess and return value and policy predictions. Inpired by https://github.com/Zeta36/chess-alpha-zero/blob/master/src/chess_zero/agent/model_chess.py Attributes: :ivar Config config: configuration to use :ivar Model model: the Keras model to use for predictions """ def __init__(self, config): self.config = config self.model = None # type: Model self.digest = None self.api = None def build(self): """ Builds the full Keras model and stores it in self.model. """ mc = self.config in_x = x = Input((12, 8, 8)) # (batch, channels, height, width) x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_first_filter_size, padding="same", data_format="channels_first", use_bias=False, kernel_regularizer=l2(mc.l2_reg), name="input_conv-" + str(mc.cnn_first_filter_size) + "-" + str(mc.cnn_filter_num))(x) x = BatchNormalization(axis=1, name="input_batchnorm")(x) x = Activation("relu", name="input_relu")(x) for i in range(mc.res_layer_num): x = self._build_residual_block(x, i + 1) res_out = x # for policy output x = Conv2D(filters=2, kernel_size=1, data_format="channels_first", use_bias=False, kernel_regularizer=l2(mc.l2_reg), name="policy_conv-1-2")(res_out) x = BatchNormalization(axis=1, name="policy_batchnorm")(x) x = Activation("relu", name="policy_relu")(x) x = Flatten(name="policy_flatten")(x) policy_out = Dense(self.config.n_labels, kernel_regularizer=l2(mc.l2_reg), activation="softmax", name="policy_out")(x) # for value output x = Conv2D(filters=4, kernel_size=1, data_format="channels_first", use_bias=False, kernel_regularizer=l2(mc.l2_reg), name="value_conv-1-4")(res_out) x = BatchNormalization(axis=1, name="value_batchnorm")(x) x = Activation("relu", name="value_relu")(x) x = Flatten(name="value_flatten")(x) x = Dense(mc.value_fc_size, kernel_regularizer=l2(mc.l2_reg), activation="relu", name="value_dense")(x) value_out = Dense(1, kernel_regularizer=l2(mc.l2_reg), activation="tanh", name="value_out")(x) self.model = Model(in_x, [policy_out, value_out], name="chess_model") def _build_residual_block(self, x, index): # mc = self.config.model mc = self.config in_x = x res_name = "res" + str(index) x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same", data_format="channels_first", use_bias=False, kernel_regularizer=l2(mc.l2_reg), name=res_name + "_conv1-" + str(mc.cnn_filter_size) + "-" + str(mc.cnn_filter_num))(x) x = BatchNormalization(axis=1, name=res_name + "_batchnorm1")(x) x = Activation("relu", name=res_name + "_relu1")(x) x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same", data_format="channels_first", use_bias=False, kernel_regularizer=l2(mc.l2_reg), name=res_name + "_conv2-" + str(mc.cnn_filter_size) + "-" + str(mc.cnn_filter_num))(x) x = BatchNormalization(axis=1, name="res" + str(index) + "_batchnorm2")(x) x = Add(name=res_name + "_add")([in_x, x]) x = Activation("relu", name=res_name + "_relu2")(x) return x def compile(self, optimizer, loss, metrics, loss_weights=None): self.model.compile(optimizer=optimizer, loss=loss, metrics=metrics, loss_weights=loss_weights) return self.model def fit(self, dataset, y=None, validation_data=None, batch_size=None, epochs=10, shuffle=True, val_split=None, callbacks=None): self.model.fit(x=dataset, y=y, batch_size=batch_size, epochs=epochs, shuffle=True, validation_split=val_split, validation_data=validation_data, callbacks=callbacks) return self.model def predict(self, x, batch_size=None, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False): value = self.model.predict(x=x, batch_size=batch_size, steps=steps, callbacks=callbacks, max_queue_size=max_queue_size, workers=workers, use_multiprocessing=use_multiprocessing) return value def summary(self): self.model.summary(line_length=None, positions=None, print_fn=None)
def run(self, ctx, exa, train: bool): session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) session = tf.Session(config=session_config) tf.keras.backend.set_session(session) config = self.read_config(exa) batch_size = config["batch_size"] epochs = config["epochs"] steps_per_epoch = ctx.size() // batch_size use_cache = config["use_cache"] load_path = None if "model_load_bucketfs_path" in config: load_path = config["model_load_bucketfs_path"] save_url = None if "model_save_bucketfs_url" in config: save_url = config["model_save_bucketfs_url"] save_path = config["model_temporary_save_path"] dataset = DatasetUtils().create_generator_dataset( ctx, epochs, batch_size, use_cache, exa.meta.input_columns) with tf.device(config["device"]): input_columns, keras_inputs, preprocessed_keras_inputs = \ ColumnEncoder().generate_inputs( exa.meta.input_columns, config["columns"]) table_network = self.create_table_network( preprocessed_keras_inputs) output_columns, keras_outputs, losses, loss_weights, output_metrics = \ ColumnEncoder().generate_outputs( exa.meta.input_columns, table_network, config["columns"]) session.run(tf.tables_initializer()) dataset = DatasetUtils().create_dataset(dataset, input_columns, output_columns, batch_size, use_cache) session.run(tf.global_variables_initializer()) session.run(tf.local_variables_initializer()) dataset_iterator = dataset.make_initializable_iterator() session.run(dataset_iterator.initializer) saver = tf.train.Saver(max_to_keep=1, save_relative_paths=True) print("load_path", load_path, flush=True) if load_path is not None and load_path != "": initial_epoch = Utils().restore_model_and_get_inital_epoch( session, saver, load_path + "/checkpoints/tmp/save") else: initial_epoch = 0 callbacks = Utils().create_callbacks(session, saver, save_path) model = Model(inputs=keras_inputs, outputs=keras_outputs) profile = config["profile"] profile_model_options = Utils().add_profiler( callbacks, profile, session, save_path) print(output_metrics, flush=True) model.compile(optimizer='rmsprop', loss=losses, loss_weights=loss_weights, metrics=output_metrics, **profile_model_options) print(model.summary(), flush=True) if train: print("Starting training", flush=True) history = model.fit(dataset_iterator, steps_per_epoch=steps_per_epoch, epochs=initial_epoch + epochs, verbose=2, callbacks=callbacks, initial_epoch=initial_epoch) ctx.emit(str(history.history)) print("save_url", save_url, flush=True) if save_url != "" and save_url is not None: tarfile = f"/tmp/save" os.makedirs(tarfile, exist_ok=True) self.tar_save(save_path, tarfile) self.upload_save(save_url, tarfile) else: print("Starting prediction", flush=True) for i in range(steps_per_epoch): print(f"Predicting Batch {i}/steps_per_epoch", flush=True) output = model.predict(dataset_iterator, steps=1) ctx.emit(output)