Esempio n. 1
0
def migrate_model(new_model):
    old_model = vgg16_model(224, 224, 3)
    # print(old_model.summary())
    old_layers = [l for l in old_model.layers]
    new_layers = [l for l in new_model.layers]

    old_conv1_1 = old_model.get_layer('conv1_1')
    old_weights = old_conv1_1.get_weights()[0]
    old_biases = old_conv1_1.get_weights()[1]
    new_weights = np.zeros((3, 3, 4, 64), dtype=np.float32)
    new_weights[:, :, 0:3, :] = old_weights
    new_weights[:, :, 3:4, :] = 0.0
    new_conv1_1 = new_model.get_layer('conv1_1')
    new_conv1_1.set_weights([new_weights, old_biases])

    for i in range(2, 31):
        old_layer = old_layers[i]
        new_layer = new_layers[i + 1]
        new_layer.set_weights(old_layer.get_weights())

    # flatten = old_model.get_layer('flatten')
    # f_dim = flatten.input_shape
    # print('f_dim: ' + str(f_dim))
    # old_dense1 = old_model.get_layer('dense1')
    # input_shape = old_dense1.input_shape
    # output_dim = old_dense1.get_weights()[1].shape[0]
    # print('output_dim: ' + str(output_dim))
    # W, b = old_dense1.get_weights()
    # shape = (7, 7, 512, output_dim)
    # new_W = W.reshape(shape)
    # new_conv6 = new_model.get_layer('conv6')
    # new_conv6.set_weights([new_W, b])

    del old_model
Esempio n. 2
0
    def simnet_unit():
        vgg = vgg16_model(original_shape[0], original_shape[1], channels,
                          num_classes)
        ds = downsample_cnn_model(scaled_shape1, scaled_shape2, channels)

        x = Concatenate()([vgg.output, ds.output])
        # linear embedding
        x = Dense(4096)(x)
        inputs = [i for i in ds.inputs]
        inputs.append(vgg.input)
        return Model(inputs=ds.inputs + [vgg.input], outputs=x)
Esempio n. 3
0
def migrate_model(new_model):
    old_model = vgg16_model(224, 224, 3)
    # print(old_model.summary())
    old_layers = [l for l in old_model.layers]
    new_layers = [l for l in new_model.layers]

    old_conv1_1 = old_model.get_layer('conv1_1')
    old_weights = old_conv1_1.get_weights()[0]
    old_biases = old_conv1_1.get_weights()[1]
    new_weights = np.zeros((3, 3, 3, 64), dtype=np.float32)
    new_weights[:, :, 0:3, :] = old_weights
    new_conv1_1 = new_model.get_layer('conv1_1')
    new_conv1_1.set_weights([new_weights, old_biases])

    for i in range(2, 31):
        old_layer = old_layers[i]
        new_layer = new_layers[i + 1]
        new_layer.set_weights(old_layer.get_weights())

    del old_model
Esempio n. 4
0
    def test_vgg():
        X_train, X_valid, Y_train, Y_valid = split_data(X, y)
        img_rows, img_cols, channels = X_train[0, :, :, :].shape

        model = vgg16_model(img_rows, img_cols, channels, num_classes)

        model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            epochs=nb_epoch,
            shuffle=True,
            verbose=1,
            validation_data=(X_valid, Y_valid),
        )

        predictions_valid = model.predict(X_valid,
                                          batch_size=batch_size,
                                          verbose=1)

        score = log_loss(Y_valid, predictions_valid)
        print(score)
def migrate_model(new_model):
    vgg_16 = [4,7,9,12,14,16,19,21,26,28,30]
    v_16 = [2,4,5,7,8,9,11,12,24,25,26]
    old_model = vgg16_model(224, 224, 3)
    # print(old_model.summary())
    old_layers = [l for l in old_model.layers]
    new_layers = [l for l in new_model.layers]

    old_conv1_1 = old_model.get_layer('conv1_1')
    old_weights = old_conv1_1.get_weights()[0]
    old_biases = old_conv1_1.get_weights()[1]
    new_weights = np.zeros((3, 3, channel, 64), dtype=np.float32)
    new_weights[:, :, 0:3, :] = old_weights
    new_weights[:, :, 3:channel, :] = 0.0
    new_conv1_1 = new_model.get_layer('conv1_1')
    new_conv1_1.set_weights([new_weights, old_biases])

    for i in range(11):
        index_vgg = vgg_16[i]
        index_model = v_16[i]
        old_layer = old_layers[index_vgg-1]
        new_layer = new_layers[index_model]
        new_layer.set_weights(old_layer.get_weights())
    del old_model
