from keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization
from keras.models import Model
from keras.applications import MobileNet, MobileNetV2

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

model3 = MobileNetV2(weights='imagenet', input_shape=(224, 224, 3))

print(len(model1.layers))
print(len(model3.layers))
    plt.plot(y[:, 2])
    plt.ylabel('reward')
    plt.subplot(514)
    plt.plot(y[:, 3])
    plt.ylabel('epsilon')
    plt.subplot(515)
    plt.plot(y[:, 4])
    plt.ylabel('action')
    plt.pause(0.0001)


if __name__ == '__main__':
    # images_dir = '/home/hsli/gnode02/imagenet-data/train/'
    images_dir = '/home/imagenet-data/train/'

    feature_extractor = MobileNetV2(include_top=False)
    x = feature_extractor.output
    x = AveragePooling2D(pool_size=(4, 4))(x)
    feature_extractor = Model(inputs=feature_extractor.input, outputs=x)

    rm = ResultManager('evaluation_results')
    agent_acc_size_dict = []
    origin_acc_size_dict = []

    agent = DQN_Agent(s_dim=1280,
                      a_dim=10,
                      epsilon_decay=0.99,
                      epsilon_min=0.02,
                      gamma=0.95,
                      replay_batchsize=256)
import cv2
#%%
# mbv2 = MobileNetV2(include_top=True)
# mbv2.save("mbv2.h5")
#%% 超参数
batch_size = 64
cols = 320
rows = 180
channels = 3
# train_dir = "/home/stephan/datasets/驭势科技/dataset/dataset/train"
train_dir = r"C:\Users\xps\Documents\Python Scripts\keras"
epochs_initial = 2
epochs_finetune = 5
train_test_ratio = 5
#%% 构建网络
mbv2 = MobileNetV2(include_top=False,input_shape=(rows,cols,channels))
# x = mbv2.get_layer("Conv_1_bn").output
x = mbv2.output
x = AveragePooling2D() (x)
# x = Dense(100,activation='relu') (x)
x = Dense(1000,activation='relu') (x)
x = Dense(100,activation='relu') (x)
predictions = Dense(2,activation='relu') (x)
mbv2_self_driving = Model(input=mbv2.input, output=predictions)
mbv2_self_driving.save("mbv2_self_driving.h5")
#%% 冻结卷积层
for layer in mbv2_self_driving.layers[:-3]:
    layer.trainable = False
#%% 查看可更新层
# for layer in mbv2_self_driving.layers:
#     print(layer.trainable)
Beispiel #4
0
def SSD(input_shape, num_classes):
    """SSD300 architecture.

    # Arguments
        input_shape: Shape of the input image,
            expected to be either (300, 300, 3) or (3, 300, 300)(not tested).
        num_classes: Number of classes including background.

    # References
        https://arxiv.org/abs/1512.02325
    """
    alpha = 1.0
    img_size = (input_shape[1], input_shape[0])
    input_shape = (input_shape[1], input_shape[0], 3)
    mobilenetv2_input_shape = (224, 224, 3)
    mobilenetv2_input_shape = (300, 300, 3)

    Input0 = Input(input_shape)
    mobilenetv2 = MobileNetV2(input_shape=mobilenetv2_input_shape,
                              include_top=False)
    FeatureExtractor = Model(
        inputs=mobilenetv2.input,
        outputs=mobilenetv2.get_layer('block_12_add').output)
    #get_3rd_layer_output = K.function([mobilenetv2.layers[114].input, K.learning_phase()],
    #                                  [mobilenetv2.layers[147].output])

    x = FeatureExtractor(Input0)
    x, pwconv3 = _isb4conv13(x,
                             filters=160,
                             alpha=alpha,
                             stride=1,
                             expansion=6,
                             block_id=13)
    #x=get_3rd_layer_output([x,1])[0]
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=14)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=15)
    x = _inverted_res_block(x,
                            filters=320,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=16)
    x, pwconv4 = Conv(x, 1280)
    x, pwconv5 = LiteConv(x, 5, 512)
    x, pwconv6 = LiteConv(x, 6, 256)
    x, pwconv7 = LiteConv(x, 7, 128)
    x, pwconv8 = LiteConv(x, 8, 128)

    pwconv3_mbox_loc_flat, pwconv3_mbox_conf_flat, pwconv3_mbox_priorbox = prediction(
        pwconv3, 3, 3, 60.0, None, [2], num_classes, img_size)
    pwconv4_mbox_loc_flat, pwconv4_mbox_conf_flat, pwconv4_mbox_priorbox = prediction(
        pwconv4, 4, 6, 105.0, 150.0, [2, 3], num_classes, img_size)
    pwconv5_mbox_loc_flat, pwconv5_mbox_conf_flat, pwconv5_mbox_priorbox = prediction(
        pwconv5, 5, 6, 150.0, 195.0, [2, 3], num_classes, img_size)
    pwconv6_mbox_loc_flat, pwconv6_mbox_conf_flat, pwconv6_mbox_priorbox = prediction(
        pwconv6, 6, 6, 195.0, 240.0, [2, 3], num_classes, img_size)
    pwconv7_mbox_loc_flat, pwconv7_mbox_conf_flat, pwconv7_mbox_priorbox = prediction(
        pwconv7, 7, 6, 240.0, 285.0, [2, 3], num_classes, img_size)
    pwconv8_mbox_loc_flat, pwconv8_mbox_conf_flat, pwconv8_mbox_priorbox = prediction(
        pwconv8, 8, 6, 285.0, 300.0, [2, 3], num_classes, img_size)

    # Gather all predictions
    mbox_loc = concatenate([
        pwconv3_mbox_loc_flat, pwconv4_mbox_loc_flat, pwconv5_mbox_loc_flat,
        pwconv6_mbox_loc_flat, pwconv7_mbox_loc_flat, pwconv8_mbox_loc_flat
    ],
                           axis=1,
                           name='mbox_loc')
    mbox_conf = concatenate([
        pwconv3_mbox_conf_flat, pwconv4_mbox_conf_flat, pwconv5_mbox_conf_flat,
        pwconv6_mbox_conf_flat, pwconv7_mbox_conf_flat, pwconv8_mbox_conf_flat
    ],
                            axis=1,
                            name='mbox_conf')
    mbox_priorbox = concatenate([
        pwconv3_mbox_priorbox, pwconv4_mbox_priorbox, pwconv5_mbox_priorbox,
        pwconv6_mbox_priorbox, pwconv7_mbox_priorbox, pwconv8_mbox_priorbox
    ],
                                axis=1,
                                name='mbox_priorbox')
    if hasattr(mbox_loc, '_keras_shape'):
        num_boxes = mbox_loc._keras_shape[-1] // 4
    elif hasattr(mbox_loc, 'int_shape'):
        num_boxes = K.int_shape(mbox_loc)[-1] // 4
    mbox_loc = Reshape((num_boxes, 4), name='mbox_loc_final')(mbox_loc)
    mbox_conf = Reshape((num_boxes, num_classes),
                        name='mbox_conf_logits')(mbox_conf)
    mbox_conf = Activation('softmax', name='mbox_conf_final')(mbox_conf)
    predictions = concatenate([mbox_loc, mbox_conf, mbox_priorbox],
                              axis=2,
                              name='predictions')
    model = Model(inputs=Input0, outputs=predictions)
    return model
