示例#1
0
    def build_lenet(self):

        base_model = InceptionV3(weights='imagenet', include_top=False)

        # add a global spatial average pooling layer
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        # let's add a fully-connected layer
        x = Dense(1024, activation='relu')(x)
        # and a logistic layer -- let's say we have 200 classes
        predictions = Dense(self.output_classes,
                            activation=self.output_activation)(x)

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

        # first: train only the top layers (which were randomly initialized)
        # i.e. freeze all convolutional InceptionV3 layers
        for layer in base_model.layers:
            layer.trainable = False

        # compile the model (should be done *after* setting layers to non-trainable)
        model.compile(optimizer=self.optimizer,
                      loss=self.loss,
                      metrics=['accuracy'])

        return model
 def construct_model(self):
     if self.name == 'inceptionv3':
         print('{:=^75}'.format('Downloading {}'.format(self.name)))
         print(params)
         self.base_model = InceptionV3(**params['network_params'])
         print('{:=^75}'.format('Download Complete'))
         
     elif self.name == 'xception':
         print('{:=^75}'.format('Downloading {}'.format(self.name)))
         self.base_model = Xception(**params['network_params'])
         print('{:=^75}'.format('Download Complete'))
         
         
     # 모델 구조  base model -> global average pooling -> dense
     print('{:=^75}'.format('Adding layers'))
     self.model = Sequential()
     self.model.add(self.base_model)
     self.model.add(GlobalAveragePooling2D())
     self.model.add(Dense(params['num_classes'], activation='softmax'))
     print('{:=^75}'.format('Added layers'))
     
     # 지정 경로에 저장
     if not os.path.exists('weight_path/'):
         os.mkdir('weight_path/')
     self.weight_save_path = os.path.join('weight_path/', self.name + "_weights.h5")
     
     print('{:=^75}'.format('Saving weights to {}'.format(self.weight_save_path)))
     self.model.save_weights(self.weight_save_path)
     print('{:=^75}'.format('Saved weights'))
示例#3
0
def inception(n_retrain_layers=0):
    K.set_image_data_format('channels_last')
    base_model = InceptionV3(include_top=False, input_shape=(224, 224, 3))
    features = GlobalAveragePooling2D()(base_model.output)
    model = Model(inputs=base_model.input, outputs=features)
    model = _set_n_retrain(model, n_retrain_layers)
    return model
 def load_model(self):
     """This function is used to load pretrained model
     """
     usage = """USAGE: %python run_experiment.py -m [model]
                      Model ResNet50:                  python run_experiment.py -o resnet50
                      Model VGG16:                     python run_experiment.py -o vgg16
                      Model LeNet-5:                   python run_experiment.py -o lenet5
                      Model AlexNet:                   python run_experiment.py -o alexnet
                      Model Xception:                  python run_experiment.py -o xception
                      Model InceptionV3:               python run_experiment.py -o inceptionv3
             """
     try:
         # resnet50
         if self.model_name == "resnet50":
             from tensorflow.python.keras.applications import ResNet50
             from tensorflow.python.keras.applications.resnet50 import preprocess_input
             self.preprocess = preprocess_input
             self.model = ResNet50()
         # vgg16
         elif self.model_name == "vgg16":
             from tensorflow.python.keras.applications import VGG16
             from tensorflow.python.keras.applications.vgg16 import preprocess_input
             self.preprocess = preprocess_input
             self.model = VGG16()
         # # vgg19
         # elif self.model_name == "lenet5":
         #     # compiling and training teacher network
         #     teacher = LeNet5Teacher()
         #     teacher.__init__()
         #     teacher.load(cfg.lenet_config + ".json", cfg.lenet_config + ".h5")
         #     self.model = teacher.getModel()
         # xception
         elif self.model_name == "xception":
             from tensorflow.python.keras.applications import Xception
             from tensorflow.python.keras.applications.xception import preprocess_input
             self.preprocess = preprocess_input
             self.model = Xception()
         # inceptionv3
         elif self.model_name == "inceptionv3":
             from tensorflow.python.keras.applications import InceptionV3
             from tensorflow.python.keras.applications.inception_v3 import preprocess_input
             self.preprocess = preprocess_input
             self.model = InceptionV3()
         # alexnet
         elif self.model_name == "alexnet":
             # TODO get a pre-trained alexnet model
             self.logger.info("[ERROR]: Not yet implemented")
         # custom teacher model
         # elif self.model_name == "custom_teacher":
         #     # compiling and training teacher network
         #     teacher = TeacherCNN()
         #     teacher.__init__()
         #     teacher.load(cfg.custom_teacher_config + ".json", cfg.custom_teacher_config + ".h5")
         #     self.model = teacher.getModel()
         else:
             self.logger.error("Invalid model name")
     except Exception as e:
         self.logger.error("Loading of model failed due to {0}".format(
             str(e)))
