Skip to content

Commit

Permalink
complete version
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkaihuappt committed Jan 16, 2018
1 parent 8972d08 commit a9dc41d
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 406 deletions.
39 changes: 22 additions & 17 deletions ResNet.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
"""
A Trainable ResNet Class is defined in this file
A Trainable ResNet-50 Class is defined in this file
Author: Kaihua Tang
"""
import math
import numpy as np
import tensorflow as tf
from functools import reduce

VGG_MEAN = [104.7546, 124.328, 167.1754]
_BATCH_NORM_DECAY = 0.99
_BATCH_NORM_EPSILON = 1e-12

class ResNet:
# some properties
"""
Initialize function
"""
def __init__(self, ResNet_npy_path=None, trainable=True, open_tensorboard=False, dropout=0.8):
def __init__(self, ResNet_npy_path=None, trainable=True, open_tensorboard=False):
"""
Initialize function
ResNet_npy_path: If path is not none, loading the model. Otherwise, initialize all parameters at random.
open_tensorboard: Is Open Tensorboard or not.
"""
if ResNet_npy_path is not None:
self.data_dict = np.load(ResNet_npy_path, encoding='latin1').item()
else:
Expand All @@ -25,26 +22,29 @@ def __init__(self, ResNet_npy_path=None, trainable=True, open_tensorboard=False,
self.var_dict = {}
self.trainable = trainable
self.open_tensorboard = open_tensorboard
self.dropout = dropout
self.is_training = True

def set_is_training(self, isTrain):
"""
Set is training bool.
"""
self.is_training = isTrain

def build(self, rgb, label_num, train_mode=None, last_layer_type = "softmax"):
def build(self, rgb, label_num, last_layer_type = "softmax"):
"""
load variable from npy to build the Resnet or Generate a new one
:param rgb: rgb image [batch, height, width, 3] values scaled [0, 1]
:param train_mode: a bool tensor, usually a placeholder: if True, dropout will be turned on
"""
# Preprocessing: Turning RGB to BGR - Mean.
BGR_MEAN = [104.7546, 124.328, 167.1754]
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=rgb)
assert red.get_shape().as_list()[1:] == [224, 224, 1]
assert green.get_shape().as_list()[1:] == [224, 224, 1]
assert blue.get_shape().as_list()[1:] == [224, 224, 1]
bgr = tf.concat(axis=3, values=[
blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2],
blue - BGR_MEAN[0],
green - BGR_MEAN[1],
red - BGR_MEAN[2],
])
print(bgr.get_shape().as_list())
assert bgr.get_shape().as_list()[1:] == [224, 224, 3]
Expand Down Expand Up @@ -114,6 +114,11 @@ def res_block_3_layers(self, bottom, channel_list, name, change_dimension = Fals
return relu

def batch_norm(self, inputsTensor):
"""
Batchnorm
"""
_BATCH_NORM_DECAY = 0.99
_BATCH_NORM_EPSILON = 1e-12
return tf.layers.batch_normalization(inputs=inputsTensor, axis = 3, momentum=_BATCH_NORM_DECAY, epsilon=_BATCH_NORM_EPSILON, center=True, scale=True, training=self.is_training)

def avg_pool(self, bottom, kernal_size = 2, stride = 2, name = "avg"):
Expand Down Expand Up @@ -241,7 +246,7 @@ def get_var(self, initial_value, name, idx, var_name):
return var


def save_npy(self, sess, npy_path="./Resnet-save.npy"):
def save_npy(self, sess, npy_path="./model/Resnet-save.npy"):
"""
Save this model into a npy file
"""
Expand Down
File renamed without changes.
69 changes: 69 additions & 0 deletions TestResNet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Extract ResNet feature
Author: Kaihua Tang
"""

import math
import time
import tensorflow as tf
import ResNet as resnet
import numpy as np
import scipy.io as scio
from scipy import misc
from utils import *

# image size
WIDTH = 224
HEIGHT = 224
CHANNELS = 3
#Number of output labels
LABELSNUM = 1200
#"Path of Label.npy"
label_path = "./label/label.npy"
#"Path of image file names"
image_name_path = "./label/name.npy"
# image path
parentPath = "F:\\CACD2000_Crop\\"


def CalculateFeature():
"""
EXtract ResNet Feature by trained model
model_path: The model we use
feature_path: The path to save feature
"""
model_path = "./model/11.npy"
feature_path = "./resnet_feature.mat"

#Lists that store name of image and its label
testNameList = np.load(image_name_path)
testLabelList = np.load(label_path)

#num of total training image
num_test_image = testLabelList.shape[0]
#load all image data
allImageData = load_all_image(testNameList, HEIGHT, WIDTH, CHANNELS, parentPath)
#container for ResNet Feature
res_feature = np.zeros((num_test_image, 2048))

with tf.Session() as sess:
images = tf.placeholder(tf.float32, shape = [None, WIDTH, HEIGHT, CHANNELS])

# build resnet model
resnet_model = resnet.ResNet(ResNet_npy_path = model_path)
resnet_model.build(images, LABELSNUM, "softmax")

sess.run(tf.global_variables_initializer())
resnet_model.set_is_training(False)

for i in range(num_test_image):
if(i%1000 == 0):
print(i)
(minibatch_X, minibatch_Y) = get_minibatch([i], testLabelList, HEIGHT, WIDTH, CHANNELS, LABELSNUM, allImageData, True)
pool2 = sess.run([resnet_model.pool2], feed_dict={images: minibatch_X})
res_feature[i][:] = pool2[0][:]

scio.savemat(feature_path,{'feature' : res_feature})

if __name__ == '__main__':
CalculateFeature()
Loading

0 comments on commit a9dc41d

Please sign in to comment.