예제 #1
0
import math
import helper
import posenet
import numpy as np
from keras.optimizers import Adam

if __name__ == "__main__":
    # Test model
    model = posenet.create_posenet()
    model.load_weights('trained_weights.h5')
    adam = Adam(lr=0.0001,
                beta_1=0.9,
                beta_2=0.999,
                epsilon=1e-08,
                decay=0.0,
                clipvalue=2.0)
    model.compile(optimizer=adam,
                  loss={
                      'cls1_fc_pose_xyz': posenet.euc_loss1x,
                      'cls1_fc_pose_wpqr': posenet.euc_loss1q,
                      'cls2_fc_pose_xyz': posenet.euc_loss2x,
                      'cls2_fc_pose_wpqr': posenet.euc_loss2q,
                      'cls3_fc_pose_xyz': posenet.euc_loss3x,
                      'cls3_fc_pose_wpqr': posenet.euc_loss3q
                  })

    dataset_train, dataset_test = helper.getKings()

    X_test = np.squeeze(np.array(dataset_test.images))
    y_test = np.squeeze(np.array(dataset_test.poses))
예제 #2
0
import helper
import posenet
import numpy as np
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint

if __name__ == "__main__":
    # Variables
    batch_size = 75

    # Train model
    model = posenet.create_posenet('posenet.npy',
                                   True)  # GoogLeNet (Trained on Places)
    adam = Adam(lr=0.001, clipvalue=1.5)
    model.compile(optimizer=adam,
                  loss={
                      'cls1_fc_pose_xyz': posenet.euc_loss1x,
                      'cls1_fc_pose_wpqr': posenet.euc_loss1q,
                      'cls2_fc_pose_xyz': posenet.euc_loss2x,
                      'cls2_fc_pose_wpqr': posenet.euc_loss2q,
                      'cls3_fc_pose_xyz': posenet.euc_loss3x,
                      'cls3_fc_pose_wpqr': posenet.euc_loss3q
                  })

    dataset_train, dataset_test = helper.getKings()

    X_train = np.squeeze(np.array(dataset_train.images))
    y_train = np.squeeze(np.array(dataset_train.poses))

    y_train_x = y_train[:, 0:3]
    y_train_q = y_train[:, 3:7]
예제 #3
0
def test(TEST_ALL_IMAGES):
    # 	root_path = '/ssd/data/fxpal_dataset/posenetDataset/posenet_brendan/posenet_data/'
    # 	model_name = '2017_09_04_22_37_train_kin_no_drift'
    # 	model_path = root_path + 'model/' + model_name + '_posenet.h5'

    ### NEW MODELS ###
    root_path = '/ssd/posenet_training_output/'
    model_name = '2018_11_09_00_00_train_train_data_training_largedataset_w_halloweendecor_rotated_BN_more_oct2018'
    model_path = root_path + 'weights/checkpoint_weights/' + model_name + '_posenet.h5'

    data_dict = readDataFromFile(
        root_path +
        'training_data_info/{}_posenet_data.h5py'.format(model_name))

    # Test model
    model = posenet.create_posenet()
    if 0:
        model_json = model.to_json()
        with open(model_name + '_model_json_file.json', 'w') as f:
            f.write(model_json)

    graph = tf.get_default_graph()
    # load model weight
    model.load_weights(model_path)
    # get image mean value
    image_mean = data_dict['image_mean']
    # print('image mean values before loading: {}'.format(image_mean) )
    image_mean = image_mean.flatten()
    image_mean = image_mean.tolist()
    with open(model_name + '_image_mean.json', 'w') as outfile:
        json.dump(image_mean, outfile)

    image_mean = load_image_mean(model_name)
    # read file to get image file name and ground truth pose
    # test data path
    if TEST_ALL_IMAGES:
        test_data_directory = '/home/mitesh/Documents/keras_to_tensorflow/'
        fread = open(test_data_directory + 'test_images_new/groundTruth.csv',
                     'r')
    else:
        test_image_name = '/home/mitesh/Documents/keras_to_tensorflow/test_images_new/2300_image_save.png'

    ## save results to file
    if TEST_ALL_IMAGES:
        fwrite = open('results' + model_name + '.csv', 'w')
        for string in fread:
            groundtruth = []
            temp = re.split("[  ' \n ,]+", string)
            # temp = string.split(',')
            img = cv2.imread(test_data_directory + temp[0])
            fwrite.write(temp[0] + ',')
            for idx in range(1, len(temp) - 1):
                groundtruth.append(float(temp[idx]))
                fwrite.write(temp[idx] + ',')

            ########### predict
            # Downsize the image and crop
            image = subtractMean(img, image_mean)

            images = np.empty((1, 224, 224, 3), dtype=float)
            images[0] = image

            # This 'with' statement is to address a bug with keras/tensorflow. It
            # doesn't have any functional effect.
            with graph.as_default():
                robot_pose = model.predict(images, batch_size=1, verbose=0)
                groundtruth = np.asarray(groundtruth)
                predictedPose = getPoseFromTensor(robot_pose)
                for pose in predictedPose:
                    value = "%.5f" % pose
                    fwrite.write(value + ',')
                error_xyz, theta_error = error(predictedPose, groundtruth)
                fwrite.write(str(error_xyz) + ',')
                fwrite.write(str(theta_error) + '\n')
            print(
                'img: {}, predicted pose is: {}, ground truth is: {}, position error: {} meters ,orientation error: {} degree'
                .format(temp[0], predictedPose, np.asarray(groundtruth),
                        error_xyz, theta_error))
        fwrite.close()
    else:
        img = cv2.imread(test_image_name)
        image = subtractMean(img, image_mean)
        images = np.empty((1, 224, 224, 3), dtype=float)
        images[0] = image
        with graph.as_default():
            robot_pose = model.predict(images, batch_size=1, verbose=0)
            predictedPose = getPoseFromTensor(robot_pose)
        print('img: {}, predicted pose is: {}'.format(test_image_name,
                                                      predictedPose))