def resnet152v2(input_shape: Tuple[int, int, int], output_shape: Tuple[int, ...], weights: str = 'imagenet', include_top: bool = False) -> Model: base_model = ResNet152V2(weights=weights, include_top=include_top, input_tensor=Input(shape=input_shape)) if include_top: return base_model else: # Construct the head of the model that will be placed on top of the base model num_classes = output_shape[0] head_model = base_model.output head_model = AveragePooling2D(pool_size=(4, 4))(head_model) head_model = Flatten(name='flatten')(head_model) head_model = Dense(64, activation='relu')(head_model) head_model = Dropout(0.5)(head_model) if num_classes > 2: activation = 'softmax' else: activation = 'sigmoid' head_model = Dense(num_classes, activation=activation)(head_model) # Place the head Fully Connected model on top of the base model (actual model) model = Model(base_model.input, head_model) # Loop over all layers in the base model and freeze them so they won't be updated during the first training process for layer in base_model.layers: layer.trainable = False return model
def build_model(classes=2): inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) x = preprocess_input(inputs) x = ResNet152V2(weights=None, classes=classes)(x) model = Model(inputs=inputs, outputs=x) model.compile(loss='categorical_crossentropy', metrics=['accuracy']) return model
def createModel(): base_model = ResNet152V2(input_shape=(224, 224, 3), weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) model = Model(inputs=base_model.input, outputs=x) return model
def __init__(self, model_name=None): if model_name == 'Xception': base_model = Xception(weights='imagenet') self.preprocess_input = xception.preprocess_input elif model_name == 'VGG19': base_model = VGG19(weights='imagenet') self.preprocess_input = vgg19.preprocess_input elif model_name == 'ResNet50': base_model = ResNet50(weights='imagenet') self.preprocess_input = resnet.preprocess_input elif model_name == 'ResNet101': base_model = ResNet101(weights='imagenet') self.preprocess_input = resnet.preprocess_input elif model_name == 'ResNet152': base_model = ResNet152(weights='imagenet') self.preprocess_input = resnet.preprocess_input elif model_name == 'ResNet50V2': base_model = ResNet50V2(weights='imagenet') self.preprocess_input = resnet_v2.preprocess_input elif model_name == 'ResNet101V2': base_model = ResNet101V2(weights='imagenet') self.preprocess_input = resnet_v2.preprocess_input elif model_name == 'ResNet152V2': base_model = ResNet152V2(weights='imagenet') self.preprocess_input = resnet_v2.preprocess_input elif model_name == 'InceptionV3': base_model = InceptionV3(weights='imagenet') self.preprocess_input = inception_v3.preprocess_input elif model_name == 'InceptionResNetV2': base_model = InceptionResNetV2(weights='imagenet') self.preprocess_input = inception_resnet_v2.preprocess_input elif model_name == 'DenseNet121': base_model = DenseNet121(weights='imagenet') self.preprocess_input = densenet.preprocess_input elif model_name == 'DenseNet169': base_model = DenseNet169(weights='imagenet') self.preprocess_input = densenet.preprocess_input elif model_name == 'DenseNet201': base_model = DenseNet201(weights='imagenet') self.preprocess_input = densenet.preprocess_input elif model_name == 'NASNetLarge': base_model = NASNetLarge(weights='imagenet') self.preprocess_input = nasnet.preprocess_input elif model_name == 'NASNetMobile': base_model = NASNetMobile(weights='imagenet') self.preprocess_input = nasnet.preprocess_input elif model_name == 'MobileNet': base_model = MobileNet(weights='imagenet') self.preprocess_input = mobilenet.preprocess_input elif model_name == 'MobileNetV2': base_model = MobileNetV2(weights='imagenet') self.preprocess_input = mobilenet_v2.preprocess_input else: base_model = VGG16(weights='imagenet') self.preprocess_input = vgg16.preprocess_input self.model = Model(inputs=base_model.input, outputs=base_model.layers[-2].output)
def resnet152V2Model(): baseModel = ResNet152V2( weights=None, include_top=False, input_shape=(32, 32, 3), ) model_input = Input(shape=(32, 32, 3)) x = baseModel(model_input) x = GlobalAveragePooling2D()(x) model_output = Dense(10, activation="softmax")(x) model = Model(inputs=model_input, outputs=model_output) return model
def transfer_resnet152v2(): resnet152v2 = ResNet152V2(include_top=False,weights='imagenet',input_shape=(160,160,3)) resnet152v2_preprocess = tf.keras.applications.resnet50.preprocess_input inputs = tf.keras.Input(shape=(160,160,3)) x = resnet152v2_preprocess(inputs) x = resnet152v2(inputs,training=False) x = tf.keras.layers.GlobalAveragePooling2D()(x) outputs = tf.keras.layers.Dense(units=1,activation='sigmoid')(x) custom_resnet152v2 = tf.keras.Model(inputs,outputs) custom_resnet152v2.summary() return custom_resnet152v2
def __init__(self, model, info): self.info = info possible_model = { "Small": self.getSmallModel(), "MobileNetV2": MobileNetV2(), "Resnet50": ResNet50(weights=None, classes=self.info.features['label'].num_classes), "Resnet200": ResNet152V2(weights=None, include_top=False), "VGG": VGG16(weights=None, include_top=False, classes=self.info.features['label'].num_classes), "DenseNet": DenseNet121(), "Cifarnet": self.cifarnet(), "Inception": InceptionV3(), "CNN": self.getCNN() } assert model in possible_model.keys(), "Selected model not available" self.model = possible_model[model]
def __init__(self, weights_init, model_architecture='vgg16'): self.weights_init = weights_init if model_architecture == 'vgg16': self.model = VGG16(weights=self.weights_init, include_top=False) self.bridge_list = [2, 5, 9, 13, 17] elif model_architecture == 'vgg19': self.model = VGG19(weights=self.weights_init, include_top=False) self.bridge_list = [2, 5, 10, 15, 20] elif model_architecture == 'resnet50': self.model = ResNet50(weights=self.weights_init, include_top=False) self.bridge_list = [4, 38, 80, 142, -1] elif model_architecture == 'resnet50v2': self.model = ResNet50V2(weights=self.weights_init, include_top=False) self.bridge_list = [2, 27, 62, 108, -1] elif model_architecture == 'resnet101': self.model = ResNet101(weghts=self.weights_init, include_top=False) self.bridge_list = [4, 38, 80, 312, -1] elif model_architecture == 'resnet101v2': self.model = ResNet101V2(weights=self.weights_init, include_top=False) self.bridge_list = [2, 27, 62, 328, -1] elif model_architecture == 'resnet152': self.model = ResNet152(weights=self.weights_init, include_top=False) self.bridge_list = [4, 38, 120, 482, -1] elif model_architecture == 'resnet152v2': self.model = ResNet152V2(weights=self.weights_init, include_top=False) self.bridge_list = [2, 27, 117, 515, -1]
MODEL_ROOT = os.path.join(MEDIA_ROOT, 'ai_models') IMAGE_ROOT = os.path.join(MEDIA_ROOT, 'image') ''' - 인공지능과 관련된 공통된 변수 ''' # 한번만 로드하는 모델 목록 # 공통으로 사용되는 모델 from tensorflow.keras.applications import ResNet152V2, ResNet50 import time import logging start = time.time() RESNET152V2 = ResNet152V2( weights='imagenet', include_top=False, input_shape=(224, 224, 3) ) RESNET152V2.trainable=False RESNET50 = ResNet50( weights='imagenet', include_top=False, input_shape=(224, 224, 3) ) RESNET50.trainable=False print('* RESNET init model time :', time.time() - start) # 성공/실패를 구분하는 기준 # PREDICTION_RATE 보다 크면 성공, 작으면 실패
batch_size = 32 my_training_batch_generator = My_Custom_Generator(X_train_filenames, robot_state_train_input, robot_state_train_label, batch_size) my_testing_batch_generator = My_Custom_Generator(X_test_filenames, robot_state_test_input, robot_state_test_label, batch_size) ####################################################################################################################################### ##################################### Define CNN architecture ####################################################################### model = ResNet152V2(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) #model.summary() y1 = model.output y2 = GlobalAveragePooling2D()(y1) y3 = Dense(1024, activation='relu')(y2) y4 = Dense(1024, activation='relu')(y3) new_model = Model(inputs=model.input, outputs=y4) for layer in new_model.layers[:561]: layer.trainable = False for layer in new_model.layers[561:]: layer.trainable = True cnn_out = new_model.output
weights='imagenet')), ("ResNet101", ResNet101(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("ResNet101V2", ResNet101V2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("ResNet152", ResNet152(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("ResNet152V2", ResNet152V2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("ResNet50", ResNet50(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("ResNet50V2", ResNet50V2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("VGG16", VGG16(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("VGG19", VGG19(input_shape=IMG_SHAPE,
FAST_RUN = False IMAGE_WIDTH=128 IMAGE_HEIGHT=128 IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT) IMAGE_CHANNELS=3 input_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS) # определим batch_size batch_size=15 """##Загружаем предварительно обученную нейронную сеть ResNet152V2 Создадим экземпляр модели ResNet152V2 """ ResNet152V2_net = ResNet152V2(weights='imagenet', include_top=False, input_shape=input_shape) ResNet152V2_net.trainable = False """## Классификатор""" model = Sequential() # добавляем в модель сеть ResNet152V2 в качестве слоя model.add(ResNet152V2_net) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(9, activation='softmax'))
from tensorflow.keras.layers import Dense, Flatten from tensorflow.keras.models import Model from tensorflow.keras.applications import ResNet152V2 from tensorflow.keras.applications.resnet50 import preprocess_input from glob import glob # re-size all the images to this IMAGE_SIZE = [224, 224] train_path = 'dataset/train' valid_path = 'dataset/test' # use imagenet weights i,e download weight file from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet152v2_weights_tf_dim_ordering_tf_kernels_notop.h5 resnet = ResNet152V2(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False) # not to train existing weights for layer in resnet.layers: layer.trainable = False # get no of output classes total_no_of_classes = glob('dataset/train/*') # flatten all the layers x = Flatten()(resnet.output) output_layer = Dense(len(total_no_of_classes), activation='softmax')(x) # create object for the model
class_mode='categorical', shuffle=True) validation_generator = train_datagen.flow_from_directory( val_dir, target_size=(224, 224), batch_size=128, class_mode='categorical', shuffle=False) import tensorflow as tf from tensorflow.keras.layers import Input from tensorflow.keras.applications import ResNet50 from tensorflow.keras.applications import ResNet152V2 model = tf.keras.models.Sequential([ ResNet152V2(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))), tf.keras.layers.Flatten(), tf.keras.layers.Dense(4096, activation='relu'), tf.keras.layers.Dropout(0.4), tf.keras.layers.BatchNormalization(), tf.keras.layers.Dense(4096, activation='relu'), tf.keras.layers.Dropout(0.4), tf.keras.layers.BatchNormalization(), tf.keras.layers.Dense(1000, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.BatchNormalization(), tf.keras.layers.Dense(38, activation='softmax') ]) model.layers[0].trainable = False model.summary()
plt.plot(epochs_range, val_acc, label='Validation Accuracy') plt.legend(loc='lower right') plt.title('Training and Validation Accuracy') plt.subplot(1, 2, 2) plt.plot(epochs_range, loss, label='Training Loss') plt.plot(epochs_range, val_loss, label='Validation Loss') plt.legend(loc='upper right') plt.title('Training and Validation Loss') plt.show() """## ResNet152V2 """ from tensorflow.keras.applications import ResNet152V2 net= ResNet152V2(include_top=False, weights='imagenet', input_tensor=Input(shape=(150,150,3))) for layer in net.layers[:-5]: layer.trainable = False x = net.output x = Flatten()(x) x = Dropout(0.5)(x) output_layer = Dense(1, activation='sigmoid', name='sigmoid')(x) model = Model(inputs=net.input, outputs=output_layer) # initiate RMSprop optimizer opt = keras.optimizers.RMSprop(lr=0.0001, decay=1e-6) # Train the model using RMSprop model.compile(loss='binary_crossentropy',
def construct_model(pretrainedNN): model = Sequential() if (pretrainedNN == 'VGG16'): model.add( VGG16(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'VGG19'): model.add( VGG19(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'ResNet101'): model.add( ResNet101(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'ResNet152'): model.add( ResNet152(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'ResNet50V2'): model.add( ResNet50V2(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'ResNet101V2'): model.add( ResNet101V2(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'ResNet152V2'): model.add( ResNet152V2(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'MobileNet'): model.add( MobileNet(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'MobileNetV2'): model.add( MobileNetV2(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'DenseNet121'): model.add( DenseNet121(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'DenseNet169'): model.add( DenseNet169(weights=None, include_top=False, input_shape=(32, 32, 3))) elif (pretrainedNN == 'DenseNet201'): model.add( DenseNet201(weights=None, include_top=False, input_shape=(32, 32, 3))) else: model.add( ResNet50(weights=None, include_top=False, input_shape=(32, 32, 3))) model.add(Flatten()) model.add(Dense(77, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) return model
def CNN_model(self, learning_rate, epoch, batchsize, whether_Adam, Momentum_gamma, weight_decay, whether_load, cnn_type): """ Resnet model :param learning_rate :param epoch :param batchsize :param whether_Adam: whether to perform Adam optimiser, if not perform Momentum :param Momentum gamma: a variable of Momentum :param weight_decay: weight decay for Momentum :param whether_load: whether to load trained Resnet model in if it exists (or cover it) """ test_cnn_mfcc = self.train_mfcc test_cnn_label = self.train_label if(isfile("model/resnet_label.hdf5") and whether_load): self.cnn_model = load_model("model/resnet_label.hdf5") else: train_cnn_mfcc = self.test_mfcc train_cnn_label = self.test_label val_cnn_mfcc = self.validate_mfcc val_cnn_label = self.validate_label # input input = Input(shape=(self.test_mfcc.shape[1], self.test_mfcc.shape[2], 1)) # Concatenate -1 dimension to be three channels, to fit the input need in ResNet50 input_concate = Concatenate()([input,input,input]) # CNN series network (VGG+Resnet) # reference: https://keras.io/api/applications/ if(cnn_type == 'ResNet50'): from tensorflow.keras.applications import ResNet50 cnn_output = ResNet50(pooling = 'avg')(input_concate) elif(cnn_type == 'ResNet101'): from tensorflow.keras.applications import ResNet101 cnn_output = ResNet101(pooling = 'avg')(input_concate) elif(cnn_type == 'ResNet152'): from tensorflow.keras.applications import ResNet152 cnn_output = ResNet152(pooling = 'avg')(input_concate) elif(cnn_type == 'ResNet50V2'): from tensorflow.keras.applications import ResNet50V2 cnn_output = ResNet50V2(pooling = 'avg')(input_concate) elif(cnn_type == 'ResNet101V2'): from tensorflow.keras.applications import ResNet101V2 cnn_output = ResNet101V2(pooling = 'avg')(input_concate) elif(cnn_type == 'ResNet152V2'): from tensorflow.keras.applications import ResNet152V2 cnn_output = ResNet152V2(pooling = 'avg')(input_concate) elif(cnn_type == 'VGG16'): # width and height should not smaller than 32 from tensorflow.keras.applications import VGG16 cnn_output = VGG16(include_top = False, pooling = 'avg')(input_concate) cnn_output = Flatten()(cnn_output) elif(cnn_type == 'VGG19'): # width and height should not smaller than 32 from tensorflow.keras.applications import VGG19 cnn_output = VGG19(include_top = False, pooling = 'avg')(input_concate) cnn_output = Flatten()(cnn_output) else: # CNN layers we design print("No recognised CNN network. The CNN layers we designed are performed") # convolution layers conv_output1 = Conv2D(filters=32, strides=(1, 1), kernel_size=5, activation='relu')(input) # pool_output1 = MaxPool2D(pool_size=(2, 2))(conv_output1) conv_output2 = Conv2D(filters=8, strides=(2, 2), kernel_size=4, activation='relu')(conv_output1) conv_output2 = Dropout(0.2)(conv_output2) conv_output2_batch = BatchNormalization()(conv_output2) cnn_output = Flatten()(conv_output2_batch) cnn_output = Flatten()(cnn_output) # dense with sigmoid Dense_sigmoid = Dense(24, activation='sigmoid')(cnn_output) Dense_sigmoid = Dropout(0.2)(Dense_sigmoid) # dense output output = Dense(self.test_label.shape[1], activation='softmax')(Dense_sigmoid) # cnn model for labels recognision self.cnn_model = Model(input, output) # optimizer if whether_Adam: optimizer = optimizers.Adam(lr=learning_rate, beta_1 = Momentum_gamma, decay=weight_decay) else: optimizer = optimizers.SGD(lr=learning_rate, momentum=Momentum_gamma, nesterov=True, decay=weight_decay) self.cnn_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['mse', 'accuracy']) start = time.time() self.history = self.cnn_model.fit(train_cnn_mfcc, train_cnn_label, epochs=epoch, batch_size=batchsize, validation_data=[val_cnn_mfcc,val_cnn_label]) self.training_time = time.time() - start self.cnn_model.save("model/resnet_label.hdf5") # model evaluation self.cnn_model.predict(test_cnn_mfcc) self.score = self.cnn_model.evaluate(test_cnn_mfcc, test_cnn_label) print("test loss: ", self.score[0], ", mse: ", self.score[1], ", accuracy", self.score[2])
def __init__(self): super(Encoder, self).__init__() self.resnet = TimeDistributed( ResNet152V2(include_top=False, weights='imagenet'))
def loadModel(mode, modelWeights, organ, modelType): """ Load model and compile it Input training or inference mode, model weights and type of model Return model """ # Load model input configuration modelInputConfig = loadModelInputConf(organ) # Get values useChannels = modelInputConfig.useChannels useClasses = modelInputConfig.useClasses useResolution = modelInputConfig.useResolution # Define model if modelType == 'ResNet101': model = ResNet101(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses) elif modelType == 'SEResNet101': mySEResNet = AllSEResNets.SEResNet101 model = mySEResNet(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses) elif modelType == 'SEResNet154': mySEResNet = AllSEResNets.SEResNet154 model = mySEResNet(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses) # elif modelType == 'SEInceptionResNetV2': # mySEInceptionResNet = AllSEInceptionResNets.SEInceptionResNetV2 # model = mySEInceptionResNet(include_top=True, weights=modelWeights, input_shape=( # useResolution[0], useResolution[1], useChannels), classes=useClasses) elif modelType == 'EfficientNetB4': model = EfficientNetB4(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses, classifier_activation="softmax") elif modelType == 'Xception': model = Xception(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses) elif modelType == 'ResNet101V2': model = ResNet101V2(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses, classifier_activation="softmax") elif modelType == 'ResNet152V2': model = ResNet152V2(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses, classifier_activation="softmax") elif modelType == 'InceptionResNetV2': model = InceptionResNetV2(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses, classifier_activation="softmax") elif modelType == 'ResNet50V2': model = ResNet50V2(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses, classifier_activation="softmax") elif modelType == 'NASNetLarge': model = NASNetLarge(include_top=True, weights=modelWeights, input_shape=(useResolution[0], useResolution[1], useChannels), classes=useClasses) else: raise ValueError('The selected model could not be found') if mode == 'training': print('Loaded model ' + modelType + ' for training, no weights loaded') # Add reglizarization if needed # model = addRegularization(model, tf.keras.regularizers.l2(0.0000)) if mode == 'inference': print('Loaded model ' + modelType + ' for inference, weights loaded.') # Do not add regularization model.compile( optimizer='adam', loss='categorical_crossentropy', # metrics=['accuracy'] metrics=[ 'accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), tf.keras.metrics.AUC() ], weighted_metrics=[ 'accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), tf.keras.metrics.AUC() ]) return model