Beispiel #1
0
           input_shape=(target_size, target_size, 3)),
     "vgg19":
     VGG19(weights="imagenet",
           include_top=False,
           input_shape=(target_size, target_size, 3)),
     "inceptionv3":
     InceptionV3(weights="imagenet",
                 include_top=False,
                 input_shape=(target_size, target_size, 3)),
     "xception":
     Xception(weights="imagenet",
              include_top=False,
              input_shape=(target_size, target_size, 3)),
     "resnet50":
     ResNet50(weights="imagenet",
              include_top=False,
              input_shape=(target_size, target_size, 3)),
 }
 freezing_layers = {
     "vgg16": ("block5_conv1", 15, 16),
     "vgg19": ("block5_conv1", 17, 16),
     "inceptionv3": ("conv2d_65", 197, 32),
     "xception": ("add_10", 105, 32),
     "resnet50": ("add_8", 89, 32),
 }
 for i in range(3):
     for n, m in models.items():
         try:
             # if not n in ["inceptionv3"]:
             #     print("pass")
             #     continue
Beispiel #2
0
def make_plain_model(num_classes):
    model = ResNet50(classes=num_classes, weights=None)
    model.compile(optimizer='sgd', loss='categorical_crossentropy')
    return model
Beispiel #3
0
import os

os.environ["CUDA_VISIBLE_DEVICES"] = ""

from keras.applications import ResNet50
import cv2
from PIL import Image
import numpy as np
from math import sqrt

model = ResNet50(include_top=False, weights='imagenet', pooling='avg')


def dot_product(v1, v2):
    return sum(a * b for a, b in zip(v1, v2))


def magnitude(vector):
    return sqrt(dot_product(vector, vector))


def similarity(v1, v2):
    """
    计算余弦相似度
    """
    return dot_product(v1, v2) / (magnitude(v1) * magnitude(v2) + .00000000001)


def template_select(image1, image2):
    image1 = np.expand_dims(np.array(image1.convert('RGB')), axis=0)
    image2 = np.expand_dims(np.array(image2.convert('RGB')), axis=0)
def SDPN(summary=False):
    """
	Create and return Semantic-aware Dense Prediction Network.

	Parameters
	----------
	summary : bool
		If True, network summary is printed to stout.

	Returns
	-------
	model : keras Model
		Model of SDPN

	"""
    input_coords = Input(shape=(4, ))
    input_crop = Input(shape=(224, 224, 3))

    # extract feature from image crop
    resnet = ResNet50(include_top=False, weights='imagenet')
    for layer in resnet.layers:  # set resnet as non-trainable
        layer.trainable = False

    crop_encoded = resnet(input_crop)  # shape of `crop_encoded` is 2018x1x1
    crop_encoded = Reshape(target_shape=(2048, ))(crop_encoded)

    # encode input coordinates
    h = Dense(256)(input_coords)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)
    h = Dense(256, kernel_constraint=maxnorm(3))(h)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)
    h = Dense(256, kernel_constraint=maxnorm(3))(h)
    h = ELU()(h)

    # merge feature vectors from crop and coords
    merged = concatenate([crop_encoded, h])

    # decoding into output distance
    h = Dense(1024)(merged)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)
    h = Dense(512, kernel_constraint=maxnorm(3))(h)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)
    h = Dense(256, kernel_constraint=maxnorm(3))(h)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)
    h = Dense(128, kernel_constraint=maxnorm(3))(h)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)
    h = Dense(64, kernel_constraint=maxnorm(3))(h)
    h = ELU()(h)
    h = Dropout(rate=0.4)(h)

    output_dist = Dense(1, activation='relu')(h)  #relu to output distance >= 0

    model = Model(inputs=[input_coords, input_crop], outputs=output_dist)

    if summary:
        model.summary()

    return model
def load_model():
    global model
    print(" * Loading pre-trained model...")
    model = ResNet50(weights="imagenet")
    print(" * Loading end")
Beispiel #6
0
from flask_ngrok import run_with_ngrok
import cv2
from keras.models import load_model
import numpy as np
from keras.applications import ResNet50
from keras.optimizers import Adam
from keras.layers import Dense, Flatten, Input, Convolution2D, Dropout, LSTM, TimeDistributed, Embedding, Bidirectional, Activation, RepeatVector, Concatenate
from keras.models import Sequential, Model
from keras.utils import np_utils
from keras.preprocessing import image, sequence
import cv2
from keras.preprocessing.sequence import pad_sequences
from tqdm import tqdm

resnet = ResNet50(include_top=False,
                  weights='imagenet',
                  input_shape=(224, 224, 3),
                  pooling='avg')

print("=" * 150)
print("RESNET MODEL LOADED")

vocab = np.load('vocab.npy', allow_pickle=True)

vocab = vocab.item()

inv_vocab = {v: k for k, v in vocab.items()}

print("+" * 50)
print("vocabulary loaded")

embedding_size = 128
cond_classes = 5 + 4 + 9