Beispiel #5
0
from keras.applications import MobileNetV2
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten, GlobalAveragePooling2D
from keras.optimizers import RMSprop, Adam, SGD
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
import glob

# 전역변수
img_rows, img_cols = 224, 224
mobilenet = MobileNetV2(weights='imagenet',
                        include_top=False,
                        input_shape=(img_rows, img_cols, 3))
train_data_dir = './data/train'
valid_data_dir = './data/validation'
num_classes = 2
batch_size = 32

for layer in mobilenet.layers:
    layer.trainable = True


def addTopModelMobileNet(bottom_model, num_classes):
    top_model = bottom_model.output
    top_model = GlobalAveragePooling2D()(top_model)
    top_model = Dense(4096, activation='relu')(top_model)
    top_model = Dropout(0.3)(top_model)
    top_model = Dense(4096, activation='relu')(top_model)
    top_model = Dropout(0.3)(top_model)
    top_model = Dense(2048, activation='relu')(top_model)
    top_model = Dropout(0.5)(top_model)
Beispiel #6
0
def build_model(name='vgg16', filepath=None, training=False, continuing=True):
    model = None
    base = None
    shape = config.IMAGE_DIMENSIONS

    checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')

    if os.path.exists(filepath) and training and continuing:
        model = load_model(filepath)
        return model, checkpoint, shape
    
    name = name.lower()
    if name == 'vgg16':
        base = VGG16()
    elif name == 'vgg19':
        base = VGG19()
    elif name == 'xception':
        base = Xception()
        shape = config.IMAGE_DIMENSIONS_299
    elif name == 'inceptionv3':
        base = InceptionV3()
        shape = config.IMAGE_DIMENSIONS_299
    elif name == 'resnet50':
        base = ResNet50()
    elif name == 'mobilenetv2':
        base = MobileNetV2()
    elif name == 'densenet121':
        base = DenseNet121()
    elif name == 'densenet169':
        base = DenseNet169()
    elif name == 'densenet201':
        base = DenseNet201()
    elif name == 'inceptionresnetv2':
        base = InceptionResNetV2()
        shape = config.IMAGE_DIMENSIONS_299
    elif name == 'nasnetmobile':
        base = NASNetMobile()
    elif name == 'control':
        input = Input(shape=config.IMAGE_SHAPE)
        base = Conv2D(input_shape=config.IMAGE_SHAPE, filters=16, kernel_size=3, activation='relu')(input)
        base = MaxPooling2D()(base)
        base = Flatten()(base)
        base = Model(inputs=input, output = base)

    if name != 'control':
        for layer in base.layers:
            layer.trainable = False
    
    x = Dense(1024, activation='relu')(base.output)
    x = BatchNormalization()(x)
    x = Dropout(0.7)(x)
    x = Dense(512, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    x = Dense(2, activation='softmax')(x)
    
    model = Model(inputs=base.input, outputs=x)

    if os.path.exists(filepath):
        model.load_weights(filepath) 
    
    return model, checkpoint, shape
Beispiel #7
0
from keras.applications import MobileNetV2

from keras.preprocessing import image

from tqdm import tqdm

from PIL import Image
import numpy as np
import tensorflow as tf
import os
from numpy import savez_compressed

files = os.listdir('data')
enc = np.zeros((len(files), 1000))
model = MobileNetV2(classifier_activation=None)
for i in tqdm(range(len(files)), desc="Encoding files..", unit="file"):
    try:
        im = np.asarray(
            image.load_img('data/' + files[i], target_size=(224, 224)))
        im = im[:, :, :3] / 225
        e = model.predict(np.expand_dims(im, axis=0))
        enc[i, :] = e[0, :]
    except:
        print("Error reading image:" + files[i])

savez_compressed("data.npz", enc)
Beispiel #8
0
import os
import time
import cv2
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications import MobileNetV2
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = MobileNetV2(weights='imagenet', include_top=False)


def get_image_content(image):
    image = cv2.resize(image, dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
    image = np.expand_dims(image, axis=0)
    img_data = preprocess_input(image)
    features = model.predict(img_data)
    print(features.shape)
    return features
from keras.layers import Dense
from keras.applications import MobileNetV2

datagen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.3)
train_generator = datagen.flow_from_directory('data/dataset/train',
                                                    target_size=(224, 224),
                                                    batch_size=64,
                                                    class_mode='categorical')

test_generator = datagen.flow_from_directory('data/dataset/test',
                                                    target_size=(224, 224),
                                                    batch_size=64,
                                                    class_mode='categorical')

mobile = MobileNetV2(include_top=False,
                          weights="imagenet", 
                          input_shape=(224,224,3),
                          pooling="avg")
model = Sequential()
model.add(mobile)
model.add(Dense(5, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit_transform(train_generator, 
                    epochs=10, 
                    steps_per_epoch=2360/64, 
                    validation_data=test_generator, 
                    validation_steps=263/64)

model.save('data/model.h5')
Beispiel #10
0
    def prepare(self):
        print("Setting up CNN v8..")
        if not os.path.exists(self.cnn_dir):
            os.makedirs(self.cnn_dir)

        ImageFile.LOAD_TRUNCATED_IMAGES = True

        # Rescale all images by 1./255 and apply image augmentation
        train_datagen = ImageDataGenerator(shear_range=0.2,
                                           zoom_range=0.2,
                                           rotation_range=20,
                                           width_shift_range=0.2,
                                           height_shift_range=0.2,
                                           rescale=1. / 255)

        validation_datagen = ImageDataGenerator(rescale=1. / 255)

        test_datagen = ImageDataGenerator(rescale=1. / 255)

        self.train_generator = train_datagen.flow_from_directory(
            self.train_dir,
            target_size=(self.img_width, self.img_height),
            batch_size=self.batch_size,
            class_mode='categorical')

        self.validation_generator = validation_datagen.flow_from_directory(
            self.validation_dir,
            target_size=(self.img_width, self.img_height),
            batch_size=self.batch_size,
            class_mode='categorical')

        self.test_generator = test_datagen.flow_from_directory(
            self.test_dir,
            target_size=(self.img_width, self.img_height),
            batch_size=self.batch_size,
            class_mode='categorical')

        print("Generators are ready, saving class indices")
        np.save((self.cnn_dir + "class_indices.npy"),
                self.train_generator.class_indices)
        self.IMG_SHAPE = (self.img_width, self.img_height, 3)
        self.num_classes = len(self.train_generator.class_indices)

        print("Creating transfer train model")

        # Create the base model from the pre-trained model MobileNet V2 on imagenet data
        self.base_model = MobileNetV2(input_shape=self.IMG_SHAPE,
                                      include_top=False,
                                      weights='imagenet')
        #  its already trained, we just use the features
        self.base_model.trainable = False

        top_model = Sequential()
        top_model.add(GlobalAveragePooling2D())
        top_model.add(
            Dense(128, activation='relu', kernel_regularizer=l2(0.01)))
        top_model.add(Dropout(0.5))
        top_model.add(Dense(64, activation='relu',
                            kernel_regularizer=l2(0.01)))
        top_model.add(Dropout(0.5))
        top_model.add(Dense(64, activation='relu',
                            kernel_regularizer=l2(0.01)))
        top_model.add(Dropout(0.5))
        top_model.add(
            Dense(self.num_classes,
                  activation='softmax',
                  kernel_regularizer=l2(0.01)))
        self.model = Model(input=self.base_model.input,
                           output=top_model(self.base_model.output))
        self.model.compile(optimizer=RMSprop(lr=self.learning_rate),
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])
        print("Model created for id %s" % self.id)
        self.steps_per_epoch = self.train_generator.n // self.batch_size
        self.validation_steps = self.validation_generator.n // self.batch_size
        self.test_steps = self.test_generator.n // self.batch_size

        print("Initializing callbacks")
        self.callbacks = [
            EarlyStopping(monitor='val_loss',
                          min_delta=0,
                          patience=3,
                          verbose=0,
                          mode='auto'),
            ModelCheckpoint(filepath=(self.cnn_dir + "model-weights.h5"),
                            verbose=1,
                            save_best_only=True),
            History(),
            TensorBoard(log_dir="logs/{}".format(self.id))
        ]
        try:
            self.model.load_weights((self.cnn_dir + "model-weights.h5"))
            print("Found weights for %s, loading them and continue training" %
                  self.id)
        except OSError:
            pass
        print("Cnn is ready for train")
Beispiel #11
0
if args.weight is not None:
    weight_type = args.weight

if weight_type == WEIGHT_TYPE_MERGE:
    model_weights_path = 'weights/mobilenet_v2_weights.h5'
elif weight_type == WEIGHT_TYPE_AVA:
    model_weights_path = 'weights/mobilenet_v2_ava_weights.h5'
elif weight_type == WEIGHT_TYPE_TID:
    model_weights_path = 'weights/mobilenet_v2_tid_weights.h5'
else:
    model_weights_path = 'weights/mobilenet_v2_weights.h5'

with tf.device('/CPU:0'):
    base_model = MobileNetV2((None, None, 3),
                             alpha=1.0,
                             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(model_weights_path)

    score_list = []

    for img_path in imgs:
        img = load_img(img_path, target_size=target_size)
        x = img_to_array(img)
        x = np.expand_dims(x, axis=0)
Beispiel #12
0
def convolutional(instruction=None,
                  read_mode=None,
                  preprocess=True,
                  data_path=None,
                  verbose=0,
                  new_folders=True,
                  image_column=None,
                  training_ratio=0.8,
                  augmentation=True,
                  custom_arch=None,
                  pretrained=None,
                  epochs=10,
                  height=None,
                  width=None,
                  save_as_tfjs=None,
                  save_as_tflite=None,
                  generate_plots=True):
    '''
    Body of the convolutional function used that is called in the neural network query
    if the data is presented in images.
    :param many parameters: used to preprocess, tune, plot generation, and parameterizing the convolutional neural network trained.
    :return dictionary that holds all the information for the finished model.
    '''

    # data_path = get_folder_dir()

    logger("Generating datasets for classes")

    if pretrained:
        if not height:
            height = 224
        if not width:
            width = 224
        if height != 224 or width != 224:
            raise ValueError(
                "For pretrained models, both 'height' and 'width' must be 224."
            )

    if preprocess:
        if custom_arch:
            raise ValueError(
                "If 'custom_arch' is not None, 'preprocess' must be set to false."
            )

        read_mode_info = set_distinguisher(data_path, read_mode)
        read_mode = read_mode_info["read_mode"]

        training_path = "/proc_training_set"
        testing_path = "/proc_testing_set"

        if read_mode == "setwise":
            processInfo = setwise_preprocessing(data_path, new_folders, height,
                                                width)
            if not new_folders:
                training_path = "/training_set"
                testing_path = "/testing_set"

        # if image dataset in form of csv
        elif read_mode == "csvwise":
            if training_ratio <= 0 or training_ratio >= 1:
                raise BaseException(f"Test ratio must be between 0 and 1.")
            processInfo = csv_preprocessing(read_mode_info["csv_path"],
                                            data_path, instruction,
                                            image_column, training_ratio,
                                            height, width)

        # if image dataset in form of one folder containing class folders
        elif read_mode == "classwise":
            if training_ratio <= 0 or training_ratio >= 1:
                raise BaseException(f"Test ratio must be between 0 and 1.")
            processInfo = classwise_preprocessing(data_path, training_ratio,
                                                  height, width)

    else:
        training_path = "/training_set"
        testing_path = "/testing_set"
        processInfo = already_processed(data_path)

    num_channels = 3
    color_mode = 'rgb'
    if processInfo["gray_scale"]:
        num_channels = 1
        color_mode = 'grayscale'

    input_shape = (processInfo["height"], processInfo["width"], num_channels)
    input_single = (processInfo["height"], processInfo["width"])
    num_classes = processInfo["num_categories"]
    loss_func = ""
    output_layer_activation = ""

    if num_classes > 2:
        loss_func = "categorical_crossentropy"
        output_layer_activation = "softmax"
    elif num_classes == 2:
        num_classes = 1
        loss_func = "binary_crossentropy"
        output_layer_activation = "sigmoid"

    logger("Creating convolutional neural network dynamically")

    # Convolutional Neural Network

    # Build model based on custom_arch configuration if given
    if custom_arch:
        with open(custom_arch, "r") as f:
            custom_arch_dict = json.load(f)
            custom_arch_json_string = json.dumps(custom_arch_dict)
            model = model_from_json(custom_arch_json_string)

    # Build an existing state-of-the-art model
    elif pretrained:

        arch_lower = pretrained.get('arch').lower()

        # If user specifies value of pretrained['weights'] as 'imagenet', weights pretrained on ImageNet will be used
        if 'weights' in pretrained and pretrained.get('weights') == 'imagenet':
            # Load ImageNet pretrained weights
            if arch_lower == "vggnet16":
                base_model = VGG16(include_top=False,
                                   weights='imagenet',
                                   input_shape=input_shape)
                x = Flatten()(base_model.output)
                x = Dense(4096)(x)
                x = Dropout(0.5)(x)
                x = Dense(4096)(x)
                x = Dropout(0.5)(x)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "vggnet19":
                base_model = VGG19(include_top=False,
                                   weights='imagenet',
                                   input_shape=input_shape)
                x = Flatten()(base_model.output)
                x = Dense(4096)(x)
                x = Dropout(0.5)(x)
                x = Dense(4096)(x)
                x = Dropout(0.5)(x)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "resnet50":
                base_model = ResNet50(include_top=False,
                                      weights='imagenet',
                                      input_shape=input_shape)
                x = Flatten()(base_model.output)
                x = GlobalAveragePooling2D()(base_model.output)
                x = Dropout(0.5)(x)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "resnet101":
                base_model = ResNet101(include_top=False,
                                       weights='imagenet',
                                       input_shape=input_shape)
                x = GlobalAveragePooling2D()(base_model.output)
                x = Dropout(0.5)(x)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "resnet152":
                base_model = ResNet152(include_top=False,
                                       weights='imagenet',
                                       input_shape=input_shape)
                x = GlobalAveragePooling2D()(base_model.output)
                x = Dropout(0.5)(x)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "mobilenet":
                base_model = MobileNet(include_top=False,
                                       weights='imagenet',
                                       input_shape=input_shape)
                x = fine_tuned_model(base_model)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "mobilenetv2":
                base_model = MobileNetV2(include_top=False,
                                         weights='imagenet',
                                         input_shape=input_shape)
                x = fine_tuned_model(base_model)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "densenet121":
                base_model = DenseNet121(include_top=False,
                                         weights='imagenet',
                                         input_shape=input_shape)
                x = fine_tuned_model(base_model)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "densenet169":
                base_model = DenseNet169(include_top=False,
                                         weights='imagenet',
                                         input_shape=input_shape)
                x = fine_tuned_model(base_model)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            elif arch_lower == "densenet201":
                base_model = DenseNet201(include_top=False,
                                         weights='imagenet',
                                         input_shape=input_shape)
                x = fine_tuned_model(base_model)
                pred = Dense(num_classes,
                             activation=output_layer_activation)(x)
                model = Model(base_model.input, pred)
            else:
                raise ModuleNotFoundError("arch \'" + pretrained.get('arch') +
                                          "\' not supported.")

        else:
            # Randomly initialized weights
            if arch_lower == "vggnet16":
                model = VGG16(include_top=True,
                              weights=None,
                              classes=num_classes,
                              classifier_activation=output_layer_activation)
            elif arch_lower == "vggnet19":
                model = VGG19(include_top=True,
                              weights=None,
                              classes=num_classes,
                              classifier_activation=output_layer_activation)
            elif arch_lower == "resnet50":
                model = ResNet50(include_top=True,
                                 weights=None,
                                 classes=num_classes)
            elif arch_lower == "resnet101":
                model = ResNet101(include_top=True,
                                  weights=None,
                                  classes=num_classes)
            elif arch_lower == "resnet152":
                model = ResNet152(include_top=True,
                                  weights=None,
                                  classes=num_classes)
            elif arch_lower == "mobilenet":
                model = MobileNet(include_top=True,
                                  weights=None,
                                  classes=num_classes)
            elif arch_lower == "mobilenetv2":
                model = MobileNetV2(include_top=True,
                                    weights=None,
                                    classes=num_classes)
            elif arch_lower == "densenet121":
                model = DenseNet121(include_top=True,
                                    weights=None,
                                    classes=num_classes)
            elif arch_lower == "densenet169":
                model = DenseNet169(include_top=True,
                                    weights=None,
                                    classes=num_classes)
            elif arch_lower == "densenet201":
                model = DenseNet201(include_top=True,
                                    weights=None,
                                    classes=num_classes)
            else:
                raise ModuleNotFoundError("arch \'" + pretrained.get('arch') +
                                          "\' not supported.")
    else:
        model = Sequential()
        # model.add(
        #     Conv2D(
        #         64,
        #         kernel_size=3,
        #         activation="relu",
        #         input_shape=input_shape))
        # model.add(MaxPooling2D(pool_size=(2, 2)))
        # model.add(Conv2D(64, kernel_size=3, activation="relu"))
        # model.add(MaxPooling2D(pool_size=(2, 2)))
        # model.add(Flatten())
        # model.add(Dense(num_classes, activation="softmax"))
        # model.compile(
        #     optimizer="adam",
        #     loss=loss_func,
        #     metrics=['accuracy'])
        model.add(
            Conv2D(filters=64,
                   kernel_size=5,
                   activation="relu",
                   input_shape=input_shape))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Conv2D(filters=64, kernel_size=3, activation="relu"))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))
        model.add(Conv2D(filters=64, kernel_size=3, activation="relu"))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Flatten())
        model.add(Dense(units=256, activation="relu"))
        model.add(Dropout(0.25))
        model.add(Dense(units=num_classes, activation="softmax"))

    model.compile(optimizer="adam", loss=loss_func, metrics=['accuracy'])

    logger("Located image data")

    if augmentation:
        train_data = ImageDataGenerator(rescale=1. / 255,
                                        shear_range=0.2,
                                        zoom_range=0.2,
                                        horizontal_flip=True)
        test_data = ImageDataGenerator(rescale=1. / 255)

        logger('Dataset augmented through zoom, shear, flip, and rescale')
    else:
        train_data = ImageDataGenerator()
        test_data = ImageDataGenerator()

    logger("->", "Optimal image size identified: {}".format(input_shape))
    X_train = train_data.flow_from_directory(
        data_path + training_path,
        target_size=input_single,
        color_mode=color_mode,
        batch_size=(16 if processInfo["train_size"] >= 16 else 1),
        class_mode=loss_func[:loss_func.find("_")])
    X_test = test_data.flow_from_directory(
        data_path + testing_path,
        target_size=input_single,
        color_mode=color_mode,
        batch_size=(16 if processInfo["test_size"] >= 16 else 1),
        class_mode=loss_func[:loss_func.find("_")])

    if epochs <= 0:
        raise BaseException("Number of epochs has to be greater than 0.")
    logger('Training image model')
    history = model.fit_generator(
        X_train,
        steps_per_epoch=X_train.n // X_train.batch_size,
        validation_data=X_test,
        validation_steps=X_test.n // X_test.batch_size,
        epochs=epochs,
        verbose=verbose)

    models = []
    losses = []
    accuracies = []
    model_data = []

    model_data.append(model)
    models.append(history)

    losses.append(
        history.history["val_loss"][len(history.history["val_loss"]) - 1])
    accuracies.append(
        history.history['val_accuracy'][len(history.history['val_accuracy']) -
                                        1])

    # final_model = model_data[accuracies.index(max(accuracies))]
    # final_hist = models[accuracies.index(max(accuracies))]

    plots = {}
    if generate_plots:
        plots = generate_classification_plots(models[len(models) - 1])

    logger(
        '->', 'Final training accuracy: {}'.format(
            history.history['accuracy'][len(history.history['accuracy']) - 1]))
    logger(
        '->',
        'Final validation accuracy: {}'.format(history.history['val_accuracy'][
            len(history.history['val_accuracy']) - 1]))
    # storing values the model dictionary

    logger("Stored model under 'convolutional_NN' key")

    if save_as_tfjs:
        tfjs.converters.save_keras_model(model, "tfjsmodel")
        logger("Saved tfjs model under 'tfjsmodel' directory")

    if save_as_tflite:
        converter = tf.lite.TFLiteConverter.from_keras_model(model)
        tflite_model = converter.convert()
        open("model.tflite", "wb").write(tflite_model)
        logger("Saved tflite model as 'model.tflite' ")

    clearLog()

    K.clear_session()

    return {
        'id': generate_id(),
        'data_type': read_mode,
        'data_path': data_path,
        'data': {
            'train': X_train,
            'test': X_test
        },
        'shape': input_shape,
        "model": model,
        "plots": plots,
        'losses': {
            'training_loss': history.history['loss'],
            'val_loss': history.history['val_loss']
        },
        'accuracy': {
            'training_accuracy': history.history['accuracy'],
            'validation_accuracy': history.history['val_accuracy']
        },
        'num_classes': (2 if num_classes == 1 else num_classes),
        'data_sizes': {
            'train_size': processInfo['train_size'],
            'test_size': processInfo['test_size']
        }
    }
