def main(): strategy = tf.distribute.MirroredStrategy() with strategy.scope(): train_generator, validation_generator, test_generator = generators() class_weight_dict = generate_class_weights(train_generator) X_train, X_validation, X_test, Y_train, Y_validation, Y_test = yield_from_generators(train_generator, validation_generator, test_generator) # Set ResNet to be base model base_model = ResNet152V2(weights="imagenet", include_top=False) classifier = create_classifier(base_model) # Freeze all base model layers for layer in base_model.layers: layer.trainable = False classifier.compile(optimizer=Adam(), loss=[categorical_focal_loss(alpha=[[.25, .25, .25]], gamma=2)], metrics=['accuracy']) classifier.summary() print("Transfer learning") fit_predict(X_train, X_validation, X_test, Y_train, Y_validation, Y_test, train_generator, validation_generator, test_generator, classifier, class_weight_dict, 0) # Unfreeze all base model layers for layer in base_model.layers: layer.trainable = True classifier.compile(optimizer=Adam(), loss=[categorical_focal_loss(alpha=[[.25, .25, .25]], gamma=2)], metrics=['accuracy']) classifier.summary() print("Fine Tuning") fit_predict(X_train, X_validation, X_test, Y_train, Y_validation, Y_test, train_generator, validation_generator, test_generator, classifier, class_weight_dict, 1)
def define_model() -> Tuple[Model, Model]: """Defines the architecture of the model.""" i = Input([None, None, 3], dtype=tf.uint8) x = tf.cast(i, tf.float32) x = preprocess_input(x) # base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=(192, 192, 3)) # base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(192, 192, 3)) base_model = ResNet152V2(include_top=False, weights='imagenet', input_shape=(192, 192, 3)) x = base_model(x) x = GlobalAveragePooling2D()(x) x = Dense(512, kernel_regularizer='l2')(x) x = BatchNormalization()(x) x = ReLU()(x) # x = LayerNormalization()(x) # x = Dense(256, kernel_regularizer='l2')(x) # x = BatchNormalization()(x) # x = ReLU()(x) # x = LayerNormalization()(x) x = Dense(64, kernel_regularizer='l2')(x) x = BatchNormalization()(x) x = ReLU()(x) # x = LayerNormalization()(x) predictions = Dense(1, activation='sigmoid', kernel_regularizer='l2')(x) model = Model(inputs=[i], outputs=predictions) return model, base_model
def get_model_resnet(img_shape, img_input, weights, resnet_depth): if resnet_depth == 50: return ResNet50(include_top=False, weights=weights, input_tensor=img_input, input_shape=img_shape, pooling=None) elif resnet_depth == 101: return ResNet101V2(include_top=False, weights=weights, input_tensor=img_input, input_shape=img_shape, pooling=None) else: return ResNet152V2(include_top=False, weights=weights, input_tensor=img_input, input_shape=img_shape, pooling=None)
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)
def get_model_resnet(num_class): base_model = ResNet152V2(weights='imagenet', include_top=False) x = base_model.output # custom top predictions = get_custom_top(x, num_class) model = Model(inputs=base_model.input, outputs=predictions) for layer in base_model.layers: layer.trainable = False model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) return model
def build(self, **kwargs) -> Model: # input shape 224x224x3 input_shape = (self.imagenet_img_width, self.imagenet_img_height, self.imagenet_rgb_no_channels) # use a ResNet152 pre trained model as a base model, we are not including the top layer for maximizing the # learned features backbone_model: Model = ResNet152V2(include_top=False, weights='imagenet') # Freeze pre-trained base model weights backbone_model.trainable = False # Opticalflow model definition inputs = Input(shape=input_shape, name='opticalflow_inputs') x = backbone_model(inputs) x = GlobalAveragePooling2D(name='opticalflow_global_avg_pooling')(x) outputs = Dense(self.no_classes, activation='softmax', name='opticalflow_classifier')(x) # Opticalflow model assembling model = Model(inputs=inputs, outputs=outputs, name='opticalflow_model') return model
def build_model(hp): ### ResNet resnet = ResNet152V2(include_top=False, weights='imagenet', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) output = resnet.layers[-1].output output = Flatten()(output) resnet = Model(resnet.input, outputs=output) used_layers = hp.Int('num_layers',3,6) for layer in resnet.layers[:-used_layers]: layer.trainable = False for layer in resnet.layers[-used_layers:]: layer.trainable = True model = Sequential() model.add(resnet) for i in range(hp.Int('num_layers_2',1,4)): model.add(Dense(units=hp.Int('units', min_value=32, max_value=512, step = 32), activation='relu')) model.add( Dropout(rate=hp.Float( 'dropout', min_value=0.0, max_value=0.5, default=0.25, step=0.05, )) ) model.add(Dense(28, activation='sigmoid')) optimizer = hp.Choice('optimizer', ['adam', 'sgd', 'rmsprop']) model.compile( loss='categorical_crossentropy', optimizer = optimizer, metrics=['accuracy']) return model
def model_Initializer(self): from tensorflow.keras.layers import Dense, Flatten from tensorflow.keras.models import Model import tensorflow as tf #Resources print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) print("Using Tensorflow : ", tf.__version__) # initializing the network model and excluding the last layer of network if self.MODEL == 'VGG16': from tensorflow.keras.applications.vgg16 import VGG16 self.model = VGG16( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'VGG19': from tensorflow.keras.applications.vgg19 import VGG19 self.model = VGG19( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'Xception': from tensorflow.keras.applications.xception import Xception self.model = Xception( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'ResNet50V2': from tensorflow.keras.applications.resnet_v2 import ResNet50V2 self.model = ResNet50V2( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'ResNet101V2': from tensorflow.keras.applications.resnet_v2 import ResNet101V2 self.model = ResNet101V2( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'ResNet152V2': from tensorflow.keras.applications.resnet_v2 import ResNet152V2 self.model = ResNet152V2( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'InceptionV3': from tensorflow.keras.applications.inception_v3 import InceptionV3 self.model = InceptionV3( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'InceptionResNetV2': from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 self.model = InceptionResNetV2( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'MobileNetV2': from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 self.model = MobileNetV2( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'DenseNet121': from tensorflow.keras.applications.densenet import DenseNet121 self.model = DenseNet121( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'DenseNet169': from tensorflow.keras.applications.densenet import DenseNet169 self.model = DenseNet169( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network if self.MODEL == 'DenseNet201': from tensorflow.keras.applications.densenet import DenseNet201 self.model = DenseNet201( input_shape=self.IMAGE_SIZE + [3], weights='imagenet', #using pretrained imagenet weights include_top=False) #excluding the last layer of network # Freezing the layes of the network for layer in self.model.layers: layer.trainable = False #flatterning the last layer self.x = Flatten()(self.model.output) #Created a dense layer for output self.outlayers = Dense(self.count_output_classes, activation='softmax')(self.x) #Binding pretrained layers with custom output layer self.model = Model(inputs=self.model.input, outputs=self.outlayers) #Compile the Model self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
def instantiate_model(): return (ResNet152V2(weights='imagenet'))
checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_accuracy', verbose=1, save_best_only=True) stop = EarlyStopping(monitor='accuracy', min_delta=0.01, patience=4, verbose=0, mode='auto', baseline=None, restore_best_weights=False) callbacks = [checkpoint, stop] resnet_model = ResNet152V2(weights='imagenet', include_top=False, input_shape=(220, 220, 3), classes=10) for layer in resnet_model.layers: if isinstance(layer, BatchNormalization): layer.trainable = True else: layer.trainable = False epochs = 50 model = Sequential() # model.add(UpSampling2D()) # model.add(UpSampling2D()) # model.add(UpSampling2D()) model.add(resnet_model) model.add(GlobalMaxPooling2D()) model.add(Dense(256, activation='relu'))
print(dataset.output_shapes) print('Creating val dataset', val_count) val_labels = tf.convert_to_tensor(val_labels, dtype=tf.int64) val_dataset = tf.data.Dataset.from_tensor_slices((val_filenames, val_labels)) val_dataset = val_dataset.map(utils._parse_function224) val_dataset = val_dataset.batch(64).repeat() print(val_dataset.output_types) print(val_dataset.output_shapes) # this could also be the output a different Keras model or layer # input_tensor = Input(shape=(240, 240, 3)) # this assumes K.image_data_format() == 'channels_last' # create the base pre-trained model base_model = ResNet152V2(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(103, activation='softmax')(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:
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
def get_resnetv2(classes=54, depth=50, input_shape=(224, 224, 3), base_layer_trainable=False): from tensorflow.keras.applications.resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2 assert depth in [50, 101, 152] if depth == 50: base_model = ResNet50V2(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 = ResNet101V2(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 = ResNet152V2(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