示例#5
0
    def __init__(self, input_sizeW, input_sizeH):
        input_image = Input(shape=(input_sizeW, input_sizeH, 3))

        inception = InceptionV3(input_shape=(input_sizeW, input_sizeH, 3),
                                include_top=False)
        inception.load_weights(INCEPTION3_BACKEND_PATH)

        x = inception(input_image)

        self.feature_extractor = Model(input_image, x)
示例#6
0
def build_model_inception_v3_avg(lock_base_model: True):
    base_model = InceptionV3(input_shape=INPUT_SHAPE, include_top=False, pooling=None)
    if lock_base_model:
        for layer in base_model.layers:
            layer.trainable = False
    # base_model.summary()
    x = GlobalAveragePooling2D(name='avg_pool_final')(base_model.layers[-1].output)
    res = Dense(NB_CLASSES, activation='sigmoid', name='classes', kernel_initializer='zero',
                kernel_regularizer=l1(1e-5))(x)
    model = Model(inputs=base_model.inputs, outputs=res)
    # model.summary()
    return model
示例#7
0
def incv3_enc_model(wanted_mixed_layers=None):
    """Returns a model that has all mixed layers of Inception V3 as output.
  """
    if not wanted_mixed_layers:
        wanted_mixed_layers = ['mixed' + str(i) for i in range(11)]
    from tensorflow.python.keras.applications import InceptionV3
    inc_v3 = InceptionV3(include_top=False, weights='imagenet')
    mixed_layer_names = [
        layer.name for layer in inc_v3.layers
        if 'mixed' in layer.name and '_' not in layer.name
    ]
    layers = [l for l in wanted_mixed_layers if l in mixed_layer_names]
    return Model(inputs=inc_v3.input,
                 outputs=[inc_v3.get_layer(ln).output for ln in layers])
    def run(self):
        input = Input(shape=(200, 200, 3))
        model = InceptionV3(input_tensor=input, include_top=False, weights='imagenet', pooling='max')

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

        input_image_size = (200, 200)

        x = model.output
        x = Dense(1024, name='fully')(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = Dense(512)(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = Dense(101, activation='softmax', name='softmax')(x)
        model = Model(model.input, x)

        model.summary()

        train_datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.2)
        batch_size = 16

        train_generator = train_datagen.flow_from_directory(
            '/result/caltech101',
            target_size=input_image_size,
            batch_size=batch_size,
            class_mode='categorical',
            subset='training')

        validation_generator = train_datagen.flow_from_directory(
            '/result/caltech101',
            target_size=input_image_size,
            batch_size=batch_size,
            class_mode='categorical',
            subset='validation')

        model.compile(
            optimizer=tf.keras.optimizers.Adam(),
            loss='categorical_crossentropy',
            metrics=['acc'])

        early_stopping = EarlyStopping(patience=20, mode='auto', monitor='val_acc')
        hist = model.fit_generator(train_generator,
                                      steps_per_epoch=train_generator.samples // batch_size,
                                      validation_data = validation_generator,
                                      epochs=100,
                                      callbacks=[early_stopping])
示例#9
0
def model_pretraining(model_name, num_labels):
    if model_name.lower() == 'resnet50':
        restnet_base_model = ResNet50(weights='imagenet',
                                      include_top=False,
                                      input_shape=input_shape)
        x = Flatten()(restnet_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(restnet_base_model.input, output, name='resnet')
    elif model_name.lower() == 'inceptionv3':
        inception_base_model = InceptionV3(weights='imagenet',
                                           include_top=False,
                                           input_shape=input_shape)
        x = Flatten()(inception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(inception_base_model.input, output, name='inception')
    elif model_name.lower() == 'xception':
        xception_base_model = Xception(weights='imagenet',
                                       include_top=False,
                                       input_shape=input_shape)
        x = Flatten()(xception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(xception_base_model.input, output, name='xception')
    elif model_name.lower() == 'densenet':
        xception_base_model = DenseNet121(weights='imagenet',
                                          include_top=False,
                                          input_shape=input_shape)
        x = Flatten()(xception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(xception_base_model.input, output, name='densenet')
    elif model_name.lower() == 'inceptionresnet':
        xception_base_model = DenseNet121(weights='imagenet',
                                          include_top=False,
                                          input_shape=input_shape)
        x = Flatten()(xception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(xception_base_model.input,
                      output,
                      name='inceptionresnet')
    else:
        raise ValueError('Invalid model name!')
    return model
示例#10
0
         .batch(batch_size, drop_remainder=True)

#RESNET50
num_classes = 15
input_shape = img_dim
input_shape += (3,)
#valid = ds_img_valid.map(augment_image)
#train = ds_img_train.map(augment_image)

base_model_xception = Xception(weights='imagenet', include_top=False, input_shape=input_shape)
x1 = Flatten()(base_model_xception.get_output_at(-1))
x1 = Dense(1024, activation='relu')(x1)
output1 = Dense(num_classes, activation='sigmoid')(x1)
input1 = base_model_xception.input

base_model_inception = InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape)
x2 = Flatten()(base_model_inception.get_output_at(-1))
x2 = Dense(1024, activation='relu')(x2)
output2 = Dense(num_classes, activation='sigmoid')(x2)
input2 = base_model_inception.input

model = Model([input1, input2], [output1, output2])
model.compile(optimizer=tf.optimizers.Adam(learning_rate=lr),
              loss=tf.nn.sigmoid_cross_entropy_with_logits, metrics=['accuracy'])
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=lr)
file_name = 'weights-improvement-{epoch:02d}-{val_loss:.2f}.hdf5'
save_model = tf.keras.callbacks.ModelCheckpoint(
    '/home/lambda/Desktop/Chest_XRay14_images_002/Saved_Models/{}'.format(file_name), monitor='val_loss')
train_history = model.fit(ds_img_train, epochs=epochs, callbacks=[reduce_lr, early_stopping, save_model],
                          validation_data=ds_img_valid)
    def run(self):
        tf.compat.v1.disable_eager_execution()
        # 입력 값을 받게 추가합니다.
        parser = argparse.ArgumentParser()
        parser.add_argument('--learning_rate',
                            required=False,
                            type=float,
                            default=0.001)
        parser.add_argument('--dropout_rate',
                            required=False,
                            type=float,
                            default=0.2)
        parser.add_argument('--batch_size',
                            required=False,
                            type=int,
                            default=16)
        parser.add_argument('--epoch', required=False, type=int, default=10)
        # relu, sigmoid, softmax, tanh
        parser.add_argument('--act', required=False, type=str, default='relu')

        args = parser.parse_args()

        input = Input(shape=(200, 200, 3))
        model = InceptionV3(input_tensor=input,
                            include_top=False,
                            weights='imagenet',
                            pooling='max')

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

        input_image_size = (200, 200)

        x = model.output
        x = Dense(1024, name='fully')(x)
        x = Dropout(args.dropout_rate)(x)
        x = BatchNormalization()(x)
        x = Activation(args.act)(x)
        x = Dense(512)(x)
        x = Dropout(args.dropout_rate)(x)
        x = BatchNormalization()(x)
        x = Activation(args.act)(x)
        x = Dense(101, activation='softmax', name='softmax')(x)
        model = Model(model.input, x)

        model.summary()

        train_datagen = ImageDataGenerator(rescale=1. / 255,
                                           validation_split=0.2)
        batch_size = args.batch_size

        train_generator = train_datagen.flow_from_directory(
            '/result/caltech101',
            target_size=input_image_size,
            batch_size=batch_size,
            class_mode='categorical',
            subset='training')

        validation_generator = train_datagen.flow_from_directory(
            '/result/caltech101',
            target_size=input_image_size,
            batch_size=batch_size,
            class_mode='categorical',
            subset='validation')
        model = multi_gpu_model(model, gpus=2)
        model.compile(
            optimizer=tf.keras.optimizers.Adam(lr=args.learning_rate),
            loss='categorical_crossentropy',
            metrics=['acc'])

        early_stopping = EarlyStopping(patience=20,
                                       mode='auto',
                                       monitor='val_acc')
        hist = model.fit_generator(
            train_generator,
            verbose=0,
            steps_per_epoch=train_generator.samples // batch_size,
            validation_data=validation_generator,
            epochs=args.epoch,
            callbacks=[early_stopping, KatibMetricLog()])
示例#12
0
def train(train_data_dir, validation_data_dir, model_path):
    # Pre-Trained CNN Model using imagenet dataset for pre-trained weights
    # base_model = Xception(input_shape=(img_width, img_height, 3), weights='imagenet', include_top=False)
    
    if K.image_data_format() == 'channels_first':
        input_shape = (3, img_width, img_height)
    else:
        input_shape = (img_width, img_height, 3)

    if os.path.isfile(MODEL):
            model = load_model(MODEL)
    else:
            base_model = InceptionV3(input_shape=input_shape, weights='imagenet', include_top=False)
    
            x = base_model.output
            x = GlobalMaxPooling2D()(x)
            x = Dense(128, activation='relu', name='fc1')(x)
            x = Dropout(0.2)(x)

            predictions = Dense(nb_classes, activation='softmax', name='predictions')(x)

            # add your top layer block to your base model
            model = Model(base_model.input, predictions)
            #print(model.summary())


            # first: train only the top layers (which were randomly initialized)
            # i.e. freeze all layers of the based model that is already pre-trained.
            for layer in base_model.layers:
                    layer.trainable = False
                    
            model.compile(optimizer='nadam',
                  loss='categorical_crossentropy',  # categorical_crossentropy if multi-class classifier
                  metrics=['accuracy'])
        

    # Read Data and Augment it: Make sure to select augmentations that are appropriate to your images.
    # To save augmentations un-comment save lines and add to your flow parameters.
    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       rotation_range=transformation_ratio,
                                       shear_range=transformation_ratio,
                                       zoom_range=transformation_ratio,
                                       cval=transformation_ratio,
                                       horizontal_flip=True,
                                       vertical_flip=True)

    validation_datagen = ImageDataGenerator(rescale=1. / 255)

    # os.makedirs(os.path.join(os.path.abspath(train_data_dir), 'preview'), exist_ok=True)
    train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                        target_size=[img_width, img_height],
                                                        batch_size=batch_size,
                                                        class_mode='categorical')
    # save_to_dir=os.path.join(os.path.abspath(train_data_dir), '../preview')
    # save_prefix='aug',
    # save_format='jpeg')
    # use the above 3 commented lines if you want to save and look at how the data augmentations look like

    validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
                                                                  target_size=[img_width, img_height],
                                                                  batch_size=batch_size,
                                                                  class_mode='categorical')



    # save weights of best training epoch: monitor either val_loss or val_acc

    #top_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5')
    callbacks_list = [
        ModelCheckpoint(MODEL, monitor='val_acc', verbose=1, save_best_only=True),
        EarlyStopping(monitor='val_acc', patience=5, verbose=0),
        TensorBoard(log_dir='tensorboard/inception-v3-train-top-layer', histogram_freq=0, write_graph=False, write_images=False)
    ]

    # Train Simple CNN
    model.fit_generator(train_generator,
                        steps_per_epoch=nb_train_samples // batch_size,
                        epochs=nb_epoch / 5,
                        validation_data=validation_generator,
                        validation_steps=nb_validation_samples // batch_size,
                        callbacks=callbacks_list)
示例#13
0
train_image_txt_file = os.path.join(text_file_base_dir,
                                    'Flickr_8k.trainImages.txt')

train_image_paths = get_image_paths(images_dir, train_image_txt_file)

test_image_txt_file = os.path.join(text_file_base_dir,
                                   'Flickr_8k.testImages.txt')

test_image_paths = get_image_paths(images_dir, test_image_txt_file)

# descriptions
train_descriptions = load_clean_descriptions('descriptions.txt', train)
print('Descriptions: train=%d' % len(train_descriptions))

# Load the inception v3 model
model = InceptionV3(weights='imagenet', include_top=True)
model_new = Model(model.input, model.layers[-2].output)
# model_new = InceptionV3(weights='imagenet', include_top=False)
# Create a new model, by removing the last layer (output layer) from the inception v3
# model_new = Model(model.input, model.layers[-2].output)

# Call the funtion to encode all the train images
# This will take a while on CPU - Execute this only once
start = time.time()
encoding_train = {
    img[len(images_dir):]: encode(model_new, img)
    for img in tqdm(train_image_paths)
}
print("Train feature taken in seconds: ", time.time() - start)

# Save the bottleneck train features to disk
示例#14
0
def createModel():
    model: Model = InceptionV3(weights=None, input_shape=(800, 800, 1), classes=2)
    model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=["accuracy"])

    return model
