def create_model(config, labels, transfer_learning): """Create the model for training""" if transfer_learning == False: m = build_cnn(config, len(labels)) else: base_model = NASNetMobile( # input_tensor = Input(shape = (256, 256, 3)), input_shape=(224, 224, 3), include_top=False, weights="imagenet", pooling="avg") base_model.trainable = False # This is set to true during finetuning # Alternative 1: Functional API inputs = Input(shape=(224, 224, 3)) # The base model contains batchnorm layers. We want to keep them in inference mode when we unfreeze the base model for fine-tuning, so we make sure that the base_model is running in inference mode here. x = base_model(inputs, training=False) # Convert features of shape `base_model.output_shape[1:]` to vectors # x = GlobalAveragePooling2D()(x) x = Dense(50, activation="relu")(x) # Regularize with dropout x = Dropout(0.2)(x) outputs = Dense(len(labels), activation="sigmoid")(x) m = Model(inputs, outputs) # Alternative 2: Sequential API # m = Sequential([ # base_model, # Flatten(), # Dense(50, activation = "relu"), # Dense(len(labels), activation = "sigmoid") # ]) F2Score = MultiLabelFBeta(n_class=len(labels), beta=2, threshold=0.4) m.compile(optimizer=config.optimizer, loss='binary_crossentropy', metrics=["AUC", F2Score]) m.summary() return m, base_model, F2Score
def get_model_imagenet(net_name, input_shape=None, plot=False, **kwargs): """Returns ImageNet models""" print('Loading model', net_name if isinstance(net_name, str)\ else net_name.__name__) if net_name == ResNet50 or net_name == 'ResNet50': base_model = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape, **kwargs) feats = base_model.layers[-2] elif net_name == NASNetMobile or net_name == 'NASNetMobile': base_model = NASNetMobile(weights='imagenet', include_top=True, input_shape=input_shape, **kwargs) feats = base_model.layers[-3] elif net_name in list(source_module.keys()): base_model = net_name(weights='imagenet', include_top=False, input_shape=input_shape, **kwargs) feats = base_model.layers[-1] else: raise Exception('Unknown model ' + net_name.__name__) gap = GlobalAveragePooling2D(name="final_gap")(feats.output) model = Model(inputs=base_model.input, outputs=gap) if plot: plot_model(base_model, show_shapes=True, to_file='plots/{}_model.png'.format(net_name.__name__)) return model, process_input[net_name]
def get_weights(save_dir: Path, model_name: str, dtype: str) -> str: """Download pre-trained imagenet weights for model. Args: save_dir: Path to where checkpoint must be downloaded. model_name: Type of image classification model, must be one of ("GoogleNet", "InceptionV1", "MobileNet", "MobileNetV2", "NASNetMobile", "DenseNet121", "ResNet50", "Xception", "InceptionV3") in all lower case. dtype: Data type of the network. Returns: Path to checkpoint file. """ if isinstance(save_dir, str): save_dir = Path(save_dir) g = tf.Graph() with tf.Session(graph=g) as sess: keras_backend.set_floatx(dtype) keras_backend.set_session(sess) if model_name == "mobilenet": MobileNet(weights='imagenet') saver = tf.train.Saver() elif model_name == "mobilenetv2": MobileNetV2(weights='imagenet') saver = tf.train.Saver() elif model_name == "nasnetmobile": NASNetMobile(weights='imagenet') saver = tf.train.Saver() elif model_name == "densenet121": DenseNet121(weights='imagenet') saver = tf.train.Saver() elif model_name == "resnet50": ResNet50(weights='imagenet') saver = tf.train.Saver() elif model_name == "xception": Xception(weights='imagenet') saver = tf.train.Saver() elif model_name == "inceptionv3": InceptionV3(weights='imagenet') saver = tf.train.Saver() elif model_name in ("googleNet", "inceptionv1"): tar_file = get_file( fname='inceptionv1_tar.gz', origin= 'http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz' ) tar_file_reader = tarfile.open(tar_file) tar_file_reader.extractall(save_dir) if dtype == 'float16': saver = convert_ckpt_to_fp16( Path(save_dir, 'inception_v1.ckpt').as_posix()) sess.run(tf.global_variables_initializer()) else: raise ValueError("""Requested model type = %s not one of ["GoogleNet", "InceptionV1", "MobileNet", "MobileNetV2", "NASNetMobile", "DenseNet121", "ResNet50", "Xception", "InceptionV3"].""" % model_name) save_dir.mkdir(parents=True, exist_ok=True) return saver.save(sess, Path(save_dir, f"{model_name}.ckpt").as_posix())
def define_model(): # load model model = NASNetMobile(include_top=False, input_shape=(224, 224, 3)) # mark loaded layers as not trainable for layer in model.layers: layer.trainable = False # add new classifier layers flat1 = Flatten()(model.layers[-1].output) class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1) output = Dense(5, activation='softmax')(class1) # define new model model = Model(inputs=model.inputs, outputs=output) # compile model opt = SGD(lr=0.001, momentum=0.9) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) return model
def define_classifier_architecture(architecture_name, image_size, weights, classifier_kwargs=None): if architecture_name == 'MobileNet': model = MobileNetV2(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'VGG16': model = VGG16(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'VGG19': model = VGG19(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'NASNetMobile': model = NASNetMobile(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'NASNetLarge': model = NASNetLarge(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'InceptionV3': model = InceptionV3(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'InceptionResNetV2': model = InceptionResNetV2(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'Resnet50': model = ResNet50(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'EfficientNetB0': model = EfficientNetB0(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) else: raise ValueError( f"Classifier '{architecture_name}' is wrong or not implemented.") return model
def get_model(model_name): if model_name == 'VGG16': from tensorflow.keras.applications.vgg16 import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions model = VGG16(weights='imagenet') if model_name == 'VGG19': from tensorflow.keras.applications.vgg19 import VGG19 from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions model = VGG19(weights='imagenet') elif model_name == 'ResNet50': from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions from tensorflow.keras.applications.resnet50 import ResNet50 model = ResNet50(weights='imagenet') elif model_name == 'InceptionV3': from tensorflow.keras.applications.inception_v3 import preprocess_input, decode_predictions from tensorflow.keras.applications.inception_v3 import InceptionV3 model = InceptionV3(weights='imagenet') elif model_name == 'InceptionResNetV2': from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 model = InceptionResNetV2(weights='imagenet') elif model_name == 'Xception': from tensorflow.keras.applications.xception import preprocess_input, decode_predictions from tensorflow.keras.applications.xception import Xception model = Xception(weights='imagenet') elif model_name == 'MobileNet': from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions from tensorflow.keras.applications.mobilenet import MobileNet model = MobileNet(weights='imagenet') elif model_name == 'MobileNetV2': from tensorflow.keras.applications.mobilenetv2 import preprocess_input, decode_predictions from tensorflow.keras.applications.mobilenetv2 import MobileNetV2 model = MobileNetV2(weights='imagenet') elif model_name == 'DenseNet': from tensorflow.keras.applications.densenet import preprocess_input, decode_predictions from tensorflow.keras.applications.densenet import DenseNet121 model = DenseNet121(weights='imagenet') elif model_name == 'NASNet': from tensorflow.keras.applications.nasnet import preprocess_input, decode_predictions from tensorflow.keras.applications.nasnet import NASNetMobile model = NASNetMobile(weights='imagenet') elif model_name == 'EfficientNet': from efficientnet.tfkeras import EfficientNetB0 from keras.applications.imagenet_utils import decode_predictions from efficientnet.tfkeras import preprocess_input model = EfficientNetB7(weights='imagenet') else: print("[INFO] No model selected") return model, preprocess_input, decode_predictions
def get_model_nasnet_mobile(num_class): base_model = NASNetMobile(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 create_model_nasnet(self): input_tensor = Input(shape=self.input_shape) base_model = NASNetMobile(include_top=False, input_tensor=input_tensor) x = base_model(input_tensor) out_1 = GlobalMaxPooling2D()(x) out_2 = GlobalAveragePooling2D()(x) out_3 = Flatten()(x) out = Concatenate(axis=-1)([out_1, out_2, out_3]) out = Dropout(0.5)(out) out = Dense(self.output_shape, activation="sigmoid")(out) model = Model(input_tensor, out) model.compile(optimizer=Adam(self.learning_rate), loss=binary_crossentropy, metrics=['acc']) return model
def get_nasnetmobile(classes=9, input_shape=(224, 224, 3), base_layer_trainable=False): from tensorflow.keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(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='0000', 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) head_model = KL.Dense(classes, activation='softmax', name='3333')(head_model) model = KM.Model(inputs=base_model.input, outputs=head_model) return model
class Prediction: def __init__(self): self.model = NASNetMobile(weights="imagenet") @staticmethod def load_image(image): # img = np.fromstring(image, np.uint8) # img = cv2.imdecode(img, cv2.IMREAD_COLOR) # img = cv2.resize(img, dsize=(224, 224), interpolation=cv2.INTER_CUBIC) img = Image.open(BytesIO(image)) img = np.asarray(img.resize((224, 224))) img = np.expand_dims(img, 0) return img def predict(self, image): img = preprocess_input(image) preds = decode_predictions(self.model.predict(img), 10)[0] results = {} for pred in preds: results.update({pred[1]: f"{pred[2] * 100:.2f}%"}) return results
def __init__(self): self.model = NASNetMobile(weights="imagenet")
def create_model_nasnet(self, mode=None): """ Creates NasNetMobile model. Arguments: - mode - mode of architecture to be trained f.e linear means that we use linear head, non linear means that we use more dense layers with relu activation and meta means that we build model that includes both metadata and image data. Returns: - Model - Keras model that is ready to be fit. """ if mode == 'meta': input_tensor = Input(shape=self.input_shape[0], name='inp1') input_meta = Input(shape=self.input_shape[1], name='inp2') else: input_tensor = Input(shape=self.input_shape) base_model = NASNetMobile(include_top=False, input_tensor=input_tensor) x = base_model(input_tensor) if mode == 'linear': x = GlobalAveragePooling2D()(x) x = Dense(2048, activation='linear')(x) x = Dense(self.output_shape, activation='sigmoid')(x) model = Model(input_tensor, x) model.compile(optimizer=self.optimizer(self.learning_rate), loss=self.loss, metrics=self.metric) model.summary() return model elif mode == 'non_linear': out1 = AdaptiveMaxPooling2D((2, 2))(x) out2 = AdaptiveAveragePooling2D((2, 2))(x) out = Concatenate(axis=-1)([out1, out2]) out = Flatten()(out) out = Dense(2048, activation='relu')(out) out = Dropout(0.4)(out) out = Dense(1024, activation='relu')(out) out = Dropout(0.3)(out) out = Dense(512, activation='relu')(out) out = Dropout(0.2)(out) out = Dense(256, activation='relu')(out) out = Dropout(0.1)(out) out = Dense(self.output_shape, activation='sigmoid')(out) model = Model(input_tensor, out) model.compile(optimizer=self.optimizer(self.learning_rate), loss=self.loss, metrics=self.metric) model.summary() return model elif mode == 'meta': x = GlobalAveragePooling2D()(x) x = Dense(2048, activation='linear')(x) x = BatchNormalization()(x) x1 = Dense(128)(input_meta) x1 = LeakyReLU()(x1) x1 = BatchNormalization()(x1) concat = Concatenate()([x, x1]) x = Dense(1000, activation='relu')(x) x = BatchNormalization()(x) x = Dense(300, activation='relu')(x) x = BatchNormalization()(x) x = Dense(80, activation='relu')(x) x1 = Dense(60, activation='linear')(x) x1 = BatchNormalization()(x1) x1 = Dense(20, activation='linear')(x1) x1 = BatchNormalization()(x1) concat = Dense(1300, activation='relu')(concat) concat = BatchNormalization()(concat) concat = Dense(400, activation='relu')(concat) concat = BatchNormalization()(concat) concat = Dense(70, activation='relu')(concat) concat_layer = Concatenate()([x, x1, concat]) last_head = Dense(20, activation='relu')(concat_layer) last_head = BatchNormalization()(last_head) output = Dense(1, activation='sigmoid')(last_head) model = Model([input_tensor, input_meta], output) model.compile(optimizer=self.optimizer(self.learning_rate), loss=self.loss, metrics=self.metric) model.summary() return model
weights='imagenet', include_top=False) architecture_name = "InceptionResNetV2" elif architecture == 2: base_model = DenseNet121(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False) architecture_name = "DenseNet121" elif architecture == 3: base_model = ResNet50(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False) architecture_name = "ResNet50" elif architecture == 4: base_model = NASNetMobile(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False) architecture_name = "NASNetMobile" elif architecture == 5: base_model = MobileNet(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False) architecture_name = "MobileNet" elif architecture == 6: base_model = InceptionV3(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False) architecture_name = "InceptionV3" else: print("Wrong Architecture Input") x = base_model.output
elif args.net == 'xception': base_model = Xception(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'resnet_50': base_model = ResNet50(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'densenet_121': base_model = DenseNet121(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'densenet_169': base_model = DenseNet169(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'densenet_201': base_model = DenseNet201(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'mobilenet_v2': base_model = MobileNetV2(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'nasnetlarge': base_model = NASNetLarge(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'nasnetmobile': base_model = NASNetMobile(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) elif args.net == 'inceptionresnet_v2': base_model = InceptionResNetV2(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) else: print('Not supported network type') sys.exit() x = base_model.output x = GlobalAveragePooling2D()(x) x = Dropout(rate=args.dropout0)(x) x = Dense(args.dense1, use_bias=False, kernel_regularizer=l2(args.l21))(x) if args.bn1: x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(rate=args.dropout1)(x) if args.dense_layers >= 2:
def get_base_model(self, name='vgg16'): print('using model : ', name) if name == 'mobilenet_v2': from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 base_model = MobileNetV2(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'mobilenet': from tensorflow.keras.applications.mobilenet import MobileNet base_model = MobileNet(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'densenet121': from tensorflow.keras.applications.densenet import DenseNet121 base_model = DenseNet121(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'densenet169': from tensorflow.keras.applications.densenet import DenseNet169 base_model = DenseNet169(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'densenet201': from tensorflow.keras.applications.densenet import DenseNet201 base_model = DenseNet201(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'inception_resnet_v2': from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 base_model = InceptionResNetV2(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'inception_v3': from tensorflow.keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'nasnet_large': from tensorflow.keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'nasnet_mobile': from tensorflow.keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'resnet50': from tensorflow.keras.applications.resnet50 import ResNet50 base_model = ResNet50(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'xception': from tensorflow.keras.applications.xception import Xception base_model = Xception(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') elif name == 'vgg19': from tensorflow.keras.applications.vgg19 import VGG19 base_model = VGG19(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') else: from tensorflow.keras.applications.vgg16 import VGG16 # 采用VGG16为基本模型,include_top为False,表示FC层是可自定义的,抛弃模型中的FC层;该模型会在~/.keras/models下载基本模型 base_model = VGG16(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') # self.preprocess_input = preprocess_input return base_model
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