#Trained top
top_model = Sequential()
top_model.add(Flatten(input_shape=(1, 2, 2048)))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.50))
top_model.add(Dense(cond_classes, activation='sigmoid'))
top_model.add(Lambda(split_mammals, name='cond_layer'))
top_model.add(Lambda(convert_cond_probabilities))
top_model.load_weights(os.path.join(config['weight_path'],
                                    'resnet_bottleneck_weights.h5'),
                       by_name=False)

conv_base = ResNet50(include_top=False,
                     weights='imagenet',
                     input_shape=(270, 480, 3))  #Pretrained base
conv_base.trainable = True  #Unfreeze convolutional base to finetune

model = Sequential()
model.add(conv_base)
model.add(top_model)

#Select trainable layers
layer_names = []
for layer in conv_base.layers:
    layer_names.append(layer.name)

set_trainable = False
for layer in conv_base.layers:
    if layer.name == 'block5_conv1':
    trainY = data['arr_1']
    testX = data['arr_2']
    testY = data['arr_3']

print('loaded data')
# initialize an our data augmenter as an "empty" image data generator
#aug = ImageDataGenerator()

# In[2]:

img_height, img_width = 128, 128
num_classes = 2
input_shape = (img_height, img_width, 3)
base_model = ResNet50(
    weights='imagenet',
    include_top=False,
    input_shape=(img_height, img_width,
                 3))  #imports the mobilenet model and discards the

# Freeze the layers except the last 4 layers
for layer in base_model.layers[:-3]:
    layer.trainable = False

# Check the trainable status of the individual layers
#for layer in base_model.layers:
#    print(layer, layer.trainable)

# ## Added three dense layers and the last layer is having 7 classes

# In[12]:
Beispiel #9
0
from keras.applications import ResNet50

model = ResNet50(include_top=True, weights="imagenet")
print(model.summary())
def empty_resnet():
    K.set_image_data_format('channels_last')
    base_model = ResNet50(weights=None,include_top=False,input_shape=(224,224,3))
    features = GlobalAveragePooling2D()(base_model.output)
    model = Model(inputs=base_model.input, outputs=features)
    return model
Beispiel #11
0
from keras.optimizers import Adam

# 1. data
(x_train, y_train),(x_test, y_test) = cifar10.load_data()

print(f"x_train.shape : {x_train.shape}")
print(f"x_test.shape : {x_test.shape}")
print(f"y_train.shape : {y_train.shape}")
print(f"y_test.shape : {y_test.shape}")

x_train = x_train.reshape(50000,32,32,3).astype('float32')/255.0
x_test = x_test.reshape(10000,32,32,3).astype('float32')/255.0

# 2. model
# vgg16 = ResNet152V2(weights='imagenet' , include_top=False, input_shape=(32,32,3))
vgg16 = ResNet50(weights='imagenet' , include_top=False,input_shape=(32,32,3))
vgg16.summary()

# vgg16 = Dense(10,activation='softmax')(vgg16)


model = Sequential()
model.add(vgg16)

model.add(Flatten())

model.add(Dense(256))
# model.add(BatchNormalization())
model.add(Dropout(0.1))
model.add(Activation('relu'))
Beispiel #12
0
    def on_epoch_end(self, epoch, logs={}):
        x, y = self.test_data
        loss, acc = self.model.evaluate(x, y, verbose=0)
        print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))


IMAGE_SIZE = [
    224, 224
]  # we will keep the image size as (64,64). You can increase the size for better results.

# loading the weights of VGG16 without the top layer. These weights are trained on Imagenet dataset.
#vgg = VGG16(input_shape = IMAGE_SIZE + [3], weights = 'imagenet', include_top = False)  # input_shape = (64,64,3) as required by VGG

#base_model = InceptionResNetV2(input_shape = IMAGE_SIZE + [3],include_top=False )

base_model = ResNet50(input_shape=IMAGE_SIZE + [3], include_top=False)

# def save_array(fname, arr):
#     c=bcolz.carray(arr, rootdir=fname, mode='w')
#     c.flush()

# def load_array(fname):
#     return bcolz.open(fname)[:]

#path = "/home/ws2/Documents/cropdata/"
path = "/home/ws2/Documents/kinectdata/seg1/"
#path ="/home/ws2/Documents/kinectdata/seg2/"
#path ="/home/ws2/Documents/kinectdata/seg3/"
#path ="/home/ws2/Documents/kinectdata/seg4/"
# path = "data/dogscats/"
model_path = path + 'models/'
Beispiel #13
0
def load_model():
    global model
    model = ResNet50(weights="imagenet")
from keras import applications

from keras.applications import Xception
from keras.applications import InceptionV3,ResNet50
from keras.layers import Input

a = Input(shape=(300,200,3))
print(type(a))
net = ResNet50(input_tensor=a)
from keras.utils import plot_model
plot_model(net,show_shapes=True,to_file='xception.png')
# print(dir(applications))
Beispiel #15
0
# initialize the validation/testing data augmentation object (which
# we'll be adding mean subtraction to)
valAug = ImageDataGenerator()

