Esempio n. 1
0
 def __init__(self, data_shape=(224, 224, 3), resnet_version=1, resnet_layer_number=50, num_classes=1000):
   super(ResNet, self).__init__()
   
   weights = None
   if num_classes == 1000 and data_shape == (224, 224, 3):
     weights = 'imagenet'
     
   self.resnet_version = resnet_version
   
   self.data_augmentation = keras.Sequential(
     [
       layers.experimental.preprocessing.RandomFlip(
         "horizontal", 
         input_shape=data_shape),
       layers.experimental.preprocessing.RandomRotation(0.1),
       layers.experimental.preprocessing.RandomZoom(0.1),
     ]
   )
   
   self.rescaling = layers.experimental.preprocessing.Rescaling(1./255)
   
   def preprocess_input(x, data_format=None):
     from tensorflow.keras.applications import imagenet_utils
     return imagenet_utils.preprocess_input(
     x, data_format=data_format, mode='tf')
     #return x
     
   self.preprocess_input = preprocess_input
   
   if resnet_layer_number == 18:
     if resnet_version == 1:
       self.resnet = ResNet18(category_num=num_classes)
     else:
       self.resnet = ResNet18V2(category_num=num_classes)
   elif resnet_layer_number == 50:
     if resnet_version == 1:
       self.resnet = ResNet50(weights=weights, input_shape=data_shape, classes=num_classes)
     else:
       self.resnet = ResNet50V2(weights=weights, input_shape=data_shape, classes=num_classes)
   elif resnet_layer_number == 101:
     if resnet_version == 1:
       self.resnet = ResNet101(weights=weights, input_shape=data_shape, classes=num_classes)
     else:
       self.resnet = ResNet101V2(weights=weights, input_shape=data_shape, classes=num_classes)
   elif resnet_layer_number == 152:
     if resnet_version == 1:
       self.resnet = ResNet152(weights=weights, input_shape=data_shape, classes=num_classes)
     else:
       self.resnet = ResNet152V2(weights=weights, input_shape=data_shape, classes=num_classes)
     
   self.build((None,) + data_shape)
