Beispiel #1
0
def _main():

    # Create the experiment rootdir if not already there
    if not os.path.exists(FLAGS.experiment_rootdir):
        os.makedirs(FLAGS.experiment_rootdir)

    # Input image dimensions
    img_width, img_height = FLAGS.img_width, FLAGS.img_height
    
    # Cropped image dimensions
    crop_img_width, crop_img_height = FLAGS.crop_img_width, FLAGS.crop_img_height

    # Image mode
    if FLAGS.img_mode=='rgb':
        img_channels = 3
    elif FLAGS.img_mode == 'grayscale':
        img_channels = 1
    else:
        raise IOError("Unidentified image mode: use 'grayscale' or 'rgb'")
        
    # Output dimension (one for steering and one for collision)
    output_dim = 1

    # Generate training data with real-time augmentation
    train_datagen = utils.DroneDataGenerator(rotation_range = 0.2,
                                             rescale = 1./255,
                                             width_shift_range = 0.2,
                                             height_shift_range=0.2)

    train_generator = train_datagen.flow_from_directory(FLAGS.train_dir,
                                                        shuffle = True,
                                                        color_mode=FLAGS.img_mode,
                                                        target_size=(img_width, img_height),
                                                        crop_size=(crop_img_height, crop_img_width),
                                                        batch_size = FLAGS.batch_size)

    # Generate validation data with real-time augmentation
    val_datagen = utils.DroneDataGenerator(rescale = 1./255)

    val_generator = val_datagen.flow_from_directory(FLAGS.val_dir,
                                                        shuffle = True,
                                                        color_mode=FLAGS.img_mode,
                                                        target_size=(img_width, img_height),
                                                        crop_size=(crop_img_height, crop_img_width),
                                                        batch_size = FLAGS.batch_size)

    # Weights to restore
    weights_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.weights_fname)
    initial_epoch = 0
    if not FLAGS.restore_model:
        # In this case weights will start from random
        weights_path = None
    else:
        # In this case weigths will start from the specified model
        initial_epoch = FLAGS.initial_epoch

    # Define model
    model = getModel(crop_img_width, crop_img_height, img_channels,
                        output_dim, weights_path)

    # Save the architecture of the model as png
    plot_arch_path = os.path.join(FLAGS.experiment_rootdir, 'architecture.png')
    plot_model(model, to_file=plot_arch_path)

    # Serialize model into json
    json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
    utils.modelToJson(model, json_model_path)

    # Train model
    trainModel(train_generator, val_generator, model, initial_epoch)
def _main():

    # Set random seed
    if FLAGS.random_seed:
        seed = np.random.randint(0, 2 * 31 - 1)
    else:
        seed = 5
    np.random.seed(seed)
    tf.set_random_seed(seed)

    # Set training phase
    K.set_learning_phase(TRAIN_PHASE)

    # Create the experiment rootdir if not already there: create a model if the name of the one in the parameters doesn't exist
    if not os.path.exists(FLAGS.experiment_rootdir):
        os.makedirs(FLAGS.experiment_rootdir)

    # Split the database into training, validation and test sets
    data_utils_mod.cross_val_create(FLAGS.data_path)

    # Input image dimensions
    num_img, img_width, img_height = FLAGS.num_img, FLAGS.img_width, FLAGS.img_height

    # Output dimension (4 classes/gestures)
    num_classes = 4

    # Generate training data with real-time augmentation: HABRÁ QUE CAMBIARLO
    # TODO LO QUE SE HAGA SOBRE LOS DATOS AQUI SE TENDRA QUE HACER EN LOS DATOS PARA EL TEST
    train_datagen = data_utils_mod.DataGenerator(rescale=1. / 255)

    # Iterator object containing training data to be generated batch by batch
    train_generator = train_datagen.flow_from_directory(
        'train',  #FLAGS.train_dir,
        num_classes,
        shuffle=True,
        img_mode=FLAGS.img_mode,
        target_size=(num_img, img_height, img_width),
        batch_size=FLAGS.batch_size)

    # Check if the number of classes in dataset corresponds to the one specified
    assert train_generator.num_classes == num_classes, \
                        " Not macthing output dimensions in training data."

    # Generate validation data with real-time augmentation
    val_datagen = data_utils_mod.DataGenerator(rescale=1. / 255)

    # Iterator object containing validation data to be generated batch by batch
    val_generator = val_datagen.flow_from_directory(
        'val',  #FLAGS.val_dir,
        num_classes,
        shuffle=False,
        img_mode=FLAGS.img_mode,
        target_size=(num_img, img_height, img_width),
        batch_size=FLAGS.batch_size)

    # Check if the number of classes in dataset corresponds to the one specified
    assert val_generator.num_classes == num_classes, \
                        " Not matching output dimensions in validation data."

    # Weights to restore
    weights_path = FLAGS.initial_weights

    # Epoch from which training starts
    initial_epoch = 0
    if not FLAGS.restore_model:
        # In this case weights are initialized randomly
        weights_path = None
    else:
        # In this case weigths are initialized as specified in pre-trained model
        initial_epoch = FLAGS.initial_epoch

    # Define model: SE DEFINE LA RED CON LOS PARAMETROS QUE QUERAMOS O SOBRE LOS PESOS QUE YA TENGAMOS ANTES
    n = 1
    version = 2  # 1 o 2
    model = getModelResnet(n, version, num_img, img_height, img_width,
                           num_classes, weights_path)

    # Save the architecture of the network as png: IMAGEN DE LA ESTRUCTURA DE LA RED
    plot_arch_path = os.path.join(FLAGS.experiment_rootdir, 'architecture.png')
    plot_model(model, to_file=plot_arch_path)

    # Serialize model into json: SE GUARDA EL "ESQUELETO" DE LA RED: NUMERO DE CAPAS, NEURONAS... LOS PESOS SE GUARDAN A PARTE
    json_model_path = os.path.join(FLAGS.experiment_rootdir,
                                   FLAGS.json_model_fname)
    utils.modelToJson(model, json_model_path)

    # Train model
    trainModel(train_generator, val_generator, model, initial_epoch)

    # Plot training and validation losses
    utils.plot_loss(FLAGS.experiment_rootdir)
