def create_model(): base_model = EfficientNetB3(weights='imagenet', include_top=False) x = base_model.output x = layers.GlobalAveragePooling2D()(x) x = layers.Dense(256, activation='relu')(x) x = layers.Dropout(0.25)(x) predictions = layers.Dense(3, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def efficientnet(b, weights='imagenet', include_top=False, input_shape=(None, None, 3)): """Loads the appropriate EfficientNet model with weights :param b: The size of the EfficientNet model. :type b: int :param weights: The pretrained weights to load. Defaults to iamgenet. :type weights: str :param include_top: Include the pretrained softmax layer. Defaults to False :type include_top: bool :param input_shape: Shape of input images. Defaults to no hight or width, 3 channels. :type input_shape: Tuple :return: EfficientNet Model. :rtype: tf.keras.models.Model """ if b == 0: return EfficientNetB0(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 1: return EfficientNetB1(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 2: return EfficientNetB2(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 3: return EfficientNetB3(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 4: return EfficientNetB4(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 5: return EfficientNetB5(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 6: return EfficientNetB6(weights=weights, include_top=include_top, input_shape=input_shape) elif b == 7: return EfficientNetB7(weights=weights, include_top=include_top, input_shape=input_shape) else: raise Exception("Invalid size for EfficientNet")
def load_effnet(shape, efftype = 'B0', weights = 'imagenet', trainable = False): if efftype == 'B0': backbone = EfficientNetB0(weights = weights, include_top = False, input_shape = shape) if efftype == 'B3': backbone = EfficientNetB3(weights = weights, include_top = False, input_shape = shape) if efftype == 'B5': backbone = EfficientNetB5(weights = weights, include_top = False, input_shape = shape) for layer in backbone.layers: layer.trainable = trainable return backbone
brightness_range=(0.9, 1.1)) train_generator = traindatagen.flow_from_dataframe(dataframe=data_train, x_col='file_path', y_col='class', target_size=(300, 300), batch_size=batch_size) val_generator = valdatagen.flow_from_dataframe(dataframe=data_test, x_col='file_path', y_col='class', target_size=(300, 300), batch_size=batch_size) esiverkko = EfficientNetB3(include_top=False, weights='imagenet', input_shape=(300, 300, 3)) verkko = models.Sequential() verkko.add(esiverkko) verkko.add(layers.GlobalMaxPooling2D()) verkko.add(layers.Dropout(rate=0.2)) # verkko.add(layers.Dense(1000, activation = 'relu', kernel_initializer = 'he_uniform')) # for InceptionV3 # verkko.add(layers.Dropout(rate = 0.70)) # for InceptionV3 # verkko.add(layers.BatchNormalization()) # for InceptionV3 verkko.add( layers.Dense(17, activation='softmax', kernel_initializer='he_uniform')) verkko.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=1e-5),
VALID_STEP_PER_EPOCH = tf.math.ceil(valid_images_len / BATCH_SIZE).numpy() # 기본 Dataset 만들기 # Cutmix 포함 Dataset 만들기 train_ds = make_tf_dataset(train_images, train_labels) train_ds = train_ds.repeat().batch(BATCH_SIZE).map(cutmix).prefetch( tf.data.experimental.AUTOTUNE) # tf.data.experimental.AUTOTUNE valid_ds = make_tf_dataset(valid_images, valid_labels) valid_ds = valid_ds.repeat().batch(BATCH_SIZE).prefetch( tf.data.experimental.AUTOTUNE) base_model = EfficientNetB3(input_shape=(IMG_SIZE, IMG_SIZE, 3), weights="imagenet", include_top=False) avg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output) output = tf.keras.layers.Dense(train_labels_len, activation="softmax")(avg) model = tf.keras.Model(inputs=base_model.input, outputs=output) #model = multi_gpu_model(model, gpus=3) for layer in base_model.layers: layer.trainable = True def build_lrfn(lr_start=0.00001, lr_max=0.00005, lr_min=0.00001, lr_rampup_epochs=5, lr_sustain_epochs=0,
action="store_true") args = parser.parse_args() image_byte = tf.io.read_file(args.image) img = tf.io.decode_jpeg(image_byte) model = tf.keras.models.load_model(args.model, compile=False) img_norm = tf.cast(img, tf.float32) img_norm = img_norm / 255. lab_img = tfio.experimental.color.rgb_to_lab(img_norm) l_img = lab_img[:, :, 0] if args.pretrained_model: effnet_model = EfficientNetB3(weights='imagenet', include_top=True) img_resized = tf.image.resize(img, (300, 300)) img_resized = img_resized / 255. embeddings = effnet_model.predict(tf.expand_dims(img_resized, axis=0)) ab_pred = model.predict([tf.expand_dims(l_img, axis=0), embeddings]) else: ab_pred = model.predict(tf.expand_dims(l_img, axis=0)) lab_img_recon = np.zeros((256, 256, 3)) lab_img_recon[:, :, 0] = l_img lab_img_recon[:, :, 1:] = np.squeeze(ab_pred * 128, axis=0) utils.visualize(ori_img=img, L=l_img, pred_color=tf.cast(
def build_model(base_model, input_shape, metrics, loss, loss_weights, **kwargs): if base_model == 'resnet50': from tensorflow.keras.applications import ResNet50 base_model = ResNet50(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'densenet121': from tensorflow.keras.applications import DenseNet121 base_model = DenseNet121(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'efficientnetb0': from efficientnet.tfkeras import EfficientNetB0 base_model = EfficientNetB0(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'efficientnetb1': from efficientnet.tfkeras import EfficientNetB1 base_model = EfficientNetB1(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'efficientnetb2': from efficientnet.tfkeras import EfficientNetB2 base_model = EfficientNetB2(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'efficientnetb3': from efficientnet.tfkeras import EfficientNetB3 base_model = EfficientNetB3(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'efficientnetb4': from efficientnet.tfkeras import EfficientNetB4 base_model = EfficientNetB4(include_top=False, weights='imagenet', input_shape=input_shape) if base_model == 'efficientnetb5': from efficientnet.tfkeras import EfficientNetB5 base_model = EfficientNetB5(include_top=False, weights='imagenet', input_shape=input_shape) x_in = Input(shape=input_shape) x = Conv2D(3, (3, 3), padding='same')(x_in) x = base_model(x) x = GlobalAvgPool2D()(x) x = BatchNormalization()(x) x = Dropout(0.5)(x) out_grapheme = Dense(168, activation='softmax', name='root')(x) out_vowel = Dense(11, activation='softmax', name='vowel')(x) out_consonant = Dense(7, activation='softmax', name='consonant')(x) model = Model(inputs=x_in, outputs=[out_grapheme, out_vowel, out_consonant]) model.compile(Adam(lr=0.0001), metrics=metrics, loss=loss, loss_weights=loss_weights) return model
headModel = Dense(51, activation='softmax')(headModel) elif network == "EfficientNetB0": print(network) train, test, lb2, labelsTest = preprocessing_EfficcientNet() baseModel = EfficientNetB0(weights=pretraining, include_top=False, input_tensor=Input(shape=(224, 224, 3)), pooling="avg") headModel = baseModel.output headModel = Dropout(0.2)(headModel) headModel = Dense(51, activation='softmax')(headModel) else: print(network) train, test, lb2, labelsTest = preprocessing_EfficcientNet() baseModel = EfficientNetB3(weights=pretraining, include_top=False, input_tensor=Input(shape=(224, 224, 3)), pooling="avg") headModel = baseModel.output headModel = Dropout(0.2)(headModel) headModel = Dense(51, activation='softmax')(headModel) corrida = str(corrida) print("corrida %s" % (corrida)) model = Model(inputs=baseModel.input, outputs=headModel) for layer in model.layers: layer.trainable = True print("[INFO] compiling model...") opt = SGD() model.compile(