# define the ImageNet mean subtraction (in RGB order) and set the
# the mean subtraction value for each of the data augmentation
# objects
mean = np.array([123.68, 116.779, 103.939], dtype="float32")
trainAug.mean = mean
valAug.mean = mean

# load the ResNet-50 network, ensuring the head FC layer sets are left
# off
baseModel = ResNet50(weights="imagenet",
                     include_top=False,
                     input_tensor=Input(shape=(224, 224, 3)))

# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation="softmax")(headModel)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
Beispiel #16
0
# from tensorflow.python.keras.applications.vgg16 import VGG16

from keras.applications import ResNet50
# conv_base = Xception(weights='imagenet', include_top=False,
#                      input_shape=(150,150,3))

conv_base = ResNet50()  # (224, 224, 3)
conv_base.summary()

# conv_base.summary()

from keras import models, layers

model = models.Sequential()
model.add(conv_base)
# model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
Beispiel #17
0
    target_size=(trgt_sz, trgt_sz),
    batch_size=batch_size,
    class_mode='binary')  # changed from "categorical" to "binary"

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    shuffle=False,
    target_size=(trgt_sz, trgt_sz),
    batch_size=batch_size,
    class_mode='binary')  # changed from "categorical" to "binary"

#######################################################################
## Model ##############################################################
#######################################################################

