Beispiel #1
0
def get_model(model_name, batch_size, include_top=True, double_prec=False):
    if model_name in ['vgg_16']:
        h = w = 224
        assert (h % 2**5 == 0)
        num_classes = 1000
        model_func = lambda weights, inp: VGG16(include_top=include_top,
                                                weights=weights,
                                                input_tensor=inp,
                                                input_shape=(h, w, 3),
                                                pooling=None,
                                                classes=num_classes)
        preprocess_func = preprocess_vgg
        bits_w = 8
        bits_x = 0
    elif model_name in ['vgg_19']:
        h = w = 224
        num_classes = 1000
        model_func = lambda weights, inp: VGG19(include_top=include_top,
                                                weights=weights,
                                                input_tensor=inp,
                                                input_shape=(h, w, 3),
                                                pooling=None,
                                                classes=num_classes)
        preprocess_func = preprocess_vgg
        bits_w = 8
        bits_x = 0
    elif model_name in ['mobilenet']:
        h = w = 224
        num_classes = 1000
        model_func = lambda weights, inp: MobileNet(include_top=include_top,
                                                    weights=weights,
                                                    input_tensor=inp,
                                                    input_shape=(224, 224, 3),
                                                    pooling=None,
                                                    classes=num_classes)
        preprocess_func = get_preprocessing('mobilenet_v1')
        bits_w = 8
        bits_x = 8
    elif model_name in ['mobilenet_sep']:
        h = w = 224
        num_classes = 1000
        model_func = lambda weights, inp: MobileNet_sep(
            include_top=include_top,
            input_tensor=inp,
            input_shape=(224, 224, 3),
            pooling=None,
            classes=num_classes)
        preprocess_func = get_preprocessing('mobilenet_v1')
        bits_w = 8
        bits_x = 8
    elif 'resnet' in model_name:
        h = w = 224
        num_classes = 1000

        num_layers = int(model_name.split('_')[-1])

        model_func = lambda weights, inp: ResNet50(include_top=include_top,
                                                   input_tensor=inp,
                                                   input_shape=(224, 224, 3),
                                                   pooling=None,
                                                   classes=num_classes,
                                                   layers=num_layers)
        """
        if model_name == "resnet_34":
            model_func = lambda weights, inp: ResNet2_34((224, 224, 3), classes=num_classes, input_tensor=inp)
        elif model_name == "resnet_50":
            model_func = lambda weights, inp: ResNet2_50((224, 224, 3), classes=num_classes, input_tensor=inp)
        elif model_name == "resnet_101":
            model_func = lambda weights, inp: ResNet2_101((224, 224, 3), classes=num_classes, input_tensor=inp)
        elif model_name == "resnet_152":
            model_func = lambda weights, inp: ResNet2_152((224, 224, 3), classes=num_classes, input_tensor=inp)
        else:
            raise AttributeError("unknown model {}".format(model_name))
        """

        preprocess_func = lambda x, h_, w_: preproc_tf(x, h_, w_)
        bits_w = 12
        bits_x = 4

    else:
        raise AttributeError("unknown model {}".format(model_name))

    images = tf.placeholder(dtype=tf.float32, shape=(batch_size, h, w, 3))
    model = model_func("imagenet", images)
    preprocess = lambda x: preprocess_func(x, h, w)

    if double_prec:
        images_dbl = tf.placeholder(dtype=tf.float64,
                                    shape=(batch_size, h, w, 3))
        images_dbl = Input(images_dbl, (batch_size, h, w, 3), dtype=tf.float64)
        K.set_floatx('float64')
        model_dbl = model_func(None, images_dbl)

        for i in range(len(model_dbl.layers)):
            weights = model.layers[i].get_weights()
            weights_dbl = [
                None if w is None else w.astype(np.float64) for w in weights
            ]
            model_dbl.layers[i].set_weights(weights_dbl)

        model = model_dbl
        preprocess = lambda x: preprocess_func(x, h, w, dtype=tf.float64)
        K.set_floatx('float32')

    print(model.summary())
    print_model_size(model)
    res = {}
    res['preprocess'] = preprocess
    res['bits_w'] = bits_w
    res['bits_x'] = bits_x
    return model, res
Beispiel #2
0
# In[10]:

from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# In[11]:

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

# In[12]:

from keras.applications.mobilenet import MobileNet
MobileNet_extractor = MobileNet(weights='imagenet', include_top=False)

# In[13]:

train_features = MobileNet_extractor.predict(x_train)
test_features = MobileNet_extractor.predict(x_test)

# In[14]:

print(train_features.shape, y_train.shape)
print(test_features.shape, y_test.shape)

# In[15]:

train_features = train_features.reshape(train_features.shape[0], -1)
test_features = test_features.reshape(test_features.shape[0], -1)
Beispiel #3
0
import numpy as np

from keras.models import Model
from keras.layers import Dense, Dropout
from keras.applications.mobilenet import MobileNet
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing.image import load_img, img_to_array

import tensorflow as tf

from utils.score_utils import mean_score, std_score

with tf.device('/CPU:0'):
    base_model = MobileNet((None, None, 3),
                           alpha=1,
                           include_top=False,
                           pooling='avg',
                           weights=None)
    x = Dropout(0.75)(base_model.output)
    x = Dense(10, activation='softmax')(x)

    model = Model(base_model.input, x)
    model.load_weights('weights/mobilenet_weights.h5')

    img_path = 'images/art1.jpg'
    img = load_img(img_path)
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)

    x = preprocess_input(x)
Beispiel #4
0
opener = urllib.request.build_opener(proxy)
# install the opener on the module-level
urllib.request.install_opener(opener)
# ----------------------
# ----------------------

#from keras.applications.resnet50 import ResNet50
#from keras.applications.resnet50 import preprocess_input, decode_predictions

from keras.applications.mobilenet import MobileNet
from keras.applications.mobilenet import preprocess_input, decode_predictions

from keras.preprocessing import image
import numpy as np

#model = ResNet50(weights='imagenet')
model = MobileNet(weights='imagenet')

img_path = 'C:\\Users\\jnafziger\\Pictures\\elephant.jpg'
#img_path = 'C:\\Users\\jnafziger\\Pictures\\elephant_indian.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])

# Need to figure out proxy settings for downloading within Keras
Beispiel #5
0
  x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)
  x = _depthwise_conv_block(x, 256, alpha, depth_multiplier,strides=(2, 2), block_id=4)
  x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier,strides=(2, 2), block_id=6)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
  shape = (1, 1, int(512 * alpha))
  x = keras.layers.GlobalAveragePooling2D()(x)
  x = keras.layers.Reshape(shape, name='reshape_1')(x)
  x = keras.layers.Dropout(dropout, name='dropout')(x)
  x = keras.layers.Conv2D(classes, (1, 1),padding='same',name='conv_preds')(x)
  x = keras.layers.Reshape((classes,), name='reshape_2')(x)
  output = keras.layers.Activation('softmax', name='act_softmax')(x)
  model = keras.models.Model(img_input, output)
  return model

model = MobileNet()
adam = keras.optimizers.Adam(lr = 0.001)
model.compile(optimizer = "adam" , loss = "categorical_crossentropy" , metrics = ["acc"])
#model.summary()

"""
    Train model.
"""

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
                              patience=3, min_lr=0.001)

