def iterative_prune_model():
    # build the inception v3 network
    base_model = inception_v3.InceptionV3(include_top=False,
                                          weights='imagenet',
                                          pooling='avg',
                                          input_shape=(299, 299, 3))
    print('Model loaded.')

    top_output = Dense(5, activation='softmax')(base_model.output)

    # add the model on top of the convolutional base
    model = Model(base_model.inputs, top_output)
    del base_model
    model.load_weights(tuned_weights_path)
    # compile the model with a SGD/momentum optimizer
    # and a very slow learning rate.
    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(lr=1e-4, momentum=0.9),
                  metrics=['accuracy'])

    # Set up data generators
    train_datagen = ImageDataGenerator(
        preprocessing_function=inception_v3.preprocess_input,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')
    train_steps = train_generator.n // train_generator.batch_size

    test_datagen = ImageDataGenerator(
        preprocessing_function=inception_v3.preprocess_input)

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_height, img_width),
        batch_size=val_batch_size,
        class_mode='categorical')
    val_steps = validation_generator.n // validation_generator.batch_size

    # Evaluate the model performance before pruning
    loss = model.evaluate_generator(validation_generator,
                                    validation_generator.n //
                                    validation_generator.batch_size)
    print('original model validation loss: ', loss[0], ', acc: ', loss[1])

    total_channels = get_total_channels(model)
    n_channels_delete = int(math.floor(percent_pruning / 100 * total_channels))

    # Incrementally prune the network, retraining it each time
    percent_pruned = 0
    # If percent_pruned > 0, continue pruning from previous checkpoint
    if percent_pruned > 0:
        checkpoint_name = ('inception_flowers_pruning_' + str(percent_pruned)
                           + 'percent')
        model = load_model(output_dir + checkpoint_name + '.h5')

    while percent_pruned <= total_percent_pruning:
        # Prune the model
        apoz_df = get_model_apoz(model, validation_generator)
        percent_pruned += percent_pruning
        print('pruning up to ', str(percent_pruned),
              '% of the original model weights')
        model = prune_model(model, apoz_df, n_channels_delete)

        # Clean up tensorflow session after pruning and re-load model
        checkpoint_name = ('inception_flowers_pruning_' + str(percent_pruned)
                           + 'percent')
        model.save(output_dir + checkpoint_name + '.h5')
        del model
        tensorflow.python.keras.backend.clear_session()
        tf.reset_default_graph()
        model = load_model(output_dir + checkpoint_name + '.h5')

        # Re-train the model
        model.compile(loss='categorical_crossentropy',
                      optimizer=SGD(lr=1e-4, momentum=0.9),
                      metrics=['accuracy'])
        checkpoint_name = ('inception_flowers_pruning_' + str(percent_pruned)
                           + 'percent')
        csv_logger = CSVLogger(output_dir + checkpoint_name + '.csv')
        model.fit_generator(train_generator,
                            steps_per_epoch=train_steps,
                            epochs=epochs,
                            validation_data=validation_generator,
                            validation_steps=val_steps,
                            workers=4,
                            callbacks=[csv_logger])

    # Evaluate the final model performance
    loss = model.evaluate_generator(validation_generator,
                                    validation_generator.n //
                                    validation_generator.batch_size)
    print('pruned model loss: ', loss[0], ', acc: ', loss[1])
Пример #2
0
            use_multiprocessing=False,
            workers=10,
            callbacks=[
                ModelCheckpoint(
                    '%s/model_{epoch:d}_loss{loss:.4f}_acc_{acc:.4f}.h5' % folder,
                    monitor='val_loss',
                    verbose=1,
                    save_best_only=False,
                    save_weights_only=True,
                    mode='auto',
                    period=1),
                CSVLogger(logger, append=log_append),
                lr_scheduler(initial_lr=3e-4, decay_factor=0.75, step_size=5, min_lr=1e-8)
            ],
            max_queue_size=30,
        )
    elif mode == 'testing':
        adam = tf.train.AdamOptimizer(learning_rate=1e-3)
        model.compile(loss="categorical_crossentropy", optimizer=adam, metrics=['accuracy'])

        res = model.evaluate_generator(
            test_sequence,
            verbose=1
        )
        print(model.metrics_names)
        print(res)
    elif mode == 'predict':
        pass
    else:
        raise ValueError('')
Пример #3
0
              metrics=['accuracy'])
