当前位置: 首页>>代码示例>>Python>>正文


Python TensorflowUtils类代码示例

本文整理汇总了Python中TensorflowUtils的典型用法代码示例。如果您正苦于以下问题:Python TensorflowUtils类的具体用法?Python TensorflowUtils怎么用?Python TensorflowUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了TensorflowUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: activation_function

def activation_function(x, name=""):
    activation_dict = {'relu': tf.nn.relu(x, name), 'elu': tf.nn.elu(x, name), 'lrelu': utils.leaky_relu(x, 0.2, name),
                       'tanh': tf.nn.tanh(x, name),
                       'sigmoid': tf.nn.sigmoid(x, name)}
    act = activation_dict[FLAGS.activation]
    utils.add_activation_summary(act)
    return act
开发者ID:shekkizh,项目名称:TensorflowProjects,代码行数:7,代码来源:MNIST_VAE.py

示例2: read_dataset

def read_dataset(data_dir):
    pickle_filename = "flowers_data.pickle"
    pickle_filepath = os.path.join(data_dir, pickle_filename)
    if not os.path.exists(pickle_filepath):
        utils.maybe_download_and_extract(data_dir, DATA_URL, is_tarfile=True)
        flower_folder = os.path.splitext(DATA_URL.split("/")[-1])[0]
        result = create_image_lists(os.path.join(data_dir, flower_folder))
        print "Training set: %d" % len(result['train'])
        print "Test set: %d" % len(result['test'])
        print "Validation set: %d" % len(result['validation'])
        print "Pickling ..."
        with open(pickle_filepath, 'wb') as f:
            pickle.dump(result, f, pickle.HIGHEST_PROTOCOL)
    else:
        print "Found pickle file!"

    with open(pickle_filepath, 'rb') as f:
        result = pickle.load(f)
        training_images = result['train']
        testing_images = result['test']
        validation_images = result['validation']

        del result

    print ("Training: %d, Validation: %d, Test: %d" % (
        len(training_images), len(validation_images), len(testing_images)))
    return training_images, testing_images, validation_images
开发者ID:shekkizh,项目名称:TensorflowProjects,代码行数:27,代码来源:read_FlowersDataset.py

示例3: vgg_net

def vgg_net(weights, image):
    layers = (
        'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',

        'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',

        'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
        'relu3_3'  # 'conv3_4', 'relu3_4', 'pool3',

        # 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
        # 'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
        #
        # 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
        # 'relu5_3', 'conv5_4', 'relu5_4'
    )

    net = {}
    current = image
    for i, name in enumerate(layers):
        kind = name[:4]
        if kind == 'conv':
            kernels, bias = weights[i][0][0][0][0]
            # matconvnet: weights are [width, height, in_channels, out_channels]
            # tensorflow: weights are [height, width, in_channels, out_channels]
            kernels = np.transpose(kernels, (1, 0, 2, 3))
            bias = bias.reshape(-1)
            current = utils.conv2d_basic(current, kernels, bias)
        elif kind == 'relu':
            current = tf.nn.relu(current)
        elif kind == 'pool':
            current = utils.avg_pool_2x2(current)
        net[name] = current

    assert len(net) == len(layers)
    return net
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:35,代码来源:GenerativeNeuralStyle.py

示例4: inference_simple

def inference_simple(dataset):
    with tf.name_scope("conv1") as scope:
        W1 = utils.weight_variable([5, 5, 1, 32], name="W1")
        b1 = utils.bias_variable([32], name="b1")
        tf.histogram_summary("W1", W1)
        tf.histogram_summary("b1", b1)
        h_conv1 = tf.nn.relu(utils.conv2d_basic(dataset, W1, b1), name="h_conv1")
        h_pool1 = utils.max_pool_2x2(h_conv1)

    with tf.name_scope("conv2") as scope:
        W2 = utils.weight_variable([3, 3, 32, 64], name="W2")
        b2 = utils.bias_variable([64], name="b2")
        tf.histogram_summary("W2", W2)
        tf.histogram_summary("b2", b2)
        h_conv2 = tf.nn.relu(utils.conv2d_basic(h_pool1, W2, b2), name="h_conv2")
        h_pool2 = utils.max_pool_2x2(h_conv2)

    with tf.name_scope("fc") as scope:
        image_size = IMAGE_SIZE // 4
        h_flat = tf.reshape(h_pool2, [-1, image_size * image_size * 64])
        W_fc = utils.weight_variable([image_size * image_size * 64, NUM_LABELS], name="W_fc")
        b_fc = utils.bias_variable([NUM_LABELS], name="b_fc")
        tf.histogram_summary("W_fc", W_fc)
        tf.histogram_summary("b_fc", b_fc)
        pred = tf.matmul(h_flat, W_fc) + b_fc

    return pred
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:27,代码来源:FaceDetection.py

