Exemple #1
0
    def __init__(self, data, label, input_sigma, is_training, noised_pretrain=None, noise=None):
        self.label = label
        self.data = tf.image.resize(data, [224, 224], method=tf.image.ResizeMethod.BILINEAR)
        #self.data = data
        self.input_sigma = input_sigma
        self.is_training = is_training
        #
        tf.debugging.set_log_device_placement(True)
        gpus = tf.config.experimental.list_logical_devices('GPU')
        #with tf.device(gpus[0].name):

        # cnn model
        self.pre_trained = nets.VGG19(self.data, is_training=True, classes=100, use_logits=True)
        self.pre_trained_cnn = tf.reshape(self.pre_trained, [self.pre_trained.get_shape().as_list()[0],-1])
        #import pdb; pdb.set_trace()

        self.noised_pre = noised_pretrain
        self.pre = self.noised_pre- noise

        initializer = tf.contrib.layers.variance_scaling_initializer(factor=1.0, mode='FAN_AVG', uniform=True)
             
        with tf.variable_scope('top_layers_1') as opt_scope_1:
            w_1 = tf.get_variable(initializer=initializer, shape=(self.pre_trained_cnn.get_shape().as_list()[-1], 32), name="W_1")
            b_1 = tf.get_variable(initializer=tf.zeros_initializer(), shape=(32), name="b_1")
            w_2 = tf.get_variable(initializer=initializer, shape=(32, 10), name="w_2")
            b_2 = tf.get_variable(initializer=tf.zeros_initializer(), shape=(10), name="b_2")

            net = self.noised_pre
            net = tf.nn.leaky_relu(tf.add(tf.matmul(net, w_1), b_1))
            self.cnn_logits = tf.add(tf.matmul(net, w_2), b_2)
            self.cnn_prediction = tf.nn.softmax(self.cnn_logits)

            correct_pred = tf.equal(tf.argmax(self.cnn_prediction, 1), tf.argmax(self.label, 1))
            self.cnn_accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

        self.opt_scope_1 = opt_scope_1
        #self.opt_scope_2 = opt_scope_2
        # recon

        with tf.variable_scope(opt_scope_1, reuse=True): 
            net = self.pre
            net = tf.nn.leaky_relu(tf.add(tf.matmul(net, w_1), b_1))

            self.cnn_clean_logits = tf.add(tf.matmul(net, w_2), b_2)
            self.cnn_clean_prediction = tf.nn.softmax(self.cnn_clean_logits)

            correct_pred = tf.equal(tf.argmax(self.cnn_clean_prediction, 1), tf.argmax(self.label, 1))
            self.cnn_clean_accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

        # bottom layers
        with tf.variable_scope(opt_scope_1, reuse=True):
            net = self.pre_trained_cnn + noise
            net = tf.nn.leaky_relu(tf.add(tf.matmul(net, w_1), b_1))

            self.cnn_bott_logits = tf.add(tf.matmul(net, w_2), b_2)
def transfer_learning(left_input_image, right_input_image):
    """
	left_input_image: 3D tensor input
	right_input_image: 3D tensor input
	label: 1 if images are from same category. 0 if not.
	"""

    left_features = nets.VGG19(left_input_image,
                               is_training=True,
                               reuse=tf.AUTO_REUSE)
    left_features.pretrained()
    right_features = nets.VGG19(right_input_image,
                                is_training=True,
                                reuse=tf.AUTO_REUSE)
    right_features.pretrained()

    merged_features = tf.abs(tf.subtract(left_features, right_features))
    logits = tf.contrib.layers.fully_connected(merged_features,
                                               num_outputs=1,
                                               activation_fn=None)
    logits = tf.reshape(logits, [-1])
    return logits, left_features, right_features
Exemple #3
0
    def get_vgg_features(input):
        """Gets activation of vgg network
        :param input: input tensor with image
        :return: activations of selected layers
        """
        input = preprocess(input)
        model = tnets.VGG19(input)
        model.pretrained()
        print(model.pretrained)

        feature_names = ['vgg19/conv3/4/Relu:0', 'vgg19/conv4/4/Relu:0', 'vgg19/conv5/4/Relu:0']

        middles = model.get_middles()
        result = []

        for tensor in middles:
            for name in feature_names:
                if tensor.name.endswith(name):
                    result.append(tensor)

        return result
