Пример #1
0
    filepath = "model/resnet_{epoch:03d}-{val_loss:.4f}.h5"
    checkpoint = ModelCheckpoint(filepath,
                                 save_best_only=False,
                                 verbose=1,
                                 mode='auto',
                                 save_weights_only=True)
    reducelr = ReduceLROnPlateau(monitor='val_loss',
                                 factor=0.5,
                                 patience=2,
                                 verbose=1,
                                 mode='min',
                                 epsilon=0.0001,
                                 cooldown=0,
                                 min_lr=0.00000001)
    es = EarlyStopping(monitor='val_loss', patience=3, verbose=1, mode='auto')
    callbacks_list = [checkpoint, reducelr, es]
    return model, callbacks_list


if __name__ == "__main__":
    x_train, y_train, x_val, y_val, x_test = get_data(max_num_word=7400,
                                                      max_len_word=1200,
                                                      level='char')
    model, callbacks_list = build_resnet(max_num_word=7400, max_len_word=1200)
    model.fit(x_train,
              y_train,
              epochs=15,
              batch_size=32,
              validation_data=(x_val, y_val),
              callbacks=callbacks_list)
Пример #2
0
def main():
    name = sys.argv[1]
    height = sys.argv[2]
    width = sys.argv[3]
    mode = sys.argv[4]

    content_path = tf.keras.utils.get_file(
        'ken.jpg',
        'https://scontent.fzty2-1.fna.fbcdn.net/v/t1.0-9/69497958_2478425912438909_3450352180421197824_n.jpg?_nc_cat=101&_nc_ohc=V4z59bUbz1oAQlwF5rMLHtqypJHMG_t17mpiHmFlL-AKOhXrEHJnVc9qw&_nc_ht=scontent.fzty2-1.fna&oh=29ebb194584872745009fbe4e856a904&oe=5E834777'
    )
    style_path = tf.keras.utils.get_file(
        's.jpg',
        'https://iheartintelligence.com/wp-content/uploads/2015/09/THE-STARRY-NIGHT.jpg'
    )
    content_path_1 = tf.keras.utils.get_file(
        's.jpg',
        'https://iheartintelligence.com/wp-content/uploads/2015/09/THE-STARRY-NIGHT.jpg'
    )
    content_image, style_image, content_image_1 = get_images(
        content_path, style_path, content_path_1, height, width)

    dataset = get_data('./data_1', 4)

    content_layers = ['block2_conv2']
    style_layers = [
        'block1_conv1', 'block2_conv2', 'block3_conv3', 'block4_conv3'
    ]
    content_layers_1 = ['block5_conv2']
    num_content_layers = len(content_layers)
    num_style_layers = len(style_layers)
    extractor = MergeModel(style_layers, content_layers, content_layers_1,
                           name)

    style_targets = extractor.style_content(style_image)[1]

    checkpoint_dir = './checkpoints'
    checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
    checkpoint = tf.train.Checkpoint(extractor=extractor)
    manager = tf.train.CheckpointManager(checkpoint,
                                         checkpoint_dir,
                                         max_to_keep=3)
    if not os.path.exists('./out_dir'):
        os.makedirs('./out_dir')
    image = tf.constant(content_image)

    if (mode == "test"):
        checkpoint.restore(manager.latest_checkpoint)
        im = ((extractor.get_image(image).numpy()) * 255)[0]
        tensor = np.array(im, dtype=np.uint8)
        s = './out_dir' + '/' + 'test' + '.png'
        imwrite(s, im)
        return

    if (mode == "train"):
        epochs = 3
        steps_per_epoch = 8000
        for i in range(epochs):
            for j in range(steps_per_epoch):
                print(i, "and", j)
                content_targets_1 = extractor.style_content(
                    tf.constant(dataset[j][0], dtype='float32'))[0]
                content_targets = extractor.style_content(
                    tf.constant(dataset[j][0], dtype='float32'))[0]
                train(image, extractor, style_targets, content_targets,
                      num_style_layers, num_content_layers, content_targets_1)
                if (j % 400 == 0):
                    im = ((extractor.get_image(image).numpy()) * 255)[0]
                    tensor = np.array(im, dtype=np.uint8)
                    s = './out_dir' + '/' + str(j) + '.png'
                    imwrite(s, im)
                    manager.save()
                    print("==saving progress===")
            im = ((extractor.get_image(image).numpy()) * 255)[0]
            tensor = np.array(im, dtype=np.uint8)
            s = './out_dir' + '/' + str(i) + '.png'
            imwrite(s, im)
            manager.save()
            print("==saving progress===")