def test_pipeline(self):
        """ Pipeline should provide correct function composition """
        img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))

        xcpt_model = Xception(weights="imagenet")
        stages = [('spimage', gfac.buildSpImageConverter(SparkMode.RGB_FLOAT32)),
                  ('xception', GraphFunction.fromKeras(xcpt_model))]
        piped_model = GraphFunction.fromList(stages)

        for fpath in img_fpaths:
            target_size = tuple(xcpt_model.input.shape.as_list()[1:-1])
            img = load_img(fpath, target_size=target_size)
            img_arr = np.expand_dims(img_to_array(img), axis=0)
            img_input = xcpt.preprocess_input(img_arr)
            preds_ref = xcpt_model.predict(img_input)

            spimg_input_dict = imageArrayToStruct(img_input).asDict()
            spimg_input_dict['data'] = bytes(spimg_input_dict['data'])
            with IsolatedSession() as issn:
                # Need blank import scope name so that spimg fields match the input names
                feeds, fetches = issn.importGraphFunction(piped_model, prefix="")
                feed_dict = dict((tnsr, spimg_input_dict[tfx.op_name(issn.graph, tnsr)]) for tnsr in feeds)
                preds_tgt = issn.run(fetches[0], feed_dict=feed_dict)
                # Uncomment the line below to see the graph
                # tfx.write_visualization_html(issn.graph,
                #                              NamedTemporaryFile(prefix="gdef", suffix=".html").name)

            self.assertTrue(np.all(preds_tgt == preds_ref))
Beispiel #2
0
auto_lr = ReduceLROnPlateau(monitor='val_loss',
                            factor=0.1,
                            patience=3,
                            verbose=0,
                            mode='auto',
                            epsilon=0.0001,
                            cooldown=0,
                            min_lr=0)

if os.path.exists('dog_single_xception.h5'):
    model = load_model('dog_single_xception.h5')
else:
    # create the base pre-trained model
    input_tensor = Input(shape=(299, 299, 3))
    base_model = Xception(include_top=True,
                          weights='imagenet',
                          input_tensor=None,
                          input_shape=None)

    base_model.layers.pop()
    base_model.outputs = [base_model.layers[-1].output]
    base_model.layers[-1].outbound_nodes = []
    base_model.output_layers = [base_model.layers[-1]]

    img1 = Input(shape=(299, 299, 3), name='img_1')

    feature1 = base_model(img1)

    # let's add a fully-connected layer
    category_predict1 = Dense(100, activation='softmax',
                              name='ctg_out_1')(Dropout(0.5)(feature1))
