import os
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import ModelCheckpoint
from processors.augmentation import get_generators
from modules.lr import CosineAnnealingLRSchedule
from constants import *

base_model = ResNet50(
    weights='imagenet',
    include_top=False,
    input_shape=(INPUT_SIZE[0], INPUT_SIZE[1], 3),
)

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)  # , activity_regularizer=l1_l2(0.001)
x = Dense(42, activation='softmax', name='output')(x)

model = Model(inputs=base_model.input, outputs=x)

for layer in base_model.layers:
    layer.trainable = False

print(model.summary())

model.compile(optimizer=SGD(momentum=0.9),
              loss='sparse_categorical_crossentropy',
              metrics=['acc'])
Exemple #2
0
                             horizontal_flip=True,
                             vertical_flip=True,
                             preprocessing_function=preprocess_input)

training_set = datagen.flow_from_directory(
    r'D:\Projects\Fruits_classifier\data\fruits-360\Training',
    target_size=[256, 256],
    shuffle=False,
    batch_size=16)
val_set = datagen.flow_from_directory(
    r'D:\Projects\Fruits_classifier\data\fruits-360\Test',
    target_size=[256, 256],
    shuffle=False,
    batch_size=16)
resnet = ResNet50(input_shape=[256, 256, 3],
                  weights='imagenet',
                  include_top=False)
for layer in resnet.layers:
    layer.trainable = False

x = tf.keras.layers.Flatten()(resnet.output)

x = tf.keras.layers.Dense(units=131, activation='softmax')(x)

model = Model(inputs=resnet.input, outputs=x)
model.summary()