Exemple #4
0
    target_size, crop_size = 331, 331
    inputs = tf.placeholder(tf.float32,
                            [None, crop_size, crop_size, 3])  # extra large
    model = nets.NASNetAlarge(inputs)
else:
    target_size, crop_size = 256, 224
    inputs = tf.placeholder(tf.float32,
                            [None, crop_size, crop_size, 3])  # normal size
    #model = nets.ResNet50(inputs)

if modelname == 'NASNetAlarge':
    model = nets.NASNetAlarge(inputs)
    featlayer = model.get_outputs()[-4]  # ?x4032
    bs = 250
elif modelname == 'VGG19':
    model = nets.VGG19(inputs)
    featlayer = model.get_outputs()[-4]  # ?x4096
    bs = 100
elif modelname == 'MobileNet25':
    model = nets.MobileNet25(inputs)
    featlayer = model.get_outputs()[-4]
    bs = 2500
elif modelname == 'SqueezeNet':
    model = nets.SqueezeNet(inputs)
    featlayer = model.get_outputs()[-2]
    bs = 1000
elif modelname == 'ResNet50':
    model = nets.ResNet50(inputs)
    featlayer = model.get_outputs()[-3]  # 'avgpool:0', ?x2048
    bs = 500
elif modelname == 'InceptionResNet2':
Exemple #5
0
#x = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
#one_hot_encode = one_hot_encode(x)

preprocess_and_save_data(cifar10_dataset_folder_path, one_hot_encode)

valid_features, valid_labels = pickle.load(
    open('preprocess_validation.p', mode='rb'))

x = tf.placeholder(tf.float32, shape=(None, 224, 224, 3), name='input_x')
y = tf.placeholder(tf.float32, shape=(None, 10), name='output_y')

learning_rate = 0.00001
epochs = 7
batch_size = 1

logits = nets.VGG19(x, is_training=True,
                    classes=10)  ################################
model = tf.identity(logits, name='logits')

loss = tf.losses.softmax_cross_entropy(y, logits)
train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)

correct_pred = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

logits.print_outputs()

logits.print_summary()


