Esempio n. 1
0
File: 09.py Progetto: koshian2/Test
def train():
    data, train_ind, test_ind = load_data()
    print(train_ind.shape, test_ind.shape)

    model = create_train_pconv_unet()
    model.summary()
    model.compile("rmsprop", identity_loss, [PSNR])

    # TPUモデルに変換
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    batch_size = 8
    cb = SamplingCallback(model, data, test_ind)
    scheduler = LearningRateScheduler(lr_decay)

    model.fit_generator(data_generator(data, train_ind, batch_size, True),
                        steps_per_epoch=len(train_ind) // batch_size,
                        validation_data=data_generator(data, test_ind,
                                                       batch_size, False),
                        validation_steps=len(test_ind) // batch_size,
                        callbacks=[cb, scheduler],
                        epochs=70,
                        max_queue_size=5)
def create_wideresnet(k, N, use_tpu):
    input = Input(shape=(32, 32, 3))
    # conv1 : 32x32
    x = Conv2D(16*k, 1)(input)
    x = create_residual_blocks(x, 16, k, N)
    # downsampling 32->16
    x = AveragePooling2D(2)(x)
    x = Conv2D(32*k, 1)(x)
    # conv2 : 16x16
    x = create_residual_blocks(x, 32, k, N)
    # downsampling 16->8
    x = AveragePooling2D(2)(x)
    x = Conv2D(64*k, 1)(x)
    # conv4 : 8x8
    x = create_residual_blocks(x, 64, k, N)
    x = GlobalAveragePooling2D()(x)
    x = Dense(100, activation="softmax")(x)

    model = Model(input, x)
    model.compile(Adam(), loss="categorical_crossentropy", metrics=["acc"])

    if use_tpu:
        tpu_grpc_url = "grpc://"+os.environ["COLAB_TPU_ADDR"]
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu_grpc_url)
        strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
        model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)
    return model
Esempio n. 3
0
def train():
    data, train_ind, test_ind = load_data()
    print(train_ind.shape, test_ind.shape)

    model = create_train_pconv_unet()
    model.load_weights("oppai_train.hdf5")
    model.summary()
    model.compile(tf.train.RMSPropOptimizer(5e-5), identity_loss, [PSNR])

    # TPUモデルに変換
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    batch_size = 8
    cb = SamplingCallback(model, data, test_ind)

    model.fit_generator(data_generator(data, train_ind, batch_size, True),
                        steps_per_epoch=len(train_ind) // batch_size,
                        validation_data=data_generator(data, test_ind,
                                                       batch_size, False),
                        validation_steps=len(test_ind) // batch_size,
                        callbacks=[cb],
                        epochs=70,
                        max_queue_size=1)
def train(inbalance_size):
    (X_train, y_train), (X_test, y_test) = inbalanced_cifar(inbalance_size)
    y_train, y_test = y_train[:, :10], y_test[:, :10]

    model = create_models()
    model.compile("adam", "categorical_crossentropy", ["acc"])
    tf.logging.set_verbosity(tf.logging.FATAL)

    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    scheduler = LearningRateScheduler(step_decay)
    f1 = F1Callback(model, X_test, y_test)

    history = model.fit(X_train,
                        y_train,
                        validation_data=(X_test, y_test),
                        callbacks=[scheduler, f1],
                        batch_size=640,
                        epochs=100,
                        verbose=0).history

    max_acc = max(history["val_acc"])
    max_f1 = max(f1.f1_log)
    print(f"{inbalance_size} {max_acc:.04} {max_f1:.04}")