model.compile(optimizer='RMSprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(training_set,
Exemple #3
0
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
import matplotlib.pyplot as plt

# re-size all the images to this
IMAGE_SIZE = [224, 224]

train_path = '/content/drive/My Drive/Car Model Classifier/Datasets/Train'
valid_path = '/content/drive/My Drive/Car Model Classifier/Datasets/test'

# Import the Vgg 16 library as shown below and add preprocessing layer to the front of VGG
# Here we will be using imagenet weights

resnet = ResNet50(input_shape=IMAGE_SIZE + [3],
                  weights='imagenet',
                  include_top=False)

# don't train existing weights
for layer in resnet.layers:
    layer.trainable = False

# useful for getting number of output classes
folders = glob('/content/drive/My Drive/Car Model Classifier/Datasets/Train/*')
folders

# our layers - you can add more if you want
x = Flatten()(resnet.output)

prediction = Dense(len(folders), activation='softmax')(x)
Exemple #4
0
def init_multitask_cnn(base_model="InceptionV3"):
    freeze_layer = FREEZE_LAYER_MAP[base_model]
    img_sz = IMG_WIDTH_MAP[base_model]

    if base_model == "InceptionV3":
        base_cnn_model = InceptionV3(weights='imagenet',
                                     include_top=False,
                                     input_shape=(img_sz, img_sz, 3))
    elif base_model == "VGG16":
        base_cnn_model = VGG16(weights='imagenet',
                               include_top=False,
                               input_shape=(img_sz, img_sz, 3))
    elif base_model == "ResNet50":
        base_cnn_model = ResNet50(weights='imagenet',
                                  include_top=False,
                                  input_shape=(img_sz, img_sz, 3))
    elif base_model == "Xception":
        base_cnn_model = Xception(weights='imagenet',
                                  include_top=False,
                                  input_shape=(img_sz, img_sz, 3))

    last_conv = base_cnn_model.output
    img_embedding_layer = GlobalAveragePooling2D(
        name='img_embedding_layer')(last_conv)
    label_out = Dense(N_CLASSES, activation='softmax',
                      name="label_out")(img_embedding_layer)
    label_input = Input(shape=(N_CLASSES, ), name="label_input")
    """
    Define the tensor shape.
    """
    def GAPalpha_output_shape(input_shapes):
        output_shape = (input_shapes[0][0], input_shapes[0][3])
        return output_shape

    def GradCAM_output_shape(input_shapes):
        output_shape = input_shapes[0][0:3]
        return output_shape

    """
    Lambda layer used to define the heatmap as a output.
    """
    alpha_weights = Lambda(Alpha_GAP,
                           GAPalpha_output_shape,
                           name="alpha_weights")(
                               [last_conv, label_out, label_input])
    cam_out = Lambda(GradCAM_Pool, GradCAM_output_shape,
                     name="cam_out")([last_conv, alpha_weights])
    """
    Define the bias-level learning relevant layers.
    """
    biaslevel_fc = Dense(2048, activation='relu',
                         name='biaslevel_fc')(img_embedding_layer)
    biaslevel_dropout = Dropout(0.5, name='biaslevel_dropout')(biaslevel_fc)
    biaslevel_out = Dense(1, name='biaslevel_out')(biaslevel_dropout)

    model = Model(
        inputs=[base_cnn_model.input, label_input
                ],  # 2 inputs: image, y ground truth (or prediction??)
        outputs=[label_out, cam_out, biaslevel_out]  # 3 outputs
    )

    # Freezing of the shallow layers for a more efficient training
    for layer in model.layers[:freeze_layer]:
        layer.trainable = False
    for layer in model.layers[freeze_layer:]:
        layer.trainable = True
    return model
Exemple #5
0
        conv_block1_params = []
        for layer in self.conv_block1.get_params():
            for param in layer:
                conv_block1_params.append(param)
        params = [
            *self.conv1.get_params(), *self.batch1.get_params(),
            *conv_block1_params
        ]
        return params


if __name__ == '__main__':
    tf.reset_default_graph()
    tf.keras.backend.clear_session()
    # you can also set weights to None, it doesn't matter
    resnet = ResNet50(weights='imagenet')

    # you can determine the correct layer
    # by looking at resnet.layers in the console
    partial_model = Model(inputs=resnet.input,
                          outputs=resnet.layers[18].output)
    print(partial_model.summary())
    # for layer in partial_model.layers:
    #   layer.trainable = False

    my_partial_resnet = PartialResNet()

    # make a fake image
    X = np.random.random((1, 224, 224, 3))

    # get keras output
test_datagen = ImageDataGenerator(
    preprocessing_function=tf.keras.applications.resnet50.preprocess_input)

train_generator = train_datagen.flow(train_x,
                                     train_y,
                                     batch_size=_BATCH_SIZE * _GPUs)
validation_generator = test_datagen.flow(val_x,
                                         val_y,
                                         batch_size=_BATCH_SIZE * _GPUs)
test_generator = test_datagen.flow(test_x,
                                   test_y,
                                   batch_size=_BATCH_SIZE * _GPUs)

#with tf.device('/cpu:0'):
#    model = VGG16(include_top=True, weights=None, input_tensor=None, input_shape=(32,32,3), pooling=None, classes=1000)
model = ResNet50(weights=None, input_shape=(64, 64, 3))
opt = tf.keras.optimizers.SGD(lr=_LR, momentum=0.9)

para_model = multi_gpu_model(model,
                             gpus=_GPUs,
                             cpu_merge=True,
                             cpu_relocation=False)
para_model.compile(optimizer=opt,
                   loss='categorical_crossentropy',
                   metrics=['accuracy'])

callbacks = [
    # Reduce the learning rate if training plateaues.
    tf.keras.callbacks.ReduceLROnPlateau(patience=60, verbose=1),

    # Early stopping
Exemple #7
0
                                      target_size=(IMG_SIZE, IMG_SIZE),
                                      class_mode='categorical',
                                      batch_size=batch_size,
                                      shuffle=False)

filepath = os.path.join(output_folder, modelname + ".hdf5")
loss_epoch_file = os.path.join(output_folder, modelname + '.csv')
plt_file = os.path.join(output_folder, modelname + '_plot.png')

print(filepath)
print(loss_epoch_file)

# define model
modelGo = ResNet50(include_top=True,
                   weights=None,
                   input_tensor=None,
                   input_shape=(IMG_SIZE, IMG_SIZE, 3),
                   classes=4)
modelGo.load_weights(filepath)
modelGo.compile(loss='categorical_crossentropy',
                optimizer='adam',
                metrics=['accuracy'])

predict = modelGo.predict_generator(test_it)
loss, acc = modelGo.evaluate_generator(test_it)

import math
tsLbl = []
test_it.reset()
count = 0
number_of_examples = len(test_it.filenames)
Exemple #8
0
SOURCE_IMG_PATH = "elephant.jpg"
SOURCE_IMG_PATH = "minivan.jpg"
SOURCE = IDX_MINIVAN
TARGET = IDX_AIRLINER

N_POPULATION = 100
N_GENERATIONS = 500

NET = 'ResNet50'
NET = 'MobileNetV2'

if NET == 'ResNet50':
    from tensorflow.keras.applications.resnet50 import ResNet50
    from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
    model = ResNet50(weights="imagenet", include_top=True)
elif NET == 'MobileNetV2':
    from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
    from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
    model = MobileNetV2(weights="imagenet", include_top=True)

# TODO: cleanup
# input_tensor = tf.keras.layers.Input(shape=(32, 32, 3))
# image = resnet50.preprocess_input(image)

# dataset = tfds.load(name='cifar10', split=tfds.Split.TRAIN)
# dataset = dataset.batch(1)

# for features in dataset.take(1):
#     image, label = features['image'], features['label']
#     ic(image.shape)
def main():
    global Width, Height, pic_dir_out, pic_dir_data
    output_model_file = os.path.expanduser("../data/model.h5")
    Width = 224
    Height = 224
    train_dir = os.path.expanduser('../images')  # 训练集数据
    val_dir = os.path.expanduser('../test-set')  # 验证集数据

    nb_classes = 101
    nb_epoch = 5
    batch_size = 100

    nb_train_samples = get_nb_files(train_dir)  # 训练样本个数
    nb_classes = len(glob.glob(train_dir + "/*"))  # 分类数
    nb_val_samples = get_nb_files(val_dir)  # 验证集样本个数
    nb_epoch = int(nb_epoch)  # epoch数量
    batch_size = int(batch_size)

    #  图片生成器
    train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                       rotation_range=30,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True)
    test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                      rotation_range=30,
                                      width_shift_range=0.2,
                                      height_shift_range=0.2,
                                      shear_range=0.2,
                                      zoom_range=0.2,
                                      horizontal_flip=True)

    # 训练数据与测试数据
    train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(Width, Height),
        batch_size=batch_size,
        class_mode='categorical')

    validation_generator = test_datagen.flow_from_directory(
        val_dir,
        target_size=(Width, Height),
        batch_size=batch_size,
        class_mode='categorical')

    base_model = ResNet50(weights='imagenet',
                          include_top=False)  # 预先要下载no_top模型
    model = add_new_last_layer(base_model, nb_classes)  # 从基本no_top模型上添加新层
    setup_to_transfer_learn(model, base_model)  # 冻结base_model所有层

    for i, layer in enumerate(base_model.layers):
        print(i, layer.name)

    tensorboard = TensorBoard(log_dir=LOG_DIR, write_images=True)
    checkpoint = ModelCheckpoint(filepath=MODEL_FILE_PATH,
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True)
    model.fit_generator(train_generator,
                        steps_per_epoch=638,
                        epochs=nb_epoch,
                        validation_data=validation_generator,
                        validation_steps=30,
                        class_weight='auto',
                        callbacks=[tensorboard, checkpoint],
                        verbose=1)

    # 设置网络结构
    setup_to_finetune_1(model)
    nb_epoch = 15

    # 模式二训练
    model.fit_generator(train_generator,
                        steps_per_epoch=638,
                        epochs=nb_epoch,
                        validation_data=validation_generator,
                        validation_steps=30,
                        class_weight='auto',
                        callbacks=[tensorboard, checkpoint],
                        verbose=1,
                        initial_epoch=5)

    # 设置网络结构
    setup_to_finetune_2(model)
    nb_epoch = 30

    # 模式三训练
    model.fit_generator(train_generator,
                        steps_per_epoch=638,
                        epochs=nb_epoch,
                        validation_data=validation_generator,
                        validation_steps=30,
                        class_weight='auto',
                        callbacks=[tensorboard, checkpoint],
                        verbose=1,
                        initial_epoch=15)

    # 设置网络结构
    setup_to_finetune_3(model)
    nb_epoch = 50

    # 模式四训练
    model.fit_generator(train_generator,
                        steps_per_epoch=638,
                        epochs=nb_epoch,
                        validation_data=validation_generator,
                        validation_steps=30,
                        class_weight='auto',
                        callbacks=[tensorboard, checkpoint],
                        verbose=1,
                        initial_epoch=30)

    # 设置网络结构
    setup_to_finetune_4(model)
    nb_epoch = 300
    train_generator.batch_size = 70
    validation_generator.batch_size = 70

    # 模式五训练
    model.fit_generator(train_generator,
                        steps_per_epoch=900,
                        epochs=nb_epoch,
                        validation_data=validation_generator,
                        validation_steps=40,
                        class_weight='auto',
                        callbacks=[tensorboard, checkpoint],
                        verbose=1,
                        initial_epoch=50)

    # 模型保存
    model.save(output_model_file)
