def __init__(self, K=16, n=128, phenotypes=10): self.n = n self.K = K self.X_train = None self.y_train = None self.X_test = None self.y_test = None self.lda = None self.boc = None self.X = None self.y = None self.p = phenotypes xception_base_model = ResNet50(include_top=False, weights="imagenet", input_shape=(self.n, self.n, 3), pooling='avg') flatten = Flatten()(xception_base_model.output) classifier = Dense(self.K, activation="softmax")(flatten) # for layer in xception_base_model.layers: # layer.trainable = False self.model = Model(inputs=xception_base_model.input, outputs=classifier) self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) self.model.summary()
def get_resnet50_model(trainable=False, num_nodes=NUM_NODES, dropout_rate=DROPOUT_RATE, learning_rate=LEARNING_RATE, print_summary=True): """ Builds and compiles ResNet50 transfer learning model. """ transfer_model = ResNet50( input_shape=INPUT_SHAPE, include_top=False, # leave out the last fully connected layer weights='imagenet') for layer in transfer_model.layers: layer.trainable = trainable hidden_layers = [] hidden_layers.append(layers.Flatten()(transfer_model.output)) hidden_layers.append( layers.Dense(num_nodes, activation="relu")(hidden_layers[-1])) hidden_layers.append(layers.Dropout(dropout_rate)(hidden_layers[-1])) output_layer = layers.Dense(NUM_LABELS, activation="softmax")(hidden_layers[-1]) model2 = Model(transfer_model.input, output_layer) model2.compile(loss=losses.CategoricalCrossentropy(), optimizer=optimizers.Adam(learning_rate=learning_rate), metrics=METRICS) if print_summary: model2.summary() return model2
def preprocessing_manager(cfg): if cfg["dataset"]["encoding"] == "inception": img_model = InceptionV3(weights='imagenet') dataset_preprocessor = PreProcessing(cfg, "inception", (299, 299)) dataset_preprocessor.run_one_time_encoding(img_model) # Load train, validation sets from the pre-processor return dataset_preprocessor.get_encoding_keras_generators("inception") elif cfg["dataset"]["encoding"] == "resnet50": img_model = ResNet50(weights='imagenet') dataset_preprocessor = PreProcessing(cfg, "resnet50", (224, 224)) dataset_preprocessor.run_one_time_encoding(img_model) # Load train, validation sets from the pre-processor return dataset_preprocessor.get_encoding_keras_generators("resnet50") elif cfg["dataset"]["encoding"] == "simple": dataset_preprocessor = PreProcessing(cfg, None, (299, 299)) return dataset_preprocessor.get_image_keras_generators() else: RuntimeError("Unsupported encoding type!")
def buildResNet50Model(img_height, img_width, img_channl, num_classes, num_GPU): inputs = Input(shape=(img_height, img_width, img_channl)) # -------------------------------------------- StopNum = 0 AppModel = ResNet50(include_top=False, pooling='avg', weights='imagenet') for idx, layer in enumerate(AppModel.layers): layer.trainable = False if (idx == StopNum): break x = AppModel.layers[-1].output x = Flatten()(x) x = BatchNormalization()(x) x = Dropout(0.5)(x) output_layer = Dense(num_classes, activation='sigmoid', name='sigmoid')(x) model = Model(inputs=AppModel.input, outputs=output_layer) # -------------------------------------------- model.summary() if (num_GPU > 1): model = multi_gpu_model(model, gpus=num_GPU) model.compile( loss='binary_crossentropy', optimizer=Adam(lr=1e-4), #0.001 metrics=['categorical_accuracy', 'binary_accuracy', f1_m]) return model
def tiny_yolo4lite_resnet50_body(inputs, num_anchors, num_classes, use_spp=True): '''Create Tiny YOLO_v4 Lite ResNet50 model CNN body in keras.''' resnet50 = ResNet50(input_tensor=inputs, weights='imagenet', include_top=False) print('backbone layers number: {}'.format(len(resnet50.layers))) # input: 416 x 416 x 3 # conv5_block3_out: 13 x 13 x 2048 # conv4_block6_out: 26 x 26 x 1024 # conv3_block4_out: 52 x 52 x 512 # f1 :13 x 13 x 2048 f1 = resnet50.get_layer('conv5_block3_out').output # f2: 26 x 26 x 1024 f2 = resnet50.get_layer('conv4_block6_out').output f1_channel_num = 1024 f2_channel_num = 512 y1, y2 = tiny_yolo4lite_predictions( (f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes, use_spp) return Model(inputs, [y1, y2])
def load_model(self): base_model=ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(137,236,3), pooling=None, classes=1000) base_model.trainable=False x = base_model.output x = layers.GlobalAveragePooling2D()(x) x = layers.BatchNormalization()(x) x = layers.Dropout(0.3)(x) x = layers.Dense(256, activation='relu')(x) x = layers.BatchNormalization()(x) x = layers.Dropout(0.3)(x) grapheme_root = layers.Dense(168, activation = 'softmax', name = 'root')(x) vowel_diacritic = layers.Dense(11, activation = 'softmax', name = 'vowel')(x) consonant_diacritic = layers.Dense(7, activation = 'softmax', name = 'consonant')(x) model = Model(inputs=base_model.input,outputs = [grapheme_root, vowel_diacritic, consonant_diacritic]) # for layer in base_model.layers: # layer.trainable = False # model.compile(optimizer='adam', loss = {'root' : 'categorical_crossentropy', # 'vowel' : 'categorical_crossentropy', # 'consonant': 'categorical_crossentropy'}, # loss_weights = {'root' : 0.333, # 'vowel' : 0.333, # 'consonant': 0.333}, # metrics={'root' : 'accuracy', # 'vowel' : 'accuracy', # 'consonant': 'accuracy'}) # print(model.summary()) return model
def yolo4_resnet50_body(inputs, num_anchors, num_classes): """Create YOLO_V4 ResNet50 model CNN body in Keras.""" resnet50 = ResNet50(input_tensor=inputs, weights='imagenet', include_top=False) print('backbone layers number: {}'.format(len(resnet50.layers))) # input: 416 x 416 x 3 # conv5_block3_out: 13 x 13 x 2048 # conv4_block6_out: 26 x 26 x 1024 # conv3_block4_out: 52 x 52 x 512 # f1 :13 x 13 x 2048 f1 = resnet50.get_layer('conv5_block3_out').output # f2: 26 x 26 x 1024 f2 = resnet50.get_layer('conv4_block6_out').output # f3 : 52 x 52 x 512 f3 = resnet50.get_layer('conv3_block4_out').output f1_channel_num = 1024 f2_channel_num = 512 f3_channel_num = 256 y1, y2, y3 = yolo4_predictions( (f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num), num_anchors, num_classes) return Model(inputs=inputs, outputs=[y1, y2, y3])
def get_weights(save_dir: Path, model_name: str, dtype: str) -> str: """Download pre-trained imagenet weights for model. Args: save_dir: Path to where checkpoint must be downloaded. model_name: Type of image classification model, must be one of ("GoogleNet", "InceptionV1", "MobileNet", "MobileNetV2", "NASNetMobile", "DenseNet121", "ResNet50", "Xception", "InceptionV3") in all lower case. dtype: Data type of the network. Returns: Path to checkpoint file. """ if isinstance(save_dir, str): save_dir = Path(save_dir) g = tf.Graph() with tf.Session(graph=g) as sess: keras_backend.set_floatx(dtype) keras_backend.set_session(sess) if model_name == "mobilenet": MobileNet(weights='imagenet') saver = tf.train.Saver() elif model_name == "mobilenetv2": MobileNetV2(weights='imagenet') saver = tf.train.Saver() elif model_name == "nasnetmobile": NASNetMobile(weights='imagenet') saver = tf.train.Saver() elif model_name == "densenet121": DenseNet121(weights='imagenet') saver = tf.train.Saver() elif model_name == "resnet50": ResNet50(weights='imagenet') saver = tf.train.Saver() elif model_name == "xception": Xception(weights='imagenet') saver = tf.train.Saver() elif model_name == "inceptionv3": InceptionV3(weights='imagenet') saver = tf.train.Saver() elif model_name in ("googleNet", "inceptionv1"): tar_file = get_file( fname='inceptionv1_tar.gz', origin= 'http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz' ) tar_file_reader = tarfile.open(tar_file) tar_file_reader.extractall(save_dir) if dtype == 'float16': saver = convert_ckpt_to_fp16( Path(save_dir, 'inception_v1.ckpt').as_posix()) sess.run(tf.global_variables_initializer()) else: raise ValueError("""Requested model type = %s not one of ["GoogleNet", "InceptionV1", "MobileNet", "MobileNetV2", "NASNetMobile", "DenseNet121", "ResNet50", "Xception", "InceptionV3"].""" % model_name) save_dir.mkdir(parents=True, exist_ok=True) return saver.save(sess, Path(save_dir, f"{model_name}.ckpt").as_posix())
def init_model(self, model_name: str = 'VGG16'): if model_name == 'VGG16': self.input_shape = Input(shape=(224, 224, 3)) self.model = VGG16(weights='imagenet') elif model_name == 'ResNet50': self.input_shape = (224, 224, 3) self.model = ResNet50(weights='imagenet')
def get_resnet50(image_shape): from tensorflow.keras.applications.resnet import ResNet50 model = ResNet50(weights='imagenet', include_top=False, input_shape=(image_shape)) for layer in model.layers: layer.trainable = False model.summary() return model
def test_save_load_model(): model = ResNet50(weights=None) data = save_model_to_bytes(model) loaded = load_model_from_bytes(data) assert loaded.get_config() == model.get_config() w_orig = model.get_weights() w_loaded = loaded.get_weights() assert len(w_orig) == len(w_loaded) for (orig, loaded) in zip(w_orig, w_loaded): assert (orig == loaded).all()
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 build_resnet50(num_classes, img_size): from tensorflow.keras.applications.resnet import ResNet50 from tensorflow.keras import Model from tensorflow.keras.layers import Dense, Flatten resnet = ResNet50(weights='imagenet', include_top=False, input_shape=img_size) x = Flatten(input_shape=resnet.output.shape)(resnet.output) x = Dense(1024, activation='sigmoid')(x) predictions = Dense(num_classes, activation='softmax', name='pred')(x) model = Model(inputs=[resnet.input], outputs=[predictions]) return model
def __init__(self, n=128): resnet50 = ResNet50(include_top=False, weights="imagenet", input_shape=(n, n, 3), pooling='avg') self.n = n for layer in resnet50.layers: layer.trainable = False self.model = Model(inputs=resnet50.input, outputs=resnet50.output) self.model.summary()
def __init__(self, data_shape=(224, 224, 3), resnet_version=1, resnet_layer_number=50, num_classes=1000): super(ResNet, self).__init__() weights = None if num_classes == 1000 and data_shape == (224, 224, 3): weights = 'imagenet' self.resnet_version = resnet_version self.data_augmentation = keras.Sequential( [ layers.experimental.preprocessing.RandomFlip( "horizontal", input_shape=data_shape), layers.experimental.preprocessing.RandomRotation(0.1), layers.experimental.preprocessing.RandomZoom(0.1), ] ) self.rescaling = layers.experimental.preprocessing.Rescaling(1./255) def preprocess_input(x, data_format=None): from tensorflow.keras.applications import imagenet_utils return imagenet_utils.preprocess_input( x, data_format=data_format, mode='tf') #return x self.preprocess_input = preprocess_input if resnet_layer_number == 18: if resnet_version == 1: self.resnet = ResNet18(category_num=num_classes) else: self.resnet = ResNet18V2(category_num=num_classes) elif resnet_layer_number == 50: if resnet_version == 1: self.resnet = ResNet50(weights=weights, input_shape=data_shape, classes=num_classes) else: self.resnet = ResNet50V2(weights=weights, input_shape=data_shape, classes=num_classes) elif resnet_layer_number == 101: if resnet_version == 1: self.resnet = ResNet101(weights=weights, input_shape=data_shape, classes=num_classes) else: self.resnet = ResNet101V2(weights=weights, input_shape=data_shape, classes=num_classes) elif resnet_layer_number == 152: if resnet_version == 1: self.resnet = ResNet152(weights=weights, input_shape=data_shape, classes=num_classes) else: self.resnet = ResNet152V2(weights=weights, input_shape=data_shape, classes=num_classes) self.build((None,) + data_shape)
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 load_model(): # model = resnet_model.resnet50( # num_classes=imagenet_preprocessing.NUM_CLASSES) model = ResNet50(include_top=True, weights='imagenet') lr_schedule = 0.1 # optimizer = common.get_optimizer(lr_schedule) optimizer = tf.keras.optimizers.Adam(0.1) model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=(['sparse_categorical_accuracy'])) return model
def load_model(self): FACTOR = 0.70 HEIGHT = 137 WIDTH = 236 HEIGHT_NEW = int(HEIGHT * FACTOR) WIDTH_NEW = int(WIDTH * FACTOR) HEIGHT_NEW = 224 WIDTH_NEW = 224 base_model = ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(HEIGHT_NEW, WIDTH_NEW, 3), pooling=None, classes=1000) # base_model.trainable=False x = base_model.output x = layers.GlobalAveragePooling2D()(x) grapheme_root = layers.Dense(168, activation='softmax', name='root')(x) vowel_diacritic = layers.Dense(11, activation='softmax', name='vowel')(x) consonant_diacritic = layers.Dense(7, activation='softmax', name='consonant')(x) model = Model( inputs=base_model.input, outputs=[grapheme_root, vowel_diacritic, consonant_diacritic]) for layer in base_model.layers: layer.trainable = True model.compile(optimizer='adam', loss={ 'root': 'categorical_crossentropy', 'vowel': 'categorical_crossentropy', 'consonant': 'categorical_crossentropy' }, loss_weights={ 'root': 0.5, 'vowel': 0.25, 'consonant': 0.25 }, metrics={ 'root': 'accuracy', 'vowel': 'accuracy', 'consonant': 'accuracy' }) # print(model.summary()) return model
def create_model(input_shape): backbone = ResNet50(weights='imagenet', input_shape=input_shape, include_top=False) model = Sequential() model.add(backbone) model.add(GlobalAveragePooling2D()) model.add(Dense(1, activation='relu')) optimizer = Adam(lr=0.0005) model.compile(optimizer=optimizer, loss='mse', metrics=['mae']) return model
def create_resnet50(): baseModel = ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=input_size) # Add final layers x = baseModel.output x = layers.Flatten()(x) predictions = layers.Dense(num_classes, activation='softmax', name='fc16')(x) # This is the model we will train model = Model(inputs=baseModel.input, outputs=predictions) return model
def create_model(input_shape): optimizer = Adam(lr=0.0005) backbone = ResNet50(input_shape=input_shape, weights='imagenet', include_top=False) # замораживаем ResNet50 без верхушки # backbone.trainable = False model = Sequential() model.add(backbone) model.add(GlobalAveragePooling2D()) model.add(Dense(100, activation='relu')) model.compile(optimizer=optimizer, loss='mse', metrics=['mae']) return model
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 create_model(input_shape): backbone = ResNet50(input_shape=(150, 150, 3), weights='imagenet', include_top=False) model = Sequential() model.add(backbone) model.add(GlobalAveragePooling2D()) model.add(Dense(12, activation='softmax')) optimizer = Adam(lr=0.001) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['acc']) return model
def create_model(input_shape): backbone = ResNet50(input_shape=(224, 224, 3), weights='imagenet', include_top=False) model = Sequential() model.add(backbone) model.add(GlobalAveragePooling2D()) model.add(Dense(1, activation='relu')) optimizer = Adam(lr=0.0003ТИ ТТТ РЬРРРРРРЬТТПЖТППДЮЛ545541988 7 4 ЬЧЯ СМИ5) model.compile(optimizer=optimizer, loss='mse', metrics=['mae']) return model
def __init__(self, conf, charset): super(TextScannerModel, self).__init__() self.input_image = Input(shape=(conf.INPUT_IMAGE_HEIGHT, conf.INPUT_IMAGE_WIDTH, 3), name='input_image') self.class_branch = ClassBranchLayer(name="ClassBranchLayer", charset_size=len(charset), filter_num=conf.FILTER_NUM) self.geometry_branch = GeometryBranch(name="GeometryBranchLayer", conf=conf) self.word_formation = WordFormation(name="WordFormationLayer") self.resnet50_model = ResNet50( include_top=False, weights='imagenet' ) # Resnet50+FCN:参考 http://www.piginzoo.com/machine-learning/2020/04/23/fcn-unet#resnet50%E7%9A%84fcn self.resnet50_model.summary() self.fcn = FCNLayer(name="FCNLayer", filter_num=conf.FILTER_NUM, resnet50_model=self.resnet50_model)
def get_model_body(input_tensor, net='vgg16', trainable=True): if net not in nets: raise ValueValidException('net: {%s} not supported, only support %s' % (net, nets)) if net == config.network[0]: share_model = VGG16(input_tensor=input_tensor, include_top=False) elif net == config.network[1]: share_model = ResNet50(input_tensor=input_tensor, include_top=False) elif net == config.network[2]: share_model = InceptionV3(input_tensor=input_tensor, include_top=False) else: print('resnet101 still not implement') if not trainable: for layer in share_model.layers: layer.trainable = False model = Model(inputs=share_model.input, outputs=share_model.get_layer(index=-2).output) return model
def tl_create_cnn(self, input_shape, selection='vgg16', filters=(16, 32, 64)): ''' Use VGG16, Resnet or Densenet as the base model Freeze pre-trained layers and add final layers to selected model ''' # Implement VGG16 model if selection == 'vgg16': model = VGG16(weights='imagenet', input_shape=input_shape, include_top=False) # Implement ResNet model if selection == 'resnet': model = ResNet50(weights='imagenet', input_shape=input_shape, include_top=False, classes=7) # Implement DenseNet model if selection == 'densenet': model = densenet(weights='imagenet', input_shape=input_shape, include_top=False, classes=7) # Make pre-trained layers non iterable and add final layers for layer in model.layers: layer.trainable = False inp = Input(shape=input_shape) base_output = model(inp) x = Flatten()(base_output) x = Dense(126, activation='relu')(x) x = Dense(32, activation='relu')(x) x = Dense(4, activation='relu')(x) # construct the CNN and return model model = Model(inp, x) return model
def resnet50_fcn(n_classes, weights=None): # load ResNet # input_tensor = Input(shape=(128, 256, 3)) input_tensor = Input(shape=(None, None, 3)) base_model = ResNet50(weights=weights, include_top=False, input_tensor=input_tensor) # add 32s classifier x = base_model.get_layer('conv5_block3_out').output x = Dropout(0.5)(x) x = Conv2D(n_classes, 1, name='pred_32', padding='same')(x) # add upsampler stride = 32 pred_32s = UpSampling2D(size=(stride, stride), interpolation='bilinear')(x) # add 16s classifier x = base_model.get_layer('conv4_block6_out').output x = Dropout(0.5)(x) x = Conv2D(n_classes, 1, name='pred_16', padding='same')(x) x = UpSampling2D(name='upsampling_16', size=(stride // 2, stride // 2), interpolation='bilinear')(x) # merge classifiers pred_16s = Add(name='pred_16s')([x, pred_32s]) # add 8s classifier x = base_model.get_layer('conv3_block4_out').output x = Dropout(0.5)(x) x = Conv2D(n_classes, 1, name='pred_8', padding='same')(x) x = UpSampling2D(name='upsampling_8', size=(stride // 4, stride // 4), interpolation='bilinear')(x) # merge classifiers pred_8s = Add(name='pred_8s')([x, pred_16s]) model = Model(inputs=base_model.input, outputs=pred_8s) return model
def create_model(input_shape): backbone = ResNet50(input_shape=input_shape, weights='imagenet', include_top=False) # замораживаем ResNet50 без верхушки (если мало картинок в датасте) # backbone.trainable = False model = Sequential() model.add(backbone) model.add(GlobalAveragePooling2D()) model.add(Dense(1, activation='relu')) optimizer = Adam(lr=0.0001) # optimizer = 'sgd' model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mae']) return model
def modelGA(): backbone = ResNet50(input_shape=(227, 227, 3), weights='imagenet', include_top=False) model = Sequential() model.add(keras.Input(shape=(227, 227, 3))) model.add(backbone) model.add(GlobalAveragePooling2D()) model.add(Dropout(0.5)) model.add(Dense(256, activation='relu')) model.add(Dropout(0.5)) if COUNT == 0: model.add(Dense(8, activation='softmax')) model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) else: model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy']) return model