def train():

    #
    # Data Preparation
    #

    train_datagen = ImageDataGenerator(
        rescale = 1. / 255,
        shear_range = 0.2,
        zoom_range = 0.2,
        horizontal_flip = True
    )
    val_datagen = ImageDataGenerator(
        rescale = 1. / 255.
    )

    generator_kwargs = {
        'target_size': (224, 224),
        'batch_size': BATCH_SIZE,
        'shuffle': True,
        'seed': 43543
    }

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        **generator_kwargs
    )
    val_generator = val_datagen.flow_from_directory(
        val_data_dir,
        **generator_kwargs
    )

    #
    # Model
    #

    model = MobileNetV2(
        include_top = True,
        weights = None,
        classes = N_CLASSES
    )
    model.compile(
        loss = 'categorical_crossentropy',
        optimizer = Adam(),
        metrics = ['accuracy']
    )

    #
    # Checkpoints
    #

    checkpoint = ModelCheckpoint(
        filepath = os.path.join(models_dir, 'weights.{epoch:02d}-{val_loss:.2f}.h5'),
        monitor = 'val_acc',
        verbose = 1,
        save_best_only = True
    )

    tensorboard = TensorBoard(log_dir = log_dir)

    early_stopping = EarlyStopping(
        monitor = 'val_loss',
        patience = 2
    )

    callbacks = [checkpoint, tensorboard, early_stopping]

    #
    # Train
    #

    model.fit_generator(
        train_generator,
        epochs = 1,
        validation_data = val_generator,
        callbacks = callbacks
    )