callbacks = EarlyStopping(monitor='val_loss',
                          patience=4,
                          verbose=1,
                          mode='auto')
best_model_file = '.../resnet50_drop_batch_best_weights_256.h5'
best_model = ModelCheckpoint(best_model_file,
                             monitor='val_acc',
                             verbose=1,
                             save_best_only=True)

# In[9]:

wp = '.../weather_pred/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
resnet50_base = ResNet50(include_top=False,
                         weights=wp,
                         input_tensor=None,
                         input_shape=(img_height, img_width, 3))

# In[10]:

print('Adding new layers...')
output = resnet50_base.get_layer(index=-1).output
output = Flatten()(output)
output = Dense(512, activation="relu")(output)
output = BatchNormalization()(output)
output = Dropout(0.2)(output)
output = Dense(512, activation="relu")(output)
output = BatchNormalization()(output)
output = Dropout(0.2)(output)
output = Dense(5, activation='softmax')(output)
print('New layers added!')
Exemple #11
0
        for step in np.arange(1):
            value = sess.run(next_batch_train)
            showimg(step, value[1], np.asarray((value[0] + 1) * 127.5,
                                               np.uint8), 10)  #显示图片

    except tf.errors.OutOfRangeError:  #捕获异常
        print("Done!!!")

###########################################
#构建模型
img_size = (224, 224, 3)
inputs = tf.keras.Input(shape=img_size)
#inputs =preprocess_input(inputs)
conv_base = ResNet50(
    weights='resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
    input_tensor=inputs,
    input_shape=img_size,
    include_top=False)  #创建ResNet网络

