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(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(): # setup session config = tf.ConfigProto() config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU sess = tf.Session(config=config) tf.keras.backend.set_session(sess) # get and shuffle batch paths # datasetPaths= exBC.preparePaths(rootFolderLocal+'Prepared-Data') datasetPaths = exBC.preparePaths(rootFolderGCP + 'Prepared-Data') random.shuffle(datasetPaths) batchAmt = len(datasetPaths) testAmt = math.ceil(batchAmt * .1) # 10% set aside for testing testingPaths = datasetPaths[:testAmt] trainingPaths = datasetPaths[testAmt:] writeVideo(testingPaths) print("Test Video Written") # prepare the model # untrainedModel = prepModel((64, 64, 3)) # untrainedModel.summary() # train the model # trainedModel = trainModelHDD(trainingPaths, testingPaths, untrainedModel) base = NASNetLarge(weights='imagenet', include_top=False) new = base.output new = GlobalAveragePooling2D()(new) new = Dense(1024, activation='relu', kernel_regularizer=regularizers.l2(0.01))(new) new = Dense(512, activation='relu')(new) final = Dense(12, activation='sigmoid')(new) untrainedModel = Model(inputs=base.input, outputs=final) for layer in base.layers: layer.trainable = False untrainedModel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy']) untrainedModel.summary() trainedModel = trainModelHDD(trainingPaths, testingPaths, untrainedModel) print("model training finished") # save the model for the server trainedModel.save("model.h5") print("Model Saved") testModel(testingPaths, trainedModel) print("Complete")
def __init__(self, name): if name == 'nas': from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input self.model = NASNetLarge(include_top=False, weights='imagenet', pooling='avg') self.size = 331 if name == 'res': from tensorflow.keras.applications.resnet import ResNet50, preprocess_input self.model = ResNet50(include_top=False, weights='imagenet', pooling='avg') self.size = 224 self.proc = preprocess_input self.model.trainable = False
def get_model_nasnet_large(num_class): base_model = NASNetLarge(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 get_basemodel(model_name): if model_name == "MobileNetV2": from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 return MobileNetV2(include_top=True,weights='imagenet',classes=1000) elif model_name == "ResNet50": from tensorflow.keras.applications.resnet50 import ResNet50 return ResNet50(include_top=True,weights='imagenet',classes=1000) elif model_name=="InceptionV2": from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 return InceptionResNetV2(include_top=True,weights='imagenet',classes=1000) elif model_name=="DenseNet201": from tensorflow.keras.applications.densenet import DenseNet201 return DenseNet201(include_top=True,weights='imagenet',classes=1000) elif model_name=="NASNetLarge": from tensorflow.keras.applications.nasnet import NASNetLarge return NASNetLarge(include_top=True,weights='imagenet',classes=1000)
def get_nasnetlarge(classes=9, input_shape=(331, 331, 3), base_layer_trainable=False): from tensorflow.keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(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
zoom_range=0.1, channel_shift_range=0.0, fill_mode="nearest", cval=0.0, horizontal_flip=True, vertical_flip=False, rescale=1. / 255, ) datagen = data.flow_from_directory(train_data_dir, target_size=(331, 331), batch_size=101, shuffle=True, class_mode='categorical') base_model = NASNetLarge(weights='imagenet', input_shape=(331, 331, 3), include_top=False) #base_model = tf.keras.applications.EfficientNetB7(weights='imagenet',input_shape=(360,360,3), include_top=False) for layer in base_model.layers: layer.trainable = False x = base_model.output x = tf.keras.layers.GlobalAveragePooling2D()(x) x = tf.keras.layers.Dropout(0.4)(x) x = tf.keras.layers.Dense(1024, activation='relu')(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Dense(512, activation='relu')(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Dense(256, activation='relu')(x)
def get_model(architecture, iteracion, models_info, pipeline): print("=" * len(architecture)) print(architecture) print("=" * len(architecture)) if iteracion > 0 and not pipeline["restart_weights"]: print("USING MODELS FROM MEMORY") base_model = models_info[architecture]['model_memory'] print("OK - USING MODELS FROM MEMORY") if architecture == 'InceptionV3': from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input if iteracion == 0 or pipeline["restart_weights"]: base_model = InceptionV3(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}") if architecture == 'InceptionV4': from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input if iteracion == 0 or pipeline["restart_weights"]: base_model = InceptionResNetV2(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}") if architecture == 'ResNet50': from tensorflow.keras.applications.resnet import ResNet50, preprocess_input if iteracion == 0 or pipeline["restart_weights"]: 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 or pipeline["restart_weights"]: 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 or pipeline["restart_weights"]: base_model = ResNet152(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}") if architecture == 'DenseNet121': from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input if iteracion == 0 or pipeline["restart_weights"]: 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 or pipeline["restart_weights"]: 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 or pipeline["restart_weights"]: 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 or pipeline["restart_weights"]: 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 or pipeline["restart_weights"]: base_model = Xception(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) return base_model, preprocess_input
elif args.net == 'inception_v3': base_model = InceptionV3(input_shape=(dim, dim, 3), weights='imagenet', include_top=False) 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)
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
from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input #from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input from tensorflow.keras.preprocessing import image import numpy as np import json from sklearn.metrics.pairwise import cosine_similarity import os import time nas_model = NASNetLarge( input_shape=None, include_top=False, weights="imagenet", input_tensor=None, pooling='max', ) # inc_res_model = InceptionResNetV2( # input_shape=None, include_top=False, weights="imagenet", input_tensor=None, pooling='max') def extract_features(img): """Extract features from input image and return the same""" print(f'>>> Entered feature extractor...', end='') start = time.time() img = image.load_img(img, target_size=(331, 331)) # img = image.load_img(img, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = nas_model.predict(x) print(f'Feature extraction took: {time.time()-start:.2}. Exiting. ')
import tensorflow as tf # model = keras.applications.NASNetMobile( # input_shape=img_size + (3,), # include_top=True, # weights=None, #randomize de weights # input_tensor=None, # pooling=None, # classes=1, # ) model = NASNetLarge( input_shape=img_size + (3, ), include_top=True, weights=None, #randomize de weights input_tensor=None, pooling=None, classes=1, ) model.summary() #%% compilando import tensorflow as tf model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model_checkpoint_callback = [