Beispiel #14
0
from keras import backend as K
import cv2, os
import numpy as np
import time

class_names = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

IMAGE_SIZE = (128, 128)
WEIGHTS_FINAL = 'pretrained.h5'

#define the network
model = MobileNetV2(input_shape=(128, 128, 3),
                    weights=None,
                    include_top=True,
                    classes=10)
model.load_weights(WEIGHTS_FINAL, by_name=True, skip_mismatch=True)

image_dir = 'test_split/airplane/'
image_names = os.listdir(image_dir)
print(image_names[1])
start = time.time()
for name in image_names:

    image = cv2.imread(image_dir + name)
    image = cv2.resize(image, (128, 128))
    np_image_data = np.asarray(image)
    np_final = np.expand_dims(np_image_data, axis=0)

    predict = model.predict(np_final)
def load_MobileNetV2_model():
    # MobileNetV2 Model 224x224
    settings.SITE_MODEL = MobileNetV2(weights="imagenet")
    # # ResNet50 Model 224x224
    # settings.SITE_MODEL = ResNet50(weights="imagenet")
    settings.SITE_GRAPH = tf.get_default_graph()
Beispiel #16
0
def augmented_MobileNetV2_model():
    # Fine tuning MobileNetV2
    patience = 10
    batch_size = 20
    epochs = 50

    # create the base pre-trained model
    base_model = MobileNetV2(weights='imagenet', include_top=False)

    base_model.summary()

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(5, activation='softmax')(x)

    # this is the model we will train
    mobilenetV2_model = Model(inputs=base_model.input, outputs=predictions)

    # we chose to train the top 2 inception blocks, i.e. we will freeze
    # the first 249 layers and unfreeze the rest:
    for layer in mobilenetV2_model.layers[:70]:
        layer.trainable = False
    for layer in mobilenetV2_model.layers[70:]:
        layer.trainable = True

    mobilenetV2_model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(lr=0.0003),
                              metrics=['accuracy'])

    checkpointer = ModelCheckpoint(filepath='../saved_models/weights.best.MobileNetV2.hdf5',
                                   verbose=1, save_best_only=True)

    early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=patience, verbose=0, mode='auto',
                                   baseline=None)

    # Create and configure augmented image generator

    datagen = ImageDataGenerator(width_shift_range=0.1,  # randomly shift images horizontally (10% of total width)
                                 height_shift_range=0.1,  # randomly shift images vertically (10% of total height)
                                 zoom_range=0.2)  # range for random zoom)

    # fit augmented image generator on data
    datagen.fit(train_tensors)

    # train the model
    mobilenetV2_model.fit_generator(
        datagen.flow(train_tensors, train_targets, batch_size=batch_size),
        steps_per_epoch=3 * (train_tensors.shape[0] // batch_size),
        epochs=epochs,
        verbose=2,
        callbacks=[checkpointer, early_stopping],
        validation_data=(valid_tensors, valid_targets),
        class_weight=None,  # Can pay more attention to underrepresented classes
    )

    # load the weights that generated the best validation accuracy
    mobilenetV2_model.load_weights('../saved_models/weights.best.MobileNetV2.hdf5')

    # get index of predicted action for each image in test set
    action_predictions = [np.argmax(mobilenetV2_model.predict(np.expand_dims(tensor, axis=0))) for tensor in
                          test_tensors]

    # report test accuracy
    test_accuracy = 100 * np.sum(np.array(action_predictions) == np.argmax(test_targets, axis=1)) / len(
        action_predictions)
    print('Test accuracy: %.4f%%' % test_accuracy)

    # Compute average prediction time
    computational_times = []

    for i in range(50):
        start = time.time()
        mobilenetV2_model.predict(np.expand_dims(test_tensors[i], axis=0))
        end = time.time()

        elapsed = end - start
        computational_times.append(elapsed)

    print("Average computational time per prediction: {:.3f} ms".format(
        np.sum(computational_times) / len(computational_times) * 1000))
Beispiel #17
0
def train():
    global args
    args = parser.parse_args()
    img_shape = (args.img_size, args.img_size, args.img_channels
                 )  # blame theano
    now_iso = datetime.now().strftime('%Y-%m-%dT%H:%M:%S%z')

    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       rotation_range=45,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True)

    train_generator = train_datagen.flow_from_directory(
        'data/sorted2/train',
        shuffle=True,
        target_size=(args.img_size, args.img_size),
        class_mode='binary',
        batch_size=args.batch_size,
    )

    val_datagen = ImageDataGenerator(rescale=1. / 255)
    val_generator = val_datagen.flow_from_directory(
        'data/sorted2/val',
        shuffle=True,  # otherwise we get distorted batch-wise metrics
        class_mode='binary',
        target_size=(args.img_size, args.img_size),
        batch_size=args.batch_size,
    )

    classes = len(train_generator.class_indices)
    assert classes > 0
    assert classes is len(val_generator.class_indices)
    n_of_train_samples = train_generator.samples
    n_of_val_samples = val_generator.samples

    # Architectures
    base_model = MobileNetV2(input_shape=img_shape,
                             weights='imagenet',
                             include_top=False)
    x = base_model.output  # Recast classification layer
    # x = Flatten()(x)  # Uncomment for Resnet based models
    x = GlobalAveragePooling2D(name='predictions_avg_pool')(
        x)  # comment for RESNET models
    # n_classes; softmax for multi-class, sigmoid for binary
    x = Dense(args.classes,
              activation='sigmoid',
              use_bias=True,
              name='predictions')(x)
    model = Model(inputs=base_model.input, outputs=x)

    # checkpoints
    checkpoint = ModelCheckpoint(
        filepath='./models/MobileNetV2.hdf5',
        verbose=0,
        monitor='val_binary_accuracy',
        save_best_only=True,
    )
    early_stop = EarlyStopping(patience=args.early_stop)
    tensorboard = TensorBoard(log_dir='./logs/MobileNetV2/{}/'.format(now_iso))
    # reduce_lr = ReduceLROnPlateau(factor=0.03, cooldown=0, patience=args.lr_wait, min_lr=0.1e-6)
    callbacks = [checkpoint, tensorboard, checkpoint, early_stop]

    # Calculate class weights
    # weights = class_weight.compute_class_weight('balanced', np.unique(train_generator.classes), train_generator.classes)
    # weights = {0: weights[0], 1: weights[1]}

    # print(model.summary())
    # for i, layer in enumerate(base_model.layers):
    #     print(i, layer.name)
    if args.resume:
        print("==> Resuming")
        model.load_weights(args.resume)
        for layer in model.layers:
            layer.set_trainable = True
    else:
        for layer in base_model.layers:
            layer.set_trainable = False

    # The network is trained end-to-end using Adam with default parameters
    model.compile(
        optimizer=Adam(lr=args.lr, decay=args.decay),
        # optimizer=SGD(lr=args.lr, decay=args.decay,momentum=args.momentum, nesterov=True),
        loss=binary_crossentropy,
        metrics=[binary_accuracy],
    )

    model_out = model.fit_generator(
        train_generator,
        steps_per_epoch=n_of_train_samples // args.batch_size,
        epochs=args.epochs,
        validation_data=val_generator,
        validation_steps=n_of_val_samples // args.batch_size,
        # class_weight=weights,
        workers=args.workers,
        use_multiprocessing=True,
        callbacks=callbacks)