"""
we set up a sequential model that we can add layers to
"""
my_new_model = Sequential()
"""
first we add all of pre-trained  model
we've written include_top=False, this is how specify that we want to exlude
the layer that makes prediction into the thousands of categories used in the ImageNet competition
we set the weights to be 'ImageNet' to specify that we use the pre-traind model on ImageNet
pooling equals average says that if we had extra channels in our tensor at the end of this step
we want to collapse them to 1d tensor by taking an average across channels
now we have a pre-trained model that creates the layer before the last layer
that we saw in the slides
"""
my_new_model.add(InceptionV3(weights='imagenet', include_top=False, pooling='avg'))
"""
we add a dense layer to make predictions,
we specify the number of nodes in this layer which in this case is
the number of classes,
then we want to apply the softmax function to turn it into probabilities 
"""
my_new_model.add(Dense(num_classes,activation='softmax',))

"""
we tell tensor flow not to train the first layer which is the  pre-trained model
because that's the model that was already pre-trained with the ImageNet data
"""
my_new_model.layers[0].trainable = False
"""
the compile command tells tensorflow how to update the relationships in the dense connections 
示例#16
0
# ONLY IMPORT DESIRED CNN MODEL
#from tensorflow.python.keras.applications import VGG16
#image_model = VGG16(include_top=True, weights='imagenet')
#transfer_layer=image_model.get_layer('fc2')

#from tensorflow.python.keras.applications import VGG19
#image_model = VGG19(include_top=True, weights='imagenet')
#transfer_layer=image_model.get_layer('fc2')

#from tensorflow.python.keras.applications import ResNet50
#image_model = ResNet50(include_top=True, weights='imagenet')
#transfer_layer=image_model.get_layer('avg_pool')

from tensorflow.python.keras.applications import InceptionV3
image_model = InceptionV3(include_top=True, weights='imagenet')
transfer_layer=image_model.get_layer('avg_pool')

# LOAD THE CORRECT TRANSFER VALUES
# SPECIFY THE CNN, resnet50 or vgg
transfer_values_train=np.load('image_features/transfer_values/InceptionV3/transfer_values_train.npy')
transfer_values_val=np.load('image_features/transfer_values/InceptionV3/transfer_values_val.npy')
transfer_values_test=np.load('image_features/transfer_values/InceptionV3/transfer_values_test.npy')

image_model_transfer = Model(inputs=image_model.input,
                             outputs=transfer_layer.output)
img_size=K.int_shape(image_model.input)[1:3]

# This function return a list of random token sequences
# with the given indices in the training set
def get_random_caption_tokens(idx):
示例#17
0
MODEL = "{}/model.h5".format(PWD)

train_data_dir = 'downloads/train'
model_path = '.'

    
if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)


if os.path.isfile(MODEL):
    model = keras.models.load_model(MODEL)
else:
    base_model = InceptionV3(input_shape=input_shape, weights='imagenet', include_top=False, pooling='avg')
    
    # freeze all layers of the based model that is already pre-trained.
    for layer in base_model.layers:
        layer.trainable = False

    x = base_model.output
    #x = GlobalMaxPooling2D()(x)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.2)(x)

    predictions = Dense(nb_classes, activation='softmax', name='predictions')(x)

    # add top layer block to your base model
    model = Model(inputs=base_model.input, outputs=predictions)