def block_inception_c(inputs, scope=None, is_train=False):

<EOL>with tf.variable_scope(scope, '<STR_LIT>', [inputs]):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>branch_0, _ = conv_module(<EOL>inputs, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL><DEDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>branch_1, _ = conv_module(<EOL>inputs, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_1a, _ = conv_module(<EOL>branch_1, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_1b, _ = conv_module(<EOL>branch_1, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_1 = tl.layers.ConcatLayer([branch_1a, branch_1b], concat_dim=<NUM_LIT:3>, name='<STR_LIT>')<EOL><DEDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>branch_2, _ = conv_module(<EOL>inputs, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_2, _ = conv_module(<EOL>branch_2, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_2, _ = conv_module(<EOL>branch_2, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_2a, _ = conv_module(<EOL>branch_2, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_2b, _ = conv_module(<EOL>branch_2, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_2 = tl.layers.ConcatLayer([branch_2a, branch_2b], concat_dim=<NUM_LIT:3>, name='<STR_LIT>')<EOL><DEDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>branch_3 = tl.layers.MeanPool2d(<EOL>inputs, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL>branch_3, _ = conv_module(<EOL>branch_3, n_out_channel=<NUM_LIT>, filter_size=(<NUM_LIT:1>, <NUM_LIT:1>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', batch_norm_init=None,<EOL>is_train=is_train, use_batchnorm=True, activation_fn='<STR_LIT>', name='<STR_LIT>'<EOL>)<EOL><DEDENT>return tl.layers.ConcatLayer([branch_0, branch_1, branch_2, branch_3], concat_dim=<NUM_LIT:3>, name='<STR_LIT>')<EOL><DEDENT>

Builds Inception-C block for Inception v4 network.


start_time = time.time()<EOL>print("<STR_LIT>")<EOL>rgb_scaled = rgb * <NUM_LIT><EOL>red, green, blue = tf.split(rgb_scaled, <NUM_LIT:3>, <NUM_LIT:3>)<EOL>if red.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if green.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if blue.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>bgr = tf.concat([<EOL>blue - VGG_MEAN[<NUM_LIT:0>],<EOL>green - VGG_MEAN[<NUM_LIT:1>],<EOL>red - VGG_MEAN[<NUM_LIT:2>],<EOL>], axis=<NUM_LIT:3>)<EOL>if bgr.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:3>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>net_in = InputLayer(bgr, name='<STR_LIT:input>')<EOL>net = Conv2dLayer(net_in, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT:64>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT:64>, <NUM_LIT:64>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = PoolLayer(net, ksize=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], strides=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], padding='<STR_LIT>', pool=tf.nn.max_pool, name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT:64>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = PoolLayer(net, ksize=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], strides=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], padding='<STR_LIT>', pool=tf.nn.max_pool, name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = PoolLayer(net, ksize=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], strides=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], padding='<STR_LIT>', pool=tf.nn.max_pool, name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = PoolLayer(net, ksize=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], strides=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], padding='<STR_LIT>', pool=tf.nn.max_pool, name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2dLayer(net, act=tf.nn.relu, shape=[<NUM_LIT:3>, <NUM_LIT:3>, <NUM_LIT>, <NUM_LIT>], strides=[<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>], padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = PoolLayer(net, ksize=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], strides=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:1>], padding='<STR_LIT>', pool=tf.nn.max_pool, name='<STR_LIT>')<EOL>net = FlattenLayer(net, name='<STR_LIT>')<EOL>net = DenseLayer(net, n_units=<NUM_LIT>, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = DenseLayer(net, n_units=<NUM_LIT>, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = DenseLayer(net, n_units=<NUM_LIT:1000>, act=None, name='<STR_LIT>')<EOL>print("<STR_LIT>" % (time.time() - start_time))<EOL>return net<EOL>

Build the VGG 19 Model

Parameters
-----------
rgb : rgb image placeholder [batch, height, width, 3] values scaled [0, 1]


start_time = time.time()<EOL>print("<STR_LIT>")<EOL>rgb_scaled = rgb * <NUM_LIT><EOL>red, green, blue = tf.split(rgb_scaled, <NUM_LIT:3>, <NUM_LIT:3>)<EOL>if red.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if green.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if blue.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>bgr = tf.concat([<EOL>blue - VGG_MEAN[<NUM_LIT:0>],<EOL>green - VGG_MEAN[<NUM_LIT:1>],<EOL>red - VGG_MEAN[<NUM_LIT:2>],<EOL>], axis=<NUM_LIT:3>)<EOL>if bgr.get_shape().as_list()[<NUM_LIT:1>:] != [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:3>]:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>net_in = InputLayer(bgr, name='<STR_LIT:input>')<EOL>net = Conv2d(net_in, <NUM_LIT:64>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT:64>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = MaxPool2d(net, filter_size=(<NUM_LIT:2>, <NUM_LIT:2>), strides=(<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = MaxPool2d(net, filter_size=(<NUM_LIT:2>, <NUM_LIT:2>), strides=(<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = MaxPool2d(net, filter_size=(<NUM_LIT:2>, <NUM_LIT:2>), strides=(<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = MaxPool2d(net, filter_size=(<NUM_LIT:2>, <NUM_LIT:2>), strides=(<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, n_filter=<NUM_LIT>, filter_size=(<NUM_LIT:3>, <NUM_LIT:3>), strides=(<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = MaxPool2d(net, filter_size=(<NUM_LIT:2>, <NUM_LIT:2>), strides=(<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = FlattenLayer(net, name='<STR_LIT>')<EOL>net = DenseLayer(net, n_units=<NUM_LIT>, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = DenseLayer(net, n_units=<NUM_LIT>, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = DenseLayer(net, n_units=<NUM_LIT:1000>, act=None, name='<STR_LIT>')<EOL>print("<STR_LIT>" % (time.time() - start_time))<EOL>return net<EOL>


I = I[<NUM_LIT>:<NUM_LIT>]<EOL>I = I[::<NUM_LIT:2>, ::<NUM_LIT:2>, <NUM_LIT:0>]<EOL>I[I == <NUM_LIT>] = <NUM_LIT:0><EOL>I[I == <NUM_LIT>] = <NUM_LIT:0><EOL>I[I != <NUM_LIT:0>] = <NUM_LIT:1><EOL>return I.astype(np.float).ravel()<EOL>

Prepro 210x160x3 uint8 frame into 6400 (80x80) 1D float vector.


def augment_with_ngrams(unigrams, unigram_vocab_size, n_buckets, n=<NUM_LIT:2>):

def get_ngrams(n):<EOL><INDENT>return list(zip(*[unigrams[i:] for i in range(n)]))<EOL><DEDENT>def hash_ngram(ngram):<EOL><INDENT>bytes_ = array.array('<STR_LIT:L>', ngram).tobytes()<EOL>hash_ = int(hashlib.sha256(bytes_).hexdigest(), <NUM_LIT:16>)<EOL>return unigram_vocab_size + hash_ % n_buckets<EOL><DEDENT>return unigrams + [hash_ngram(ngram) for i in range(<NUM_LIT:2>, n + <NUM_LIT:1>) for ngram in get_ngrams(i)]<EOL>

Augment unigram features with hashed n-gram features.


X_train, y_train, X_test, y_test = tl.files.load_imdb_dataset(nb_words=VOCAB_SIZE)<EOL>if n_gram is not None:<EOL><INDENT>X_train = np.array([augment_with_ngrams(x, VOCAB_SIZE, N_BUCKETS, n=n_gram) for x in X_train])<EOL>X_test = np.array([augment_with_ngrams(x, VOCAB_SIZE, N_BUCKETS, n=n_gram) for x in X_test])<EOL><DEDENT>return X_train, y_train, X_test, y_test<EOL>

Load IMDb data and augment with hashed n-gram features.


if FLAGS.model == "<STR_LIT>":<EOL><INDENT>init_scale = <NUM_LIT:0.1><EOL>learning_rate = <NUM_LIT:1.><EOL>max_grad_norm = <NUM_LIT:5><EOL>num_steps = <NUM_LIT:20><EOL>hidden_size = <NUM_LIT:200><EOL>max_epoch = <NUM_LIT:4><EOL>max_max_epoch = <NUM_LIT><EOL>keep_prob = <NUM_LIT:1.0><EOL>lr_decay = <NUM_LIT:0.5><EOL>batch_size = <NUM_LIT:20><EOL>vocab_size = <NUM_LIT><EOL><DEDENT>elif FLAGS.model == "<STR_LIT>":<EOL><INDENT>init_scale = <NUM_LIT><EOL>learning_rate = <NUM_LIT:1.0><EOL>max_grad_norm = <NUM_LIT:5><EOL>num_steps = <NUM_LIT><EOL>hidden_size = <NUM_LIT><EOL>max_epoch = <NUM_LIT:6><EOL>max_max_epoch = <NUM_LIT><EOL>keep_prob = <NUM_LIT:0.5><EOL>lr_decay = <NUM_LIT><EOL>batch_size = <NUM_LIT:20><EOL>vocab_size = <NUM_LIT><EOL><DEDENT>elif FLAGS.model == "<STR_LIT>":<EOL><INDENT>init_scale = <NUM_LIT><EOL>learning_rate = <NUM_LIT:1.0><EOL>max_grad_norm = <NUM_LIT:10><EOL>num_steps = <NUM_LIT><EOL>hidden_size = <NUM_LIT><EOL>max_epoch = <NUM_LIT><EOL>max_max_epoch = <NUM_LIT><EOL>keep_prob = <NUM_LIT><EOL>lr_decay = <NUM_LIT:1> / <NUM_LIT><EOL>batch_size = <NUM_LIT:20><EOL>vocab_size = <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>", FLAGS.model)<EOL><DEDENT>train_data, valid_data, test_data, vocab_size = tl.files.load_ptb_dataset()<EOL>print('<STR_LIT>'.format(len(train_data)))  <EOL>print('<STR_LIT>'.format(len(valid_data)))  <EOL>print('<STR_LIT>'.format(len(test_data)))  <EOL>print('<STR_LIT>'.format(vocab_size))  <EOL>sess = tf.InteractiveSession()<EOL>input_data = tf.placeholder(tf.int32, [batch_size, num_steps])<EOL>targets = tf.placeholder(tf.int32, [batch_size, num_steps])<EOL>input_data_test = tf.placeholder(tf.int32, [<NUM_LIT:1>, <NUM_LIT:1>])<EOL>targets_test = tf.placeholder(tf.int32, [<NUM_LIT:1>, <NUM_LIT:1>])<EOL>def inference(x, is_training, num_steps, reuse=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>print("<STR_LIT>" % (num_steps, is_training, reuse))<EOL>init = tf.random_uniform_initializer(-init_scale, init_scale)<EOL>with tf.variable_scope("<STR_LIT>", reuse=reuse):<EOL><INDENT>net = tl.layers.EmbeddingInputlayer(x, vocab_size, hidden_size, init, name='<STR_LIT>')<EOL>net = tl.layers.DropoutLayer(net, keep=keep_prob, is_fix=True, is_train=is_training, name='<STR_LIT>')<EOL>net = tl.layers.RNNLayer(<EOL>net,<EOL>cell_fn=tf.contrib.rnn.BasicLSTMCell,  <EOL>cell_init_args={<EOL>'<STR_LIT>': <NUM_LIT:0.0>,<EOL>'<STR_LIT>': True<EOL>},<EOL>n_hidden=hidden_size,<EOL>initializer=init,<EOL>n_steps=num_steps,<EOL>return_last=False,<EOL>name='<STR_LIT>'<EOL>)<EOL>lstm1 = net<EOL>net = tl.layers.DropoutLayer(net, keep=keep_prob, is_fix=True, is_train=is_training, name='<STR_LIT>')<EOL>net = tl.layers.RNNLayer(<EOL>net,<EOL>cell_fn=tf.contrib.rnn.BasicLSTMCell,  <EOL>cell_init_args={<EOL>'<STR_LIT>': <NUM_LIT:0.0>,<EOL>'<STR_LIT>': True<EOL>},<EOL>n_hidden=hidden_size,<EOL>initializer=init,<EOL>n_steps=num_steps,<EOL>return_last=False,<EOL>return_seq_2d=True,<EOL>name='<STR_LIT>'<EOL>)<EOL>lstm2 = net<EOL>net = tl.layers.DropoutLayer(net, keep=keep_prob, is_fix=True, is_train=is_training, name='<STR_LIT>')<EOL>net = tl.layers.DenseLayer(net, vocab_size, W_init=init, b_init=init, act=None, name='<STR_LIT>')<EOL><DEDENT>return net, lstm1, lstm2<EOL><DEDENT>net, lstm1, lstm2 = inference(input_data, is_training=True, num_steps=num_steps, reuse=None)<EOL>net_val, lstm1_val, lstm2_val = inference(input_data, is_training=False, num_steps=num_steps, reuse=True)<EOL>net_test, lstm1_test, lstm2_test = inference(input_data_test, is_training=False, num_steps=<NUM_LIT:1>, reuse=True)<EOL>sess.run(tf.global_variables_initializer())<EOL>def loss_fn(outputs, targets, batch_size):<EOL><INDENT>loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example(<EOL>[outputs], [tf.reshape(targets, [-<NUM_LIT:1>])], [tf.ones_like(tf.reshape(targets, [-<NUM_LIT:1>]), dtype=tf.float32)]<EOL>)<EOL>cost = tf.reduce_sum(loss) / batch_size<EOL>return cost<EOL><DEDENT>cost = loss_fn(net.outputs, targets, batch_size)<EOL>cost_val = loss_fn(net_val.outputs, targets, batch_size)<EOL>cost_test = loss_fn(net_test.outputs, targets_test, <NUM_LIT:1>)<EOL>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>lr = tf.Variable(<NUM_LIT:0.0>, trainable=False)<EOL><DEDENT>tvars = tf.trainable_variables()<EOL>grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), max_grad_norm)<EOL>optimizer = tf.train.GradientDescentOptimizer(lr)<EOL>train_op = optimizer.apply_gradients(zip(grads, tvars))<EOL>sess.run(tf.global_variables_initializer())<EOL>net.print_params()<EOL>net.print_layers()<EOL>tl.layers.print_all_variables()<EOL>print("<STR_LIT>")<EOL>for i in range(max_max_epoch):<EOL><INDENT>new_lr_decay = lr_decay**max(i - max_epoch, <NUM_LIT:0.0>)<EOL>sess.run(tf.assign(lr, learning_rate * new_lr_decay))<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, sess.run(lr)))<EOL>epoch_size = ((len(train_data) // batch_size) - <NUM_LIT:1>) // num_steps<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1.initial_state)<EOL>state2 = tl.layers.initialize_rnn_state(lstm2.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, num_steps)):<EOL><INDENT>feed_dict = {<EOL>input_data: x,<EOL>targets: y,<EOL>lstm1.initial_state.c: state1[<NUM_LIT:0>],<EOL>lstm1.initial_state.h: state1[<NUM_LIT:1>],<EOL>lstm2.initial_state.c: state2[<NUM_LIT:0>],<EOL>lstm2.initial_state.h: state2[<NUM_LIT:1>],<EOL>}<EOL>feed_dict.update(net.all_drop)<EOL>_cost, state1_c, state1_h, state2_c, state2_h, _ = sess.run(<EOL>[cost, lstm1.final_state.c, lstm1.final_state.h, lstm2.final_state.c, lstm2.final_state.h, train_op],<EOL>feed_dict=feed_dict<EOL>)<EOL>state1 = (state1_c, state1_h)<EOL>state2 = (state2_c, state2_h)<EOL>costs += _cost<EOL>iters += num_steps<EOL>if step % (epoch_size // <NUM_LIT:10>) == <NUM_LIT:10>:<EOL><INDENT>print(<EOL>"<STR_LIT>" %<EOL>(step * <NUM_LIT:1.0> / epoch_size, np.exp(costs / iters), iters * batch_size / (time.time() - start_time))<EOL>)<EOL><DEDENT><DEDENT>train_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, train_perplexity))<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1_val.initial_state)<EOL>state2 = tl.layers.initialize_rnn_state(lstm2_val.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(valid_data, batch_size, num_steps)):<EOL><INDENT>feed_dict = {<EOL>input_data: x,<EOL>targets: y,<EOL>lstm1_val.initial_state.c: state1[<NUM_LIT:0>],<EOL>lstm1_val.initial_state.h: state1[<NUM_LIT:1>],<EOL>lstm2_val.initial_state.c: state2[<NUM_LIT:0>],<EOL>lstm2_val.initial_state.h: state2[<NUM_LIT:1>],<EOL>}<EOL>_cost, state1_c, state1_h, state2_c, state2_h, _ = sess.run(<EOL>[<EOL>cost_val, lstm1_val.final_state.c, lstm1_val.final_state.h, lstm2_val.final_state.c,<EOL>lstm2_val.final_state.h,<EOL>tf.no_op()<EOL>], feed_dict=feed_dict<EOL>)<EOL>state1 = (state1_c, state1_h)<EOL>state2 = (state2_c, state2_h)<EOL>costs += _cost<EOL>iters += num_steps<EOL><DEDENT>valid_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, valid_perplexity))<EOL><DEDENT>print("<STR_LIT>")<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1_test.initial_state)<EOL>state2 = tl.layers.initialize_rnn_state(lstm2_test.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(test_data, batch_size=<NUM_LIT:1>, num_steps=<NUM_LIT:1>)):<EOL><INDENT>feed_dict = {<EOL>input_data_test: x,<EOL>targets_test: y,<EOL>lstm1_test.initial_state.c: state1[<NUM_LIT:0>],<EOL>lstm1_test.initial_state.h: state1[<NUM_LIT:1>],<EOL>lstm2_test.initial_state.c: state2[<NUM_LIT:0>],<EOL>lstm2_test.initial_state.h: state2[<NUM_LIT:1>],<EOL>}<EOL>_cost, state1_c, state1_h, state2_c, state2_h = sess.run(<EOL>[<EOL>cost_test,<EOL>lstm1_test.final_state.c,<EOL>lstm1_test.final_state.h,<EOL>lstm2_test.final_state.c,<EOL>lstm2_test.final_state.h,<EOL>], feed_dict=feed_dict<EOL>)<EOL>state1 = (state1_c, state1_h)<EOL>state2 = (state2_c, state2_h)<EOL>costs += _cost<EOL>iters += <NUM_LIT:1><EOL><DEDENT>test_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (test_perplexity, time.time() - start_time))<EOL>print(<EOL>"<STR_LIT>"<EOL>)<EOL>

The core of the model consists of an LSTM cell that processes one word at
a time and computes probabilities of the possible continuations of the
sentence. The memory state of the network is initialized with a vector
of zeros and gets updated after reading each word. Also, for computational
reasons, we will process data in mini-batches of size batch_size.


if FLAGS.model == "<STR_LIT>":<EOL><INDENT>init_scale = <NUM_LIT:0.1><EOL>learning_rate = <NUM_LIT:1.0><EOL>max_grad_norm = <NUM_LIT:5><EOL>num_steps = <NUM_LIT:20><EOL>hidden_size = <NUM_LIT:200><EOL>max_epoch = <NUM_LIT:4><EOL>max_max_epoch = <NUM_LIT><EOL>keep_prob = <NUM_LIT:1.0><EOL>lr_decay = <NUM_LIT:0.5><EOL>batch_size = <NUM_LIT:20><EOL>vocab_size = <NUM_LIT><EOL><DEDENT>elif FLAGS.model == "<STR_LIT>":<EOL><INDENT>init_scale = <NUM_LIT><EOL>learning_rate = <NUM_LIT:1.0><EOL>max_grad_norm = <NUM_LIT:5><EOL>num_steps = <NUM_LIT><EOL>hidden_size = <NUM_LIT><EOL>max_epoch = <NUM_LIT:6><EOL>max_max_epoch = <NUM_LIT><EOL>keep_prob = <NUM_LIT:0.5><EOL>lr_decay = <NUM_LIT><EOL>batch_size = <NUM_LIT:20><EOL>vocab_size = <NUM_LIT><EOL><DEDENT>elif FLAGS.model == "<STR_LIT>":<EOL><INDENT>init_scale = <NUM_LIT><EOL>learning_rate = <NUM_LIT:1.0><EOL>max_grad_norm = <NUM_LIT:10><EOL>num_steps = <NUM_LIT><EOL>hidden_size = <NUM_LIT><EOL>max_epoch = <NUM_LIT><EOL>max_max_epoch = <NUM_LIT><EOL>keep_prob = <NUM_LIT><EOL>lr_decay = <NUM_LIT:1> / <NUM_LIT><EOL>batch_size = <NUM_LIT:20><EOL>vocab_size = <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>", FLAGS.model)<EOL><DEDENT>train_data, valid_data, test_data, vocab_size = tl.files.load_ptb_dataset()<EOL>print('<STR_LIT>'.format(len(train_data)))  <EOL>print('<STR_LIT>'.format(len(valid_data)))  <EOL>print('<STR_LIT>'.format(len(test_data)))  <EOL>print('<STR_LIT>'.format(vocab_size))  <EOL>sess = tf.InteractiveSession()<EOL>input_data = tf.placeholder(tf.int32, [batch_size, num_steps])<EOL>targets = tf.placeholder(tf.int32, [batch_size, num_steps])<EOL>input_data_test = tf.placeholder(tf.int32, [<NUM_LIT:1>, <NUM_LIT:1>])<EOL>targets_test = tf.placeholder(tf.int32, [<NUM_LIT:1>, <NUM_LIT:1>])<EOL>def inference(x, is_training, num_steps, reuse=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>print("<STR_LIT>" % (num_steps, is_training, reuse))<EOL>init = tf.random_uniform_initializer(-init_scale, init_scale)<EOL>with tf.variable_scope("<STR_LIT>", reuse=reuse):<EOL><INDENT>net = tl.layers.EmbeddingInputlayer(x, vocab_size, hidden_size, init, name='<STR_LIT>')<EOL>net = tl.layers.DropoutLayer(net, keep=keep_prob, is_fix=True, is_train=is_training, name='<STR_LIT>')<EOL>net = tl.layers.RNNLayer(<EOL>net,<EOL>cell_fn=tf.contrib.rnn.BasicLSTMCell,  <EOL>cell_init_args={'<STR_LIT>': <NUM_LIT:0.0>},  <EOL>n_hidden=hidden_size,<EOL>initializer=init,<EOL>n_steps=num_steps,<EOL>return_last=False,<EOL>name='<STR_LIT>'<EOL>)<EOL>lstm1 = net<EOL>net = tl.layers.DropoutLayer(net, keep=keep_prob, is_fix=True, is_train=is_training, name='<STR_LIT>')<EOL>net = tl.layers.RNNLayer(<EOL>net,<EOL>cell_fn=tf.contrib.rnn.BasicLSTMCell,  <EOL>cell_init_args={'<STR_LIT>': <NUM_LIT:0.0>},  <EOL>n_hidden=hidden_size,<EOL>initializer=init,<EOL>n_steps=num_steps,<EOL>return_last=False,<EOL>return_seq_2d=True,<EOL>name='<STR_LIT>'<EOL>)<EOL>lstm2 = net<EOL>net = tl.layers.DropoutLayer(net, keep=keep_prob, is_fix=True, is_train=is_training, name='<STR_LIT>')<EOL>net = tl.layers.DenseLayer(net, vocab_size, W_init=init, b_init=init, act=None, name='<STR_LIT>')<EOL><DEDENT>return net, lstm1, lstm2<EOL><DEDENT>net, lstm1, lstm2 = inference(input_data, is_training=True, num_steps=num_steps, reuse=None)<EOL>net_val, lstm1_val, lstm2_val = inference(input_data, is_training=False, num_steps=num_steps, reuse=True)<EOL>net_test, lstm1_test, lstm2_test = inference(input_data_test, is_training=False, num_steps=<NUM_LIT:1>, reuse=True)<EOL>sess.run(tf.global_variables_initializer())<EOL>def loss_fn(outputs, targets):  <EOL><INDENT>loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example(<EOL>[outputs], [tf.reshape(targets, [-<NUM_LIT:1>])], [tf.ones_like(tf.reshape(targets, [-<NUM_LIT:1>]), dtype=tf.float32)]<EOL>)<EOL>cost = tf.reduce_sum(loss) / batch_size<EOL>return cost<EOL><DEDENT>cost = loss_fn(net.outputs, targets)  <EOL>cost_val = loss_fn(net_val.outputs, targets)  <EOL>cost_test = loss_fn(net_test.outputs, targets_test)  <EOL>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>lr = tf.Variable(<NUM_LIT:0.0>, trainable=False)<EOL><DEDENT>tvars = tf.trainable_variables()<EOL>grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), max_grad_norm)<EOL>optimizer = tf.train.GradientDescentOptimizer(lr)<EOL>train_op = optimizer.apply_gradients(zip(grads, tvars))<EOL>sess.run(tf.global_variables_initializer())<EOL>net.print_params()<EOL>net.print_layers()<EOL>tl.layers.print_all_variables()<EOL>print("<STR_LIT>")<EOL>for i in range(max_max_epoch):<EOL><INDENT>new_lr_decay = lr_decay**max(i - max_epoch, <NUM_LIT:0.0>)<EOL>sess.run(tf.assign(lr, learning_rate * new_lr_decay))<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, sess.run(lr)))<EOL>epoch_size = ((len(train_data) // batch_size) - <NUM_LIT:1>) // num_steps<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1.initial_state)<EOL>state2 = tl.layers.initialize_rnn_state(lstm2.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, num_steps)):<EOL><INDENT>feed_dict = {<EOL>input_data: x,<EOL>targets: y,<EOL>lstm1.initial_state: state1,<EOL>lstm2.initial_state: state2,<EOL>}<EOL>feed_dict.update(net.all_drop)<EOL>_cost, state1, state2, _ = sess.run(<EOL>[cost, lstm1.final_state, lstm2.final_state, train_op], feed_dict=feed_dict<EOL>)<EOL>costs += _cost<EOL>iters += num_steps<EOL>if step % (epoch_size // <NUM_LIT:10>) == <NUM_LIT:10>:<EOL><INDENT>print(<EOL>"<STR_LIT>" %<EOL>(step * <NUM_LIT:1.0> / epoch_size, np.exp(costs / iters), iters * batch_size / (time.time() - start_time))<EOL>)<EOL><DEDENT><DEDENT>train_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, train_perplexity))<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1_val.initial_state)<EOL>state2 = tl.layers.initialize_rnn_state(lstm2_val.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(valid_data, batch_size, num_steps)):<EOL><INDENT>feed_dict = {<EOL>input_data: x,<EOL>targets: y,<EOL>lstm1_val.initial_state: state1,<EOL>lstm2_val.initial_state: state2,<EOL>}<EOL>_cost, state1, state2, _ = sess.run(<EOL>[cost_val, lstm1_val.final_state, lstm2_val.final_state,<EOL>tf.no_op()], feed_dict=feed_dict<EOL>)<EOL>costs += _cost<EOL>iters += num_steps<EOL><DEDENT>valid_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, valid_perplexity))<EOL><DEDENT>print("<STR_LIT>")<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1_test.initial_state)<EOL>state2 = tl.layers.initialize_rnn_state(lstm2_test.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(test_data, batch_size=<NUM_LIT:1>, num_steps=<NUM_LIT:1>)):<EOL><INDENT>feed_dict = {<EOL>input_data_test: x,<EOL>targets_test: y,<EOL>lstm1_test.initial_state: state1,<EOL>lstm2_test.initial_state: state2,<EOL>}<EOL>_cost, state1, state2 = sess.run(<EOL>[cost_test, lstm1_test.final_state, lstm2_test.final_state], feed_dict=feed_dict<EOL>)<EOL>costs += _cost<EOL>iters += <NUM_LIT:1><EOL><DEDENT>test_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (test_perplexity, time.time() - start_time))<EOL>print(<EOL>"<STR_LIT>"<EOL>)<EOL>


string = re.sub(r"<STR_LIT:\n>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>return string.strip().lower()<EOL>

Tokenization/string cleaning for a datasets.


string = re.sub(r"<STR_LIT:\n>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)<EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT>", string)  <EOL>string = re.sub(r"<STR_LIT>", "<STR_LIT:U+0020>", string)  <EOL>return string.strip().lower()<EOL>


<EOL>vocabulary_size = <NUM_LIT><EOL>embedding_size = <NUM_LIT><EOL>model_file_name = "<STR_LIT>"<EOL>batch_size = None<EOL>print("<STR_LIT>")<EOL>all_var = tl.files.load_npy_to_any(name=model_file_name + '<STR_LIT>')<EOL>data = all_var['<STR_LIT:data>']<EOL>count = all_var['<STR_LIT:count>']<EOL>dictionary = all_var['<STR_LIT>']<EOL>reverse_dictionary = all_var['<STR_LIT>']<EOL>tl.nlp.save_vocab(count, name='<STR_LIT>' + model_file_name + '<STR_LIT>')<EOL>del all_var, data, count<EOL>load_params = tl.files.load_npz(name=model_file_name + '<STR_LIT>')<EOL>x = tf.placeholder(tf.int32, shape=[batch_size])<EOL>emb_net = tl.layers.EmbeddingInputlayer(x, vocabulary_size, embedding_size, name='<STR_LIT>')<EOL>sess.run(tf.global_variables_initializer())<EOL>tl.files.assign_params(sess, [load_params[<NUM_LIT:0>]], emb_net)<EOL>emb_net.print_params()<EOL>emb_net.print_layers()<EOL>word = b'<STR_LIT:hello>'<EOL>word_id = dictionary[word]<EOL>print('<STR_LIT>', word_id)<EOL>words = [b'<STR_LIT:i>', b'<STR_LIT>', b'<STR_LIT>', b'<STR_LIT>']<EOL>word_ids = tl.nlp.words_to_word_ids(words, dictionary, _UNK)<EOL>context = tl.nlp.word_ids_to_words(word_ids, reverse_dictionary)<EOL>print('<STR_LIT>', word_ids)<EOL>print('<STR_LIT>', context)<EOL>vector = sess.run(emb_net.outputs, feed_dict={x: [word_id]})<EOL>print('<STR_LIT>', vector.shape)<EOL>vectors = sess.run(emb_net.outputs, feed_dict={x: word_ids})<EOL>print('<STR_LIT>', vectors.shape)<EOL>

How to use Embedding layer, and how to convert IDs to vector,
    IDs to words, etc.


<EOL>init_scale = <NUM_LIT:0.1><EOL>learning_rate = <NUM_LIT:1.0><EOL>max_grad_norm = <NUM_LIT:5><EOL>sequence_length = <NUM_LIT:20><EOL>hidden_size = <NUM_LIT:200><EOL>max_epoch = <NUM_LIT:4><EOL>max_max_epoch = <NUM_LIT:100><EOL>lr_decay = <NUM_LIT><EOL>batch_size = <NUM_LIT:20><EOL>top_k_list = [<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:5>, <NUM_LIT:10>]<EOL>print_length = <NUM_LIT:30><EOL>model_file_name = "<STR_LIT>"<EOL>words = customized_read_words(input_fpath="<STR_LIT>")<EOL>vocab = tl.nlp.create_vocab([words], word_counts_output_file='<STR_LIT>', min_word_count=<NUM_LIT:1>)<EOL>vocab = tl.nlp.Vocabulary('<STR_LIT>', unk_word="<STR_LIT>")<EOL>vocab_size = vocab.unk_id + <NUM_LIT:1><EOL>train_data = [vocab.word_to_id(word) for word in words]<EOL>seed = "<STR_LIT>"<EOL>seed = nltk.tokenize.word_tokenize(seed)<EOL>print('<STR_LIT>' % seed)<EOL>sess = tf.InteractiveSession()<EOL>input_data = tf.placeholder(tf.int32, [batch_size, sequence_length])<EOL>targets = tf.placeholder(tf.int32, [batch_size, sequence_length])<EOL>input_data_test = tf.placeholder(tf.int32, [<NUM_LIT:1>, <NUM_LIT:1>])<EOL>def inference(x, is_train, sequence_length, reuse=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>print("<STR_LIT>" % (sequence_length, is_train, reuse))<EOL>rnn_init = tf.random_uniform_initializer(-init_scale, init_scale)<EOL>with tf.variable_scope("<STR_LIT>", reuse=reuse):<EOL><INDENT>network = EmbeddingInputlayer(x, vocab_size, hidden_size, rnn_init, name='<STR_LIT>')<EOL>network = RNNLayer(<EOL>network, cell_fn=tf.contrib.rnn.BasicLSTMCell, cell_init_args={<EOL>'<STR_LIT>': <NUM_LIT:0.0>,<EOL>'<STR_LIT>': True<EOL>}, n_hidden=hidden_size, initializer=rnn_init, n_steps=sequence_length, return_last=False,<EOL>return_seq_2d=True, name='<STR_LIT>'<EOL>)<EOL>lstm1 = network<EOL>network = DenseLayer(network, vocab_size, W_init=rnn_init, b_init=rnn_init, act=None, name='<STR_LIT>')<EOL><DEDENT>return network, lstm1<EOL><DEDENT>network, lstm1 = inference(input_data, is_train=True, sequence_length=sequence_length, reuse=None)<EOL>network_test, lstm1_test = inference(input_data_test, is_train=False, sequence_length=<NUM_LIT:1>, reuse=True)<EOL>y_linear = network_test.outputs<EOL>y_soft = tf.nn.softmax(y_linear)<EOL>def loss_fn(outputs, targets, batch_size, sequence_length):<EOL><INDENT>loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example(<EOL>[outputs], [tf.reshape(targets, [-<NUM_LIT:1>])], [tf.ones([batch_size * sequence_length])]<EOL>)<EOL>cost = tf.reduce_sum(loss) / batch_size<EOL>return cost<EOL><DEDENT>cost = loss_fn(network.outputs, targets, batch_size, sequence_length)<EOL>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>lr = tf.Variable(<NUM_LIT:0.0>, trainable=False)<EOL><DEDENT>tvars = network.all_params<EOL>grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), max_grad_norm)<EOL>optimizer = tf.train.GradientDescentOptimizer(lr)<EOL>train_op = optimizer.apply_gradients(zip(grads, tvars))<EOL>sess.run(tf.global_variables_initializer())<EOL>print("<STR_LIT>")<EOL>for i in range(max_max_epoch):<EOL><INDENT>new_lr_decay = lr_decay**max(i - max_epoch, <NUM_LIT:0.0>)<EOL>sess.run(tf.assign(lr, learning_rate * new_lr_decay))<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, sess.run(lr)))<EOL>epoch_size = ((len(train_data) // batch_size) - <NUM_LIT:1>) // sequence_length<EOL>start_time = time.time()<EOL>costs = <NUM_LIT:0.0><EOL>iters = <NUM_LIT:0><EOL>state1 = tl.layers.initialize_rnn_state(lstm1.initial_state)<EOL>for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, sequence_length)):<EOL><INDENT>_cost, state1, _ = sess.run(<EOL>[cost, lstm1.final_state, train_op], feed_dict={<EOL>input_data: x,<EOL>targets: y,<EOL>lstm1.initial_state: state1<EOL>}<EOL>)<EOL>costs += _cost<EOL>iters += sequence_length<EOL>if step % (epoch_size // <NUM_LIT:10>) == <NUM_LIT:1>:<EOL><INDENT>print(<EOL>"<STR_LIT>" %<EOL>(step * <NUM_LIT:1.0> / epoch_size, np.exp(costs / iters), iters * batch_size / (time.time() - start_time))<EOL>)<EOL><DEDENT><DEDENT>train_perplexity = np.exp(costs / iters)<EOL>print("<STR_LIT>" % (i + <NUM_LIT:1>, max_max_epoch, train_perplexity))<EOL>for top_k in top_k_list:<EOL><INDENT>state1 = tl.layers.initialize_rnn_state(lstm1_test.initial_state)<EOL>outs_id = [vocab.word_to_id(w) for w in seed]<EOL>for ids in outs_id[:-<NUM_LIT:1>]:<EOL><INDENT>a_id = np.asarray(ids).reshape(<NUM_LIT:1>, <NUM_LIT:1>)<EOL>state1 = sess.run(<EOL>[lstm1_test.final_state], feed_dict={<EOL>input_data_test: a_id,<EOL>lstm1_test.initial_state: state1<EOL>}<EOL>)<EOL><DEDENT>a_id = outs_id[-<NUM_LIT:1>]<EOL>for _ in range(print_length):<EOL><INDENT>a_id = np.asarray(a_id).reshape(<NUM_LIT:1>, <NUM_LIT:1>)<EOL>out, state1 = sess.run(<EOL>[y_soft, lstm1_test.final_state], feed_dict={<EOL>input_data_test: a_id,<EOL>lstm1_test.initial_state: state1<EOL>}<EOL>)<EOL>a_id = tl.nlp.sample_top(out[<NUM_LIT:0>], top_k=top_k)<EOL>outs_id.append(a_id)<EOL><DEDENT>sentence = [vocab.id_to_word(w) for w in outs_id]<EOL>sentence = "<STR_LIT:U+0020>".join(sentence)<EOL>print(top_k, '<STR_LIT::>', sentence)<EOL><DEDENT><DEDENT>print("<STR_LIT>")<EOL>tl.files.save_npz(network_test.all_params, name=model_file_name)<EOL>

Generate text by Synced sequence input and output.


def model_batch_norm(x, y_, reuse, is_train):

W_init = tf.truncated_normal_initializer(stddev=<NUM_LIT>)<EOL>W_init2 = tf.truncated_normal_initializer(stddev=<NUM_LIT>)<EOL>b_init2 = tf.constant_initializer(value=<NUM_LIT:0.1>)<EOL>with tf.variable_scope("<STR_LIT>", reuse=reuse):<EOL><INDENT>net = InputLayer(x, name='<STR_LIT:input>')<EOL>net = Conv2d(net, <NUM_LIT:64>, (<NUM_LIT:5>, <NUM_LIT:5>), (<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', W_init=W_init, b_init=None, name='<STR_LIT>')<EOL>net = BatchNormLayer(net, decay=<NUM_LIT>, is_train=is_train, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = MaxPool2d(net, (<NUM_LIT:3>, <NUM_LIT:3>), (<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = Conv2d(net, <NUM_LIT:64>, (<NUM_LIT:5>, <NUM_LIT:5>), (<NUM_LIT:1>, <NUM_LIT:1>), padding='<STR_LIT>', W_init=W_init, b_init=None, name='<STR_LIT>')<EOL>net = BatchNormLayer(net, decay=<NUM_LIT>, is_train=is_train, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = MaxPool2d(net, (<NUM_LIT:3>, <NUM_LIT:3>), (<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = FlattenLayer(net, name='<STR_LIT>')  <EOL>net = DenseLayer(net, <NUM_LIT>, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='<STR_LIT>')<EOL>net = DenseLayer(net, <NUM_LIT>, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='<STR_LIT>')<EOL>net = DenseLayer(net, <NUM_LIT:10>, act=None, W_init=W_init2, name='<STR_LIT>')<EOL>y = net.outputs<EOL>ce = tl.cost.cross_entropy(y, y_, name='<STR_LIT>')<EOL>L2 = <NUM_LIT:0><EOL>for p in tl.layers.get_variables_with_name('<STR_LIT>', True, True):<EOL><INDENT>L2 += tf.contrib.layers.l2_regularizer(<NUM_LIT>)(p)<EOL><DEDENT>cost = ce + L2<EOL>correct_prediction = tf.equal(tf.argmax(y, <NUM_LIT:1>), y_)<EOL>acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))<EOL>return net, cost, acc<EOL><DEDENT>

Batch normalization should be placed before rectifier.


<EOL>x = tl.prepro.crop(x, <NUM_LIT>, <NUM_LIT>, is_random=is_train)<EOL>if is_train:<EOL><INDENT>x = tl.prepro.flip_axis(x, axis=<NUM_LIT:1>, is_random=True)<EOL>x = tl.prepro.brightness(x, gamma=<NUM_LIT:0.1>, gain=<NUM_LIT:1>, is_random=True)<EOL><DEDENT>x = (x - np.mean(x)) / max(np.std(x), <NUM_LIT>)  <EOL>return x<EOL>

The images are processed as follows:
.. They are cropped to 24 x 24 pixels, centrally for evaluation or randomly for training.
.. They are approximately whitened to make the model insensitive to dynamic range.
For training, we additionally apply a series of random distortions to
artificially increase the data set size:
.. Randomly flip the image from left to right.
.. Randomly distort the image brightness.


def data_to_tfrecord(images, labels, filename):

if os.path.isfile(filename):<EOL><INDENT>print("<STR_LIT>" % filename)<EOL>return<EOL><DEDENT>print("<STR_LIT>" % filename)<EOL>writer = tf.python_io.TFRecordWriter(filename)<EOL>for index, img in enumerate(images):<EOL><INDENT>img_raw = img.tobytes()<EOL>label = int(labels[index])<EOL>example = tf.train.Example(<EOL>features=tf.train.Features(<EOL>feature={<EOL>"<STR_LIT:label>": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),<EOL>'<STR_LIT>': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),<EOL>}<EOL>)<EOL>)<EOL>writer.write(example.SerializeToString())  <EOL><DEDENT>writer.close()<EOL>


filename_queue = tf.train.string_input_producer([filename])<EOL>reader = tf.TFRecordReader()<EOL>_, serialized_example = reader.read(filename_queue)<EOL>features = tf.parse_single_example(<EOL>serialized_example, features={<EOL>'<STR_LIT:label>': tf.FixedLenFeature([], tf.int64),<EOL>'<STR_LIT>': tf.FixedLenFeature([], tf.string),<EOL>}<EOL>)<EOL>img = tf.decode_raw(features['<STR_LIT>'], tf.float32)<EOL>img = tf.reshape(img, [<NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>])<EOL>if is_train ==True:<EOL><INDENT>img = tf.random_crop(img, [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:3>])<EOL>img = tf.image.random_flip_left_right(img)<EOL>img = tf.image.random_brightness(img, max_delta=<NUM_LIT>)<EOL>img = tf.image.random_contrast(img, lower=<NUM_LIT>, upper=<NUM_LIT>)<EOL>img = tf.image.per_image_standardization(img)<EOL><DEDENT>elif is_train == False:<EOL><INDENT>img = tf.image.resize_image_with_crop_or_pad(img, <NUM_LIT>, <NUM_LIT>)<EOL>img = tf.image.per_image_standardization(img)<EOL><DEDENT>elif is_train == None:<EOL><INDENT>img = img<EOL><DEDENT>label = tf.cast(features['<STR_LIT:label>'], tf.int32)<EOL>return img, label<EOL>


return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))<EOL>

Wrapper for inserting an int64 Feature into a SequenceExample proto,
    e.g, An integer label.


<EOL>return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))<EOL>

Wrapper for inserting a bytes Feature into a SequenceExample proto,
    e.g, an image in byte


return tf.train.FeatureList(feature=[_int64_feature(v) for v in values])<EOL>

Wrapper for inserting an int64 FeatureList into a SequenceExample proto,
    e.g, sentence in list of ints


return tf.train.FeatureList(feature=[_bytes_feature(v) for v in values])<EOL>

Wrapper for inserting a bytes FeatureList into a SequenceExample proto,
    e.g, sentence in list of bytes


<EOL>with tf.name_scope("<STR_LIT>"):  <EOL><INDENT>image = tf.image.random_flip_left_right(image)<EOL><DEDENT>color_ordering = thread_id % <NUM_LIT:2><EOL>with tf.name_scope("<STR_LIT>"):  <EOL><INDENT>if color_ordering == <NUM_LIT:0>:<EOL><INDENT>image = tf.image.random_brightness(image, max_delta=<NUM_LIT> / <NUM_LIT>)<EOL>image = tf.image.random_saturation(image, lower=<NUM_LIT:0.5>, upper=<NUM_LIT>)<EOL>image = tf.image.random_hue(image, max_delta=<NUM_LIT>)<EOL>image = tf.image.random_contrast(image, lower=<NUM_LIT:0.5>, upper=<NUM_LIT>)<EOL><DEDENT>elif color_ordering == <NUM_LIT:1>:<EOL><INDENT>image = tf.image.random_brightness(image, max_delta=<NUM_LIT> / <NUM_LIT>)<EOL>image = tf.image.random_contrast(image, lower=<NUM_LIT:0.5>, upper=<NUM_LIT>)<EOL>image = tf.image.random_saturation(image, lower=<NUM_LIT:0.5>, upper=<NUM_LIT>)<EOL>image = tf.image.random_hue(image, max_delta=<NUM_LIT>)<EOL><DEDENT>image = tf.clip_by_value(image, <NUM_LIT:0.0>, <NUM_LIT:1.0>)<EOL><DEDENT>return image<EOL>

Perform random distortions on an image.
    Args:
        image: A float32 Tensor of shape [height, width, 3] with values in [0, 1).
        thread_id: Preprocessing thread id used to select the ordering of color
        distortions. There should be a multiple of 2 preprocessing threads.
    Returns:````
        distorted_image: A float32 Tensor of shape [height, width, 3] with values in
        [0, 1].


def prefetch_input_data(<EOL>reader, file_pattern, is_training, batch_size, values_per_shard, input_queue_capacity_factor=<NUM_LIT:16>,<EOL>num_reader_threads=<NUM_LIT:1>, shard_queue_name="<STR_LIT>", value_queue_name="<STR_LIT>"<EOL>):

data_files = []<EOL>for pattern in file_pattern.split("<STR_LIT:U+002C>"):<EOL><INDENT>data_files.extend(tf.gfile.Glob(pattern))<EOL><DEDENT>if not data_files:<EOL><INDENT>tl.logging.fatal("<STR_LIT>", file_pattern)<EOL><DEDENT>else:<EOL><INDENT>tl.logging.info("<STR_LIT>", len(data_files), file_pattern)<EOL><DEDENT>if is_training:<EOL><INDENT>print("<STR_LIT>")<EOL>filename_queue = tf.train.string_input_producer(data_files, shuffle=True, capacity=<NUM_LIT:16>, name=shard_queue_name)<EOL>min_queue_examples = values_per_shard * input_queue_capacity_factor<EOL>capacity = min_queue_examples + <NUM_LIT:100> * batch_size<EOL>values_queue = tf.RandomShuffleQueue(<EOL>capacity=capacity, min_after_dequeue=min_queue_examples, dtypes=[tf.string],<EOL>name="<STR_LIT>" + value_queue_name<EOL>)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL>filename_queue = tf.train.string_input_producer(data_files, shuffle=False, capacity=<NUM_LIT:1>, name=shard_queue_name)<EOL>capacity = values_per_shard + <NUM_LIT:3> * batch_size<EOL>values_queue = tf.FIFOQueue(capacity=capacity, dtypes=[tf.string], name="<STR_LIT>" + value_queue_name)<EOL><DEDENT>enqueue_ops = []<EOL>for _ in range(num_reader_threads):<EOL><INDENT>_, value = reader.read(filename_queue)<EOL>enqueue_ops.append(values_queue.enqueue([value]))<EOL><DEDENT>tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner(values_queue, enqueue_ops))<EOL>tf.summary.scalar(<EOL>"<STR_LIT>" % (values_queue.name, capacity),<EOL>tf.cast(values_queue.size(), tf.float32) * (<NUM_LIT:1.> / capacity)<EOL>)<EOL>return values_queue<EOL>

Prefetches string values from disk into an input queue.

    In training the capacity of the queue is important because a larger queue
    means better mixing of training examples between shards. The minimum number of
    values kept in the queue is values_per_shard * input_queue_capacity_factor,
    where input_queue_memory factor should be chosen to trade-off better mixing
    with memory usage.

    Args:
        reader: Instance of tf.ReaderBase.
        file_pattern: Comma-separated list of file patterns (e.g.
            /tmp/train_data-?????-of-00100).
        is_training: Boolean; whether prefetching for training or eval.
        batch_size: Model batch size used to determine queue capacity.
        values_per_shard: Approximate number of values per shard.
        input_queue_capacity_factor: Minimum number of values to keep in the queue
        in multiples of values_per_shard. See comments above.
        num_reader_threads: Number of reader threads to fill the queue.
        shard_queue_name: Name for the shards filename queue.
        value_queue_name: Name for the values input queue.

    Returns:
        A Queue containing prefetched string values.


def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_summaries=True):

enqueue_list = []<EOL>for image, caption in images_and_captions:<EOL><INDENT>caption_length = tf.shape(caption)[<NUM_LIT:0>]<EOL>input_length = tf.expand_dims(tf.subtract(caption_length, <NUM_LIT:1>), <NUM_LIT:0>)<EOL>input_seq = tf.slice(caption, [<NUM_LIT:0>], input_length)<EOL>target_seq = tf.slice(caption, [<NUM_LIT:1>], input_length)<EOL>indicator = tf.ones(input_length, dtype=tf.int32)<EOL>enqueue_list.append([image, input_seq, target_seq, indicator])<EOL><DEDENT>images, input_seqs, target_seqs, mask = tf.train.batch_join(<EOL>enqueue_list, batch_size=batch_size, capacity=queue_capacity, dynamic_pad=True, name="<STR_LIT>"<EOL>)<EOL>if add_summaries:<EOL><INDENT>lengths = tf.add(tf.reduce_sum(mask, <NUM_LIT:1>), <NUM_LIT:1>)<EOL>tf.summary.scalar("<STR_LIT>", tf.reduce_min(lengths))<EOL>tf.summary.scalar("<STR_LIT>", tf.reduce_max(lengths))<EOL>tf.summary.scalar("<STR_LIT>", tf.reduce_mean(lengths))<EOL><DEDENT>return images, input_seqs, target_seqs, mask<EOL>

Batches input images and captions.

    This function splits the caption into an input sequence and a target sequence,
    where the target sequence is the input sequence right-shifted by 1. Input and
    target sequences are batched and padded up to the maximum length of sequences
    in the batch. A mask is created to distinguish real words from padding words.

    Example:
        Actual captions in the batch ('-' denotes padded character):
        [
            [ 1 2 5 4 5 ],
            [ 1 2 3 4 - ],
            [ 1 2 3 - - ],
        ]

        input_seqs:
        [
            [ 1 2 3 4 ],
            [ 1 2 3 - ],
            [ 1 2 - - ],
        ]

        target_seqs:
        [
            [ 2 3 4 5 ],
            [ 2 3 4 - ],
            [ 2 3 - - ],
        ]

        mask:
        [
            [ 1 1 1 1 ],
            [ 1 1 1 0 ],
            [ 1 1 0 0 ],
        ]

    Args:
        images_and_captions: A list of pairs [image, caption], where image is a
        Tensor of shape [height, width, channels] and caption is a 1-D Tensor of
        any length. Each pair will be processed and added to the queue in a
        separate thread.
        batch_size: Batch size.
        queue_capacity: Queue capacity.
        add_summaries: If true, add caption length summaries.

    Returns:
        images: A Tensor of shape [batch_size, height, width, channels].
        input_seqs: An int32 Tensor of shape [batch_size, padded_length].
        target_seqs: An int32 Tensor of shape [batch_size, padded_length].
        mask: An int32 0/1 Tensor of shape [batch_size, padded_length].


st = time.time()<EOL>for _ in range(<NUM_LIT:100>):  <EOL><INDENT>xx = tl.prepro.rotation(image, rg=-<NUM_LIT:20>, is_random=False)<EOL>xx = tl.prepro.flip_axis(xx, axis=<NUM_LIT:1>, is_random=False)<EOL>xx = tl.prepro.shear2(xx, shear=(<NUM_LIT:0.>, -<NUM_LIT>), is_random=False)<EOL>xx = tl.prepro.zoom(xx, zoom_range=<NUM_LIT:1> / <NUM_LIT>)<EOL>xx = tl.prepro.shift(xx, wrg=-<NUM_LIT:0.1>, hrg=<NUM_LIT:0>, is_random=False)<EOL><DEDENT>print("<STR_LIT>" % ((time.time() - st) / <NUM_LIT:100>))<EOL>tl.vis.save_image(xx, '<STR_LIT>')<EOL>

Example 1: Applying transformation one-by-one is very SLOW !


st = time.time()<EOL>for _ in range(<NUM_LIT:100>):  <EOL><INDENT>transform_matrix = create_transformation_matrix()<EOL>result = tl.prepro.affine_transform_cv2(image, transform_matrix)  <EOL><DEDENT>print("<STR_LIT>" % ((time.time() - st) / <NUM_LIT:100>))  <EOL>tl.vis.save_image(result, '<STR_LIT>')<EOL>

Example 2: Applying all transforms in one is very FAST !


n_data = <NUM_LIT:100><EOL>imgs_file_list = ['<STR_LIT>'] * n_data<EOL>train_targets = [np.ones(<NUM_LIT:1>)] * n_data<EOL>def generator():<EOL><INDENT>if len(imgs_file_list) != len(train_targets):<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>for _input, _target in zip(imgs_file_list, train_targets):<EOL><INDENT>yield _input, _target<EOL><DEDENT><DEDENT>def _data_aug_fn(image):<EOL><INDENT>transform_matrix = create_transformation_matrix()<EOL>result = tl.prepro.affine_transform_cv2(image, transform_matrix)  <EOL>return result<EOL><DEDENT>def _map_fn(image_path, target):<EOL><INDENT>image = tf.read_file(image_path)<EOL>image = tf.image.decode_jpeg(image, channels=<NUM_LIT:3>)  <EOL>image = tf.image.convert_image_dtype(image, dtype=tf.float32)<EOL>image = tf.py_func(_data_aug_fn, [image], [tf.float32])<EOL>target = tf.reshape(target, ())<EOL>return image, target<EOL><DEDENT>n_epoch = <NUM_LIT:10><EOL>batch_size = <NUM_LIT:5><EOL>dataset = tf.data.Dataset().from_generator(generator, output_types=(tf.string, tf.int64))<EOL>dataset = dataset.shuffle(buffer_size=<NUM_LIT>)  <EOL>dataset = dataset.repeat(n_epoch)<EOL>dataset = dataset.map(_map_fn, num_parallel_calls=multiprocessing.cpu_count())<EOL>dataset = dataset.batch(batch_size)  <EOL>dataset = dataset.prefetch(<NUM_LIT:1>)  <EOL>iterator = dataset.make_one_shot_iterator()<EOL>one_element = iterator.get_next()<EOL>sess = tf.Session()<EOL>n_step = round(n_epoch * n_data / batch_size)<EOL>st = time.time()<EOL>for _ in range(n_step):<EOL><INDENT>_images, _targets = sess.run(one_element)<EOL><DEDENT>print("<STR_LIT>" % ((time.time() - st) / batch_size / n_step))<EOL>

Example 3: Using TF dataset API to load and process image for training


transform_matrix = create_transformation_matrix()<EOL>result = tl.prepro.affine_transform_cv2(image, transform_matrix)  <EOL>coords = [[(<NUM_LIT:50>, <NUM_LIT:100>), (<NUM_LIT:100>, <NUM_LIT:100>), (<NUM_LIT:100>, <NUM_LIT:50>), (<NUM_LIT:200>, <NUM_LIT:200>)], [(<NUM_LIT>, <NUM_LIT:50>), (<NUM_LIT:200>, <NUM_LIT:50>), (<NUM_LIT:200>, <NUM_LIT:100>)]]<EOL>coords_result = tl.prepro.affine_transform_keypoints(coords, transform_matrix)<EOL>def imwrite(image, coords_list, name):<EOL><INDENT>coords_list_ = []<EOL>for coords in coords_list:<EOL><INDENT>coords = np.array(coords, np.int32)<EOL>coords = coords.reshape((-<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:2>))<EOL>coords_list_.append(coords)<EOL><DEDENT>image = cv2.polylines(image, coords_list_, True, (<NUM_LIT:0>, <NUM_LIT:255>, <NUM_LIT:255>), <NUM_LIT:3>)<EOL>cv2.imwrite(name, image[..., ::-<NUM_LIT:1>])<EOL><DEDENT>imwrite(image, coords, '<STR_LIT>')<EOL>imwrite(result, coords_result, '<STR_LIT>')<EOL>

Example 4: Transforming coordinates using affine matrix.


X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>))<EOL>sess = tf.InteractiveSession()<EOL>batch_size = <NUM_LIT><EOL>x = tf.placeholder(tf.float32, shape=[batch_size, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>])  <EOL>y_ = tf.placeholder(tf.int64, shape=[batch_size])<EOL>net = tl.layers.InputLayer(x, name='<STR_LIT:input>')<EOL>net = tl.layers.Conv2d(net, <NUM_LIT:32>, (<NUM_LIT:5>, <NUM_LIT:5>), (<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = tl.layers.MaxPool2d(net, (<NUM_LIT:2>, <NUM_LIT:2>), (<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = tl.layers.Conv2d(net, <NUM_LIT:64>, (<NUM_LIT:5>, <NUM_LIT:5>), (<NUM_LIT:1>, <NUM_LIT:1>), act=tf.nn.relu, padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = tl.layers.MaxPool2d(net, (<NUM_LIT:2>, <NUM_LIT:2>), (<NUM_LIT:2>, <NUM_LIT:2>), padding='<STR_LIT>', name='<STR_LIT>')<EOL>net = tl.layers.FlattenLayer(net, name='<STR_LIT>')<EOL>net = tl.layers.DropoutLayer(net, keep=<NUM_LIT:0.5>, name='<STR_LIT>')<EOL>net = tl.layers.DenseLayer(net, <NUM_LIT>, act=tf.nn.relu, name='<STR_LIT>')<EOL>net = tl.layers.DropoutLayer(net, keep=<NUM_LIT:0.5>, name='<STR_LIT>')<EOL>net = tl.layers.DenseLayer(net, <NUM_LIT:10>, act=None, name='<STR_LIT>')<EOL>y = net.outputs<EOL>print([n.name for n in tf.get_default_graph().as_graph_def().node])<EOL>with open(graph_output_path, "<STR_LIT:wb>") as file:<EOL><INDENT>graph = tf.get_default_graph().as_graph_def(add_shapes=True)<EOL>file.write(graph.SerializeToString())<EOL><DEDENT>cost = tl.cost.cross_entropy(y, y_, '<STR_LIT>')<EOL>correct_prediction = tf.equal(tf.argmax(y, <NUM_LIT:1>), y_)<EOL>acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))<EOL>n_epoch = <NUM_LIT:200><EOL>learning_rate = <NUM_LIT><EOL>print_freq = <NUM_LIT:10><EOL>train_params = net.all_params<EOL>train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost, var_list=train_params)<EOL>tl.layers.initialize_global_variables(sess)<EOL>net.print_params()<EOL>net.print_layers()<EOL>print('<STR_LIT>' % learning_rate)<EOL>print('<STR_LIT>' % batch_size)<EOL>for epoch in range(n_epoch):<EOL><INDENT>start_time = time.time()<EOL>for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):<EOL><INDENT>feed_dict = {x: X_train_a, y_: y_train_a}<EOL>feed_dict.update(net.all_drop)  <EOL>sess.run(train_op, feed_dict=feed_dict)<EOL><DEDENT>if epoch % <NUM_LIT:10> == <NUM_LIT:0>:<EOL><INDENT>tl.files.save_ckpt(sess, mode_name='<STR_LIT>', save_dir=checkpoint_output_path, printable=True)<EOL><DEDENT>if epoch + <NUM_LIT:1> == <NUM_LIT:1> or (epoch + <NUM_LIT:1>) % print_freq == <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % (epoch + <NUM_LIT:1>, n_epoch, time.time() - start_time))<EOL>train_loss, train_acc, n_batch = <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0><EOL>for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):<EOL><INDENT>dp_dict = tl.utils.dict_to_one(net.all_drop)  <EOL>feed_dict = {x: X_train_a, y_: y_train_a}<EOL>feed_dict.update(dp_dict)<EOL>err, ac = sess.run([cost, acc], feed_dict=feed_dict)<EOL>train_loss += err<EOL>train_acc += ac<EOL>n_batch += <NUM_LIT:1><EOL><DEDENT>print("<STR_LIT>" % (train_loss / n_batch))<EOL>print("<STR_LIT>" % (train_acc / n_batch))<EOL>val_loss, val_acc, n_batch = <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0><EOL>for X_val_a, y_val_a in tl.iterate.minibatches(X_val, y_val, batch_size, shuffle=True):<EOL><INDENT>dp_dict = tl.utils.dict_to_one(net.all_drop)  <EOL>feed_dict = {x: X_val_a, y_: y_val_a}<EOL>feed_dict.update(dp_dict)<EOL>err, ac = sess.run([cost, acc], feed_dict=feed_dict)<EOL>val_loss += err<EOL>val_acc += ac<EOL>n_batch += <NUM_LIT:1><EOL><DEDENT>print("<STR_LIT>" % (val_loss / n_batch))<EOL>print("<STR_LIT>" % (val_acc / n_batch))<EOL><DEDENT><DEDENT>print('<STR_LIT>')<EOL>test_loss, test_acc, n_batch = <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0><EOL>for X_test_a, y_test_a in tl.iterate.minibatches(X_test, y_test, batch_size, shuffle=True):<EOL><INDENT>dp_dict = tl.utils.dict_to_one(net.all_drop)  <EOL>feed_dict = {x: X_test_a, y_: y_test_a}<EOL>feed_dict.update(dp_dict)<EOL>err, ac = sess.run([cost, acc], feed_dict=feed_dict)<EOL>test_loss += err<EOL>test_acc += ac<EOL>n_batch += <NUM_LIT:1><EOL><DEDENT>print("<STR_LIT>" % (test_loss / n_batch))<EOL>print("<STR_LIT>" % (test_acc / n_batch))<EOL>

Reimplementation of the TensorFlow official MNIST CNN tutorials and generate the graph and checkpoint for this model:
- https://www.tensorflow.org/versions/r0.8/tutorials/mnist/pros/index.html
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/mnist/convolutional.py

- For simplified CNN layer see "Convolutional layer (Simplified)"

Parameters
-----------
graph_output_path : string
    the path of the graph where you want to save.
checkpoint_output_path : string
    the path of the checkpoint where you want to save.

References
-----------
- `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__


def freeze_graph(graph_path, checkpoint_path, output_path, end_node_names, is_binary_graph):

_freeze_graph(<EOL>input_graph=graph_path, input_saver='<STR_LIT>', input_binary=is_binary_graph, input_checkpoint=checkpoint_path,<EOL>output_graph=output_path, output_node_names=end_node_names, restore_op_name='<STR_LIT>',<EOL>filename_tensor_name='<STR_LIT>', clear_devices=True, initializer_nodes=None<EOL>)<EOL>

Reimplementation of the TensorFlow official freeze_graph function to freeze the graph and checkpoint together:

    Parameters
    -----------
    graph_path : string
        the path where your graph file save.
    checkpoint_output_path : string
        the path where your checkpoint save.
    output_path : string
        the path where you want to save the output proto buff
    end_node_names : string
        the name of the end node in your graph you want to get in your proto buff
    is_binary_graph : boolean
        declare your file whether is a binary graph

    References
    ----------
    - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__
    - `tensorflow freeze_graph <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py>`


def convert_model_to_onnx(frozen_graph_path, end_node_names, onnx_output_path):

with tf.gfile.GFile(frozen_graph_path, "<STR_LIT:rb>") as f:<EOL><INDENT>graph_def = tf.GraphDef()<EOL>graph_def.ParseFromString(f.read())<EOL>onnx_model = tensorflow_graph_to_onnx_model(graph_def, end_node_names, opset=<NUM_LIT:6>)<EOL>file = open(onnx_output_path, "<STR_LIT:wb>")<EOL>file.write(onnx_model.SerializeToString())<EOL>file.close()<EOL><DEDENT>

Reimplementation of the TensorFlow-onnx official tutorial convert the proto buff to onnx file:

    Parameters
    -----------
    frozen_graph_path : string
        the path where your frozen graph file save.
    end_node_names : string
        the name of the end node in your graph you want to get in your proto buff
    onnx_output_path : string
        the path where you want to save the onnx file.

    References
    -----------
    - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`


model = onnx.load(onnx_input_path)<EOL>tf_rep = prepare(model)<EOL>img = np.load("<STR_LIT>")<EOL>output = tf_rep.run(img.reshape([<NUM_LIT:1>, <NUM_LIT>]))<EOL>print("<STR_LIT>", np.argmax(output))<EOL>

Reimplementation of the TensorFlow-onnx official tutorial convert the onnx file to specific: model

    Parameters
    -----------
    onnx_input_path : string
    the path where you save the onnx file.

    References
    -----------
    - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__


global _log_counter_per_token  <EOL>_log_counter_per_token[token] = <NUM_LIT:1> + _log_counter_per_token.get(token, -<NUM_LIT:1>)<EOL>return _log_counter_per_token[token]<EOL>

Wrapper for _log_counter_per_token.

    Args:
    token: The token for which to look up the count.

    Returns:
    The number of times this function has been called with
    *token* as an argument (starting at 0)


count = _GetNextLogCountPerToken(_GetFileAndLine())<EOL>log_if(level, msg, not (count % n), *args)<EOL>

Log 'msg % args' at level 'level' once per 'n' times.

    Logs the 1st call, (N+1)st call, (2N+1)st call,  etc.
    Not threadsafe.

    Args:
    level: The level at which to log.
    msg: The message to be logged.
    n: The number of times this should be called before it is logged.
    *args: The args to be substituted into the msg.


count = _GetNextLogCountPerToken(_GetFileAndLine())<EOL>log_if(level, msg, count < n, *args)<EOL>

Log 'msg % args' at level 'level' only first 'n' times.

    Not threadsafe.

    Args:
    level: The level at which to log.
    msg: The message to be logged.
    n: The number of times this should be called before it is logged.
    *args: The args to be substituted into the msg.


def log_if(level, msg, condition, *args):

if condition:<EOL><INDENT>vlog(level, msg, *args)<EOL><DEDENT>

Log 'msg % args' at level 'level' only if condition is fulfilled.


<EOL>f = _sys._getframe()<EOL>our_file = f.f_code.co_filename<EOL>f = f.f_back<EOL>while f:<EOL><INDENT>code = f.f_code<EOL>if code.co_filename != our_file:<EOL><INDENT>return (code.co_filename, f.f_lineno)<EOL><DEDENT>f = f.f_back<EOL><DEDENT>return ('<STR_LIT>', <NUM_LIT:0>)<EOL>

Returns (filename, linenumber) for the stack frame.


def google2_log_prefix(level, timestamp=None, file_and_line=None):

<EOL>global _level_names<EOL>now = timestamp or _time.time()<EOL>now_tuple = _time.localtime(now)<EOL>now_microsecond = int(<NUM_LIT> * (now % <NUM_LIT:1.0>))<EOL>(filename, line) = file_and_line or _GetFileAndLine()<EOL>basename = _os.path.basename(filename)<EOL>severity = '<STR_LIT:I>'<EOL>if level in _level_names:<EOL><INDENT>severity = _level_names[level][<NUM_LIT:0>]<EOL><DEDENT>s = '<STR_LIT>' % (<EOL>severity,<EOL>now_tuple[<NUM_LIT:1>],  <EOL>now_tuple[<NUM_LIT:2>],  <EOL>now_tuple[<NUM_LIT:3>],  <EOL>now_tuple[<NUM_LIT:4>],  <EOL>now_tuple[<NUM_LIT:5>],  <EOL>now_microsecond,<EOL>_get_thread_id(),<EOL>basename,<EOL>line<EOL>)<EOL>return s<EOL>

Assemble a logline prefix using the google2 format.


return _get_logger().getEffectiveLevel()<EOL>

Return how much logging output will be produced.


Sets the threshold for what messages will be logged.


<EOL>thread_id = six.moves._thread.get_ident()<EOL>return thread_id & _THREAD_ID_MASK<EOL>

Get id of current thread, suitable for logging as an unsigned quantity.


if shape[<NUM_LIT:0>] != shape[<NUM_LIT:1>]:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if shape[<NUM_LIT:3>] < shape[<NUM_LIT:2>]:<EOL><INDENT>raise Exception(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>filter_size = shape[<NUM_LIT:0>]<EOL>num_out_channels = shape[<NUM_LIT:2>]<EOL>num_in_channels = shape[<NUM_LIT:3>]<EOL>bilinear_kernel = np.zeros([filter_size, filter_size], dtype=np.float32)<EOL>scale_factor = (filter_size + <NUM_LIT:1>) // <NUM_LIT:2><EOL>if filter_size % <NUM_LIT:2> == <NUM_LIT:1>:<EOL><INDENT>center = scale_factor - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>center = scale_factor - <NUM_LIT:0.5><EOL><DEDENT>for x in range(filter_size):<EOL><INDENT>for y in range(filter_size):<EOL><INDENT>bilinear_kernel[x, y] = (<NUM_LIT:1> - abs(x - center) / scale_factor) * (<NUM_LIT:1> - abs(y - center) / scale_factor)<EOL><DEDENT><DEDENT>weights = np.zeros((filter_size, filter_size, num_out_channels, num_in_channels))<EOL>for i in range(num_out_channels):<EOL><INDENT>weights[:, :, i, i] = bilinear_kernel<EOL><DEDENT>return tf.constant_initializer(value=weights, dtype=LayersConfig.tf_dtype)<EOL>

Returns the initializer that can be passed to DeConv2dLayer for initializing the
    weights in correspondence to channel-wise bilinear up-sampling.
    Used in segmentation approaches such as [FCN](https://arxiv.org/abs/1605.06211)

    Parameters
    ----------
    shape : tuple of int
        The shape of the filters, [height, width, output_channels, in_channels].
        It must match the shape passed to DeConv2dLayer.

    Returns
    -------
    ``tf.constant_initializer``
        A constant initializer with weights set to correspond to per channel bilinear upsampling
        when passed as W_int in DeConv2dLayer

    Examples
    --------
    - Upsampling by a factor of 2, ie e.g 100->200
    >>> import tensorflow as tf
    >>> import tensorlayer as tl
    >>> rescale_factor = 2
    >>> imsize = 128
    >>> num_channels = 3
    >>> filter_shape = (5, 5)
    >>> filter_size = (2 * rescale_factor - rescale_factor % 2) #Corresponding bilinear filter size
    >>> num_in_channels = 3
    >>> num_out_channels = 3
    >>> deconv_filter_shape = (filter_size, filter_size, num_out_channels, num_in_channels)
    >>> x = tf.placeholder(tf.float32, (1, imsize, imsize, num_channels))
    >>> net = tl.layers.InputLayer(x, name='input_layer')
    >>> bilinear_init = deconv2d_bilinear_upsampling_initializer(shape=filter_shape)
    >>> net = tl.layers.DeConv2dLayer(net,
    ...                    shape=filter_shape,
    ...                    output_shape=(1, imsize*rescale_factor, imsize*rescale_factor, num_out_channels),
    ...                    strides=(1, rescale_factor, rescale_factor, 1),
    ...                    W_init=bilinear_init,
    ...                    padding='SAME',
    ...                    act=None, name='g/h1/decon2d')


def load_cifar10_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>), path='<STR_LIT:data>', plotable=False):

path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>def unpickle(file):<EOL><INDENT>fp = open(file, '<STR_LIT:rb>')<EOL>if sys.version_info.major == <NUM_LIT:2>:<EOL><INDENT>data = pickle.load(fp)<EOL><DEDENT>elif sys.version_info.major == <NUM_LIT:3>:<EOL><INDENT>data = pickle.load(fp, encoding='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>fp.close()<EOL>return data<EOL><DEDENT>filename = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>maybe_download_and_extract(filename, path, url, extract=True)<EOL>X_train = None<EOL>y_train = []<EOL>for i in range(<NUM_LIT:1>, <NUM_LIT:6>):<EOL><INDENT>data_dic = unpickle(os.path.join(path, '<STR_LIT>', "<STR_LIT>".format(i)))<EOL>if i == <NUM_LIT:1>:<EOL><INDENT>X_train = data_dic['<STR_LIT:data>']<EOL><DEDENT>else:<EOL><INDENT>X_train = np.vstack((X_train, data_dic['<STR_LIT:data>']))<EOL><DEDENT>y_train += data_dic['<STR_LIT>']<EOL><DEDENT>test_data_dic = unpickle(os.path.join(path, '<STR_LIT>', "<STR_LIT>"))<EOL>X_test = test_data_dic['<STR_LIT:data>']<EOL>y_test = np.array(test_data_dic['<STR_LIT>'])<EOL>if shape == (-<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:32>, <NUM_LIT:32>):<EOL><INDENT>X_test = X_test.reshape(shape)<EOL>X_train = X_train.reshape(shape)<EOL><DEDENT>elif shape == (-<NUM_LIT:1>, <NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>):<EOL><INDENT>X_test = X_test.reshape(shape, order='<STR_LIT:F>')<EOL>X_train = X_train.reshape(shape, order='<STR_LIT:F>')<EOL>X_test = np.transpose(X_test, (<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:3>))<EOL>X_train = np.transpose(X_train, (<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:3>))<EOL><DEDENT>else:<EOL><INDENT>X_test = X_test.reshape(shape)<EOL>X_train = X_train.reshape(shape)<EOL><DEDENT>y_train = np.array(y_train)<EOL>if plotable:<EOL><INDENT>logging.info('<STR_LIT>')<EOL>import matplotlib.pyplot as plt<EOL>fig = plt.figure(<NUM_LIT:1>)<EOL>logging.info('<STR_LIT>' % X_train[<NUM_LIT:0>].shape)<EOL>plt.ion()  <EOL>count = <NUM_LIT:1><EOL>for _ in range(<NUM_LIT:10>):  <EOL><INDENT>for _ in range(<NUM_LIT:10>):  <EOL><INDENT>_ = fig.add_subplot(<NUM_LIT:10>, <NUM_LIT:10>, count)<EOL>if shape == (-<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:32>, <NUM_LIT:32>):<EOL><INDENT>plt.imshow(np.transpose(X_train[count - <NUM_LIT:1>], (<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:0>)), interpolation='<STR_LIT>')<EOL><DEDENT>elif shape == (-<NUM_LIT:1>, <NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>):<EOL><INDENT>plt.imshow(X_train[count - <NUM_LIT:1>], interpolation='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>plt.gca().xaxis.set_major_locator(plt.NullLocator())<EOL>plt.gca().yaxis.set_major_locator(plt.NullLocator())<EOL>count = count + <NUM_LIT:1><EOL><DEDENT><DEDENT>plt.draw()  <EOL>plt.pause(<NUM_LIT:3>)  <EOL>logging.info("<STR_LIT>" % X_train.shape)<EOL>logging.info("<STR_LIT>" % y_train.shape)<EOL>logging.info("<STR_LIT>" % X_test.shape)<EOL>logging.info("<STR_LIT>" % y_test.shape)<EOL><DEDENT>X_train = np.asarray(X_train, dtype=np.float32)<EOL>X_test = np.asarray(X_test, dtype=np.float32)<EOL>y_train = np.asarray(y_train, dtype=np.int32)<EOL>y_test = np.asarray(y_test, dtype=np.int32)<EOL>return X_train, y_train, X_test, y_test<EOL>

Load CIFAR-10 dataset.

    It consists of 60000 32x32 colour images in 10 classes, with
    6000 images per class. There are 50000 training images and 10000 test images.

    The dataset is divided into five training batches and one test batch, each with
    10000 images. The test batch contains exactly 1000 randomly-selected images from
    each class. The training batches contain the remaining images in random order,
    but some training batches may contain more images from one class than another.
    Between them, the training batches contain exactly 5000 images from each class.

    Parameters
    ----------
    shape : tupe
        The shape of digit images e.g. (-1, 3, 32, 32) and (-1, 32, 32, 3).
    path : str
        The path that the data is downloaded to, defaults is ``data/cifar10/``.
    plotable : boolean
        Whether to plot some image examples, False as default.

    Examples
    --------
    >>> X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3))

    References
    ----------
    - `CIFAR website <https://www.cs.toronto.edu/~kriz/cifar.html>`__
    - `Data download link <https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz>`__
    - `<https://teratail.com/questions/28932>`__


path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>filename = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>maybe_download_and_extract(filename, path, url, extract=True)<EOL>data_path = os.path.join(path, '<STR_LIT>', '<STR_LIT:data>')<EOL>train_path = os.path.join(data_path, "<STR_LIT>")<EOL>valid_path = os.path.join(data_path, "<STR_LIT>")<EOL>test_path = os.path.join(data_path, "<STR_LIT>")<EOL>word_to_id = nlp.build_vocab(nlp.read_words(train_path))<EOL>train_data = nlp.words_to_word_ids(nlp.read_words(train_path), word_to_id)<EOL>valid_data = nlp.words_to_word_ids(nlp.read_words(valid_path), word_to_id)<EOL>test_data = nlp.words_to_word_ids(nlp.read_words(test_path), word_to_id)<EOL>vocab_size = len(word_to_id)<EOL>return train_data, valid_data, test_data, vocab_size<EOL>

Load Penn TreeBank (PTB) dataset.

    It is used in many LANGUAGE MODELING papers,
    including "Empirical Evaluation and Combination of Advanced Language
    Modeling Techniques", "Recurrent Neural Network Regularization".
    It consists of 929k training words, 73k validation words, and 82k test
    words. It has 10k words in its vocabulary.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to, defaults is ``data/ptb/``.

    Returns
    --------
    train_data, valid_data, test_data : list of int
        The training, validating and testing data in integer format.
    vocab_size : int
        The vocabulary size.

    Examples
    --------
    >>> train_data, valid_data, test_data, vocab_size = tl.files.load_ptb_dataset()

    References
    ---------------
    - ``tensorflow.models.rnn.ptb import reader``
    - `Manual download <http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz>`__

    Notes
    ------
    - If you want to get the raw data, see the source code.


def load_flickr1M_dataset(tag='<STR_LIT>', size=<NUM_LIT:10>, path="<STR_LIT:data>", n_threads=<NUM_LIT:50>, printable=False):

import shutil<EOL>path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(size * <NUM_LIT:10>, size * <NUM_LIT>))<EOL>images_zip = [<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'<EOL>]<EOL>tag_zip = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>for image_zip in images_zip[<NUM_LIT:0>:size]:<EOL><INDENT>image_folder = image_zip.split("<STR_LIT:.>")[<NUM_LIT:0>]<EOL>if folder_exists(os.path.join(path, image_folder)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(image_folder, path))<EOL>maybe_download_and_extract(image_zip, path, url, extract=True)<EOL>del_file(os.path.join(path, image_zip))<EOL>shutil.move(os.path.join(path, '<STR_LIT>'), os.path.join(path, image_folder))<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(image_folder, path))<EOL><DEDENT><DEDENT>if folder_exists(os.path.join(path, "<STR_LIT>")) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(path))<EOL>maybe_download_and_extract(tag_zip, path, url, extract=True)<EOL>del_file(os.path.join(path, tag_zip))<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(path))<EOL><DEDENT>images_list = []<EOL>images_folder_list = []<EOL>for i in range(<NUM_LIT:0>, size):<EOL><INDENT>images_folder_list += load_folder_list(path=os.path.join(path, '<STR_LIT>' % i))<EOL><DEDENT>images_folder_list.sort(key=lambda s: int(s.split('<STR_LIT:/>')[-<NUM_LIT:1>]))  <EOL>for folder in images_folder_list[<NUM_LIT:0>:size * <NUM_LIT:10>]:<EOL><INDENT>tmp = load_file_list(path=folder, regx='<STR_LIT>', printable=False)<EOL>tmp.sort(key=lambda s: int(s.split('<STR_LIT:.>')[-<NUM_LIT:2>]))  <EOL>images_list.extend([os.path.join(folder, x) for x in tmp])<EOL><DEDENT>tag_list = []<EOL>tag_folder_list = load_folder_list(os.path.join(path, "<STR_LIT>"))<EOL>tag_folder_list.sort(key=lambda s: int(os.path.basename(s)))<EOL>for folder in tag_folder_list[<NUM_LIT:0>:size * <NUM_LIT:10>]:<EOL><INDENT>tmp = load_file_list(path=folder, regx='<STR_LIT>', printable=False)<EOL>tmp.sort(key=lambda s: int(s.split('<STR_LIT:.>')[-<NUM_LIT:2>]))  <EOL>tmp = [os.path.join(folder, s) for s in tmp]<EOL>tag_list += tmp<EOL><DEDENT>logging.info("<STR_LIT>".format(tag))<EOL>select_images_list = []<EOL>for idx, _val in enumerate(tag_list):<EOL><INDENT>tags = read_file(tag_list[idx]).split('<STR_LIT:\n>')<EOL>if tag in tags:<EOL><INDENT>select_images_list.append(images_list[idx])<EOL><DEDENT><DEDENT>logging.info("<STR_LIT>".format(tag))<EOL>images = visualize.read_images(select_images_list, '<STR_LIT>', n_threads=n_threads, printable=printable)<EOL>return images<EOL>

Load Flick1M dataset.

    Returns a list of images by a given tag from Flickr1M dataset,
    it will download Flickr1M from `the official website <http://press.liacs.nl/mirflickr/mirdownload.html>`__
    at the first time you use it.

    Parameters
    ------------
    tag : str or None
        What images to return.
            - If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search <https://www.flickr.com/search/>`__.
            - If you want to get all images, set to ``None``.

    size : int
        integer between 1 to 10. 1 means 100k images ... 5 means 500k images, 10 means all 1 million images. Default is 10.
    path : str
        The path that the data is downloaded to, defaults is ``data/flickr25k/``.
    n_threads : int
        The number of thread to read image.
    printable : boolean
        Whether to print infomation when reading images, default is ``False``.

    Examples
    ----------
    Use 200k images

    >>> images = tl.files.load_flickr1M_dataset(tag='zebra', size=2)

    Use 1 Million images

    >>> images = tl.files.load_flickr1M_dataset(tag='zebra')


path = os.path.join(path, '<STR_LIT>')<EOL>url = '<STR_LIT>'<EOL>if folder_exists(os.path.join(path, filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(filename, path))<EOL>maybe_download_and_extract(filename + '<STR_LIT>', path, url, extract=True)<EOL>del_file(os.path.join(path, filename + '<STR_LIT>'))<EOL><DEDENT>def load_image_from_folder(path):<EOL><INDENT>path_imgs = load_file_list(path=path, regx='<STR_LIT>', printable=False)<EOL>return visualize.read_images(path_imgs, path=path, n_threads=<NUM_LIT:10>, printable=False)<EOL><DEDENT>im_train_A = load_image_from_folder(os.path.join(path, filename, "<STR_LIT>"))<EOL>im_train_B = load_image_from_folder(os.path.join(path, filename, "<STR_LIT>"))<EOL>im_test_A = load_image_from_folder(os.path.join(path, filename, "<STR_LIT>"))<EOL>im_test_B = load_image_from_folder(os.path.join(path, filename, "<STR_LIT>"))<EOL>def if_2d_to_3d(images):  <EOL><INDENT>for i, _v in enumerate(images):<EOL><INDENT>if len(images[i].shape) == <NUM_LIT:2>:<EOL><INDENT>images[i] = images[i][:, :, np.newaxis]<EOL>images[i] = np.tile(images[i], (<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:3>))<EOL><DEDENT><DEDENT>return images<EOL><DEDENT>im_train_A = if_2d_to_3d(im_train_A)<EOL>im_train_B = if_2d_to_3d(im_train_B)<EOL>im_test_A = if_2d_to_3d(im_test_A)<EOL>im_test_B = if_2d_to_3d(im_test_B)<EOL>return im_train_A, im_train_B, im_test_A, im_test_B<EOL>

Load images from CycleGAN's database, see `this link <https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/>`__.

    Parameters
    ------------
    filename : str
        The dataset you want, see `this link <https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/>`__.
    path : str
        The path that the data is downloaded to, defaults is `data/cyclegan`

    Examples
    ---------
    >>> im_train_A, im_train_B, im_test_A, im_test_B = load_cyclegan_dataset(filename='summer2winter_yosemite')


def load_mnist_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT>), path='<STR_LIT:data>'):

return _load_mnist_dataset(shape, path, name='<STR_LIT>', url='<STR_LIT>')<EOL>

Load the original mnist.

    Automatically download MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 digit images respectively.

    Parameters
    ----------
    shape : tuple
        The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)).
    path : str
        The path that the data is downloaded to.

    Returns
    -------
    X_train, y_train, X_val, y_val, X_test, y_test: tuple
        Return splitted training/validation/test set respectively.

    Examples
    --------
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1,784), path='datasets')
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))


path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>filename = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>maybe_download_and_extract(filename, path, url, expected_bytes=<NUM_LIT>)<EOL>with zipfile.ZipFile(os.path.join(path, filename)) as f:<EOL><INDENT>word_list = f.read(f.namelist()[<NUM_LIT:0>]).split()<EOL>for idx, _ in enumerate(word_list):<EOL><INDENT>word_list[idx] = word_list[idx].decode()<EOL><DEDENT><DEDENT>return word_list<EOL>

Load Matt Mahoney's dataset.

    Download a text file from Matt Mahoney's website
    if not present, and make sure it's the right size.
    Extract the first file enclosed in a zip file as a list of words.
    This dataset can be used for Word Embedding.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to, defaults is ``data/mm_test8/``.

    Returns
    --------
    list of str
        The raw text data e.g. [.... 'their', 'families', 'who', 'were', 'expelled', 'from', 'jerusalem', ...]

    Examples
    --------
    >>> words = tl.files.load_matt_mahoney_text8_dataset()
    >>> print('Data size', len(words))


data_dir = '<STR_LIT>'<EOL>filename, drive_id = "<STR_LIT>", "<STR_LIT>"<EOL>save_path = os.path.join(path, filename)<EOL>image_path = os.path.join(path, data_dir)<EOL>if os.path.exists(image_path):<EOL><INDENT>logging.info('<STR_LIT>'.format(save_path))<EOL><DEDENT>else:<EOL><INDENT>exists_or_mkdir(path)<EOL>download_file_from_google_drive(drive_id, save_path)<EOL>zip_dir = '<STR_LIT>'<EOL>with zipfile.ZipFile(save_path) as zf:<EOL><INDENT>zip_dir = zf.namelist()[<NUM_LIT:0>]<EOL>zf.extractall(path)<EOL><DEDENT>os.remove(save_path)<EOL>os.rename(os.path.join(path, zip_dir), image_path)<EOL><DEDENT>data_files = load_file_list(path=image_path, regx='<STR_LIT>', printable=False)<EOL>for i, _v in enumerate(data_files):<EOL><INDENT>data_files[i] = os.path.join(image_path, data_files[i])<EOL><DEDENT>return data_files<EOL>

Load CelebA dataset

    Return a list of image path.

    Parameters
    -----------
    path : str
        The path that the data is downloaded to, defaults is ``data/celebA/``.


def load_fashion_mnist_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT>), path='<STR_LIT:data>'):

return _load_mnist_dataset(<EOL>shape, path, name='<STR_LIT>', url='<STR_LIT>'<EOL>)<EOL>

Load the fashion mnist.

    Automatically download fashion-MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 fashion images respectively, `examples <http://marubon-ds.blogspot.co.uk/2017/09/fashion-mnist-exploring.html>`__.

    Parameters
    ----------
    shape : tuple
        The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)).
    path : str
        The path that the data is downloaded to.

    Returns
    -------
    X_train, y_train, X_val, y_val, X_test, y_test: tuple
        Return splitted training/validation/test set respectively.

    Examples
    --------
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1,784), path='datasets')
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1))