示例5: train

def train(loss_val, var_list):
    optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
    grads = optimizer.compute_gradients(loss_val, var_list=var_list)
    if FLAGS.debug:
        # print(len(var_list))
        for grad, var in grads:
            utils.add_gradient_summary(grad, var)
    return optimizer.apply_gradients(grads)
开发者ID:Selimam,项目名称:AutoPortraitMatting,代码行数:8,代码来源:FCN.py

示例6: main

def main(argv=None):
    utils.maybe_download_and_extract(FLAGS.model_dir, DATA_URL)
    model_data = get_model_data()
    model_params = {}
    mean = model_data['normalization'][0][0][0]
    model_params["mean_pixel"] = np.mean(mean, axis=(0, 1))
    model_params["weights"] = np.squeeze(model_data['layers'])
    visualize_layer(model_params)
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:8,代码来源:LayerVisualization.py

示例7: visualize

    def visualize():
        count = 20
        z_feed = np.random.uniform(-1.0, 1.0, size=(count, FLAGS.z_dim)).astype(np.float32)
        # z_feed = np.tile(np.random.uniform(-1.0, 1.0, size=(1, FLAGS.z_dim)).astype(np.float32), (count, 1))
        # z_feed[:, 25] = sorted(10.0 * np.random.randn(count))
        image = sess.run(gen_images, feed_dict={z_vec: z_feed, train_phase: False})

        for iii in xrange(count):
            print(image.shape)
            utils.save_image(image[iii, :, :, :], IMAGE_SIZE, FLAGS.logs_dir, name=str(iii))
            print("Saving image" + str(iii))
开发者ID:shekkizh,项目名称:TensorflowProjects,代码行数:11,代码来源:Flowers_GAN.py

示例8: main

def main(argv=None):
    utils.maybe_download_and_extract(FLAGS.model_dir, DATA_URL)
    model_data = get_model_data()
    dream_image = get_image(FLAGS.image_path)
    # dream_image = np.random.uniform(size=(1, 300, 300, 3)) + 100.0
    print dream_image.shape

    model_params = {}
    mean = model_data['normalization'][0][0][0]
    model_params["mean_pixel"] = np.mean(mean, axis=(0, 1))
    model_params["weights"] = np.squeeze(model_data['layers'])
    deepdream_image(model_params, dream_image, no_of_octave=3)
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:12,代码来源:DeepDream.py

示例9: main