Beispiel #3
0
def transfer_learning_model(train_x, train_y, val_x, val_y, test_x, test_y, num_class, epoch, batch_size, model_type, reshape_size, l1_weight, l2_weight):

    print(train_x.shape)
    print(type(train_x))
    print('\n')
    print(train_x[0].shape)

    # change label into one hot
    train_y = keras.utils.to_categorical(train_y, num_classes=num_class)
    val_y = keras.utils.to_categorical(val_y, num_classes=num_class)
    #test_y = keras.utils.to_categorical(test_y, num_classes=num_class)
    print(test_y)

    if model_type == 'vgg16':
        pre_trained = VGG16(
            weights='imagenet',
            include_top=False, # True if we want to add Fully Connected Layer at the Last (False)
            input_shape=reshape_size + (3,)
        )
        pre_trained.trainable = False  # False if we want to freeze the weight

    elif model_type == 'vgg19':
        pre_trained = VGG19(
            weights='imagenet',
            include_top=False,
            input_shape=reshape_size+ (3,)
        )

    elif model_type == 'resnet101':
        pre_trained = ResNet101(
            weights='imagenet',
            include_top = False
            input_shape = reshape_size + (3,)
        )

    elif model_type == 'resnet50':
        pre_trained = ResNet50(
            weights='imagenet'
            include_top = False,
            input_shape = reshape_size + (3,)
        )

    elif model_type == 'xception':
        pre_trained = Xception(
            weights='imagenet',
            include_top=False,
            input_shape=reshape_size + (3,)
        )

    elif model_type == 'inception_v3':
        pre_trained = InceptionV3(
            weights='imagenet',
            include_top=False,
            input_shape=reshape_size + (3,)
        )

    elif model_type == 'mobilenet':
        pre_trained = MobileNet(
            weights='imagenet',
            include_top=False,
            input_shape=reshape_size + (3,)
        )

    #pre_trained.summary()
    # Add Fine-Tuning Layers
    finetune_model = models.Sequential()
    finetune_model.add(pre_trained)
    
    if model_type == 'resnet50':
        pass

    else:
        finetune_model.add(layers.Flatten())

    finetune_model.add(layers.Dense(num_class*128,# activation='relu',
        kernel_regularizer=regularizers.l1_l2(
            l1=l1_weight,
            l2=l2_weight)
        ))
    finetune_model.add(BatchNormalization())
    finetune_model.add(Activation('relu'))
    #finetune_model.add(layers.Dense(num_class*64, activation='relu'))
    
    finetune_model.add(layers.Dense(num_class*32,# activation='relu',
        kernel_regularizer=regularizers.l1_l2(
                l1=l1_weight,
                l2=l2_weight)
        ))
    finetune_model.add(BatchNormalization())
    finetune_model.add(Activation('relu'))
    
    #finetune_model.add(layers.Dense(num_class*16, activation='relu'))
    
    finetune_model.add(layers.Dense(num_class*8,# activation='relu',
        kernel_regularizer=regularizers.l1_l2(
                    l1=l1_weight,
                    l2=l2_weight)    
        ))
    finetune_model.add(BatchNormalization())
    finetune_model.add(Activation('relu'))
    
    finetune_model.add(layers.Dense(num_class, activation='softmax')) # Final Activation

    #finetune_model.summary()

    # Compile
    finetune_model.compile(
        loss = 'categorical_crossentropy',
        optimizer = 'adam',
        metrics=['acc']
    )

    history = finetune_model.fit(
        train_x,
        train_y,
        epochs=epoch,
        batch_size = batch_size,
        validation_data = (val_x, val_y)
    )

    # Test Performance
    '''
    TODO: Result 해결하는데 이슈가 있음 ### !
    '''
    y_pred = finetune_model.predict(test_x) #np.argmax
    y_pred = np.argmax(y_pred, axis=1)
    print('>> Predicted Results')
    print(y_pred)
    
    #test_y = np.argmax(test_y, axis=1)
    print('>> Ground Truth')
    print(test_y)

    accuracy = accuracy_score(test_y, y_pred)
    precision, recall, f1_score, _ = precision_recall_fscore_support(test_y, y_pred, average='micro')
    
    print(">> Test Performance <<")
    print('Acc: ', accuracy)
    print('Precision: ', precision)
    print('Recall: ', recall)
    print('F1 Score: ', f1_score)
    
Beispiel #4
0
def merge(classes, epochs, steps_per_epoch, validation_steps, input_shape):
    # 加载数据
    train_batches, valid_batches = load_data(input_shape)

    input_shape += (3, )

    input_layer = Input(shape=input_shape)
    dense = DenseNet121(include_top=False,
                        pooling='avg',
                        input_shape=input_shape)
    xception = Xception(include_top=False,
                        pooling='avg',
                        input_shape=input_shape)

    dense = dense(input_layer)
    xception = xception(input_layer)

    # 把top1_model和top2_model连接起来
    t = keras.layers.Concatenate(axis=1)([dense, xception])
    if classes == 1:
        print("二元分类")
        top_model = Dense(units=classes, activation="sigmoid")(t)
        model = Model(inputs=input_layer, outputs=top_model)
        asdl = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss='binary_crossentropy',
                      optimizer=asdl,
                      metrics=['accuracy'])
    else:
        print("多分类")
        top_model = Dense(units=classes, activation="softmax")(t)
        model = Model(inputs=input_layer, outputs=top_model)
        asdl = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss='categorical_crossentropy',
                      optimizer=asdl,
                      metrics=['accuracy'])
    # 保存模型
    out_dir = "weights/"
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    filepath = "weights/merge_{epoch:04d}.h5"
    # 中途训练效果提升, 则将文件保存, 每提升一次, 保存一次
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_acc',
                                 verbose=0,
                                 save_best_only=True,
                                 mode='max',
                                 period=2)
    # 学习率调整
    lr_reduce = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.5,
                                  patience=10,
                                  verbose=1,
                                  min_lr=0.000005,
                                  mode="min")
    # 早停
    earlystopping = EarlyStopping(monitor='val_loss',
                                  patience=15,
                                  verbose=1,
                                  mode='min')
    # 保存训练过程
    log_dir = "logs/"
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logfile = "logs/merge.csv"
    log = keras.callbacks.CSVLogger(logfile, separator=',', append=False)
    loggraph = keras.callbacks.TensorBoard(log_dir='./logs',
                                           histogram_freq=0,
                                           write_graph=True,
                                           write_images=True)

    callbacks_list = [checkpoint, lr_reduce, log]

    # 训练
    model.fit_generator(train_batches,
                        steps_per_epoch=steps_per_epoch,
                        validation_data=valid_batches,
                        validation_steps=validation_steps,
                        epochs=epochs,
                        verbose=2,
                        callbacks=callbacks_list,
                        workers=16,
                        max_queue_size=20)