Esempio n. 5
0
def train(use_augmentation, use_stem_block):
    tf.logging.set_verbosity(tf.logging.FATAL)
    (X_train, y_train), (X_test, y_test) = keras.datasets.cifar10.load_data()
    y_train = keras.utils.to_categorical(y_train)
    y_test = keras.utils.to_categorical(y_test)

    # generator
    batch_size = 512
    scale = 7 if use_stem_block else 1
    train_gen = generator(X_train, y_train, batch_size=batch_size,
                          use_augmentation=use_augmentation, shuffle=True, scale=scale)
    test_gen = generator(X_test, y_test, batch_size=1000,
                         use_augmentation=False, shuffle=False, scale=scale)
    
    # network
    input_shape = (224,224,3) if use_stem_block else (32,32,3)
    model = PeleeNet(input_shape=input_shape, use_stem_block=use_stem_block, n_classes=10)
    model.compile(keras.optimizers.SGD(0.4, 0.9), "categorical_crossentropy", ["acc"])

    # GoogleColab TPU training
    # Plese comment out following 4 lines if train on GPUs
    tpu_grpc_url = "grpc://"+os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    scheduler = keras.callbacks.LearningRateScheduler(lr_scheduler)
    hist = keras.callbacks.History()

    model.fit_generator(train_gen, steps_per_epoch=X_train.shape[0]//batch_size,
                        validation_data=test_gen, validation_steps=X_test.shape[0]//1000,
                        callbacks=[scheduler, hist], epochs=1, max_queue_size=1)
    history = hist.history
    with open(f"pelee_aug_{use_augmentation}_stem_{use_stem_block}.pkl", "wb") as fp:
        pickle.dump(history, fp)
Esempio n. 6
0
def train(batch_size, use_tpu, load_existing_weights):
    model = create_resnet()
    gen = CatGenerator()

    if load_existing_weights:
        model.load_weights("weights.hdf5")

    model.compile(tf.train.MomentumOptimizer(1e-3, 0.9), loss=loss_function_multiple_distance_and_area, metrics=[loss_function_simple])

    if use_tpu:
        tpu_grpc_url = "grpc://"+os.environ["COLAB_TPU_ADDR"]
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu_grpc_url)
        strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
        model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    cb = CatsCallback(model)
    history = History()

    model.fit_generator(gen.flow_from_directory(batch_size, True), steps_per_epoch=6996//batch_size,
                        validation_data=gen.flow_from_directory(batch_size, False), validation_steps=2999//batch_size,
                        callbacks=[cb, history], epochs=200)

    with open("history.dat", "wb") as fp:
        pickle.dump(history.history, fp)

    with zipfile.ZipFile("cats_result.zip", "w") as zip:
        zip.write("history.dat")
        zip.write("cats_weights.hdf5")
Esempio n. 7
0
def train(lambd, sigma, n_centers, trial):
    K.clear_session()
    (X_train, y_train), (X_test, y_test) = inbalanced_cifar(200)

    model = create_models(sigma, n_centers)
    model.compile("adam", affinity_loss(lambd), [acc])
    tf.logging.set_verbosity(tf.logging.FATAL)  # ログを埋めないようにする

    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    scheduler = LearningRateScheduler(step_decay)
    f1 = F1Callback(model, X_test, y_test, trial)

    history = model.fit(X_train,
                        y_train,
                        callbacks=[scheduler, f1],
                        batch_size=640,
                        epochs=100,
                        verbose=0).history

    max_f1 = max(f1.f1_log)
    print(
        f"lambda:{lambd:.04}, sigma:{sigma:.04} n_centers:{n_centers} / f1 = {max_f1:.04}"
    )
    return max_f1
Esempio n. 8
0
def main(argv):
    logging.info('Building Keras ResNet-50 model.')
    model = tf.keras.applications.resnet50.ResNet50(include_top=True,
                                                    weights=None,
                                                    input_tensor=None,
                                                    input_shape=None,
                                                    pooling=None,
                                                    classes=NUM_CLASSES)
    if FLAGS.tpu is not None:
        logging.info('Converting from CPU to TPU model.')
        strategy = keras_support.TPUDistributionStrategy(num_cores_per_host=8)
        model = keras_support.tpu_model(model,
                                        strategy=strategy,
                                        tpu_name_or_address=FLAGS.tpu)

    logging.info('Compiling model.')
    model.compile(
        optimizer=tf.train.GradientDescentOptimizer(learning_rate=1.0),
        loss='sparse_categorical_crossentropy',
        metrics=['sparse_categorical_accuracy'])

    training_images = np.random.randn(128, IMAGE_SIZE, IMAGE_SIZE,
                                      3).astype(np.float32)
    training_labels = np.random.randint(NUM_CLASSES, size=128, dtype=np.int32)

    logging.info('Training model.')
    model.fit(training_images,
              training_labels,
              epochs=NUM_EPOCHS,
              batch_size=128)