"""
8. 重新训练组合的新模型,仍然保持 VGG16 的 15 个最低层处于冻结状态。在这个特定的例子中,也使用 Image Augumentator 来增强训练集:
"""
train_datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                    target_size=(img_height,
                                                                 img_width),
                                                    batch_size=batch_size,
                                                    class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary',
    shuffle=False)
model.fit_generator(train_generator,
                    steps_per_epoch=nb_train_samples // batch_size,
                    epochs=epochs,
                    validation_data=nb_validation_samples // batch_size,
                    verbose=2,
                    workers=12)
"""
9. 在组合网络上评估结果
"""
score = model.evaluate_generator(validation_generator,
                                 nb_validation_samples / batch_size)
scores = model.predict_generator(validation_generator,
                                 nb_validation_samples / batch_size)
# print(score, scores)
def tune_model():
    # Build the Inception V3 network.
    base_model = inception_v3.InceptionV3(include_top=False,
                                          weights='imagenet',
                                          pooling='avg')
    print('Model loaded.')

    # build a classifier model to put on top of the convolutional model
    top_input = Input(shape=base_model.output_shape[1:])
    top_output = Dense(5, activation='softmax')(top_input)
    top_model = Model(top_input, top_output)

    # Note that it is necessary to start with a fully-trained classifier,
    # including the top classifier, in order to successfully do fine-tuning.
    top_model.load_weights(top_model_weights_path)

    # add the model on top of the convolutional base
    model = Model(inputs=base_model.inputs,
                  outputs=top_model(base_model.outputs))

    # Set all layers up to 'mixed8' to non-trainable (weights will not be updated)
    last_train_layer = model.get_layer(name='mixed8')
    for layer in model.layers[:model.layers.index(last_train_layer)]:
        layer.trainable = False

    # Compile the model with a SGD/momentum optimizer and a very slow learning rate.
    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(lr=1e-4, momentum=0.9),
                  metrics=['accuracy'])

    # Prepare data augmentation configuration
    train_datagen = ImageDataGenerator(
        preprocessing_function=inception_v3.preprocess_input,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

    test_datagen = ImageDataGenerator(
        preprocessing_function=inception_v3.preprocess_input)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')

    loss = model.evaluate_generator(validation_generator,
                                    nb_validation_samples // batch_size)
    print('Model validation performance before fine-tuning:', loss)

    csv_logger = CSVLogger(output_dir + 'model_tuning.csv')
    # fine-tune the model
    model.fit_generator(train_generator,
                        steps_per_epoch=nb_train_samples // batch_size,
                        epochs=tune_epochs,
                        validation_data=validation_generator,
                        validation_steps=nb_validation_samples // batch_size,
                        workers=4,
                        callbacks=[csv_logger])
    model.save_weights(tuned_weights_path)
Пример #5
0
def train_validate_test(train_files, valid_files, test_files, dims, extension):
    """
    Trains and validates a model using Keras.
    """
    def make_iterator(dataset):
        iterator = dataset.make_one_shot_iterator()
        next_val = iterator.get_next()

        with backend.get_session().as_default() as sess:
            while True:
                inputs, labels = sess.run(next_val)
                yield inputs, labels

    nclasses, nepochs, batch_size = 12, 10, 128

    npics_train = 0
    for filename in train_files:
        for record in tf.python_io.tf_record_iterator(filename):
            npics_train += 1
    steps_per_epoch_train = int(
        (npics_train + batch_size - 1) / batch_size) - 1

    npics_valid = 0
    for filename in valid_files:
        for record in tf.python_io.tf_record_iterator(filename):
            npics_valid += 1
    steps_per_epoch_valid = int(
        (npics_valid + batch_size - 1) / batch_size) - 1
    print("steps:", steps_per_epoch_valid, npics_valid)

    npics_test = 0
    for filename in test_files:
        for record in tf.python_io.tf_record_iterator(filename):
            npics_test += 1
    steps_test = int((npics_test + batch_size - 1) / batch_size) - 1
    print("steps:", steps_test, npics_test)

    train_dataset = BData().load_tfrec_bonsai(train_files, dims)
    train_dataset = train_dataset.shuffle(buffer_size=npics_train)
    train_dataset = train_dataset.repeat(2 * nepochs)
    train_dataset = train_dataset.batch(batch_size)
    train_iterator = make_iterator(train_dataset)

    valid_dataset = BData().load_tfrec_bonsai(valid_files, dims)
    valid_dataset = valid_dataset.shuffle(buffer_size=npics_valid)
    valid_dataset = valid_dataset.repeat(2 * nepochs)
    valid_dataset = valid_dataset.batch(batch_size)
    valid_iterator = make_iterator(valid_dataset)

    test_dataset = BData().load_tfrec_bonsai(test_files, dims)
    test_dataset = test_dataset.shuffle(buffer_size=npics_test)
    test_dataset = test_dataset.repeat(2 * nepochs)
    test_dataset = test_dataset.batch(batch_size)
    test_iterator = make_iterator(test_dataset)

    #x_train, y_train = train_iterator.get_next()
    #print("y_train shape:", y_train.shape)

    if backend.image_data_format() == 'channels_first':
        input_shape = (dims[2], dims[0], dims[1])
    else:
        input_shape = (dims[0], dims[1], dims[2])

    #model_input = layers.Input(tensor=x_train, shape=input_shape)
    model_input = layers.Input(shape=input_shape)
    model_output = nn(model_input, input_shape, nclasses)
    model = Model(inputs=model_input, outputs=model_output)
    plot_model(model,
               to_file='model_plot.png',
               show_shapes=True,
               show_layer_names=True)

    def custom_accuracy(y_true, y_pred):
        y_true_1 = tf.slice(y_true, [0, 0], [batch_size, 6])
        y_true_2 = tf.slice(y_true, [0, 6], [batch_size, 6])
        y_pred_1 = tf.slice(y_pred, [0, 0], [batch_size, 6])
        y_pred_2 = tf.slice(y_pred, [0, 6], [batch_size, 6])

        equal = backend.equal(
            backend.equal(backend.argmax(y_true_1, axis=-1),
                          backend.argmax(y_pred_1, axis=-1)),
            backend.equal(backend.argmax(y_true_2, axis=-1),
                          backend.argmax(y_pred_2, axis=-1)))
        return backend.mean(equal)

    model.compile(optimizer='adam',
                  loss=categorical_crossentropy,
                  metrics=[custom_accuracy, 'categorical_accuracy'])
    #target_tensors=[y_train])
    model.summary()
    callbacks = [  #EarlyStopping(monitor='loss', min_delta=0.000005, patience=5),
        #ModelCheckpoint(FLAGS.save_model_name, verbose=1, period=1),
        TensorBoard(log_dir=FLAGS.tensorboard, batch_size=batch_size),
        WriteMetrics(FLAGS.file_metrics)
    ]  #custom callback
    model.fit_generator(generator=train_iterator,
                        validation_data=valid_iterator,
                        validation_steps=steps_per_epoch_valid,
                        epochs=nepochs,
                        steps_per_epoch=steps_per_epoch_train,
                        callbacks=callbacks,
                        verbose=1,
                        workers=0)
    model.save(FLAGS.save_model_name)
    test_results = model.evaluate_generator(generator=test_iterator,
                                            steps=steps_test,
                                            max_queue_size=10,
                                            workers=0,
                                            use_multiprocessing=False)
    print('Test loss:', test_results[0])
    print('Test accuracy:', test_results[1])
Пример #6
0
def train_top_model_densenet121():



    # DesneNet generators

    # Set up generators
    train_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        train_path,
        target_size=(image_size, image_size),
        batch_size=train_batch_size)

    valid_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        valid_path,
        target_size=(image_size, image_size),
        batch_size=val_batch_size)

    test_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        test_path,
        target_size=(image_size, image_size),
        batch_size=test_batch_size,
        shuffle=False)

    input_tensor = Input(shape = (224,224,3))

    #Loading the model
    model = DenseNet121(input_tensor= input_tensor,weights='imagenet',include_top=False)

    # add a global spatial average pooling layer
    x = model.output
    x = GlobalAveragePooling2D()(x)
    # add relu layer
    x = Dense(1024, activation='relu')(x)
    # and a softmax layer for 7 classes
    predictions = Dense(7, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=model.input, outputs=predictions)


    def top_3_accuracy(y_true, y_pred):
        return top_k_categorical_accuracy(y_true, y_pred, k=3)

    def top_2_accuracy(y_true, y_pred):
        return top_k_categorical_accuracy(y_true, y_pred, k=2)


    model.compile(optimizer=optimizers.SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False),
                          loss="categorical_crossentropy",
                          metrics=[categorical_accuracy, top_2_accuracy, top_3_accuracy])

    print(model.summary())

    # Declare a checkpoint to save the best version of the model
    checkpoint = ModelCheckpoint("modelDenseNet121.h5", monitor='val_categorical_accuracy', verbose=1,
                                 save_best_only=True, mode='max')

    # Reduce the learning rate as the learning stagnates
    reduce_lr = ReduceLROnPlateau(monitor='val_categorical_accuracy', factor=0.5, patience=2,
                                  verbose=1, mode='max', min_lr=0.00001)

    early_stopping = EarlyStopping(monitor='val_categorical_accuracy', patience=10, verbose=1, mode='max')

    callbacks_list = [checkpoint, reduce_lr,
                      early_stopping
                      ]

    history = model.fit_generator(train_batches,
                                  # class_weight = class_weights,
                                  epochs=epochs, shuffle=True, validation_data = valid_batches, steps_per_epoch=train_steps, validation_steps = val_steps,  verbose=1, callbacks=callbacks_list)


    # # Evaluation of the best epoch
    model.load_weights('modelDenseNet.h5')

    val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \
        model.evaluate_generator(valid_batches, steps=val_steps)

    print('val_loss:', val_loss)
    print('val_cat_acc:', val_cat_acc)
    print('val_top_2_acc:', val_top_2_acc)
    print('val_top_3_acc:', val_top_3_acc)
