Python keras.backend.set_image_dim_ordering() Examples

The following are 11 code examples for showing how to use keras.backend.set_image_dim_ordering(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module keras.backend , or try the search function .

Example 1
Project: keras-transfer-learning-for-oxford102   Author: Arsey   File: util.py    License: MIT License 6 votes vote down vote up
def set_img_format():
    try:
        if K.backend() == 'theano':
            K.set_image_data_format('channels_first')
        else:
            K.set_image_data_format('channels_last')
    except AttributeError:
        if K._BACKEND == 'theano':
            K.set_image_dim_ordering('th')
        else:
            K.set_image_dim_ordering('tf') 
Example 2
Project: keras-video-classifier   Author: chen0040   File: vgg16_bidirectional_lstm_train.py    License: MIT License 6 votes vote down vote up
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(patch_path('..'))

    from keras_video_classifier.library.recurrent_networks import VGG16BidirectionalLSTMVideoClassifier
    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf

    data_set_name = 'UCF-101'
    input_dir_path = patch_path('very_large_data')
    output_dir_path = patch_path('models/' + data_set_name)
    report_dir_path = patch_path('reports/' + data_set_name)

    np.random.seed(42)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    load_ucf(input_dir_path)

    classifier = VGG16BidirectionalLSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, data_set_name=data_set_name)

    plot_and_save_history(history, VGG16BidirectionalLSTMVideoClassifier.model_name,
                          report_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-history.png') 
Example 3
Project: keras-video-classifier   Author: chen0040   File: vgg16_lstm_hi_dim_train.py    License: MIT License 6 votes vote down vote up
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf

    data_set_name = 'UCF-101'
    input_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    output_dir_path = os.path.join(os.path.dirname(__file__), 'models', data_set_name)
    report_dir_path = os.path.join(os.path.dirname(__file__), 'reports', data_set_name)

    np.random.seed(42)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    load_ucf(input_dir_path)

    classifier = VGG16LSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, vgg16_include_top=False,
                             data_set_name=data_set_name)

    plot_and_save_history(history, VGG16LSTMVideoClassifier.model_name,
                          report_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-history.png') 
Example 4
Project: keras-video-classifier   Author: chen0040   File: vgg16_bidirectional_lstm_hi_dim_train.py    License: MIT License 6 votes vote down vote up
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.recurrent_networks import VGG16BidirectionalLSTMVideoClassifier
    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf

    data_set_name = 'UCF-101'
    input_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    output_dir_path = os.path.join(os.path.dirname(__file__), 'models', data_set_name)
    report_dir_path = os.path.join(os.path.dirname(__file__), 'reports', data_set_name)

    np.random.seed(42)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    load_ucf(input_dir_path)

    classifier = VGG16BidirectionalLSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, vgg16_include_top=False,
                             data_set_name=data_set_name)

    plot_and_save_history(history, VGG16BidirectionalLSTMVideoClassifier.model_name,
                          report_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-hi-dim-history.png') 
Example 5
Project: keras-video-classifier   Author: chen0040   File: vgg16_lstm_train.py    License: MIT License 6 votes vote down vote up
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf

    data_set_name = 'UCF-101'
    input_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    output_dir_path = os.path.join(os.path.dirname(__file__), 'models', data_set_name)
    report_dir_path = os.path.join(os.path.dirname(__file__), 'reports', data_set_name)

    np.random.seed(42)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    load_ucf(input_dir_path)

    classifier = VGG16LSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, data_set_name=data_set_name)

    plot_and_save_history(history, VGG16LSTMVideoClassifier.model_name,
                          report_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-history.png') 
Example 6
Project: openai_lab   Author: kengz   File: conv_dqn.py    License: MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        from keras.layers.core import Dense, Flatten
        from keras.layers.convolutional import Convolution2D
        from keras import backend as K
        if K.backend() == 'theano':
            K.set_image_dim_ordering('tf')
        self.Dense = Dense
        self.Flatten = Flatten
        self.Convolution2D = Convolution2D

        self.kernel = 4
        self.stride = (2, 2)
        super(ConvDQN, self).__init__(*args, **kwargs) 
Example 7
Project: keras-video-classifier   Author: chen0040   File: vgg16_lstm_hi_dim_predict.py    License: MIT License 5 votes vote down vote up
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf, scan_ucf_with_labels

    vgg16_include_top = False
    data_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    model_dir_path = os.path.join(os.path.dirname(__file__), 'models', 'UCF-101')
    config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(model_dir_path,
                                                                     vgg16_include_top=vgg16_include_top)
    weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(model_dir_path,
                                                                     vgg16_include_top=vgg16_include_top)

    np.random.seed(42)

    load_ucf(data_dir_path)

    predictor = VGG16LSTMVideoClassifier()
    predictor.load_model(config_file_path, weight_file_path)

    videos = scan_ucf_with_labels(data_dir_path, [label for (label, label_index) in predictor.labels.items()])

    video_file_path_list = np.array([file_path for file_path in videos.keys()])
    np.random.shuffle(video_file_path_list)

    correct_count = 0
    count = 0

    for video_file_path in video_file_path_list:
        label = videos[video_file_path]
        predicted_label = predictor.predict(video_file_path)
        print('predicted: ' + predicted_label + ' actual: ' + label)
        correct_count = correct_count + 1 if label == predicted_label else correct_count
        count += 1
        accuracy = correct_count / count
        print('accuracy: ', accuracy) 
Example 8
Project: Benchmarks   Author: ECP-CANDLE   File: p2b1_baseline_keras2.py    License: MIT License 5 votes vote down vote up
def initialize_parameters(default_model = 'p2b1_default_model.txt'):

    # Build benchmark object
    p2b1Bmk = p2b1.BenchmarkP2B1(p2b1.file_path, default_model, 'keras',
    prog='p2b1_baseline', desc='Train Molecular Frame Autoencoder - Pilot 2 Benchmark 1')

    # Initialize parameters
    GP = candle.finalize_parameters(p2b1Bmk)
    #p2b1.logger.info('Params: {}'.format(gParameters))

    print ('\nTraining parameters:')
    for key in sorted(GP):
        print ("\t%s: %s" % (key, GP[key]))

    # print json.dumps(GP, indent=4, skipkeys=True, sort_keys=True)

    if GP['backend'] != 'theano' and GP['backend'] != 'tensorflow':
        sys.exit('Invalid backend selected: %s' % GP['backend'])

    os.environ['KERAS_BACKEND'] = GP['backend']
    reload(K)
    '''
    if GP['backend'] == 'theano':
        K.set_image_dim_ordering('th')
    elif GP['backend'] == 'tensorflow':
        K.set_image_dim_ordering('tf')
    '''
    K.set_image_data_format('channels_last')
#"th" format means that the convolutional kernels will have the shape (depth, input_depth, rows, cols)

#"tf" format means that the convolutional kernels will have the shape (rows, cols, input_depth, depth)
    print ("Image data format: ", K.image_data_format())
#    print "Image ordering: ", K.image_dim_ordering()
    return GP 
Example 9
Project: keras-text-to-image   Author: chen0040   File: dcgan_v3.py    License: MIT License 5 votes vote down vote up
def __init__(self):
        K.set_image_dim_ordering('tf')
        self.generator = None
        self.discriminator = None
        self.model = None
        self.img_width = 7
        self.img_height = 7
        self.img_channels = 1
        self.random_input_dim = 100
        self.text_input_dim = 100
        self.config = None
        self.glove_source_dir_path = './very_large_data'
        self.glove_model = GloveModel() 
Example 10
Project: keras-text-to-image   Author: chen0040   File: dcgan_v2.py    License: MIT License 5 votes vote down vote up
def __init__(self):
        K.set_image_dim_ordering('tf')
        self.generator = None
        self.discriminator = None
        self.model = None
        self.img_width = 7
        self.img_height = 7
        self.img_channels = 1
        self.text_input_dim = 100
        self.config = None
        self.glove_source_dir_path = './very_large_data'
        self.glove_model = GloveModel() 
Example 11
Project: keras-text-to-image   Author: chen0040   File: dcgan.py    License: MIT License 5 votes vote down vote up
def __init__(self):
        K.set_image_dim_ordering('tf')
        self.generator = None
        self.discriminator = None
        self.model = None
        self.img_width = 7
        self.img_height = 7
        self.img_channels = 1
        self.random_input_dim = 100
        self.text_input_dim = 100
        self.config = None
        self.glove_source_dir_path = './very_large_data'
        self.glove_model = GloveModel()