Exemplo n.º 1
0
    def __init__(self, dir_z, dir_x, nb_channels=3):
        assert get_nb_files(dir_z) == get_nb_files(dir_x)  #如果条件不满足,返回错误
        assert nb_channels in [1, 3]

        self.nb_files = get_nb_files(dir_z)

        self.nb_channels = nb_channels

        self.dir_z = dir_z
        self.dir_x = dir_x
    def __init__(self, dir_z, dir_x, nb_channels=3):
        assert get_nb_files(dir_z) == get_nb_files(dir_x)
        assert nb_channels in [1, 3]

        self.nb_files = get_nb_files(dir_z)

        self.nb_channels = nb_channels

        self.dir_z = dir_z
        self.dir_x = dir_x
Exemplo n.º 3
0
    def __init__(self, dir_z, dir_x, nb_channels=3):
        assert get_nb_files(dir_z) == get_nb_files(
            dir_x), "data infomation: {}".format(dir_z)
        assert nb_channels in [1, 3]
        self.firstchannel = net_config['first_channels']
        self.nb_files = get_nb_files(dir_z)
        self.nb_channels = nb_channels

        self.dir_z = dir_z
        self.dir_x = dir_x
        self.filename = os.listdir(dir_x)
Exemplo n.º 4
0
    def __init__(self, dir_z, dir_x, nb_channels=3):

        # check the number of files in X(means original image) and Z(the implicit vector after vector) are equal?
        assert get_nb_files(dir_z) == get_nb_files(dir_x), \
            "The files numbers are not equal. \n Please check infomation in {0} and {1}\n".format(dir_z, dir_x)
        assert nb_channels in [1, 3]

        self.nb_files = get_nb_files(dir_z)

        self.nb_channels = nb_channels

        self.dir_z = dir_z
        self.dir_x = dir_x
        self.filename = os.listdir(dir_x)
 def __init__(self, dir_x, nb_channels=3):
     assert nb_channels in [1, 3]
     self.nb_channels = nb_channels
     self.nb_files = get_nb_files(dir_x)
     self.dir_x = dir_x
Exemplo n.º 6
0
def TransferLearning(args):
    """
    Performs training.
    """
    train_dir = args.train_folder
    val_dir = args.validation_folder
    nb_train_samples = utils.get_nb_files(args.train_folder)
    nb_val_samples = utils.get_nb_files(args.validation_folder)
    nb_classes = utils.get_labels(args.train_folder, args.validation_folder)
    nb_epochs = int(args.nb_epoch)
    batch_size = int(args.batch_size)
    base_architecture = args.base_architecture
    model_load = args.model_load
    # Define base layer
    if base_architecture == 'VGG16':
        base_model = applications.VGG16(weights='imagenet', include_top=False)
        layers_to_freeze = 10
    elif base_architecture == 'VGG19':
        base_model = applications.VGG19(weights='imagenet', include_top=False)
        layers_to_freeze = 11
    elif base_architecture == 'InceptionV3':
        base_model = applications.InceptionV3(weights='imagenet', include_top=False)
        layers_to_freeze = 172 # TODO: understand how many levels!
    elif base_architecture == 'ResNet50':
        base_model = applications.ResNet50(weights='imagenet', include_top=False)
    elif base_architecture == 'Xception':
        base_model = applications.Xception(weights='imagenet', include_top=False)

    model = replace_classification_layer(base_model, nb_classes, 1024)
    
    train_datagen = ImageDataGenerator(
        rescale = 1./255,
        fill_mode='nearest')
    test_datagen = ImageDataGenerator(
        rescale = 1./255,
        fill_mode='nearest')

    train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')
    validation_generator = test_datagen.flow_from_directory(
        val_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')
    

    transfer_learning(base_model, model, model_load)

    history_tl = model.fit_generator(
        train_generator,
        nb_epoch=nb_epochs,
        samples_per_epoch=nb_train_samples,
        validation_data=validation_generator,
        nb_val_samples=nb_val_samples,
        class_weight='auto',
        callbacks=args.callbacks)
    
    utils.plot_training(history_tl)

    setup_fine_tuning(model, layers_to_freeze, model_load)

    history_ft = model.fit_generator(
        train_generator,
        samples_per_epoch=nb_train_samples,
        nb_epoch=nb_epochs,
        validation_data=validation_generator,
        nb_val_samples=nb_val_samples,
        class_weight='auto',
        callbacks=args.callbacks)
    
    # NOTE
    model.save(os.path.join(os.getcwd(), 'models', args.output_model_file))

    utils.plot_training(history_ft)