예제 #1
0
    def buildModel(self, dense_list):
        '''
        n_filters: num of filters in the initial layers
        depth: depth of the resnet
        For dense layers, the dropouts are always 0.5
        den_list: key arg list for den layers
        '''
        in_layer = Input(self.input_shape)
        
        # use keras existing resnet50
        xceptionModel = Xception(include_top=False, weights='imagenet', 
        input_tensor=Input(shape=self.input_shape), pooling="avg")

        kernel = Dropout(0.5) (xceptionModel (in_layer))
        xceptionModel.summary()
        print kernel.shape

        denlayer = kernel
        # denlayer = GlobalMaxPooling2D() (kernel)
        # denlayer = Flatten() (kernel)
        
        # adding dense layers
        for kargs in dense_list:
            denlayer = Dropout(0.5) (Dense(**kargs) (denlayer))
        
        out_layer = Dense(self.output_dim, activation='softmax') (denlayer)
        self.model = Model(inputs=[in_layer], outputs=[out_layer])
def load_xcep_model(img_batch):
    model = Xception(include_top=True,
                     weights=MODEL_PATH,
                     input_shape=(input_height, input_width, 3),
                     classes=classes)
    model.summary()
    plot_model(model, 'xcep_model.jpg')
    # preds = model.predict(img_batch)
    return model, img_batch
def extract_features(directory):
	# load the model
	model = Xception()
	# re-structure the model
	model.layers.pop()
	model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
	# summarize
	print(model.summary())
	# extract features from each photo
	features = dict()
	for name in listdir(directory):
		# load an image from file
		filename = directory + '/' + name
		image = load_img(filename, target_size=(299, 299))
		# convert the image pixels to a numpy array
		image = img_to_array(image)
		# reshape data for the model
		image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
		# prepare the image for the VGG model
		image = preprocess_input(image)
		# get features
		feature = model.predict(image, verbose=0)
		# get image id
		image_id = name.split('.')[0]
		# store feature
		features[image_id] = feature
		print('>%s' % name)
	return features
class Xceptionmodel(CNNmodel):
    def __init__(self, data_directory, batch_size=16):
        self.cnn = CNNmodel(data_directory)
        self.height, self.width, self.depth = (48, 48, 1)
        self.batch_size = batch_size
        self.xception = Xception(include_top=True,
                                 weights=None,
                                 input_shape=(self.height, self.width,
                                              self.depth),
                                 classes=4)

    def summary(self):
        print(self.xception.summary())

    def train(self,
              optimizer='adam',
              loss='categorical_crossentropy',
              epoch=20):

        self.xception.compile(loss="categorical_crossentropy",
                              optimizer=optimizer,
                              metrics=["accuracy"])
        print("[INFO] training network...")
        es = EarlyStopping(monitor='val_acc',
                           mode='max',
                           verbose=1,
                           patience=4)
        self.history = self.xception.fit(self.cnn.train_data,
                                         epochs=epoch,
                                         validation_data=self.cnn.val_data,
                                         verbose=1,
                                         callbacks=[es])

        print("[INFO] Saving model...")
        pickle.dump(self.xception, open('xception.pkl', 'wb'))
        print("[INFO] Done...")
예제 #5
0
from keras.applications import VGG16
from keras.applications import VGG19
from keras.applications import Xception
from keras.applications import ResNet50
from keras.applications import MobileNet
from keras.applications import MobileNetV2
from keras.applications import InceptionV3
from keras.applications import InceptionResNetV2

# conv_base = VGG16(weights='imagenet', include_top=False,
#                   input_shape=(150,150,3))
# conv_base = VGG16()   # 224,224,3
conv_base = Xception()
conv_base.summary()

from keras import models, layers
model = models.Sequential()
model.add(conv_base)
# model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
예제 #6
0
## define the model (preset weights)
print('[INFO] defining model...')

if worker == 'single':
    model = Xception(include_top=True,
                     weights=None,
                     classes=len(labeltonumber))
elif worker == 'parallel':
    with tf.device('/cpu:0'):
        model = Xception(include_top=True,
                         weights=None,
                         classes=len(labeltonumber))

