def DenseNet(pretrained=True, tnb_extractor=True): DenseNet121().summary() VGG16().summary() if (pretrained): feature_extractor = DenseNet121(weights='imagenet', include_top=False, input_shape=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH, config.CHANNELS)) feature_extractor.trainable = tnb_extractor else: feature_extractor = DenseNet121(include_top=False, input_shape=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH, config.CHANNELS)) feature_extractor.trainable = True feature_extractor.summary() x = Conv2D(1024, (7, 7), activation='relu', padding='same')(feature_extractor.output) x = Dropout(0.5)(x) '''x = Conv2D(4096, (1, 1), activation='relu', padding='same')(x) x = Dropout(0.5)(x)''' x = Conv2D(5, (1, 1), activation='linear')(x) x = Conv2DTranspose(5, kernel_size=(64, 64), strides=(32, 32), padding='same')(x) # x = Reshape((IMAGE_WIDTH*IMAGE_HEIGHT, -1))(x) outputs = Softmax(axis=-1)(x) '''outputs = Lambda(prob_to_labels)(x) outputs = Reshape((224, 224))(outputs)''' model = Model(inputs=feature_extractor.inputs, outputs=outputs, name="DenseNet") model.summary() return model
def build_densenet121(train_model=True, **kwargs): inputs = Input(shape=(None, None, len(kwargs.get('channels')) ), name='inputlayer_0') weights = kwargs.get('weights') if weights is None: print("instantiated with random weights") densenet121 = DenseNet121(input_tensor=inputs, weights=weights, include_top=False) densenet121 = densenet121.output else: print("instantiated model with weights: {}".format(weights)) #dense_filter = Conv2D(filters=3, kernel_size=(3,3), padding='same')(inputs) x = Conv2D(3, (1,1))(inputs) densenet121 = DenseNet121(weights=weights, include_top=False)(x) if kwargs.get('top') == False: # top is not needed for object detectors model = Model(inputs=inputs, outputs=densenet121) else: output = GlobalAveragePooling2D()(densenet121) output = Dense(kwargs.get('num_classes'), activation='softmax')(output) model = Model(inputs=inputs, outputs=output) model.summary() return model
def build(self): pre_trained_model = DenseNet121(input_shape=(self.width, self.height, self.depth), include_top=False, weights="imagenet") # local_weights_file = self.config["local_weights_file"] # pre_trained_model.load_weights(local_weights_file) for layer in pre_trained_model.layers: layer.trainable = False last_layer = pre_trained_model.get_layer('conv4_block17_concat') print('last layer output shape :', last_layer.output_shape) last_output = last_layer.output # Flatten the output layer to 1 dimension x = tf.keras.layers.Flatten()(last_output) # Add a fully connected layer with 1,024 hidden units and ReLU activation x = tf.keras.layers.Dense(256, activation='relu')(x) # Add a dropout rate of 0.2 x = tf.keras.layers.Dropout(0.2)(x) # Add a final sigmoid layer for classification x = tf.keras.layers.Dense(self.classes, activation='softmax')(x) model = tf.keras.Model(pre_trained_model.input, x) return model
def buildDenseNet121Model(img_height, img_width, img_channl, num_classes, num_GPU): inputs = Input(shape=(img_height, img_width, img_channl)) # -------------------------------------------- StopNum = 0 AppModel = DenseNet121(include_top=False, pooling='avg', weights='imagenet') for idx, layer in enumerate(AppModel.layers): layer.trainable = False if (idx == StopNum): break x = AppModel.layers[-1].output x = Flatten()(x) x = BatchNormalization()(x) x = Dropout(0.5)(x) output_layer = Dense(num_classes, activation='sigmoid', name='sigmoid')(x) model = Model(inputs=AppModel.input, outputs=output_layer) # -------------------------------------------- model.summary() if (num_GPU > 1): model = multi_gpu_model(model, gpus=num_GPU) model.compile( loss='binary_crossentropy', optimizer=Adam(lr=1e-4), #0.001 metrics=['categorical_accuracy', 'binary_accuracy', f1_m]) return model
def create_model(image_shape=(224, 224, 3), restart_checkpoint=None, backbone='mobilnetv2', feature_len=128, freeze=False): """ Creates an image encoder. Args: image_shape: input image shape (use [None, None] for resizable network) restart_checkpoint: snapshot to be restored backbone: the backbone CNN (one of mobilenetv2, densent121, custom) feature_len: the length of the additional feature layer freeze: freeze the backbone """ input_img = Input(shape=image_shape) # add the backbone backbone_name = backbone if backbone_name == 'densenet121': print('Using DenseNet121 backbone.') backbone = DenseNet121(input_tensor=input_img, include_top=False) backbone.layers.pop() if freeze: for layer in backbone.layers: layer.trainable = False backbone = backbone.output elif backbone_name == 'mobilenetv2': print('Using MobileNetV2 backbone.') backbone = MobileNetV2(input_tensor=input_img, include_top=False) backbone.layers.pop() if freeze: for layer in backbone.layers: layer.trainable = False backbone = backbone.output elif backbone_name == 'custom': backbone = custom_backbone(input_tensor=input_img) else: raise Exception('Unknown backbone: {}'.format(backbone_name)) # add the head layers gmax = GlobalMaxPool2D()(backbone) gavg = GlobalAvgPool2D()(backbone) gmul = Multiply()([gmax, gavg]) ggavg = Average()([gmax, gavg]) backbone = Concatenate()([gmax, gavg, gmul, ggavg]) backbone = BatchNormalization()(backbone) backbone = Dense(feature_len)(backbone) backbone = Activation('sigmoid')(backbone) encoder = Model(input_img, backbone) if restart_checkpoint: print('Loading weights from {}'.format(restart_checkpoint)) encoder.load_weights(restart_checkpoint, by_name=True, skip_mismatch=True) return encoder
def build_base_model(self): # build the Densenet network densenet = DenseNet121(weights='imagenet', include_top=False, input_shape=self.input_shape) for layer in densenet.layers: layer.trainable = False # make batch normalization layers trainable to prevent overfitting for layer in densenet.layers: if "BatchNormalization" in layer.__class__.__name__: layer.trainable = True x = densenet.output x = Flatten()(x) layer_units = (64, 16) for num_units in layer_units: x = Dense(num_units, activation='relu')(x) x = Dropout(0.4)(x) predictions = Dense(self.num_classes, activation='softmax')(x) custom_model = Model(inputs=densenet.input, outputs=predictions) optimizer = optimizers.Adam(lr=self.learning_rate) custom_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=self.metrics) self.model = custom_model
def pre_train_wh_fine_tuning(self): base = DenseNet121(input_shape=self.input_shape, weights='imagenet', include_top=False) for layer in base.layers: layer.trainable = False for layer in base.layers[-3:]: layer.trainable = True return base
def get_model(class_names, model_name="DenseNet121", train_weights_path=None, input_shape=None, weights="imagenet"): if train_weights_path is not None: # 不加载预训练模型权重 weights = None # 加载预训练模型,原有1000个神经元的全连接层替换为有14个神经元的全连接层 base_DenseNet121_model = DenseNet121( include_top=False, # 不加载最后一层 weights=weights, pooling="avg") # 平均池化 x = base_DenseNet121_model.output predictions = Dense(len(class_names), activation="sigmoid", name="predictions")(x) # 14个神经元的全连接层 model = Model(inputs=base_DenseNet121_model.input, outputs=predictions, name='my' + model_name) # 函数式API构建模型 # 再加载训练的模型 if train_weights_path is not None: print(f"load model weights_path: {train_weights_path}") model.load_weights(train_weights_path) return model
def make_model_DenseNet121(output_bias_init=None): retrofitted_model = Sequential() base_model = DenseNet121(include_top=False, pooling='avg', weights='imagenet', input_shape=(224, 224, 3)) base_model.summary() for layer in base_model.layers[0:313]: layer.trainable = False for layer in base_model.layers: print(layer.name, layer.trainable) # 1st layer as the lumpsum weights from resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 # NOTE that this layer will be set below as NOT TRAINABLE, i.e., use it as is retrofitted_model.add(base_model) # retrofitted_model.add(Dropout(0.333)) # retrofitted_model.add(Dense(1024, activation='relu')) retrofitted_model.add(Dropout(0.333)) retrofitted_model.add(Dense(512, activation='relu')) retrofitted_model.add(Dropout(0.333)) retrofitted_model.add(Dense(256, activation='relu')) # 2nd layer as Dense for 2-class classification, i.e., dog or cat using SoftMax activation # retrofitted_model.add(Dense(2, activation='softmax'))""" retrofitted_model.add( Dense(1, activation='sigmoid', bias_initializer=output_bias_init)) # Say not to train first layer (ResNet) model as it is already trained # retrofitted_model.layers[0].trainable = False return retrofitted_model
def pretrained_model3(): from tensorflow.keras.applications.densenet import DenseNet121 pretrained_model3 = DenseNet121(include_top=False, input_shape=(224, 224, 3), weights='imagenet', layers=tf.keras.layers) for layer in pretrained_model3.layers[:-200]: layer.trainable = False model3 = Sequential() # first (and only) set of FC => RELU layers model3.add(layers.AveragePooling2D((2, 2), name='avg_pool')) model3.add(layers.Flatten()) model3.add(layers.Dense(64, activation='relu')) model3.add(layers.Dropout(0.3)) model3.add(layers.Dense(4, activation='softmax')) preinput3 = pretrained_model3.input preoutput3 = pretrained_model3.output output3 = model3(preoutput3) model3 = Model(preinput3, output3) model3.summary() return model3
def loadPretrainedWeights(): pretrained_weights={} pretrained_weights['vgg16']=VGG16(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['vgg19']=VGG19(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['resnet50']=ResNet50(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inceptionv3']=InceptionV3(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inception-resentv2']=InceptionResNetV2(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['xception']=Xception(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet121']=DenseNet121(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet169']=DenseNet169(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet201']=DenseNet201(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['mobilenet']=MobileNet(weights='imagenet', include_top=False,pooling='avg') #N retrained_weights['nasnetlarge']=NASNetLarge(weights='imagenet', include_top=False,pooling='avg',input_shape = (224, 224, 3)) #N pretrained_weights['nasnetmobile']=NASNetMobile(weights='imagenet', include_top=False,pooling='avg') #N pretrained_weights['mobilenetV2']=MobileNetV2(weights='imagenet', include_top=False,pooling='avg') return pretrained_weights
def get_densenet121_unet_softmax(input_shape, weights='imagenet'): blocks = [6, 12, 24, 16] img_input = Input(input_shape + (4, )) x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) x = Activation('relu', name='conv1/relu')(x) conv1 = x x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = MaxPooling2D(3, strides=2, name='pool1')(x) x = dense_block(x, blocks[0], name='conv2') conv2 = x x = transition_block(x, 0.5, name='pool2') x = dense_block(x, blocks[1], name='conv3') conv3 = x x = transition_block(x, 0.5, name='pool3') x = dense_block(x, blocks[2], name='conv4') conv4 = x x = transition_block(x, 0.5, name='pool4') x = dense_block(x, blocks[3], name='conv5') x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x) conv5 = x conv6 = conv_block(UpSampling2D()(conv5), 320) conv6 = concatenate([conv6, conv4], axis=-1) conv6 = conv_block(conv6, 320) conv7 = conv_block(UpSampling2D()(conv6), 256) conv7 = concatenate([conv7, conv3], axis=-1) conv7 = conv_block(conv7, 256) conv8 = conv_block(UpSampling2D()(conv7), 128) conv8 = concatenate([conv8, conv2], axis=-1) conv8 = conv_block(conv8, 128) conv9 = conv_block(UpSampling2D()(conv8), 96) conv9 = concatenate([conv9, conv1], axis=-1) conv9 = conv_block(conv9, 96) conv10 = conv_block(UpSampling2D()(conv9), 64) conv10 = conv_block(conv10, 64) res = Conv2D(3, (1, 1), activation='softmax')(conv10) model = Model(img_input, res) if weights == 'imagenet': densenet = DenseNet121(input_shape=input_shape + (3, ), weights=weights, include_top=False) w0 = densenet.layers[2].get_weights() w = model.layers[2].get_weights() w[0][:, :, [0, 1, 2], :] = 0.9 * w0[0][:, :, :3, :] w[0][:, :, 3, :] = 0.1 * w0[0][:, :, 1, :] model.layers[2].set_weights(w) for i in range(3, len(densenet.layers)): model.layers[i].set_weights(densenet.layers[i].get_weights()) model.layers[i].trainable = False return model
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 extract_transfer_learning_features(img_path): model = DenseNet121(weights='imagenet', include_top=False) img = image.load_img(img_path, target_size=(331, 331)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = model.predict(x).flatten() return features
def get_densenet(num_classes): base_model = DenseNet121(include_top=False, weights='imagenet') x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(num_classes, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def densenet121(): base_model = DenseNet121(include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=x) model.compile(optimizer='adam', loss='binary_crossentropy') return model
def get_pretrained_model(type): arr = range(308, 133, -7) densenet = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) pretrained = Model(inputs=densenet.input, outputs=densenet.get_layer(index=arr[type]).output) del densenet return pretrained
def DenseNet121_cifar10(): inputs = Input((32, 32, 3)) x = DenseNet121(weights=None, input_shape=(32, 32, 3), include_top=False, pooling='avg')(inputs) outputs = Dense(10, activation='softmax')(x) model = tf.keras.Model(inputs=inputs, outputs=outputs) return model
def __init__(self): super(Model, self).__init__() base_model = DenseNet121( include_top=False, weights=None, input_shape=(224, 224, 3), ) base_model.load_weights( "../input/densenet-keras/DenseNet-BC-121-32-no-top.h5") self.model = base_model
def __get_models(self): input_shape = (224, 224, 3) # Densenet121 img_input_dennsenet121 = Input(shape=input_shape) base_model_dennsenet121 = DenseNet121( include_top=False, input_tensor=img_input_dennsenet121, input_shape=input_shape, pooling='avg', weights=None) predictions_dennsenet121 = Dense(15, activation='sigmoid', name='predictions')( base_model_dennsenet121.output) model_dennsenet121 = Model(inputs=img_input_dennsenet121, outputs=predictions_dennsenet121) model_dennsenet121.load_weights( settings['models']['densenet121']['weights']) # Mobilenet img_input_mobilenet = Input(shape=input_shape) base_model_mobilenet = MobileNetV2(include_top=False, input_tensor=img_input_mobilenet, input_shape=input_shape, pooling='avg', weights=None) predictions_mobilenet = Dense(15, activation='sigmoid', name='predictions')( base_model_mobilenet.output) model_mobilenet = Model(inputs=img_input_mobilenet, outputs=predictions_mobilenet) model_mobilenet.load_weights( settings['models']['mobilenet']['weights']) # VGG16 img_input_vgg16 = Input(shape=input_shape) base_model_vgg16 = VGG16(include_top=False, input_tensor=img_input_vgg16, input_shape=input_shape, pooling="avg", weights=None) predictions_vgg16 = Dense(15, activation="sigmoid", name="predictions")(base_model_vgg16.output) model_vgg16 = Model(inputs=img_input_vgg16, outputs=predictions_vgg16) model_vgg16.load_weights(settings['models']['vgg16']['weights']) return { settings['models']['densenet121']['name']: model_dennsenet121, settings['models']['mobilenet']['name']: model_mobilenet, settings['models']['vgg16']['name']: model_vgg16, }
def load_model(): labels = [ 'Cardiomegaly', 'Emphysema', 'Effusion', 'Hernia', 'Infiltration', 'Mass', 'Nodule', 'Atelectasis', 'Pneumothorax', 'Pleural_Thickening', 'Pneumonia', 'Fibrosis', 'Edema', 'Consolidation' ] train_df = pd.read_csv("nih_new/train-small.csv") valid_df = pd.read_csv("nih_new/valid-small.csv") test_df = pd.read_csv("nih_new/test.csv") class_pos = train_df.loc[:, labels].sum(axis=0) class_neg = len(train_df) - class_pos class_total = class_pos + class_neg pos_weights = class_pos / class_total neg_weights = class_neg / class_total print("Got loss weights") # create the base pre-trained model base_model = DenseNet121(weights='densenet.hdf5', include_top=False) print("Loaded DenseNet") # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # and a logistic layer predictions = Dense(len(labels), activation="sigmoid")(x) print("Added layers") model = Model(inputs=base_model.input, outputs=predictions) def get_weighted_loss(neg_weights, pos_weights, epsilon=1e-7): def weighted_loss(y_true, y_pred): # L(X, y) = −w * y log p(Y = 1|X) − w * (1 − y) log p(Y = 0|X) # from https://arxiv.org/pdf/1711.05225.pdf loss = 0 for i in range(len(neg_weights)): loss -= ( neg_weights[i] * y_true[:, i] * K.log(y_pred[:, i] + epsilon) + pos_weights[i] * (1 - y_true[:, i]) * K.log(1 - y_pred[:, i] + epsilon)) loss = K.sum(loss) return loss return weighted_loss model.compile(optimizer='adam', loss=get_weighted_loss(neg_weights, pos_weights)) print("Compiled Model") model.load_weights("nih_new/pretrained_model.h5") print("Loaded Weights") 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 model_v0_1_dense(input_size=448, d_rate=1, Finetune=False): base_model = DenseNet121(input_shape=(input_size, input_size, 3), include_top=False) base_model.summary() x = base_model.layers[-3].output prediction = upsampling_module(x) model = Model(inputs=base_model.input, outputs=prediction) if Finetune: for layer in base_model.layers: layer.trainable = False return model
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 main(): baseModel = DenseNet121(include_top=False, pooling='avg', weights='imagenet') fire_detector_model = lib.createModel(baseModel, hidden_layers, num_classes) history = lib.trainModel(dataset, fire_detector_model, epochs, batch_size, image_size, preprocess_input) lib.create_pdf(history, model_name) lib.testModel(fire_detector_model, batch_size, dataset, num_classes, model_name, image_size, preprocess_input, output_statistics) fire_detector_model.save(f'saved_models/{model_name}.h5')
def init_model(model_name): if (model_name == "VGG19"): return VGG19(include_top=True, weights='imagenet') if (model_name == "VGG16"): return tf.keras.applications.VGG16(include_top=True, weights='imagenet') if (model_name == "ResNet50"): return ResNet50(include_top=True, weights="imagenet") if (model_name == "DenseNet201"): return DenseNet201(include_top=True, weights="imagenet") if (model_name == "DenseNet121"): return DenseNet121(include_top=True, weights="imagenet") if (model_name == "InceptionResNetV2"): return InceptionResNetV2(include_top=True, weights="imagenet")
def create_model(input_shape, n_out): input_tensor = Input(shape=input_shape) base_model = DenseNet121(include_top=False, weights=None, input_tensor=input_tensor) base_model.load_weights( "../input/densenet-keras/DenseNet-BC-121-32-no-top.h5") x = GlobalAveragePooling2D()(base_model.output) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) final_output = Dense(n_out, activation='softmax', name='final_output')(x) model = Model(input_tensor, final_output) return model
def __init__(self, input_size): inputs = Input(shape=(input_size[0], input_size[1], 5), name='inputlayer_0') x = Conv2D(3, (1,1))(inputs) densenet121 = DenseNet121(weights= 'imagenet', include_top=False)(x) self.feature_extractor = Model(inputs=inputs, outputs=densenet121) try: print("loading backend weights") self.feature_extractor.load_weights(DENSENET121_BACKEND_PATH) except: print("Unable to load backend weights. Using a fresh model") self.feature_extractor.summary()
def import_image_encoder(next_X, mesh_embedding_size, cam_embedding_size, name=None): features = DenseNet121(weights='imagenet', include_top=False)(next_X) features = GlobalAveragePooling2D()(features) features = tf.layers.flatten(features) embedding = tf.layers.dense(features, mesh_embedding_size + cam_embedding_size, name=name) mesh_embedding, camera_embedding = embedding[:, : mesh_embedding_size], embedding[:, mesh_embedding_size:] return mesh_embedding, camera_embedding
def build_CheXNet_model_(self): """ Network built as in Rajpurkar, et al. "CheXnet: Radiologist-level pneumonia detection on chest x-rays with deep learning." arXiv preprint arXiv:1711.05225 (2017). :return: """ if self.use_imagenet_weights: base_weights = 'imagenet' else: base_weights = None densenet_121 = DenseNet121(weights=base_weights, include_top=False, pooling='avg') x = densenet_121.output output_layer = tf.keras.layers.Dense(3, activation='softmax', name='predictions')(x) return tf.keras.models.Model(inputs=densenet_121.input, outputs=output_layer)