Exemplo n.º 1
0
import re
import os
import numpy as np
import tensorflow as tf
import json

from keras import backend as K
from keras.preprocessing.image import Iterator
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.generic_utils import Progbar
from keras.models import model_from_json
import utils
import img_utils
from common_flags import FLAGS

test_datagen = utils.DroneDataGenerator(rescale=1. / 255)
y = img_utils.load_img("data/testing/HMB_3/images/1479425597710017397.jpg",
                       grayscale=True,
                       crop_size=(200, 200),
                       target_size=(320, 240))

x = test_datagen.random_transform(y, seed=None)
x = test_datagen.standardize(x)
batch_x = np.zeros((1, ) + (200, 200, 1), dtype=K.floatx())
batch_x[0] = x
json_model_path = os.path.join(FLAGS.experiment_rootdir,
                               FLAGS.json_model_fname)
model = utils.jsonToModel(json_model_path)

# Load weights
weights_load_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.weights_fname)
Exemplo n.º 2
0
def _main():
    output_dim = 2
    batch_size = 16
    experiment_rootdir = '/home/jing/PycharmProjects/dcnn/new_model'
    train_dir = '/home/jing/PycharmProjects/dcnn/training'
    val_dir = '/home/jing/PycharmProjects/dcnn/val'
    epochs = 100
    img_mode = 'rgb'
    img_channels = 3
    img_width = 320
    img_height = 240
    crop_img_height = 200
    crop_img_width = 200
    initial_epoch = 0

    # 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_data_generator = train_datagen.flow_from_directory(
        train_dir,
        shuffle=True,
        color_mode=img_mode,
        target_size=(img_width, img_height),
        crop_size=(crop_img_height, crop_img_width),
        batch_size=batch_size)

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

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

    model = resnet8(img_width, img_height, img_channels, output_dim)
    # Initialize loss weights
    model.gama = tf.Variable(1,
                             trainable=False,
                             name='alpha',
                             dtype=tf.float32)
    # Initialize number of samples for hard-mining
    model.k_mse_v = tf.Variable(batch_size,
                                trainable=False,
                                name='k_mse_v',
                                dtype=tf.int32)
    model.k_mse_x = tf.Variable(batch_size,
                                trainable=False,
                                name='k_mse_x',
                                dtype=tf.int32)
    optimizer = optimizers.Adam(decay=1e-5)

    # Configure training process
    model.compile(
        loss=[hard_mining_mse(model.k_mse_v),
              hard_mining_mse(model.k_mse_x)],
        optimizer=optimizer,
        loss_weights=[model.gama, 1])
    # Save model with the lowest validation loss
    weights_path = os.path.join(experiment_rootdir, 'weights_{epoch:03d}.h5')
    writeBestModel = ModelCheckpoint(filepath=weights_path,
                                     monitor='val_loss',
                                     save_best_only=True,
                                     save_weights_only=True)
    # Train model
    steps_per_epoch = int(np.ceil(train_data_generator.samples / batch_size))
    validation_steps = int(np.ceil(val_data_generator.samples / batch_size))
    print('Training ------------')
    model.fit_generator(train_data_generator,
                        epochs=epochs,
                        steps_per_epoch=steps_per_epoch,
                        callbacks=writeBestModel,
                        validation_data=val_data_generator,
                        validation_steps=validation_steps,
                        initial_epoch=initial_epoch)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
def _main():

    # Set testing mode (dropout/batchnormalization)
    K.set_learning_phase(TEST_PHASE)

    # Generate testing data
    test_datagen = utils.DroneDataGenerator(rescale=1. / 255)
    test_generator = test_datagen.flow_from_directory(
        FLAGS.test_dir,
        shuffle=False,
        color_mode=FLAGS.img_mode,
        target_size=(FLAGS.img_width, FLAGS.img_height),
        crop_size=(FLAGS.crop_img_height, FLAGS.crop_img_width),
        batch_size=FLAGS.batch_size)

    # Load json and create model
    json_model_path = os.path.join(FLAGS.experiment_rootdir,
                                   FLAGS.json_model_fname)
    model = utils.jsonToModel(json_model_path)

    # Load weights
    weights_load_path = os.path.join(FLAGS.experiment_rootdir,
                                     FLAGS.weights_fname)
    try:
        model.load_weights(weights_load_path)
        print("Loaded model from {}".format(weights_load_path))
    except:
        print("Impossible to find weight path. Returning untrained model")

    # Compile model
    model.compile(loss='mse', optimizer='adam')

    # Get predictions and ground truth
    n_samples = test_generator.samples
    nb_batches = int(np.ceil(n_samples / FLAGS.batch_size))

    predictions, ground_truth, t = utils.compute_predictions_and_gt(
        model, test_generator, nb_batches, verbose=1)

    # Param t. t=1 steering, t=0 collision
    t_mask = t == 1

    # ************************* Steering evaluation ***************************

    # Predicted and real steerings
    pred_steerings = predictions[t_mask, 0]
    real_steerings = ground_truth[t_mask, 0]

    # Compute random and constant baselines for steerings
    random_steerings = random_regression_baseline(real_steerings)
    constant_steerings = constant_baseline(real_steerings)

    # Create dictionary with filenames
    dict_fname = {
        'test_regression.json': pred_steerings,
        'random_regression.json': random_steerings,
        'constant_regression.json': constant_steerings
    }

    # Evaluate predictions: EVA, residuals, and highest errors
    print('direction:')
    for fname, pred in dict_fname.items():
        abs_fname = os.path.join(FLAGS.experiment_rootdir, fname)
        evaluate_regression(pred, real_steerings, abs_fname)

    # Write predicted and real steerings
    dict_test = {
        'pred_steerings': pred_steerings.tolist(),
        'real_steerings': real_steerings.tolist()
    }
    utils.write_to_file(
        dict_test,
        os.path.join(FLAGS.experiment_rootdir,
                     'predicted_and_real_steerings.json'))

    # ************************* collision(translation) evaluation ***************************

    # Predicted and real labels
    pred_prob = predictions[~t_mask, 1]
    real_labels = ground_truth[~t_mask, 1]

    # Compute random and constant baselines for steerings
    random_labels = random_regression_baseline(real_labels)
    constant_labels = constant_baseline(real_labels)

    # Create dictionary with filenames
    dict_fname = {
        'translation-test_regression.json': pred_prob,
        'translation-random_regression.json': random_labels,
        'translation-constant_regression.json': constant_labels
    }

    # Evaluate predictions: EVA, residuals, and highest errors
    print('translation:')
    for fname, pred in dict_fname.items():
        abs_fname = os.path.join(FLAGS.experiment_rootdir, fname)
        evaluate_regression(pred, real_labels, abs_fname)

    # Write predicted and real steerings
    dict_test = {
        'pred_labels': pred_prob.tolist(),
        'real_probs': real_labels.tolist()
    }
    utils.write_to_file(
        dict_test,
        os.path.join(FLAGS.experiment_rootdir,
                     'predicted_and_real_labels.json'))
Exemplo n.º 6
0
import utils
import os
import tensorflow as tf
import cnn_models
from keras import optimizers
import logz
from common_flags import FLAGS
import log_utils
from keras.callbacks import ModelCheckpoint

OUTPUT_PATH = '/home/stmoon/Project/AE590/dronet/output/'
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)