path = os.path.join(path, '<STR_LIT>')<EOL>_WMT_ENFR_TRAIN_URL = "<STR_LIT>"<EOL>_WMT_ENFR_DEV_URL = "<STR_LIT>"<EOL>def gunzip_file(gz_path, new_path):<EOL><INDENT>"""<STR_LIT>"""<EOL>logging.info("<STR_LIT>" % (gz_path, new_path))<EOL>with gzip.open(gz_path, "<STR_LIT:rb>") as gz_file:<EOL><INDENT>with open(new_path, "<STR_LIT:wb>") as new_file:<EOL><INDENT>for line in gz_file:<EOL><INDENT>new_file.write(line)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>def get_wmt_enfr_train_set(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>filename = "<STR_LIT>"<EOL>maybe_download_and_extract(filename, path, _WMT_ENFR_TRAIN_URL, extract=True)<EOL>train_path = os.path.join(path, "<STR_LIT>")<EOL>gunzip_file(train_path + "<STR_LIT>", train_path + "<STR_LIT>")<EOL>gunzip_file(train_path + "<STR_LIT>", train_path + "<STR_LIT>")<EOL>return train_path<EOL><DEDENT>def get_wmt_enfr_dev_set(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>filename = "<STR_LIT>"<EOL>dev_file = maybe_download_and_extract(filename, path, _WMT_ENFR_DEV_URL, extract=False)<EOL>dev_name = "<STR_LIT>"<EOL>dev_path = os.path.join(path, "<STR_LIT>")<EOL>if not (gfile.Exists(dev_path + "<STR_LIT>") and gfile.Exists(dev_path + "<STR_LIT>")):<EOL><INDENT>logging.info("<STR_LIT>" % dev_file)<EOL>with tarfile.open(dev_file, "<STR_LIT>") as dev_tar:<EOL><INDENT>fr_dev_file = dev_tar.getmember("<STR_LIT>" + dev_name + "<STR_LIT>")<EOL>en_dev_file = dev_tar.getmember("<STR_LIT>" + dev_name + "<STR_LIT>")<EOL>fr_dev_file.name = dev_name + "<STR_LIT>"  <EOL>en_dev_file.name = dev_name + "<STR_LIT>"<EOL>dev_tar.extract(fr_dev_file, path)<EOL>dev_tar.extract(en_dev_file, path)<EOL><DEDENT><DEDENT>return dev_path<EOL><DEDENT>logging.info("<STR_LIT>".format(path))<EOL>train_path = get_wmt_enfr_train_set(path)<EOL>dev_path = get_wmt_enfr_dev_set(path)<EOL>return train_path, dev_path<EOL>

Load WMT'15 English-to-French translation dataset.

    It will download the data from the WMT'15 Website (10^9-French-English corpus), and the 2013 news test from the same site as development set.
    Returns the directories of training data and test data.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to, defaults is ``data/wmt_en_fr/``.

    References
    ----------
    - Code modified from /tensorflow/models/rnn/translation/data_utils.py

    Notes
    -----
    Usually, it will take a long time to download this dataset.


path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"<EOL>if folder_exists(os.path.join(path, extracted_filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(extracted_filename, path))<EOL>maybe_download_and_extract(tar_filename, path, url, extract=True)<EOL>del_file(os.path.join(path, tar_filename))<EOL><DEDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename2 = "<STR_LIT>"<EOL>if folder_exists(os.path.join(path, extracted_filename2)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(extracted_filename, path))<EOL>maybe_download_and_extract(tar_filename, path, url, extract=True)<EOL>del_file(os.path.join(path, tar_filename))<EOL><DEDENT>import scipy.io as sio<EOL>logging.info("<STR_LIT>")<EOL>ann_train_list = []<EOL>ann_test_list = []<EOL>img_train_list = []<EOL>img_test_list = []<EOL>def save_joints():<EOL><INDENT>mat = sio.loadmat(os.path.join(path, extracted_filename, "<STR_LIT>"))<EOL>for _, (anno, train_flag) in enumerate(  <EOL>zip(mat['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>, <NUM_LIT:0>][<NUM_LIT:0>], mat['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>, <NUM_LIT:0>][<NUM_LIT:0>])):<EOL><INDENT>img_fn = anno['<STR_LIT:image>']['<STR_LIT:name>'][<NUM_LIT:0>, <NUM_LIT:0>][<NUM_LIT:0>]<EOL>train_flag = int(train_flag)<EOL>if train_flag:<EOL><INDENT>img_train_list.append(img_fn)<EOL>ann_train_list.append([])<EOL><DEDENT>else:<EOL><INDENT>img_test_list.append(img_fn)<EOL>ann_test_list.append([])<EOL><DEDENT>head_rect = []<EOL>if '<STR_LIT>' in str(anno['<STR_LIT>'].dtype):<EOL><INDENT>head_rect = zip(<EOL>[x1[<NUM_LIT:0>, <NUM_LIT:0>] for x1 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]], [y1[<NUM_LIT:0>, <NUM_LIT:0>] for y1 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]],<EOL>[x2[<NUM_LIT:0>, <NUM_LIT:0>] for x2 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]], [y2[<NUM_LIT:0>, <NUM_LIT:0>] for y2 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]]<EOL>)<EOL><DEDENT>else:<EOL><INDENT>head_rect = []  <EOL><DEDENT>if '<STR_LIT>' in str(anno['<STR_LIT>'].dtype):<EOL><INDENT>annopoints = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_x1s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_y1s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_x2s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_y2s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>for annopoint, head_x1, head_y1, head_x2, head_y2 in zip(annopoints, head_x1s, head_y1s, head_x2s,<EOL>head_y2s):<EOL><INDENT>if annopoint.size:<EOL><INDENT>head_rect = [<EOL>float(head_x1[<NUM_LIT:0>, <NUM_LIT:0>]),<EOL>float(head_y1[<NUM_LIT:0>, <NUM_LIT:0>]),<EOL>float(head_x2[<NUM_LIT:0>, <NUM_LIT:0>]),<EOL>float(head_y2[<NUM_LIT:0>, <NUM_LIT:0>])<EOL>]<EOL>annopoint = annopoint['<STR_LIT>'][<NUM_LIT:0>, <NUM_LIT:0>]<EOL>j_id = [str(j_i[<NUM_LIT:0>, <NUM_LIT:0>]) for j_i in annopoint['<STR_LIT:id>'][<NUM_LIT:0>]]<EOL>x = [x[<NUM_LIT:0>, <NUM_LIT:0>] for x in annopoint['<STR_LIT:x>'][<NUM_LIT:0>]]<EOL>y = [y[<NUM_LIT:0>, <NUM_LIT:0>] for y in annopoint['<STR_LIT:y>'][<NUM_LIT:0>]]<EOL>joint_pos = {}<EOL>for _j_id, (_x, _y) in zip(j_id, zip(x, y)):<EOL><INDENT>joint_pos[int(_j_id)] = [float(_x), float(_y)]<EOL><DEDENT>if '<STR_LIT>' in str(annopoint.dtype):<EOL><INDENT>vis = [v[<NUM_LIT:0>] if v.size > <NUM_LIT:0> else [<NUM_LIT:0>] for v in annopoint['<STR_LIT>'][<NUM_LIT:0>]]<EOL>vis = dict([(k, int(v[<NUM_LIT:0>])) if len(v) > <NUM_LIT:0> else v for k, v in zip(j_id, vis)])<EOL><DEDENT>else:<EOL><INDENT>vis = None<EOL><DEDENT>if ((is_16_pos_only ==True) and (len(joint_pos) == <NUM_LIT:16>)) or (is_16_pos_only == False):<EOL><INDENT>data = {<EOL>'<STR_LIT:filename>': img_fn,<EOL>'<STR_LIT:train>': train_flag,<EOL>'<STR_LIT>': head_rect,<EOL>'<STR_LIT>': vis,<EOL>'<STR_LIT>': joint_pos<EOL>}<EOL>if train_flag:<EOL><INDENT>ann_train_list[-<NUM_LIT:1>].append(data)<EOL><DEDENT>else:<EOL><INDENT>ann_test_list[-<NUM_LIT:1>].append(data)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>save_joints()<EOL>logging.info("<STR_LIT>")<EOL>img_dir = os.path.join(path, extracted_filename2)<EOL>_img_list = load_file_list(path=os.path.join(path, extracted_filename2), regx='<STR_LIT>', printable=False)<EOL>for i, im in enumerate(img_train_list):<EOL><INDENT>if im not in _img_list:<EOL><INDENT>print('<STR_LIT>'.format(im, img_dir))<EOL>del img_train_list[i]<EOL>del ann_train_list[i]<EOL><DEDENT><DEDENT>for i, im in enumerate(img_test_list):<EOL><INDENT>if im not in _img_list:<EOL><INDENT>print('<STR_LIT>'.format(im, img_dir))<EOL>del img_train_list[i]<EOL>del ann_train_list[i]<EOL><DEDENT><DEDENT>n_train_images = len(img_train_list)<EOL>n_test_images = len(img_test_list)<EOL>n_images = n_train_images + n_test_images<EOL>logging.info("<STR_LIT>".format(n_images, n_train_images, n_test_images))<EOL>n_train_ann = len(ann_train_list)<EOL>n_test_ann = len(ann_test_list)<EOL>n_ann = n_train_ann + n_test_ann<EOL>logging.info("<STR_LIT>".format(n_ann, n_train_ann, n_test_ann))<EOL>n_train_people = len(sum(ann_train_list, []))<EOL>n_test_people = len(sum(ann_test_list, []))<EOL>n_people = n_train_people + n_test_people<EOL>logging.info("<STR_LIT>".format(n_people, n_train_people, n_test_people))<EOL>for i, value in enumerate(img_train_list):<EOL><INDENT>img_train_list[i] = os.path.join(img_dir, value)<EOL><DEDENT>for i, value in enumerate(img_test_list):<EOL><INDENT>img_test_list[i] = os.path.join(img_dir, value)<EOL><DEDENT>return img_train_list, ann_train_list, img_test_list, ann_test_list<EOL>

Load MPII Human Pose Dataset.

    Parameters
    -----------
    path : str
        The path that the data is downloaded to.
    is_16_pos_only : boolean
        If True, only return the peoples contain 16 pose keypoints. (Usually be used for single person pose estimation)

    Returns
    ----------
    img_train_list : list of str
        The image directories of training data.
    ann_train_list : list of dict
        The annotations of training data.
    img_test_list : list of str
        The image directories of testing data.
    ann_test_list : list of dict
        The annotations of testing data.

    Examples
    --------
    >>> import pprint
    >>> import tensorlayer as tl
    >>> img_train_list, ann_train_list, img_test_list, ann_test_list = tl.files.load_mpii_pose_dataset()
    >>> image = tl.vis.read_image(img_train_list[0])
    >>> tl.vis.draw_mpii_pose_to_image(image, ann_train_list[0], 'image.png')
    >>> pprint.pprint(ann_train_list[0])

    References
    -----------
    - `MPII Human Pose Dataset. CVPR 14 <http://human-pose.mpi-inf.mpg.de>`__
    - `MPII Human Pose Models. CVPR 16 <http://pose.mpi-inf.mpg.de>`__
    - `MPII Human Shape, Poselet Conditioned Pictorial Structures and etc <http://pose.mpi-inf.mpg.de/#related>`__
    - `MPII Keyponts and ID <http://human-pose.mpi-inf.mpg.de/#download>`__