Beispiel #3
0
def _main():
    if not os.path.exists(FLAGS.experiment_rootdir):
        os.makedirs(FLAGS.experiment_rootdir)
    base_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

    # Print mobilenet summary

    x=base_model.output
    x=GlobalAveragePooling2D()(x)
    x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
    x=Dense(1024,activation='relu')(x) #dense layer 2
    x=Dense(512,activation='relu')(x) #dense layer 3
    steering=Dense(1,activation='softmax')(x) #final layer with softmax activation
    collision=Dense(1,activation='softmax')(x) #final layer with softmax activation

    model=Model(inputs=base_model.input,outputs=[steering,collision])

    # Print mobilenet summary
    for i,layer in enumerate(model.layers[:(len(model.layers)-4)]):
            print('Setting as non-trainable',i,layer.name)
            layer.trainable=False

    for i,layer in enumerate(model.layers[(len(model.layers)-4):]):
            print('Setting as trainable',i,layer.name)
            layer.trainable=True


    crop_img_width, crop_img_height = FLAGS.crop_img_width, FLAGS.crop_img_height
    img_width, img_height = FLAGS.img_width, FLAGS.img_height
    # Generate training data with real-time augmentation
    train_datagen = utils.DroneDataGenerator(rotation_range = 0.2,
                                            rescale = 1./255,
                                            width_shift_range = 0.2, 
                                            height_shift_range=0.2)

    train_generator = train_datagen.flow_from_directory(FLAGS.train_dir,
                                                        shuffle = True,
                                                        color_mode='rgb',
                                                        target_size=(img_width, img_height),
                                                        crop_size=(crop_img_height, crop_img_width),
                                                        batch_size = FLAGS.batch_size)

    val_datagen = utils.DroneDataGenerator(rescale = 1./255)

    val_generator = val_datagen.flow_from_directory(FLAGS.val_dir,
                                                    shuffle = True,
                                                    color_mode='rgb',
                                                    target_size=(img_width, img_height),
                                                    crop_size=(crop_img_height, crop_img_width),
                                                    batch_size = FLAGS.batch_size)
    
    # Serialize model into json
    json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
    utils.modelToJson(model, json_model_path)
    
    initial_epoch = FLAGS.initial_epoch
    
    model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])

    #step_size_train=train_generator.samples//FLAGS.batch_size
    step_size_train = int(np.ceil(train_generator.samples / FLAGS.batch_size))
    validation_steps = int(np.ceil(val_generator.samples / FLAGS.batch_size))
    model.fit_generator(train_generator,
                        steps_per_epoch=step_size_train,
                        epochs=FLAGS.epochs,
                        validation_data=val_generator,
                        validation_steps = validation_steps,
                        initial_epoch=initial_epoch)
