def classification( shape: Tuple[int, int, int] = (224, 224, 3), n_class: int = 3, model_net: str = "Resnet50V2", resnet_train: bool = True, ) -> Model: """ Modelo de classificação entre covid, normal e pneumonia Args: ----- input_size (tuple, optional): Tamanho da imagem de entrada. Defaults to (224, 224, 3). n_class (int, optional): Número de classes de saída. Defaults to 3. Returns: -------- (keras.Model) : Modelo do keras """ inputs = Input(shape, name="entrada_modelo") model = Conv2D( filters=3, kernel_size=(3, 3), padding="same", activation="relu", name="conv_gray_rgb", )(inputs) params = { "include_top": False, "weights": "imagenet", "input_shape": (shape[0], shape[1], 3), "pooling": "avg", } if model_net == "VGG19": base_model = VGG19(**params) elif model_net == "InceptionResNetV2": base_model = InceptionV3(**params) elif model_net == "MobileNetV2": base_model = MobileNetV3Small(**params) elif model_net == "DenseNet201": base_model = DenseNet201(**params) else: base_model = ResNet50V2(**params) base_model.trainable = resnet_train model = base_model(model) model = Dropout(0.5, name="drop_0")(model) model = Dense(units=256, name="dense_0")(model) model = Dropout(0.5, name="drop_1")(model) model = Dense(units=n_class, name="classifier")(model) predictions = Activation(activation="softmax", name="output")(model) return Model(inputs=inputs, outputs=predictions)
def init_model(model_name): if (model_name == "VGG19"): # Initialisierung des 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 __init__(self, top_model, dog_breed_names, image_shape=(224, 224, 3)): ''' Initializes the model. :param top_model: trained dog breed detector :param image_shape: image shape ''' self.cnn_model = DenseNet201(weights='imagenet', include_top=False, input_shape=image_shape) self.top_model = top_model self.model = Model(inputs=self.cnn_model.input, outputs=self.top_model(self.cnn_model.output)) self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) self.dog_breed_names = dog_breed_names
def __init__(self, train_data_dir, valid_data_dir, test_data_dir, image_shape=(224, 224, 3)): ''' Initializes the instance :param train_data_dir: directory containing train dog images. Expected structure: one subdirectory for one breed that contains corresponding dog breed images as jpegs :param valid_data_dir: same as train_data_dir for validation images :param test_data_dir: same as train_data_dir for test images :param images_shape: target image shape for the image resize ''' # data directories self.train_data_dir = train_data_dir self.valid_data_dir = valid_data_dir self.test_data_dir = test_data_dir # image generator self.batch_size = 16 self.image_shape = image_shape self.train_image_gen = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, preprocessing_function=preprocess_input) self.test_image_gen = ImageDataGenerator( preprocessing_function=preprocess_input) # DenseNet self.model = DenseNet201(weights='imagenet', include_top=False, input_shape=self.image_shape) # file names bottleneck_feature_dir = "bottleneck_features" if not os.path.exists(bottleneck_feature_dir): os.makedirs(bottleneck_feature_dir) self.train_bottleneck_features_filename = bottleneck_feature_dir + '/train.npy' self.valid_bottleneck_features_filename = bottleneck_feature_dir + '/valid.npy' self.test_bottleneck_features_filename = bottleneck_feature_dir + '/test.npy'
def get_model(self, model_name, weights): if model_name == 'InceptionV3': from tensorflow.python.keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(weights=weights, include_top=False) elif model_name == 'NASNetLarge': from tensorflow.python.keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(weights=weights, include_top=False) elif model_name == 'DenseNet201': from tensorflow.python.keras.applications.densenet import DenseNet201 base_model = DenseNet201(weights=weights, include_top=False) elif model_name == 'Xception': from tensorflow.python.keras.applications.xception import Xception base_model = Xception(weights=weights, include_top=False) elif model_name == 'VGG16': from tensorflow.python.keras.applications.vgg16 import VGG16 base_model = VGG16(weights=weights, include_top=False) elif model_name == 'VGG19': from tensorflow.python.keras.applications.vgg19 import VGG19 base_model = VGG19(weights=weights, include_top=False) elif model_name == 'NASNetMobile': from tensorflow.python.keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(weights=weights, include_top=False) elif model_name == 'MobileNetV2': from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2 base_model = MobileNetV2(weights=weights, include_top=False) elif model_name == 'ResNet50': from tensorflow.python.keras.applications.resnet50 import ResNet50 base_model = ResNet50(weights=weights, include_top=False) elif model_name == 'InceptionResNetV2': from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2 base_model = InceptionResNetV2(weights=weights, include_top=False, ) else: raise KeyError('Unknown network.') x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def get_model(self, model_name, weights='imagenet'): if model_name == 'InceptionV3': from tensorflow.python.keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(weights=weights, include_top=False) elif model_name == 'NASNetLarge': from tensorflow.python.keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(weights=weights, include_top=False) elif model_name == 'DenseNet201': from tensorflow.python.keras.applications.densenet import DenseNet201 base_model = DenseNet201(weights=weights, include_top=False) elif model_name == 'Xception': from tensorflow.python.keras.applications.xception import Xception base_model = Xception(weights=weights, include_top=False) elif model_name == 'VGG19': from tensorflow.python.keras.applications.vgg19 import VGG19 base_model = VGG19(weights=weights, include_top=False) elif model_name == 'NASNetMobile': from tensorflow.python.keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(weights=weights, include_top=False) elif model_name == 'MobileNetV2': from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2 base_model = MobileNetV2(weights=weights, include_top=False) elif model_name == 'ResNet50': from tensorflow.python.keras.applications.resnet50 import ResNet50 base_model = ResNet50(weights=weights, include_top=False) elif model_name == 'InceptionResNetV2': from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2 base_model = InceptionResNetV2(weights=weights, include_top=False) else: raise KeyError('Unknown network.') x = base_model.output x = GlobalAveragePooling2D()(x) # x = Dense(1024, activation='relu')(x) output = Dense(1, activation='sigmoid')(x) # output = SiameseLossLayer(self.batch_size, self.num_distort, self.num_level)(x) model = Model(inputs=base_model.input, outputs=output) self.name = model_name return model
def get_predictions(self, in_img, network_name, top=3): # network model selection model = None size = (224, 224) int_model = False if network_name == 'VGG19': if self.MODEL_VGG19 is None: from tensorflow.keras.applications.vgg19 import VGG19 self.MODEL_VGG19 = VGG19(weights='imagenet') model = self.MODEL_VGG19 int_model = True from tensorflow.keras.applications.vgg19 import decode_predictions elif network_name == 'DenseNet201': if self.MODEL_DENSENET201 is None: from tensorflow.python.keras.applications.densenet import DenseNet201 self.MODEL_DENSENET201 = DenseNet201(weights='imagenet') model = self.MODEL_DENSENET201 from tensorflow.keras.applications.densenet import decode_predictions elif network_name == 'MobileNetV2': if self.MODEL_MOBILENET is None: from tensorflow.keras.applications.mobilenet import MobileNet # (244,244) self.MODEL_MOBILENET = MobileNet(weights='imagenet') model = self.MODEL_MOBILENET from tensorflow.keras.applications.mobilenet import decode_predictions elif network_name == 'InceptionV3': if self.MODEL_INCEPTIONV3 is None: from tensorflow.keras.applications.inception_v3 import InceptionV3 # (299,299) self.MODEL_INCEPTIONV3 = InceptionV3(weights='imagenet') model = self.MODEL_INCEPTIONV3 size = (299, 299) from tensorflow.keras.applications.inception_v3 import decode_predictions elif network_name == 'ResNet50': if self.MODEL_RESNET50 is None: from tensorflow.python.keras.applications.resnet import ResNet50 # (244,244) self.MODEL_RESNET50 = ResNet50(weights='imagenet') model = self.MODEL_RESNET50 int_model = True from tensorflow.keras.applications.resnet import decode_predictions else: print("no valid model selected") return # if input is a numpy image, convert it to a cv image if (isinstance(in_img, numpy.ndarray)): in_img = _to_cv_image(in_img) img = cv2.resize(in_img, dsize=size, interpolation=cv2.INTER_CUBIC) img = _cv_to_array(img) if (int_model): img = img * 255 img = numpy.expand_dims(img, axis=0) # predict model and return top predictions features = model.predict(img) predictions = [] for p in decode_predictions(features, top=top)[0]: predictions.append([p[0], p[1], float(p[2])]) # get the indexes of the top predictions class_codes = numpy.flip(numpy.argsort(features[0]))[:top] return (predictions, class_codes) # ottiene nome della classe predetta
def create_model(self): """ https://github.com/tensorflow/tensorflow/issues/14356 """ input_shape = (self.config.tfr_image_height, self.config.tfr_image_width, self.config.tfr_image_channels) ## VGG16 if self.config.model_name == 'vgg16': base_model = VGG16(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) ## Xception elif self.config.model_name == 'xception': base_model = Xception(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) ## Resnet50 elif self.config.model_name == 'resnet50': base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_resnet50(base_model) ## InceptionResNetV2 elif self.config.model_name == 'inception_resnet_v2': base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) ## Densenet121 elif self.config.model_name == 'densenet121': base_model = DenseNet121(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_densenet121(base_model) ## Densenet169 elif self.config.model_name == 'densenet169': base_model = DenseNet169(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_densenet121(base_model) ## Densenet201 elif self.config.model_name == 'densenet201': base_model = DenseNet201(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_densenet121(base_model) else: logging.error('Unknown model_name {}'.format(model_name)) exit(1) return logits
import tensorflow as tf from tensorflow.python import keras from tensorflow.python.keras.applications.vgg16 import VGG16 from tensorflow.python.keras.applications.vgg19 import VGG19 from tensorflow.python.keras.applications.densenet import DenseNet121 from tensorflow.python.keras.applications.densenet import DenseNet201 from tensorflow.python.keras.applications.resnet50 import ResNet50 from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2 from tensorflow.python.keras.models import Model # Für jedes Modell wird die Summary ausgegeben print("=== VGG 16 ===") VGG16().summary() print("=== VGG 19 ===") VGG19().summary() print("=== ResNet50 ===") ResNet50().summary() print("=== DenseNet121 ===") DenseNet121().summary() print("=== DenseNet201 ===") DenseNet201().summary() print("=== InceptionResNetV2 ===") InceptionResNetV2().summary()