model = tf.keras.models.Sequential()
model.add(conv_base)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
conv_base.trainable = False
model.summary()
model.compile(loss='binary_crossentropy',
              optimizer=tf.keras.optimizers.RMSprop(lr=2e-5),
              metrics=['acc'])

All_steps = 300
random.shuffle(temp)
img_arr, img_label = zip(*temp)
img_arr = np.asarray(img_arr)
img_label = np.asarray(img_label)

print(img_arr)

train_data, test_data, train_label, test_label = train_test_split(
    img_arr, img_label, test_size=0.2, random_state=42)
train_data, val_data, train_label, val_label = train_test_split(
    train_data, train_label, test_size=0.2, random_state=42)

# 以訓練好的 ResNet50 為基礎來建立模型,
# 捨棄 ResNet50 頂層的 fully connected layers
net = ResNet50(include_top=False,
               weights='imagenet',
               input_tensor=None,
               input_shape=(size[0], size[1], 3))
x = net.output
x = Flatten()(x)

# 增加 DropOut layer
x = Dropout(0.5)(x)

# 增加 Dense layer,以 softmax 產生個類別的機率值
output_layer = Dense(NUM_CLASSES, activation='softmax', name='softmax')(x)

# 設定凍結與要進行訓練的網路層
net_final = Model(inputs=net.input, outputs=output_layer)
for layer in net_final.layers[:FREEZE_LAYERS]:
    layer.trainable = False
