Ejemplo n.º 1
0
def make_shape(args):
    tf.set_random_seed(args.seed)
    tools.gen_coord_datasets()

    # get dataset
    xdata = tools.load_coord_dataset(args.size)
    x = tf.placeholder(tf.float32, [None, 4])

    for i in range(args.amount):
        # cppn network graph
        wrange = 2
        w1 = tf.Variable(tf.random_uniform([4, 30], -wrange, wrange))
        w2 = tf.Variable(tf.random_uniform([30, 30], -wrange, wrange))
        w3 = tf.Variable(tf.random_uniform([30, 30], -wrange, wrange))
        w4 = tf.Variable(tf.random_uniform([30, 30], -wrange, wrange))
        w5 = tf.Variable(tf.random_uniform([30, 1], -wrange, wrange))

        layer1 = gaussian(tf.matmul(x, w1))
        layer2 = tf.sin(tf.matmul(layer1, w2))
        layer3 = tf.nn.sigmoid(tf.matmul(layer2, w3))
        layer4 = tf.tanh(tf.matmul(layer3, w4))
        layer5 = tf.sigmoid(tf.matmul(layer4, w5))

        # run cppn network for number of shapes
        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init)
            outputs = sess.run(layer5, feed_dict={x: xdata})

            voxels = np.rint(outputs).reshape(args.size, args.size, args.size)
            voxels[voxels < 0] = 0
            # save to shapes dir
            path = tools.get_path('shapes', 'shape{}'.format(i))
            np.save(path, voxels)
            tools.render_voxels(voxels)
Ejemplo n.º 2
0
def load_header(img_name, relative_path=''):
    '''
    load fits header
    inputs: str
    return: dictionary
    '''
    img_path = get_path('../docs/' + relative_path + img_name)
    img_header = fits.open(img_path)[1].header  #~~TODO:
    return img_header
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--size',
                        type=int,
                        default=32,
                        help='Voxel dimensions cubed')
    parser.add_argument('--amount',
                        type=int,
                        default=3,
                        help='The number of voxel shapes to generate')
    parser.add_argument('--seed',
                        type=int,
                        default=None,
                        help='tensorflow weight init seed')
    args = parser.parse_args()

    for file in os.listdir(tools.get_path('shapes')):
        os.remove(tools.get_path('shapes', file))

    make_shape(args)
Ejemplo n.º 4
0
def plot_model(model,
               name,
               folder=FOLDER,
               extension="png",
               summarize=SUMMARIZE):
    """summarize and plot a keras model"""
    if summarize:
        model.summary()
    path = get_path(folder, name, extension)
    log(f"SCREENSHOT, {path}")
    K.utils.plot_model(model, path, show_shapes=SHOW_SHAPES)
Ejemplo n.º 5
0
def aes_decode(data):
    '''
    aes解密
    :param ciphertext: 密文
    :param key: 用来解密的key,和加密的key是同一个,所以是对称加密
    :return:
    '''
    file_path = get_path(u'key.bcp')
    with open(file_path) as file:
        key = file.read()
    data = smart_decode(data)
    key = smart_decode(key).encode('utf8')
    # 解密的话要用key和iv生成新的AES对象
    mydecrypt = AES.new(key, AES.MODE_CFB, data[:16])
    # 使用新生成的AES对象,将加密的密文解密
    decrypttext = mydecrypt.decrypt(data[16:])
    return decrypttext.decode('utf8')
Ejemplo n.º 6
0
def load_img(img_name, relative_path='', ext=0):
    '''
    load fits data
    inputs:name of a fits file in '../docs/', str
    return: array
    '''
    img_path = get_path('../docs/' + relative_path + img_name)
    if isinstance(ext, int) == True:
        img_data = fits.open(img_path)[ext].data
        return img_data
    else:
        dic_data = {}
        fits_file = fits.open(img_path)
        # ext = {0:'img',1:'err2',2:'exp'}
        for i in ext:
            dic_data[ext[i]] = fits_file[i].data
        return dic_data
Ejemplo n.º 7
0
def load_reg_list(reg_name, relative_path=''):
    reg_path = get_path('../docs/' + relative_path + reg_name)
    reg_file = open(reg_path)
    center_i_list = []
    center_j_list = []
    radiu_list = []
    for line in islice(reg_file, 3, None):
        reg_data = line.split('(')[1]
        reg_data = reg_data.split(')')[0]
        reg_data = reg_data.split(',')
        center_j_list.append(float(reg_data[0]))
        center_i_list.append(float(reg_data[1]))
        if len(reg_data[2:]) == 1:
            radiu_list.append(float(reg_data[2:][0]))
        else:
            radiu_list.append([float(k) for k in reg_data[2:]])
    return center_i_list, center_j_list, radiu_list
