Example #1
0
def build_model(input_data_tensor, input_label_tensor):
    num_classes = config["num_classes"]
    images = tf.image.resize_images(input_data_tensor, [224, 224])
    logits = vgg.build(images, n_classes=num_classes, training=True)
    probs = tf.nn.softmax(logits)
    loss = L.loss(logits, tf.one_hot(input_label_tensor, num_classes))
    error_top5 = L.topK_error(probs, input_label_tensor, K=5)
    error_top1 = L.topK_error(probs, input_label_tensor, K=1)

    # you must return a dictionary with at least the "loss" as a key
    return dict(loss=loss,
                logits=logits,
                error_top5=error_top5,
                error_top1=error_top1)
Example #2
0
def build_model(input_data_tensor, input_label_tensor):
    num_classes = config["num_classes"]
    weight_decay = config["weight_decay"]
    images = tf.image.resize_images(input_data_tensor, [224, 224])
    logits = vgg.build(images, n_classes=num_classes, training=True)
    probs = tf.nn.softmax(logits)
    loss_classify = L.loss(logits, tf.one_hot(input_label_tensor, num_classes))
    loss_weight_decay = tf.reduce_sum(tf.stack([tf.nn.l2_loss(i) for i in tf.get_collection('variables')]))
    loss = loss_classify + weight_decay*loss_weight_decay
    error_top5 = L.topK_error(probs, input_label_tensor, K=5)
    error_top1 = L.topK_error(probs, input_label_tensor, K=1)

    # you must return a dictionary with loss as a key, other variables
    return dict(loss=loss,
                probs=probs,
                logits=logits,
                error_top5=error_top5,
                error_top1=error_top1)
Example #3
0
import sys
import tensorflow as tf
import tools
import numpy as np
import vgg
import argparse
from skimage.transform import resize
from skimage.io import imread

G = tf.Graph()
with G.as_default():
    images = tf.placeholder("float", [1, 224, 224, 3])
    logits = vgg.build(images, n_classes=10, training=False)
    probs = tf.nn.softmax(logits)

def predict(im):
    labels = ['airplane', 'automobile', 'bird', 'cat', 'deer',
              'dog', 'frog', 'horse', 'ship', 'truck']
    if im.shape != (224, 224, 3):
        im = resize(im, (224, 224))
    im = np.expand_dims(im, 0)
    sess = tf.get_default_session()
    results = sess.run(probs, {images: im})
    return labels[np.argmax(results)]

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-w", "--weights", required=True, help="path to weights.npz file")
    parser.add_argument("image", help="path to jpg image")
    args = parser.parse_args()
    im = imread(args.image)
                                           dataset.output_shapes)
dataset_init_op = iterator.make_initializer(dataset)
#iterator = dataset.make_one_shot_iterator()
images, labels = iterator.get_next()

print(images.shape, labels.shape)
print("Time for initializing dataset and pipelining:",
      "{0:.2f}".format(time.time() - start), "sec")

# Initialize input and output
x = images  # image batch
y = labels  # label batch

# 2. classifier model
# @TODO use stored weight and session  See  def __init__(self, imgs, weights=None, sess=None):
logits = vgg.build(x, n_classes=output_classes, training=True)
probs = tf.nn.softmax(logits)

# 3. Loss & optimizer function
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y))
vars = tf.trainable_variables()
lossL2 = tf.add_n([tf.nn.l2_loss(v) for v in vars]) * 0.0005
loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,
                                               logits=logits)) + lossL2
#optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9, use_nesterov=True).minimize(loss)
#optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(loss)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(
    loss, global_step=global_step)

#prediction = vgg.probs
Example #5
0
# initializing the VGG network from pre trained downloaded Data
vgg = vgg.Vgg19()

# getting input images and resizing the style image to content image
content_image = np.asarray(PIL.Image.open("./34.jpg"), dtype=float)
img_width = content_image.shape[0]
img_height = content_image.shape[1]
style_image = np.asarray(PIL.Image.open("./33.jpg"))
style_image = tf.image.resize_images(style_image, size=[img_width, img_height])
b = np.zeros(shape=[1, img_width, img_height, 3])
b[0] = content_image
input_var = tf.clip_by_value(tf.Variable(b, trainable=True, dtype=tf.float32),
                             0.0, 255.0)