for layer in net_final.layers[FREEZE_LAYERS:]:
Exemple #13
0
def create_model():
    base_model = ResNet50(include_top=True, weights=None, classes=class_num)
    model = tf.keras.Model(inputs=base_model.input, outputs=base_model.output)
    return model
Exemple #14
0
def download():
    """ Download Cifar10 and Resnet50. """
    ResNet50(weights='imagenet', include_top=False)
Exemple #15
0
    def train_and_predict(img: np.ndarray) -> TrainPredictOutput:
        model = ResNet50(weights="imagenet")

        preds = model.predict(img)
        return TrainPredictOutput(predictions=preds,
                                  model=TensorFlow2ONNX(model))
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
import cv2

model = ResNet50(
    weights='/script/neuralNet/resnet50_weights_tf_dim_ordering_tf_kernels.h5')


def predict_from_rgb(img):
    global model
    im_rgb = cv2.cvtColor(img, cv2.COLOR_RG2RGB)
    im_rgb = cv2.resize(im_rgb, (224, 224))
    x = im_rgb
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    preds = model.predict(x)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    return decode_predictions(preds, top=3)[0]


def predict(img):
    global model
    im_rgb = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    im_rgb = cv2.cvtColor(im_rgb, cv2.COLOR_BGR2RGB)
    im_rgb = cv2.resize(im_rgb, (224, 224))
    x = im_rgb
    x = np.expand_dims(x, axis=0)
Exemple #17
0
(trainData, trainLabels), (testData, testLabels) = mnist.load_data()

# 训练数据 60000张手写图片,28*28*1
# 测试数据 10000张手写图片,28*28*1
trainData = trainData.reshape(60000, 784)
testData = testData.reshape(10000, 784)

trainLabels = keras.utils.to_categorical(trainLabels.reshape(-1, 1), 10)
testLabels = keras.utils.to_categorical(testLabels.reshape(-1, 1), 10)

# tensorflow后端
trainData = trainData.reshape(trainData.shape[0], 28, 28, 1)
testData = testData.reshape(testData.shape[0], 28, 28, 1)

# tf.Keras 自带的ResNet50,需要科学上网 否则下载速度太慢
model = ResNet50()
model.summary()

lr = 0.05
num_epochs = 5
batch_size = 256
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=lr), metrics=['accuracy'])

for epoch in range(num_epochs):
    start = time.time()
    history = model.fit(trainData, trainLabels, batch_size=batch_size, shuffle=True, verbose=0)
    score = model.evaluate(testData, testLabels, verbose=0)
    print 'epoch ', epoch + 1, ', loss %.3f' % history.history['loss'][0], \
        ', train acc %.9f' % history.history['acc'][0], ', test acc %.9f' % score[1], \
        ', time %.3f' % (time.time() - start), ' sec'
Exemple #18
0
def predice(img, model: Model):
    #print("llego a predice")
    x=cv2.merge([img,img,img])
    #x = image.img_to_array(img)
    #print("traza1 "+ str(x.shape))
    x = np.expand_dims(x, axis=0)
    #print("traza2 "+ str(x.shape))
    x = preprocess_input(x)
    #print("traza3 "+ str(x.shape))
    return model.predict(x)

def findDifference(f1, f2):
    return np.linalg.norm(f1-f2)