Esempio n. 9
0
def train():
    (X_train, _), (_, _) = mnist.load_data()

    X_train = X_train / 255.0

    model = create_model()
    model.compile(tf.train.AdamOptimizer(), loss_function)
    model.summary()

    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    cb = Sampling(model)
    hist = History()
    dummy_rand = np.zeros((X_train.shape[0], 64))
    y_train = np.concatenate(
        (X_train.reshape(-1, 784), np.zeros((X_train.shape[0], 1))), axis=-1)

    model.fit([X_train, dummy_rand],
              y_train,
              batch_size=1024,
              callbacks=[cb, hist],
              epochs=20)

    history = hist.history
    with open("vae_history.json", "w") as fp:
        json.dump(history, fp)
Esempio n. 10
0
def get_transformer_model(config,
                          model_path=None,
                          weights_only=True,
                          custom_objects=None):
    _custom_objects = get_custom_objects()
    if custom_objects is not None:
        _custom_objects.update(custom_objects)
    with tf.keras.utils.custom_object_scope(_custom_objects):

        if model_path is None:
            model = _get_transformer_model(**config.asdict())
        else:
            if weights_only:
                model = _get_transformer_model(**config.asdict())
                model.load_weights(model_path)
            else:
                model = tf.keras.models.load_model(model_path)

        if config.use_tpu:
            tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
                config.tpu_grpc_url)
            strategy = keras_support.TPUDistributionStrategy(
                tpu_cluster_resolver)
            model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    return model
Esempio n. 11
0
def main(argv):
  logging.info('Building Keras ResNet-50 model.')
  model = tf.keras.applications.resnet50.ResNet50(
      include_top=True,
      weights=None,
      input_tensor=None,
      input_shape=None,
      pooling=None,
      classes=NUM_CLASSES)

  per_core_batch_size = 128
  num_cores = 8
  batch_size = per_core_batch_size * num_cores

  if FLAGS.tpu is not None:
    logging.info('Converting from CPU to TPU model.')
    strategy = keras_support.TPUDistributionStrategy(
        num_cores_per_host=num_cores)
    model = keras_support.tpu_model(model, strategy=strategy,
                                    tpu_name_or_address=FLAGS.tpu)

  logging.info('Compiling model.')
  model.compile(
      optimizer=tf.train.GradientDescentOptimizer(learning_rate=1.0),
      loss='sparse_categorical_crossentropy',
      metrics=['sparse_categorical_accuracy'])

  if FLAGS.data is None:
    training_images = np.random.randn(
        batch_size, IMAGE_SIZE, IMAGE_SIZE, 3).astype(np.float32)
    training_labels = np.random.randint(NUM_CLASSES, size=batch_size,
                                        dtype=np.int32)
    logging.info('Training model using synthetica data.')
    num_epochs = 100  # TPUs are very fast when running a single step per epoch!
    model.fit(training_images, training_labels, epochs=num_epochs,
              batch_size=batch_size)
    logging.info('Evaluating the model on synthetic data.')
    model.evaluate(training_images, training_labels, verbose=0)
  else:

    imagenet_train, imagenet_eval = [imagenet_input.ImageNetInput(
        is_training=is_training,
        data_dir=FLAGS.data,
        per_core_batch_size=per_core_batch_size)
                                     for is_training in [True, False]]
    logging.info('Training model using real data in directory "%s".',
                 FLAGS.data)
    num_epochs = 90  # Standard imagenet training regime.
    model.fit(imagenet_train.input_fn,
              epochs=num_epochs,
              steps_per_epoch=int(APPROX_IMAGENET_TRAINING_IMAGES / batch_size))
    logging.info('Evaluating the model on the validation dataset.')
    model.evaluate(imagenet_eval.input_fn)