## 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/
EPOCHS = 200
print('epochs: {}'.format(EPOCHS))
## batch normalization batch sizes: 64, 128, 256, 512
BATCHSIZE = 32
print('batchsize: {}'.format(BATCHSIZE))
STEPS_PER_EPOCH = len(X_train) / BATCHSIZE
print('steps per epoch: {}'.format(STEPS_PER_EPOCH))


## Olafenwa and Olafenva - 2018 #######################
## step decay
def lr_schedule(epoch):
from keras.applications import VGG16, ResNet50, InceptionV3, Xception, DenseNet121
from keras.utils import plot_model

# https://github.com/keras-team/keras-applications/tree/master/keras_applications

vgg16 = VGG16(weights=None)
vgg16.summary()
plot_model(vgg16, "vgg16.png", show_layer_names=True, show_shapes=True)

resnet50 = ResNet50(weights=None)
resnet50.summary()
plot_model(resnet50, "resnet50.png", show_layer_names=True, show_shapes=True)

inception = InceptionV3(weights=None)
inception.summary()
plot_model(inception, "inception.png", show_layer_names=True, show_shapes=True)

xception = Xception(weights=None)
xception.summary()
plot_model(xception, "xception.png", show_layer_names=True, show_shapes=True)

densenet = DenseNet121(weights=None)
densenet.summary()
plot_model(densenet, "densenet.png", show_layer_names=True, show_shapes=True)
예제 #8
0
def Xception_Net(trainable=None):
    
    # Preprocessing the dataset into keras feedable format
    train_datagen = ImageDataGenerator(
        rotation_range = rotation,
        width_shift_range = width_shift,
        height_shift_range= height_shift,
        rescale= scale,
        shear_range= shear,
        zoom_range= zoom,
        horizontal_flip= horizontal,
        fill_mode=fill,
        validation_split=validation
    )
    test_datagen = ImageDataGenerator(
        rescale= scale,
    )

    train_generator = train_datagen.flow_from_directory(
        path,
        target_size=target,
        batch_size=batch,
        class_mode='categorical',
        subset='training',
    )
    validation_generator = train_datagen.flow_from_directory(
        path,
        target_size=target,
        batch_size=batch,
        class_mode='categorical',
        subset='validation'
    )
    
    
    # Loading the ResNet50 Model
    
    xception = Xception(include_top=False, weights='imagenet', input_shape=input_sh)
    output = xception.layers[-1].output
    output = keras.layers.Flatten()(output)
    xception = Model(xception.input, output=output)
    print(xception.summary())
    print('\n\n\n')
    # If you chose not for fine tuning
    if trainable is None:
        model = Sequential()
        model.add(xception)
        model.add(Dense(hidden, activation='relu', input_dim=input_sh))
        model.add(Dropout(dropout_num))
        model.add(Dense(hidden, activation='relu'))
        model.add(Dropout(dropout_num ))
        if classes == 1:
            model.add(Dense(classes, activation='sigmoid', name='Output'))
        else:
            model.add(Dense(classes, activation='softmax', name='Output'))
            
        for layer in xception.layers:
            layer.trainable = False
        print("The model summary of Xception  -->\n\n\n")        # In this the Resnet50 layers are not trainable 
        
        for i, layer in enumerate(xception.layers):
            print(i, layer.name, layer.trainable)
        model.compile(loss=loss_param,                # Change according to data
                      optimizer=optimizers.RMSprop(),
                      metrics=['accuracy'])
        print("The summary of final Model \n\n\n")
        print(model.summary())
        print('\n\n\n')
       
       

        fit_history = model.fit_generator(
            train_generator,
            steps_per_epoch=len(train_generator.filenames) // batch,
            epochs=epoch,
            shuffle=True,
            validation_data=validation_generator,
            validation_steps=len(train_generator.filenames) // batch,
            class_weight=n,
            callbacks=[
                EarlyStopping(patience=patience_param, restore_best_weights=True),
                ReduceLROnPlateau(patience=patience_param)
            ])
        os.chdir(output_path)    
        model.save("model.h5")
        print(fit_history.history.keys())
        plt.figure(1, figsize = (15,8)) 

        plt.subplot(221)  
        plt.plot(fit_history.history['accuracy'])  
        plt.plot(fit_history.history['val_accuracy'])  
        plt.title('model accuracy')  
        plt.ylabel('accuracy')  
        plt.xlabel('epoch')  
        plt.legend(['train', 'valid']) 

        plt.subplot(222)  
        plt.plot(fit_history.history['loss'])  
        plt.plot(fit_history.history['val_loss'])  
        plt.title('model loss')  
        plt.ylabel('loss')  
        plt.xlabel('epoch')  
        plt.legend(['train', 'valid']) 

        plt.show()
        
              
    if trainable is not None:
        # Make last block of the conv_base trainable:

        for layer in xception.layers[:trainable]:
            layer.trainable = False
        for layer in xception.layers[trainable:]:
            layer.trainable = True

        print('Last block of the conv_base is now trainable')
        
        for i, layer in enumerate(xception.layers):
            print(i, layer.name, layer.trainable)
            
        model = Sequential()
        model.add(xception)
        model.add(Dense(hidden, activation='relu', input_dim=input_sh))
        model.add(Dropout(dropout_num))
        model.add(Dense(hidden, activation='relu'))
        model.add(Dropout(dropout_num ))
        model.add(Dense(hidden, activation='relu'))
        model.add(Dropout(dropout_num ))
        if classes == 1:
            model.add(Dense(classes, activation='sigmoid', name='Output'))
        else:
            model.add(Dense(classes, activation='softmax', name='Output'))
            
        for layer in xception.layers:
            layer.trainable = False
        print("The model summary of Xception  -->\n\n\n")        # In this the Resnet50 layers are not trainable     
        model.compile(loss=loss_param,                # Change according to data
                      optimizer=optimizers.RMSprop(),
                      metrics=['accuracy'])
        print("The summary of final Model \n\n\n")
        print(model.summary())
        print('\n\n\n')
       
       

        fit_history = model.fit_generator(
            train_generator,
            steps_per_epoch=len(train_generator.filenames) // batch,
            epochs=epoch,
            shuffle=True,
            validation_data=validation_generator,
            validation_steps=len(train_generator.filenames) // batch,
            class_weight=n,
            callbacks=[
                EarlyStopping(patience=patience_param, restore_best_weights=True),
                ReduceLROnPlateau(patience=patience_param)
            ])
        os.chdir(output_path)    
        model.save("model.h5")
        print(fit_history.history.keys())
        plt.figure(1, figsize = (15,8)) 

        plt.subplot(221)  
        plt.plot(fit_history.history['accuracy'])  
        plt.plot(fit_history.history['val_accuracy'])  
        plt.title('model accuracy')  
        plt.ylabel('accuracy')  
        plt.xlabel('epoch')  
        plt.legend(['train', 'valid']) 

        plt.subplot(222)  
        plt.plot(fit_history.history['loss'])  
        plt.plot(fit_history.history['val_loss'])  
        plt.title('model loss')  
        plt.ylabel('loss')  
        plt.xlabel('epoch')  
        plt.legend(['train', 'valid']) 

        plt.show()