example = model.fit_generator(
        train_generator,
        epochs=100,
        validation_data=validation_generator,
Beispiel #6
0
                        target_size=IMG_SIZE,
                        color_mode='grayscale',
                        batch_size=1024))

# print test_Y[:10]

#model
t_x, t_y = next(train_gen)
# exit()

from keras.applications.mobilenet import MobileNet
from keras.layers import GlobalAveragePooling2D, Dense, Dropout, Flatten
from keras.models import Sequential

base_mobilenet_model = MobileNet(input_shape=t_x.shape[1:],
                                 include_top=False,
                                 weights=None)
multi_disease_model = Sequential()
multi_disease_model.add(base_mobilenet_model)
multi_disease_model.add(GlobalAveragePooling2D())
multi_disease_model.add(Dropout(0.5))
multi_disease_model.add(Dense(512))
multi_disease_model.add(Dropout(0.5))
multi_disease_model.add(Dense(len(all_labels), activation='sigmoid'))
multi_disease_model.compile(optimizer='adam',
                            loss='binary_crossentropy',
                            metrics=['binary_accuracy', 'mae'])
multi_disease_model.summary()

from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau
Beispiel #7
0
def DefineArchitecture(architectureName, classes, customImageSize):
    if architectureName == "VGG16":
        imageSize = 224
        baseModel = VGG16(weights='imagenet', include_top=True)

    elif architectureName == "VGG19":
        imageSize = 224
        baseModel = VGG19(weights='imagenet', include_top=True)

    elif architectureName == "ResNet50":
        imageSize = 224
        baseModel = ResNet50(weights='imagenet', include_top=True)

    elif architectureName == "InceptionV3":
        imageSize = 299
        baseModel = InceptionV3(weights='imagenet', include_top=True)

    elif architectureName == "InceptionResNetV2":
        imageSize = 299
        baseModel = InceptionResNetV2(weights='imagenet', include_top=True)

    elif architectureName == "MobileNet":
        imageSize = 224
        baseModel = MobileNet(weights='imagenet', include_top=True)

    elif architectureName == "MobileNetV2":
        imageSize = 224
        baseModel = MobileNetV2(weights='imagenet', include_top=True)

    elif architectureName == "DenseNet121":
        imageSize = 224
        baseModel = DenseNet121(weights='imagenet', include_top=True)

    elif architectureName == "DenseNet169":
        imageSize = 224
        baseModel = DenseNet169(weights='imagenet', include_top=True)

    elif architectureName == "DenseNet201":
        imageSize = 224
        baseModel = DenseNet201(weights='imagenet', include_top=True)

    elif architectureName == "Custom Model (1-Layer Network w 32 Filters)":
        imageSize = customImageSize

        baseModel = Sequential()
        baseModel.add(
            Conv2D(32, (3, 3),
                   activation='relu',
                   input_shape=(customImageSize, customImageSize, 3)))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Flatten())
        baseModel.add(Dense(128, activation='relu'))
        baseModel.add(Dropout(0.5))
        baseModel.add(Dense(classes, activation='softmax'))

    elif architectureName == "Custom Model (2-Layer Network)":
        imageSize = customImageSize

        baseModel = Sequential()
        baseModel.add(
            Conv2D(32, (3, 3),
                   activation='relu',
                   input_shape=(customImageSize, customImageSize, 3)))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(32, (3, 3), activation='relu'))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Flatten())
        baseModel.add(Dense(128, activation='relu'))
        baseModel.add(Dropout(0.5))
        baseModel.add(Dense(classes, activation='softmax'))

    elif architectureName == "Custom Model (1-Layer Network w 64 Filters)":
        imageSize = customImageSize

        baseModel = Sequential()
        baseModel.add(
            Conv2D(64, (3, 3),
                   activation='relu',
                   input_shape=(customImageSize, customImageSize, 3)))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Flatten())
        baseModel.add(Dense(128, activation='relu'))
        baseModel.add(Dropout(0.5))
        baseModel.add(Dense(classes, activation='softmax'))

    elif architectureName == "Custom Model (1-Layer Network w 128 Filters)":
        imageSize = customImageSize

        baseModel = Sequential()
        baseModel.add(
            Conv2D(customImageSize, (3, 3),
                   activation='relu',
                   input_shape=(customImageSize, customImageSize, 3)))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Flatten())
        baseModel.add(Dense(128, activation='relu'))
        baseModel.add(Dropout(0.5))
        baseModel.add(Dense(classes, activation='softmax'))

    elif architectureName == "Custom Model (3-Layers)":
        imageSize = customImageSize

        baseModel = Sequential()
        baseModel.add(
            Conv2D(32, (3, 3),
                   activation='relu',
                   input_shape=(customImageSize, customImageSize, 3)))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(32, (3, 3), activation='relu'))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(64, (3, 3), activation='relu'))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Flatten())
        baseModel.add(Dense(128, activation='relu'))
        baseModel.add(Dropout(0.5))
        baseModel.add(Dense(classes, activation='softmax'))

    elif architectureName == "Custom Model (CFCN-Shallow)":
        imageSize = customImageSize

        # Create CNN architecture
        modelInput = Input(shape=(customImageSize, customImageSize, 3))

        padding_1 = ZeroPadding2D(padding=1)(modelInput)
        conv2D_1a = Conv2D(32, (5, 5), strides=(2, 2),
                           activation='relu')(padding_1)
        conv2D_1b = Conv2D(32, (11, 11), strides=(4, 4),
                           activation='relu')(modelInput)
        # conv2D_1c = Conv2D(32, (5,5), activation='relu')(modelInput)
        conv2D_1 = Conv2D(32, (3, 3), activation='relu')(modelInput)
        pool_1 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_1)

        merged1 = keras.layers.concatenate([pool_1, conv2D_1a])

        conv2D_2a = Conv2D(64, (5, 5), strides=(2, 2),
                           activation='relu')(merged1)
        conv2D_2 = Conv2D(64, (3, 3), activation='relu')(merged1)
        # pool_2a = MaxPooling2D((4, 4), strides=(4,4))(conv2D_1c)
        pool_2 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_2)

        merged2 = keras.layers.concatenate([pool_2, conv2D_2a, conv2D_1b])

        padding_2 = ZeroPadding2D(padding=1)(merged2)
        conv2D_3a = Conv2D(128, (5, 5), strides=(2, 2),
                           activation='relu')(padding_2)
        conv2D_3 = Conv2D(128, (3, 3), activation='relu')(merged2)
        pool_3 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_3)

        merged3 = keras.layers.concatenate([pool_3, conv2D_3a])

        dense = Flatten()(merged3)
        output = Dense(128, activation='relu')(dense)
        dropout = Dropout(0.5)(output)
        predictions = Dense(classes, activation='softmax')(dropout)
        baseModel = Model(inputs=modelInput, outputs=predictions)

    elif architectureName == "Custom Model (Linear-5 layers)":
        imageSize = customImageSize

        baseModel = Sequential()
        baseModel.add(
            Conv2D(128, (3, 3),
                   activation='relu',
                   input_shape=(customImageSize, customImageSize, 3)))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(64, (3, 3), activation='relu'))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(32, (3, 3), activation='relu'))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(32, (3, 3), activation='relu'))
        baseModel.add(MaxPooling2D(pool_size=(2, 2)))
        baseModel.add(Conv2D(32, (3, 3), activation='relu'))
        baseModel.add(Flatten())
        baseModel.add(Dropout(0.5))
        baseModel.add(
            Dense(128,
                  activation='relu',
                  kernel_regularizer=regularizers.l2(0.01)))
        baseModel.add(Dropout(0.5))
        baseModel.add(
            Dense(64,
                  activation='relu',
                  kernel_regularizer=regularizers.l2(0.01)))
        baseModel.add(Dense(classes, activation='softmax'))

    elif architectureName == "Custom Model (CFNC-Deep)":
        imageSize = customImageSize

        # Create CNN architecture
        modelInput = Input(shape=(customImageSize, customImageSize, 3))

        padding_1 = ZeroPadding2D(padding=1)(modelInput)

        conv2D_1a = Conv2D(128, (3, 3), activation='relu')(modelInput)
        conv2d_1b = Conv2D(128, (5, 5), strides=(2, 2),
                           activation='relu')(padding_1)
        conv2d_1c = Conv2D(128, (11, 11), strides=(4, 4),
                           activation='relu')(modelInput)

        pool_1 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_1a)

        merged_1 = keras.layers.concatenate([pool_1, conv2d_1b])

        conv2D_2a = Conv2D(64, (3, 3), activation='relu')(merged_1)
        conv2D_2b = Conv2D(64, (5, 5), strides=(2, 2),
                           activation='relu')(merged_1)

        pool_2 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_2a)

        merged_2 = keras.layers.concatenate([pool_2, conv2D_2b, conv2d_1c])

        pool_2a = MaxPooling2D((2, 2), strides=(2, 2))(merged_2)

        padding_2 = ZeroPadding2D(padding=1)(merged_2)

        conv2D_3a = Conv2D(32, (3, 3), activation='relu')(merged_2)
        conv2D_3b = Conv2D(32, (5, 5), strides=(2, 2),
                           activation='relu')(padding_2)
        conv2D_2c = Conv2D(64, (11, 11), strides=(4, 4),
                           activation='relu')(padding_2)

        pool_3 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_3a)

        merged_3 = keras.layers.concatenate([pool_3, conv2D_3b])

        padding_3 = ZeroPadding2D(padding=1)(merged_3)

        conv2D_4a = Conv2D(32, (3, 3), activation='relu')(merged_3)
        conv2D_4b = Conv2D(32, (5, 5), strides=(2, 2),
                           activation='relu')(padding_3)
        conv2D_4c = Conv2D(32, (5, 5), strides=(2, 2),
                           activation='relu')(pool_2a)
        conv2D_4d = Conv2D(32, (7, 7), strides=(2, 2),
                           activation='relu')(merged_3)

        pool_4 = MaxPooling2D((2, 2), strides=(2, 2))(conv2D_4a)

        merged_4 = keras.layers.concatenate(
            [pool_4, conv2D_4b, conv2D_2c, conv2D_4c])

        conv2D_5 = Conv2D(32, (3, 3), activation='relu')(merged_4)

        merged_5 = keras.layers.concatenate([conv2D_5, conv2D_4d])

        flatten = Flatten()(merged_5)
        dropout = Dropout(0.5)(flatten)
        dense_1 = Dense(128, activation='relu')(dropout)
        dropout = Dropout(0.5)(dense_1)
        dense_1 = Dense(64, activation='relu')(dropout)
        predictions = Dense(classes, activation='softmax')(dropout)

        baseModel = Model(inputs=modelInput, outputs=predictions)

    return imageSize, baseModel
                    os.path.join(store_path, photo_name))

    products_df_final = products_df_final.sort_values(by="photo")
    products_df_final.to_csv(os.path.join("..", "utils", "products.csv"),
                             index=False)