Esempio n. 12
0
def train(alpha):
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    train_gen = ImageDataGenerator(rescale=1.0 / 255,
                                   horizontal_flip=True,
                                   width_shift_range=4.0 / 32.0,
                                   height_shift_range=4.0 / 32.0)
    test_gen = ImageDataGenerator(rescale=1.0 / 255)
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)

    tf.logging.set_verbosity(tf.logging.FATAL)

    if alpha <= 0:
        model = create_normal_wide_resnet()
    else:
        model = create_octconv_wide_resnet(alpha)
    model.compile(SGD(0.1, momentum=0.9), "categorical_crossentropy", ["acc"])
    model.summary()

    # convert to tpu model
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    batch_size = 128
    scheduler = LearningRateScheduler(lr_scheduler)
    hist = History()

    start_time = time.time()
    model.fit_generator(train_gen.flow(X_train,
                                       y_train,
                                       batch_size,
                                       shuffle=True),
                        steps_per_epoch=X_train.shape[0] // batch_size,
                        validation_data=test_gen.flow(X_test,
                                                      y_test,
                                                      batch_size,
                                                      shuffle=False),
                        validation_steps=X_test.shape[0] // batch_size,
                        callbacks=[scheduler, hist],
                        max_queue_size=5,
                        epochs=200)
    elapsed = time.time() - start_time
    print(elapsed)

    history = hist.history
    history["elapsed"] = elapsed

    with open(f"octconv_alpha_{alpha}.pkl", "wb") as fp:
        pickle.dump(history, fp)
Esempio n. 13
0
def train(content_path, style_path):
    # Store the original image size
    input_size = 256
    with Image.open(content_path) as content:
        original_width, original_height = content.size
        content_numpy = np.asarray(
            content.convert("RGB").resize((input_size, input_size),
                                          Image.LANCZOS), np.uint8)
    # Loading the style image
    with Image.open(style_path) as style:
        style_numpy = np.asarray(
            style.convert("RGB").resize((input_size, input_size),
                                        Image.LANCZOS), np.uint8)
    # To Batch
    batch_size = 8
    content_batch = np.expand_dims(content_numpy, axis=0) * np.ones(
        (batch_size, 1, 1, 3), np.uint8)
    style_batch = np.expand_dims(style_numpy, axis=0) * np.ones(
        (batch_size, 1, 1, 3), np.uint8)
    dummy_y = np.zeros((batch_size, input_size, input_size, 3), np.float32)
    # preprocessing
    content_batch = preprocess_input(content_batch)
    style_batch = preprocess_input(style_batch)

    # create model
    model = create_neural_style_transfer_train(input_size, input_size)
    # initialize generating layer with content image
    # [0,1] -> logit
    content_r = content_numpy.astype(np.float32) / 255.0
    content_logit = np.log(content_r / (1 - content_r + 1e-8))
    model.get_layer("generated").set_weights([content_logit])
    # compile
    model.compile(tf.train.AdamOptimizer(5e-2), identity_loss)

    # convert to tpu model
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    # train
    cb = SamplingCallback(model, content_batch, style_batch, original_width,
                          original_height)

    model.fit([content_batch, style_batch],
              dummy_y,
              callbacks=[cb],
              epochs=3000,
              batch_size=batch_size)
    model.save_weights("style_trainsfer_train.hdf5")