Beispiel #18
0
def mobilenetv2():
    from keras.applications import MobileNetV2
    model = MobileNetV2(weights='imagenet')
    keras2lwnn(model, 'mobilenetv2')
Beispiel #19
0
                               min_delta=0,
                               patience=7,
                               verbose=1)

print('Starting')
stage_1_epoch = 15
dropout_rate = 0.2
input_shape = (224, 224)
n_classes = 5
batch_size = 4
train_annotation_path = 'train_data.txt'
test_annotation_path = 'test_data.txt'
"""
Modify Model to Custom data
"""
base_model = MobileNetV2(weights='imagenet', include_top=True)
# model = Xception(weights='imagenet',  include_top=True)
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(n_classes, activation='softmax', name='probs')(x)
model = Model(inputs=base_model.input, outputs=predictions)
base_model.summary()
"""
Data Prepocessing
"""

with open(train_annotation_path, 'r') as f:
    train_lines = f.readlines()

with open(test_annotation_path, 'r') as f:
    test_lines = f.readlines()
Beispiel #20
0
    model.add(Dense(4096))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    #8
    model.add(Dense(257))
    model.add(Activation('sigmoid'))
    return model


# model = getAlexNet() 
# model.summary()

# model = baseline_model()
# model.summary()

model = MobileNetV2(classes = 257, weights = None)
model.summary()