if __name__ == "__main__":
    seed = 2018
    csvs_path = os.path.join("..", "notebooks")
    dataset_path = os.path.join("..", "photos_resized")
    store_path = os.path.join("..", "static", "images", "store")

    remove_gitkeep(store_path)  #removes gitkeep files
    remove_gitkeep(os.path.join(".", "static", "images", "recommend"))
    remove_gitkeep(os.path.join(".", "static", "images", "user"))

    start_store(
        seed, csvs_path, dataset_path, store_path
    )  #creating the store dataframe and placing the images on the store directory

    resizing = (224, 224)
    shape_output = 1024
    model = MobileNet(input_shape=(224, 224, 3),
                      weights="imagenet",
                      include_top=False,
                      pooling="avg")
    save_embeddings(
        store_path, "embeddings.npy", model, preprocess_input, shape_output,
        resizing
    )  #creating the embeddings file for the store imagescess_input, shape_output, resizing) #creating the embeddings file for the store imagescess_input, shape_output, resizing) #creating the embeddings file for the store images
# In[ ]:

from keras.applications.mobilenet import MobileNet
from keras.applications.mobilenet import preprocess_input

# # 读取VGG16预训练模型参数

# In[ ]:

model_vgg16 = VGG16(weights='imagenet', include_top=False)

# In[ ]:

model_vgg16 = MobileNet(input_shape=(224, 224, 3),
                        weights='imagenet',
                        include_top=False)

# # 读取训练集图片计算特征

# In[ ]:

training_set_vgg16_features = []
for i in list(range(1, 401)) + list(range(501, 901)):
    img_path = 'test8/%06d.png' % i
    # print(img_path)
    img = image.load_img(img_path, target_size=(224, 224))  # 读取图片

    #plt.imshow(img)
    #plt.show()# 显示图片
    x = image.img_to_array(img)  # 图片转换为ndarray
Beispiel #10
0
def train(dataset, architecture, task_name):
    ROOT_MODELS = '/home/dembanakh/.ml-manager/tasks-weights/'
    ROOT_DATASETS = '/home/dembanakh/.ml-manager/datasets/'
    if dataset == 'IMAGENET':
        if architecture == 'VGG16':
            from keras.applications.vgg16 import VGG16
            model = VGG16(weights='imagenet')
        elif architecture == 'VGG19':
            from keras.applications.vgg19 import VGG19
            model = VGG19(weights='imagenet')
        elif architecture == 'MobileNet':
            from keras.applications.mobilenet import MobileNet
            model = MobileNet(weights='imagenet')
        elif architecture == 'ResNet':
            from keras.applications.resnet import ResNet50, preprocess_input
            model = ResNet50(weights='imagenet')
        elif architecture == 'DenseNet':
            from keras.applications.densenet import DenseNet121, preprocess_input
            model = DenseNet121(weights='imagenet')
        else:
            return 0
        model.compile(optimizer='adam',
                      metrics=['accuracy'],
                      loss='sparse_categorical_crossentropy')
        model.save(ROOT_MODELS + task_name + '.h5')
    else:
        input_shape = (224, 224, 3)
        batch_size = 1  # subject to change, but Azure server has little RAM
        import os
        import numpy as np
        from keras.preprocessing import image
        try:
            samples = [i for i in os.listdir(dataset + '/samples')]
        except OSError:
            print 'There is no such directory', dataset + '/samples'
            return 0
        X = np.zeros((len(samples), input_shape[0], input_shape[1],
                      input_shape[2]))  # maybe depends on architecture
        y = np.zeros((len(samples), ))
        if architecture == 'VGG16':
            from keras.applications.vgg16 import VGG16, preprocess_input
            model = VGG16()
            for i in range(X.shape[0]):
                X[i] = preprocess_input(X[i])
        elif architecture == 'VGG19':
            from keras.applications.vgg19 import VGG19, preprocess_input
            model = VGG19()
            for i in range(X.shape[0]):
                X[i] = preprocess_input(X[i])
        elif architecture == 'MobileNet':
            from keras.applications.mobilenet import MobileNet, preprocess_input
            model = MobileNet()
            for i in range(X.shape[0]):
                X[i] = preprocess_input(X[i])
        elif architecture == 'ResNet':
            from keras.applications.resnet import ResNet50, preprocess_input
            model = ResNet50()
            for i in range(X.shape[0]):
                X[i] = preprocess_input(X[i])
        elif architecture == 'DenseNet':
            from keras.applications.densenet import DenseNet121, preprocess_input
            model = DenseNet121()
            for i in range(X.shape[0]):
                X[i] = preprocess_input(X[i])
        else:
            return 0
        for i, sample in enumerate(samples):
            try:
                img = image.load_img(dataset + '/samples/' + sample,
                                     target_size=input_shape)
            except IOError:
                print 'Failed to open file', dataset + '/samples/' + sample
                return 0
            try:
                f_lbl = open(
                    dataset + '/labels/' + sample.split('.')[0] + '.txt', 'r')
            except IOError:
                print 'Failed to open file', dataset + '/labels/' + sample.split(
                    '.')[0] + '.txt'
                return 0
            try:
                y[i] = int(f_lbl.read())
            except ValueError:
                print 'File', dataset + '/labels/' + sample.split(
                    '.')[0] + '.txt', 'doesn\'t contain integer'
                return 0
        model.compile(optimizer='adam',
                      metrics=['accuracy'],
                      loss='sparse_categorical_crossentropy')
        model.fit(X, y, batch_size=batch_size)
        model.save(ROOT_MODELS + task_name + '.h5')
    return 1