Esempio n. 14
0
def train(use_ricap):
    batch_size = 1024
    if use_ricap:
        train_gen_instance = RICAPGenerator(rescale=1.0 / 255,
                                            width_shift_range=15.0 / 160,
                                            height_shift_range=15.0 / 160,
                                            horizontal_flip=True,
                                            ricap_beta=0.3)
    else:
        train_gen_instance = ImageDataGenerator(rescale=1.0 / 255,
                                                width_shift_range=15.0 / 160,
                                                height_shift_range=15.0 / 160,
                                                horizontal_flip=True)
    train_gen = train_gen_instance.flow_from_directory(
        "animeface-character-dataset/train",
        target_size=(160, 160),
        batch_size=batch_size)
    test_gen = ImageDataGenerator(rescale=1.0 / 255).flow_from_directory(
        "animeface-character-dataset/test",
        target_size=(160, 160),
        batch_size=batch_size)

    model = create_network()
    model.compile(tf.train.RMSPropOptimizer(1e-4), "categorical_crossentropy",
                  ["acc"])

    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    hist = History()
    model.fit_generator(train_gen,
                        steps_per_epoch=10062 // batch_size,
                        callbacks=[hist],
                        validation_data=test_gen,
                        validation_steps=4428 // batch_size,
                        epochs=1)

    history = hist.history
    with open(f"anime_ricap_{use_ricap}.dat", "wb") as fp:
        pickle.dump(history, fp)
Esempio n. 15
0
def train():
    # データの読み込み
    data = np.load("wiki_crop/wiki_all.npz")
    image, gender, age = data["image"], data["gender"], data["age"]
    # TrainとTestのSplit(インデックスを指定するだけ)
    np.random.seed(45)
    indices = np.random.permutation(image.shape[0])
    n_test = 8192  # 8192枚をテストとする
    test_indices = indices[:n_test]
    train_indices = indices[n_test:]

    # モデルの作成
    model = create_model()
    # 損失関数を自作してコンパイル
    model.compile(tf.train.RMSPropOptimizer(3e-3), multitask_loss)
    # TPUモデルに変換
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    # 訓練
    batch_size = 512
    history = History()
    checkpoint = Checkpoint(model)
    model.fit_generator(data_generator(image, gender, age, train_indices,
                                       batch_size, True),
                        steps_per_epoch=len(train_indices) // batch_size,
                        validation_data=data_generator(image, gender, age,
                                                       test_indices,
                                                       batch_size, False),
                        validation_steps=len(test_indices) // batch_size,
                        max_queue_size=1,
                        callbacks=[history, checkpoint],
                        epochs=50)

    # 結果保存
    hist = history.history
    with open("history.dat", "wb") as fp:
        pickle.dump(hist, fp)
Esempio n. 16
0
def train(network, epoch_offset):
    if network == "resnet50":
        model = resnet50.ResNet50(include_top=True, weights=None)
        size = 224
        preprocess = resnet50.preprocess_input
    if network == "test":
        model = sandbox_model.SandboxModelA()
        size = 224
        preprocess = resnet50.preprocess_input

    if USE_TPU:
        train_batch_size, val_batch_size = 1280, 1000
    nb_epoch = 2

    initial_lr = 0.1 * train_batch_size / 256
    model.compile(keras.optimizers.SGD(initial_lr, 0.9), "categorical_crossentropy", 
                  [top1])

    if USE_TPU:
        # convert to tpu model
        tpu_grpc_url = "grpc://"+os.environ["COLAB_TPU_ADDR"]
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu_grpc_url)
        strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
        model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    cache = load_caches(IMAGE_NET_ROOT)
    n_train, n_val = len(cache["train"]), len(cache["val"])

    # train_gen = imagenet_generator(cache["train"], size, True, train_batch_size, preprocess)
    # val_gen = imagenet_generator(cache["val"], size, False, val_batch_size, preprocess)
    train_gen = fake_data_generator(size, train_batch_size, preprocess)
    val_gen = fake_data_generator(size, train_batch_size, preprocess)
    # fake_data_generator

    scheduler_obj = CustomScuduling(epoch_offset, initial_lr)
    scheduler = keras.callbacks.LearningRateScheduler(scheduler_obj.scheduler)
    cp = Checkpoint(model, network, nb_epoch)

    model.fit_generator(train_gen, steps_per_epoch=n_train//train_batch_size, epochs=nb_epoch,
                        validation_data=val_gen, validation_steps=n_val//val_batch_size,
                        max_queue_size=3, callbacks=[scheduler])
    def train(self, X_train, y_train, X_val, y_val):
        # コンパイル
        self.model.compile(optimizer=SGD(lr=self.initial_lr, momentum=0.9),
                           loss="categorical_crossentropy",
                           metrics=["acc"])
        # Data Augmentation
        traingen = ImageDataGenerator(rescale=1. / 255,
                                      width_shift_range=4. / 32,
                                      height_shift_range=4. / 32,
                                      horizontal_flip=True)
        valgen = ImageDataGenerator(rescale=1. / 255)
        # TPU
        tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
            tpu_grpc_url)
        strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
        self.model = tf.contrib.tpu.keras_to_tpu_model(self.model,
                                                       strategy=strategy)

        # Callback
        time_cb = TimeHistory()
        lr_cb = LearningRateScheduler(self.lr_schduler)
        # Train
        history = self.model.fit_generator(traingen.flow(X_train,
                                                         y_train,
                                                         batch_size=1024),
                                           epochs=self.nb_epochs,
                                           steps_per_epoch=len(X_train) / 1024,
                                           validation_data=valgen.flow(
                                               X_val, y_val, batch_size=1024),
                                           validation_steps=len(X_val) / 1024,
                                           callbacks=[time_cb, lr_cb]).history
        history["time"] = time_cb.times
        # Save history
        file_name = f"{self.framework}_n{self.n}.dat"
        with open(file_name, "wb") as fp:
            pickle.dump(history, fp)
