def init_resnet101(self): originResnet = ResNet101(weights='imagenet') for idx in range(1, len(self.resnet101_conv3.layers)): if len(self.resnet101_conv3.get_layer(index=idx).get_weights()) > 0: if self.resnet101_conv3.get_layer(index=idx).get_weights()[0].shape == originResnet.get_layer(index=idx).get_weights()[0].shape: self.resnet101_conv3.get_layer(index=idx).set_weights( originResnet.get_layer(index=idx).get_weights()) else: nowOriginWeights, nowOriginBiases = originResnet.get_layer(index=idx).get_weights() nowResnetWeights, nowResnetBiases = self.resnet101_conv3.get_layer(index=idx).get_weights() self.resnet101_conv3.get_layer(index=idx).set_weights( [np.random.choice( np.reshape(nowOriginWeights, (-1,)), nowResnetWeights.shape), np.random.choice( nowOriginBiases, nowResnetBiases.shape) ]) for resnetIdx, originIdx in enumerate(range(len(self.resnet101_conv3.layers), len(self.resnet101_conv5.layers))): resnetIdx += 1 if len(self.resnet101_conv5.get_layer(index=resnetIdx).get_weights()) > 0: if self.resnet101_conv5.get_layer(index=resnetIdx).get_weights()[0].shape == originResnet.get_layer(index=originIdx).get_weights()[0].shape: self.resnet101_conv5.get_layer(index=resnetIdx).set_weights( originResnet.get_layer(index=originIdx).get_weights()) else: nowOriginWeights, nowOriginBiases = originResnet.get_layer(index=originIdx).get_weights() nowResnetWeights, nowResnetBiases = self.resnet101_conv5.get_layer(index=resnetIdx).get_weights() self.resnet101_conv5.get_layer(index=resnetIdx).set_weights( [np.random.choice( np.reshape(nowOriginWeights, (-1,)), nowResnetWeights.shape), np.random.choice( nowOriginBiases, nowResnetBiases.shape) ])
def backbone(x_in): if version_tensorflow == '1': from tensorflow.keras.applications import (MobileNetV2, ResNet50) if backbone_type == 'ResNet50': return ResNet50(input_shape=x_in.shape[1:], include_top=False, weights=weights)(x_in) elif backbone_type == 'MobileNetV2': return MobileNetV2(input_shape=x_in.shape[1:], include_top=False, weights=weights)(x_in) else: raise TypeError('backbone_type error!') elif version_tensorflow == '2': from tensorflow.keras.applications import (MobileNetV2, ResNet50, ResNet101) if backbone_type == 'ResNet50': return ResNet50(input_shape=x_in.shape[1:], include_top=False, weights=weights)(x_in) elif backbone_type == 'MobileNetV2': return MobileNetV2(input_shape=x_in.shape[1:], include_top=False, weights=weights)(x_in) elif backbone_type == 'ResNet101': return ResNet101(input_shape=x_in.shape[1:], include_top=False, weights=weights)(x_in) else: raise TypeError('backbone_type error!')
def resnet101_fpn(input_shape, channels=1, activation="softmax"): img_input = Input(input_shape) resnet_base = ResNet101(img_input, include_top=True) resnet_base.load_weights(download_resnet_imagenet("resnet101")) conv1 = resnet_base.get_layer("conv1_relu").output conv2 = resnet_base.get_layer("res2c_relu").output conv3 = resnet_base.get_layer("res3b3_relu").output conv4 = resnet_base.get_layer("res4b22_relu").output conv5 = resnet_base.get_layer("res5c_relu").output P1, P2, P3, P4, P5 = create_pyramid_features(conv1, conv2, conv3, conv4, conv5) x = concatenate([ prediction_fpn_block(P5, "P5", (8, 8)), prediction_fpn_block(P4, "P4", (4, 4)), prediction_fpn_block(P3, "P3", (2, 2)), prediction_fpn_block(P2, "P2"), ]) x = conv_bn_relu(x, 256, 3, (1, 1), name="aggregation") x = decoder_block_no_bn(x, 128, conv1, 'up4') x = UpSampling2D()(x) x = conv_relu(x, 64, 3, (1, 1), name="up5_conv1") x = conv_relu(x, 64, 3, (1, 1), name="up5_conv2") if activation == 'softmax': name = 'mask_softmax' x = Conv2D(channels, (1, 1), activation=activation, name=name)(x) else: x = Conv2D(channels, (1, 1), activation=activation, name="mask")(x) model = Model(img_input, x) return model
def build_classifier(self): if cfg.ARCHI.CLASSIFIER == 'resnet101': from tensorflow.keras.applications import ResNet101 resnet = ResNet101(include_top=False, weights='imagenet', input_shape=self.input_shape) elif cfg.ARCHI.CLASSIFIER == 'resnet50': resnet = ResNet50(include_top=False, weights='imagenet', input_shape=self.input_shape) self.cls_model = Model(inputs=resnet.inputs, outputs=resnet.output, name='cls_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 DeepLabV3Plus(img_height, img_width, nclasses=21): print('*** Building DeepLabv3Plus Network ***') base_model = ResNet101(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False) image_features = base_model.get_layer( 'conv4_block23_out').output # size = 32*32*1024 x_a = ASPP(image_features) x_a = Upsample(tensor=x_a, size=[img_height // 4, img_width // 4]) x_b = base_model.get_layer('conv2_block3_out').output # size = 128*128*256 x_b = Conv2D(filters=48, kernel_size=1, padding='same', kernel_initializer='he_normal', name='low_level_projection', use_bias=False)(x_b) x_b = BatchNormalization(name=f'bn_low_level_projection')(x_b) x_b = Activation('relu', name='low_level_activation')(x_b) x = concatenate([x_a, x_b], name='decoder_concat') x = Conv2D(filters=256, kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal', name='decoder_conv2d_1', use_bias=False)(x) x = BatchNormalization(name=f'bn_decoder_1')(x) x = Activation('relu', name='activation_decoder_1')(x) x = Conv2D(filters=256, kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal', name='decoder_conv2d_2', use_bias=False)(x) x = BatchNormalization(name=f'bn_decoder_2')(x) x = Activation('relu', name='activation_decoder_2')(x) x = Upsample(x, [img_height, img_width]) x = Conv2D(nclasses, (1, 1), name='output_layer')(x) # x = tf.math.argmax(x, axis=2) ''' x = Activation('softmax')(x) tf.losses.SparseCategoricalCrossentropy(from_logits=True) Args: from_logits: Whether `y_pred` is expected to be a logits tensor. By default, we assume that `y_pred` encodes a probability distribution. ''' model = Model(inputs=base_model.input, outputs=x, name='DeepLabV3_Plus') print(f'*** Output_Shape => {model.output_shape} ***') return model
def get_resnet101(img_shape=(80,80,3)): ''' Arguments: input image shape default = (80,80) ''' image_input = keras.Input(shape=img_shape) #Define the input layer in our embedding_extraction_model temp_model = ResNet101.ResNet101(weights='imagenet',include_top=False)(image_input) #Set resnet50 to temp_model image_embedding_output_layer = keras.layers.GlobalAveragePooling2D()(temp_model) img_embedding_extractor_model = Model(inputs=image_input, outputs=image_embedding_output_layer) #Create a new model and add GlobalAveragePooling2D layer to it. return img_embedding_extractor_model
def get_base_model(self): try: base_model = None if self.MODEL_NAME == 'resnet101': base_model = ResNet101(weigths='imagenet', include_top=False, input_shape=(self.WIDTH, self.HEIGHT, 3)) return (True, base_model) except: return (False, 'Error generating base model')
def create_ResNet101_model(input_shape, num_classes): model = tf.keras.Sequential([ ResNet101(include_top=False, weights='imagenet', input_shape=input_shape), GlobalAveragePooling2D(), Dense(num_classes), Activation("softmax") ]) model.summary() return model
def transfer_resnet101(): resnet101 = ResNet101(include_top=False,weights='imagenet',input_shape=(160,160,3)) resnet101_preprocess = tf.keras.applications.resnet50.preprocess_input inputs = tf.keras.Input(shape=(160,160,3)) x = resnet101_preprocess(inputs) x = resnet101(inputs,training=False) x = tf.keras.layers.GlobalAveragePooling2D()(x) outputs = tf.keras.layers.Dense(units=1,activation='sigmoid')(x) custom_resnet101 = tf.keras.Model(inputs,outputs) custom_resnet101.summary() return custom_resnet101
def create_model(): model_pre = ResNet101(input_shape=(224, 224, 3), include_top=False, weights='imagenet', pooling='max') model_tomato = model_pre.output model_tomato = Dense(512, activation='relu')(model_tomato) model_tomato = Dropout(0.5)(model_tomato) model_tomato = Dense(512, activation='relu')(model_tomato) model_tomato = Dropout(0.5)(model_tomato) output = Dense(5, activation='softmax')(model_tomato) model_final = Model(inputs=model_pre.input, outputs=output) return model_final, model_pre
def get_encoder_model(name, in_shape, pooling): if name == "InceptionV3": model = InceptionV3(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet50": model = ResNet50(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet50V2": model = ResNet50V2(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet101": model = ResNet101(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet101V2": model = ResNet101V2(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet152": model = ResNet152(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "InceptionResNetV2": model = InceptionResNetV2(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "DenseNet121": model = DenseNet121(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) else: raise ValueError("model " + name + " not found") return model
def get_model(weights, length, shape): if (weights == None): print('Using Random weights') else: print('Using ' + str(weights) + ' weights') assert length in [50, 101, 152] if (length == 50): from tensorflow.keras.applications import ResNet50 model = ResNet50(include_top=False, weights=weights, input_shape=shape) elif (length == 101): from tensorflow.keras.applications import ResNet101 model = ResNet101(include_top=False, weights=weights, input_shape=shape) elif (length == 152): from tensorflow.keras.applications import ResNet152 model = ResNet152(include_top=False, weights=weights, input_shape=shape) return 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]
def get_model(model_name): if model_name == "vgg16": return vgg16.VGG16(weights='imagenet' ), vgg16.decode_predictions, vgg16.preprocess_input elif model_name == "vgg19": return vgg19.VGG19(weights='imagenet' ), vgg19.decode_predictions, vgg19.preprocess_input elif model_name == "resnet50": return resnet50.ResNet50( weights='imagenet' ), resnet50.decode_predictions, resnet50.preprocess_input elif model_name == "resnet101": return ResNet101(weights='imagenet' ), resnet.decode_predictions, resnet.preprocess_input elif model_name == "mobilenet": return mobilenet.MobileNet( weights='imagenet' ), mobilenet.decode_predictions, mobilenet.preprocess_input elif model_name == "densenet": return densenet.DenseNet121( weights='imagenet' ), densenet.decode_predictions, densenet.preprocess_input
def res_base_network(in_dims, out_dims, res_ver): model = Sequential() if res_ver == "resnext101": model.add(ResNeXt101( include_top=False, weights='imagenet', input_shape=in_dims, pooling='avg', classes=1000, backend = tf.keras.backend, layers = tf.keras.layers, models = tf.keras.models, utils = tf.keras.utils)) elif res_ver == "resnet50": model.add(ResNet50( include_top=False, weights='imagenet', input_shape=in_dims, pooling='avg', classes=1000)) elif res_ver =="resnet101": model.add(ResNet101( include_top=False, weights='imagenet', input_shape=in_dims, pooling='avg', classes=1000)) # model.add(Dense(1024, activation = 'relu')) model.add(Dense(1024)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dense(out_dims, activation = 'sigmoid')) return model
def model(weight_path): model = ResNet101(include_top=False, weights="imagenet", input_tensor=None, input_shape=(256, 256, 3), pooling=None, classes=40) final_model = Sequential() final_model.add(model) final_model.add(Flatten()) final_model.add(Dense(units=1024, activation="relu")) final_model.add(Dense(units=512, activation="relu")) final_model.add(Dense(units=40, activation="softmax")) opt = SGD(lr=1e-4, momentum=0.9) final_model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) final_model.load_weights(weight_path) # final_model = load_model(weight_path) return final_model
from tensorflow.keras.models import Sequential from tensorflow.keras.applications.vgg16 import preprocess_input from keras.utils.np_utils import to_categorical from tensorflow.keras.applications import VGG16, VGG19, Xception from tensorflow.keras.applications import ResNet101, ResNet101V2, ResNet152, ResNet152V2 from tensorflow.keras.applications import ResNet50, ResNet50V2 from tensorflow.keras.applications import InceptionV3, InceptionResNetV2 from tensorflow.keras.applications import MobileNet, MobileNetV2 from tensorflow.keras.applications import DenseNet121, DenseNet169, DenseNet201 from tensorflow.keras.applications import NASNetLarge, NASNetMobile from tensorflow.keras.applications import EfficientNetB0, EfficientNetB1 (x_train, y_train), (x_test, y_test) = cifar10.load_data() resnet101 = ResNet101(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) # print(model.weights) # ============== 전처리 =================== x_train = preprocess_input(x_train) x_test = preprocess_input(x_test) x_train = x_train.astype('float32') / 255. # 전처리 x_test = x_test.astype('float32') / 255. # 전처리 #다중분류 y원핫코딩 y_train = to_categorical(y_train) #(50000, 10) y_test = to_categorical(y_test) #(10000, 10) # ============== 모델링 ===================== resnet101.trainable = False # 훈련을 안시키겠다, 저장된 가중치 사용
vgg16 = VGG16() # vgg16.summary() print("VGG16",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------') vgg16 = VGG19() # vgg16.summary() print("VGG19레이어 수 ",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------') vgg16 = Xception() # vgg16.summary() print("Xception",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------') vgg16 = ResNet101() # vgg16.summary() print("ResNet101",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------') vgg16 = ResNet101V2() # vgg16.summary() print("ResNet101V2",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------') vgg16 = ResNet152() # vgg16.summary() print("ResNet152",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------') vgg16 = ResNet50() # vgg16.summary() print("ResNet50",len(vgg16.trainable_weights)/2) print('----------------------------------------------------------------------------')
#실습 # cifar10으로 vgg16만들기 #결과치 비교 import numpy as np from tensorflow.keras.datasets import cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() print(x_train.shape) # (50000,32,32,3) print(x_test.shape) # (50000,1) from tensorflow.keras.applications import ResNet101 from tensorflow.keras.layers import Dense, Flatten from tensorflow.keras.models import Sequential resNet101 = ResNet101( weights='imagenet', include_top=False, input_shape=(32, 32, 3)) #원하는 사이즈는 include_top=False / 디폴트 224*224 # print(model.weights) resNet101.trainable = False # vgg16.summary() # print(len(vgg16.weights)) # 26 # print(len(vgg16.trainable_weights)) # 0 model = Sequential() model.add(resNet101) model.add(Flatten()) model.add(Dense(10)) model.add(Dense(5)) model.add(Dense(1)) #, activation='softmax')) model.summary()
from tensorflow.keras.datasets import cifar10 from tensorflow.keras.utils import to_categorical #1. 데이터 (x_train, y_train), (x_test, y_test) = cifar10.load_data() y_train = to_categorical(y_train) y_test = to_categorical(y_test) x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 3).astype('float32') / 255. x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 3).astype('float32') / 255. #2. 모델 t = ResNet101(weights='imagenet', include_top=False, input_shape=(x_train.shape[1], x_train.shape[2], 3)) t.trainable = False #학습시키지 않겠다 이미지넷 가져다가 그대로 쓰겠다 # model.trainable=True model = Sequential() model.add(t) model.add(Flatten()) model.add(Dense(256)) model.add(BatchNormalization()) model.add(Dropout(0.2)) model.add(Activation('relu')) model.add(Dense(256)) model.add(Dense(10, activation='softmax')) model.compile(loss='categorical_crossentropy',
from tensorflow.keras.utils import to_categorical from tensorflow.keras.applications import ResNet101 from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Flatten from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau (x_train, y_train), (x_test, y_test) = cifar10.load_data() y_train = to_categorical(y_train) y_test = to_categorical(y_test) print(x_train.shape, x_test.shape) print(y_train.shape, y_test.shape) resnet101 = ResNet101(weights='imagenet', include_top=False, input_shape=x_train.shape[1:]) resnet101.summary() resnet101.trainable = False model = Sequential() model.add(resnet101) model.add(Flatten()) model.add(Dense(256)) model.add(Dense(64)) model.add(Dense(10, activation='softmax')) model.summary()
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])
y, train_size=0.8, shuffle=True, random_state=42) aaa = 1 x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], aaa) x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], aaa) print(x_train.shape, y_train.shape) # (3628, 128, 862, 1) (3628,) print(x_test.shape, y_test.shape) # (908, 128, 862, 1) (908,) model = ResNet101( include_top=True, input_shape=(128, 862, 1), classes=2, pooling=None, weights=None, ) model.summary() # model.trainable = False model.save('C:\\nmb\\nmb_data\\h5\\pre_train\\inception_adam.h5') # 컴파일, 훈련 op = Adam(lr=1e-5) batch_size = 32 es = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True, verbose=1) lr = ReduceLROnPlateau(monitor='val_loss', vactor=0.5, patience=10, verbose=1)
def build_model(encoder='efficientnetb7', center='dac', full_skip=True, attention='sc', upscore='upall'): MODEL_NAME = encoder if center is not None: MODEL_NAME = MODEL_NAME+'_'+center if attention is not None: MODEL_NAME = MODEL_NAME+'_'+attention if full_skip: MODEL_NAME = MODEL_NAME + '_fullskip' if upscore is not None: MODEL_NAME = MODEL_NAME + '_'+upscore if encoder == 'resnet50': encoder = ResNet50(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'conv1_relu', 'conv2_block3_out', 'conv3_block4_out', 'conv4_block6_out'] encoder_output = encoder.get_layer('conv5_block3_out').output # data 320x320x3 # conv1_relu 160x160x64 # conv2_block3_out 80x80x256 # conv3_block4_out 40x40x512 # conv4_block6_out 20x20x1024 # conv5_block3_out 10x10x2048 --> encoder output elif encoder == 'resnet101': encoder = ResNet101(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'conv1_relu', 'conv2_block3_out', 'conv3_block4_out'] encoder_output = encoder.get_layer('conv4_block23_out').output #data 320x320x3 #conv1_relu 160x160x64 #conv2_block3_out 80x80x256 #conv3_block4_out 40x40x512 #conv4_block23_out 20x20x1024 --> encoder output #conv5_block3_out 10x10x2048 elif encoder == 'resnet50v2': encoder = ResNet50V2(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'conv1_conv', 'conv2_block3_1_relu', 'conv3_block4_1_relu', 'conv4_block6_1_relu'] encoder_output = encoder.get_layer('post_relu').output # data 320x320x3 # conv1_conv 160x160x64 # conv2_block3_1_relu 80x80x64 # conv3_block4_1_relu 40x40x128 # conv4_block6_1_relu 20x20x256 # post_relu 10x10x2048 --> encoder output elif encoder == 'resnet101v2': encoder = ResNet101V2(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'conv1_conv', 'conv2_block3_1_relu', 'conv3_block4_1_relu', 'conv4_block23_1_relu'] encoder_output = encoder.get_layer('post_relu').output #data 320x320x3 #conv1_conv 160x160x64 #conv2_block3_1_relu 80x80x64 #conv3_block4_1_relu 40x40x128 #conv4_block23_1_relu 20x20x256 #post_relu 10x10x2048 --> encoder output elif encoder == 'vgg19': encoder = VGG19(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), weights='imagenet', include_top=False) skip_names = ['block1_conv2', 'block2_conv2', 'block3_conv4', 'block4_conv4', 'block5_conv4'] encoder_output = encoder.get_layer('block5_pool').output # block1_conv2 320x320x64 # block2_conv2 160x160x128 # block3_conv4 80x80x256 # block4_conv4 40x40x512 # block5_conv4 20x20x512 # block5_pool 10x10x512 --> encoder output elif encoder == 'efficientnetb6': encoder = EfficientNetB6(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'block2a_expand_activation', 'block3a_expand_activation', 'block4a_expand_activation'] encoder_output = encoder.get_layer('block6a_expand_activation').output #data 320x320x3 #block2a_expand_activation 160x160x192 #block3a_expand_activation 80x80x240 #block4a_expand_activation 40x40x432 #block6a_expand_activation 20x20x1200 --> encoder output #top_activation 10x10x2304 elif encoder == 'efficientnetb7': encoder = EfficientNetB7(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'block2a_expand_activation', 'block3a_expand_activation', 'block4a_expand_activation'] encoder_output = encoder.get_layer('block6a_expand_activation').output #data 320x320x3 #block2a_expand_activation 160x160x192 #block3a_expand_activation 80x80x288 #block4a_expand_activation 40x40x480 #block6a_expand_activation 20x20x1344 --> encoder output #top_activation 10x10x elif encoder == 'mobilenetv2': encoder = MobileNetV2(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False) skip_names = ['data', 'block_1_expand_relu', 'block_3_expand_relu', 'block_6_expand_relu', 'block_13_expand_relu'] encoder_output = encoder.get_layer('out_relu').output # data 320x320x3 # block_1_expand_relu 160x160x96 # block_3_expand_relu 80x80x144 # block_6_expand_relu 40x40x192 # block_13_expand_relu 20x20x576 # out_relu 10x10x1248 --> encoder output skip_layers = [encoder.get_layer(i).output for i in skip_names] # Center -------------- if center == 'atrous': x = atrous_block(encoder_output) elif center == 'dac': x = dense_atrous_block(encoder_output) elif center == 'aspp': x = aspp_block(encoder_output) elif center is None: x = encoder_output # Decoder -------------- if attention == 'se': attn_block = se_block elif attention == 'cbam': attn_block = cbam_block elif attention == 'sc': attn_block = scSE_block filters = [i.shape[-1] for i in skip_layers] filters[0] = 64 scales = [2 ** i for i in range(1, len(filters))][::-1] X = [] for i in range(1, len(filters) + 1): X.append(x) down = [] if full_skip: for j in range(len(scales) - (i - 1)): d = down_skip(skip_layers[j], scales[j + (i - 1)], filters[-1]//4) if attention is not None: d = attn_block(d) down.append(d) direct = direct_skip(skip_layers[-i], filters[-1]//4) if attention is not None: direct = attn_block(direct) x = convtranspose_block(x, filters[-1]//4) if attention is not None: x = attn_block(x) x = Concatenate()([x] + [direct] + down) x = conv3_block(x, x.shape[-1]) if upscore is not None: if upscore=='upall': up_scales=[2 ** i for i in range(1, len(filters)+1)][::-1] UP = [upscore_block(x, 32, up_scales[i]) for i, x in enumerate(X)] if attention is not None: UP = [attn_block(x) for x in UP] up = Concatenate()(UP) elif upscore=='upcenter': up = upscore_block(X[0], 64, 2 ** len(filters)) if attention is not None: up = attn_block(up) x = Concatenate()([x, up]) x = Conv2D(1, 1, padding='same')(x) x = Activation('sigmoid')(x) model = Model(encoder.input, x) metrics = [dice_coef, Recall(), Precision()] opt = Nadam(LR) model.compile(loss=bce_dice_loss, optimizer=opt, metrics=metrics) return model, MODEL_NAME
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
train_generator = ImageDataGenerator(rescale=1. / 255, width_shift_range=0.1, height_shift_range=0.1).flow( x_train, y_train, batch_size=32) val_generator = ImageDataGenerator(rescale=1. / 255, width_shift_range=0.1, height_shift_range=0.1).flow(x_val, y_val, batch_size=32) test_generator = ImageDataGenerator(rescale=1. / 255).flow(x_test, shuffle=False) resnet = ResNet101(weights='imagenet', include_top=False, input_shape=(128, 128, 3)) # resnet.summary() resnet.trainable = False input_tensor = Input(shape=(128, 128, 3)) layer = resnet(input_tensor) layer = GlobalAveragePooling2D()(layer) layer = Flatten()(layer) layer = Dense(2048, activation='relu')(layer) layer = Dense(1024, activation='relu')(layer) output_tensor = Dense(1000, activation='softmax')(layer) model = Model(inputs=input_tensor, outputs=output_tensor)
parser.add_argument("--norm-hist", action="store_true", dest="norm_hist") parser.add_argument("--grad-norm", action="store_true", dest="grad_norm") parser.add_argument("--image-size", action="store", default="224") args = parser.parse_args() train_data = ImageDataSequence([os.path.join(args.dataset, f) for f in (["train", "val"] if args.test else ["train"])], batch_size=int(args.batch_size), target_size=(224, 224, 3), frac=float(args.train_frac)) val_data = ImageDataSequence(os.path.join(args.dataset, "test" if args.test else "val"), batch_size=int(args.batch_size), target_size=(224, 224, 3)) img_size = int(args.image_size) input_tensor = Input(shape=(img_size, img_size, 3)) model_kwargs = {"backend": tensorflow.keras.backend, "layers": tensorflow.keras.layers, "models": tensorflow.keras.models, "utils": tensorflow.keras.utils} if args.network == "resnet101": zero = ResNet101(include_top=False, weights=None if args.random_init else "imagenet", input_tensor=input_tensor, **model_kwargs) model = ResNet101(include_top=False, weights=None if args.random_init else "imagenet", input_tensor=input_tensor, **model_kwargs) elif args.network == "enb0": zero = en.EfficientNetB0(include_top=False, weights=None if args.random_init else "imagenet", input_tensor=input_tensor) model = en.EfficientNetB0(include_top=False, weights=None if args.random_init else "imagenet", input_tensor=input_tensor) elif args.network.startswith("file:"): zero = load_model(args.network[5:]) model = load_model(args.network[5:]) else: raise Exception("Unknown network: " + args.network) num_frozen = int(float(args.freeze_frac) * len(model.layers)) for l in model.layers[:num_frozen]: if type(l) != keras.layers.normalization.BatchNormalization: l.trainable = False
from tensorflow.keras.callbacks import EarlyStopping, TensorBoard, ReduceLROnPlateau #전처리 (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train.astype('float32') / 255. ## 10000,32,32,3 x_test = x_test.astype('float32') / 255. y_train = to_categorical(y_train) y_test = to_categorical(y_test) #모델링 ##여기만 수정 name = 'ResNet101' t_model = ResNet101(include_top=False, weights='imagenet', input_shape=(32, 32, 3)) ## t_model.trainable = False model = Sequential() model.add(t_model) model.add(Flatten()) model.add(Dense(10, activation='softmax')) #컴파일 및 훈련 model.compile(loss='categorical_crossentropy', metrics=['acc'], optimizer='adam') ealystopping = EarlyStopping(monitor='loss', patience=10, mode='auto')
weights='imagenet')), ("InceptionResNetV2", InceptionResNetV2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("MobileNet", MobileNet(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')), ("MobileNetV2", MobileNetV2(input_shape=IMG_SHAPE, include_top=False, 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,