Пример #7
0
def train_top_model_resnet():

    # ResNet50 generators

    # Set up generators
    train_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.resnet50.preprocess_input).flow_from_directory(
        train_path,
        target_size=(image_size, image_size),
        batch_size=train_batch_size)

    valid_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.resnet50.preprocess_input).flow_from_directory(
        valid_path,
        target_size=(image_size, image_size),
        batch_size=val_batch_size)

    test_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.resnet50.preprocess_input).flow_from_directory(
        test_path,
        target_size=(image_size, image_size),
        batch_size=test_batch_size,
        shuffle=False)

    input_tensor = Input(shape = (224,224,3))

    #Loading the model
    model = ResNet50(input_tensor= input_tensor,weights='imagenet',include_top=False)


    x = model.output
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    x = Dropout(0.5)(x)
    predictions = tf.keras.layers.Dense(7,activation ='softmax')(x)

    model = Model(inputs=model.input, outputs=predictions)


    def top_3_accuracy(y_true, y_pred):
        return top_k_categorical_accuracy(y_true, y_pred, k=3)

    def top_2_accuracy(y_true, y_pred):
        return top_k_categorical_accuracy(y_true, y_pred, k=2)

    model.compile(optimizer=optimizers.SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False),
                          loss="categorical_crossentropy",
                          metrics=[categorical_accuracy, top_2_accuracy, top_3_accuracy, "accuracy"])

    print(model.summary())

    # Declare a checkpoint to save the best version of the model
    checkpoint = ModelCheckpoint("modelResNet2.h5", monitor='val_categorical_accuracy', verbose=1,
                                 save_best_only=True, mode='max')

    # Reduce the learning rate as the learning stagnates
    reduce_lr = ReduceLROnPlateau(monitor='val_categorical_accuracy', factor=0.5, patience=2,
                                  verbose=1, mode='max', min_lr=0.00001)

    early_stopping = EarlyStopping(monitor='val_categorical_accuracy', patience=10, verbose=1, mode='max')

    callbacks_list = [checkpoint, reduce_lr,
                      early_stopping
                      ]

    history = model.fit_generator(train_batches, epochs=epochs, shuffle=True, validation_data = valid_batches, steps_per_epoch=train_steps, validation_steps = val_steps,  verbose=1, callbacks=callbacks_list)


    try:

        # # Evaluation of the best epoch
        model.load_weights('modelResNet.h5')

        val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \
            model.evaluate_generator(valid_batches, steps=val_steps)

        print('val_loss:', val_loss)
        print('val_cat_acc:', val_cat_acc)
        print('val_top_2_acc:', val_top_2_acc)
        print('val_top_3_acc:', val_top_3_acc)

        # summarize history for accuracy
        plt.plot(history.history['acc'])
        plt.plot(history.history['val_acc'])
        plt.title('model accuracy')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.legend(['train', 'test'], loc='upper left')
        plt.savefig("ResNet50_accuracy_training_plot.png")
        # summarize history for loss
        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'test'], loc='upper left')
        plt.savefig("ResNet50_loss_training_plot.png")
    except :
        print("Error")