def train_and_generate_model():
    #global scaler

    K.clear_session()

    data_len = len(exchange_rates)
    train_len = int(len(exchange_rates) / TRAINDATA_DIV)

    print("data size: " + str(data_len))
    print("train len: " + str(train_len))

    tr_input_mat = []
    tr_angle_mat = []
    for i in range(1000, train_len, OUTPUT_LEN):
        tr_input_mat.append([
            exchange_rates[i], (exchange_rates[i] - exchange_rates[i - 1]) /
            exchange_rates[i - 1],
            get_rsi(exchange_rates, i),
            get_ma(exchange_rates, i),
            get_ma_kairi(exchange_rates, i),
            get_bb_1(exchange_rates, i),
            get_bb_2(exchange_rates, i),
            get_ema(exchange_rates, i),
            get_ema_rsi(exchange_rates, i),
            get_cci(exchange_rates, i),
            get_mo(exchange_rates, i),
            get_lw(exchange_rates, i),
            get_ss(exchange_rates, i),
            get_dmi(exchange_rates, i),
            get_vorarity(exchange_rates, i),
            get_macd(exchange_rates, i),
            judge_chart_type(exchange_rates[i - CHART_TYPE_JDG_LEN:i])
        ])
        tr_input_mat.append([
            reverse_exchange_rates[i],
            (reverse_exchange_rates[i] - reverse_exchange_rates[i - 1]) /
            reverse_exchange_rates[i - 1],
            get_rsi(reverse_exchange_rates, i),
            get_ma(reverse_exchange_rates, i),
            get_ma_kairi(reverse_exchange_rates, i),
            get_bb_1(reverse_exchange_rates, i),
            get_bb_2(reverse_exchange_rates, i),
            get_ema(reverse_exchange_rates, i),
            get_ema_rsi(reverse_exchange_rates, i),
            get_cci(reverse_exchange_rates, i),
            get_mo(reverse_exchange_rates, i),
            get_lw(reverse_exchange_rates, i),
            get_ss(reverse_exchange_rates, i),
            get_dmi(reverse_exchange_rates, i),
            get_vorarity(reverse_exchange_rates, i),
            get_macd(reverse_exchange_rates, i),
            judge_chart_type(reverse_exchange_rates[i - CHART_TYPE_JDG_LEN:i])
        ])

        tmp = (exchange_rates[i + OUTPUT_LEN] -
               exchange_rates[i]) / float(OUTPUT_LEN)
        if tmp >= 0:
            tr_angle_mat.append(1)
        else:
            tr_angle_mat.append(0)
        tmp = (reverse_exchange_rates[i + OUTPUT_LEN] -
               reverse_exchange_rates[i]) / float(OUTPUT_LEN)
        if tmp >= 0:
            tr_angle_mat.append(1)
        else:
            tr_angle_mat.append(0)

    X = np.array(tr_input_mat, dtype=np.float32)
    Y = np.array(tr_angle_mat, dtype=np.float32)

    X, scaler = preprocess_data(X)
    Y, encoder = preprocess_labels(Y)

    joblib.dump(scaler, "./sklearn.scaler.dump")

    np.random.seed(1337)  # for reproducibility

    nb_classes = Y.shape[1]
    print(nb_classes, 'classes')

    dims = X.shape[1]
    print(dims, 'dims')

    neuro_num = 50

    # setup deep NN
    model = Sequential()
    model.add(Dense(neuro_num, input_shape=(dims, ), activation="relu"))
    #model.add(Dense(neuro_num, activation="relu"))
    #model.add(BatchNormalization((neuro_num,)))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    model.add(Dense(int(neuro_num / 2), activation="relu"))
    #model.add(BatchNormalization((neuro_num/2,)))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    model.add(Dense(nb_classes, activation="sigmoid"))

    model.summary()

    model.compile(loss='binary_crossentropy', optimizer="adam")

    # # TPU
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
        tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    tpu_model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

    print("Training model...")
    start = time.time()
    tpu_model.fit(X,
                  Y,
                  batch_size=1024,
                  epochs=1000,
                  verbose=2,
                  validation_split=0.15)
    process_time = time.time() - start
    print("excecution time of training: " + str(process_time))

    dump_fd = open("./keras.model.json", "w")
    model_json_str = model.to_json()
    dump_fd.write(model_json_str)
    model.save_weights("./keras.weight")
    dump_fd.close()