Beispiel #4
0
def _main():
    
    # Set random seed
    if FLAGS.random_seed:
        seed = np.random.randint(0,2*31-1)
    else:
        seed = 5
    np.random.seed(seed)
    tf.set_random_seed(seed)

    # Set training phase
    K.set_learning_phase(TRAIN_PHASE)

    # Create the experiment rootdir if not already there
    if not os.path.exists(FLAGS.experiment_rootdir):
        os.makedirs(FLAGS.experiment_rootdir)

    # Input image dimensions
    img_width, img_height = FLAGS.img_width, FLAGS.img_height

    
    # Image mode (RGB or grayscale)
    if FLAGS.img_mode=='rgb':
        img_channels = 3
    elif FLAGS.img_mode == 'grayscale':
        img_channels = 1
    else:
        raise IOError("Unidentified image mode: use 'grayscale' or 'rgb'")

    # Output dimension (empty place probability)
    output_dim=1

    # Generate training data with real-time augmentation
    train_datagen = data_utils.DataGenerator(rescale = 1./255)
    
    # Iterator object containing training data to be generated batch by batch
    train_generator = train_datagen.flow_from_directory(FLAGS.train_dir,
                                                        output_dim,
                                                        shuffle = True,
                                                        img_mode = FLAGS.img_mode,
                                                        target_size=(img_height, img_width),
                                                        batch_size = FLAGS.batch_size)
    
    # Check if the number of classes in dataset corresponds to the one specified                                                    
#    assert train_generator.num_classes == num_classes, \
#                        " Not macthing output dimensions in training data."                                                    


    # Generate validation data with real-time augmentation
    val_datagen = data_utils.DataGenerator(rescale = 1./255)
    
    # Iterator object containing validation data to be generated batch by batch
    val_generator = val_datagen.flow_from_directory(FLAGS.val_dir,
                                                    output_dim,
                                                    shuffle = False,
                                                    img_mode = FLAGS.img_mode,
                                                    target_size=(img_height, img_width),
                                                    batch_size = FLAGS.batch_size)

    # Check if the number of classes in dataset corresponds to the one specified
#    assert val_generator.num_classes == num_classes, \
#                        " Not macthing output dimensions in validation data."
                        

    # Weights to restore
    weights_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.weights_fname)
    
    # Epoch from which training starts
    initial_epoch = 0
    if not FLAGS.restore_model:
        # In this case weights are initialized randomly
        weights_path = None
    else:
        # In this case weigths are initialized as specified in pre-trained model
        initial_epoch = FLAGS.initial_epoch

    # Define model
    model = getModelResnet(FLAGS.n_layers, FLAGS.rn_version, img_width, img_height, img_channels,
                        output_dim, weights_path)  

    # Serialize model into json
    json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
    utils.modelToJson(model, json_model_path)

    # Train model
    trainModel(train_generator, val_generator, model, initial_epoch)
    
    # Plot training and validation losses
    utils.plot_loss(FLAGS.experiment_rootdir)
Beispiel #5
0
TRAIN_DATA_PATH = '/home/stmoon/Project/AE590/deeplab/out'
TEST_DATA_PATH = '/home/stmoon/Project/AE590/deeplab/test_data'

train_datagen = utils.DroneDataGenerator(rotation_range=0.2,
                                         rescale=1. / 255,
                                         width_shift_range=0.2,
                                         height_shift_range=0.2)

train_data_generator = train_datagen.flow_from_directory(TRAIN_DATA_PATH)
test_data_generator = train_datagen.flow_from_directory(TEST_DATA_PATH)

model = cnn_models.resnet8(700, 500, 1, 1)

# Serialize model into json
json_model_path = os.path.join(OUTPUT_PATH, "model.json")
utils.modelToJson(model, json_model_path)

optimizer = optimizers.Adam(decay=1e-5)

model.alpha = tf.Variable(1, trainable=False, name='alpha', dtype=tf.float32)
#model.beta  = tf.Variable(0, trainable=False, name='beta', dtype=tf.float32)

# Initialize number of samples for hard-mining
model.k_mse = tf.Variable(32, trainable=False, name='k_mse', dtype=tf.int32)
#model.k_entropy = tf.Variable(32, trainable=False, name='k_entropy', dtype=tf.int32)

model.compile(
    loss=[utils.hard_mining_mse(model.k_mse)],
    #utils.hard_mining_entropy(model.k_entropy)],
    optimizer=optimizer,
    loss_weights=[model.alpha])