Beispiel #11
0
#option to include "none" state or exclude it
CLASSES_TO_TRAIN = ["0", "1", "2", "3"]
#CLASSES_TO_TRAIN=["0", "1", "2"]
NUMBER_OF_CLASSES = len(CLASSES_TO_TRAIN)
EPOCHS = 8

freeze_flag = False  # With frozen layers the results were much worse, so I let Keras retrain the whole model
weights_flag = 'imagenet'  # 'imagenet' or None
preprocess_flag = True  # Should be true for ImageNet pre-trained typically

# This is the largest supported size for the Keras application
input_size = (224, 224, 3)

# we have to remove the top to change the number of classes
mobilenet = MobileNet(input_shape=input_size,
                      include_top=False,
                      weights=weights_flag)

if freeze_flag == True:
    for layer in mobilenet.layers:
        layer.trainable = False

# Makes the input placeholder layer 32x32x3 for CIFAR-10
input_ph = Input(shape=(224, 224, 3), name="input_tensor")

inp = mobilenet(input_ph)

# Add back the missing top (based on the mobilenet.py in Keras)
gap = GlobalAveragePooling2D()(inp)
gap = Reshape((1, 1, 1024))(gap)
gap = Dropout(1e-3)(gap)
Beispiel #12
0

def earth_mover_loss(y_true, y_pred):
    cdf_ytrue = K.cumsum(y_true, axis=-1)
    cdf_ypred = K.cumsum(y_pred, axis=-1)
    samplewise_emd = K.sqrt(
        K.mean(K.square(K.abs(cdf_ytrue - cdf_ypred)), axis=-1))
    return K.mean(samplewise_emd)


# ■■■■■■■■ [2]模型设计 ■■■■■■■■
image_size = 224
# ———— base模型
base_model = MobileNet((image_size, image_size, 3),
                       alpha=1,
                       weights=None,
                       include_top=False,
                       pooling='avg')

for layer in base_model.layers:
    layer.trainable = False  # 让不想参加训练的层[冻结].

# ———— 主模型
x = Dropout(FLAGS.dropout_keep_prob)(base_model.output)
x = Dense(10, activation='softmax')(x)
model_main = Model(base_model.input, x)
model_main.load_weights(FLAGS.h5_path, by_name=True)