x = []
n = 2
m = 11
for i in range(0, n):
    x.append(model.layers.pop())

for i in range(m):
    model.layers.pop()

for i in range(0,n):
    model.layers.append(x[n -i -1])

Beispiel #21
0
        save_img('mobilenet_{0:}_{1:}x{1:}.png'.format(layer_name, n), stitched_filters)

def display_activation(activations, col_size, row_size, act_index): 
    """
    https://www.kaggle.com/amarjeet007/visualize-cnn-with-keras
    """
    activation = activations[act_index]
    activation_index=0
    fig, ax = plt.subplots(row_size, col_size, figsize=(row_size*2.5,col_size*1.5))
    for row in range(0,row_size):
        for col in range(0,col_size):
            ax[row][col].imshow(activation[0, :, :, activation_index], cmap='hsv')
            activation_index += 1
    plt.plot()
    plt.show()
        

start_time = time.time()
model = MobileNetV2()
# model.summary()

layer_outputs = [layer.output for layer in model.layers]
activation_model = Model(inputs=model.input, outputs=layer_outputs[1:-6])
imageToUse = cv2.imread('object_detection/img/bounty/gameplay_3.png')[0:705, 0:1280]
input_img_data = cv2.resize(imageToUse, (224, 224))

activations = activation_model.predict(input_img_data.reshape(1, 224, 224, 3))
print(time.time() - start_time)