Ejemplo n.º 8
0
def main(args):

    np.random.seed(args.seed)
    tf.set_random_seed(args.seed)

    shape_amount = len(os.listdir(tools.get_path('shapes')))
    vol = args.size**3

    # data
    latent_vec = np.random.uniform(size=(shape_amount, 1))
    coord_vec = tools.load_coord_dataset(args.size)
    xdata = np.append(coord_vec,
                      latent_vec[args.shape] * np.ones((vol, 1)),
                      axis=1)

    print('latent vector input\n', latent_vec)

    # load the model
    params = np.load('model.npy')

    # model graph
    x = tf.placeholder(tf.float32, [None, 5])
    W = params[0]
    B = params[1]
    w1 = tf.constant(W[0])
    w2 = tf.constant(W[1])
    w3 = tf.constant(W[2])
    b1 = tf.constant(B[0])
    b2 = tf.constant(B[1])

    hidden1 = tf.tanh(tf.matmul(x, w1) + b1)
    hidden2 = tf.tanh(tf.matmul(hidden1, w2) + b2)
    output = tf.nn.sigmoid(tf.matmul(hidden2, w3))

    with tf.Session() as sess:
        pred = sess.run(output, feed_dict={x: xdata})

    # render in browser
    voxels = np.rint(pred).reshape(args.size, args.size, args.size)
    tools.render_voxels(voxels)
Ejemplo n.º 9
0
def load_reg_list(reg_name, relative_path=''):
    '''
    load regions from a .reg file (requirement: a list of circle regions)
    inputs: .reg file name (str)
    outputs: y position list; x position list; radius list
    '''
    reg_path = get_path('../docs/' + relative_path + reg_name)
    reg_file = open(reg_path)
    center_i_list = []
    center_j_list = []
    radiu_list = []
    for line in islice(reg_file, 3, None):
        reg_data = line.split('(')[1]
        reg_data = reg_data.split(')')[0]
        reg_data = reg_data.split(',')
        center_j_list.append(float(reg_data[0]))
        center_i_list.append(float(reg_data[1]))
        if len(reg_data[2:]) == 1:
            radiu_list.append(float(reg_data[2:][0]))
        else:
            radiu_list.append([float(k) for k in reg_data[2:]])
    return center_i_list, center_j_list, radiu_list