# now building the pre trained vgg model graph for style transfer
vgg.build(input_var)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

# getting all the style layers

num_channels_5 = int(vgg.conv5_1.shape[3])
space_dim_5_1 = int(vgg.conv5_1.shape[1])
space_dim_5_2 = int(vgg.conv5_1.shape[2])
tensor_5 = tf.reshape(
    vgg.conv5_1, shape=[-1, space_dim_5_1 * space_dim_5_2, num_channels_5])
gram_matrix_5 = tf.matmul(tensor_5,
                          tensor_5 /
                          (2 * num_channels_5 * space_dim_5_1 * space_dim_5_2),
                          transpose_a=True)
logdir = 'cnn_{}_{}/train_logs/'.format(network, mode)

# Set up training data:
NUM_BATCHES = int(NUM_EPOCHS * DATASET_SIZE / BATCH_SIZE)
data_generator = utilities.infinite_generator(cifar10.get_train(), BATCH_SIZE)

# Define the placeholders:
n_input = tf.placeholder(tf.float32,
                         shape=cifar10.get_shape_input(),
                         name="input")
n_label = tf.placeholder(tf.int64,
                         shape=cifar10.get_shape_label(),
                         name="label")

# Build the model
n_output = vgg.build(n_input)

# Define the loss function
loss = tf.reduce_sum(
    tf.nn.sparse_softmax_cross_entropy_with_logits(logits=n_output,
                                                   labels=n_label,
                                                   name="softmax"))
accuracy = tf.reduce_mean(
    tf.cast(tf.equal(tf.argmax(n_output, axis=1), n_label), tf.float32))

# Add summaries to track the state of training:
tf.summary.scalar('summary/loss', loss)
tf.summary.scalar('summary/accuracy', accuracy)
summaries = tf.summary.merge_all()

# Define training operations:
Example #7
0
import vgg
import os
import tensorflow as tf
from PIL import Image
import numpy as np
vgg = vgg.Vgg19()
images = tf.placeholder(tf.float32, [None, 224, 224, 3])
vgg.build(images)
features = vgg.fc7
image_lists = os.listdir('D:\\train2014')
start = 0
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
all_features = []
temp = []
image_lists = sorted(image_lists)
with tf.Session(config=config) as session:
    for i in range(len(image_lists)):
        name = image_lists[i]
        print(name)
        temp_im = np.array(
            Image.open('D:\\train2014\\' + name).resize((224, 224)))
        if len(temp_im.shape) == 3:
            temp.append(temp_im)
        if len(temp) >= 16 or i == len(image_lists) - 1:
            temp = np.array(temp)
            temp = temp / 256
            _features = session.run(features, feed_dict={images: temp})
            all_features.append(_features)
            temp = []
all_features = np.concatenate(all_features, axis=0)
Example #8
0
            global_step = tf.get_variable(
                    'global_step', [],
                    initializer=tf.constant_initializer(0), trainable=False)

        decay_steps = 50000*350.0/FLAGS.batch_size
        batch_size = tf.placeholder(dtype=tf.int32, shape=(), name='batch_size')
            images, labels = cifar10.distorted_inputs(batch_size)
            print('zx0')
            print(images.get_shape().as_list())
#            print (str(tf.shape(images))+ str(tf.shape(labels)))
        re = tf.shape(images)[0]
            #network = resnet_model.cifar10_resnet_v2_generator(FLAGS.resnet_size, _NUM_CLASSES)

            inputs = tf.reshape(images, [-1, _HEIGHT, _WIDTH, _DEPTH])
        logits = vgg.build(inputs, _NUM_CLASSES, True)
#            labels = tf.reshape(labels, [-1, _NUM_CLASSES])
            labels = tf.one_hot(labels, 10, 1, 0)


            print(logits.get_shape())
	    #loss_classify = L.loss(logits, labels)
	    #loss_weight_decay = tf.reduce_sum(tf.stack([tf.nn.l2_loss(i) for i in tf.get_collection('variables')]))
	    #loss = loss_classify + _WEIGHT_DECAY * loss_weight_decay
            cross_entropy = tf.losses.softmax_cross_entropy(
                logits=logits, 
                onehot_labels=labels)

#            logits = cifar10.inference(images, batch_size)

#            loss = cifar10.loss(logits, labels, batch_size)