예제 #9
0
def get_features_CNN(image_list, method='VGG16'):
    """
    Extracts image features using CNN

    Arguments:
        - image_list: list, image set
        - method: str, VGG16 or Xception

    Returns:
        - features: list, contains tuples with image path + histogram of features
    """
    features = []
    list_features_batch = []
    if method in ['VGG16', 'VGG16DAB']:
        print('Loading network...')
        model = VGG16(weights='imagenet', include_top=False, pooling='avg')
        model.summary()

        x = 0
        n = 0
        for im in tqdm(image_list):
            image = imread(im)
            if method == 'VGG16DAB':
                image = imagetoDAB(image)
            image = numpy.asarray(image)
            image = numpy.expand_dims(image, axis=0)
            image = imagenet_utils.preprocess_input(image)
            curr_feat = model.predict(image)
            curr_feat = curr_feat.flatten()
            features.append((im, curr_feat))
            n += 1
            # When list has 100000 images + features, it is saved with pickle and a new list starts
            if n == 100000:
                features_batch = os.path.join(
                    outpath, 'features_{}_batch{}.p'.format(method, x))
                with open(features_batch, "wb") as f:
                    pickle.dump(features, f)
                list_features_batch.append(features_batch)
                features = []
                n = 0
                x += 1

        features_batch = os.path.join(
            outpath, 'features_{}_batch{}.p'.format(method, x))
        with open(features_batch, "wb") as f:
            pickle.dump(features, f)
        list_features_batch.append(features_batch)

    if method in ['Xception', 'XceptionDAB']:
        print('Loading network...')
        model = Xception(weights='imagenet', include_top=False, pooling='avg')
        model.summary()

        x = 0
        n = 0
        for im in tqdm(image_list):
            image = imread(im)
            if method == 'XceptionDAB':
                image = imagetoDAB(image)
            image = numpy.asarray(image)
            image = numpy.expand_dims(image, axis=0)
            image = preprocess_input(image)
            curr_feat = model.predict(image)
            curr_feat = curr_feat.flatten()
            features.append((im, curr_feat))
            n += 1
            # When list has 100000 images + features, it is saved with pickle and a new list starts
            if n == 100000:
                features_batch = os.path.join(
                    outpath, 'features_{}_batch{}.p'.format(method, x))
                with open(features_batch, "wb") as f:
                    pickle.dump(features, f)
                list_features_batch.append(features_batch)
                features = []
                n = 0
                x += 1

        features_batch = os.path.join(
            outpath, 'features_{}_batch{}.p'.format(method, x))
        with open(features_batch, "wb") as f:
            pickle.dump(features, f)
        list_features_batch.append(features_batch)

    return list_features_batch