_draw_filters(activations[1][0], 'test', output_dim=(112, 112))
# display_activation(activations, 4, 4, 128)
from keras.preprocessing.image import load_img
from keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from keras.layers import Dense, Dropout, Flatten, MaxPooling2D
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import os
from keras.models import Model
import random

# importing MobileNet_v2 for higher accuracy
from keras.applications import MobileNetV2
mobile = MobileNetV2(input_shape=(224, 224, 3),
                     include_top=False,
                     weights='imagenet')

#print(mobile.summary())

# layer should not be change
for layer in mobile.layers:
    layer.trainable = False

# Make output layer of mobilenet
op_layer = mobile.output
op_layer = MaxPooling2D(pool_size=(6, 6))(op_layer)
op_layer = Flatten()(op_layer)
op_layer = Dense(128, activation='relu')(op_layer)
op_layer = Dropout((0.5))(op_layer)
op_layer = Dense(2, activation='softmax')(op_layer)
Beispiel #23
0
from keras.applications.resnet50 import ResNet50
from keras import models
# from VESI import layers
from keras import optimizers
from keras import regularizers
from keras.callbacks import ModelCheckpoint, LearningRateScheduler,CSVLogger
from keras.callbacks import ReduceLROnPlateau
from keras.models import Sequential
from keras.layers import Activation, Dropout, Flatten, Dense


target_size = (224,224)
batch_size = 32
#模型
model = Sequential()
model.add(MobileNetV2(include_top = False,pooling='avg', weights = 'imagenet',input_shape=(224, 224, 3)))
model.trainable = False
model.add(Dense(66, activation = 'softmax'))
# model.layers[0].trainable = True #或者这个

# model = Sequential()
# model.add(MobileNetV2(include_top = False, weights = './saved_models/TF_2_%s_model.008.h5',input_shape=(224, 224, 3)))
# model.add(Dense(65, activation = 'softmax'))
# model.layers[0].trainable = True

#数据预处理
train_datagen = ImageDataGenerator(
        rescale=1./255)
        # shear_range=0.2, #实时图片增强
        # zoom_range=0.2,
        # horizontal_flip=True)
    shuffle=True,
    classes=['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE'],
    class_mode='categorical')

test_gen = test_datagen.flow_from_directory(
    './datasets/fingers/test/',
    target_size=(224, 224),
    color_mode='rgb',
    batch_size=nbatch,
    classes=['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE'],
    class_mode='categorical')

print(test_gen.class_indices, "\n")

base_model = MobileNetV2(weights='imagenet',
                         include_top=False,
                         input_shape=(224, 224, 3))
"""
x = base_model.output
x = Conv2D(32, (3, 3), activation='relu')(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
# x = Conv2D(128, (3, 3), activation='relu')(x)
x = Flatten()(x)
x = Dense(512, kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01), activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.4)(x)
preds = Dense(6, activation='softmax')(x)
"""
Beispiel #25
0
trainaug = ImageDataGenerator(rotation_range=30,
                              zoom_range=0.15,
                              width_shift_range=0.2,
                              height_shift_range=0.2,
                              shear_range=0.15,
                              horizontal_flip=True,
                              fill_mode="nearest")

validaug = ImageDataGenerator()

mean = np.array([123.68, 116.779, 103.939], dtype="float32")
trainaug.mean = mean
validaug.mean = mean

prtmodel = MobileNetV2(weights="imagenet",
                       include_top=False,
                       input_tensor=Input(shape=(224, 224, 3)))

i = prtmodel.output
#x = AveragePooling2D(pool_size=(7, 7))(i)
x = Flatten()(i)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(3, activation='softmax')(x)

model = Model(inputs=prtmodel.input, outputs=x)

for layer in prtmodel.layers:
    layer.trainable = False