Beispiel #5
0
fig, m_axs = plt.subplots(4, 4, figsize=(16, 16))
for (c_x, c_y, c_ax) in zip(t_x, t_y, m_axs.flatten()):
    c_ax.imshow(c_x[:, :, 0], cmap='bone', vmin=-1.5, vmax=1.5)
    c_ax.set_title(', '.join([
        n_class for n_class, n_score in zip(all_labels, c_y) if n_score > 0.5
    ]))
    c_ax.axis('off')
plt.show()

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

xception_model = Xception(input_shape=t_x.shape[1:],
                          weights=None,
                          include_top=False)
multi_disease_model = Sequential()
multi_disease_model.add(xception_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
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import numpy as np
import argparse
import cv2
##################################################################
## initialize the input image shape (224x224 pixels) along with the pre-processing function
# inputShape = (224, 224)
# preprocess = imagenet_utils.preprocess_input
## inception, xception 用下面的
inputShape = (299, 299)
preprocess = preprocess_input
##################################################################
## loading model
model = Xception(weights="imagenet")
##################################################################
## loading image; 尺寸要和上面的一样
image = load_img("/Users/coder352/github/jImage/Dream_Afar/Acanalonia conica planthopper.jpg", target_size=inputShape)
image = img_to_array(image)  # a NumPy array of shape (inputShape[0], inputShape[1], 3)
image = np.expand_dims(image, axis=0)  # we need to expand the dimension by making the shape (1, inputShape[0], inputShape[1], 3)
image = preprocess(image)  # pre-process the image using the appropriate function based on the model that has been loaded (i.e., mean subtraction, scaling, etc.)

preds = model.predict(image)
P = imagenet_utils.decode_predictions(preds)

# loop over the predictions and display the rank-5 predictions +
# probabilities to our terminal
for (i, (imagenetID, label, prob)) in enumerate(P[0]):
	print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100))
Beispiel #7
0
                                                    stratify=y)
X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                  y_train,
                                                  test_size=0.25,
                                                  random_state=42,
                                                  stratify=y_train)
# print(X_train)
# print(y_train)
# print(X_test)
# print(y_test)

## define the model (preset weights)
print('[INFO] defining model...')

## create the pre-trained base model
base_model = Xception(include_top=True, weights=None, classes=40)
base_model.load_weights(
    'frogsumimodels/Xception_species_distort_TF000/Xception.101.0.952.hdf5')
base_model.layers.pop()
base_model_layers = base_model.output
predictions = Dense(len(labeltonumber),
                    activation='softmax')(base_model_layers)

## create the model to train with the correct number of output
model = Model(inputs=base_model.input, outputs=predictions)

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

## values from Olafenwa and Olafenva - 2018 and
## https://machinelearningmastery.com/evaluate-performance-deep-learning-models-keras/
import keras
from keras.applications import Xception
from keras.layers import Dense, Flatten, Dropout
from keras.optimizers import RMSprop
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator

base_dir = "D:/dataset/images/kaggle/"
train_dir = os.path.join(base_dir, "train")
test_dir = os.path.join(base_dir, 'test')
validation_dir = os.path.join(base_dir, 'validation')
num_classes = 12
import matplotlib.pyplot as plt

conv_base = Xception(include_top=False,
                     weights='imagenet',
                     input_shape=(71, 71, 3))
print(conv_base.summary())

datagen = ImageDataGenerator(rescale=1. / 255,
                             rotation_range=0.2,
                             width_shift_range=0.2,
                             height_shift_range=0.2,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode='nearest')

train_gen = datagen.flow_from_directory(train_dir,
                                        target_size=(71, 71),
                                        batch_size=20,
Beispiel #9
0
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')

train_cats_dir = os.path.join(train_dir, 'cats')
train_dogs_dir = os.path.join(train_dir, 'dogs')

validation_cats_dir = os.path.join(validation_dir, 'cats')
validation_dogs_dir = os.path.join(validation_dir, 'dogs')

test_cats_dir = os.path.join(test_dir, 'cats')
test_dogs_dir = os.path.join(test_dir, 'dogs')

xc_base = Xception(input_shape=(150, 150, 3),
                   weights='imagenet',
                   include_top=False)
xc_base.trainable = False

model = models.Sequential()
# model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3)))
# model.add(layers.MaxPooling2D((2, 2)))
# model.add(layers.Conv2D(64, (3, 3), activation='relu'))
# model.add(layers.MaxPooling2D((2, 2)))
# model.add(layers.Conv2D(128, (3, 3), activation='relu'))
# model.add(layers.MaxPooling2D((2, 2)))
# model.add(layers.Conv2D(128, (3, 3), activation='relu'))
# model.add(layers.MaxPooling2D((2, 2)))

model.add(xc_base)
model.add(layers.Flatten())
Beispiel #10
0
def xception():
    model = Xception(weights="imagenet")
    graph = tf.get_default_graph()
    return model, graph
Beispiel #11
0
val_datagen = ImageDataGenerator(rescale=1. / 255)

val_generator = val_datagen.flow_from_directory('./data/val/',
                                                target_size=(299, 299),
                                                batch_size=BATCH_SIZE,
                                                class_mode='categorical',
                                                shuffle=True)

# pretrain dense layer
# to avoid large gradient to destroy the pretrained model
# build model
tensorboard = TensorBoard('./log/new_xception')

basic_model = Xception(input_shape=(299, 299, 3),
                       include_top=False,
                       weights='imagenet',
                       pooling='avg')

for layer in basic_model.layers:
    layer.trainable = False

# build top
x = basic_model.output
x = Dropout(0.5)(x)
pred = Dense(12, activation='softmax')(x)

model = Model(inputs=basic_model.input, outputs=pred)
model.compile(optimizer=optimizers.RMSprop(1e-3),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
Beispiel #12
0
from preprocess import train, val, train_steps, val_steps
from keras.applications import Xception
from keras import layers, models
from keras import callbacks, optimizers
# Uneven class distribution
class_weight = {
    train.class_indices['normal']: 1,
    train.class_indices['glaucoma']: 1.86
}

# Using pretrained Xception Net as Convolutional feature extractor
conv_base = Xception(include_top=False,
                     weights='imagenet',
                     input_shape=(299, 299, 3))

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizers.RMSprop(1e-5),
              'binary_crossentropy',
              metrics=['accuracy'])
model.load_weights('/model/best_weights.h5')

conv_base.trainable = True
for layer in conv_base.layers:
    if layer.name != 'block14_sepconv2':
        layer.trainable = False
# print(y_test)
X_test_mean = X_test - mean

if modus == 'save':

    ## for tests use data_{}_{}.test.npz
    ## /home/stine/repositories
    # filename = 'npz/img_attr_{}_{}.npz'.format(mode, resize)
    # img_attr = load_img_attr_data(filename)

    ## creates model used for training and loads weights
    print('[INFO] create model and load weights ...')
    weights = path_weights + weightfile
    # with tf.device('/cpu:0'):
    model = Xception(include_top=True,
                     weights=weights,
                     classes=len(labeltonumber))

    # model = multi_gpu_model(model, gpus=2)
    # model.load_weights(weights)

    ## changes last layer activation from softmax to linear to improve attribution results
    print('[INFO] change activations of last layer to linear ...')
    layer_idx = utils.find_layer_idx(model, 'predictions')
    model.layers[layer_idx].activation = activations.linear
    model = utils.apply_modifications(model)

    print(model.summary())
    print(model.input)

    print('[INFO] calculate cam ...')
Beispiel #14
0
def create_model():

    backbone = Xception(input_shape=(400, 400, 3),
                        weights='imagenet',
                        include_top=False)
    input_ = backbone.input

    conv4 = backbone.layers[121].output  #(None, 25, 25, 1024)
    conv4 = LeakyReLU(alpha=0.1)(conv4)
    pool4 = MaxPooling2D((2, 2))(conv4)  #(None, 12, 12, 1024)
    pool4 = Dropout(.5)(pool4)

    decoder4 = decoder_block(pool4, 512, (3, 3))  #(None, 12, 12, 512)
    deconv4 = Conv2DTranspose(256, (3, 3), strides=(2, 2),
                              padding="valid")(decoder4)  #(None, 25, 25, 256)

    conv4 = decoder_block(conv4, 256, activation=False)  #(None, 25, 25, 256)
    uconv4 = concatenate([deconv4, conv4])  #(None, 25, 25, 512)
    uconv4 = LeakyReLU(alpha=0.1)(uconv4)
    uconv4 = Dropout(.5)(uconv4)

    decoder3 = decoder_block(uconv4, 256, (3, 3))  #(None, 25, 25, 256)
    deconv3 = Conv2DTranspose(128, (3, 3), strides=(2, 2),
                              padding="same")(decoder3)  #(None, 50, 50, 128)
    conv3 = backbone.layers[31].output  #(None, 50, 50, 728)
    conv3 = LeakyReLU(alpha=0.1)(conv3)
    conv3 = decoder_block(conv3, 128, activation=False)  #(None, 50, 50, 128)

    uconv3 = concatenate([deconv3, conv3])  #(None, 50, 50, 256)
    uconv3 = Dropout(.5)(uconv3)
    uconv3 = LeakyReLU(alpha=0.1)(uconv3)

    decoder2 = decoder_block(uconv3, 128, (3, 3))  #(None, 50, 50, 128)
    deconv2 = Conv2DTranspose(64, (3, 3), strides=(2, 2),
                              padding="same")(decoder2)  #(None, 100, 100, 64)
    conv2 = backbone.layers[21].output  #(None, 99, 99, 256)
    conv2 = LeakyReLU(alpha=0.1)(conv2)
    conv2 = ZeroPadding2D(((1, 0), (1, 0)))(conv2)  #(None, 100, 100, 256)
    conv2 = decoder_block(conv2, 64, activation=False)  #(None, 100, 100, 64)

    uconv2 = concatenate([deconv2, conv2])  #(None, 100, 100, 128)
    uconv2 = Dropout(.1)(uconv2)
    uconv2 = LeakyReLU(alpha=0.1)(uconv2)

    decoder1 = decoder_block(uconv2, 64, (3, 3))  #(None, 100, 100, 64)
    deconv1 = Conv2DTranspose(32, (3, 3), strides=(2, 2),
                              padding="same")(decoder1)  #(None, 200, 200, 32)
    conv1 = backbone.layers[11].output  #(None, 197, 197, 128)
    conv1 = LeakyReLU(alpha=0.1)(conv1)
    conv1 = ZeroPadding2D(((3, 0), (3, 0)))(conv1)  #(None, 200, 200, 128)
    conv1 = decoder_block(conv1, 32, activation=False)  #(None, 200, 200, 32)

    uconv1 = concatenate([deconv1, conv1])  #(None, 200, 200, 64)
    uconv1 = Dropout(.1)(uconv1)
    uconv1 = LeakyReLU(alpha=0.1)(uconv1)

    decoder0 = decoder_block(uconv1, 32, (3, 3))  #(None, 200, 200, 32)
    deconv0 = Conv2DTranspose(16, (3, 3), strides=(2, 2),
                              padding="same")(decoder0)  #(None, 400, 400, 16)
    conv0 = decoder_block(input_, 16, activation=False)  #(None, 400, 400, 16)

    uconv0 = concatenate([deconv0, conv0])  #(None, 400, 400, 32)
    uconv0 = Dropout(.4)(uconv0)
    uconv0 = LeakyReLU(alpha=0.1)(uconv0)

    final_decoder = decoder_block(uconv0, 16, (3, 3))  #(None, 400, 400, 16)
    final_decoder = Dropout(.25)(final_decoder)
    output_ = Conv2D(1, (1, 1), padding="same",
                     activation="sigmoid")(final_decoder)  #(None, 400, 400, 1)

    model = Model(inputs=input_, outputs=output_)
    model.name = 'ures_xception'

    return model
Beispiel #15
0
    return img


imgs_dir = "/home/alex/Downloads/ILSVRC/Data/DET/test/"
vecs_path = 'imagenet_vectors.npy'

img_paths = sorted(glob('%s/*.JPEG' % imgs_dir))
fp_paths = open('imagenet_paths.txt', 'w')

dim = 224
batch_size = 500
imgs_batch = np.zeros((batch_size, dim, dim, 3))

# Instantiate model and chop off some layers.
vector_layer = "avg_pool"
m1 = Xception()
m2 = Model(inputs=m1.input, outputs=m1.get_layer(vector_layer).output)

vecs = np.zeros((len(img_paths), m2.output_shape[-1]))

for i in range(0, len(img_paths), batch_size):

    for j in range(batch_size):
        imgs_batch[j] = get_img(img_paths[i + j], dim)

    imgs_batch = preprocess_input(imgs_batch, mode='tf')
    prds_batch = m2.predict(imgs_batch)
    vecs[i:i + batch_size] = prds_batch
    fp_paths.write('\n'.join(img_paths[i:i + batch_size]) + '\n')

    print('%d-%d %.3lf %.3lf %.3lf' %
Beispiel #16
0
from keras.applications import VGG16, VGG19
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, BatchNormalization, Activation
from keras.applications import MobileNet, MobileNetV2, DenseNet121, DenseNet169, DenseNet201
from keras.applications import NASNetLarge, NASNetMobile
from keras.applications import Xception, ResNet101
from keras.optimizers import Adam
from keras.applications import ResNet101V2, ResNet152, ResNet152V2, ResNet50, ResNet50V2, InceptionV3, InceptionResNetV2

model = VGG19()
model = Xception()
model = ResNet101()
model = ResNet101V2()
model = ResNet152()
model = ResNet152V2()
model = ResNet50()
model = ResNet50V2()
model = InceptionV3()
model = InceptionResNetV2()
model = MobileNet()
model = MobileNetV2()
model = DenseNet121()
model = DenseNet169()
model = DenseNet201()
model = NASNetLarge()
model = NASNetMobile()

vgg16 = VGG16(
    weights=None,
    include_top=False,
    classes=10,
print('****************')
classes_name = [0 for i in range(NUM_CLASSES)]
for cls, idx in train_batches.class_indices.items():
    print('Class #{} = {}'.format(idx, cls))
    classes_name[idx] = cls
print(classes_name)
print('****************')

# build our classifier model based on pre-trained InceptionResNetV2:
# 1. we don't include the top (fully connected) layers of InceptionResNetV2
# 2. we add a DropOut layer followed by a Dense (fully connected)
#    layer which generates softmax class score for each class
# 3. we compile the final model using an Adam optimizer, with a
#    low learning rate (since we are 'fine-tuning')
base_model = Xception(include_top=False,
                      weights='imagenet',
                      input_tensor=None,
                      input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3))
for layer in base_model.layers:
    layer.trainable = True

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dropout(0.5)(x)

# and a logistic layer -- let's say we have 200 classes
predictions = Dense(NUM_CLASSES, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
print(len(model.layers))
Beispiel #18
0
        deep_learning_feature_file_name = 'feature_data/' + dataset_name + '_' + model_name + '_image_tag_feature.npy'

        if model_name == 'VGG16':
            model = VGG16(weights='imagenet', include_top=True)
            im_size = 224
        elif model_name == 'VGG19':
            model = VGG19(weights='imagenet', include_top=True)
            im_size = 224
        elif model_name == 'ResNet50':
            model = ResNet50(weights='imagenet', include_top=True)
            im_size = 224
        elif model_name == 'InceptionV3':
            model = InceptionV3(weights='imagenet', include_top=True)
            im_size = 299
        elif model_name == 'Xception':
            model = Xception(weights='imagenet', include_top=True)
            im_size = 299
        #print(model.summary())

        for im in os.listdir(im_path):
            print(im)
            try:
                img = Image.load_img(im_path + im,
                                     target_size=(im_size, im_size))
            except:
                print(im_path + im)
                continue
            x = Image.img_to_array(img)
            x = np.expand_dims(x, axis=0)

            # im_last_layer_feature = image_feature_extraction(x, model)
Beispiel #19
0
def xception(classes, epochs, steps_per_epoch, validation_steps, input_shape):
    # 加载数据
    train_batches, valid_batches = load_data(input_shape)
    input_shape += (3, )
    base_model = Xception(include_top=False, input_shape=input_shape)
    x = base_model.output
    x = GlobalAveragePooling2D()(
        x)  # GlobalAveragePooling2D 将 MxNxC 的张量转换成 1xC 张量,C是通道数
    x = Dropout(0.5)(x)
    if classes == 1:
        print("二元分类")
        outputs = Dense(classes, activation='sigmoid')(x)
        model = Model(base_model.inputs, outputs)

        model.compile(optimizer=keras.optimizers.Adam(lr=0.0001),
                      metrics=['accuracy'],
                      loss='binary_crossentropy')
    else:
        print("多分类")
        outputs = Dense(classes, activation='softmax')(x)
        model = Model(base_model.inputs, outputs)

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

    # 保存模型
    out_dir = "../weights/"
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    filepath = "../weights/xception_{epoch:04d}.h5"
    # 中途训练效果提升, 则将文件保存, 每提升一次, 保存一次
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_acc',
                                 verbose=0,
                                 save_best_only=True,
                                 mode='max')
    # 学习率调整
    lr_reduce = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.5,
                                  patience=5,
                                  verbose=1,
                                  min_lr=0.00000001,
                                  mode="min")
    # 早停
    earlystopping = EarlyStopping(monitor='val_loss',
                                  patience=15,
                                  verbose=1,
                                  mode='min')
    # 保存训练过程
    log_dir = "../logs/"
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logfile = "../logs/xception.csv"
    log = keras.callbacks.CSVLogger(logfile, separator=',', append=False)
    loggraph = keras.callbacks.TensorBoard(log_dir='./logs',
                                           histogram_freq=0,
                                           write_graph=True,
                                           write_images=True)

    callbacks_list = [checkpoint, lr_reduce, log, earlystopping]

    # 训练
    model.fit_generator(train_batches,
                        steps_per_epoch=steps_per_epoch,
                        validation_data=valid_batches,
                        validation_steps=validation_steps,
                        epochs=epochs,
                        verbose=2,
                        callbacks=callbacks_list,
                        workers=16,
                        max_queue_size=20)
        batch_size=10,
        shuffle=False,
        class_mode="categorical",
        target_size=(299, 299))
    test_generator = test_datagen.flow_from_dataframe(
        dataframe=df_test,
        directory=img_path,
        x_col="Überschrift der Spalte mit den Dateinamen",
        y_col="Überschrift der Spalte mit den Schadensklassen",
        classes=['Klasse 1', 'Klasse 2', 'Klasse 3', 'Klasse 4', 'Klasse 5'],
        batch_size=9,
        shuffle=False,
        class_mode='categorical',
        target_size=(299, 299))
    inp = layers.Input([299, 299, 3])
    model_1 = Xception(weights='imagenet', include_top=False, input_tensor=inp)

    model_1.trainable = True

    set_trainable = False
    for layer in model_1.layers:
        if layer.name == 'block14_sepconv2':
            set_trainable = True
        if set_trainable:
            layer.trainable = True
        else:
            layer.trainable = False

    x = layers.Flatten()(model_1.output)
    x = layers.Dropout(0.3)(x)
    x = layers.Dense(256, activation='relu')(x)