def main(argv=None):
    print "Reading notMNIST data..."
    train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = \
        read_notMNIST.get_notMNISTData(FLAGS.data_dir)

    print "Setting up tf model..."
    dataset = tf.placeholder(tf.float32, shape=(None, IMAGE_SIZE * IMAGE_SIZE))

    labels = tf.placeholder(tf.float32, shape=(None, NUMBER_OF_CLASSES))

    global_step = tf.Variable(0, trainable=False)

    logits = inference_fully_convolutional(dataset)

    for var in tf.trainable_variables():
        utils.add_to_regularization_and_summary(var)

    loss_val = loss(logits, labels)
    train_op = train(loss_val, global_step)
    summary_op = tf.merge_all_summaries()
    with tf.Session() as sess:
        print "Setting up summary and saver..."
        sess.run(tf.initialize_all_variables())
        summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)
        saver = tf.train.Saver()
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print "Model restored!"

        if FLAGS.mode == "train":
            for step in xrange(MAX_ITERATIONS):
                offset = (step * BATCH_SIZE) % (train_labels.shape[0] - BATCH_SIZE)

                batch_data = train_dataset[offset:(offset + BATCH_SIZE), :]
                batch_labels = train_labels[offset:(offset + BATCH_SIZE), :]

                feed_dict = {dataset: batch_data, labels: batch_labels}
                if step % 100 == 0:
                    l, summary_str = sess.run([loss_val, summary_op], feed_dict=feed_dict)
                    print "Step: %d Mini batch loss: %g"%(step, l)
                    summary_writer.add_summary(summary_str, step)

                if step % 1000 == 0:
                    valid_loss = sess.run(loss_val, feed_dict={dataset:valid_dataset, labels:valid_labels})
                    print "-- Validation loss %g" % valid_loss
                    saver.save(sess, FLAGS.logs_dir +"model.ckpt", global_step=step)

                sess.run(train_op, feed_dict=feed_dict)

        test_loss = sess.run(loss_val, feed_dict={dataset:test_dataset, labels:test_labels})
        print "Test loss: %g" % test_loss
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:52,代码来源:notMNISTFullyConvultional.py

示例10: read_input

def read_input(model_params):
    if FLAGS.mode == "test":
        content_image = get_image(FLAGS.test_image_path)
        print content_image.shape
        processed_content = utils.process_image(content_image, model_params["mean_pixel"]).astype(np.float32) / 255.0
        return processed_content, None

    else:
        data_directory = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin')
        filenames = [os.path.join(data_directory, 'data_batch_%d.bin' % i) for i in xrange(1, 6)]
        for f in filenames:
            if not tf.gfile.Exists(f):
                raise ValueError('Failed to find file: ' + f)

        filename_queue = tf.train.string_input_producer(filenames)
        print "Reading cifar10 data"
        read_input = read_cifar10(model_params, filename_queue)
        num_preprocess_threads = 8
        min_queue_examples = int(0.4 * NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN)
        print "Shuffling train batch"
        input_images, input_content_features = tf.train.shuffle_batch([read_input.image, read_input.content_features],
                                                                      batch_size=FLAGS.batch_size,
                                                                      num_threads=num_preprocess_threads,
                                                                      capacity=min_queue_examples + 3 * FLAGS.batch_size,
                                                                      min_after_dequeue=min_queue_examples)
        return input_images, input_content_features
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:26,代码来源:GenerativeNeuralStyle.py

示例11: read_cifar10

def read_cifar10(model_params, filename_queue):
    class CIFAR10Record(object):
        pass

    result = CIFAR10Record()

    label_bytes = 1  # 2 for CIFAR-100
    result.height = IMAGE_SIZE
    result.width = IMAGE_SIZE
    result.depth = 3
    image_bytes = result.height * result.width * result.depth
    record_bytes = label_bytes + image_bytes

    reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
    result.key, value = reader.read(filename_queue)

    record_bytes = tf.decode_raw(value, tf.uint8)

    depth_major = tf.cast(tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]),
                                     [result.depth, result.height, result.width]), tf.float32)

    result.image = utils.process_image(tf.transpose(depth_major, [1, 2, 0]), model_params['mean_pixel']) / 255.0
    extended_image = 255 * tf.reshape(result.image, (1, result.height, result.width, result.depth))

    result.net = vgg_net(model_params["weights"], extended_image)
    content_feature = result.net[CONTENT_LAYER]
    result.content_features = content_feature
    return result
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:28,代码来源:GenerativeNeuralStyle.py

示例12: inference