Esempio n. 2
0
def get_model(architecture, iteracion, models_info, pipeline):

    print("="*len(architecture))
    print(architecture)
    print("="*len(architecture))

    if iteracion > 0:
        base_model = models_info[architecture]['model_memory']

    if architecture == 'InceptionV3':
        from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input
        if iteracion == 0:
            base_model = InceptionV3(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'InceptionV4':
        from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
        if iteracion == 0:
            base_model = InceptionResNetV2(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'ResNet50':
        from tensorflow.keras.applications.resnet import ResNet50, preprocess_input
        if iteracion == 0:
            base_model = ResNet50(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'ResNet101':
        from tensorflow.keras.applications.resnet import ResNet101, preprocess_input
        if iteracion == 0:
            base_model = ResNet101(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'ResNet152':
        from tensorflow.keras.applications.resnet import ResNet152, preprocess_input
        if iteracion == 0:
            base_model = ResNet152(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'DenseNet121':
        from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input
        if iteracion == 0:
            base_model = DenseNet121(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'DenseNet169':
        from tensorflow.keras.applications.densenet import DenseNet169, preprocess_input
        if iteracion == 0:
            base_model = DenseNet169(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'DenseNet201': 
        from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input
        if iteracion == 0:
            base_model = DenseNet201(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'NASNetLarge': 
        from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input
        if iteracion == 0:
            base_model = NASNetLarge(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'Xception':
        from tensorflow.keras.applications.xception import Xception, preprocess_input
        if iteracion == 0:
            base_model = Xception(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3))

    return base_model, preprocess_input
    def __init__(self, datapath, classes, max_frames, img_shape, channels,
                 saving_dir):
        self.datapath = datapath
        self.classes = classes
        self.seq_length = max_frames
        self.height = img_shape[0]
        self.width = img_shape[1]
        self.channels = channels
        self.base_model = ResNet152(include_top=False,
                                    input_shape=(224, 224, 3),
                                    weights='imagenet')
        #self.base_model.load_weights(r'D:\\Downloads\\resnet152_weights_tf_dim_ordering_tf_kernels_notop.h5')
        self.saving_dir = saving_dir

        for layer in self.base_model.layers:
            layer.trainable = False
        self.op = self.base_model.output
        self.x_model = AveragePooling2D((7, 7), name='avg_pool')(self.op)
        self.x_model = Flatten()(self.x_model)

        self.model = Model(self.base_model.input, self.x_model)
        print(self.model.summary())
Esempio n. 4
0
def create_Classifier(args):
    '''
        - Create the pre-trained model based on InceptionV3
        - Want weights? Include: weights='imagenet')
    '''
    weights = 'imagenet' if args.pretrained else None

    if args.model == "resnet50":
        base_model = ResNet50(weights=weights, include_top=False)
    elif args.model == "resnet101":
        base_model = ResNet101(weights=weights, include_top=False)
    elif args.model == "resnet152":
        base_model = ResNet152(weights=weights, include_top=False)
    else:
        base_model = InceptionV3(weights=weights, 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',
              kernel_initializer=glorot_uniform(seed=args.seed))(x)

    # and a logistic layer for our num_classes classes
    predictions = Dense(2,
                        activation='softmax',
                        kernel_initializer=glorot_uniform(seed=args.seed))(x)

    # prep model with new layers and compile
    model = Model(inputs=base_model.input, outputs=predictions)
    optimizer = optimizers.Adam(lr=args.learning_rate)
    model.compile(optimizer=optimizer,
                  loss='categorical_crossentropy',
                  metrics=['mse', 'accuracy'])

    return model
Esempio n. 5
0
    def make_encoder(self, input, name='resnet50', pretrained=True):
        if name == 'resnet18':
            from classification_models.keras import Classifiers
            ResNet18, _ = Classifiers.get('resnet18')
            model = ResNet18(weights='imagenet' if pretrained else None,
                             input_tensor=input,
                             include_top=False)
        elif name == 'resnet50':
            from tensorflow.keras.applications.resnet import ResNet50
            model = ResNet50(weights='imagenet' if pretrained else None,
                             input_tensor=input,
                             include_top=False)
        elif name == 'resnet101':
            from tensorflow.keras.applications.resnet import ResNet101
            model = ResNet101(weights='imagenet' if pretrained else None,
                              input_tensor=input,
                              include_top=False)
        elif name == 'resnet152':
            from tensorflow.keras.applications.resnet import ResNet152
            model = ResNet152(weights='imagenet' if pretrained else None,
                              input_tensor=input,
                              include_top=False)
        elif name == 'vgg16':
            from tensorflow.keras.applications.vgg16 import VGG16
            model = VGG16(weights='imagenet' if pretrained else None,
                          input_tensor=input,
                          include_top=False)
        elif name == 'vgg19':
            from tensorflow.keras.applications.vgg19 import VGG19
            model = VGG19(weights='imagenet' if pretrained else None,
                          input_tensor=input,
                          include_top=False)
        else:
            raise Exception(f'unknown encoder {name}')

        return model
Esempio n. 6
0
def create_model(name, **kwargs):
    """Create model with model's name
    """
    assert "input_shape" in kwargs
    assert "num_classes" in kwargs
    input_shape = kwargs["input_shape"]
    num_classes = kwargs["num_classes"]

    if name == "LeNet5":
        model = LeNet5(input_shape=input_shape, num_classes=num_classes)

    elif name == "LeCunLeNet5":
        model = LeCunLeNet5(input_shape=input_shape, num_classes=num_classes)

    elif name.startswith("AttentionLeNet5"):
        from .attention_lenet import AttentionLeNet5
        model = AttentionLeNet5(input_shape=input_shape,
                                num_classes=num_classes,
                                attention="senet")

    elif name == "ResNet18":
        from models.keras_fn.resnet_extension import ResNet18
        model = ResNet18(include_top=True,
                         weights=None,
                         input_shape=input_shape,
                         classes=num_classes)

    elif name == "ResNet34":
        from models.keras_fn.resnet_extension import ResNet34
        model = ResNet34(include_top=True,
                         weights=None,
                         input_shape=input_shape,
                         classes=num_classes)

    elif name == "ResNet50":
        from tensorflow.keras.applications.resnet import ResNet50
        model = ResNet50(include_top=True,
                         weights=None,
                         input_shape=input_shape,
                         classes=num_classes)

    elif name == "ResNet101":
        from tensorflow.keras.applications.resnet import ResNet101
        model = ResNet101(include_top=True,
                          weights=None,
                          input_shape=input_shape,
                          classes=num_classes)

    elif name == "ResNet152":
        from tensorflow.keras.applications.resnet import ResNet152
        model = ResNet152(include_top=True,
                          weights=None,
                          input_shape=input_shape,
                          classes=num_classes)

    elif name == "ResNet20v2":  # "ResNet20v2",  "ResNet56v2"
        # hparams: n, version, input_shape, num_classes
        assert "n" in kwargs
        assert "version" in kwargs
        n = kwargs["n"]
        version = kwargs["version"]

        from .fault_resnet import model_depth, resnet_v2, lr_schedule
        depth = model_depth(n=2, version=2)
        model = resnet_v2(input_shape=input_shape,
                          depth=depth,
                          num_classes=num_classes)
        # TODO
        optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule(0))

    else:
        raise Exception('Unknown model: ' + name)

    return model
Esempio n. 7
0
def get_model(architecture, iteracion, models_info, pipeline):

    print("=" * len(architecture))
    print(architecture)
    print("=" * len(architecture))

    if iteracion > 0 and not pipeline["restart_weights"]:
        print("USING MODELS FROM MEMORY")
        base_model = models_info[architecture]['model_memory']
        print("OK - USING MODELS FROM MEMORY")

    if architecture == 'InceptionV3':
        from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input

        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = InceptionV3(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
            print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}")

    if architecture == 'InceptionV4':
        from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input

        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = InceptionResNetV2(weights=pipeline['weights'],
                                           include_top=False,
                                           input_shape=(pipeline['img_height'],
                                                        pipeline['img_width'],
                                                        3))
            print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}")

    if architecture == 'ResNet50':
        from tensorflow.keras.applications.resnet import ResNet50, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = ResNet50(weights=pipeline['weights'],
                                  include_top=False,
                                  input_shape=(pipeline['img_height'],
                                               pipeline['img_width'], 3))
    if architecture == 'ResNet101':
        from tensorflow.keras.applications.resnet import ResNet101, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = ResNet101(weights=pipeline['weights'],
                                   include_top=False,
                                   input_shape=(pipeline['img_height'],
                                                pipeline['img_width'], 3))

    if architecture == 'ResNet152':
        from tensorflow.keras.applications.resnet import ResNet152, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = ResNet152(weights=pipeline['weights'],
                                   include_top=False,
                                   input_shape=(pipeline['img_height'],
                                                pipeline['img_width'], 3))
            print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}")

    if architecture == 'DenseNet121':
        from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = DenseNet121(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'DenseNet169':
        from tensorflow.keras.applications.densenet import DenseNet169, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = DenseNet169(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'DenseNet201':
        from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = DenseNet201(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'NASNetLarge':
        from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = NASNetLarge(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'Xception':
        from tensorflow.keras.applications.xception import Xception, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = Xception(weights=pipeline['weights'],
                                  include_top=False,
                                  input_shape=(pipeline['img_height'],
                                               pipeline['img_width'], 3))

    return base_model, preprocess_input
Esempio n. 8
0
# train : test = 8 : 2
num_train = int(np.round(check_label.shape[0] * 0.8))
num_test = int(np.round(check_label.shape[0] * 0.2))

train_img = check_img[0:num_train, :, :, :]
test_img = check_img[num_train:, :, :, :]

train_label = check_label[0:num_train]
test_label = check_label[num_train:]

# transfer learning ( ResNet50 )
IMG_SHAPE = (224, 224, 3)

base_model = ResNet152(input_shape=IMG_SHAPE,
                       weights='imagenet',
                       include_top=False)

# Freeze Network
base_model.trainable = False

inputs = tf.keras.Input(IMG_SHAPE)
# Separately from setting trainable on the model, we set training to False
x = base_model(inputs, training=False)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(128, activation='relu')(x)
x = tf.keras.layers.BatchNormalization()(x)
outputs = tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)(x)
model = tf.keras.Model(inputs, outputs)
model.summary()
Esempio n. 9
0
    from queue import Queue

else:
    from Queue import Queue


parser = argparse.ArgumentParser()
parser.add_argument('-m','--modelPath',required=True,
                    help="path to model")
'''parser.add_argument('-w','--weights',required=True,
                    help="weights file for encoder network")'''

args = vars(parser.parse_args())

''' Encoder Network architecture'''
encoder_network = ResNet152(include_top = False,input_shape=(224,224,3),weights = 'imagenet')
#encoder_network.load_weights(args['weights'])
for layer in encoder_network.layers:
            layer.trainable = False
op = encoder_network.output
x_model = AveragePooling2D((7,7),name='avg_pool')(op)
x_model = Flatten()(x_model)
        
encoder_model = Model(encoder_network.input,x_model)
print("Encoder Model prepared....")

#Decoder Network(laoding trained model)
decoder_network = load_model(args['modelPath'],compile=False)
print("Decoder network prepared.....")

# class VideoStream:
Esempio n. 10
0
 def download_for_url(self, path: str, **kwargs):
     """
     Download the file at the given URL
     :param path:  the path to download
     :param kwargs:  various kwargs for customizing the underlying behavior of
     the model download and setup
     :return: the absolute path to the model
     """
     path_split = path.split('/')
     type = path_split[0]
     weights_file = path_split[1]
     include_top = 'no_top' in weights_file
     if type == 'vgg19':
         ret = VGG19(include_top=include_top, **kwargs)
     elif type == 'vgg16':
         ret = VGG16(include_top=include_top, **kwargs)
     elif type == 'resnet50':
         ret = ResNet50(include_top=include_top, **kwargs)
     elif type == 'resnet101':
         ret = ResNet101(include_top=include_top, **kwargs)
     elif type == 'resnet152':
         ret = ResNet152(include_top=include_top, **kwargs)
     elif type == 'resnet50v2':
         ret = ResNet50V2(include_top=include_top, **kwargs)
     elif type == 'resnet101v2':
         ret = ResNet101V2(include_top=include_top, **kwargs)
     elif type == 'resnet152v2':
         ret = ResNet152V2(include_top=include_top, **kwargs)
     elif type == 'densenet121':
         ret = DenseNet121(include_top=include_top)
     elif type == 'densenet169':
         ret = DenseNet169(include_top=include_top, **kwargs)
     elif type == 'densenet201':
         ret = DenseNet201(include_top=include_top, **kwargs)
     elif type == 'inceptionresnetv2':
         ret = InceptionResNetV2(include_top=include_top, **kwargs)
     elif type == 'efficientnetb0':
         ret = EfficientNetB0(include_top=include_top, **kwargs)
     elif type == 'efficientnetb1':
         ret = EfficientNetB1(include_top=include_top, **kwargs)
     elif type == 'efficientnetb2':
         ret = EfficientNetB2(include_top=include_top, **kwargs)
     elif type == 'efficientnetb3':
         ret = EfficientNetB3(include_top=include_top, **kwargs)
     elif type == 'efficientnetb4':
         ret = EfficientNetB4(include_top=include_top, **kwargs)
     elif type == 'efficientnetb5':
         ret = EfficientNetB5(include_top=include_top, **kwargs)
     elif type == 'efficientnetb6':
         ret = EfficientNetB6(include_top=include_top, **kwargs)
     elif type == 'efficientnetb7':
         efficient_net = EfficientNetB7(include_top=include_top, **kwargs)
     elif type == 'mobilenet':
         ret = MobileNet(include_top=include_top, **kwargs)
     elif type == 'mobilenetv2':
         ret = MobileNetV2(include_top=include_top)
     #  MobileNetV3() missing 2 required positional arguments: 'stack_fn' and 'last_point_ch'
     #elif type == 'mobilenetv3':
     #    mobile_net = MobileNetV3(include_top=include_top, **kwargs)
     elif type == 'inceptionv3':
         ret = InceptionV3(include_top=include_top, **kwargs)
     elif type == 'nasnet':
         ret = NASNetLarge(include_top=include_top, **kwargs)
     elif type == 'nasnet_mobile':
         ret = NASNetMobile(include_top=include_top, **kwargs)
     elif type == 'xception':
         ret = Xception(include_top=include_top, **kwargs)
     model_path = os.path.join(keras_path, weights_file)
     ret.save(model_path)
     return model_path
Esempio n. 11
0
def get_resnet(classes=54,
               depth=50,
               input_shape=(224, 224, 3),
               base_layer_trainable=False):
    assert depth in [50, 101, 152]
    from tensorflow.keras.applications.resnet import ResNet50, ResNet101, ResNet152
    if depth == 50:
        base_model = ResNet50(include_top=False, input_shape=input_shape)
        for layer in base_model.layers:
            layer.trainable = base_layer_trainable
        head_model = KL.GlobalMaxPool2D()(base_model.output)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='00',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        # head_model = KL.Dense(1024, activation='relu', name='1111', kernel_initializer='he_uniform')(head_model)
        # head_model = KL.Dropout(0.5)(head_model)
        if classes == 2:
            head_model = KL.Dense(classes, activation='sigmoid',
                                  name='3333')(head_model)
        else:
            head_model = KL.Dense(classes, activation='softmax',
                                  name='3333')(head_model)
        model = KM.Model(inputs=base_model.input, outputs=head_model)
        return model
    elif depth == 101:
        base_model = ResNet101(include_top=False, input_shape=input_shape)
        for layer in base_model.layers:
            layer.trainable = base_layer_trainable
        head_model = KL.GlobalMaxPool2D()(base_model.output)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='00',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        # head_model = KL.Dense(1024, activation='relu', name='1111', kernel_initializer='he_uniform')(head_model)
        # head_model = KL.Dropout(0.5)(head_model)
        if classes == 2:
            head_model = KL.Dense(classes, activation='sigmoid',
                                  name='3333')(head_model)
        else:
            head_model = KL.Dense(classes, activation='softmax',
                                  name='3333')(head_model)
        model = KM.Model(inputs=base_model.input, outputs=head_model)
        return model
    else:
        base_model = ResNet152(include_top=False, input_shape=input_shape)
        for layer in base_model.layers:
            layer.trainable = base_layer_trainable
        head_model = KL.GlobalMaxPool2D()(base_model.output)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='00',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='11',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        if classes == 2:
            head_model = KL.Dense(classes, activation='sigmoid',
                                  name='3333')(head_model)
        else:
            head_model = KL.Dense(classes, activation='softmax',
                                  name='3333')(head_model)
        model = KM.Model(inputs=base_model.input, outputs=head_model)
        return model
Esempio n. 12
0
validation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,
                                                              batch_size=batch_size,
                                                              class_mode='categorical',
                                                              target_size=(img_height, img_width)
                                                             )

# In[8]:

callbacks = EarlyStopping(monitor='val_loss', patience=4, verbose=1, mode='auto')
best_model_file = '.../resnet152_drop_batch_best_weights_256.h5'
best_model = ModelCheckpoint(best_model_file, monitor='val_acc', verbose = 1, save_best_only = True)

# In[9]:

wp = '.../resnet152_weights_tf_dim_ordering_tf_kernels_notop.h5'
resnet152_base = ResNet152(include_top=False, weights=wp,
                           input_tensor=None, input_shape=(img_width, img_height,3))

# In[10]:

print('Adding new layers...')
output = resnet152_base.get_layer(index = -1).output  
output = Flatten()(output)
# let's add a fully-connected layer
output = Dense(1024,activation = "relu")(output)
output = BatchNormalization()(output)
output = Dropout(0.2)(output)
output = Dense(1024,activation = "relu")(output)
output = BatchNormalization()(output)
output = Dropout(0.2)(output)
output = Dense(5, activation='softmax')(output)
print('New layers added!')