def EfficientNetB7(include_top=False, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, *args, **kwargs): model = efn.EfficientNetB7(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=input_shape, pooling=pooling, classes=classes) # or weights='noisy-student' # 一共806层 if weights: for i in model.layers[:688]: i.trainable = False x = model.output if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) else: x = layers.Flatten(name='flatten')(x) # x = layers.GlobalAveragePooling2D(name='avg_pool')(x) activation = 'sigmoid' if classes == 1 else 'softmax' x = layers.Dense(classes, activation=activation, name='predictions')(x) model = Model(model.input, x, name='EfficientNetB7') return model
def _create_cord_reg_model(self, input_shape, input_tensor, num_landmark): """ This is EfficientNet-B7 combined with one stack of StackedHourGlassNetwork used as heatmap & geo regressor network. :param input_shape: :param input_tensor: :param num_landmark: :return: model """ initializer = tf.random_normal_initializer(0., 0.02) eff_net = efn.EfficientNetB7(include_top=True, weights=None, input_tensor=input_tensor, input_shape=input_shape, pooling=None, classes=num_landmark) # or weights='noisy-student' eff_net.layers.pop() inp = eff_net.input x = eff_net.get_layer('top_activation').output x = GlobalAveragePooling2D()(x) x = Dropout(rate=0.3)(x) output = Dense(num_landmark, activation='linear', name='out', kernel_initializer=initializer, use_bias=False)(x) eff_net = Model(inp, output) eff_net.summary() model_json = eff_net.to_json() with open("./model_arch/cord_reg_model.json", "w") as json_file: json_file.write(model_json) return eff_net
def build_backbone_net_graph(input_tensor, architecture, weights=None): """ Build basic feature extraction networks. :param input_tensor: Input of the basic networks, should be a tensor or tf.keras.layers.Input :param architecture: The architecture name of the basic network. :param weights: Whether download and initialize weights from the pre-trained weights, could be either 'imagenet', (pre-training on ImageNet) 'noisy-student', 'None' (random initialization), or the path to the weights file to be loaded。 :return: Efficient Model and corresponding endpoints. """ assert architecture in ['efficientnet-b0', 'efficientnet-b1', 'efficientnet-b2', 'efficientnet-b3', 'efficientnet-b4', 'efficientnet-b5', 'efficientnet-b7', 'efficientnet-b7', 'efficientnet-l2'] if architecture == 'efficientnet-b0': return efn.EfficientNetB0(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b1': return efn.EfficientNetB1(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b2': return efn.EfficientNetB2(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b3': return efn.EfficientNetB3(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b4': return efn.EfficientNetB4(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b5': return efn.EfficientNetB5(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b6': return efn.EfficientNetB6(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b7': return efn.EfficientNetB7(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-l2': return efn.EfficientNetL2(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) else: raise ValueError("Argument architecture should in " "[efficientnet-b0, efficientnet-b1, " "efficientnet-b2, efficientnet-b3, efficientnet-b4, efficientnet-b5, " "efficientnet-b7, efficientnet-b7, efficientnet-l2] " "but get %s" % architecture)
def create_model(model_name, input_shape): if model_name == 'efn_b4': model = efn.EfficientNetB4(weights=None, classes=4) elif model_name == 'efn_b4_p': model = tf.keras.models.Sequential() model.add(efn.EfficientNetB4(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'efn_b5_p': model = tf.keras.models.Sequential() model.add(efn.EfficientNetB5(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'efn_b7_p': model = tf.keras.models.Sequential() model.add(efn.EfficientNetB7(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'resnet18': model = ResNet([2, 2, 2, 2], input_shape=input_shape) elif model_name == 'densenet121_p': model = tf.keras.models.Sequential() model.add(DenseNet121(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'densenet201_p': model = tf.keras.models.Sequential() model.add(DenseNet201(input_shape=input_shape, weights='imagenet', include_top=False)) if model_name.split('_')[-1] == 'p': model.add(GlobalAveragePooling2D()) # model.add(Dense(128, activation='relu')) # model.add(Dense(64, activation='relu')) model.add(Dense(4, activation='softmax')) model.summary() return model
def create_cnn_model(): model = keras.models.Sequential() pre_trained_model = efn.EfficientNetB7(input_shape=(*IMG_SIZE, 3),mju87 include_top=False, weights='noisy-student') # freeze the batch normalisation layers for layer in reversed(pre_trained_model.layers): if isinstance(layer, tf.keras.layers.BatchNormalization): layer.trainable = False else: layer.trainable = True model.add(pre_trained_model) model.add(layers.Dropout(0.25)) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dropout(0.25)) model.add(layers.Dense(5, activation='softmax')) optimizer = tf.keras.optimizers.Adam() loss = tf.keras.losses.CategoricalCrossentropy() model.compile(optimizer=optimizer, loss=loss, metrics='accuracy') print(model.summary()) return model
def efficientnet(): first = efn.EfficientNetB7(input_shape = (224, 224, 3), weights = 'imagenet', include_top = False) x = first.output x = GlobalAveragePooling2D()(x) x = Dense(1)(x) output = Activation('sigmoid')(x) model = Model(first.input, output) return model
def effnet_retinanet(num_classes, backbone='EfficientNetB0', 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=(224, 224, 3)) inputs = keras.layers.Input(shape=(None, None, 3)) # get last conv layer from the end of each block [28x28, 14x14, 7x7] if backbone == 'EfficientNetB0': model = efn.EfficientNetB0(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB1': model = efn.EfficientNetB1(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB2': model = efn.EfficientNetB2(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB3': model = efn.EfficientNetB3(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB4': model = efn.EfficientNetB4(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB5': model = efn.EfficientNetB5(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB6': model = efn.EfficientNetB6(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB7': model = efn.EfficientNetB7(input_tensor=inputs, include_top=False, weights=None) else: raise ValueError('Backbone (\'{}\') is invalid.'.format(backbone)) layer_outputs = ['block4a_expand_activation', 'block6a_expand_activation', 'top_activation'] layer_outputs = [ model.get_layer(name=layer_outputs[0]).output, # 28x28 model.get_layer(name=layer_outputs[1]).output, # 14x14 model.get_layer(name=layer_outputs[2]).output, # 7x7 ] # create the densenet backbone model = keras.Model(inputs=inputs, outputs=layer_outputs, name=model.name) # invoke modifier if given if modifier: model = modifier(model) # create the full model return retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=model.outputs, **kwargs)
def get_efficientnet_model(model_version): if model_version == "B0": return efn.EfficientNetB0(weights='imagenet') elif model_version == "B1": return efn.EfficientNetB1(weights='imagenet') elif model_version == "B2": return efn.EfficientNetB2(weights='imagenet') elif model_version == "B3": return efn.EfficientNetB3(weights='imagenet') elif model_version == "B4": return efn.EfficientNetB4(weights='imagenet') elif model_version == "B5": return efn.EfficientNetB5(weights='imagenet') elif model_version == "B6": return efn.EfficientNetB6(weights='imagenet') elif model_version == "B7": return efn.EfficientNetB7(weights='imagenet') else: return efn.EfficientNetB0(weights='imagenet')
def model_EfficientNet(args): base_model = efn.EfficientNetB7(include_top=False, input_shape=(args.IMG_HEIGHT, args.IMG_WIDTH, 3), weights='imagenet', pooling='max') model = Sequential() model.add(base_model) model.add(Dense(512, activation='relu')) model.add(Dense(args.num_classes, activation='softmax')) return model
def model_efn(): input_shape = (150, 150, 3) classes = 4 model = efn.EfficientNetB7(weights='imagenet', input_shape=input_shape, pooling='max', include_top=False) x = model.output output = Dense(classes, activation='softmax')(x) return Model(inputs=model.input, outputs=output)
def get_efficientnet(self): models_dict ={ 'b0': efn.EfficientNetB0(input_shape=self.shape,weights=None,include_top=False), 'b1': efn.EfficientNetB1(input_shape=self.shape,weights=None,include_top=False), 'b2': efn.EfficientNetB2(input_shape=self.shape,weights=None,include_top=False), 'b3': efn.EfficientNetB3(input_shape=self.shape,weights=None,include_top=False), 'b4': efn.EfficientNetB4(input_shape=self.shape,weights=None,include_top=False), 'b5': efn.EfficientNetB5(input_shape=self.shape,weights=None,include_top=False), 'b6': efn.EfficientNetB6(input_shape=self.shape,weights=None,include_top=False), 'b7': efn.EfficientNetB7(input_shape=self.shape,weights=None,include_top=False) } return models_dict[self.model_class]
def __init__(self, hparams): super(InputEmbedding, self).__init__() self.hparams = hparams if hparams.base_model_name == 'InceptionV3': base_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'InceptionResNetV2': base_model = tf.keras.applications.InceptionResNetV2( include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB0': base_model = efn.EfficientNetB0(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB1': base_model = efn.EfficientNetB1(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB2': base_model = efn.EfficientNetB2(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB3': base_model = efn.EfficientNetB3(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB4': base_model = efn.EfficientNetB4(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB5': base_model = efn.EfficientNetB5(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB6': base_model = efn.EfficientNetB6(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB7': base_model = efn.EfficientNetB7(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] assert hparams.end_point in base_model_layers, "no {} layer in {}".format( hparams.end_point, hparams.base_model_name) conv_tower_output = base_model.get_layer(hparams.end_point).output self.conv_model = tf.keras.models.Model(inputs=base_model.input, outputs=conv_tower_output) self.conv_out_shape = self.conv_model.predict( np.array([np.zeros(hparams.image_shape)])).shape self.encode_cordinate = EncodeCordinate( input_shape=self.conv_out_shape)
def create_b7(include_top=False, input_shape=None, input_tensor=None, weights="noisy-student"): """ネットワークの作成。""" import efficientnet.tfkeras as efn return efn.EfficientNetB7( include_top=include_top, input_shape=input_shape, input_tensor=input_tensor, weights=weights, )
def crnn_network(self): print('Loading crnn backbone model.....') b7 = ef.EfficientNetB7(include_top=False, weights=None, input_shape=(32, None, 3)) print('Done!') y = keras.layers.MaxPool2D(pool_size=(4, 1))( b7.get_layer(name='block4a_expand_activation').output) y = self.blstm(y) y = layers.Dropout(self.dropout)(y) y = layers.Dense(self.num_classes, activation='softmax', name='FC_1')(y) crnn_model = Model(b7.inputs, y) print('Construct crnn_model Done!') return crnn_model
def create_EfficientNet_model_am_loss(): pretrained_model = efficientnet.EfficientNetB7( weights='noisy-student', include_top=False, input_shape=[*IMAGE_SIZE, 3]) pretrained_model.trainable = True model = tf.keras.Sequential([ pretrained_model, tf.keras.layers.GlobalAveragePooling2D(), CosLayer(len(CLASSES)) ]) model.compile(optimizer='adam', loss=am_loss, metrics=['sparse_categorical_accuracy']) return model
def prepare_model_efficient_net(): model = Sequential() # Build Model enet = efn.EfficientNetB7(input_shape=input_shape, weights='imagenet', include_top=False) enet.trainable = True model = tf.keras.Sequential([ enet, tf.keras.layers.GlobalMaxPooling2D(name="Layer1"), tf.keras.layers.Dense(num_classes, activation='softmax') ]) # Compile Model model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy']) return model
def create_effNet(self, input_shape, input_tensor, num_landmark): """ This is EfficientNet-B7 main network. :param input_shape: :param input_tensor: :param num_landmark: :return: model """ eff_net = efn.EfficientNetB7(include_top=True, weights=None, input_tensor=input_tensor, input_shape=input_shape, pooling=None, classes=num_landmark) model_json = eff_net.to_json() with open("./model_arch/effNet-b7_main.json", "w") as json_file: json_file.write(model_json) return eff_net
def get_model(): with strategy.scope(): rnet = efn.EfficientNetB7( input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3), weights='noisy-student', include_top=False ) # trainable rnet rnet.trainable = True model = tf.keras.Sequential([ rnet, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(len(CLASSES), activation='softmax') ]) model.compile( optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'] ) return model
def create_base_model(base_model_name, pretrained=True, IMAGE_SIZE=[300, 300]): if pretrained is False: weights = None else: weights = "imagenet" if base_model_name == 'B0': base = efn.EfficientNetB0(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B1': base = efn.EfficientNetB1(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B2': base = efn.EfficientNetB2(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B3': base = efn.EfficientNetB3(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B4': base = efn.EfficientNetB4(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B5': base = efn.EfficientNetB5(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B6': base = efn.EfficientNetB6(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B7': base = efn.EfficientNetB7(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) base = remove_dropout(base) base.trainable = True return base
def get_model(): with strategy.scope(): base_model = efn.EfficientNetB7(weights=pre_trained, include_top=False, pooling='avg', input_shape=(img_size, img_size, 3)) x = base_model.output predictions = Dense(nb_classes, activation=dense_activation)(x) model = Model(inputs=base_model.input, outputs=predictions) if label_smoothing_rate: print('启用label_smoothing') my_loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=label_smoothing_rate) elif bool_focal_loss: my_loss = tfa.losses.SigmoidFocalCrossEntropy(reduction=tf.keras.losses.Reduction.AUTO) else: my_loss='categorical_crossentropy' model.compile(optimizer=tf.keras.optimizers.Adam(lr_if_without_scheduler), loss=my_loss, metrics=my_metrics ) return model
def __get_backbone(): if FLAGS.model == 'DenseNet121': return tf.keras.applications.DenseNet121(include_top=False, weights='imagenet', input_shape=(FLAGS.img_width, FLAGS.img_height, 3)) elif FLAGS.model == 'DenseNet169': return tf.keras.applications.DenseNet169(include_top=False, weights='imagenet', input_shape=(FLAGS.img_width, FLAGS.img_height, 3)) elif FLAGS.model == 'DenseNet201': return tf.keras.applications.DenseNet201(include_top=False, weights='imagenet', input_shape=(FLAGS.img_width, FLAGS.img_height, 3)) elif FLAGS.model == 'EfficentNetB7': return efficientnet.EfficientNetB7(include_top=False, weights='noisy-student', input_shape=(FLAGS.img_width, FLAGS.img_height, 3))
def create_model(model, img_height, img_width, classes): if model == 'densenet': model = tf.keras.Sequential( [DenseNet121(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(classes, activation='softmax')] ) elif model == 'efficientnetB7': model = tf.keras.Sequential( [efn.EfficientNetB7(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(classes, activation='softmax')] ) elif model == 'efficientnetB6': model = tf.keras.Sequential( [efn.EfficientNetB6(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(classes, activation='softmax')] ) model.summary() weights = model.get_weights() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model, weights
alpha = baseLR * ((1 - (ite / float(maxEpochs * step_each_epoch)))**power) # return the new learning rate return alpha def loss_(y_true, y_pred): return y_pred labels = Input(name='labels', shape=(max_labels), dtype='int32') label_length = Input(name='input_length', shape=(1), dtype='int32') logit_length = Input(name='label_length', shape=(1), dtype='int32') print('Loading backbone model.....') b7 = ef.EfficientNetB7(include_top=False, weights=None, input_shape=(32, 280, 3)) print('Loading Done!') # block4a_expand_activation y = keras.layers.MaxPool2D(pool_size=(4, 1))( b7.get_layer(name='block4a_expand_bn').output) y = keras.layers.Activation('sigmoid')(y) y = EncoderModel(num_layers=num_layers, d_model=480)(y, training) print(y.shape) y = layers.Dropout(dropout)(y, training=training) y = layers.Dense(num_classes, activation='softmax', name='FC_1')(y) loss = layers.Lambda(lambda x: ctc_loss(x[0], x[1], x[2], x[3]))( [labels, y, label_length, logit_length])
lr = (lr_max - lr_min) * lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs) + lr_min return lr return lrfn # ### Load Model into TPU # In[9]: # with strategy.scope(): model = tf.keras.Sequential([ efn.EfficientNetB7( input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), weights='imagenet', include_top=False ), L.GlobalAveragePooling2D(), L.Dense(train_labels.shape[1], activation='softmax') ]) model.compile( optimizer='adam', loss = 'categorical_crossentropy', metrics=['categorical_accuracy'] ) model.summary() # ### Start training
shutil.copy(test_image_pneumonia, 'working/test/Pneumonia') for image in testing_images_normal: test_image_normal = os.path.join(testing_data_path, str(image)) shutil.copy(test_image_normal, 'working/test/Normal') # Model configuration batch_size = 64 img_width, img_height, img_num_channels = 224, 224, 3 no_epochs = 15 verbosity = 1 input_shape = (img_width, img_height, img_num_channels) #Creating an EffNet model model_B7 = efn.EfficientNetB7(weights='imagenet', input_shape=input_shape, include_top=False) # Function to build, compile and train the model train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, rotation_range=0.2, width_shift_range=0.2, height_shift_range=0.2, zoom_range=0.2, horizontal_flip=True, vertical_flip=True, fill_mode='nearest') train_generator = train_datagen.flow_from_directory('working/train',
bsize=BATCH_SIZE, decode_fn=decoder, repeat=False, shuffle=False, augment=False) try: n_labels = train_labels.shape[1] except: n_labels = 1 with strategy.scope(): if CFG.model == "effb7": model = tf.keras.Sequential([ efn.EfficientNetB7(input_shape=(600, 600, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(n_labels, activation='sigmoid') ]) if CFG.model == "vit": model = vit.vit_b16(image_size=384, activation='sigmoid', pretrained=True, include_top=True, pretrained_top=False, classes=19) if CFG.model == "resnext": model = tf.keras.Sequential([ resnext.ResNeXt101(include_top=False,
# for i in x_train: # plt.imshow(i) # plt.show() #make sure hey are RGB #y = x_train[0] # y[:,:,0] = 0 # plt.imshow(y) #loading the model #include_top = false will remove the last layer because i have 2 categories only, not 1000 as imagenet #predefine image shape Image_size = (224, 224, 3) efnet = efc.EfficientNetB7(input_shape=Image_size, weights='imagenet', include_top=False) x = efnet.input y = efnet.output # a vector with a size of 2 for each image (2 probabilities) y = Flatten(name='flatten')(y) #normaliza the input to the neural network to speed up training y = BatchNormalization()(y) y = Dense(32, name='FC1')(y) y = LeakyReLU()(y) y = Dropout(0.2)(y) #y = Dense(512, activation='relu', name = 'FC2')(y) #y = Dropout(0.5)(y) y = Dense(2, activation='softmax', name='prediction')(y) model = Model(inputs=x, outputs=y) model.compile(loss='binary_crossentropy',
'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower', # 90 - 99 'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose' ] # 100 - 102 with strategy.scope(): enet = efn.EfficientNetB7(input_shape=(512, 512, 3), weights='noisy-student', include_top=False) model = tf.keras.Sequential([ enet, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(len(CLASSES), activation='softmax') ]) model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy']) model.summary() LR_START = 0.00001 LR_MAX = 0.00005 * strategy.num_replicas_in_sync
def get_model(arch="b3", pretrained="imagenet", image_size=(128, 128, 3)): image_input = tf.keras.layers.Input(shape=image_size, dtype='float32', name='image_input') if arch.startswith("b2"): base_model = efn.EfficientNetB2(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b3"): base_model = efn.EfficientNetB3(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b4"): base_model = efn.EfficientNetB4(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b5"): base_model = efn.EfficientNetB5(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b6"): base_model = efn.EfficientNetB6(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b7"): base_model = efn.EfficientNetB7(weights=pretrained, input_shape=image_size, include_top=False) else: raise ValueError("Unknown arch!") base_model.trainable = True tmp = base_model(image_input) hidden_dim = base_model.output_shape[-1] tmp = tf.keras.layers.GlobalAveragePooling2D()(tmp) tmp = tf.keras.layers.Dropout(0.5)(tmp) if arch.endswith("g"): prediction_0 = tf.keras.layers.Dense(CLASS_COUNTS[0], activation='softmax', name="root", dtype='float32')(SELayer( hidden_dim, 8)(tmp)) prediction_1 = tf.keras.layers.Dense(CLASS_COUNTS[1], activation='softmax', name="vowel", dtype='float32')(SELayer( hidden_dim, 8)(tmp)) prediction_2 = tf.keras.layers.Dense(CLASS_COUNTS[2], activation='softmax', name="consonant", dtype='float32')(SELayer( hidden_dim, 8)(tmp)) else: prediction_0 = tf.keras.layers.Dense(CLASS_COUNTS[0], activation='softmax', name="root", dtype='float32')(tmp) prediction_1 = tf.keras.layers.Dense(CLASS_COUNTS[1], activation='softmax', name="vowel", dtype='float32')(tmp) prediction_2 = tf.keras.layers.Dense(CLASS_COUNTS[2], activation='softmax', name="consonant", dtype='float32')(tmp) prediction = tf.keras.layers.Concatenate(axis=-1)( [prediction_0, prediction_1, prediction_2]) return tf.keras.Model(image_input, prediction)
dn201, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(len(CLASSES), activation='softmax') ]) model1.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy']) model1.summary() with strategy.scope(): enb7 = efn.EfficientNetB7(weights='noisy-student', include_top=False, input_shape=[*IMAGE_SIZE, 3]) enb7.trainable = True # Full Training model2 = tf.keras.Sequential([ enb7, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(len(CLASSES), activation='softmax') ]) model2.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])