예제 #1
0
def models_efn_base(input_shape, lr=1e-4):
    '''
    EfficientNetB6 +batchnorm
    EfficientNet models expect their inputs to be
    float tensors of pixels with values in the [0-255] range.
    '''

    base_model = EfficientNetB6(weights='imagenet',
                                include_top=False,
                                input_shape=input_shape,
                                pooling='avg')

    # base_model.trainable = False

    x = base_model.output
    # let's add a fully-connected layer
    x = Dense(256, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.25)(x)
    # and a logistic layer -- let's say we have 10 classes
    predictions = Dense(CLASS_NUM, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=base_model.input,
                  outputs=predictions,
                  name=f'EfficientNetB6')
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizers.Adam(lr),
                  metrics=["accuracy"])

    return model, base_model
예제 #2
0
def build_model(num_classes, model="B7"):
    inputs = layers.Input(shape=(WIDTH, HEIGHT, 3))
    if model == "B7":
        model = EfficientNetB7(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B6":
        model = EfficientNetB6(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B5":
        model = EfficientNetB5(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B4":
        model = EfficientNetB4(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B3":
        model = EfficientNetB3(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B2":
        model = EfficientNetB2(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B1":
        model = EfficientNetB1(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B0":
        model = EfficientNetB0(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    # Freeze the pretrained weights
    model.trainable = False

    # Rebuild top
    x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
    x = layers.BatchNormalization()(x)

    top_dropout_rate = 0.2
    x = layers.Dropout(top_dropout_rate, name="top_dropout", seed=SEED)(x)
    outputs = layers.Dense(num_classes,
                           activation="softmax",
                           name="predictions")(x)

    # Compile
    model = tf.keras.Model(inputs, outputs, name="EfficientNet")
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)
    model.compile(
        optimizer=optimizer,
        loss="sparse_categorical_crossentropy",
        metrics=[tf.keras.metrics.SparseTopKCategoricalAccuracy(k=1)])
    return model
예제 #3
0
def get_efficientB6_model(config):
    # https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/

    size = (config['img_height'], config['img_width'])
    inputs = tf.keras.layers.Input(shape=(config['img_height'],
                                          config['img_width'], 3))
    #x = tf.keras.layers.GaussianNoise(20)(inputs)
    x = inputs
    from tensorflow.keras.applications import EfficientNetB6
    weights = 'imagenet'
    if 'should_init_pretrained' in config and config[
            'should_init_pretrained'] == False:
        weights = None

    encoder = EfficientNetB6(include_top=False,
                             weights=weights,
                             drop_connect_rate=0.2,
                             input_tensor=x)
    encoder.trainable = False
    for l in encoder.layers:
        l.trainable = False
    #encoder.summary()

    encoded_layer_names = [
        'block1a_activation',  # (112,112,32)
        'block3a_expand_activation',  # (56,56,144)
        'block4a_expand_activation',  # (28,28,240)
        'block5a_expand_activation'  #, # (14,14,672)
        #'block6a_activation' # (7,7,1152)
    ]

    outputs = [get_decoded(config, encoder, encoded_layer_names)]
    net = tf.keras.Model(inputs=encoder.inputs,
                         outputs=[outputs],
                         name="LargeEfficientUnet")
    return encoder, net
예제 #4
0
        target = train.iloc[:, 1:49].iloc[i, :]  # keypoint 뽑아주기

        yield (img, target)


#generator를 활용해 데이터셋 만들기
train_dataset = tf.data.Dataset.from_generator(
    trainGenerator, (tf.float32, tf.float32),
    (tf.TensorShape([360, 640, 3]), tf.TensorShape([48])))
train_dataset = train_dataset.batch(32).prefetch(1)

#2.========================================== 모델링

#간단한 CNN 모델을 적용합니다.
TF = EfficientNetB6(weights='imagenet',
                    include_top=False,
                    input_shape=(360, 640, 3))  #레이어 16개
TF.trainable = False  #훈련시키지 않고 가중치만 가져오겠다.
model = Sequential()
model.add(TF)

model.add(Flatten())

model.add(Dense(512))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())

model.add(Dense(48))  #activation='softmax')) #mnist사용할 경우
model.add(LeakyReLU(alpha=0.1))
model.summary()
예제 #5
0
def create_efficientnet(width, height, depth, model_base,
                        first_layers_to_freeze, num_classes, learning_rate,
                        epochs):
    inputShape = (height, width, depth)

    inputs = K.Input(shape=inputShape)

    if model_base == "b0":
        effnet = EfficientNetB0(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b1":
        effnet = EfficientNetB1(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b2":
        effnet = EfficientNetB2(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b3":
        effnet = EfficientNetB3(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b4":
        effnet = EfficientNetB4(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b5":
        effnet = EfficientNetB5(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b6":
        effnet = EfficientNetB6(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    else:
        effnet = EfficientNetB7(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")

    # # Print architecture of effnet
    # for i, layer in enumerate(effnet.layers[:]):
    # 	print(i, layer.name, layer.output_shape)
    # print(f"Effnet len: {len(effnet.layers[:])}")

    # b0: 20; b2: 33; b4: 236; b6: 45; b7: 265
    for i, layer in enumerate(effnet.layers[:first_layers_to_freeze]):
        layer.trainable = False
    for i, layer in enumerate(effnet.layers[first_layers_to_freeze:]):
        if not isinstance(layer, K.layers.BatchNormalization):
            layer.trainable = True

    model = Sequential()
    model.add(effnet)
    model.add(K.layers.Dropout(0.25))
    model.add(K.layers.Dense(effnet.layers[-1].output_shape[3]))
    model.add(K.layers.LeakyReLU())
    model.add(K.layers.GlobalAveragePooling2D())
    model.add(K.layers.BatchNormalization())
    model.add(K.layers.Dropout(0.5))
    model.add(K.layers.Dense(num_classes, activation='softmax'))

    # Freeze the batchnorm layer of our model
    for i, layer in enumerate(model.layers[:]):
        if isinstance(layer, K.layers.BatchNormalization):
            layer.trainable = False

    opt = Adam(lr=learning_rate, decay=learning_rate / epochs)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    model.summary()

    return model
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
model_dir = os.path.join(BASE_DIR, 'model/EfficientNet.h5')

ROWS = 256
COLS = 256
Channel = 3

#
model_graph = Graph()
with model_graph.as_default():
    tf_session = tf.compat.v1.Session()
    with tf_session.as_default():
        # create the base pre-trained model
        base_model = EfficientNetB6(weights='imagenet',
                                    include_top=False,
                                    input_tensor=Input(shape=(ROWS, COLS, 3)))
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        # # let's add a fully-connected layer
        x = Dense(2024, activation='relu')(x)
        x = BatchNormalization()(x)
        x = Dropout(0.2)(x)
        x = Dense(512, activation='relu')(x)
        x = BatchNormalization()(x)
        x = Dropout(0.2)(x)
        x = Dense(128, activation='relu')(x)
        x = BatchNormalization()(x)
        x = Dropout(0.1)(x)
        # and a logistic layer -- let's say we have 200 classes
        predictions = Dense(2, activation='softmax')(x)
예제 #7
0
def build_model(encoder='efficientnetb7', center='dac', full_skip=True, attention='sc', upscore='upall'):

	MODEL_NAME = encoder
	if center is not None:
		MODEL_NAME = MODEL_NAME+'_'+center
	if attention is not None:
		MODEL_NAME = MODEL_NAME+'_'+attention
	if full_skip:
		MODEL_NAME = MODEL_NAME + '_fullskip'
	if upscore is not None:
		MODEL_NAME = MODEL_NAME + '_'+upscore


	if encoder == 'resnet50':
		encoder = ResNet50(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'conv1_relu', 'conv2_block3_out', 'conv3_block4_out', 'conv4_block6_out']
		encoder_output = encoder.get_layer('conv5_block3_out').output
		# data    320x320x3
		# conv1_relu    160x160x64
		# conv2_block3_out     80x80x256
		# conv3_block4_out    40x40x512
		# conv4_block6_out    20x20x1024
		# conv5_block3_out    10x10x2048  --> encoder output

	elif encoder == 'resnet101':
		encoder = ResNet101(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'conv1_relu', 'conv2_block3_out', 'conv3_block4_out']
		encoder_output = encoder.get_layer('conv4_block23_out').output
		#data   320x320x3
		#conv1_relu   160x160x64
		#conv2_block3_out   80x80x256
		#conv3_block4_out    40x40x512
		#conv4_block23_out   20x20x1024 --> encoder output
		#conv5_block3_out  10x10x2048

	elif encoder == 'resnet50v2':
		encoder = ResNet50V2(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'conv1_conv', 'conv2_block3_1_relu', 'conv3_block4_1_relu', 'conv4_block6_1_relu']
		encoder_output = encoder.get_layer('post_relu').output
		# data   320x320x3
		# conv1_conv   160x160x64
		# conv2_block3_1_relu   80x80x64
		# conv3_block4_1_relu   40x40x128
		# conv4_block6_1_relu   20x20x256
		# post_relu   10x10x2048  --> encoder output

	elif encoder == 'resnet101v2':
		encoder = ResNet101V2(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'conv1_conv', 'conv2_block3_1_relu', 'conv3_block4_1_relu', 'conv4_block23_1_relu']
		encoder_output = encoder.get_layer('post_relu').output
		#data   320x320x3
		#conv1_conv   160x160x64
		#conv2_block3_1_relu   80x80x64
		#conv3_block4_1_relu    40x40x128
		#conv4_block23_1_relu   20x20x256 
		#post_relu  10x10x2048 --> encoder output

	elif encoder == 'vgg19':
		encoder = VGG19(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), weights='imagenet', include_top=False)
		skip_names = ['block1_conv2', 'block2_conv2', 'block3_conv4', 'block4_conv4', 'block5_conv4']
		encoder_output = encoder.get_layer('block5_pool').output
		# block1_conv2   320x320x64
		# block2_conv2   160x160x128
		# block3_conv4   80x80x256
		# block4_conv4   40x40x512
		# block5_conv4   20x20x512
		# block5_pool   10x10x512   --> encoder output

	elif encoder == 'efficientnetb6':
		encoder = EfficientNetB6(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'block2a_expand_activation', 'block3a_expand_activation', 'block4a_expand_activation']
		encoder_output = encoder.get_layer('block6a_expand_activation').output
		#data   320x320x3
		#block2a_expand_activation   160x160x192
		#block3a_expand_activation   80x80x240
		#block4a_expand_activation    40x40x432
		#block6a_expand_activation   20x20x1200 --> encoder output
		#top_activation   10x10x2304

	elif encoder == 'efficientnetb7':
		encoder = EfficientNetB7(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'block2a_expand_activation', 'block3a_expand_activation', 'block4a_expand_activation']
		encoder_output = encoder.get_layer('block6a_expand_activation').output
		#data   320x320x3
		#block2a_expand_activation   160x160x192
		#block3a_expand_activation   80x80x288
		#block4a_expand_activation    40x40x480
		#block6a_expand_activation   20x20x1344 --> encoder output
		#top_activation   10x10x

	elif encoder == 'mobilenetv2':
		encoder = MobileNetV2(input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name='data'), weights='imagenet', include_top=False)
		skip_names = ['data', 'block_1_expand_relu', 'block_3_expand_relu', 'block_6_expand_relu', 'block_13_expand_relu']
		encoder_output = encoder.get_layer('out_relu').output
		# data   320x320x3
		# block_1_expand_relu   160x160x96
		# block_3_expand_relu   80x80x144
		# block_6_expand_relu    40x40x192
		# block_13_expand_relu   20x20x576
		# out_relu   10x10x1248   --> encoder output

	skip_layers = [encoder.get_layer(i).output for i in skip_names]
	# Center --------------
	if center == 'atrous':
		x = atrous_block(encoder_output)
	elif center == 'dac':
		x = dense_atrous_block(encoder_output)
	elif center == 'aspp':
		x = aspp_block(encoder_output)
	elif center is None:
		x = encoder_output

    # Decoder --------------
	if attention == 'se':
		attn_block = se_block
	elif attention == 'cbam':
		attn_block = cbam_block
	elif attention == 'sc':
		attn_block = scSE_block

	filters = [i.shape[-1] for i in skip_layers]
	filters[0] = 64

	scales = [2 ** i for i in range(1, len(filters))][::-1]
	X = []
	for i in range(1, len(filters) + 1):
		X.append(x)

		down = []
		if full_skip:
			for j in range(len(scales) - (i - 1)):
				d = down_skip(skip_layers[j], scales[j + (i - 1)], filters[-1]//4)
				if attention is not None:
					d = attn_block(d) 
				down.append(d)


		direct = direct_skip(skip_layers[-i], filters[-1]//4)
		if attention is not None:
			direct = attn_block(direct)


		x = convtranspose_block(x, filters[-1]//4)
		if attention is not None:
			x = attn_block(x)

		x = Concatenate()([x] + [direct] + down)
		
		x = conv3_block(x, x.shape[-1])

	if upscore is not None:
		if upscore=='upall':
			up_scales=[2 ** i for i in range(1, len(filters)+1)][::-1]
			UP = [upscore_block(x, 32, up_scales[i]) for i, x in enumerate(X)]
			if attention is not None:
				UP = [attn_block(x) for x in UP]

			up = Concatenate()(UP)
     
		elif upscore=='upcenter':
			up = upscore_block(X[0], 64, 2 ** len(filters))
			if attention is not None:
				up = attn_block(up)

		x = Concatenate()([x, up])


	x = Conv2D(1, 1, padding='same')(x)
	x = Activation('sigmoid')(x)

	model = Model(encoder.input, x)

	metrics = [dice_coef, Recall(), Precision()]
	opt = Nadam(LR)
	model.compile(loss=bce_dice_loss, optimizer=opt, metrics=metrics)

	return model, MODEL_NAME
                                         batch_size=batch,
                                         labels='inferred',
                                         label_mode='int',
                                         image_size=(355, 370),
                                         shuffle=False)
test_set = image_dataset_from_directory(test_path,
                                        batch_size=batch,
                                        labels='inferred',
                                        label_mode='int',
                                        image_size=(355, 370),
                                        shuffle=False)

#Carregando a rede
#As outras redes podem ser importadas VGG16, MobileNetV2
model = EfficientNetB6(include_top=False,
                       input_shape=(355, 370, 3),
                       pooling='avg',
                       weights='imagenet')

train_feats = []
y_train = []

for img in train_set:
    img_train = img[0].numpy()
    #img_train = tf.keras.applications.mobilenet_v2.preprocess_input(img_train)
    #img_train = tf.keras.applications.vgg16.preprocess_input(img_train)
    predicts = model.predict(img_train)
    y_train.append(img[1].numpy()[0])
    train_feats.append(predicts.squeeze())

test_feats = []
y_test = []
예제 #9
0
def create_efficientnet(width, height, depth, model_base,
                        first_layers_to_freeze):
    inputShape = (height, width, depth)

    inputs = K.Input(shape=inputShape)

    if model_base == "b0":
        effnet = EfficientNetB0(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b1":
        effnet = EfficientNetB1(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b2":
        effnet = EfficientNetB2(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b3":
        effnet = EfficientNetB3(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b4":
        effnet = EfficientNetB4(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b5":
        effnet = EfficientNetB5(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b6":
        effnet = EfficientNetB6(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    else:
        effnet = EfficientNetB7(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")

    # # Print architecture of effnet
    # for i, layer in enumerate(effnet.layers[:]):
    # 	print(i, layer.name, layer.output_shape)

    # b0: 20; b2: 33; b4: 147; b6: 45; b7: 265

    for i, layer in enumerate(effnet.layers[:first_layers_to_freeze]):
        layer.trainable = False
    for i, layer in enumerate(effnet.layers[first_layers_to_freeze:]):
        layer.trainable = True

    effnet.summary()

    model = Sequential()
    model.add(effnet)
    model.add(K.layers.Dropout(0.25))
    model.add(K.layers.Dense(effnet.layers[-1].output_shape[3]))
    model.add(K.layers.LeakyReLU())
    model.add(K.layers.GlobalAveragePooling2D())
    model.add(K.layers.Dropout(0.5))
    model.add(K.layers.Dense(1, activation='linear'))

    return model