• 大小: 6.45MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-18
  • 语言: Python
  • 标签:

资源简介

汉字的神经风格转移(Neural Style Transfer)实现

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
import numpy as np


def read_font_data(font unit_scale):
    F = np.load(font)
    if unit_scale:
        return F / 255.
    return F


class FontDataProvider(object):
    def __init__(self font start end):
        self.start = start
        self.end = end
        self.data = np.copy(font[start: end])
        self.cursor = 0
        self.length = self.end - self.start

    def get_data(self):
        return self.data

    def next_batch(self batch_size):
        if self.cursor >= self.length:
            self.cursor = 0
        batch_start = self.cursor
        batch_end = self.cursor + batch_size
        self.cursor += batch_size
        return self.data[batch_start: batch_end]


class FontDataManager(object):
    def __init__(self src target total split_point unit_scale=True shuffle=False):
        src_data = read_font_data(src unit_scale)
        target_data = read_font_data(target unit_scale)
        if shuffle:
            perm = np.arange(src_data.shape[0])
            np.random.shuffle(perm)
            src_data = src_data[perm]
            target_data = target_data[perm]
        self.train_source = FontDataProvider(src_data 0 split_point)
        self.validation_source = FontDataProvider(src_data split_point total)
        self.train_target = FontDataProvider(target_data 0 split_point)
        self.validation_target = FontDataProvider(target_data split_point total)

    def next_train_batch(self batch_size):
        if self.train_source.cursor >= self.train_source.length:
            # random shuffle the training examples
            # otherwise the model‘s performance fluctuate periodically
            perm = np.arange(self.train_source.length)
            np.random.shuffle(perm)
            self.train_source.data = self.train_source.data[perm]
            self.train_target.data = self.train_target.data[perm]
        return self.train_source.next_batch(batch_size) self.train_target.next_batch(batch_size)

    def get_validation(self):
        return self.validation_source.get_data() \
               self.validation_target.get_data()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-04-07 08:29  Rewrite-master\
     文件          28  2017-04-07 08:29  Rewrite-master\.gitignore
     文件       35140  2017-04-07 08:29  Rewrite-master\LICENSE
     文件       14305  2017-04-07 08:29  Rewrite-master\README.md
     目录           0  2017-04-07 08:29  Rewrite-master\charsets\
     文件      106446  2017-04-07 08:29  Rewrite-master\charsets\gbk.txt
     文件       11976  2017-04-07 08:29  Rewrite-master\charsets\top_3000_simplified.txt
     文件       11976  2017-04-07 08:29  Rewrite-master\charsets\top_3000_traditional.txt
     文件        2128  2017-04-07 08:29  Rewrite-master\dataset.py
     目录           0  2017-04-07 08:29  Rewrite-master\images\
     文件       26916  2017-04-07 08:29  Rewrite-master\images\architecture.png
     文件     1486293  2017-04-07 08:29  Rewrite-master\images\bigger_test.png
     文件       20260  2017-04-07 08:29  Rewrite-master\images\box_for_ideas.png
     文件       47517  2017-04-07 08:29  Rewrite-master\images\different_training_size.png
     文件     2887427  2017-04-07 08:29  Rewrite-master\images\mixed_font.gif
     文件       93045  2017-04-07 08:29  Rewrite-master\images\predicted_vs_ground_truth.png
     文件     2406995  2017-04-07 08:29  Rewrite-master\images\single_font_progress.gif
     文件       28320  2017-04-07 08:29  Rewrite-master\images\structure.png
     文件        3605  2017-04-07 08:29  Rewrite-master\preprocess.py
     文件          54  2017-04-07 08:29  Rewrite-master\requirements.txt
     文件       15763  2017-04-07 08:29  Rewrite-master\rewrite.py
     文件         901  2017-04-07 08:29  Rewrite-master\utils.py

评论

共有 条评论