Beispiel #26
0
def train(x_target, x_ref, y_ref, epoch_num):

    # VGG16読み込み, S network用
    print("Model build...")
    #mobile = VGG16(include_top=False, input_shape=input_shape, weights='imagenet')

    # mobile net読み込み, S network用
    mobile = MobileNetV2(include_top=True,
                         input_shape=input_shape,
                         alpha=alpha,
                         weights='imagenet')

    #最終層削除
    mobile.layers.pop()

    # 重みを固定
    for layer in mobile.layers:
        if layer.name == "block_13_expand":  # "block5_conv1": for VGG16
            break
        else:
            layer.trainable = False

    model_t = Model(inputs=mobile.input, outputs=mobile.layers[-1].output)

    # R network用 Sと重み共有
    model_r = Network(inputs=model_t.input,
                      outputs=model_t.output,
                      name="shared_layer")

    #Rに全結合層を付ける
    prediction = Dense(classes, activation='softmax')(model_t.output)
    model_r = Model(inputs=model_r.input, outputs=prediction)

    #コンパイル
    optimizer = SGD(lr=5e-5, decay=0.00005)
    model_r.compile(optimizer=optimizer, loss="categorical_crossentropy")
    model_t.compile(optimizer=optimizer, loss=original_loss)

    model_t.summary()
    model_r.summary()

    print("x_target is", x_target.shape[0], 'samples')
    print("x_ref is", x_ref.shape[0], 'samples')

    ref_samples = np.arange(x_ref.shape[0])
    loss, loss_c = [], []

    print("training...")

    #学習
    for epochnumber in range(epoch_num):
        x_r, y_r, lc, ld = [], [], [], []

        #ターゲットデータシャッフル
        np.random.shuffle(x_target)

        #リファレンスデータシャッフル
        np.random.shuffle(ref_samples)
        for i in range(len(x_target)):
            x_r.append(x_ref[ref_samples[i]])
            y_r.append(y_ref[ref_samples[i]])
        x_r = np.array(x_r)
        y_r = np.array(y_r)

        for i in range(int(len(x_target) / batchsize)):

            #batchsize分のデータロード
            batch_target = x_target[i * batchsize:i * batchsize + batchsize]
            batch_ref = x_r[i * batchsize:i * batchsize + batchsize]
            batch_y = y_r[i * batchsize:i * batchsize + batchsize]

            #target data
            #学習しながら、損失を取得
            lc.append(
                model_t.train_on_batch(batch_target,
                                       np.zeros((batchsize, feature_out))))

            #reference data
            #学習しながら、損失を取得
            ld.append(model_r.train_on_batch(batch_ref, batch_y))

        loss.append(np.mean(ld))
        loss_c.append(np.mean(lc))

        if (epochnumber + 1) % 5 == 0:
            print("epoch:", epochnumber + 1)
            print("Descriptive loss:", loss[-1])
            print("Compact loss", loss_c[-1])

    #結果グラフ
    #plt.plot(loss,label="Descriptive loss")
    #plt.xlabel("epoch")
    #plt.legend()
    #plt.show()

    #plt.plot(loss_c,label="Compact loss")
    #plt.xlabel("epoch")
    #plt.legend()
    #plt.show()

    return model_t
Beispiel #27
0
import os

from keras.applications import MobileNetV2
from keras.applications.mobilenet_v2 import preprocess_input
from keras.models import Model
from keras.layers import Dropout, Dense, Input, Lambda
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau, EarlyStopping

df = pd.read_csv('processed_table.csv')
df['label'] = (df['year'] - 2010) / 5  # .apply(lambda x: np.log(x))
df['filename'] = df['ad_id'].apply(lambda x: str(x) + '.jpg')

mobilenet = MobileNetV2(input_shape=(224, 224, 3),
                        include_top=False,
                        pooling='avg')
for layer in mobilenet.layers:
    layer.trainable = False

new_input = Input(shape=(224, 224, 3))
x = Lambda(lambda y: (y / 128) - 1)(new_input)
x = mobilenet(x)
x = Dropout(rate=0.2)(x)
x = Dense(256, activation='tanh')(x)
x = Dropout(rate=0.2)(x)
x = Dense(1)(x)

model = Model(input=new_input, output=x)
print(model.summary())
Beispiel #28
0
import av
import numpy as np  # linear algebra

from keras import backend as K
from keras.applications import MobileNetV2, imagenet_utils
from PIL import Image, ImageFile
from sklearn.preprocessing import LabelEncoder
from sklearn.utils.extmath import softmax
from tensorflow.keras.applications.mobilenet_v2 import (MobileNetV2,
                                                        preprocess_input)
from tensorflow.keras.layers import (Dense, GlobalAveragePooling2D,
                                     GlobalMaxPooling2D, Lambda, Softmax)
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing import image

model_imagenet = MobileNetV2(weights='imagenet')


def process_video(vf):
    video_dict_list = []
    try:
        container = av.open(vf)
        stream = container.streams.video[0]
        stream.codec_context.skip_frame = 'NONKEY'
        folder, filename = os.path.split(vf)
        #fn = filename.replace(".mp4","").strip()
        frames_list = []
        frame_times = []
        ts_list = []
        for frame in container.decode(stream):
            # try:
Beispiel #29
0
from keras.layers import Dense, Flatten
from keras.applications import MobileNetV2
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator

#####################
# STEP - 2: The Model
#####################

# Making the model
model = Sequential()

# For this model I'm using InceptionResNetV2
# I'll use imagenet weights here also
mobile = MobileNetV2(include_top=False,
                     weights="imagenet",
                     input_shape=(128, 128, 3),
                     pooling="max")

# Adding the mobile model and configuting the output layer
model.add(mobile)
model.add(Dense(units=102, activation="softmax"))

# Compiling the model
model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.RMSprop(lr=2e-5),
              metrics=['accuracy'])

# Printing the summary of the model
print(model.summary())

############################
Beispiel #30
0
                  metrics=['accuracy'])

    return model


config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

BATCH_SIZE = 32

trainGen = HDF5DatasetGenerator("hdf5/train1.hdf5", BATCH_SIZE, classes=5)
valGen = HDF5DatasetGenerator("hdf5/val.hdf5", BATCH_SIZE, classes=5)

mobilenet = MobileNetV2(weights="imagenet",
                        include_top=False,
                        input_shape=(224, 224, 3))
model = build_model(mobilenet)

labels = np_utils.to_categorical(valGen.db["labels"][:-1], 5)
multilabels = np.empty(labels.shape, dtype=labels.dtype)
multilabels[:, 4] = labels[:, 4]
for i in range(3, -1, -1):
    multilabels[:, i] = np.logical_or(labels[:, i], multilabels[:, i + 1])
labels = multilabels
kappa_metrics = Metrics()
kappa_metrics.my(valdata=(valGen.db["images"][:-1], labels),
                 name="MobileNetV2")
path = os.path.sep.join(["output", "{}.png".format(os.getpid())])
mycallback = TrainingMonitor(path)