def reconstruct_model(checkpoint_filepath, ifMC): """ This function reconstruct models with input trained models. The model is DenseNet 201 + DropOut layer. If it is a multi-class model, there are 5 nodes at the softmax layer. If it is a decision flow model, there are only 2 nodes at the softmax layer. Trained model is then returned. """ # Use DenseNet 201 denseNet = applications.DenseNet201(include_top=False, weights=None, input_shape=(128, 128, 3), pooling='avg') output = denseNet.output output = layers.Dropout(0.5)(output) # Add Dropout layer if ifMC == 0: output = layers.Dense(2, activation='softmax')(output) elif ifMC == 1: output = layers.Dense(5, activation='softmax')(output) model = tf.keras.models.Model(denseNet.input, output) opt = tf.keras.optimizers.SGD(learning_rate=0.0002, momentum=0.7) loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True) model.compile(optimizer=opt, loss=loss, metrics=['accuracy', 'AUC']) model.load_weights(checkpoint_filepath) # Load trained weights return model
def dn201(img_size=(224,224), num_class=2, weights="imagenet", dtype=tf.float32): input_layer = layers.Input(shape=(img_size[0],img_size[1],3), dtype=dtype) base = models.DenseNet201(input_tensor=input_layer, include_top=False, weights=weights) base.trainable = True x = base.output x = layers.GlobalAveragePooling2D()(x) x = layers.Dense(num_class)(x) preds = layers.Activation("softmax", dtype=tf.float32)(x) model = tf.keras.models.Model(inputs=input_layer, outputs=preds) return model
def compute_mean_and_std(model_name, X, input_shape): if model_name == 'Xception': model = applications.Xception(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'VGG16': model = applications.VGG16(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'VGG19': model = applications.VGG19(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'ResNet50': model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'InceptionResNetV2': model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'InceptionV3': model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'MobileNet': model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'DenseNet121': model = applications.DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'DenseNet169': model = applications.DenseNet169(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'DenseNet201': model = applications.DenseNet201(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'NASNetMobile': model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'NASNetLarge': model = applications.NASNetLarge(weights='imagenet', include_top=False, input_shape=input_shape) else: assert (False), "Specified base model is not available !" features = model.predict(X)[:, 0, 0, :] return features.mean(axis=0), features.std(axis=0)
def init_network(self): model = applications.DenseNet201(include_top=False, weights='imagenet', input_shape=self.input_size) x = model.output x = AveragePooling2D(pool_size=(4, 4))(x) x = Flatten()(x) x = Dense(128, activation="relu")(x) x = Dropout(0.5)(x) output = Dense(len(self.CLASS_TARGETS), activation="softmax")(x) for layer in model.layers: layer.trainable = False # create Model self.model = Model(inputs=model.inputs, outputs=output) # compile Model self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
def reconstruct_model(checkpoint_filepath, ifMC): denseNet = applications.DenseNet201(include_top=False, weights=None, input_shape=(128, 128, 3), pooling='avg') output = denseNet.output output = layers.Dropout(0.5)(output) if ifMC == 0: output = layers.Dense(2, activation='softmax')(output) elif ifMC == 1: output = layers.Dense(5, activation='softmax')(output) model = tf.keras.models.Model(denseNet.input, output) opt = tf.keras.optimizers.SGD(learning_rate=0.0002, momentum=0.7) loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True) model.compile(optimizer=opt, loss=loss, metrics=['accuracy', 'AUC']) model.load_weights(checkpoint_filepath) return model
y_train = y_train.reshape(len(y_train), 1) x_test = x_test.reshape(len(x_test), 128, 128, 3) / 255.0 y_test = y_test.reshape(len(y_test), 1) x_validation = x_validation.reshape(len(x_validation), 128, 128, 3) / 255.0 y_validation = y_validation.reshape(len(y_validation), 1) """ Convert to one hot encoding """ y_train = tf.keras.utils.to_categorical(y_train, 2) y_test = tf.keras.utils.to_categorical(y_test, 2) y_validation = tf.keras.utils.to_categorical(y_validation, 2) """ Model setup """ denseNet = applications.DenseNet201(include_top=False, weights="imagenet", input_shape=(128, 128, 3), pooling='avg') output = denseNet.output output = layers.Dropout(0.5)(output) output = layers.Dense(2, activation='softmax')(output) model = tf.keras.models.Model(denseNet.input, output) model.trainable = True regularizer = tf.keras.regularizers.l2(0.0001) for layer in model.layers: for attr in ['kernel_regularizer']: if hasattr(layer, attr): setattr(layer, attr, regularizer)
def estimate(X_train, y_train, back_bone): IMAGE_WIDTH = 224 # Image width IMAGE_HEIGHT = 224 # Image height input_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, 3) # (width, height, channel) channel = 3 ---> RGB batch_size = 8 epochs = 1 # Number of epochs ntrain = 0.8 * len(X_train) # split data with 80/20 train/validation nval = 0.2 * len(X_train) back_bone = str(back_bone) print("Using " + back_bone + "...") X = [] X_train = np.reshape(np.array(X_train), [len(X_train), ]) for img in list(range(0, len(X_train))): if X_train[img].ndim >= 3: X.append(cv2.resize( X_train[img][:, :, :3], (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation=cv2.INTER_CUBIC)) else: smimg = cv2.cvtColor(X_train[img][0], cv2.COLOR_GRAY2RGB) X.append(cv2.resize(smimg, (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation=cv2.INTER_CUBIC)) if y_train[img] == 'COVID': y_train[img] = 1 elif y_train[img] == 'NonCOVID': y_train[img] = 0 else: continue x = np.array(X) X_train, X_val, y_train, y_val = train_test_split( # 20% validation set x, y_train, test_size=0.20, random_state=2) # data generator if back_bone == 'ResNet50V2' or back_bone =='1': train_datagen = ImageDataGenerator( preprocessing_function=resnet_preprocess, rotation_range=15, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1 ) val_datagen = ImageDataGenerator(preprocessing_function=resnet_preprocess) elif back_bone == 'Xception' or back_bone =='2': train_datagen = ImageDataGenerator( preprocessing_function=xception_preprocess, rotation_range=15, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1 ) val_datagen = ImageDataGenerator(preprocessing_function=xception_preprocess) elif back_bone == "DenseNet201" or back_bone =='3': train_datagen = ImageDataGenerator( preprocessing_function=denset_preprocess, rotation_range=15, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1 ) val_datagen = ImageDataGenerator(preprocessing_function=denset_preprocess) elif back_bone == "MobileNetV2" or back_bone == '4': train_datagen = ImageDataGenerator( preprocessing_function= mobile_preprocess, rotation_range=15, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1 ) val_datagen = ImageDataGenerator(preprocessing_function=mobile_preprocess) else: raise ValueError('Please select transfer learning model!') train_generator = train_datagen.flow( X_train, y_train, batch_size=batch_size, shuffle=True) val_generator = val_datagen.flow( X_val, y_val, batch_size=batch_size, shuffle=True) # model model = Sequential() if back_bone == 'ResNet50V2' or back_bone == '1': base_model = applications.resnet_v2.ResNet50V2( include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape) elif back_bone == 'Xception' or back_bone == '2': base_model = applications.Xception( include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape) elif back_bone == 'DenseNet201' or back_bone =='3': base_model = applications.DenseNet201( include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape) elif back_bone == 'MobileNetV2' or back_bone =='4': base_model = applications.MobileNetV2( include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape) else: raise ValueError('Please select transfer learning model!') base_model.trainable = False model.add(base_model) model.add(BatchNormalization()) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.1)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(lr=1e-4), metrics=['acc']) # callbacks lr_reducer = ReduceLROnPlateau( monitor='val_loss', factor=0.8, patience=5, verbose=1, mode='auto', min_delta=0.0001, cooldown=3, min_lr=1e-14) best_loss_path = "Model.h5" ck_loss_model = ModelCheckpoint( best_loss_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min') callbacks = [ck_loss_model, lr_reducer] history = model.fit_generator(train_generator, steps_per_epoch=ntrain//batch_size, epochs=epochs, validation_data=val_generator, validation_steps=nval // batch_size, callbacks=callbacks) return model
def build_autoencoder(base_model_name, input_shape, imagenet_mean, imagenet_std, hidden_layer_size, n_classes, weight_decay): if base_model_name == 'Xception': base_model = applications.Xception(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'VGG16': base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'VGG19': base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'ResNet50': base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'InceptionResNetV2': base_model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'InceptionV3': base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'MobileNet': base_model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'DenseNet121': base_model = applications.DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'DenseNet169': base_model = applications.DenseNet169(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'DenseNet201': base_model = applications.DenseNet201(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'NASNetMobile': base_model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'NASNetLarge': base_model = applications.NASNetLarge(weights='imagenet', include_top=False, input_shape=input_shape) else: assert (False), "Specified base model is not available !" n_features = base_model.output.shape[-1] x = base_model.output x = tf.keras.layers.Lambda(lambda x: (x - imagenet_mean) / imagenet_std)( x) # normalization x = tf.keras.layers.Activation(activation='sigmoid', name='encoder')(x) # sigmoid x = tf.keras.layers.Dense(units=hidden_layer_size, activation=None)(x) # encoding x = tf.keras.layers.Activation(activation='relu')(x) # relu x = tf.keras.layers.Dense(units=n_features, activation=None, name='decoder')(x) # decoding x = tf.keras.layers.Dense(units=n_classes, activation='sigmoid')( x) # x = tf.keras.layers.Activation(activation='sigmoid')(x) # sigmoid model = tf.keras.Model(inputs=base_model.input, outputs=x[:, 0, 0, :]) for layer in model.layers: if isinstance(layer, tf.keras.layers.Conv2D) or isinstance( layer, tf.keras.layers.Dense): layer.add_loss( tf.keras.regularizers.l2(weight_decay)(layer.kernel)) if hasattr(layer, 'bias_regularizer') and layer.use_bias: layer.add_loss(tf.keras.regularizers.l2(weight_decay)(layer.bias)) return model
return image_fingerprint_vector tf.compat.v1.set_random_seed(1234) model1 = applications.EfficientNetB7(weights='imagenet', include_top=False, pooling='avg') model2 = applications.EfficientNetB6(weights='imagenet', include_top=False, pooling='avg') model3 = applications.inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, pooling='avg') model4 = applications.DenseNet201(weights='imagenet', include_top=False, pooling='avg') model5 = applications.inception_v3.InceptionV3(weights='imagenet', include_top=False, pooling='avg') model6 = applications.NASNetLarge(weights='imagenet', include_top=False, pooling='avg') model7 = applications.ResNet152V2(weights='imagenet', include_top=False, pooling='avg') img = image.load_img('Ardee_Arollado__Number_08.png', target_size=(224, 224)) x = image.img_to_array(img) # convert image to numpy array x = np.expand_dims( x, axis=0