Esempio n. 6
0
    def getVGG16(self, train_images, train_labels, load_saved_model,
                 model_save_path, use_pretraining, pretrained_weights_path, train_dir,
                 val_dir, fine_tuning_method, batch_size, num_epochs, optimizer, loss, initial_epoch, sample, lr=None):
        """

        :param load_saved_model: boolean (whether to just load the model from weights path)
        :param model_save_path: (final model weights path, if load_pretrained is true)
        :param pretrained_weights_path: if load_trained is false and if use_pretraining is true, the path of weights to load for pre-training
        :param train_dir: training data directory
        :param val_dir: validation data directory
        :param use_pretraining: boolean, whether to use pre-training or train from scratch
        :param fine_tuning_method: whether to use end-to-end pre-training or phase-by-phase pre-training
        :param batch_size: batch_size to use while fitting the model
        :param num_epochs: number of epochs to train the model
        :param optimizer: type of optimizer to use (sgd|adagrad)
        :param loss: type of loss to use (mse|l1)
        :param initial_epoch: starting epoch to start training
        :return: Returns the AlexNet model according to the parameters provided

        """

        print(get_time_string() + 'Creating VGG16 model..')

        img_rows, img_cols = 224, 224  # Resolution of inputs
        channels = 3

        if load_saved_model:
            if model_save_path is None:
                raise Exception('Unable to load trained model as model_save_path is None!')
            print(get_time_string() + 'Loading saved model from ' + model_save_path + '..')
            model = load_model(model_save_path)
        else:
            model = vgg16_model(img_rows=img_rows, img_cols=img_cols, channels=channels,
                                num_classes=NUM_CLASSES_YEARBOOK,
                                use_pretraining=use_pretraining, pretrained_weights_path=pretrained_weights_path,
                                optimizer=optimizer, loss=loss, fine_tuning_method=fine_tuning_method)

        if initial_epoch >= num_epochs:
            print(get_time_string() + 'Not fitting the model since initial_epoch is >= num_epochs. Returning model..')
            return model

        # Start Fine-tuning
        print(get_time_string() + 'Fitting the model..')
        for e in range(initial_epoch, num_epochs):
            print_line()
            print('Starting epoch ' + str(e))
            print_line()
            completed = 0

            for x_chunk, y_chunk in chunks(train_images, train_labels, batch_size, VGG16_ARCHITECTURE):
                print(get_time_string() + 'Fitting model for chunk of size ' + str(len(x_chunk)) + '...')
                model.fit(x_chunk, y_chunk,
                          batch_size=batch_size,
                          nb_epoch=1,
                          verbose=1
                          )
                completed += len(x_chunk)
                print(get_time_string() + str(completed) + ' of ' + str(len(train_images)) + ' complete. ')

            print(get_time_string() + 'Epoch ' + str(e) + ' complete.')

            file_name = self.getCheckpointFileName(base_model_save_path=model_save_path, epoch=e)
            print(get_time_string() + 'Saving model to ' + file_name)
            model.save(file_name)

            print(get_time_string() + 'Evaluating on validation set..')
            evaluateYearbookFromModel(model=model, architecture=VGG16_ARCHITECTURE, sample=sample)

            print_line()

        # model.fit(processed_train_images, train_labels,
        #           batch_size=batch_size,
        #           nb_epoch=num_epochs,
        #           shuffle=True,
        #           verbose=1, validation_data=(processed_valid_images, valid_labels),
        #           callbacks=[self.getCheckpointer(model_save_path)],
        #           initial_epoch=initial_epoch
        #           )

        print(get_time_string() + 'Fitting complete. Returning model..')

        if model_save_path is not None:
            print(get_time_string() + 'Saving final model to ' + model_save_path + '..')
            model.save(model_save_path)

        return model
Esempio n. 7
0
num_classes = 10
# nb_epoch = 100
train_size = 50000
valid_size = 10000
INIT_LR = 1e-3

# Load Cifar10 data. Please implement your own load_data() module for your own dataset
print("[INFO] loading CIFAR-10 data...")
X_train, Y_train, X_valid, Y_valid = load_cifar10_data(img_rows, img_cols,
                                                       train_size, valid_size,
                                                       num_classes)

print("Num of GPU requested", G)
if G <= 1:
    print("[INFO] training with 1 GPU...")
    model = vgg16_model(img_rows, img_cols, channel, num_classes)

# otherwise, we are compiling using multiple GPUs
else:
    print("[INFO] training with {} GPUs...".format(G))

    # we'll store a copy of the model on *every* GPU and then combine
    # the results from the gradient updates on the CPU
    with tf.device("/cpu:0"):
        # initialize the model
        model = vgg16_model(img_rows, img_cols, channel, num_classes)

    # make the model parallel
    model = multi_gpu_model(model, gpus=G)

# initialize the optimizer and model