def inference(data, keep_prob):
    with tf.variable_scope("inference") as scope:
        weight_variable_size = IMAGE_SIZE * IMAGE_SIZE * 50 + 50 * 50 * 3 + 50 * 10
        bias_variable_size = 4 * 50 + 10
        print (weight_variable_size + bias_variable_size)
        variable = utils.weight_variable([weight_variable_size + bias_variable_size], name="variables")
        weight_variable = tf.slice(variable, [0], [weight_variable_size], name="weights")
        bias_variable = tf.slice(variable, [weight_variable_size], [bias_variable_size], name="biases")
        weight_offset = 0
        bias_offset = 0
        W_1 = tf.slice(weight_variable, [weight_offset], [IMAGE_SIZE * IMAGE_SIZE * 50], name="W_1")
        b_1 = tf.slice(bias_variable, [bias_offset], [50], name="b_1")
        h_1_relu = tf.nn.relu(tf.matmul(data, tf.reshape(W_1, [IMAGE_SIZE * IMAGE_SIZE, 50])) + b_1, name='h_1')
        h_1 = tf.nn.dropout(h_1_relu, keep_prob)
        utils.add_activation_summary(h_1)

        weight_offset += IMAGE_SIZE * IMAGE_SIZE * 50
        bias_offset += 50

        W_2 = tf.slice(weight_variable, [weight_offset], [50 * 50], name="W_2")
        b_2 = tf.slice(bias_variable, [bias_offset], [50], name="b_2")
        h_2_relu = tf.nn.relu(tf.matmul(h_1, tf.reshape(W_2, [50, 50])) + b_2, name='h_2')
        h_2 = tf.nn.dropout(h_2_relu, keep_prob)
        utils.add_activation_summary(h_2)

        weight_offset += 50 * 50
        bias_offset += 50

        W_3 = tf.slice(weight_variable, [weight_offset], [50 * 50], name="W_3")
        b_3 = tf.slice(bias_variable, [bias_offset], [50], name="b_3")
        h_3_relu = tf.nn.relu(tf.matmul(h_2, tf.reshape(W_3, [50, 50])) + b_3, name='h_3')
        h_3 = tf.nn.dropout(h_3_relu, keep_prob)
        utils.add_activation_summary(h_3)

        weight_offset += 50 * 50
        bias_offset += 50

        W_4 = tf.slice(weight_variable, [weight_offset], [50 * 50], name="W_4")
        b_4 = tf.slice(bias_variable, [bias_offset], [50], name="b_4")
        h_4_relu = tf.nn.relu(tf.matmul(h_3, tf.reshape(W_4, [50, 50])) + b_4, name='h_4')
        h_4 = tf.nn.dropout(h_4_relu, keep_prob)
        utils.add_activation_summary(h_4)

        weight_offset += 50 * 50
        bias_offset += 50

        W_final = tf.slice(weight_variable, [weight_offset], [50 * 10], name="W_final")
        b_final = tf.slice(bias_variable, [bias_offset], [10], name="b_final")
        pred = tf.nn.softmax(tf.matmul(h_4, tf.reshape(W_final, [50, 10])) + b_final, name='h_final')
        # utils.add_activation_summary(pred)
    return pred
开发者ID:shekkizh,项目名称:TensorflowProjects,代码行数:51,代码来源:OptimalBrainDamage_2.py

示例13: deepdream_image

