def app_net(archname='mobilenet', z_dim=128, input_shape=(160, 160, 3)): if archname == 'mobilenet': basemodel = app.MobileNetV2(input_shape=input_shape, include_top=False, weights='imagenet') elif archname == 'resnet50': basemodel = app.ResNet50(input_shape=input_shape, include_top=False, weights='imagenet') elif archname == 'resnet101': basemodel = app.ResNet101(input_shape=input_shape, include_top=False, weights='imagenet') elif archname == 'resnet152': basemodel = app.ResNet152(input_shape=input_shape, include_top=False, weights='imagenet') else: raise ValueError('Invalid archname: {}'.format(archname)) gal = L.GlobalAveragePooling2D() dense = L.Dense(z_dim) inputs = tf.keras.Input(shape=input_shape) h = basemodel(inputs, training=True) h = gal(h) h = dense(h) outputs = tf.tanh(h) model = tf.keras.Model(inputs, outputs) return model
def EncoderNetwork(): import tensorflow.keras.applications as apps with tf.name_scope("Encoder_resnet"): resnet = apps.ResNet50(include_top=False, weights='imagenet', pooling='avg') return resnet
def _create_model(self, x: tf.Tensor, is_training: bool) -> tf.Tensor: """ Implement the architecture of your model :param x: input data :param is_training: flag if currently training :return: completely constructed model """ tf.logging.info("Constructing OAHO Model Deeplab") def decoder_block(input_tensor: tf.Tensor, feature_maps: int, kernel_size: Tuple[int, int], strides: Tuple[int, int], is_training: bool, skip_connection: tf.Tensor = None) -> tf.Tensor: x = kl.Conv2DTranspose(feature_maps, kernel_size=kernel_size, strides=strides, padding='same')(input_tensor) x = kl.BatchNormalization()(x) x = kl.ReLU()(x) if skip_connection is not None: x = tf.keras.layers.concatenate([x, skip_connection]) return x no_filters = [10, 32, 128, 512] filter_sizes= [(3, 3), (3, 3), (3, 3), (2, 2)] input_tensor = x x = tf.keras.layers.concatenate([x,x,x]) pretrained_weights = 'imagenet' if is_training else None resnet = ka.ResNet50(weights=pretrained_weights, include_top=False, input_tensor=x, input_shape=(480,640,3)) enc1 = resnet.get_layer('activation').output # resnet.layers[4].output enc2 = resnet.get_layer('activation_9').output # resnet.layers[38].output enc3 = resnet.get_layer('activation_21').output # resnet.layers[80].output enc4 = resnet.get_layer('activation_39').output # resnet.layers[142].output # enc5 = resnet.get_layer('activation_48').output # resnet.layers[174].output dec = enc4 # dec = decoder_block(dec, no_filters[3], kernel_size=filter_sizes[3], strides=(2, 2), skip_connection=enc4, is_training=is_training) dec = decoder_block(dec, no_filters[3], kernel_size=filter_sizes[3], strides=(2, 2), skip_connection=enc3, is_training=is_training) dec = decoder_block(dec, no_filters[2], kernel_size=filter_sizes[2], strides=(2, 2), skip_connection=enc2, is_training=is_training) dec = decoder_block(dec, no_filters[1], kernel_size=filter_sizes[1], strides=(2, 2), skip_connection=enc1, is_training=is_training) dec = decoder_block(dec, no_filters[0], kernel_size=filter_sizes[0], strides=(2, 2), skip_connection=input_tensor, is_training=is_training) x = dec # =================================================================================================== # Output layers seg_head = kl.Conv2D(32, kernel_size=2, padding='same', name='seg_head_1', activation='relu')(x) seg_output = kl.Conv2D(4, kernel_size=2, padding='same', name='seg_out')(seg_head) grasp_head = kl.Conv2D(32, kernel_size=2, padding='same', name='grasp_head_1', activation='relu')(x) pos_output = kl.Conv2D(1, kernel_size=2, padding='same', name='pos_out')(grasp_head) cos_output = kl.Conv2D(1, kernel_size=2, padding='same', name='cos_out')(grasp_head) sin_output = kl.Conv2D(1, kernel_size=2, padding='same', name='sin_out')(grasp_head) width_output = kl.Conv2D(1, kernel_size=2, padding='same', name='width_out')(grasp_head) return seg_output, pos_output, cos_output, sin_output, width_output
def compute_mean_and_std(model_name, X, input_shape): if model_name == 'Xception': model = applications.Xception(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'VGG16': model = applications.VGG16(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'VGG19': model = applications.VGG19(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'ResNet50': model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'InceptionResNetV2': model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'InceptionV3': model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'MobileNet': model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'DenseNet121': model = applications.DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'DenseNet169': model = applications.DenseNet169(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'DenseNet201': model = applications.DenseNet201(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'NASNetMobile': model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'NASNetLarge': model = applications.NASNetLarge(weights='imagenet', include_top=False, input_shape=input_shape) else: assert (False), "Specified base model is not available !" features = model.predict(X)[:, 0, 0, :] return features.mean(axis=0), features.std(axis=0)
def Det_RN50(): base_model = applications.ResNet50(weights='imagenet', include_top=False) x = base_model.output # for layer in base_model.layers[:140]: # Keep the pretrained params # layer.trainable = True # for layer in base_model.layers[140:]: # Keep the pretrained params # layer.trainable = True x = GlobalAveragePooling2D()(x) predictions = Dense(1, name='predictions')(x) model = Model(base_model.input, predictions) return model
def CreateModel(): model = models.Sequential() model.add( applications.ResNet50(include_top=False, input_shape=(150, 150, 3))) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(6, activation='softmax')) model.compile(optimizer=optimizers.Adam(0.001), loss='categorical_crossentropy', metrics=['acc']) return model
def build_features_extractor(model_name, input_shape): if model_name == 'VGG16': model = applications.VGG16(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'VGG19': model = applications.VGG19(weights='imagenet', include_top=False, input_shape=input_shape) elif model_name == 'ResNet50': model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) else: assert (False), "Specified base model is not available !" x = model.output x = layers.Flatten()(x) return keras.Model(inputs=model.input, outputs=x)
def build_resnet50(pretrained=True): if pretrained: weights = 'imagenet' else: weights = None model = model_zoo.ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(224, 224, 3), classes=3) if pretrained: model.save('saved_keras_resnet.h5') return model
def main(): resnet = applications.ResNet50(weights='imagenet', include_top=False) resnet.build((4, 224, 224, 3)) # resnet.summary() x = tf.random.normal((4, 224, 224, 3)) out = resnet(x) print(out.shape) # 全局平均池化 global_average_layer = layers.GlobalAveragePooling2D() # 利用上一层的输出作为本层的输入,测试其输出 x = tf.random.normal([4, 7, 7, 2048]) out = global_average_layer(x) # 池化层降维 print(out.shape)
def create_model(input_shape, n_out): input_tensor = Input(shape=input_shape) base_model = applications.ResNet50( weights='imagenet', # à modifier include_top=False, input_tensor=input_tensor) print(base_model.summary()) x = GlobalAveragePooling2D()(base_model.output) x = Dropout(0.5)(x) output = Dense(512, activation='relu', name='output')(x) # 1024 output = Dropout(0.5)(output) model_prim = Model(input_tensor, output) final_output = Dense(n_out, activation='softmax', name='final_output')(model_prim.output) model = Model(input_tensor, final_output) return model
def CreateModelnd(): model = models.Sequential() resnet = applications.ResNet50(include_top=False, input_shape=(150, 150, 3)) for layer in resnet.layers: layer.trainable = False model.add(resnet) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(2048, activation='relu')) model.add(layers.Dense(6, activation='softmax')) model.compile(optimizer=optimizers.Adam(0.001), loss='categorical_crossentropy', metrics=['acc']) return model
def create_model(input_shape, n_out): input_tensor = Input(shape=input_shape) base_model = applications.ResNet50( weights='imagenet', #à modifier include_top=False, input_tensor=input_tensor) for layer in base_model.layers: layer.trainable = False x = GlobalAveragePooling2D()(base_model.output) output = Dense(2300, activation='relu', name='output')(x) output = Dropout(0.25)(output) model_prim = Model(input_tensor, output) final_output = Dense(n_out, activation='softmax', kernel_regularizer=regularizers.l2(0.01), name='final_output')(model_prim.output) model = Model(input_tensor, final_output) return model
def my_resnet_func(input_shape, num_classes): resnet50 = applications.ResNet50(include_top=False, weights='imagenet', input_shape=input_shape) resnet50.trainable = False add_model = Sequential() add_model.add(resnet50) add_model.add(Flatten()) # add_model.add(Dense(256, activation='relu')) add_model.add(Dense(512, activation='relu')) add_model.add(Dropout(0.5)) add_model.add(Dense(num_classes, activation='softmax')) # add_model.add(Dense(num_classes, activation='sigmoid')) # binary_crossentropy add_model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy']) return add_model
def create_model(input_shape, n_out): input_tensor = Input(shape=input_shape) base_model = applications.ResNet50( weights=None, #à modifier include_top=False, input_tensor=input_tensor) base_model.load_weights( 'C:/Users/asus/Desktop/model/Resnet_Weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' ) for layer in base_model.layers: layer.trainable = False x = GlobalAveragePooling2D()(base_model.output) output = Dense(2300, activation='relu', name='output')(x) output = Dropout(0.25)(output) model_prim = Model(input_tensor, output) final_output = Dense(n_out, activation='softmax', kernel_regularizer=regularizers.l2(0.01), name='final_output')(model_prim.output) model = Model(input_tensor, final_output) return model
checkpoint_history = os.listdir("training/") os.mkdir("training/"+str(len(checkpoint_history))) checkpoint_path = "training/"+str(len(checkpoint_history))+"/cp-{epoch:04d}.ckpt" checkpoint_dir = os.path.dirname(checkpoint_path) cp_callback = tf.keras.callbacks.ModelCheckpoint( checkpoint_path, verbose=1, save_weights_only=True, period=5) rd_callback = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=0.00001) es_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=20) # model model = models.Sequential() resnet = applications.ResNet50(include_top=False, input_shape=(150, 150, 3)) for layer in resnet.layers: layer.trainable = False model.add(resnet) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(2048, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(6, activation='softmax')) model.compile(optimizer=optimizers.Adam(0.001), loss='categorical_crossentropy', metrics=['acc']) # training
from tensorflow.keras import applications from tensorflow.keras import Model, layers import tensorflow as tf img_shape = (254, 200, 3) #Loading a pre-trained model #Here we use ResNet50 architecture, we use "imagenet" weights, #also we pass the image shape Note that include_top means that we do NOT want the top layers base_cnn = applications.ResNet50(weights='imagenet', input_shape=img_shape, include_top=False) #Fine Tuning #Here we fine tune the ResNet50 we freeze all layers that exist before "conv5_block1_out" layer, #starting from "conv5_block2_2_relu" layer we unfreeze all the layers so we can just train these layers trainable = False for layer in base_cnn.layers: if layer.name == 'conv5_block1_out': trainable = True layer.trainable = trainable #Adding top layers #Here we customize the model by adding Dense layers and Batch Normalization layers. # we start with the image input then we pass the input to the base_cnn then we flatten it. #Finally we pass each layer as an input to the next layer #the output layer is just a dense layer which will act as an embedding for our images. flatten = layers.Flatten()(base_cnn.output) dense1 = layers.Dense(512, activation='relu')(flatten) dense1 = layers.BatchNormalization()(dense1) dense2 = layers.Dense(256, activation='relu')(dense1)
warnings.filterwarnings("ignore") from tensorflow.keras.models import Model from tensorflow.keras import optimizers, applications from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D, Input tf.random.set_random_seed(42) IMAGES = 20 print('\nGenerating image data...') t = time() images = np.random.random((IMAGES, 224, 224, 3)) labels = np.random.randint(0, 9, (IMAGES, 1)) print(f'It took {(time()-t):.2f} s.\n') input_tensor = Input(shape=(224, 224, 3)) base_model = applications.ResNet50(include_top=False, input_tensor=input_tensor) x = GlobalAveragePooling2D()(base_model.output) final_output = Dense(10, activation='softmax')(x) model = Model(input_tensor, final_output) model.compile(optimizer=optimizers.Adam(lr=1e-3), loss="sparse_categorical_crossentropy", metrics=['accuracy']) print(f'Training 5 epochs of 10-class ResNet50 on ({IMAGES}, 224, 224, 3)...') t = time() model.fit(images, labels, batch_size=2, epochs=5) print(f'Training took {(time()-t):.2f} s.\n')
# Augment training and test data train_generator = fun.augmentation(train_images, train_labels, VERTICAL_FLIP, HORIZONTAL_FLIP, BATCH) test_generator = fun.augmentation(test_images, test_labels, VERTICAL_FLIP, HORIZONTAL_FLIP, BATCH) # Plot images and correct labels from augmented data fun.plot_images(test_generator, train_labels, CLASS_NAMES) # Choose model # model = cnn.LeNet() # model = cnn.CustomNet() # %% TRANSFER LEARNING # Choose pretrained network pretrainedNet = applications.ResNet50(weights='imagenet', include_top=False, input_shape=(32,32,3)) # Set trainable parameters False, so you can train only last layer pretrainedNet.trainable = False inputs = Input(shape=(32,32,3)) x = pretrainedNet(inputs, training=False) x = layers.GlobalAveragePooling2D()(x) # Last layer, dense size is the number of classes outputs = layers.Dense(3)(x) model = Model(inputs, outputs) # %% COMPILE AND TRAIN MODEL
def build_autoencoder(base_model_name, input_shape, imagenet_mean, imagenet_std, hidden_layer_size, n_classes, weight_decay): if base_model_name == 'Xception': base_model = applications.Xception(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'VGG16': base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'VGG19': base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'ResNet50': base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'InceptionResNetV2': base_model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'InceptionV3': base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'MobileNet': base_model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'DenseNet121': base_model = applications.DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'DenseNet169': base_model = applications.DenseNet169(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'DenseNet201': base_model = applications.DenseNet201(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'NASNetMobile': base_model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=input_shape) elif base_model_name == 'NASNetLarge': base_model = applications.NASNetLarge(weights='imagenet', include_top=False, input_shape=input_shape) else: assert (False), "Specified base model is not available !" n_features = base_model.output.shape[-1] x = base_model.output x = tf.keras.layers.Lambda(lambda x: (x - imagenet_mean) / imagenet_std)( x) # normalization x = tf.keras.layers.Activation(activation='sigmoid', name='encoder')(x) # sigmoid x = tf.keras.layers.Dense(units=hidden_layer_size, activation=None)(x) # encoding x = tf.keras.layers.Activation(activation='relu')(x) # relu x = tf.keras.layers.Dense(units=n_features, activation=None, name='decoder')(x) # decoding x = tf.keras.layers.Dense(units=n_classes, activation='sigmoid')( x) # x = tf.keras.layers.Activation(activation='sigmoid')(x) # sigmoid model = tf.keras.Model(inputs=base_model.input, outputs=x[:, 0, 0, :]) for layer in model.layers: if isinstance(layer, tf.keras.layers.Conv2D) or isinstance( layer, tf.keras.layers.Dense): layer.add_loss( tf.keras.regularizers.l2(weight_decay)(layer.kernel)) if hasattr(layer, 'bias_regularizer') and layer.use_bias: layer.add_loss(tf.keras.regularizers.l2(weight_decay)(layer.bias)) return model
def make_model(args, nclass, input_shape): # Weight decay if args.weight_decay is not None and args.weight_decay > 0: regularizer = keras.regularizers.L2(args.weight_decay / 2) else: regularizer = None if args.optimizer == 'lamb': regularizer = None logging.info( 'adding weight decay via the LAMB optimizer instead of Keras regularization' ) # Inputs input = keras.Input(input_shape, name='image') input2 = keras.Input(input_shape, name='image2') if args.backbone == 'resnet50v2': backbone = applications.ResNet50V2(weights=None, include_top=False, input_shape=input_shape, pooling='avg') if (input_shape[0] or 224) < 224: logging.warning('using standard resnet on small dataset') elif args.backbone == 'small-resnet50v2': backbone = small_resnet_v2.SmallResNet50V2(include_top=False, input_shape=input_shape, pooling='avg') if (input_shape[0] or 224) >= 224: logging.warning('using small resnet on large dataset') elif args.backbone == 'resnet50': backbone = applications.ResNet50(weights=None, include_top=False, input_shape=input_shape, pooling='avg') elif args.backbone == 'affine': backbone = keras.Sequential( [layers.GlobalAveragePooling2D(), layers.Dense(1)]) else: raise Exception(f'unknown model {args.backbone}') # Add weight decay to backbone backbone = add_regularization_with_reset(backbone, regularizer) # Standardize input stand_img = custom_layers.StandardizeImage() # Features raw_feats, raw_feats2 = backbone(stand_img(input)), backbone( stand_img(input2)) # Normalize features? feats, feats2 = optional_normalize(args.feat_norm, raw_feats, raw_feats2) # Name the features feats = custom_layers.Identity(name='feats')(feats) feats2 = custom_layers.Identity(name='feats2')(feats2) # Measure the norms of the features if args.feat_norm is not None: feats = custom_layers.MeasureNorm(name='feat_norm')(feats) # Projection if args.proj_dim is None or args.proj_dim <= 0: projection = custom_layers.Identity(name='projection') else: projection = tf.keras.Sequential([ layers.Dense(2048, kernel_regularizer=regularizer, bias_regularizer=regularizer), layers.BatchNormalization(), layers.ReLU(), layers.Dense(2048, kernel_regularizer=regularizer, bias_regularizer=regularizer), layers.BatchNormalization(), layers.ReLU(), layers.Dense( args.proj_dim, kernel_regularizer=regularizer, use_bias=False) ], name='projection') if args.proj_norm == 'sn': projection = custom_layers.SpectralNormalization(projection, name='sn_projection') # Projected features proj_feats, proj_feats2 = projection(feats), projection(feats2) # Normalize projected features? proj_feats, proj_feats2 = optional_normalize(args.proj_norm, proj_feats, proj_feats2) # Name the projected features proj_feats = custom_layers.Identity(name='proj_feats')(proj_feats) proj_feats2 = custom_layers.Identity(name='proj_feats2')(proj_feats2) # Measure the norms of the projected features if args.proj_dim is not None and args.proj_dim > 0: proj_feats = custom_layers.MeasureNorm(name='proj_norm')(proj_feats) # Feature views proj_views = custom_layers.FeatViews(name='contrast', dtype=tf.float32)( (proj_feats, proj_feats2)) # Label logits if args.stop_gradient: feats = tf.stop_gradient(feats) prediction = layers.Dense( nclass, name='label', kernel_regularizer=regularizer if args.optimizer != 'lamb' else None, bias_regularizer=regularizer if args.optimizer != 'lamb' else None, dtype=tf.float32)(feats) # Model inputs = [input, input2] outputs = [prediction, proj_views] model = keras.Model(inputs, outputs) return model
def create(): """ This function imports ResNet50 from Keras.applications and load it with pretrained imagenet weights. Then it adds some layer to adapt the model for performing classifications on cifar10, specifically it adds: - Flatten - BatchNormalization - Dense (fully connected) with 128 neurons, activation ReLU - Dropout of 0.5 - BatchNormalization - Dense (fully connected) with 64 neurons, activation ReLU - Dropout of 0.5 - BatchNormalization - Dense (fully connected) with 10 neurons (= number of classes), activation Softmax Then the function compiles, fits and saves the model. """ # ALTERNATIVE 1: work with small images, it takes more epochs to get a good accuracy and pruning will be possible # Comment this block and uncomment ALTERNATIVE 2 if you wish to use the other method resnet = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) inp = resnet.input out = layers.Flatten()(resnet.output) out = layers.BatchNormalization()(out) out = layers.Dense(128, activation='relu')(out) out = layers.Dropout(0.5)(out) out = layers.BatchNormalization()(out) out = layers.Dense(64, activation='relu')(out) out = layers.Dropout(0.5)(out) out = layers.BatchNormalization()(out) out = layers.Dense(10, activation='softmax')(out) model = Model(inp, out) model.summary() """ # ALTERNATIVE 2: work with upsampled images, it takes few epochs to get a good accuracy but it won't be possible # to prune ResNet50's layers, this because ResNet50 itself is seen as a single layer in a Sequential model. # Comment this block and uncomment ALTERNATIVE 1 if you wish to use the other method resnet = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(256, 256, 3)) model = Sequential() model.add(layers.UpSampling2D((2,2))) model.add(layers.UpSampling2D((2,2))) model.add(layers.UpSampling2D((2,2))) model.add(resnet) model.add(layers.Flatten()) model.add(layers.BatchNormalization()) model.add(layers.Dense(128, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.BatchNormalization()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.BatchNormalization()) model.add(layers.Dense(10, activation='softmax')) """ model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='binary_crossentropy', metrics=['acc']) history = model.fit(train_imgs, train_lbls, epochs=20, batch_size=20, validation_data=(test_imgs, test_lbls), use_multiprocessing=True) model.save(model_path)
rotation_range=20, validation_split=0.2) train_dataset = train_data.flow_from_directory( base_path + "/train", target_size=(256, 256), class_mode='spare', subset='validation', ) test_data = ImageDataGenerator(rescale=1. / 255) test_dataset = test_data.flow_from_directory(base_path + "/valid", target_size=(256, 256), class_mode='spare') base_model = applications.ResNet50(input_shape=(256, 256, 3), include_top=False, weights='imagenet') base_model.trainable = False model = tf.keras.Sequential([ base_model, layers.GlobalAveragePooling2D(), layers.Flatten(), layers.Dense(512, activation='relu'), layers.Dropout(0.5), layers.Dense(102, activation='softmax') ]) model.compile(optimizers.Adam(learning_rate=0.003), loss=losses.SparseCategoricalCrossentropy(),
def resnet_retinanet(num_classes, backbone="resnet50", inputs=None, modifier=None, **kwargs): """ Constructs a retinanet model using a resnet backbone. Args num_classes: Number of classes to predict. backbone: Which backbone to use (one of ('resnet50', 'resnet101', 'resnet152')). inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)). modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example). Returns RetinaNet model with a ResNet backbone. """ # choose default input if inputs is None: if keras.backend.image_data_format() == "channels_first": inputs = keras.layers.Input(shape=(3, None, None)) else: inputs = keras.layers.Input(shape=(None, None, 3)) # create the resnet backbone if backbone == "resnet50": resnet = app.ResNet50(input_tensor=inputs, include_top=False) resnet_output_names = [ "conv3_block4_out", "conv4_block6_out", "conv5_block3_out", ] elif backbone == "resnet101": resnet = app.ResNet101(input_tensor=inputs, include_top=False) resnet_output_names = [ "conv3_block4_out", "conv4_block23_out", "conv5_block3_out", ] elif backbone == "resnet152": resnet = app.ResNet152(input_tensor=inputs, include_top=False) resnet_output_names = [ "conv3_block8_out", "conv4_blocki36_out", "conv5_block3_out", ] else: raise ValueError("Backbone ('{}') is invalid.".format(backbone)) _freeze_bn(resnet) if False: print("Backbone summary:") resnet.summary() from tensorflow.keras.utils import plot_model plot_model(resnet, to_file="model.png") backbone_outputs = [ resnet.get_layer(layer_name).output for layer_name in resnet_output_names ] # invoke modifier if given if modifier: resnet = modifier(resnet) # create the full model return retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=backbone_outputs, **kwargs)