from keras.layers import Conv2D
from keras.layers import SeparableConv2D
from keras.layers import MaxPooling2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import GlobalMaxPooling2D
from keras.engine.topology import get_source_inputs
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import _obtain_input_shape
from keras.applications import Xception
from keras.applications.xception import preprocess_input

TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5'
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'


if __name__ == '__main__':
    model = Xception(include_top=True, weights='imagenet')

    img_path = 'data\\dogscats\\train\\cats\\cat.10013.jpg'
    img = image.load_img(img_path, target_size=(299, 299))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    print('Input image shape:', x.shape)

    preds = model.predict(x)
    print(np.argmax(preds))
    print('Predicted:', decode_predictions(preds, 1))  # ('n02123394', 'Persian_cat', 0.91428012)
Beispiel #22
0
    batch_size=batch_size,
    seed=42  # set seed for reproducability
)

# Validation image generator
validation_generator = test_image_gen.flow_from_directory(
    validate_dir,
    target_size=(Image_width, Image_height),
    batch_size=batch_size,
    seed=42  # set seed for reproducability
)

# Load the INception V3 model and load it with it's pre-trained weights. But exclude the final
# Fully Connected Layer
Xception_base_model = Xception(
    weights='imagenet',
    include_top=False)  # include_top=False excludes final FC layer