def deepdream_image(model_params, image, octave_scale=1.4, no_of_octave=4):
    filename = "%s_deepdream_%s.jpg" % (os.path.splitext((FLAGS.image_path.split("/")[-1]))[0], DREAM_LAYER)

    processed_image = utils.process_image(image, model_params["mean_pixel"]).astype(np.float32)
    input_image = tf.placeholder(tf.float32)
    dream_net = vgg_net(model_params["weights"], input_image)

    def calc_grad_tiled(img, gradient, tile_size=512):
        sz = tile_size
        h, w = img.shape[1:3]
        sx, sy = np.random.randint(sz, size=2)
        img_shift = np.roll(np.roll(img, sx, 2), sy, 1)
        gradient_val = np.zeros_like(img)
        for y in xrange(0, max(h - sz // 2, sz), sz):
            for x in xrange(0, max(w - sz // 2, sz), sz):
                sub_img = img_shift[:, y:y + sz, x:x + sz]
                # print sub_img.shape
                g = sess.run(gradient, {input_image: sub_img})
                gradient_val[:, y:y + sz, x:x + sz] = g

        return np.roll(np.roll(gradient_val, -sx, 2), -sy, 1)

    step = LEARNING_RATE
    feature = DREAM_FEATURE
    with tf.Session() as sess:
        dream_layer_features = dream_net[DREAM_LAYER][:, :, :, feature]
        feature_score = tf.reduce_mean(dream_layer_features)
        grad_op = tf.gradients(feature_score, input_image)[0]

        dummy_image = processed_image.copy()+100.0
        for itr in xrange(5):
            octaves = []
            for i in xrange(no_of_octave - 1):
                hw = dummy_image.shape[1:3]
                lo = resize_image(dummy_image, np.int32(np.float32(hw) / octave_scale))
                hi = dummy_image - resize_image(dummy_image, hw)
                dummy_image = lo
                octaves.append(hi)

            for octave in xrange(no_of_octave):
                if octave > 0:
                    hi = octaves[-octave]
                    dummy_image = resize_image(dummy_image, hi.shape[1:3]) + hi
                for i in xrange(MAX_ITERATIONS):
                    grad = calc_grad_tiled(dummy_image, grad_op)
                    dummy_image += grad * (step / (np.abs(grad).mean() + 1e-8))
                    print '.',
                print "."

            # step /= 2.0  # halfing step size every itr
            feature += 15
            temp_file = "%d_%s" % (itr, filename)
            # print dummy_image.shape
            output = dummy_image.reshape(processed_image.shape[1:]) - 100.0
            save_image(os.path.join(FLAGS.logs_dir, "checkpoints", temp_file), output, model_params["mean_pixel"])
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:55,代码来源:DeepDream.py

示例14: read_dataset

def read_dataset(data_dir):
    pickle_filename = "MITSceneParsing.pickle"
    pickle_filepath = os.path.join(data_dir, pickle_filename)
    if not os.path.exists(pickle_filepath):
        utils.maybe_download_and_extract(data_dir, DATA_URL, is_zipfile=True)
        SceneParsing_folder = os.path.splitext(DATA_URL.split("/")[-1])[0]
        result = create_image_lists(os.path.join(data_dir, SceneParsing_folder))
        print ("Pickling ...")
        with open(pickle_filepath, 'wb') as f:
            pickle.dump(result, f, pickle.HIGHEST_PROTOCOL)
    else:
        print ("Found pickle file!")

    with open(pickle_filepath, 'rb') as f:
        result = pickle.load(f)
        training_records = result['training']
        validation_records = result['validation']
        del result

    return training_records, validation_records
开发者ID:hephaex,项目名称:tensorflow_note,代码行数:20,代码来源:read_MITSceneParsingData.py

示例15: main

def main(argv=None):
    utils.maybe_download_and_extract(FLAGS.data_dir, DATA_URL, is_tarfile=True)
    print "Setting up model..."
    global_step = tf.Variable(0, trainable=False)
    gray, color = inputs()
    pred = 255 * inference(gray) + 128
    tf.image_summary("Gray", gray, max_images=1)
    tf.image_summary("Ground_truth", color, max_images=1)
    tf.image_summary("Prediction", pred, max_images=1)

    image_loss = loss(pred, color)
    train_op = train(image_loss, global_step)

    summary_op = tf.merge_all_summaries()
    with tf.Session() as sess:
        print "Setting up summary writer, queue, saver..."
        sess.run(tf.initialize_all_variables())

        summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)
        saver = tf.train.Saver()

        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            print "Restoring model from checkpoint..."
            saver.restore(sess, ckpt.model_checkpoint_path)
        tf.train.start_queue_runners(sess)
        for step in xrange(MAX_ITERATIONS):
            if step % 400 == 0:
                loss_val, summary_str = sess.run([image_loss, summary_op])
                print "Step %d, Loss: %g" % (step, loss_val)
                summary_writer.add_summary(summary_str, global_step=step)

            if step % 1000 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=step)
                print "%s" % datetime.now()

            sess.run(train_op)
开发者ID:RosieCampbell,项目名称:TensorflowProjects,代码行数:37,代码来源:ImageColoring.py


注:本文中的TensorflowUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。