我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用keras.layers.Conv3D()。
def test_keras_import(self): # Pad 1D model = Sequential() model.add(ZeroPadding1D(2, input_shape=(224, 3))) model.add(Conv1D(32, 7, strides=2)) model.build() self.pad_test(model, 'pad_w', 2) # Pad 2D model = Sequential() model.add(ZeroPadding2D(2, input_shape=(224, 224, 3))) model.add(Conv2D(32, 7, strides=2)) model.build() self.pad_test(model, 'pad_w', 2) # Pad 3D model = Sequential() model.add(ZeroPadding3D(2, input_shape=(224, 224, 224, 3))) model.add(Conv3D(32, 7, strides=2)) model.build() self.pad_test(model, 'pad_w', 2) # ********** Export json tests ********** # ********** Data Layers Test **********
def build_model(dropout_rate = 0.2): input_image = Input(shape = IMAGE_SHAPE, dtype = 'float32', name = INPUT_IMAGE) x = MaxPooling2D()(input_image) x = MaxPooling2D()(x) x = MaxPooling2D()(x) x = MaxPooling2D()(x) x = Dropout(dropout_rate)(x) x = Conv2D(32, kernel_size=3, strides=(2,2))(x) x = MaxPooling2D()(x) x = Conv2D(32, kernel_size=3, strides=(2,2))(x) x = MaxPooling2D()(x) x = Dropout(dropout_rate)(x) image_out = Flatten()(x) # image_out = Dense(32, activation='relu')(conv) input_lidar_panorama = Input(shape = PANORAMA_SHAPE, dtype = 'float32', name = INPUT_LIDAR_PANORAMA) x = pool_and_conv(input_lidar_panorama) x = pool_and_conv(x) x = Dropout(dropout_rate)(x) panorama_out = Flatten()(x) input_lidar_slices = Input(shape = SLICES_SHAPE, dtype = 'float32', name = INPUT_LIDAR_SLICES) x = MaxPooling3D(pool_size=(2,2,1))(input_lidar_slices) x = Conv3D(32, kernel_size=3, strides=(2,2,1))(x) x = MaxPooling3D(pool_size=(2,2,1))(x) x = Dropout(dropout_rate)(x) x = Conv3D(32, kernel_size=2, strides=(2,2,1))(x) x = MaxPooling3D(pool_size=(2,2,1))(x) x = Dropout(dropout_rate)(x) slices_out = Flatten()(x) x = keras.layers.concatenate([image_out, panorama_out, slices_out]) x = Dense(32, activation='relu')(x) x = Dense(32, activation='relu')(x) x = Dense(32, activation='relu')(x) pose_output = Dense(9, name=OUTPUT_POSE)(x) model = Model(inputs=[input_image, input_lidar_panorama, input_lidar_slices], outputs=[pose_output]) # Fix error with TF and Keras import tensorflow as tf tf.python.control_flow_ops = tf model.compile(loss='mean_squared_error', optimizer='adam') return model
def test_conv3d(self): keras_model = Sequential() keras_model.add(Conv3D(8, (5, 5, 5), input_shape=(3, 8, 8, 8), name='conv')) keras_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD()) pytorch_model = Conv3DNet() self.transfer(keras_model, pytorch_model) self.assertEqualPrediction(keras_model, pytorch_model, self.test_data_3d, delta=1e-4)
def test_keras_export(self): tests = open(os.path.join(settings.BASE_DIR, 'tests', 'unit', 'keras_app', 'keras_export_test.json'), 'r') response = json.load(tests) tests.close() net = yaml.safe_load(json.dumps(response['net'])) net = {'l0': net['Input'], 'l1': net['Input2'], 'l2': net['Input4'], 'l3': net['Convolution']} # Conv 1D net['l1']['connection']['output'].append('l3') net['l3']['connection']['input'] = ['l1'] net['l3']['params']['layer_type'] = '1D' net['l3']['shape']['input'] = net['l1']['shape']['output'] net['l3']['shape']['output'] = [128, 12] inp = data(net['l1'], '', 'l1')['l1'] temp = convolution(net['l3'], [inp], 'l3') model = Model(inp, temp['l3']) self.assertEqual(model.layers[2].__class__.__name__, 'Conv1D') # Conv 2D net['l0']['connection']['output'].append('l0') net['l3']['connection']['input'] = ['l0'] net['l3']['params']['layer_type'] = '2D' net['l3']['shape']['input'] = net['l0']['shape']['output'] net['l3']['shape']['output'] = [128, 226, 226] inp = data(net['l0'], '', 'l0')['l0'] temp = convolution(net['l3'], [inp], 'l3') model = Model(inp, temp['l3']) self.assertEqual(model.layers[2].__class__.__name__, 'Conv2D') # Conv 3D net['l2']['connection']['output'].append('l3') net['l3']['connection']['input'] = ['l2'] net['l3']['params']['layer_type'] = '3D' net['l3']['shape']['input'] = net['l2']['shape']['output'] net['l3']['shape']['output'] = [128, 226, 226, 18] inp = data(net['l2'], '', 'l2')['l2'] temp = convolution(net['l3'], [inp], 'l3') model = Model(inp, temp['l3']) self.assertEqual(model.layers[2].__class__.__name__, 'Conv3D')
def create_model(self): inputs = Input(shape=(self.x, self.y, self.z, self.channel_size)) masked_inputs = MaskConv(self.mask_value)(inputs) outputs = MaskConvNet( Conv3D( self.filters, self.kernel, strides=self.strides ) )(masked_inputs) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def layer_test_helper_flatten_3d(layer, channel_index, data_format): # This should test that the output is the correct shape so it should pass # into a Dense layer rather than a Conv layer. # The weighted layer is the previous layer, # Create model main_input = Input(shape=list(random.randint(10, 20, size=4))) x = Conv3D(3, [3, 3, 2], data_format=data_format)(main_input) x = layer(x) x = Flatten()(x) main_output = Dense(5)(x) model = Model(inputs=main_input, outputs=main_output) # Delete channels del_layer_index = 1 next_layer_index = 4 del_layer = model.layers[del_layer_index] surgeon = Surgeon(model) surgeon.add_job('delete_channels', del_layer, channels=channel_index) new_model = surgeon.operate() new_w = new_model.layers[next_layer_index].get_weights() # Calculate next layer's correct weights flat_sz = np.prod(layer.get_output_shape_at(0)[1:]) channel_count = getattr(del_layer, utils.get_channels_attr(del_layer)) channel_index = [i % channel_count for i in channel_index] if data_format == 'channels_first': delete_indices = [x * flat_sz // channel_count + i for x in channel_index for i in range(0, flat_sz // channel_count, )] elif data_format == 'channels_last': delete_indices = [x + i for i in range(0, flat_sz, channel_count) for x in channel_index] else: raise ValueError correct_w = model.layers[next_layer_index].get_weights() correct_w[0] = np.delete(correct_w[0], delete_indices, axis=0) assert weights_equal(correct_w, new_w)
def test_keras_import(self): # Conv 1D model = Sequential() model.add(Conv1D(32, 3, kernel_regularizer=regularizers.l2(0.01), bias_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l2(0.01), kernel_constraint='max_norm', bias_constraint='max_norm', activation='relu', input_shape=(1, 16))) model.build() self.keras_param_test(model, 1, 9) # Conv 2D model = Sequential() model.add(Conv2D(32, (3, 3), kernel_regularizer=regularizers.l2(0.01), bias_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l2(0.01), kernel_constraint='max_norm', bias_constraint='max_norm', activation='relu', input_shape=(1, 16, 16))) model.build() self.keras_param_test(model, 1, 13) # Conv 3D model = Sequential() model.add(Conv3D(32, (3, 3, 3), kernel_regularizer=regularizers.l2(0.01), bias_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l2(0.01), kernel_constraint='max_norm', bias_constraint='max_norm', activation='relu', input_shape=(1, 16, 16, 16))) model.build() self.keras_param_test(model, 1, 17) # This is currently unavailable with Theano backend