def fcn_32s(input_dim, nb_classes=2):
inputs = Input(shape=(input_dim,input_dim,3))
vgg16 = VGG16(weights=None, include_top=False, input_tensor=inputs)
pretrain_model_path = "../weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
if not os.path.exists(pretrain_model_path):
raise RuntimeError("No pretrained model loaded.")
vgg16.load_weights(pretrain_model_path)
x = Conv2D(filters=nb_classes,
kernel_size=(1, 1))(vgg16.output)
x = Conv2DTranspose(filters=nb_classes,
kernel_size=(64, 64),
strides=(32, 32),
padding='same',
activation='sigmoid',
kernel_initializer=initializers.Constant(bilinear_upsample_weights(32, nb_classes)))(x)
model = Model(inputs=inputs, outputs=x)
for layer in model.layers[:15]:
layer.trainable = False
return model
python类VGG16的实例源码
def deprocess_image(x):
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + 1e-5)
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_dim_ordering() == 'th':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
# build the VGG16 network with ImageNet weights
def deprocess_image(x):
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + 1e-5)
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_data_format() == 'channels_first':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
# build the VGG16 network with ImageNet weights
def get_loss_net(pastiche_net_output, input_tensor=None):
'''
Instantiates a VGG net and applies its layers on top of the pastiche net's
output.
'''
loss_net = vgg16.VGG16(weights='imagenet', include_top=False,
input_tensor=input_tensor)
targets_dict = dict([(layer.name, layer.output) for layer in loss_net.layers])
i = pastiche_net_output
# We need to apply all layers to the output of the style net
outputs_dict = {}
for l in loss_net.layers[1:]: # Ignore the input layer
i = l(i)
outputs_dict[l.name] = i
return loss_net, outputs_dict, targets_dict
def get_learning_rate(cnn_type):
if cnn_type == 'VGG16' or cnn_type == 'VGG16_DROPOUT':
return 0.00004
elif cnn_type == 'VGG16_KERAS':
return 0.00005
elif cnn_type == 'VGG19':
return 0.00003
elif cnn_type == 'VGG19_KERAS':
return 0.00005
elif cnn_type == 'RESNET50':
return 0.00004
elif cnn_type == 'INCEPTION_V3':
return 0.00003
elif cnn_type == 'SQUEEZE_NET':
return 0.00003
elif cnn_type == 'DENSENET_161':
return 0.00003
elif cnn_type == 'DENSENET_121':
return 0.00001
else:
print('Error Unknown CNN type for learning rate!!')
exit()
return 0.00005
def get_random_state(cnn_type):
if cnn_type == 'VGG16' or cnn_type == 'VGG16_DROPOUT':
return 51
elif cnn_type == 'VGG19_KERAS':
return 52
elif cnn_type == 'RESNET50':
return 53
elif cnn_type == 'INCEPTION_V3':
return 54
elif cnn_type == 'VGG16_KERAS':
return 55
elif cnn_type == 'VGG19':
return 56
elif cnn_type == 'SQUEEZE_NET':
return 66
elif cnn_type == 'DENSENET_161':
return 71
elif cnn_type == 'DENSENET_121':
return 72
else:
print('Error Unknown CNN Type for random state!!')
exit()
return 0
def get_batch_size(cnn_type):
if cnn_type == 'VGG19' or cnn_type == 'VGG19_KERAS':
return 18
if cnn_type == 'VGG16_DROPOUT':
return 15
if cnn_type == 'VGG16' or cnn_type == 'VGG16_KERAS':
return 20
if cnn_type == 'RESNET50':
return 20
if cnn_type == 'INCEPTION_V3':
return 22
if cnn_type == 'SQUEEZE_NET':
return 40
if cnn_type == 'DENSENET_161':
return 8
if cnn_type == 'DENSENET_121':
return 25
return -1
def VGG_16_KERAS(classes_number, optim_name='Adam', learning_rate=-1):
from keras.layers.core import Dense, Dropout, Flatten
from keras.applications.vgg16 import VGG16
from keras.models import Model
base_model = VGG16(include_top=True, weights='imagenet')
x = base_model.layers[-2].output
del base_model.layers[-1:]
x = Dense(classes_number, activation='softmax', name='predictions')(x)
vgg16 = Model(input=base_model.input, output=x)
optim = get_optim('VGG16_KERAS', optim_name, learning_rate)
vgg16.compile(optimizer=optim, loss='categorical_crossentropy', metrics=['accuracy'])
# print(vgg16.summary())
return vgg16
# MIN: 1.00 Fast: 60 sec
def VGG_16_2_v2(classes_number, optim_name='Adam', learning_rate=-1):
from keras.layers.core import Dense, Dropout, Flatten
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Input
input_tensor = Input(shape=(3, 224, 224))
base_model = VGG16(input_tensor=input_tensor, include_top=False, weights='imagenet')
x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(classes_number, activation='softmax', name='predictions')(x)
vgg16 = Model(input=base_model.input, output=x)
optim = get_optim('VGG16_KERAS', optim_name, learning_rate)
vgg16.compile(optimizer=optim, loss='categorical_crossentropy', metrics=['accuracy'])
# print(vgg16.summary())
return vgg16
def feature_extractor(FLAGS, suffix = ""):
weights = FLAGS.weights if FLAGS.weights != "random" else None
if FLAGS.model == "vgg16":
from keras.applications.vgg16 import VGG16
feature_extractor = VGG16(weights = weights)
remove_last_layer(feature_extractor)
elif FLAGS.model == "vgg19":
from keras.applications.vgg19 import VGG19
feature_extractor = VGG19(weights = weights)
remove_last_layer(feature_extractor)
elif FLAGS.model == "resnet50":
from keras.applications.resnet50 import ResNet50
feature_extractor = ResNet50(weights = weights)
remove_last_layer(feature_extractor)
else:
raise NotImplementedError
feature_extractor.name = FLAGS.model + suffix
if FLAGS.regularizer == "l2":
add_regularizer(feature_extractor)
elif FLAGS.regularizer != "none":
raise NotImplementedError
return feature_extractor
def extract_vgg16_features(x):
from keras.preprocessing.image import img_to_array, array_to_img
from keras.applications.vgg16 import preprocess_input, VGG16
from keras.models import Model
# im_h = x.shape[1]
im_h = 224
model = VGG16(include_top=True, weights='imagenet', input_shape=(im_h, im_h, 3))
# if flatten:
# add_layer = Flatten()
# else:
# add_layer = GlobalMaxPool2D()
# feature_model = Model(model.input, add_layer(model.output))
feature_model = Model(model.input, model.get_layer('fc1').output)
print('extracting features...')
x = np.asarray([img_to_array(array_to_img(im, scale=False).resize((im_h,im_h))) for im in x])
x = preprocess_input(x) # data - 127. #data/255.#
features = feature_model.predict(x)
print('Features shape = ', features.shape)
return features
def deprocess_image(x):
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + 1e-5)
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_dim_ordering() == 'th':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
# build the VGG16 network with ImageNet weights
def build_model():
input_tensor = Input(shape=(150, 150, 3))
vgg16_model = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)
dense = Flatten()( \
Dense(2048, activation='relu')( \
BN()( \
vgg16_model.layers[-1].output ) ) )
result = Activation('sigmoid')(\
Activation('linear')( \
Dense(4096)(\
dense) ) )
model = Model(input=vgg16_model.input, output=result)
for i in range(len(model.layers)):
print(i, model.layers[i])
for layer in model.layers[:12]: # default 15
layer.trainable = False
model.compile(loss='binary_crossentropy', optimizer='adam')
return model
#build_model()
def deprocess_image(x):
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + 1e-5)
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_dim_ordering() == 'th':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
# build the VGG16 network with ImageNet weights
def get_features(url):
response = requests.get(url)
img = Image.open(BytesIO(response.content)).convert('RGB')
target_size = (224, 224)
model = VGG16(weights='imagenet', include_top=False, pooling='avg')
if img.size != target_size:
img = img.resize(target_size)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x).flatten()
return features.tolist()
def make_model(self):
# create the base pre-trained model
if self.model_architecture == 'vgg16':
from keras.applications.vgg16 import VGG16
self.base_model = VGG16(weights='imagenet', include_top=False)
elif self.model_architecture == 'resnet':
from keras.applications.resnet50 import ResNet50
self.base_model = ResNet50(weights='imagenet', include_top=False)
elif self.model_architecture == 'inception':
from keras.applications.inception_v3 import InceptionV3
self.base_model = InceptionV3(weights='imagenet', include_top=False)
else:
print 'Model architecture parameter unknown. Options are: vgg16, resnet, and inception'
rospy.signal_shutdown("Model architecture unknown.")
# now we add a new dense layer to the end of the network inplace of the old layers
x = self.base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
# add the outplut layer
predictions = Dense(len(self.categories.keys()), activation='softmax')(x)
# create new model composed of pre-trained network and new final layers
# if you want to change the input size, you can do this with the input parameter below
self.model = Model(input=self.base_model.input, output=predictions)
# now we go through and freeze all of the layers that were pretrained
for layer in self.base_model.layers:
layer.trainable = False
if self.verbose:
print 'compiling model ... '
start_time = time.time()
# in finetuning, these parameters can matter a lot, it is wise to observe
# how well your model is learning for this to work well
self.model.compile(optimizer=self.optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
if self.verbose:
end_time = time.time()
self.print_time(start_time,end_time,'compiling model')
def model(self, preprocessed, featurize):
# Model provided by Keras. All cotributions by Keras are provided subject to the
# MIT license located at https://github.com/fchollet/keras/blob/master/LICENSE
# and subject to the below additional copyrights and licenses.
#
# Copyright 2014 Oxford University
#
# Licensed under the Creative Commons Attribution License CC BY 4.0 ("License").
# You may obtain a copy of the License at
#
# https://creativecommons.org/licenses/by/4.0/
#
return vgg16.VGG16(input_tensor=preprocessed, weights="imagenet",
include_top=(not featurize))
def _testKerasModel(self, include_top):
return vgg16.VGG16(weights="imagenet", include_top=include_top)
def __init__(self, batchsize=1, img_height=224, img_width=224, FCN_CLASSES=21):
self.batchsize = batchsize
self.img_height = img_height
self.img_width = img_width
self.FCN_CLASSES = FCN_CLASSES
self.vgg16 = VGG16(include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=(3, self.img_height, self.img_width))
extract_cnn_vgg16_keras.py 文件源码
项目:flask-keras-cnn-image-retrieval
作者: willard-yuan
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def extract_feat(img_path):
# weights: 'imagenet'
# pooling: 'max' or 'avg'
# input_shape: (width, height, 3), width and height should >= 48
input_shape = (224, 224, 3)
model = VGG16(weights = 'imagenet', input_shape = (input_shape[0], input_shape[1], input_shape[2]), pooling = 'max', include_top = False)
img = image.load_img(img_path, target_size=(input_shape[0], input_shape[1]))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
feat = model.predict(img)
norm_feat = feat[0]/LA.norm(feat[0])
return norm_feat
def create_model(img_dim=(128, 128, 3)):
input_tensor = Input(shape=img_dim)
base_model = VGG16(include_top=False,
weights='imagenet',
input_shape=img_dim)
bn = BatchNormalization()(input_tensor)
x = base_model(bn)
x = Flatten()(x)
output = Dense(17, activation='sigmoid')(x)
model = Model(input_tensor, output)
return model
def _configure_network(self, build=True):
network = self.config['network']
type_, weights = network['type'].lower(), network.get('weights', None)
fine_tuning = " with pre-trained weights '{}'".format(weights) if weights else " without pre-training"
if 'vgg' in type_:
from keras.applications.vgg16 import VGG16
logging.info("Instantiating VGG model" + fine_tuning)
self.model = VGG16(weights=weights, input_shape=(3, 227, 227), include_top=True)
elif 'resnet' in type_:
from keras.applications.resnet50 import ResNet50
logging.info("Instantiating ResNet model" + fine_tuning)
input_layer = Input(shape=(3, 224, 224))
base_model = ResNet50(weights=weights, include_top=False, input_tensor=input_layer)
x = base_model.output
x = Flatten()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(3, activation='softmax')(x)
self.model = Model(input=base_model.input, output=predictions)
# for layer in base_model.layers:
# layer.trainable = fine_tuning
else:
if 'googlenet' in type_:
custom_objects = {"PoolHelper": PoolHelper, "LRN": LRN}
mod_str = 'GoogLeNet'
else:
custom_objects = {}
mod_str = 'custom'
from googlenet import create_googlenet
logging.info("Instantiating {} model".format(mod_str) + fine_tuning)
arch = network.get('arch', None)
if arch is None:
self.model = create_googlenet(network.get('no_classes', 3), network.get('no_features', 1024))
else:
self.model = model_from_json(open(arch).read(), custom_objects=custom_objects)
if weights:
print "Loading weights '{}'".format(weights)
self.model.load_weights(weights, by_name=True)
# Configure optimizer
if build:
opt_options = self.config['optimizer']
name, loss, params = opt_options['type'], opt_options['loss'], opt_options['params']
optimizer = OPTIMIZERS[name](**params)
self.model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
def main():
parser = argparser()
args = parser.parse_args()
image_path = args.image
layer_name = args.layer_name
feature_to_visualize = args.feature
visualize_mode = args.mode
model = vgg16.VGG16(weights = 'imagenet', include_top = True)
layer_dict = dict([(layer.name, layer) for layer in model.layers])
if not layer_dict.has_key(layer_name):
print('Wrong layer name')
sys.exit()
# Load data and preprocess
img = Image.open(image_path)
img = img.resize(224, 224)
img_array = np.array(img)
img_array = np.transpose(img_array, (2, 0, 1))
img_array = img_array[np.newaxis, :]
img_array = img_array.astype(np.float)
img_array = imagenet_utils.preprocess_input(img_array)
deconv = visualize(model, img_array,
layer_name, feature_to_visualize, visualize_mode)
# postprocess and save image
deconv = np.transpose(deconv, (1, 2, 0))
deconv = deconv - deconv.min()
deconv *= 1.0 / (deconv.max() + 1e-8)
deconv = deconv[:, :, ::-1]
uint8_deconv = (deconv * 255).astype(np.uint8)
img = Image.fromarray(uint8_deconv, 'RGB')
img.save('results/{}_{}_{}.png'.format(layer_name, feature_to_visualize, visualize_mode))
def vgg16():
base_model = VGG16(weights=None, include_top=False, input_shape = (224,224,3))
# Classification block
x = Flatten(name='flatten', input_shape=base_model.output_shape[1:])(base_model.output)
x = Dense(512, activation='relu', name='fc1')(x)
x = Dense(512, activation='relu', name='fc2')(x)
x = Dense(17, activation='softmax', name='predictions')(x)
model = Model(inputs=base_model.input, outputs=x)
return model
ArtisticStyleTransfer.py 文件源码
项目:Artistic-Style-Transfer-using-Keras-Tensorflow
作者: anujdutt9
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def style_loss(style, combination):
S = gram_matrix(style)
C = gram_matrix(combination)
channels = 3
size = ht * wd
return K.sum(K.square(S - C))/(4. *(channels ** 2)*(size ** 2))
# Feature Layers of VGG16 Trained Neural Network
def __init__(self, input_shape, classes, model_save_filepath):
self.model_save_filepath = model_save_filepath
self.neptune_organizer = None
self.old_session = K.get_session()
session = tf.Session('')
K.set_session(session)
K.set_learning_phase(1)
face_input = Input(batch_shape=(None,) + (input_shape))
pretrained_model = VGG16(input_tensor=face_input,
weights='imagenet',
include_top=False)
x = pretrained_model.get_layer('block4_pool').output
x = Flatten(name='flatten')(x)
x = Dense(256, activation='relu', name='fc1')(x)
x = Dense(256, activation='relu', name='fc2')(x)
output = Dense(classes, activation='softmax', name='predictions')(x)
self.facenet = Model(face_input, output)
self.facenet.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
self.facenet.summary()
self.datagen = ImageDataGenerator(rotation_range=5,
horizontal_flip=False,
vertical_flip=True)
def load_model(img_size = (100,100,3),mode = "VGG"):
'''softmax
Helper for loading model. Only VGG available now
'''
if mode == "VGG":
input_template = Input(batch_shape=(None,) + img_size)
model = VGG16(input_tensor=input_template,
weights='imagenet',
include_top=False)
else:
raise NotImplemented
return model
def VGGregionModel(inputshape):
input_tensor = Input(shape=inputshape) #(448, 448, 3))
vgg_model = VGG16(input_tensor=input_tensor, weights='imagenet', include_top=False )
# add region detection layers
x = vgg_model.output
x = Flatten()(x)
#x = Dense(256, activation='relu')(x)
#x = Dense(2048, activation='relu')(x)
#x = Dropout(0.5)(x)
x = Dense((cfgconst.side**2)*(cfgconst.classes+5)*cfgconst.bnum)(x)
model = Model(input=vgg_model.input, output=x)
#
print 'returned model:'
index = 0
for l in model.layers:
if index <= (18-8):
l.trainable = False
#print l.name+' '+str(l.input_shape)+' -> '+str(l.output_shape)+', trainable:'+str(l.trainable)
index = index + 1
return model
#
# pretrained
#model = VGGregionModel((448, 448, 3) )
def VGGregionModel(inputshape):
input_tensor = Input(shape=inputshape) #(448, 448, 3))
vgg_model = VGG16(input_tensor=input_tensor, weights='imagenet', include_top=False )
# add region detection layers
x = vgg_model.output
x = Flatten()(x)
#x = Dense(256, activation='relu')(x)
#x = Dense(2048, activation='relu')(x)
#x = Dropout(0.5)(x)
x = Dense((cfgconst.side**2)*(cfgconst.classes+5)*cfgconst.bnum)(x)
model = Model(input=vgg_model.input, output=x)
#
print 'returned model:'
index = 0
for l in model.layers:
if index <= (18-8):
l.trainable = False
#print l.name+' '+str(l.input_shape)+' -> '+str(l.output_shape)+', trainable:'+str(l.trainable)
index = index + 1
return model
#
# pretrained
#model = VGGregionModel((448, 448, 3) )
def _create(self):
base_model = KerasVGG16(weights='imagenet', include_top=False, input_tensor=self.get_input_tensor())
self.make_net_layers_non_trainable(base_model)
x = base_model.output
x = Flatten()(x)
x = Dense(4096, activation='elu', name='fc1')(x)
x = Dropout(0.6)(x)
x = Dense(self.noveltyDetectionLayerSize, activation='elu', name=self.noveltyDetectionLayerName)(x)
x = Dropout(0.6)(x)
predictions = Dense(len(config.classes), activation='softmax', name='predictions')(x)
self.model = Model(input=base_model.input, output=predictions)