checkpoint = ModelCheckpoint(FLAGS.h5_path,
                             monitor='val_loss',
                             verbose=1,
## prediction with preprocessing
pred_resnet50 = model_resnet50.predict(img_resnet50)
# print(pred_class)

n = 10
top_n_resnet50 = decode_predictions(pred_resnet50, top=n)

for a in top_n_resnet50[0]:
    print(a)

#2
#USING MOBILENET MODEL

from keras.applications.mobilenet import MobileNet

model_mobilenet = MobileNet()

img_mobilenet_pth = '/content/drive/My Drive/Colab Notebooks/data/image/cat.jpg'
img_mobilenet = load_img(img_mobilenet_pth, target_size=(
    224, 224))  # image size can be calibrated with target_size parameter
img_mobilenet

img_mobilenet = img_to_array(img_mobilenet)
print(img_mobilenet.shape)

img_mobilenet = np.expand_dims(img_mobilenet, axis=0)
print(img_mobilenet.shape)

img_mobilenet = preprocess_input(
    img_mobilenet)  # preprocess image with preprocess_input function
print(img_mobilenet.shape)
def add_new_top_layer(base_model):
    """
    Add top layer to the net
    Args:
        base_model: keras model excluding top
    """
    x = base_model.output
    x = Dropout(0.75)(x)
    x = Dense(10, activation='softmax')(x)
    model = Model(input=base_model.input, output=x)
    return model


# setup model
base_model = MobileNet(input_shape=(224, 224, 3),
                       alpha=1,
                       include_top=False,
                       pooling='avg',
                       weights=None)
model = add_new_top_layer(base_model)
model.load_weights('weights/mobilenet_weights.h5')


def predict(image_paths):
    X = np.array(
        list(
            map(lambda path: val_preprocess_mobilenet(parse_fn(path)),
                image_paths)))
    scores = model.predict(X, batch_size=32, verbose=1)
    return scores
Beispiel #15
0
    return np.array(resized_images) / 255


X_train = double_dimensions(X_train, dim)

# Data
## Training the model

# The Input Layer: accepts 56 by 56 images
input_image = Input(shape=(dim * 2, dim * 2))
# Adding image channels to data
input_image_dim = Lambda(
    lambda x: K.repeat_elements(K.expand_dims(x, 3), 3, 3))(input_image)
# Using MobileNet as the pre-trained base model
base_model = MobileNet(input_tensor=input_image_dim,
                       include_top=False,
                       pooling='avg')
# Randomly dropping 1/2 of the input units to prevent overfitting
output = Dropout(0.5)(base_model.output)
# Adding a logistic layer for 10 classes
predict = Dense(10, activation='softmax')(output)

# the final model to train
model = Model(inputs=input_image, outputs=predict)
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
#model.summary()

model.fit(X_train, Y_train, batch_size=200, epochs=11)
        return np.squeeze(
            lstm_model.predict(
                np.expand_dims(get_frame_features(get_frames_video(file_path)),
                               axis=0)))


def testfile_class(file_name):
    return os.path.split(file_name)[-1].split("_")[1]


def squeeze_array(array):
    return np.array([np.squeeze(i) for i in array])


if __name__ == '__main__':
    mobile_net = MobileNet()
    mobile_net_submodel = get_submodel(mobile_net,
                                       "global_average_pooling2d_1")
    lstm_model = load_model()

    # testing path
    # creating test set
    test_path = "DataSets/KTH/test/"
    test_set = list()
    lstm_sub_model = get_submodel(lstm_model, "lstm_1")

    test_folders = dict([(idx, folder)
                         for idx, folder in enumerate(os.listdir(test_path))])
    rev_test_folders = dict([(v, k) for k, v in test_folders.items()])

    for file in glob(test_path + "*/*"):
Beispiel #17
0
#!/usr/bin/env python
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.mobilenet import preprocess_input
from keras.applications.mobilenet import decode_predictions
from keras.applications.mobilenet import MobileNet

# iteration count
_iter = 1
"""
    Main
"""
if __name__ == '__main__':
    # load the model
    model = MobileNet(alpha=1.0)
    # load an image from file
    image = load_img('mug.jpg', target_size=(224, 224))
    # convert the image pixels to a numpy array
    image = img_to_array(image)
    # reshape data for the model
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    # prepare the image for the VGG model
    image = preprocess_input(image)

    # predict the probability across all output classes
    for i in range(_iter):
        raw_input('{} iteration, press any key to perform...'.format(str(i)))
        yhat = model.predict(image)

    # return if not iter
    if not _iter: exit()
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense, Dropout, AveragePooling2D, GlobalAveragePooling2D, Reshape
import numpy as np
from keras.applications.mobilenet import MobileNet
from keras.models import Model
from keras.preprocessing.image import ImageDataGenerator

img_width, img_height = 224, 224
train_data_dir = "dataset\Training_Data"
validation_data_dir = "dataset\Test_Data"
batch_size = 8
nb_validation_samples = 100

model = MobileNet(weights='imagenet',
                  include_top=False,
                  input_shape=(img_width, img_height, 3))

model.summary()

x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
output = Dense(2, activation="softmax")(x)

model_final = Model(input=model.input, output=output)

model_final.compile(loss='binary_crossentropy',
                    optimizer='adam',
Beispiel #19
0
#TensorFlow backend uses all GPU memory by default, so we need limit
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
set_session(tf.Session(config=config))

caffe.set_mode_gpu()

#converting

keras_model = MobileNet(input_shape=(224, 224, 3),
                        alpha=1.0,
                        depth_multiplier=1,
                        dropout=1e-3,
                        include_top=True,
                        weights='imagenet',
                        input_tensor=None,
                        pooling=None,
                        classes=1000)

caffe_proto = 'MobileNet.prototxt'
caffe_weights = 'MobileNet.caffemodel'

keras2caffe.convert(keras_model, caffe_proto, caffe_weights)

#testing the model

net = caffe.Net(caffe_proto, caffe_weights, caffe.TEST)

img = cv2.imread('bear.jpg')
def initialise_model(model_type):
    if model_type == 'inception_resnet':
        from keras.applications.inception_resnet_v2 import preprocess_input, InceptionResNetV2

        IM_WIDTH, IM_HEIGHT = 224, 224
        ft_layers = 780

        base_model = InceptionResNetV2(
            weights='imagenet', include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    elif model_type == 'inception':
        from keras.applications.inception_v3 import preprocess_input, InceptionV3

        IM_WIDTH, IM_HEIGHT = 229, 229
        ft_layers = 249

        base_model = InceptionV3(weights='imagenet',
                                 include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    elif model_type == 'mobilenet':
        from keras.applications.mobilenet import preprocess_input, MobileNet

        IM_WIDTH, IM_HEIGHT = 224, 224
        ft_layers = 95

        base_model = MobileNet(input_shape=(224, 224, 3),
                               weights='imagenet',
                               include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    elif model_type == 'resnet':
        from keras.applications.resnet50 import ResNet50, preprocess_input

        IM_WIDTH, IM_HEIGHT = 224, 224
        ft_layers = 172

        base_model = ResNet50(weights='imagenet',
                              include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    elif model_type == 'vgg16':
        from keras.applications.vgg16 import preprocess_input, VGG16

        IM_WIDTH, IM_HEIGHT = 224, 224
        ft_layers = 19

        base_model = VGG16(weights='imagenet',
                           include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    elif model_type == 'vgg19':
        from keras.applications.vgg19 import preprocess_input, VGG19

        IM_WIDTH, IM_HEIGHT = 224, 224
        ft_layers = 22

        base_model = VGG19(weights='imagenet',
                           include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    elif model_type == 'xception':
        from keras.applications.xception import preprocess_input, Xception

        IM_WIDTH, IM_HEIGHT = 299, 299
        ft_layers = 126

        base_model = Xception(weights='imagenet',
                              include_top=False)  #Not Icluding the FC layer
        return base_model, (IM_WIDTH, IM_HEIGHT), preprocess_input, ft_layers
    else:
        raise ValueError(
            "Please Specify a model type by using --model-type argument. See --help for more information."
        )
Beispiel #21
0
        base_model = Xception(weights='imagenet',
                              include_top=False,
                              input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "InceptionResNetV2":
    from keras.application.inception_resnet_v2 import preprocess_input
    preprocessing_function = preprocess_input
    if args.mode != "predict":
        base_model = InceptionResNetV2(weights='imagenet',
                                       include_top=False,
                                       input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "MobileNet":
    from keras.applications.mobilenet import preprocess_input
    preprocessing_function = preprocess_input
    if args.mode != "predict":
        base_model = MobileNet(weights='imagenet',
                               include_top=False,
                               input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet121":
    from keras.applications.densenet import preprocess_input
    preprocessing_function = preprocess_input
    if args.mode != "predict":
        base_model = DenseNet121(weights='imagenet',
                                 include_top=False,
                                 input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet169":
    from keras.applications.densenet import preprocess_input
    preprocessing_function = preprocess_input
    if args.mode != "predict":
        base_model = DenseNet169(weights='imagenet',
                                 include_top=False,
                                 input_shape=(HEIGHT, WIDTH, 3))
Beispiel #22
0
#    return params

run_meta = tf.RunMetadata()

input_size = 416
max_box_per_image = 10

with tf.Session(graph=tf.Graph()) as sess:
    K.set_session(sess)

    input_image = Input(shape=(input_size, input_size, 3), name="input1")
    true_boxes = Input(shape=(1, 1, 1, max_box_per_image, 4))

    mobilenet = MobileNet(input_tensor=tf.placeholder('float32',
                                                      shape=(1, input_size,
                                                             input_size, 3)),
                          include_top=False,
                          input_shape=(input_size, input_size, 3),
                          weights=None)(input_image)
    detect = Conv2D(30, (1, 1),
                    strides=(1, 1),
                    padding='same',
                    name='DetectoinLayer0')(mobilenet)
    detect = Reshape((13, 13, 5, 6))(detect)
    detect = Lambda(lambda args: args[0])([detect, true_boxes])

    yolo = Model([input_image, true_boxes], detect)
    plot_model(yolo, to_file='yolo.png')
    print(detect)
    yolo.summary()

    optimizer = Adam(lr=1e-4,
Beispiel #23
0
test_datagen = ImageDataGenerator(
# rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.3,
width_shift_range = 0.3,
height_shift_range=0.3,
rotation_range=180)

test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size, 
class_mode = "categorical")

#####get model#####
model = MobileNet(weights=None, classes=4)
model.load_weights('/model/mobnet.h5')
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
score,acc = model.evaluate_generator(test_generator, steps = (1050/batch_size)+1)
# calculate predictions
# pred = model.predict(test_features)
# print(test_labels.shape,pred.shape)
# print(test_labels[0],pred[0])
target_names = ['blade','gun','others','shuriken']
print("Test score: " + str(score))
print("Test accuracy: " + str(acc))


# print(classification_report(test_labels, pred,target_names=target_names))
Beispiel #24
0
def get_test_neural_net(type):
    model = None
    if type == 'mobilenet_small':
        from keras.applications.mobilenet import MobileNet
        model = MobileNet((128, 128, 3),
                          depth_multiplier=1,
                          alpha=0.25,
                          include_top=True,
                          weights='imagenet')
    elif type == 'mobilenet':
        from keras.applications.mobilenet import MobileNet
        model = MobileNet((224, 224, 3),
                          depth_multiplier=1,
                          alpha=1.0,
                          include_top=True,
                          weights='imagenet')
    elif type == 'mobilenet_v2':
        from keras.applications.mobilenetv2 import MobileNetV2
        model = MobileNetV2((224, 224, 3),
                            depth_multiplier=1,
                            alpha=1.4,
                            include_top=True,
                            weights='imagenet')
    elif type == 'resnet50':
        from keras.applications.resnet50 import ResNet50
        model = ResNet50(input_shape=(224, 224, 3),
                         include_top=True,
                         weights='imagenet')
    elif type == 'inception_v3':
        from keras.applications.inception_v3 import InceptionV3
        model = InceptionV3(input_shape=(299, 299, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'inception_resnet_v2':
        from keras.applications.inception_resnet_v2 import InceptionResNetV2
        model = InceptionResNetV2(input_shape=(299, 299, 3),
                                  include_top=True,
                                  weights='imagenet')
    elif type == 'xception':
        from keras.applications.xception import Xception
        model = Xception(input_shape=(299, 299, 3),
                         include_top=True,
                         weights='imagenet')
    elif type == 'densenet121':
        from keras.applications.densenet import DenseNet121
        model = DenseNet121(input_shape=(224, 224, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'densenet169':
        from keras.applications.densenet import DenseNet169
        model = DenseNet169(input_shape=(224, 224, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'densenet201':
        from keras.applications.densenet import DenseNet201
        model = DenseNet201(input_shape=(224, 224, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'nasnetmobile':
        from keras.applications.nasnet import NASNetMobile
        model = NASNetMobile(input_shape=(224, 224, 3),
                             include_top=True,
                             weights='imagenet')
    elif type == 'nasnetlarge':
        from keras.applications.nasnet import NASNetLarge
        model = NASNetLarge(input_shape=(331, 331, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'vgg16':
        from keras.applications.vgg16 import VGG16
        model = VGG16(input_shape=(224, 224, 3),
                      include_top=False,
                      pooling='avg',
                      weights='imagenet')
    elif type == 'vgg19':
        from keras.applications.vgg19 import VGG19
        model = VGG19(input_shape=(224, 224, 3),
                      include_top=False,
                      pooling='avg',
                      weights='imagenet')
    elif type == 'multi_io':
        model = get_custom_multi_io_model()
    elif type == 'multi_model_layer_1':
        model = get_custom_model_with_other_model_as_layer()
    elif type == 'multi_model_layer_2':
        model = get_small_model_with_other_model_as_layer()
    return model
def yolo_body(inputs, num_anchors, num_classes):
    """Create YOLO_V3 model CNN body in Keras."""
    '''Layer Nanem: input_1 Output: Tensor("input_1:0", shape=(?, 416, 416, 3), dtype=float32)
    Layer Nanem: conv1_pad Output: Tensor("conv1_pad/Pad:0", shape=(?, 418, 418, 3), dtype=float32)
    Layer Nanem: conv1 Output: Tensor("conv1/convolution:0", shape=(?, 208, 208, 32), dtype=float32)
    Layer Nanem: conv1_bn Output: Tensor("conv1_bn/cond/Merge:0", shape=(?, 208, 208, 32), dtype=float32)
    Layer Nanem: conv1_relu Output: Tensor("conv1_relu/Minimum:0", shape=(?, 208, 208, 32), dtype=float32)
    Layer Nanem: conv_pad_1 Output: Tensor("conv_pad_1/Pad:0", shape=(?, 210, 210, 32), dtype=float32)
    Layer Nanem: conv_dw_1 Output: Tensor("conv_dw_1/depthwise:0", shape=(?, 208, 208, 32), dtype=float32)
    Layer Nanem: conv_dw_1_bn Output: Tensor("conv_dw_1_bn/cond/Merge:0", shape=(?, 208, 208, 32), dtype=float32)
    Layer Nanem: conv_dw_1_relu Output: Tensor("conv_dw_1_relu/Minimum:0", shape=(?, 208, 208, 32), dtype=float32)
    Layer Nanem: conv_pw_1 Output: Tensor("conv_pw_1/convolution:0", shape=(?, 208, 208, 64), dtype=float32)
    Layer Nanem: conv_pw_1_bn Output: Tensor("conv_pw_1_bn/cond/Merge:0", shape=(?, 208, 208, 64), dtype=float32)
    Layer Nanem: conv_pw_1_relu Output: Tensor("conv_pw_1_relu/Minimum:0", shape=(?, 208, 208, 64), dtype=float32)
    Layer Nanem: conv_pad_2 Output: Tensor("conv_pad_2/Pad:0", shape=(?, 210, 210, 64), dtype=float32)
    Layer Nanem: conv_dw_2 Output: Tensor("conv_dw_2/depthwise:0", shape=(?, 104, 104, 64), dtype=float32)
    Layer Nanem: conv_dw_2_bn Output: Tensor("conv_dw_2_bn/cond/Merge:0", shape=(?, 104, 104, 64), dtype=float32)
    Layer Nanem: conv_dw_2_relu Output: Tensor("conv_dw_2_relu/Minimum:0", shape=(?, 104, 104, 64), dtype=float32)
    Layer Nanem: conv_pw_2 Output: Tensor("conv_pw_2/convolution:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pw_2_bn Output: Tensor("conv_pw_2_bn/cond/Merge:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pw_2_relu Output: Tensor("conv_pw_2_relu/Minimum:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pad_3 Output: Tensor("conv_pad_3/Pad:0", shape=(?, 106, 106, 128), dtype=float32)
    Layer Nanem: conv_dw_3 Output: Tensor("conv_dw_3/depthwise:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_dw_3_bn Output: Tensor("conv_dw_3_bn/cond/Merge:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_dw_3_relu Output: Tensor("conv_dw_3_relu/Minimum:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pw_3 Output: Tensor("conv_pw_3/convolution:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pw_3_bn Output: Tensor("conv_pw_3_bn/cond/Merge:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pw_3_relu Output: Tensor("conv_pw_3_relu/Minimum:0", shape=(?, 104, 104, 128), dtype=float32)
    Layer Nanem: conv_pad_4 Output: Tensor("conv_pad_4/Pad:0", shape=(?, 106, 106, 128), dtype=float32)
    Layer Nanem: conv_dw_4 Output: Tensor("conv_dw_4/depthwise:0", shape=(?, 52, 52, 128), dtype=float32)
    Layer Nanem: conv_dw_4_bn Output: Tensor("conv_dw_4_bn/cond/Merge:0", shape=(?, 52, 52, 128), dtype=float32)
    Layer Nanem: conv_dw_4_relu Output: Tensor("conv_dw_4_relu/Minimum:0", shape=(?, 52, 52, 128), dtype=float32)
    Layer Nanem: conv_pw_4 Output: Tensor("conv_pw_4/convolution:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pw_4_bn Output: Tensor("conv_pw_4_bn/cond/Merge:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pw_4_relu Output: Tensor("conv_pw_4_relu/Minimum:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pad_5 Output: Tensor("conv_pad_5/Pad:0", shape=(?, 54, 54, 256), dtype=float32)
    Layer Nanem: conv_dw_5 Output: Tensor("conv_dw_5/depthwise:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_dw_5_bn Output: Tensor("conv_dw_5_bn/cond/Merge:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_dw_5_relu Output: Tensor("conv_dw_5_relu/Minimum:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pw_5 Output: Tensor("conv_pw_5/convolution:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pw_5_bn Output: Tensor("conv_pw_5_bn/cond/Merge:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pw_5_relu Output: Tensor("conv_pw_5_relu/Minimum:0", shape=(?, 52, 52, 256), dtype=float32)
    Layer Nanem: conv_pad_6 Output: Tensor("conv_pad_6/Pad:0", shape=(?, 54, 54, 256), dtype=float32)
    Layer Nanem: conv_dw_6 Output: Tensor("conv_dw_6/depthwise:0", shape=(?, 26, 26, 256), dtype=float32)
    Layer Nanem: conv_dw_6_bn Output: Tensor("conv_dw_6_bn/cond/Merge:0", shape=(?, 26, 26, 256), dtype=float32)
    Layer Nanem: conv_dw_6_relu Output: Tensor("conv_dw_6_relu/Minimum:0", shape=(?, 26, 26, 256), dtype=float32)
    Layer Nanem: conv_pw_6 Output: Tensor("conv_pw_6/convolution:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_6_bn Output: Tensor("conv_pw_6_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_6_relu Output: Tensor("conv_pw_6_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pad_7 Output: Tensor("conv_pad_7/Pad:0", shape=(?, 28, 28, 512), dtype=float32)
    Layer Nanem: conv_dw_7 Output: Tensor("conv_dw_7/depthwise:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_7_bn Output: Tensor("conv_dw_7_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_7_relu Output: Tensor("conv_dw_7_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_7 Output: Tensor("conv_pw_7/convolution:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_7_bn Output: Tensor("conv_pw_7_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_7_relu Output: Tensor("conv_pw_7_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pad_8 Output: Tensor("conv_pad_8/Pad:0", shape=(?, 28, 28, 512), dtype=float32)
    Layer Nanem: conv_dw_8 Output: Tensor("conv_dw_8/depthwise:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_8_bn Output: Tensor("conv_dw_8_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_8_relu Output: Tensor("conv_dw_8_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_8 Output: Tensor("conv_pw_8/convolution:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_8_bn Output: Tensor("conv_pw_8_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_8_relu Output: Tensor("conv_pw_8_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pad_9 Output: Tensor("conv_pad_9/Pad:0", shape=(?, 28, 28, 512), dtype=float32)
    Layer Nanem: conv_dw_9 Output: Tensor("conv_dw_9/depthwise:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_9_bn Output: Tensor("conv_dw_9_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_9_relu Output: Tensor("conv_dw_9_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_9 Output: Tensor("conv_pw_9/convolution:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_9_bn Output: Tensor("conv_pw_9_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_9_relu Output: Tensor("conv_pw_9_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pad_10 Output: Tensor("conv_pad_10/Pad:0", shape=(?, 28, 28, 512), dtype=float32)
    Layer Nanem: conv_dw_10 Output: Tensor("conv_dw_10/depthwise:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_10_bn Output: Tensor("conv_dw_10_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_10_relu Output: Tensor("conv_dw_10_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_10 Output: Tensor("conv_pw_10/convolution:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_10_bn Output: Tensor("conv_pw_10_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_10_relu Output: Tensor("conv_pw_10_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pad_11 Output: Tensor("conv_pad_11/Pad:0", shape=(?, 28, 28, 512), dtype=float32)
    Layer Nanem: conv_dw_11 Output: Tensor("conv_dw_11/depthwise:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_11_bn Output: Tensor("conv_dw_11_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_dw_11_relu Output: Tensor("conv_dw_11_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_11 Output: Tensor("conv_pw_11/convolution:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_11_bn Output: Tensor("conv_pw_11_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pw_11_relu Output: Tensor("conv_pw_11_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32)
    Layer Nanem: conv_pad_12 Output: Tensor("conv_pad_12/Pad:0", shape=(?, 28, 28, 512), dtype=float32)
    Layer Nanem: conv_dw_12 Output: Tensor("conv_dw_12/depthwise:0", shape=(?, 13, 13, 512), dtype=float32)
    Layer Nanem: conv_dw_12_bn Output: Tensor("conv_dw_12_bn/cond/Merge:0", shape=(?, 13, 13, 512), dtype=float32)
    Layer Nanem: conv_dw_12_relu Output: Tensor("conv_dw_12_relu/Minimum:0", shape=(?, 13, 13, 512), dtype=float32)
    Layer Nanem: conv_pw_12 Output: Tensor("conv_pw_12/convolution:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_pw_12_bn Output: Tensor("conv_pw_12_bn/cond/Merge:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_pw_12_relu Output: Tensor("conv_pw_12_relu/Minimum:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_pad_13 Output: Tensor("conv_pad_13/Pad:0", shape=(?, 15, 15, 1024), dtype=float32)
    Layer Nanem: conv_dw_13 Output: Tensor("conv_dw_13/depthwise:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_dw_13_bn Output: Tensor("conv_dw_13_bn/cond/Merge:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_dw_13_relu Output: Tensor("conv_dw_13_relu/Minimum:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_pw_13 Output: Tensor("conv_pw_13/convolution:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_pw_13_bn Output: Tensor("conv_pw_13_bn/cond/Merge:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: conv_pw_13_relu Output: Tensor("conv_pw_13_relu/Minimum:0", shape=(?, 13, 13, 1024), dtype=float32)
    Layer Nanem: global_average_pooling2d_1 Output: Tensor("global_average_pooling2d_1/Mean:0", shape=(?, 1024), dtype=float32)
    Layer Nanem: reshape_1 Output: Tensor("reshape_1/Reshape:0", shape=(?, 1, 1, 1024), dtype=float32)
    Layer Nanem: dropout Output: Tensor("dropout/cond/Merge:0", shape=(?, 1, 1, 1024), dtype=float32)
    Layer Nanem: conv_preds Output: Tensor("conv_preds/BiasAdd:0", shape=(?, 1, 1, 1000), dtype=float32)
    Layer Nanem: act_softmax Output: Tensor("act_softmax/truediv:0", shape=(?, 1, 1, 1000), dtype=float32)
    Layer Nanem: reshape_2 Output: Tensor("reshape_2/Reshape:0", shape=(?, 1000), dtype=float32)
    '''

    #net, endpoint = inception_v2.inception_v2(inputs)
    mobilenet = MobileNet(input_tensor=inputs,weights='imagenet')

    # input: 416 x 416 x 3
    # conv_pw_13_relu :13 x 13 x 1024
    # conv_pw_11_relu :26 x 26 x 512
    # conv_pw_5_relu : 52 x 52 x 256

    f1 = mobilenet.get_layer('conv_pw_13_relu').output
    # f1 :13 x 13 x 1024
    x, y1 = make_last_layers(f1, 512, num_anchors * (num_classes + 5))

    x = compose(
            DarknetConv2D_BN_Leaky(256, (1,1)),
            UpSampling2D(2))(x)

    f2 = mobilenet.get_layer('conv_pw_11_relu').output
    # f2: 26 x 26 x 512
    x = Concatenate()([x,f2])

    x, y2 = make_last_layers(x, 256, num_anchors*(num_classes+5))

    x = compose(
            DarknetConv2D_BN_Leaky(128, (1,1)),
            UpSampling2D(2))(x)

    f3 = mobilenet.get_layer('conv_pw_5_relu').output
    # f3 : 52 x 52 x 256
    x = Concatenate()([x, f3])
    x, y3 = make_last_layers(x, 128, num_anchors*(num_classes+5))

    return Model(inputs = inputs, outputs=[y1,y2,y3])
Beispiel #26
0
DROPOUT_2 = 0.5  # dropout probability in fully connected layer

# Directory Paths
main_path = 'C:\\Users\\jjmiy_000\\Documents\\GitHub'  # root directory
dataset_path = os.path.join(main_path,
                            'data_sets')  # Data set directory location
train_path = os.path.join(os.getcwd(), 'bottleneck_train_features.npy')
validate_path = os.path.join(os.getcwd(), 'bottleneck_validate_features.npy')

# Input Shape
shape = (IMAGE_SIZE, IMAGE_SIZE, 3)  # input has form (height, width, channels)

# LOAD MOBILENET pre-trained model
mobileNet_model = MobileNet(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3),
                            alpha=1,
                            include_top=False,
                            weights='imagenet',
                            input_tensor=None,
                            pooling=max)

# TRAINGING AND VALIDATION DATASET
# generates batches of normalized/augmented image training data in tensor form
data_gen = ImageDataGenerator(rescale=1.0 / 255,
                              data_format='channels_last',
                              validation_split=0.1)

train_generator = data_gen.flow_from_directory(dataset_path,
                                               target_size=(IMAGE_SIZE,
                                                            IMAGE_SIZE),
                                               batch_size=BATCH_SIZE,
                                               class_mode='categorical',
                                               subset='training',
Beispiel #27
0
def fc(enco, size, ncats):
    mobile = MobileNet(input_shape=(size, size, 128),
                       alpha=1.,
                       weights=None,
                       classes=ncats)(enco)
    return mobile
Beispiel #28
0
#         model.add(Conv2D(192, (3,3)))
#         model.add(Activation('relu'))
#         model.add(MaxPooling2D((2,2)))
#         model.add(Dropout(0.4))
#         model.add(Conv2D(256, (2,2)))
#         model.add(Activation('relu'))
#         model.add(MaxPooling2D((2,2)))
#         model.add(Dropout(0.5))

#         model.add(Flatten())
#         model.add(Dense(512, activation='relu'))
#         model.add(Dropout(0.5))
#         # Output Layer
#         model.add(Dense(NUM_CLASSES))
#         model.add(Activation('softmax'))
 model = MobileNet(weights='imagenet',include_top = False)
        x = model.output #Take the last layer
        x = GlobalAveragePooling2D()(x) #Add a GlobalAvgPooling        
        x = Dense(1024, activation='relu')(x)
        
        out = Dense(NUM_CLASSES, activation='softmax')(x)
        
        model = Model(inputs=model.input, outputs=out)        
        
        model.summary()

        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        history = model.fit_generator(train_generator,
                                  steps_per_epoch = train_generator.n // _batch,
                                  epochs = _epochs,
                                  verbose=1)
Beispiel #29
0
def make_model(config, mname, cfg, shape, dense_cfg):
    print(mname)
    length = shape[2]
    if 'pre' in mname:
        if 'vgg16' in mname:
            backend = VGG16(weights='imagenet',
                            include_top=False,
                            input_shape=(128, 157, 3))
        elif 'vgg19' in mname:
            backend = VGG19(weights='imagenet',
                            include_top=False,
                            input_shape=(128, 157, 3))
        elif 'xception' in mname:
            backend = Xception(weights='imagenet',
                               include_top=False,
                               input_shape=(128, 157, 3))
        elif 'resnet50' in mname:
            backend = ResNet50(weights='imagenet',
                               include_top=False,
                               input_shape=(197, 197, 3))
        elif 'inception_v3' in mname:
            backend = InceptionV3(weights='imagenet',
                                  include_top=False,
                                  input_shape=(139, 157, 3))
        elif 'inception_resnet_v2' in mname:
            backend = InceptionResNetV2(weights='imagenet',
                                        include_top=False,
                                        input_shape=(139, 157, 3))
        elif 'mobilenet' in mname:
            backend = MobileNet(weights='imagenet',
                                include_top=False,
                                input_shape=(160, 160, 3))
        elif 'densenet121' in mname:
            backend = DenseNet121(weights='imagenet',
                                  include_top=False,
                                  input_shape=(221, 221, 3))
        elif 'densenet201' in mname:
            backend = DenseNet201(weights='imagenet',
                                  include_top=False,
                                  input_shape=(221, 221, 3))

        else:
            print('\npretrained model not found\n')

        out = dense_outlayer(backend.output,
                             config.n_classes,
                             dense_cfg,
                             flatten=True)
        return cnn2d.make_model(config, backend.input, out)

    elif 'vgg' in mname:
        return make_vgg(config, cfg, length, True, dense_cfg)
    elif 'seresnet' in mname:
        return make_seresnet(mname, config, length, dense_cfg)
    elif 'resnet' in mname:
        return make_resnet(config, cfg, length)
    elif 'resnext' in mname:
        return make_resnext(config, length)
    elif 'numeric' in mname:
        length = shape[1]
        return make_numeric(config, cfg, length, dense_cfg)
    elif '2rdcnn' in mname:
        return make_2rdcnn(config, cfg, length, dense_cfg)
Beispiel #30
0
                                    height_shift_range=0.2,
                                    zoom_range=0.2,
                                    shear_range=0.2)
train_generator = train_data_gen.flow_from_directory(directory='assets/train/',
                                                     target_size=(224, 224),
                                                     batch_size=32,
                                                     class_mode='categorical')

valid_data_gen = ImageDataGenerator(rescale=1. / 255)
valid_generator = valid_data_gen.flow_from_directory(directory='assets/valid/',
                                                     target_size=(224, 224),
                                                     batch_size=32,
                                                     class_mode='categorical')

base_model = MobileNet(weights='imagenet',
                       include_top=False,
                       input_shape=(224, 224, 3))
top_model = load_model('models/MobileNet/Dense/top_model.hdf5',
                       custom_objects={'top1_loss': top1_loss})
x = base_model.output
predictions = top_model(x)

model = Model(inputs=base_model.input, outputs=predictions)
#for layer in base_model.layers:
#   layer.trainable = False

model.summary()
model.compile(optimizer=SGD(momentum=0.9, lr=0.0001, decay=0.001),
              loss=categorical_crossentropy,
              metrics=[top1_loss])
file_path = 'models/MobileNet/Dense/mobilenet_dense.hdf5'