Example #1
0
def create_embedding():

    base_cnn = resnet.ResNet50(weights="imagenet",
                               input_shape=target_shape + (3, ),
                               pooling="max",
                               include_top=False)

    dense1 = layers.Dropout(0.3)(base_cnn.output)
    output = layers.Dense(128)(dense1)

    embedding = Model(base_cnn.input, output, name="Embedding")

    trainable = False
    for layer in base_cnn.layers:
        if layer.name == "conv5_block3_1_conv":
            trainable = True
        layer.trainable = trainable

    return embedding
Example #2
0
def create_embedding():

    base_cnn = resnet.ResNet50(weights="imagenet",
                               input_shape=target_shape + (3, ),
                               include_top=False)

    flatten = layers.Flatten()(base_cnn.output)
    dense1 = layers.Dense(512, activation="relu")(flatten)
    dense1 = layers.BatchNormalization()(dense1)
    dense1 = layers.Dropout(0.5)(dense1)
    dense2 = layers.Dense(256, activation="relu")(dense1)
    dense2 = layers.BatchNormalization()(dense2)
    dense2 = layers.Dropout(0.5)(dense2)
    output = layers.Dense(256)(dense2)

    embedding = Model(base_cnn.input, output, name="Embedding")

    trainable = False
    for layer in base_cnn.layers:
        if layer.name == "conv5_block1_out":
            trainable = True
        layer.trainable = trainable

    return embedding
Example #3
0
"""
## Setting up the embedding generator model

Our Siamese Network will generate embeddings for each of the images of the
triplet. To do this, we will use a ResNet50 model pretrained on ImageNet and
connect a few `Dense` layers to it so we can learn to separate these
embeddings.

We will freeze the weights of all the layers of the model up until the layer `conv5_block1_out`.
This is important to avoid affecting the weights that the model has already learned.
We are going to leave the bottom few layers trainable, so that we can fine-tune their weights
during training.
"""

base_cnn = resnet.ResNet50(
    weights="imagenet", input_shape=target_shape + (3,), include_top=False
)

flatten = layers.Flatten()(base_cnn.output)
dense1 = layers.Dense(512, activation="relu")(flatten)
dense1 = layers.BatchNormalization()(dense1)
dense2 = layers.Dense(256, activation="relu")(dense1)
dense2 = layers.BatchNormalization()(dense2)
output = layers.Dense(256)(dense2)

embedding = Model(base_cnn.input, output, name="Embedding")

trainable = False
for layer in base_cnn.layers:
    if layer.name == "conv5_block1_out":
        trainable = True
Example #4
0
    def __call__(self, *args, **kwargs):
        if self.model_name in ["VGG16", "vgg16"]:
            pre_trained = vgg16.VGG16(include_top=False,
                                      weights='imagenet',
                                      input_shape=self.input_shape)

        elif self.model_name in ["VGG19", "vgg19"]:
            pre_trained = vgg19.VGG19(include_top=False,
                                      weights='imagenet',
                                      input_shape=self.input_shape)

        elif self.model_name in ["MobileNet", "mobilenet"]:
            pre_trained = mobilenet.MobileNet(include_top=False,
                                              weights='imagenet',
                                              input_shape=self.input_shape)

        elif self.model_name in [
                "MobileNetV2", "MobileNet_V2", "mobilenetv2", "mobilenet_v2",
                "mobilenet_V2"
        ]:
            pre_trained = mobilenet_v2.MobileNetV2(
                include_top=False,
                weights='imagenet',
                input_shape=self.input_shape)

        elif self.model_name in ["resnet50", "ResNet50"]:
            pre_trained = resnet.ResNet50(include_top=False,
                                          weights='imagenet',
                                          input_shape=self.input_shape)

        # elif self.model_name in ["EfficientNetB0", "efficientnetb0"]:
        #     pre_trained = efficientnet.EfficientNetB0(include_top=False, weights='imagenet', input_shape=self.input_shape)
        #
        # elif self.model_name in ["EfficientNetB5", "efficientnetb5"]:
        #     pre_trained = efficientnet.EfficientNetB5(include_top=False, weights='imagenet', input_shape=self.input_shape)

        else:
            print("Not exists {}".format(self.model_name))
            return None

        if self.extractor:
            for layer in pre_trained.layers:
                layer.trainable = False

        if self.model_name in ["VGG16", "vgg16", "VGG19", "vgg19"]:
            x = Flatten()(pre_trained.output)
            x = Dense(1024, activation="relu",
                      kernel_initializer="he_normal")(x)
            x = Dropout(0.5)(x)
            x = Dense(1024, activation="relu",
                      kernel_initializer="he_normal")(x)
            x = Dropout(0.5)(x)

        else:
            x = GlobalAveragePooling2D()(pre_trained.output)
            x = Flatten()(x)

        y = Dense(2, activation="softmax")(x)
        model = Model(inputs=pre_trained.input, outputs=y)
        model.layers[0]._name = "input"
        return model
Example #5
0
                             rotation_range=30,
                             zoom_range=[.8, 1.2],
                             rescale=1 / 255.)
gen = datagen.flow_from_directory(directory='./chars/',
                                  target_size=(100, 100),
                                  color_mode='grayscale',
                                  batch_size=8,
                                  interpolation='nearest')

a_file = open("model_loader.pkl", "rb")
dict_file = pickle.load(a_file)
a_file.close()
gen.class_indices = dict_file

model = resnet.ResNet50(input_shape=(100, 100, 1),
                        weights=None,
                        classes=gen.num_classes)
opt = Adam(learning_rate=0.0001)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()
plot_model(model, show_shapes=True)

hist = model.fit_generator(gen, epochs=30)
model.save('model.h5', include_optimizer=False)

loss = hist.history['loss']
acc = hist.history['acc']
epochs = range(1, len(loss) + 1)
   chosen_dist = tf.gather(dist, chosen_idx)
   chosen_sim  = tf.gather(sim, chosen_idx)

   return (chosen_img, chosen_start, chosen_end, chosen_dist, chosen_sim)




# Code start

src = '/home/anhuynh/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5_tf2'
dst = '/home/anhuynh/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5'
shutil.copy(src,dst)

model = resnet.ResNet50()
model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('avg_pool').output)
# model.summary()

num_stop = 1000
stop_flg = False

# load image
img_1_path = 'hit2.png'

gsv_dir = 'GSV_Step_1_3_7/'
gsv_names = os.listdir(gsv_dir)
gsv_names.sort()

# gsv_names = ['4.jpeg','5.jpeg']
                                                  y,
                                                  test_size=0.2,
                                                  random_state=42,
                                                  stratify=y)

# MinMax
print(np.min(X), np.max(X))

x_train /= -80
x_val /= -80

# 모델
input_tensor = Input(shape=X.shape[1:], dtype='float32', name='input')

model = resnet.ResNet50(input_tensor=input_tensor,
                        weights=None,
                        pooling='max',
                        classes=13)

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
file_path = './project/mini/data/genre_model_resnet50_app.hdf5'
es = EarlyStopping(monitor='val_accuracy', patience=50)
cp = ModelCheckpoint(filepath=file_path,
                     monitor='val_accuracy',
                     save_best_only=True)
lr = ReduceLROnPlateau(monitor='val_accuracy', factor=0.5, patience=20)
history = model.fit(x_train,
                    y_train,