def base_net(weights): if weights == 'imagenet': base_model = MobileNetV2(include_top=False, weights=None, input_shape=(300, 300, 3)) else: base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=(300, 300, 3)) # Up to expanded_conv_project_BN (Batch first_block = base_model.layers[:10] # Conv_1 (Conv2D) to end end_block = base_model.layers[150:] # 16 conv_block base = base_model.layers[10:150] block_list = [] block = [] start = 1 for layer in base: if 'add' not in layer.name: block_num = int(layer.name.split('_')[1]) if start != block_num: start = block_num block_list.append(block) # block 초기화 block = [] block.append(layer) else: block.append(layer) # add block_16 block_list.append(block) # add first_block and end_block block_list.insert(0, first_block) block_list.append(end_block) # 4th block # (None, 300, 300, 3) -> (None, 19, 19, 96) # Up to block_12_project_BN (BatchNorma conv4_layer = block_list[:13] # 7th block # (None, 19, 19, 96) -> (None, 10, 10, 1280) # Up to end conv7_layer = block_list[13:] mobile_v2_conv4 = create_conv4_layer(conv4_layer) mobile_v2_conv7 = create_conv7_layer(conv7_layer) return mobile_v2_conv4, mobile_v2_conv7
def transfer_MobileNetV2(optimizer=SGD, lr=0.001, momentum=0.95, pretrained_weights='imagenet', freeze=False, dropout_frac=0.5): # build the model input = Input((IMAGE_SIZE, IMAGE_SIZE, 3)) pretrained = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, weights=pretrained_weights) # Freeze the feature-extracting layers of the pretrained model if freeze: for layer in pretrained.layers: layer.trainable = False output = pretrained(input) output = GlobalAveragePooling2D()(output) output = Dropout(dropout_frac)(output) output = Dense(64, activation='sigmoid')(output) output = Dense(1, activation='sigmoid')(output) model = Model(input, output) # compile and return the model if momentum is not None: model.compile(optimizer(lr=lr, momentum=momentum), loss='binary_crossentropy', metrics=['accuracy']) else: model.compile(optimizer(lr=lr), loss='binary_crossentropy', metrics=['accuracy']) return model
def buildKerasAppModel(img_height, img_width, img_channl, num_classes, num_GPU): FreezeNum = 0 # inputs = Input(shape = (img_height, img_width, img_channl)) # AppModel = MobileNetV2(include_top=False, pooling='avg', weights='imagenet', input_shape=inputs) AppModel = MobileNetV2(include_top=False, pooling='avg', weights='imagenet') # Freeze 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='softmax', name='softmax')(x) model = Model(inputs=AppModel.input, outputs=output_layer) model.summary() if (num_GPU > 1): model = multi_gpu_model(model, gpus=num_GPU) #categorical_crossentropy , sparse_categorical_crossentropy model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-5), metrics=['accuracy']) return model
def create_model(trainable=True): model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA, weights="imagenet") for layer in model.layers: layer.trainable = trainable block1 = model.get_layer("block_5_add").output block2 = model.get_layer("block_12_add").output block3 = model.get_layer("block_15_add").output blocks = [block2, block1] x = block3 for block in blocks: x = UpSampling2D()(x) x = Conv2D(256, kernel_size=3, padding="same", strides=1)(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = Concatenate()([x, block]) x = Conv2D(256, kernel_size=3, padding="same", strides=1)(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = Conv2D(1, kernel_size=1, activation="sigmoid")(x) return Model(inputs=model.input, outputs=x)
def get_model(num_classes): input_tensor = Input( shape=(224, 224, 3)) # this assumes K.image_data_format() == 'channels_last' # create the base pre-trained model base_model = MobileNetV2(input_tensor=input_tensor, weights='imagenet', include_top=False) # for layer in base_model.layers: # layer.trainable = False x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')( x ) # we add dense layers so that the model can learn more complex functions and classify for better results. x = Dense(1024, activation='relu')(x) # dense layer 2 x = Dense(512, activation='relu')(x) # dense layer 3 x = Dense(num_classes, activation='softmax')(x) # final layer with softmax activation updatedModel = Model(base_model.input, x) return updatedModel
def buildMobileNetV2Model(img_height, img_width, img_channl, num_classes, num_GPU): inputs = Input(shape=(img_height, img_width, img_channl)) # -------------------------------------------- StopNum = 0 AppModel = MobileNetV2(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 baseline_model(): input_1 = Input(shape=(None, None, 3)) input_2 = Input(shape=(None, None, 3)) base_model = MobileNetV2(weights="imagenet", include_top=False) x1 = base_model(input_1) x2 = base_model(input_2) x1 = Concatenate(axis=-1)([GlobalMaxPool2D()(x1), GlobalAvgPool2D()(x1)]) x2 = Concatenate(axis=-1)([GlobalMaxPool2D()(x2), GlobalAvgPool2D()(x2)]) x3 = Subtract()([x1, x2]) x3 = Multiply()([x3, x3]) x = Multiply()([x1, x2]) x = Concatenate(axis=-1)([x, x3]) x = Dense(100, activation="relu")(x) x = Dropout(0.01)(x) out = Dense(1, activation="sigmoid")(x) model = Model([input_1, input_2], out) model.compile(loss="binary_crossentropy", metrics=[acc], optimizer=Adam(0.00001)) model.summary() return model
def create_model(image_shape=(224, 224, 3), restart_checkpoint=None, backbone='mobilnetv2', feature_len=128, freeze=False): """ Creates an image encoder. Args: image_shape: input image shape (use [None, None] for resizable network) restart_checkpoint: snapshot to be restored backbone: the backbone CNN (one of mobilenetv2, densent121, custom) feature_len: the length of the additional feature layer freeze: freeze the backbone """ input_img = Input(shape=image_shape) # add the backbone backbone_name = backbone if backbone_name == 'densenet121': print('Using DenseNet121 backbone.') backbone = DenseNet121(input_tensor=input_img, include_top=False) backbone.layers.pop() if freeze: for layer in backbone.layers: layer.trainable = False backbone = backbone.output elif backbone_name == 'mobilenetv2': print('Using MobileNetV2 backbone.') backbone = MobileNetV2(input_tensor=input_img, include_top=False) backbone.layers.pop() if freeze: for layer in backbone.layers: layer.trainable = False backbone = backbone.output elif backbone_name == 'custom': backbone = custom_backbone(input_tensor=input_img) else: raise Exception('Unknown backbone: {}'.format(backbone_name)) # add the head layers gmax = GlobalMaxPool2D()(backbone) gavg = GlobalAvgPool2D()(backbone) gmul = Multiply()([gmax, gavg]) ggavg = Average()([gmax, gavg]) backbone = Concatenate()([gmax, gavg, gmul, ggavg]) backbone = BatchNormalization()(backbone) backbone = Dense(feature_len)(backbone) backbone = Activation('sigmoid')(backbone) encoder = Model(input_img, backbone) if restart_checkpoint: print('Loading weights from {}'.format(restart_checkpoint)) encoder.load_weights(restart_checkpoint, by_name=True, skip_mismatch=True) return encoder
def create_model(trainable=False): model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA, weights="imagenet") for layer in model.layers: layer.trainable = trainable block = model.get_layer("block_16_project_BN").output x = Conv2D(112, padding="same", kernel_size=3, strides=1, activation="relu")(block) x = Conv2D(112, padding="same", kernel_size=3, strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = Conv2D(5, padding="same", kernel_size=1, activation="sigmoid")(x) model = Model(inputs=model.input, outputs=x) # divide by 2 since d/dweight learning_rate * weight^2 = 2 * learning_rate * weight # see https://arxiv.org/pdf/1711.05101.pdf regularizer = l2(WEIGHT_DECAY / 2) for weight in model.trainable_weights: with tf.keras.backend.name_scope("weight_regularizer"): model.add_loss(regularizer(weight)) return model
def yolo2lite_mobilenetv2_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V2 Lite MobileNetV2 model CNN body in Keras.""" mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # mobilenetv2.output : 13 x 13 x 1280 # block_13_expand_relu(layers[119]) : 26 x 26 x (576*alpha) conv_head1 = compose( Depthwise_Separable_Conv2D_BN_Leaky(1280, (3, 3)), Depthwise_Separable_Conv2D_BN_Leaky(1280, (3, 3)))(mobilenetv2.output) # block_13_expand_relu output shape: 26 x 26 x (576*alpha) block_13_expand_relu = mobilenetv2.layers[119].output conv_head2 = DarknetConv2D_BN_Leaky(int(64*alpha), (1, 1))(block_13_expand_relu) # TODO: Allow Keras Lambda to use func arguments for output_shape? conv_head2_reshaped = Lambda( space_to_depth_x2, output_shape=space_to_depth_x2_output_shape, name='space_to_depth')(conv_head2) x = Concatenate()([conv_head2_reshaped, conv_head1]) x = Depthwise_Separable_Conv2D_BN_Leaky(1280, (3, 3))(x) x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x) return Model(inputs, x)
def tiny_yolo3_mobilenetv2_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create Tiny YOLO_v3 MobileNetV2 model CNN body in keras.''' mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # out_relu: 13 x 13 x 1280 # block_13_expand_relu: 26 x 26 x (576*alpha) # block_6_expand_relu: 52 x 52 x (192*alpha) x1 = mobilenetv2.get_layer('block_13_expand_relu').output x2 = mobilenetv2.get_layer('out_relu').output x2 = DarknetConv2D_BN_Leaky(int(576 * alpha), (1, 1))(x2) y1 = compose( DarknetConv2D_BN_Leaky(int(1280 * alpha), (3, 3)), #Depthwise_Separable_Conv2D_BN_Leaky(filters=int(1280*alpha), kernel_size=(3, 3), block_id_str='17'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2) x2 = compose(DarknetConv2D_BN_Leaky(int(288 * alpha), (1, 1)), UpSampling2D(2))(x2) y2 = compose( Concatenate(), DarknetConv2D_BN_Leaky(int(576 * alpha), (3, 3)), #Depthwise_Separable_Conv2D_BN_Leaky(filters=int(576*alpha), kernel_size=(3, 3), block_id_str='18'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, x1]) return Model(inputs, [y1, y2])
def build_COVIDNet(num_classes=3, flatten=True, checkpoint='',args=None): if args.model == 'resnet50v2': base_model = ResNet50V2(include_top=False, weights='imagenet', input_shape=(args.img_size, args.img_size, 3)) x = base_model.output if args.model =='mobilenetv2': base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=(args.img_size, args.img_size, 3)) x = base_model.output if args.model == 'custom': base_model = covidnet(input_tensor=None, input_shape=(args.img_size, args.img_size, 3), classes=3) x = base_model.output if args.model == 'EfficientNet': import efficientnet.tfkeras as efn base_model = efn.EfficientNetB4(weights=None, include_top=True, input_shape=(args.img_size, args.img_size, 3), classes=3) x = base_model.output if flatten: x = Flatten()(x) else: # x = GlobalAveragePooling2D()(x) x = GlobalMaxPool2D()(x) if args.datapipeline == 'covidx': x = Dense(1024, activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001))(x) x = Dense(256, activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001))(x) # x = Dropout(0.2)(x) predictions = Dense(num_classes, activation='softmax',name=f'FC_{num_classes}')(x) model = Model(inputs=base_model.input, outputs=predictions) if len(checkpoint): model.load_weights(checkpoint) return model
def define_model(num): if num == 0: convbase = MobileNetV2( weights='imagenet', include_top=False, input_shape=(150, 150, 3)) print("Creating MobileNet layer!") elif num == 1: convbase = ResNet50V2(weights='imagenet', include_top=False, input_shape=(150, 150, 3)) print("Creating ResNet layer!") elif num == 2: convbase = VGG16( weights='imagenet', include_top=False, input_shape=(150, 150, 3)) print("Creating VGG16 layer!") else: print("Out of range! Something's wrong") convbase.trainable = False # Preserve weights of pre-trained conv layers model = tf.keras.Sequential() model.add(convbase) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(10, activation='softmax')) # compile model model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=2e-5), metrics=['accuracy']) return model
def make_feature_extractor(): feature_extractor = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) last_layer = K.layers.GlobalAveragePooling2D()(feature_extractor.output) convolutional_base = K.models.Model(feature_extractor.input, last_layer) return convolutional_base
def tiny_yolo4_mobilenetv2_body(inputs, num_anchors, num_classes, alpha=1.0, use_spp=True): '''Create Tiny YOLO_v4 MobileNetV2 model CNN body in keras.''' mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenetv2.layers))) # input: 416 x 416 x 3 # out_relu: 13 x 13 x 1280 # block_13_expand_relu: 26 x 26 x (576*alpha) # block_6_expand_relu: 52 x 52 x (192*alpha) # f1 :13 x 13 x 1280 f1 = mobilenetv2.get_layer('out_relu').output # f2: 26 x 26 x (576*alpha) f2 = mobilenetv2.get_layer('block_13_expand_relu').output f1_channel_num = int(1280 * alpha) f2_channel_num = int(576 * alpha) #f1_channel_num = 1024 #f2_channel_num = 512 y1, y2 = tiny_yolo4_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes, use_spp) return Model(inputs, [y1, y2])
def create_model(trainable=False): model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA, weights="imagenet") for layer in model.layers: layer.trainable = trainable #block0 = model.get_layer("Conv1_relu").output #block1 = model.get_layer("block_2_add").output #block2 = model.get_layer("block_5_add").output block3 = model.get_layer("block_12_add").output block4 = model.get_layer("block_15_add").output blocks = [block3]#, block2, block1, block0] x = block4 for block in blocks: x = UpSampling2D()(x) x = Concatenate()([x, block]) x = Conv2D(256, kernel_size=3, padding="same", strides=1, activation="relu")(x) x = Conv2D(256, kernel_size=3, padding="same", strides=1, activation="relu")(x) x = Conv2D(3, kernel_size=1, activation=lambda l : tf.concat([l[...,:2], tf.sigmoid(l[...,2:])], axis=-1))(x) x = Lambda(clip)(x) return Model(inputs=model.input, outputs=x)
def trainMobilenetFullyTrainable(): # Transfer learning Mobilenet model with no layers trainable from tensorflow.keras.optimizers import Adam from tensorflow.keras import layers from tensorflow.keras import Model from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 # Base model is a MobileNet without pretrained weights base_model = MobileNetV2(input_shape=(32, 32, 3), weights=None, classes=num_classes, include_top=True) # Define model using functional method, input -> resize -> MobileNet input_layer = keras.layers.Input(shape=(28, 28, 3)) x = keras.layers.Layer()(input_layer) x = layers.experimental.preprocessing.Resizing(32, 32, interpolation="bilinear", name="the_resize_layer")(x) output = base_model(x) model = Model(inputs=input_layer, outputs=output) # Compile model model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) # Train model with samples flowing from ImageDataGenerator model.fit_generator(datagen.flow(x_train, y_train_onehot, batch_size=10), steps_per_epoch=len(x_train) // 10, epochs=epochs, verbose=1) return model
def set_model(self, model_name="inception_v3"): """ # This is a function that composes a model, and proceeds to compile. # [Reference] - https://www.tensorflow.org/api_docs/python/tf/keras/applications/inception_v3 """ if model_name == "inception_v3": self.model = InceptionV3(weights="imagenet", include_top=False) elif model_name == "mobilenet_v2": self.model = MobileNetV2(weights="imagenet", include_top=False) self.model_name = model_name x = self.model.output x = GlobalAveragePooling2D()(x) x = Dense(128, activation="relu")(x) x = Dropout(0.2)(x) pred = Dense( len(self.class_list), kernel_regularizer=regularizers.l2(0.005), activation="softmax", )(x) self.model = Model(inputs=self.model.input, outputs=pred) self.model.compile( optimizer=SGD(lr=0.0001, momentum=0.9), loss="categorical_crossentropy", metrics=["accuracy"], ) return 1
def define_model(): model_name = 'miczi_v12' # Inputs input_img_seq = Input(shape=(224, 224, 3)) input_command = Input(shape=(6, )) # encoded = noise.GaussianNoise(0.2)(input_sh) # Image branch cnn_model = MobileNetV2(input_shape=(224, 224, 3), include_top=False, pooling='avg', weights='imagenet') freeze_layers(cnn_model) _ = Dense(512)(cnn_model.output) _ = Dropout(0.4)(_) cnn_output = Dense(128)(_) # Merge two branches: features extracted by CNN with command _ = concatenate([cnn_output, input_command]) _ = Dense(128, activation='relu')(_) _ = Dense(128, activation='relu')(_) outputs = Dense(14, activation='linear')(_) # Combine inputs, outputs model = Model(inputs=[cnn_model.input, input_command], outputs=outputs) model.compile(loss=LOSS_FUNCTION_NAME, optimizer='adam', metrics=['accuracy', 'mae']) print(model_name, model.summary()) # plot_model(model, show_shapes=True, to_file=model_name +'_plot.png') return model, model_name
def create_model(trainable=True): model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA, weights=None) # to freeze layers for layer in model.layers: layer.trainable = trainable x = model.layers[-1].output x = MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)(x) x = Conv2D(256, kernel_size=1, padding='valid', use_bias=False, activation=None, name='Custom_Conv2D_1')(x) x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Custom_BN_1')(x) x = ReLU(6., name='Custom_ReLU_1')(x) x = Conv2D(4, kernel_size=3, padding='valid', use_bias=False, activation=None, name='Custom_Conv2D_2')(x) x = Reshape((4, ))(x) return Model(inputs=model.input, outputs=x)
def yolo4lite_mobilenetv2_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create YOLO_v4 Lite MobileNetV2 model CNN body in keras.''' mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenetv2.layers))) # input: 416 x 416 x 3 # out_relu: 13 x 13 x 1280 # block_13_expand_relu: 26 x 26 x (576*alpha) # block_6_expand_relu: 52 x 52 x (192*alpha) # f1 :13 x 13 x 1280 f1 = mobilenetv2.get_layer('out_relu').output # f2: 26 x 26 x (576*alpha) f2 = mobilenetv2.get_layer('block_13_expand_relu').output # f3 : 52 x 52 x (192*alpha) f3 = mobilenetv2.get_layer('block_6_expand_relu').output f1_channel_num = int(1280 * alpha) f2_channel_num = int(576 * alpha) f3_channel_num = int(192 * alpha) #f1_channel_num = 1024 #f2_channel_num = 512 #f3_channel_num = 256 y1, y2, y3 = yolo4lite_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_model(hyper_params): """Generating rpn model for given hyper params. inputs: hyper_params = dictionary outputs: rpn_model = tf.keras.model feature_extractor = feature extractor layer from the base model """ img_size = hyper_params["img_size"] base_model = MobileNetV2(include_top=False, input_shape=(img_size, img_size, 3)) feature_extractor = base_model.get_layer("block_13_expand_relu") output = Conv2D(512, (3, 3), activation="relu", padding="same", name="rpn_conv")(feature_extractor.output) rpn_cls_output = Conv2D(hyper_params["anchor_count"], (1, 1), activation="sigmoid", name="rpn_cls")(output) rpn_reg_output = Conv2D(hyper_params["anchor_count"] * 4, (1, 1), activation="linear", name="rpn_reg")(output) rpn_model = Model(inputs=base_model.input, outputs=[rpn_reg_output, rpn_cls_output]) return rpn_model, feature_extractor
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 get_classifier() -> MobileNetV2: """Load MobileNetv2 as classifier model. Returns: MobileNetV2: Classifier. """ mobile = MobileNetV2() return mobile
def __init__(self): super(SimpsonsNet, self).__init__() self.base_model = MobileNetV2(input_shape=(224, 224, 3), weights='imagenet', include_top=False) self.base_model.trainable = True self.pool = GlobalAveragePooling2D() self.fc2 = Dense(18, activation='softmax')
def _mobilenet_2(): net = MobileNetV2(include_top=False) layer_names = [ 'block_6_expand_relu', 'block_13_expand_relu', 'block_16_expand_relu' ] output = [net.get_layer(n).output for n in layer_names] backbone = tf.keras.Model(net.input, output, name=name) return backbone
def mobilenetv2(input_size): input_tensor = Input(shape=(input_size, input_size, 3)) base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=(input_size, input_size, 3)) model = create_model(base_model, input_tensor) return model
def pred_img(): img_to_pred = image.load_img(img_dir, target_size=(224, 224)) cnn = MobileNetV2(include_top=True, weights='imagenet') img_arr = image.img_to_array(img_to_pred) img_arr = np.expand_dims(img_arr, axis=0) img_processed = preprocess_input(img_arr) global pred pred = cnn.predict(img_processed) disp_pred()
def create_model(self, input_shape=(224, 224, 3)): inputs = tf.keras.layers.Input(input_shape) base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=input_shape) x = base_model(inputs) outputs = tf.keras.layers.GlobalAveragePooling2D()(x) print("Creating model complete") return tf.keras.Model(inputs, outputs)
def setup_mobilenet_v2_model(): base_model = MobileNetV2(include_top=False, weights='imagenet', pooling='avg', input_shape=(IMG_SIZE, IMG_SIZE, 3)) x = base_model.output x = tf.keras.layers.Dense(info.features['label'].num_classes, activation="softmax")(x) model_functional = tf.keras.Model(inputs=base_model.input, outputs=x) return model_functional