Exemplo n.º 1
0
def main(argv):
    if not os.path.exists("./experiments/" + FLAGS.exp_name):
        os.makedirs("./experiments/" + FLAGS.exp_name)

    # 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 = 5

    train_datagen = utils.NavemDataGenerator(rescale=1. / 255)
    train_generator = train_datagen.flow_from_directory(
        os.path.sep.join(["./datasets", FLAGS.dataset, "train"]),
        shuffle=True,
        color_mode=FLAGS.img_mode,
        target_size=(FLAGS.img_width, FLAGS.img_height),
        crop_size=(FLAGS.crop_img_width, FLAGS.crop_img_height),
        batch_size=FLAGS.batch_size)

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

    val_generator = val_datagen.flow_from_directory(
        os.path.sep.join(["./datasets", FLAGS.dataset, "val"]),
        shuffle=True,
        color_mode=FLAGS.img_mode,
        target_size=(FLAGS.img_width, FLAGS.img_height),
        crop_size=(FLAGS.crop_img_width, FLAGS.crop_img_height),
        batch_size=FLAGS.batch_size)

    # Weights to restore
    weights_path = os.path.join("./experiments/" + FLAGS.exp_name,
                                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 = models.getModel(FLAGS.img_width, FLAGS.img_height, img_channels,
                            output_dim, weights_path)

    # Serialize model into json
    json_model_path = os.path.join("./experiments/" + FLAGS.exp_name,
                                   FLAGS.json_model_fname)
    utils.modelToJson(model, json_model_path)

    # Train model
    trainModel(train_generator, val_generator, model, initial_epoch)
Exemplo n.º 2
0
def main(argv):
    #python3 evaluation.py --exp_name="exp_005" --dataset="dataset_navem_224_224" --weights_fname="weights_050.h5" --batch_size=64
    #python3 evaluation.py --exp_name="exp_029" --dataset="\vgg16\sidewalk_accx" --weights_fname="model_weights_259.h5" --batch_size=64
    # Set testing mode (dropout/batchnormalization)

    ########### command aws #############
    #python3 evaluation.py --exp_name="exp_029" --dataset="sidewalk_accy_proportion_classes_20" --weights_fname="model_weights_259.h5" --batch_size=64

    TEST_PHASE = 0
    TRAIN_PHASE = 1

    K.set_learning_phase(TEST_PHASE)

    # Generate testing data
    test_datagen = utils.NavemDataGenerator(rescale=1. / 255)
    test_generator = test_datagen.flow_from_directory(
        os.path.sep.join([
            ".\\..\\datasets", "vgg16", FLAGS.dataset, FLAGS.dataset, "train"
        ]),
        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(".\\..\\experiments", FLAGS.exp_name,
                                   FLAGS.json_model_fname)
    model = utils.jsonToModel(json_model_path)

    # Load weights
    weights_load_path = os.path.join("./experiments/" + FLAGS.exp_name,
                                     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')  # change to "categorical_crossentropy"

    # 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)

    print(predictions[1])
    print(ground_truth[1])
Exemplo n.º 3
0
def main(argv):
	if not os.path.exists("./experiments/" + FLAGS.exp_name):
		os.makedirs("./experiments/" + FLAGS.exp_name)

    # 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

	print("[INFO] loading attributes...")
	##inputPath = os.path.sep.join(["./datasets/labels", FLAGS.dataset + ".txt"])
	##df = datasets.load_navem_attributes(inputPath)

	# load the house images and then scale the pixel intensities to the range [0, 1]
	print("[INFO] loading images...")
	##images = datasets.load_navem_images(df)
	##images = images / 255.0

	# partition the data into training and testing splits using 75% of
	# the data for training and the remaining 25% for testing
	#print("\n\n\n" + str(len(df)) + "| " + str(len(images)) + "\n\n\n")
	##split = train_test_split(df, images, test_size=0.20, random_state=42)
	#programPause = input("Press the <ENTER> key to continue save train and test...")
	##(trainAttrX, testAttrX, trainImagesX, testImagesX) = split

	#Save train and test state
	##utils.write_divided_dataset(os.path.join("./experiments/" + FLAGS.exp_name), trainAttrX, testAttrX)

	#Nomalization
	#trainY = (trainAttrX["gyro_z"] - trainAttrX["gyro_z"].min()) / (trainAttrX["gyro_z"].max() - trainAttrX["gyro_z"].min())
	#testY = (testAttrX["gyro_z"] - testAttrX["gyro_z"].min()) / (testAttrX["gyro_z"].max() - testAttrX["gyro_z"].min())

	##train_generator = (trainImagesX, trainAttrX["gyro_z"])
	##val_generator = (testImagesX, testAttrX["gyro_z"])

	#Data augmentation FLAGS
	#rotation_range = 0.2
	#width_shift_range = 0.2
	#height_shift_range=0.2

	train_datagen = utils.NavemDataGenerator(rescale = 1./255, rotation_range = 0.2, width_shift_range = 0.2, height_shift_range=0.2)
	train_generator = train_datagen.flow_from_directory(os.path.sep.join(["./datasets", FLAGS.dataset, "train"]),
                                                        shuffle = True,
                                                        color_mode=FLAGS.img_mode,
                                                        target_size=(FLAGS.img_width, FLAGS.img_height),
														crop_size=(FLAGS.crop_img_width, FLAGS.crop_img_height),
                                                        batch_size = FLAGS.batch_size)
	#print(train_generator.samples)



	val_datagen = utils.NavemDataGenerator(rescale=1./255, rotation_range = 0.2, width_shift_range = 0.2, height_shift_range=0.2)
	#val_datagen.fit(testImagesX)
	val_generator = val_datagen.flow_from_directory(os.path.sep.join(["./datasets", FLAGS.dataset, "val"]),
                                                        shuffle = True,
                                                        color_mode=FLAGS.img_mode,
                                                        target_size=(FLAGS.img_width, FLAGS.img_height),
														crop_size=(FLAGS.crop_img_width, FLAGS.crop_img_height),
                                                        batch_size = FLAGS.batch_size)

	# Weights to restore
	weights_path = os.path.join("./experiments/" + FLAGS.exp_name, 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 = models.getModel(FLAGS.img_width, FLAGS.img_height, img_channels, output_dim, weights_path)
	#model = models.create_cnn(64, 64, 3, regress=True)

    # Serialize model into json
	json_model_path = os.path.join("./experiments/" + FLAGS.exp_name, FLAGS.json_model_fname)
	utils.modelToJson(model, json_model_path)

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