예제 #10
0
                                                                 width),
                                                    batch_size=batch_size,
                                                    seed=seed,
                                                    class_mode='categorical')

# Test generator
test_datagen = ImageDataGenerator(rescale=1. / 255)
test_generator = test_datagen.flow_from_directory(test_dir,
                                                  target_size=(height, width),
                                                  batch_size=batch_size,
                                                  seed=seed,
                                                  class_mode='categorical')
base_model = Xception(weights=weights,
                      include_top=False,
                      input_shape=(height, width, channels))
base_model.summary()


def extract_features(sample_count, datagen):
    start = dt.datetime.now()
    features = np.zeros(shape=(sample_count, 5, 5, 2048))
    labels = np.zeros(shape=(sample_count, 10))
    generator = datagen
    i = 0
    for inputs_batch, labels_batch in generator:
        stop = dt.datetime.now()
        time = (stop - start).seconds
        print('\r',
              'Extracting features from batch',
              str(i + 1),
              '/',
예제 #11
0
model = DenseNet121()
model = DenseNet169()
model = DenseNet201()
model = NASNetLarge()
model = NASNetMobile()

vgg16 = VGG16(
    weights=None,
    include_top=False,
    classes=10,
    input_shape=(224, 224, 3),
    # classifier_activation="softmax"
)
# (None, 224, 224, 3)
# 모델 다운 받아 와야함

vgg16.summary()

# 잘만든 모델 가져다 쓰는 거 = 전이학습
# 이미지 모델에서 준우승 한 모델

# VGG16이랑 엮기

model = Sequential()
model.add(vgg16)
model.add(Dense(256))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
예제 #12
0
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,
                                        class_mode='categorical')
예제 #13
0
val_gen = test_datagen.flow_from_directory(
        val_dir,
        target_size=(299,299),
        batch_size=20,
        class_mode='binary')


test_gen = test_datagen.flow_from_directory(
        test_dir,
        target_size=(299,299),
        batch_size=24,
        class_mode='binary')

#Xception model
conv_xception = Xception(include_top=False, weights='imagenet', input_shape = (299, 299, 3))
conv_xception.summary()
def XceptionModel():
    conv_xception.trainable = False
    # Model create
    model = models.Sequential()
    model.add(conv_xception)
    model.add(layers.Flatten())
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(512, activation = 'relu'))
    model.add(layers.Dense(1, activation = 'sigmoid'))
    print(model.summary())
    #model compile
    opt = optimizers.Adam(lr = 1e-4)
    model.compile(loss = 'binary_crossentropy', optimizer = opt, metrics = ['acc'])
    return model