def batch_features_labels(features, labels, batch_size):
    """
Exemple #6
0
# In[11]:

tf.reset_default_graph()
g = tf.get_default_graph()
with g.as_default():
    # Placeholder nodes.
    x_holder = tf.placeholder(tf.float32, [batch_size, 32, 32, 3])
    label_holder = tf.placeholder(tf.float32, [batch_size, 10])
    is_training = tf.placeholder(tf.bool, ())

    # cnn model
    x_ = tf.image.resize(x_holder, [224, 224],
                         method=tf.image.ResizeMethod.BILINEAR)
    pre_trained = nets.VGG19(x_,
                             is_training=is_training,
                             classes=100,
                             use_logits=True)
    pre_trained_cnn = tf.reshape(pre_trained,
                                 [pre_trained.get_shape().as_list()[0], -1])

    initializer = tf.contrib.layers.variance_scaling_initializer(
        factor=1.0, mode='FAN_AVG', uniform=True)
    with tf.variable_scope('top_layers') as opt_scope_1:
        w_1 = tf.get_variable(initializer=initializer,
                              shape=(pre_trained_cnn.get_shape().as_list()[-1],
                                     32),
                              name="w_1")
        b_1 = tf.get_variable(initializer=tf.zeros_initializer(),
                              shape=(32),
                              name="b_1")
        w_2 = tf.get_variable(initializer=initializer,
    def __init__(self,
                 data,
                 label,
                 input_sigma,
                 is_training,
                 noised_pretrain=None,
                 noise=None):
        self.label = label
        self.data = data
        self.input_sigma = input_sigma
        self.is_training = is_training
        #
        tf.debugging.set_log_device_placement(True)
        gpus = tf.config.experimental.list_logical_devices('GPU')
        #with tf.device(gpus[0].name):

        tiled_data = tf.tile(self.data, [1, 1, 1, 3])

        # cnn model
        with tf.variable_scope('CNN') as scope:
            pre_trained = nets.VGG19(tiled_data,
                                     is_training=False,
                                     early_stem=True,
                                     stem=True)
            net = tf.identity(pre_trained)
            print(net.get_shape().as_list())
            initializer = tf.contrib.layers.variance_scaling_initializer(
                factor=1.0, mode='FAN_AVG', uniform=True)

            w_1 = tf.get_variable(initializer=initializer,
                                  shape=(3, 3, net.get_shape().as_list()[-1],
                                         128),
                                  name="w_1")
            b_1 = tf.get_variable(initializer=initializer,
                                  shape=(128),
                                  name="b_1")
            net = tf.nn.conv2d(net, w_1, strides=[1, 1, 1, 1],
                               padding="SAME") + b_1
            net = tf.nn.leaky_relu(net)
            net = tf.reshape(net, [FLAGS.BATCH_SIZE, -1])
        self.pretrain_cnn = net

        self.noised_pretrain = noised_pretrain
        self.pretrain = self.noised_pretrain - noise

        with tf.variable_scope('top_layers') as opt_scope_1:
            w_2 = tf.get_variable(
                initializer=initializer,
                shape=(self.noised_pretrain.get_shape().as_list()[-1], 16),
                name="w_2")
            b_2 = tf.get_variable(initializer=initializer,
                                  shape=(16),
                                  name="b_2")
            w_3 = tf.get_variable(initializer=initializer,
                                  shape=(16, 10),
                                  name="w_3")
            b_3 = tf.get_variable(initializer=initializer,
                                  shape=(10),
                                  name="b_3")

            net = self.noised_pretrain
            #net = ne.layer_norm(self.noised_pretrain, self.is_training)
            net = tf.nn.leaky_relu(tf.add(tf.matmul(net, w_2), b_2))
            #net = ne.layer_norm(net, self.is_training)

            self.cnn_logits = tf.add(tf.matmul(net, w_3), b_3)
            #self.cnn_logits = tf.add(tf.matmul(net, w_2), b_2)
            self.cnn_prediction = tf.nn.softmax(self.cnn_logits)

            correct_pred = tf.equal(tf.argmax(self.cnn_prediction, 1),
                                    tf.argmax(self.label, 1))
            self.cnn_accuracy = tf.reduce_mean(tf.cast(correct_pred,
                                                       tf.float32),
                                               name='accuracy')

        self.opt_scope_1 = opt_scope_1
        # recon
        with tf.variable_scope(opt_scope_1, reuse=True):
            w_2 = tf.get_variable(
                initializer=initializer,
                shape=(self.pretrain.get_shape().as_list()[-1], 16),
                name="w_2")
            b_2 = tf.get_variable(initializer=initializer,
                                  shape=(16),
                                  name="b_2")
            w_3 = tf.get_variable(initializer=initializer,
                                  shape=(16, 10),
                                  name="w_3")
            b_3 = tf.get_variable(initializer=initializer,
                                  shape=(10),
                                  name="b_3")

            net = self.pretrain
            #net = ne.layer_norm(self.pretrain, self.is_training)
            net = tf.nn.leaky_relu(tf.add(tf.matmul(net, w_2), b_2))
            #net = ne.layer_norm(net, self.is_training)

            self.cnn_clean_logits = tf.add(tf.matmul(net, w_3), b_3)
            #self.cnn_clean_logits = tf.add(tf.matmul(net, w_2), b_2)
            self.cnn_clean_prediction = tf.nn.softmax(self.cnn_clean_logits)

            correct_pred = tf.equal(tf.argmax(self.cnn_clean_prediction, 1),
                                    tf.argmax(self.label, 1))
            self.cnn_clean_accuracy = tf.reduce_mean(tf.cast(
                correct_pred, tf.float32),
                                                     name='accuracy')
Exemple #8
0
def do_extract(args):
  if args.seed is not None:
    np.random.seed(args.seed)

  FILENAMES = sorted(glob.glob(args.images))
  nimages = len(FILENAMES)
  if nimages == 0:
    print("Did not find any images in '{}".format(args.images))
    return 
  if args.verbose:
    print("Processing {} images".format(nimages))
    
  if args.model == 'NASNetAlarge':
    target_size, crop_size = 331, 331
  else:
    target_size, crop_size = 256, 224
  
  inputs = tf.placeholder(tf.float32, [None, crop_size, crop_size, 3])
  
  if args.model == 'NASNetAlarge':
    model = nets.NASNetAlarge(inputs)
    bs = 250
  elif args.model == 'VGG19':
    model = nets.VGG19(inputs)
    bs = 200
  elif args.model == 'MobileNet25':
    model = nets.MobileNet25(inputs)
    bs = 2500
  elif args.model == 'SqueezeNet':
    model = nets.SqueezeNet(inputs)
    bs = 1000
  elif args.model == 'ResNet50':
    model = nets.ResNet50(inputs)
    bs = 500
  else:
    raise ValueError # this should not happen
  
  model_pretrained = model.pretrained()

  if args.batchsize: 
    bs = args.batchsize # overwrite default batchsize

  nchunks = (nimages+bs-1)//bs
  
  PREDS = [] # beware: we store all activations (#images x #classes x sizeof(float))
  with tf.Session() as sess:
      sess.run(model_pretrained)
      
      for i in xrange(nchunks):
          images = []
          for filename in FILENAMES[i*bs:(i+1)*bs]:
              img = utils.load_img(filename, target_size=target_size, crop_size=crop_size)
              if args.blur:
                  img = apply_blur(img, args.blur)
              if args.noise:
                  img = apply_noise(img, args.noise)
              if args.dead:
                  img = apply_deadpixels(img, args.dead)
              if args.dark:
                  img = apply_scaletoblack(img, args.dark)
              if args.bright:
                  img = apply_scaletoblack(img, args.bright)
              if args.geometry:
                  img = apply_geometry(img, args.geometry)

              images.append( img.squeeze() )

          images = model.preprocess(np.asarray(images))

          preds = sess.run(model, {inputs: images})
          PREDS.extend(preds)

          if args.verbose:
            print('Processed chunk {} of {}'.format(i, nchunks))
            print('Most recent prediction:', utils.decode_predictions(preds, top=1)[0])

      PREDS = np.asarray(PREDS)
      if args.output:
          np.savetxt(args.output, PREDS.max(axis=1), fmt='%.12f')
      if args.rawoutput:
          np.savez_compressed(args.rawoutput, PREDS)
      if args.labeloutput:
          np.savetxt(args.labeloutput, PREDS.argmax(axis=1), fmt='%d')

  if args.verbose:
    print("Done.")
Exemple #9
0
    def __init__(self,
                 data,
                 label,
                 input_sigma,
                 is_training,
                 noised_pretrain=None,
                 noise=None):
        self.label = label
        self.data = tf.image.resize(data, [224, 224],
                                    method=tf.image.ResizeMethod.BILINEAR)
        #self.data = data
        self.input_sigma = input_sigma
        self.is_training = is_training
        #
        tf.debugging.set_log_device_placement(True)
        gpus = tf.config.experimental.list_logical_devices('GPU')
        #with tf.device(gpus[0].name):

        # cnn model
        self.pre_trained = nets.VGG19(self.data, is_training=True)
        self.pre_trained_cnn = tf.reshape(
            self.pre_trained.outputs()[-3],
            [self.pre_trained.get_shape().as_list()[0], 32, 32, 4])
        #import pdb; pdb.set_trace()

        self.noised_pre = noised_pretrain
        self.pre = self.noised_pre - noise

        initializer = tf.contrib.layers.variance_scaling_initializer(
            factor=1.0, mode='FAN_IN', uniform=True)
        initializer2 = tf.contrib.layers.variance_scaling_initializer(
            factor=1.0, mode='FAN_AVG', uniform=False)
        with tf.variable_scope('top_layers') as opt_scope_1:
            w_1 = tf.get_variable(initializer=initializer,
                                  shape=(4, 4, 4, 4),
                                  name="W_conv_1")
            b_1 = tf.get_variable(initializer=tf.zeros_initializer(),
                                  shape=(4),
                                  name="b_conv_1")
            w_3 = tf.get_variable(initializer=initializer,
                                  shape=(4, 4, 4, 4),
                                  name="W_conv3")
            b_3 = tf.get_variable(initializer=tf.zeros_initializer(),
                                  shape=(4),
                                  name="b_conv_3")

            net = self.noised_pre
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_1, strides=[1, 2, 2, 1], padding="SAME") +
                b_1)
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_3, strides=[1, 2, 2, 1], padding="SAME") +
                b_3)

        with tf.variable_scope('top_layers_2') as opt_scope_2:
            w_5 = tf.get_variable(initializer=initializer,
                                  shape=(4, 4, 4, 4),
                                  name="W_conv_5")
            b_5 = tf.get_variable(initializer=tf.zeros_initializer(),
                                  shape=(4),
                                  name="b_conv_5")
            w_6 = tf.get_variable(initializer=initializer2,
                                  shape=(64, 10),
                                  name="W_6")
            b_6 = tf.get_variable(initializer=tf.zeros_initializer(),
                                  shape=(10),
                                  name="b_6")

            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_5, strides=[1, 2, 2, 1], padding="SAME") +
                b_5)
            net = tf.reshape(net, [net.get_shape().as_list()[0], -1])
            self.cnn_logits = tf.add(tf.matmul(net, w_6), b_6)
            self.cnn_prediction = tf.nn.softmax(self.cnn_logits)

            correct_pred = tf.equal(tf.argmax(self.cnn_prediction, 1),
                                    tf.argmax(self.label, 1))
            self.cnn_accuracy = tf.reduce_mean(tf.cast(correct_pred,
                                                       tf.float32),
                                               name='accuracy')

        self.opt_scope_1 = opt_scope_1
        self.opt_scope_2 = opt_scope_2
        # recon
        with tf.variable_scope(opt_scope_1, reuse=True):
            net = self.pre
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_1, strides=[1, 2, 2, 1], padding="SAME") +
                b_1)
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_3, strides=[1, 2, 2, 1], padding="SAME") +
                b_3)

        with tf.variable_scope(opt_scope_2, reuse=True):
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_5, strides=[1, 2, 2, 1], padding="SAME") +
                b_5)
            net = tf.reshape(net, [net.get_shape().as_list()[0], -1])

            self.cnn_clean_logits = tf.add(tf.matmul(net, w_6), b_6)
            self.cnn_clean_prediction = tf.nn.softmax(self.cnn_clean_logits)

            correct_pred = tf.equal(tf.argmax(self.cnn_clean_prediction, 1),
                                    tf.argmax(self.label, 1))
            self.cnn_clean_accuracy = tf.reduce_mean(tf.cast(
                correct_pred, tf.float32),
                                                     name='accuracy')

        # bottom layers
        with tf.variable_scope(opt_scope_1, reuse=True):
            net = self.pre_trained_cnn + noise
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_1, strides=[1, 2, 2, 1], padding="SAME") +
                b_1)
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_3, strides=[1, 2, 2, 1], padding="SAME") +
                b_3)

        with tf.variable_scope(opt_scope_2, reuse=True):
            net = tf.nn.leaky_relu(
                tf.nn.conv2d(net, w_5, strides=[1, 2, 2, 1], padding="SAME") +
                b_5)
            net = tf.reshape(net, [net.get_shape().as_list()[0], -1])

            self.cnn_bott_logits = tf.add(tf.matmul(net, w_6), b_6)
Exemple #10
0
import tensornets as nets
import tensorflow as tf
import numpy as np
import helper

learning_rate = 0.00001
batch_size = 16
no_of_epochs = 100
n_classes = 10
no_of_test_splits = 100
image_size = 224

inputs = tf.placeholder(tf.float32, [None, image_size, image_size, 3])
outputs = tf.placeholder(tf.float32, [None, n_classes])

vgg = nets.VGG19(inputs, is_training=True, classes=n_classes)
model = tf.identity(vgg, name='logits')
cost = tf.losses.softmax_cross_entropy(outputs, vgg)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(model, 1), tf.argmax(outputs, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
vgg.print_outputs()
vgg.print_summary()


cifar = Cifar(batch_size=batch_size)
cifar.create_resized_test_set(dim=n_classes)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(vgg.pretrained())