print('Xception base model without last FC loaded')

# Define the layers in the new classification prediction
x = Xception_base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(Number_FC_Neurons, activation='relu')(x)  # new FC layer, random init
predictions = Dense(num_classes, activation='softmax')(x)  # new softmax layer

# Define trainable model which links input from the Xception base model to the new classification prediction layers
model = Model(inputs=Xception_base_model.input, outputs=predictions)

# Print model structure diagram
print(model.summary())
def extract_features(input_dir, output_dir, model_type='inceptionv3', batch_size=32):
    """
    Extracts features from a CNN trained on ImageNet classification from all
    videos in a directory.

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

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

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


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

    model = None
    input_shape = (224, 224)

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

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

    # Get outputs of model from layer just before softmax predictions

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


    # Create output directories

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

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


    # Find all videos that need to have features extracted

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

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

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


    # Go through each video and extract features

    from keras.applications.imagenet_utils import preprocess_input

    for video_filename in tqdm(video_filenames):

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

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


        n_frames = len(frames)

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

        frames_arr = preprocess_input(frames_arr)

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

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

        with open(feat_filepath, 'wb') as f:
            np.save(f, features)
Beispiel #24
0
    epochs = 20  # number of iteration the algorithm gets trained.
    augmentation_strength = 0.2
    GPUS = True

    # Important to utilize CPUs even when training on multi-GPU instances as the CPUs can be a bottle neck when feeding to the GPUs
    CPUS = 16

    train_folder = 'data/train'
    validation_folder = 'data/test'

    save_class_names(train_folder, project_name)

    target_size = (299, 299)
    input_size = target_size + (3,)
    base_model = Xception(weights='imagenet',
                          include_top=False,
                          input_shape=input_size)

    model = update_base_model(base_model)

    # Initialize a dictionary with the layer name and corresponding learning rate ratio
    layer_mult = create_learn_rate_dict(model)

    # Initialize optimizer and compile the model
    adam_with_lr_multipliers = Adam_lr_mult(multipliers=layer_mult)
    model.compile(optimizer=adam_with_lr_multipliers,
                  loss='categorical_crossentropy', metrics=['accuracy'])

    if GPUS:
        # Intialize a multi-GPU model utilizing keras.utils.multi_gpu_model and then overriding the save and load models so that we can save in a format that the model can be read as a serial model
        model = ModelMGPU(model)