Skip to content

Commit

Permalink
vggnet19 for extracting features
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjey committed Nov 12, 2016
1 parent c2d783c commit a35e23d
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions core/vggnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,52 @@
'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4']

class Vgg19(object):
def __init__(self, vgg_path):
self.vgg_path = vgg_path

def build_inputs(self):
self.images = tf.placeholder(tf.float32, [None, 224, 224, 3], 'images')

def build_params(self):
model = scipy.io.loadmat(self.vgg_path)
layers = model['layers'][0]
self.params = {}
with tf.variable_scope('encoder'):
for i, layer in enumerate(layers):
layer_name = layer[0][0][0][0]
layer_type = layer[0][0][1][0]
if layer_type == 'conv':
w = layer[0][0][2][0][0].transpose(1, 0, 2, 3)
b = layer[0][0][2][0][1].reshape(-1)
self.params[layer_name] = {}
self.params[layer_name]['w'] = tf.get_variable(layer_name+'/w', initializer=tf.constant(w))
self.params[layer_name]['b'] = tf.get_variable(layer_name+'/b',initializer=tf.constant(b))

def _conv(self, x, w, b):
return tf.nn.bias_add(tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME'), b)

def _relu(self, x):
return tf.nn.relu(x)

def _pool(self, x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

def build_model(self):
for i, layer in enumerate(vgg_layers):
layer_type = layer[:4]
if layer_type == 'conv':
if layer == 'conv1_1':
h = self.images
h = self._conv(h, self.params[layer]['w'], self.params[layer]['b'])
elif layer_type == 'relu':
h = self._relu(h)
elif layer_type == 'pool':
h = self._pool(h)
if layer == 'conv5_3':
self.features = tf.reshape(h, [-1, 196, 512])


def build(self):
self.build_inputs()
self.build_params()
self.build_model()
def __init__(self, vgg_path):
self.vgg_path = vgg_path

def build_inputs(self):
self.images = tf.placeholder(tf.float32, [None, 224, 224, 3], 'images')

def build_params(self):
model = scipy.io.loadmat(self.vgg_path)
layers = model['layers'][0]
self.params = {}
with tf.variable_scope('encoder'):
for i, layer in enumerate(layers):
layer_name = layer[0][0][0][0]
layer_type = layer[0][0][1][0]
if layer_type == 'conv':
w = layer[0][0][2][0][0].transpose(1, 0, 2, 3)
b = layer[0][0][2][0][1].reshape(-1)
self.params[layer_name] = {}
self.params[layer_name]['w'] = tf.get_variable(layer_name+'/w', initializer=tf.constant(w))
self.params[layer_name]['b'] = tf.get_variable(layer_name+'/b',initializer=tf.constant(b))

def _conv(self, x, w, b):
return tf.nn.bias_add(tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME'), b)

def _relu(self, x):
return tf.nn.relu(x)

def _pool(self, x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

def build_model(self):
for i, layer in enumerate(vgg_layers):
layer_type = layer[:4]
if layer_type == 'conv':
if layer == 'conv1_1':
h = self.images
h = self._conv(h, self.params[layer]['w'], self.params[layer]['b'])
elif layer_type == 'relu':
h = self._relu(h)
elif layer_type == 'pool':
h = self._pool(h)
if layer == 'conv5_3':
self.features = tf.reshape(h, [-1, 196, 512])

def build(self):
self.build_inputs()
self.build_params()
self.build_model()

0 comments on commit a35e23d

Please sign in to comment.