model = ResNet50(weights='imagenet')

def escalaGrises(img_path, img_path2):
    print("estoy en escala grises")
    global  model
    # Reading the image from the present directory
    image = cv2.imread(img_path)
    # Resizing the image for compatibility
    image = cv2.resize(image, (224, 224))

    image2 = cv2.imread(img_path2)
    # Resizing the image for compatibility
    image2 = cv2.resize(image2, (224, 224))

    # The initial processing of the image
    # image = cv2.medianBlur(image, 3)
              callbacks=custom_callbacks(ckpt_path),
              verbose=1)

    if True:
        model_path = os.path.join(ckpt_path, 'saved_model')

        if not os.path.exists(model_path):
            os.makedirs(model_path)

        model_name = os.path.join(model_path, 'resnet50') + '.h5'
        model.save(model_name)


if do_train:

    resnet = ResNet50(weights='imagenet')  #To use pretrained model
    # resnet = Tf_ResNet50._resnet50(input_shape = (224, 224, 3), classes = 1000) #scratchmodel

    #resnet = tf.keras.models.load_model('out/model-05-0.63/') #to resume
    for layer in resnet.layers:
        layer.use_bias = True
    _bias = [l.use_bias for l in resnet.layers]

    train_datagen_kwargs = dict(rescale=1. / 255, validation_split=0.3)
    train_dataflow_kwargs = dict(target_size=(224, 224),
                                 batch_size=batch_size,
                                 interpolation="bilinear",
                                 class_mode='categorical')

    train_datagen = ImageDataGenerator(rotation_range=20,
                                       horizontal_flip=True,
Exemple #20
0
    # Arguments
        epoch (int): The number of epochs

    # Returns
        lr (float32): learning rate
    """
    lr = args.lr  #1e-3
    print('Learning rate: ', lr)
    return lr


model = models.Sequential()

if '50' in args.model:
    base_model = ResNet50(weights=None,
                          include_top=False,
                          input_shape=(32, 32, 3),
                          pooling=None)
elif '101' in args.model:
    base_model = ResNet101(weights=None,
                           include_top=False,
                           input_shape=(32, 32, 3),
                           pooling=None)
elif '152' in args.model:
    base_model = ResNet152(weights=None,
                           include_top=False,
                           input_shape=(32, 32, 3),
                           pooling=None)

#base_model.summary()

#pdb.set_trace()
Exemple #21
0
def load_ResNet50():
    return ResNet50(include_top=True, weights='imagenet')
Exemple #22
0
    def build_model(self):
        def DeConv(output_filter, last_layer, skip, skip_layer):
            l1 = Conv2DTranspose(filters=last_layer.get_shape().as_list()[-1],
                                 kernel_size=(3, 3),
                                 strides=(2, 2),
                                 padding="same")(last_layer)
            b1 = BatchNormalization()(l1)
            a1 = Activation("relu")(b1)

            if skip:
                skip_layer = concatenate([skip_layer, l1], axis=-1)

            l2 = Conv2D(filters=output_filter,
                        kernel_size=(1, 1),
                        padding="same")(skip_layer)
            l2 = BatchNormalization()(l2)
            l2 = Activation("relu")(l2)

            return l2

        def Attention_Module(inputs):
            GAP = Lambda(lambda x: tf.reduce_mean(x, [1, 2], keepdims=True))(
                inputs)

            l = Conv2D(filters=inputs.get_shape().as_list()[-1],
                       kernel_size=(1, 1))(GAP)
            l = BatchNormalization()(l)
            l = Activation("sigmoid")(l)
            l = Multiply()([inputs, l])

            return l

        def Attention_Fusion_Module(input1, input2, n_filters=128):
            inputs = concatenate([input1, input2], axis=-1)

            a = Conv2D(filters=n_filters, kernel_size=(3, 3),
                       padding="same")(inputs)
            a = BatchNormalization()(a)
            a1 = Activation("relu")(a)

            GAP = Lambda(lambda x: tf.reduce_mean(x, [1, 2], keepdims=True))(
                a1)
            a = Conv2D(filters=n_filters, kernel_size=(1, 1))(GAP)
            a2 = Activation("relu")(a)
            a = Conv2D(filters=n_filters, kernel_size=(1, 1))(a2)
            a2 = Activation("sigmoid")(a)

            mul = Multiply()([a1, a2])
            add = Add()([a1, mul])

            return add

        # Load Resnet50
        resnet = ResNet50()

        # remove last 2 layers
        resnet._layers.pop()
        resnet._layers.pop()

        input = resnet.input

        resnet_last_layer = resnet.layers[-1].output

        # Encoding..
        block_01 = resnet.get_layer("res4f_branch2c").output  # 14*14
        block_02 = resnet.get_layer("res3d_branch2c").output  # 28*28
        block_03 = resnet.get_layer("res2c_branch2c").output  # 56*56
        block_04 = resnet.get_layer("conv1").output  # 112*112
        block_05 = resnet.get_layer("input_1").output  # 224*224
        """
        Context Attention Path
        """

        Attention_01 = Attention_Module(inputs=block_04)
        Attention_02 = Attention_Module(inputs=block_03)

        global_channel = GlobalAveragePooling2D()(Attention_02)
        Attention_Fusion_01 = Multiply()([Attention_02, global_channel])

        Attention_01 = UpSampling2D(size=(2, 2))(Attention_01)
        Attention_Fusion_01 = UpSampling2D(size=(4, 4))(Attention_Fusion_01)

        Attention_output = concatenate([Attention_01, Attention_Fusion_01],
                                       axis=-1)
        """
        Deconding Path
        """
        DeConv_01 = DeConv(self.output_filter * 16,
                           last_layer=resnet_last_layer,
                           skip=True,
                           skip_layer=block_01)
        DeConv_02 = DeConv(self.output_filter * 8,
                           last_layer=DeConv_01,
                           skip=True,
                           skip_layer=block_02)
        DeConv_03 = DeConv(self.output_filter * 4,
                           last_layer=DeConv_02,
                           skip=True,
                           skip_layer=block_03)
        DeConv_04 = DeConv(self.output_filter * 2,
                           last_layer=DeConv_03,
                           skip=True,
                           skip_layer=block_04)
        DeConv_05 = DeConv(self.output_filter,
                           last_layer=DeConv_04,
                           skip=True,
                           skip_layer=block_05)

        Final_Fusion = Attention_Fusion_Module(Attention_output, DeConv_05)

        output = Conv2D(1, (1, 1), activation="sigmoid")(Final_Fusion)

        return Model(inputs=input, outputs=[output])
    output = Concatenate()([positive_pair, negative_pair])
    main_model = Model(inputs=[image_input, caption_input, noise_input],
                       outputs=output)
    image_model = Model(inputs=image_input, outputs=image_pipeline)
    caption_model = Model(inputs=caption_input, outputs=caption_pipeline)
    main_model.compile(loss=custom_loss, optimizer='adam', metrics=[accuracy])
    return main_model, image_model, caption_model


if __name__ == '__main__':

    Lang = PathByLanguage('en', directory)
    NAME = "CNN_FramScratch"
    tensorboard = TensorBoard(log_dir="./logs/{}".format(NAME))
    resnet_model = ResNet50(weights='imagenet',
                            include_top=False,
                            pooling='avg')

    # extract features from all images
    # features = extract_features_dir(resnet_model, dataset_path)
    # print('Extracted Features for %d images' % len(features))

    # # save to file
    # np_features = np.array(features)
    # np.save(output_path + '/features.npy', np_features)

    # features=np.load(output_path+'/features.npy')
    # text_file = directory + '/Flickr8k_text/Flickr8k.token.en.txt'
    # images, texts = load(text_file)
    # features, texts, names_extend=extend_dataset(dataset_path, features, images, texts)