base_model = ResNet50(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.2)(x)  # new dropout layer
x = Dense(1024, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(
    x)  # changing to detect only two types

model = Model(inputs=base_model.input, output=predictions)

########################################################################

## Freez the base model #################

for layer in base_model.layers:
    layer.trainable = False  # all layers are  not trainable
Beispiel #18
0
def train_categorical(gpus, experiment_path, train_df_path, train_image_path, val_df_path, val_image_path, y_name, y_classes, augment_number,
                      model_type, model_save_name, epoch_number, learning_rate, decay, regularization, batch_size=32, img_height=224, img_width=224):
    
    checkpoint = ModelCheckpoint(os.path.join(experiment_path, model_save_name+'.h5'), monitor='val_loss', verbose=1, save_best_only=False, save_weights_only=False, mode='auto', period=25)
    callbacks = [checkpoint]
    
    #Session variables
    traindf = pd.read_csv(train_df_path)
    valdf = pd.read_csv(val_df_path)
    
    freq_dict = get_freq_dict(traindf, y_name, y_classes)
    num_labels = len(y_classes)
    
    numTrainImgs = numImgs(traindf, y_name, y_classes)
    numValImgs = numImgs(valdf, y_name, y_classes)
    
    class_weight = list(map(lambda x: 1/freq_dict[x], y_classes))
    
    #Generators
    train_generator = make_batch(generator='categorical',
                                 dataframe=traindf,
                                 image_path=train_image_path,
                                 y_name=y_name,
                                 y_classes=y_classes,
                                 batch_size=batch_size,
                                 augment_number=augment_number,
                                 height=img_height,
                                 width=img_width,
                                 forTrain=True
                                 )
    
    val_generator = make_batch(generator='categorical',
                               dataframe=valdf,
                               image_path=val_image_path,
                               y_name=y_name,
                               y_classes=y_classes,
                               batch_size=batch_size,
                               height=img_height,
                               width=img_width,
                               forTrain=False
                               )
    
    #Model
    if model_type == 'vgg16':
    
        net_input = Input(shape=(img_height, img_width, 3))
        conv_base = VGG16(
            weights='imagenet',
            include_top=False,
            input_shape=(img_height, img_width, 3))(net_input)
        conv_base.trainable = False
        flatten1 = Flatten()(conv_base)
        dropout1 = Dropout(0.5)(flatten1)
        dense1 = Dense(
            num_labels,
            activation='softmax',
            kernel_regularizer=regularizers.l2(regularization))(dropout1) 
        with tf.device('/cpu:0'):
            model = Model(inputs=net_input, outputs=dense1)
    
    elif model_type == 'resnet':
        
        net_input = Input(shape=(img_height, img_width, 3))
        conv_base = ResNet50(
            weights='imagenet',
            include_top=False,
            input_shape=(img_height, img_width, 3))(net_input)
        conv_base.trainable = False
        flatten1 = Flatten()(conv_base)
        dropout1 = Dropout(0.5)(flatten1)
        dense1 = Dense(
            num_labels,
            activation='softmax',
            kernel_regularizer=regularizers.l2(regularization))(dropout1)
        with tf.device('/cpu:0'):
            model = Model(inputs=net_input, outputs=dense1)
        
    elif model_type == 'inception':
        
        net_input = Input(shape=(img_height, img_width, 3))
        conv_base = InceptionV3(
            weights='imagenet',
            include_top=False,
            input_shape=(img_height, img_width, 3))(net_input)
        conv_base.trainable = False
        flatten1 = Flatten()(conv_base)
        dropout1 = Dropout(0.5)(flatten1)
        dense1 = Dense(
            num_labels,
            activation='softmax',
            kernel_regularizer=regularizers.l2(regularization))(dropout1)
        with tf.device('/cpu:0'):
            model = Model(inputs=net_input, outputs=dense1)
    
    else:
        print('\nError in train_categorical: invalid model type\n')
    
    multi_model = multi_gpu_model(model, gpus=gpus)
    model.summary()
        
    #Train
    multi_model.compile(optimizers.RMSprop(lr=learning_rate, decay=decay), loss='categorical_crossentropy', metrics=['accuracy'])
    
    history = multi_model.fit_generator(generator=train_generator,
                                        steps_per_epoch=numTrainImgs//batch_size,
                                        validation_data=val_generator,
                                        validation_steps=numValImgs,
                                        class_weight=class_weight,
                                        epochs=epoch_number,
                                        verbose=1,
                                        callbacks=callbacks
                                        )
    
    #Save model parameters
    model.save(os.path.join(experiment_path, model_save_name+'.h5'))
    
    #Save model index meanings
    with open(os.path.join(experiment_path, model_save_name+':indices.txt'), mode='w', encoding='utf-8') as filewrite:
        csv_writer = csv.writer(filewrite, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(['class', 'frequency', 'index'])
        for i in range(len(y_classes)):
            csv_writer.writerow([y_classes[i], freq_dict[y_classes[i]], i])
    
    #Plots
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs = range(len(acc))
    
    plt.figure()
    plt.plot(epochs, acc, 'bo', label='Training accuracy')
    plt.plot(epochs, val_acc, 'r*', label='Validation accuracy')
    plt.title('Training and validation accuracy')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend()
    plt.savefig(os.path.join(experiment_path, 'accuracy.png'))
    
    plt.figure()
    plt.plot(epochs, loss, 'bo', label='Training loss')
    plt.plot(epochs, val_loss, 'r*', label='Validation loss')
    plt.title('Training and validation loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend()
    plt.savefig(os.path.join(experiment_path, 'loss.png'))
from keras.applications import ResNet50
from keras.models import Model
import os

class_weight = {0: 0.05, 1: 0.95}
training_to_all_data_ratio = 0.9
nb_cross_val = 1
nb_epoch = 1000
batch_size = 32

dir_name = os.path.dirname(__file__)
dir_name = os.path.dirname(dir_name)

image_path = os.path.join(dir_name, 'data/input')

backbone_model = ResNet50(weights='imagenet')
backbone_model = Model(inputs=backbone_model.input,
                       outputs=backbone_model.get_layer(index=-2).output)

data = DataSet()
data.model = backbone_model
data.extract_features(image_path,
                      option='fixed frame amount',
                      number_of_frames=50)
data.read_risk_data("LCTable.csv")
data.convert_risk_to_one_hot(risk_threshold=0.05)

Data = data.video_features
label = data.risk_one_hot

model = Models(nb_epoch=nb_epoch,
Beispiel #20
0
def load_model():
    # load the pre-trained Keras model (here we are using a model
    # pre-trained on ImageNet and provided by Keras, but you can
    # substitute in your own networks just as easily)
    global model
    model = ResNet50(weights="imagenet")
def classify_process():
    # load the pre-trained Keras model (here we are using a model
    # pre-trained on ImageNet and provided by Keras, but you can
    # substitute in your own networks just as easily)
    print("* Loading model...")
    model = ResNet50(weights="imagenet")
    print("* Model loaded")

    # continually pool for new images to classify
    while True:
        # attempt to grab a batch of images from the database, then
        # initialize the image IDs and batch of images themselves
        queue = db.lrange(IMAGE_QUEUE, 0, BATCH_SIZE - 1)
        imageIDs = []
        batch = None

        # loop over the queue
        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = base64_decode_image(
                q["image"], IMAGE_DTYPE,
                (1, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANS))

            # check to see if the batch list is None
            if batch is None:
                batch = image

            # otherwise, stack the data
            else:
                batch = np.vstack([batch, image])

            # update the list of image IDs
            imageIDs.append(q["id"])

        # check to see if we need to process the batch
        if len(imageIDs) > 0:
            # classify the batch
            print("* Batch size: {}".format(batch.shape))
            preds = model.predict(batch)
            results = imagenet_utils.decode_predictions(preds)

            # loop over the image IDs and their corresponding set of
            # results from our model
            for (imageID, resultSet) in zip(imageIDs, results):
                # initialize the list of output predictions
                output = []

                # loop over the results and add them to the list of
                # output predictions
                for (imagenetID, label, prob) in resultSet:
                    r = {"label": label, "probability": float(prob)}
                    output.append(r)

                # store the output predictions in the database, using
                # the image ID as the key so we can fetch the results
                db.set(imageID, json.dumps(output))

            # remove the set of images from our queue
            db.ltrim(IMAGE_QUEUE, len(imageIDs), -1)

        # sleep for a small amount
        time.sleep(SERVER_SLEEP)
def load_model():
    global graph
    global model
    model = ResNet50(weights="imagenet")
    graph = tf.get_default_graph()
Beispiel #23
0
test_STEPS = np.ceil(112163.0 / hps.batch_size)

CHANNEL = 3 if hps.rgb is True else 1

if hps.debug:
    hps.epochs = 2
    hps.eval_samples_num = 3400
    STEPS = 500 # debug的时候steps可以设置小一点
    valid_STEPS= 50
    print("\n---------debug mode!---------\n")
    
print("EPOCHS = %d \nSTEPS = %d \nvalid_STEPS = %d " % (hps.epochs, STEPS, valid_STEPS))


if hps.use_pretrain_model:
    pretrain_model = ResNet50(weights="imagenet", include_top=False)
    x = pretrain_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predict = Dense(NCATS, activation='softmax')(x)
    single_model = Model(inputs=pretrain_model.input, outputs=predict)
else:
    single_model = ResNet50(input_shape=(hps.image_size, hps.image_size, CHANNEL), weights=None, classes=NCATS)


print("\n ---------------Using %d GPUs----------------\n" % hps.gpus)
model = multi_gpu_model(single_model, gpus=hps.gpus)

model.compile(optimizer=Adam(lr=0.002), loss='categorical_crossentropy',
              metrics=[categorical_crossentropy, categorical_accuracy, top_3_accuracy])
print(model.summary())
Beispiel #24
0
    return imgs_resize, lbls, lbls_str


#%%
train_images, train_labels, train_labels_str = resnet_transfer_preparedata(
    no=0)
test_images, test_labels, test_labels_str = resnet_transfer_preparedata('test',
                                                                        no=0)

train_labels_cat = to_categorical(train_labels, num_classes=10)
test_labels_cat = to_categorical(test_labels, num_classes=10)

#%% prepare new model

base_model = ResNet50(include_top=False,
                      weights='imagenet',
                      input_shape=[38, 38, 3])

#%% freeze: set base_model layers to non trainable , freeze early stages,
# assuming that these will do extraction of basic features, and retrain later ones
# going with the assumption that this set is very differnt from the orignal set

for l in base_model.layers[:10]:
    l.trainable = False

#%%
base_model.summary()

#%%

added_layers = base_model.output
Beispiel #25
0
def model_choose(depth, width, model_name="ResNet50", weights=None):
    if model_name == "ResNet50":
        base = ResNet50(include_top=False,
                        weights='imagenet',
                        input_shape=(224, 224, 3),
                        pooling="avg")
        prediction = Dense(
            units=101,
            kernel_initializer="he_normal",
            use_bias=False,
            activation="softmax",
            name="pred_age"
        )(
            base.output
        )  #he_normal draws samples from normal distribution of weights for intialization of layers
        model = Model(inputs=base.input, outputs=prediction, name="ResNet-50")
        return model

    elif model_name == "AlexNet":
        input_shape = (200, 200, 3)
        num_classes = 101
        input_layer = Input(shape=input_shape)

        #First layer of model
        x = ZeroPadding2D(padding=(0, 0))(input_layer)
        x = Conv2D(filters=96,
                   kernel_size=(11, 11),
                   strides=(4, 4),
                   padding="valid",
                   kernel_regularizer=l2(0.0),
                   kernel_initializer="he_normal",
                   name="conv1")(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = MaxPool2D(pool_size=(3, 3),
                      strides=(2, 2),
                      padding="valid",
                      name="maxpool1")(x)

        #Second layer of model
        x = Conv2D(filters=256,
                   kernel_size=(5, 5),
                   strides=(1, 1),
                   padding="same",
                   kernel_regularizer=l2(0.0),
                   kernel_initializer="he_normal",
                   name="conv2")(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = MaxPool2D(pool_size=(3, 3),
                      strides=(2, 2),
                      padding="valid",
                      name="maxpool2")(x)

        #Third layer of model
        x = Conv2D(filters=384,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   padding="same",
                   kernel_regularizer=l2(0.0),
                   kernel_initializer="he_normal",
                   name="conv3")(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)

        #Fourth layer of model
        x = Conv2D(filters=384,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   padding="same",
                   kernel_regularizer=l2(0.0),
                   kernel_initializer="he_normal",
                   name="conv4")(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)

        #Fifth layer of model
        x = Conv2D(filters=256,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   padding="same",
                   kernel_regularizer=l2(0.0),
                   kernel_initializer="he_normal",
                   name="conv5")(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = MaxPool2D(pool_size=(3, 3),
                      strides=(2, 2),
                      padding="valid",
                      name="maxpool3")(x)

        #Sixth layer of model
        x = Flatten()(x)
        #x = Dropout(0.5)
        x = Dense(units=4096)(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)

        #Seventh layer of model
        #x = Dropout(0.5)
        x = Dense(units=4096)(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)

        #Eighth layer of model
        x = Dense(units=num_classes)(x)
        x = BatchNormalization()(x)
        x = Activation("softmax")(x)

        if weights is not None:
            x.load_weights(weights)
        model = Model(input_layer, x, name="AlexNet_8")
        return model

    else:
        print("Wide ResNet model")
        return buildmodel(64, depth, width)
Beispiel #26
0
def load_models():
    # Set up global variables that will be used throughout the "routes". This will be done on server start up
    global model
    global model_custom
    global face_cascade
    global dog_names
    global graph

    # Load the models as well as the face classifier
    face_cascade = cv2.CascadeClassifier(
        "dogclassifierapp/static/haarcascade_frontalface_alt.xml")
    model = ResNet50(weights='imagenet', include_top=False)
    model_custom = load_model("dogclassifierapp/static/ResNetDog.h5")
    # Prepare the dog_names to be retrieved after predicitions are made to provide the predicted name
    dog_names = [
        'ages/train/001.Affenpinscher', 'ages/train/002.Afghan_hound',
        'ages/train/003.Airedale_terrier', 'ages/train/004.Akita',
        'ages/train/005.Alaskan_malamute',
        'ages/train/006.American_eskimo_dog',
        'ages/train/007.American_foxhound',
        'ages/train/008.American_staffordshire_terrier',
        'ages/train/009.American_water_spaniel',
        'ages/train/010.Anatolian_shepherd_dog',
        'ages/train/011.Australian_cattle_dog',
        'ages/train/012.Australian_shepherd',
        'ages/train/013.Australian_terrier', 'ages/train/014.Basenji',
        'ages/train/015.Basset_hound', 'ages/train/016.Beagle',
        'ages/train/017.Bearded_collie', 'ages/train/018.Beauceron',
        'ages/train/019.Bedlington_terrier', 'ages/train/020.Belgian_malinois',
        'ages/train/021.Belgian_sheepdog', 'ages/train/022.Belgian_tervuren',
        'ages/train/023.Bernese_mountain_dog', 'ages/train/024.Bichon_frise',
        'ages/train/025.Black_and_tan_coonhound',
        'ages/train/026.Black_russian_terrier', 'ages/train/027.Bloodhound',
        'ages/train/028.Bluetick_coonhound', 'ages/train/029.Border_collie',
        'ages/train/030.Border_terrier', 'ages/train/031.Borzoi',
        'ages/train/032.Boston_terrier', 'ages/train/033.Bouvier_des_flandres',
        'ages/train/034.Boxer', 'ages/train/035.Boykin_spaniel',
        'ages/train/036.Briard', 'ages/train/037.Brittany',
        'ages/train/038.Brussels_griffon', 'ages/train/039.Bull_terrier',
        'ages/train/040.Bulldog', 'ages/train/041.Bullmastiff',
        'ages/train/042.Cairn_terrier', 'ages/train/043.Canaan_dog',
        'ages/train/044.Cane_corso', 'ages/train/045.Cardigan_welsh_corgi',
        'ages/train/046.Cavalier_king_charles_spaniel',
        'ages/train/047.Chesapeake_bay_retriever', 'ages/train/048.Chihuahua',
        'ages/train/049.Chinese_crested', 'ages/train/050.Chinese_shar-pei',
        'ages/train/051.Chow_chow', 'ages/train/052.Clumber_spaniel',
        'ages/train/053.Cocker_spaniel', 'ages/train/054.Collie',
        'ages/train/055.Curly-coated_retriever', 'ages/train/056.Dachshund',
        'ages/train/057.Dalmatian', 'ages/train/058.Dandie_dinmont_terrier',
        'ages/train/059.Doberman_pinscher', 'ages/train/060.Dogue_de_bordeaux',
        'ages/train/061.English_cocker_spaniel',
        'ages/train/062.English_setter',
        'ages/train/063.English_springer_spaniel',
        'ages/train/064.English_toy_spaniel',
        'ages/train/065.Entlebucher_mountain_dog',
        'ages/train/066.Field_spaniel', 'ages/train/067.Finnish_spitz',
        'ages/train/068.Flat-coated_retriever',
        'ages/train/069.French_bulldog', 'ages/train/070.German_pinscher',
        'ages/train/071.German_shepherd_dog',
        'ages/train/072.German_shorthaired_pointer',
        'ages/train/073.German_wirehaired_pointer',
        'ages/train/074.Giant_schnauzer',
        'ages/train/075.Glen_of_imaal_terrier',
        'ages/train/076.Golden_retriever', 'ages/train/077.Gordon_setter',
        'ages/train/078.Great_dane', 'ages/train/079.Great_pyrenees',
        'ages/train/080.Greater_swiss_mountain_dog',
        'ages/train/081.Greyhound', 'ages/train/082.Havanese',
        'ages/train/083.Ibizan_hound', 'ages/train/084.Icelandic_sheepdog',
        'ages/train/085.Irish_red_and_white_setter',
        'ages/train/086.Irish_setter', 'ages/train/087.Irish_terrier',
        'ages/train/088.Irish_water_spaniel', 'ages/train/089.Irish_wolfhound',
        'ages/train/090.Italian_greyhound', 'ages/train/091.Japanese_chin',
        'ages/train/092.Keeshond', 'ages/train/093.Kerry_blue_terrier',
        'ages/train/094.Komondor', 'ages/train/095.Kuvasz',
        'ages/train/096.Labrador_retriever', 'ages/train/097.Lakeland_terrier',
        'ages/train/098.Leonberger', 'ages/train/099.Lhasa_apso',
        'ages/train/100.Lowchen', 'ages/train/101.Maltese',
        'ages/train/102.Manchester_terrier', 'ages/train/103.Mastiff',
        'ages/train/104.Miniature_schnauzer',
        'ages/train/105.Neapolitan_mastiff', 'ages/train/106.Newfoundland',
        'ages/train/107.Norfolk_terrier', 'ages/train/108.Norwegian_buhund',
        'ages/train/109.Norwegian_elkhound',
        'ages/train/110.Norwegian_lundehund', 'ages/train/111.Norwich_terrier',
        'ages/train/112.Nova_scotia_duck_tolling_retriever',
        'ages/train/113.Old_english_sheepdog', 'ages/train/114.Otterhound',
        'ages/train/115.Papillon', 'ages/train/116.Parson_russell_terrier',
        'ages/train/117.Pekingese', 'ages/train/118.Pembroke_welsh_corgi',
        'ages/train/119.Petit_basset_griffon_vendeen',
        'ages/train/120.Pharaoh_hound', 'ages/train/121.Plott',
        'ages/train/122.Pointer', 'ages/train/123.Pomeranian',
        'ages/train/124.Poodle', 'ages/train/125.Portuguese_water_dog',
        'ages/train/126.Saint_bernard', 'ages/train/127.Silky_terrier',
        'ages/train/128.Smooth_fox_terrier', 'ages/train/129.Tibetan_mastiff',
        'ages/train/130.Welsh_springer_spaniel',
        'ages/train/131.Wirehaired_pointing_griffon',
        'ages/train/132.Xoloitzcuintli', 'ages/train/133.Yorkshire_terrier'
    ]
    # Set the default graph as otherwise the model won't work
    graph = tf.get_default_graph()
Beispiel #27
0
def extract_features(input_dir,
                     output_dir,
                     model_type='inceptionv3',
                     batch_size=32):
    """
    Extracts features from a CNN trained on ImageNet classification from all
    videos in a directory.

    Args:
        input_dir (str): Input directory of videos to extract from.
        output_dir (str): Directory where features should be stored.
        model_type (str): Model type to use.
        batch_size (int): Batch size to use when processing.
    """

    input_dir = os.path.expanduser(input_dir)
    output_dir = os.path.expanduser(output_dir)

    if not os.path.isdir(input_dir):
        sys.stderr.write("Input directory '%s' does not exist!\n" % input_dir)
        sys.exit(1)

    # Load desired ImageNet model

    # Note: import Keras only when needed so we don't waste time revving up
    #       Theano/TensorFlow needlessly in case of an error

    model = None
    input_shape = (224, 224)

    if model_type.lower() == 'inceptionv3':
        from keras.applications import InceptionV3
        model = InceptionV3(include_top=True, weights='imagenet')
    elif model_type.lower() == 'xception':
        from keras.applications import Xception
        model = Xception(include_top=True, weights='imagenet')
    elif model_type.lower() == 'resnet50':
        from keras.applications import ResNet50
        model = ResNet50(include_top=True, weights='imagenet')
    elif model_type.lower() == 'vgg16':
        from keras.applications import VGG16
        model = VGG16(include_top=True, weights='imagenet')
    elif model_type.lower() == 'vgg19':
        from keras.applications import VGG19
        model = VGG19(include_top=True, weights='imagenet')
    else:
        sys.stderr.write("'%s' is not a valid ImageNet model.\n" % model_type)
        sys.exit(1)

    if model_type.lower() == 'inceptionv3' or model_type.lower() == 'xception':
        shape = (299, 299)

    # Get outputs of model from layer just before softmax predictions

    from keras.models import Model
    model = Model(model.inputs, output=model.layers[-2].output)

    # Create output directories

    visual_dir = os.path.join(output_dir, 'visual')  # RGB features
    #motion_dir = os.path.join(output_dir, 'motion') # Spatiotemporal features
    #opflow_dir = os.path.join(output_dir, 'opflow') # Optical flow features

    for directory in [visual_dir]:  #, motion_dir, opflow_dir]:
        if not os.path.exists(directory):
            os.makedirs(directory)

    # Find all videos that need to have features extracted

    def is_video(x):
        return x.endswith('.mp4') or x.endswith('.avi') or x.endswith('.mov')

    vis_existing = [x.split('.')[0] for x in os.listdir(visual_dir)]
    #mot_existing = [os.path.splitext(x)[0] for x in os.listdir(motion_dir)]
    #flo_existing = [os.path.splitext(x)[0] for x in os.listdir(opflow_dir)]

    video_filenames = [
        x for x in sorted(os.listdir(input_dir))
        if is_video(x) and os.path.splitext(x)[0] not in vis_existing
    ]

    # Go through each video and extract features

    from keras.applications.imagenet_utils import preprocess_input

    for video_filename in tqdm(video_filenames):

        # Open video clip for reading
        try:
            clip = VideoFileClip(os.path.join(input_dir, video_filename))
        except Exception as e:
            sys.stderr.write("Unable to read '%s'. Skipping...\n" %
                             video_filename)
            sys.stderr.write("Exception: {}\n".format(e))
            continue

        # Sample frames at 1fps
        fps = int(np.round(clip.fps))
        frames = [
            scipy.misc.imresize(crop_center(x.astype(np.float32)), shape)
            for idx, x in enumerate(clip.iter_frames())
            if idx % fps == fps // 2
        ]

        n_frames = len(frames)

        frames_arr = np.empty((n_frames, ) + shape + (3, ), dtype=np.float32)
        for idx, frame in enumerate(frames):
            frames_arr[idx, :, :, :] = frame

        frames_arr = preprocess_input(frames_arr)

        features = model.predict(frames_arr, batch_size=batch_size)

        name, _ = os.path.splitext(video_filename)
        feat_filepath = os.path.join(visual_dir, name + '.npy')

        with open(feat_filepath, 'wb') as f:
            np.save(f, features)
Beispiel #28
0
encoded_Y = encoder.transform(labels)
dummy_y = np_utils.to_categorical(encoded_Y)
num_subjects = np.shape(dummy_y)[1]

# identify train set
dummy_y_val = np.array(dummy_y[0:len_val_set])
X_val = np.array(faces[0:len_val_set])
# identify validation set
dummy_y_train = np.array(dummy_y[len_val_set:ntot])
X_train = np.array(faces[len_val_set:ntot])
print("Data prepared")

print("Preparing network")
# define the network
my_model = Sequential()
my_model.add(ResNet50(include_top=False, pooling='avg', weights='imagenet'))
my_model.add(Dense(num_subjects, activation='softmax'))
# Say not to train first layer (ResNet) model. It is already trained
my_model.layers[0].trainable = False

# load weights from checkpoint
# in case of already partially trained
# network with checkpoints
my_model.load_weights("weights.best.hdf5")
# compile the networ
my_model.compile(optimizer='sgd',
                 loss='categorical_crossentropy',
                 metrics=['accuracy'])
# checkpoint
filepath = "weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath,
Beispiel #29
0
from keras.applications import VGG16, VGG19, InceptionV3, ResNet50, Xception

__network_model_dictionary = {
    'Xception':
    Xception(weights='imagenet',
             include_top=False,
             input_shape=input_layer_shape),
    'VGG16':
    VGG16(weights='imagenet', include_top=False,
          input_shape=input_layer_shape),
    'VGG19':
    VGG19(weights='imagenet', include_top=False,
          input_shape=input_layer_shape),
    'ResNet50':
    ResNet50(weights='imagenet',
             include_top=False,
             input_shape=input_layer_shape),
    'InceptionV3':
    InceptionV3(weights='imagenet',
                include_top=False,
                input_shape=input_layer_shape)
}


def load_base_network(network_model='VGG16', input_layer_shape=(197, 197, 3)):
    """Load a pretrained base network model.

    Args:
        network_model (string): A string which represents the base network model.
        input_layer_shape (3d tensor): A 3d tensor (height x width x channels) which represents the input layer's shape.
    
Beispiel #30
0
    args.add_argument('--iteration', type=str, default='0',
                      help='fork 명령어를 입력할때의 체크포인트로 설정됩니다. 체크포인트 옵션을 안주면 마지막 wall time 의 model 을 가져옵니다.')
    args.add_argument('--pause', type=int, default=0, help='model 을 load 할때 1로 설정됩니다.')
    config = args.parse_args()

    # training parameters
    nb_epoch = config.epoch
    batch_size = config.batch_size
    num_classes = config.num_classes
    input_shape = (224, 224, 3)  # input image shape

    """ Model """
    # Pretrained model
    # base_model = MobileNet(weights='imagenet', include_top=False, pooling='max')
    # base_model = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)
    base_model = ResNet50(weights=None, include_top=False, input_shape=input_shape)

    # base_model = VGG16(weights='imagenet', include_top=False)
    # base_model = VGG19(weights='imagenet', include_top=False)
    print('-------------------------- Base model ---------------------------')
    base_model.summary()

    print('-------------------------- Full model ---------------------------')
    print()
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(num_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=x, name='fc1383')
    
    # for layer in model.layers[:-1]:
    #     layer.trainable = False # Don't train initial pretrained weights