Esempio n. 19
0
def main(unused_dev):
    use_tpu = flags.FLAGS.use_tpu

    print('Mode:', 'TPU' if use_tpu else 'CPU')

    if flags.FLAGS.fake_data:
        print('Using fake data')
        x_train = np.random.random((128, img_rows, img_cols))
        y_train = np.zeros([128, 1], dtype=np.int32)
        x_test, y_test = x_train, y_train
    else:
        # the data, split between train and test sets
        print('Using real data')
        (x_train, y_train), (x_test,
                             y_test) = tf.keras.datasets.mnist.load_data()

    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    y_train = tf.keras.utils.to_categorical(y_train, num_classes)
    y_test = tf.keras.utils.to_categorical(y_test, num_classes)

    model = tf.keras.models.Sequential()
    model.add(
        tf.keras.layers.Conv2D(32,
                               kernel_size=(3, 3),
                               activation='relu',
                               input_shape=input_shape))
    model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
    model.add(tf.keras.layers.Dropout(0.25))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(128, activation='relu'))
    model.add(tf.keras.layers.Dropout(0.5))
    model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

    if use_tpu:
        strategy = keras_support.TPUDistributionStrategy(num_cores_per_host=8)
        model = keras_support.tpu_model(model,
                                        strategy=strategy,
                                        tpu_name_or_address=flags.FLAGS.tpu)

    model.compile(
        loss=tf.keras.losses.categorical_crossentropy,
        optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.05),
        metrics=['accuracy'])

    model.fit(x_train,
              y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_test, y_test))
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Esempio n. 20
0
"""

epochs = 100
batch_size = 100

steps_per_epoch = 5050 // batch_size * 15   # 元は  5050 // batch_size * 15 (15=trainのh5ファイル数)
validation_steps = 5050 // batch_size * 5   # 元は  5050 // batch_size * 5  (5=testのh5ファイル数)

"""
以降は、原則いじらない.
ただし、early stopping, 学習率減衰などを使う場合は、訓練ループ内をいじる。
"""
if tpu:
    tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
    tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu_grpc_url)
    strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
    model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

history = {"train_loss": [], "train_acc": [], "val_loss": [], "val_acc": []}
train_generator = flow_from_h5(train_path, batch_size, data_aug=True)
validation_generator = flow_from_h5(test_path, batch_size, data_aug=False)

# 訓練ループ fitの代わり
for e in range(epochs):
    train_loss, train_acc, val_loss, val_acc = 0, 0, 0, 0
    print("=" * 30)
    print(f"epoch: {e + 1}/{epochs}")

    # train
    for step in range(steps_per_epoch):
        x_batch, y_batch = next(train_generator)
Esempio n. 21
0
def target_train_tpu_main(gen_targets_dir,
                          model_file_path,
                          early_stopping_patience=None,
                          length=None,
                          batch_size=1,
                          period=1,
                          retrain_file=None,
                          retrain_do_compile=False,
                          base_model_file_path='target_common.h5',
                          optimizer=Adam(),
                          optimizer_lr=0.001,
                          epochs=100000):
    gc.collect()

    with CustomObjectScope({'RandomLayer': RandomLayer}):
        if retrain_file is None:
            gen = VoiceGeneratorTargetTpu(gen_targets_dir,
                                          0.1,
                                          batch_size,
                                          length,
                                          train=True)
            shape0 = gen[0][0].shape[1]
            val_gen = VoiceGeneratorTargetTpu(gen_targets_dir,
                                              0.1,
                                              train=False,
                                              max_size=shape0)

            model = load_model(base_model_file_path)
            config = model.get_config()
            config['layers'][0]['config']['batch_input_shape'] = (None, shape0,
                                                                  139)
            config['layers'][3]['config']['rate'] = 0.1
            config['layers'][6]['config']['target_shape'] = (shape0 * 2, 64)
            config['layers'][8]['config']['rate'] = 0.1
            config['layers'][11]['config']['target_shape'] = (shape0 * 4, 32)
            config['layers'][13]['config']['rate'] = 0.1
            config['layers'][16]['config']['target_shape'] = (shape0 * 8, 16)
            config['layers'][18]['config']['rate'] = 0.1
            model = Sequential.from_config(config)
            model.load_weights(base_model_file_path, by_name=True)
            model.summary()
            model.compile(loss='mse', optimizer=optimizer)

            baseline = None
        else:
            model = load_model(retrain_file)
            if retrain_do_compile:
                model.compile(loss='mse', optimizer=optimizer)

            config = model.get_config()
            shape0 = config['layers'][0]['config']['batch_input_shape'][1]
            gen = VoiceGeneratorTargetTpu(gen_targets_dir,
                                          0.1,
                                          batch_size,
                                          length,
                                          train=True,
                                          max_size=shape0)
            val_gen = VoiceGeneratorTargetTpu(gen_targets_dir,
                                              0.1,
                                              train=False,
                                              max_size=shape0)

            baseline = model.test_on_batch(val_gen[0][0], val_gen[0][1])

        tpu_grpc_url = 'grpc://' + os.environ['COLAB_TPU_ADDR']
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
            tpu_grpc_url)
        strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
        model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

        cp = ModelCheckpoint(filepath=model_file_path,
                             monitor='val_loss',
                             save_best_only=True,
                             period=period)
        if baseline is not None:
            cp.best = baseline

        def lr_scheduler(epoch):
            return optimizer_lr

        scheduler = LearningRateScheduler(lr_scheduler)

        if early_stopping_patience is not None:
            es = EarlyStopping(monitor='val_loss',
                               patience=early_stopping_patience,
                               verbose=0,
                               mode='auto',
                               baseline=baseline)
            callbacks = [es, cp, scheduler]
        else:
            callbacks = [cp, scheduler]

        model.fit_generator(gen,
                            shuffle=True,
                            epochs=epochs,
                            verbose=1,
                            callbacks=callbacks,
                            validation_data=val_gen)

        K.clear_session()