Ejemplo n.º 10
0
Archivo: net.py Proyecto: eosay/voxCPPN
def main(args):
    '''Simple feed forward neural network for voxel shape encoding and generative 
       modeling with latent vectors. Uses Tensorflow and can easily run on a CPU.

       usage: net.py [-h] [--op OP] [--size SIZE] [--seed SEED]

        optional arguments:
        -h, --help   show this help message and exit
        --op OP      operation to complete: train | latent
        --size SIZE  Voxel dimensions cubed, can be different size for train vs
                    latent op
        --seed SEED  tensorflow weight init seed
       
       The network is very small which allows for fast training (<10 min on CPU). However it should 
       be noted that there is a balance with network size and the net's ability to 
       encode complex shapes. This is purely a simple interesting project for generative modeling
       that can be trained and run on a laptop. Inspired by Compositional Pattern Producing Networks (CPPN).
       
       For training: train at a low resolution using the SIZE CLI argument, 32 works well
       For running the net/latent space visualizations: SIZE 32-128 are good visualization 
       resoultions. Be careful with this becuase you can quickly run out of memory.

       Author: Dominic Cascino
       Date: Oct 2017'''
    # hyper paramters
    batch_size = 1000
    lr = 0.001
    iters = 2000
    samples = 100
    size = args.size
    vol = size ** 3
    seed = args.seed
    np.random.seed(seed)
    tf.set_random_seed(seed)
    shape_amount = len(os.listdir(tools.get_path('shapes')))
    save_path = 'model'
    # dataset
    coord_vec = tools.load_coord_dataset(size)
    # scalar latent vector for each shape, latent space is only 1d
    latent_vec = np.random.uniform(size=(shape_amount, 1))    
    xdata = []
    ydata = []

    for i in range(shape_amount):
        latent = np.expand_dims(latent_vec[i], axis=0).repeat(vol, axis=0)
        shape_path = tools.get_path('shapes', 'shape{}.npy'.format(i))
        xdata.append(np.append(coord_vec, latent, axis=1))
        ydata.append(np.expand_dims(np.load(shape_path).flatten(), axis=1))

    # small feed forward neural network graph
    # a small network seems to work better, the less paramters, 
    # but a larger network does allow for more complexity to be encoded
    x = tf.placeholder(tf.float32, [None, 5])
    y = tf.placeholder(tf.float32, [None, 1])

    w1 = tf.Variable(tf.random_uniform([5, 10]))
    w2 = tf.Variable(tf.random_uniform([10, 10]))   
    w3 = tf.Variable(tf.random_uniform([10, 1]))

    b1 = tf.Variable(tf.random_uniform([10]))
    b2 = tf.Variable(tf.random_uniform([10]))

    hidden1 = tf.tanh(tf.matmul(x, w1) + b1)
    hidden2 = tf.tanh(tf.matmul(hidden1, w2) + b2)    
    output = tf.nn.sigmoid(tf.matmul(hidden2, w3))

    # loss and optim
    loss = tf.reduce_sum(tf.square(y - output))
    train = tf.train.GradientDescentOptimizer(lr).minimize(loss)
    init = tf.global_variables_initializer()

    if args.op == 'train':
        # training loop
        with tf.Session() as sess:
            sess.run(init)
            for i in range(iters):
                for j in range(0, vol, batch_size):
                    # train on each shape one after the other
                    for k in range(shape_amount):
                        sess.run(train, feed_dict={x: xdata[k][j:batch_size+j], y: ydata[k][j:batch_size+j]})

                if i % samples == 0:
                    # loss sampling
                    print('epoch', i)
                    for shp in range(shape_amount):
                        e = sess.run(loss, feed_dict={x: xdata[shp], y: ydata[shp]})
                        print('loss{} {:0.3f}    '.format(shp, e), end='', flush=True)
                    print('\n')

            # save model here
            for data in xdata:
                out = sess.run(output, feed_dict={x: data})
                tools.render_voxels(np.rint(out).reshape(size, size, size))
            
            # save just the weights
            W = sess.run([w1, w2, w3])
            B = sess.run([b1, b2])
            np.save(save_path, [W, B])

    elif args.op == 'latent':
        # dataset
        # size can be greater or smaller than what the model has been trained on,
        # changing the size scales the output shape since the difference between
        # the minmax normalized datapoints increases or decreases
        # this means that the model can train on a low resolution and 
        # output an arbitrary resolution shape after training
        coord_vec = tools.load_coord_dataset(args.size)
        vol = args.size ** 3
        print('latent vector inputs\n', latent_vec)

        lmin, lmax = np.amin(latent_vec), np.amax(latent_vec)
        # steps: how many shapes to build and render in animation
        # resolution 32 -> 20ish steps
        # resolution 64 -> 20ish steps
        # resolution 128 -> 5 steps, dont go above 10
        # resolution 256 -> you will probably run out of memory!
        steps = 5
        if args.size < 128:
            steps = 20
        shifts = np.linspace(lmin, lmax, steps)
        data_list = []
        base = np.ones((vol, 1))
        # traverse the latent space between the latent vector inputs
        for i in range(steps):
            latent_shift = shifts[i] * base
            # np.append is a memory issue for large sizes 128-256,
            # since it makes copies of the original array
            data_list.append(np.append(coord_vec, latent_shift, axis=1))

        # restore model, just uses numpy saving and not tf saver
        params = np.load(save_path + '.npy')

        x = tf.placeholder(tf.float32, [None, 5])
        W = params[0]
        B = params[1]
        w1 = tf.constant(W[0])
        w2 = tf.constant(W[1])
        w3 = tf.constant(W[2])
        b1 = tf.constant(B[0])
        b2 = tf.constant(B[1])

        hidden1 = tf.tanh(tf.matmul(x, w1) + b1)
        hidden2 = tf.tanh(tf.matmul(hidden1, w2) + b2)    
        output = tf.nn.sigmoid(tf.matmul(hidden2, w3))

        results = []
        with tf.Session() as sess:
            for data in data_list:
                results.append(np.rint(sess.run(output, feed_dict={x: data}).reshape(size, size, size)))

        # render the latent space traversal as animation in browser
        tools.render_voxel_ani(results)
Ejemplo n.º 11
0
from docx import Document
from docx.document import Document as _Document
from docx.oxml.text.paragraph import CT_P
from docx.oxml.table import CT_Tbl
from docx.table import _Cell, Table
from docx.text.paragraph import Paragraph
from docx.shared import RGBColor

import tools
import difflib
import docx_to_txt
import inspect

docx_path = tools.get_path('../../material/2-电子版对照.docx')
docx_txt_path = tools.get_path('../../output/2-电子版对照.txt')
ocr_path = tools.get_path('../../material/2-打印出纸质版.ocr.txt')
result_path = tools.get_path('../../output/2-对照.docx')

ocr_content = ''
with open(ocr_path, 'r') as file:
    ocr_content = file.read()

docx_to_txt.word_to_txt(docx_path, docx_txt_path)

docx_content = ''
with open(docx_txt_path, 'r') as file:
    docx_content = file.read()

differ = difflib.Differ()
diff_content = differ.compare(docx_content, ocr_content)
diff = list(diff_content)