首页 > 解决方案 > 具有云 mnist 数据的 Keras PointNet 自动编码器

问题描述

我在 keras pointNet 示例https://keras.io/examples/vision/pointnet/中添加了一个编码器,然后是一个解码器,但我有这些问题:

你有解决这些问题的提示吗?在我的代码中是 (4),我使用的是 tensorflow-gpu 2.4.1。我已经尝试:

(1) Encoded_Decoded_1 Encoded_decoded_2

(2) 损失

(3) Epoch 245/250 11/11 [===============================] - 38s 3s/step - loss: 0.6501 - sparse_categorical_accuracy: 0.0000e+00 - val_loss: 32737073675688970648939397120.0000 - val_sparse_categorical_accuracy: 0.0000e+00 Epoch 246/250 11/11 [============== =======] - 38s 3s/step - loss: 0.6508 - sparse_categorical_accuracy: 0.0000e+00 - val_loss: 5034634056772354048.0000 - val_sparse_categorical_accuracy: 0.0000e+00 Epoch 247/250 11/11 [====== =======================] - 38s 3s/步 - 损失:0.6508 - sparse_categorical_accuracy: 0.0000e+00 - val_loss: 290447969937276811457069056.0000 - val_sparse_categorical_accuracy: 0.000+ 00 纪元 248/250 11/11 [===============================] - 38s 3s/步 - 损失:0.6508 - sparse_categorical_accuracy:0.0000e+00 - val_loss:11876466.0000 - val_sparse_categorical_accuracy:0。0000e+00 时期 249/250 11/11 [===============================] - 38s 3s/步 - 损失: 0.6506 - sparse_categorical_accuracy: 0.0000e+00 - val_loss: 3841184579953962778624.0000 - val_sparse_categorical_accuracy: 0.0000e+00 250/250 11/11 [================= =======] - 38 秒 3 秒/步 - 损失:0.6501 - sparse_categorical_accuracy:0.0000e+00 - val_loss:1371222868048511175229440.0000 - val_sparse_categorical_accuracy:0.0000e+00

(4)

#####################
import ...

#####################
# load 3D mnist data and return cloud data into numpy array
def parse_dataset(num_points):

    train_points = []
    test_points = []

    folders = glob.glob(os.path.join(DATA_DIR, "[!README]*"))
    print('folders:', folders)

    for i, folder in enumerate(folders):
        # gather all files
        train_files = glob.glob(os.path.join(folder, "train/*"))
        test_files = glob.glob(os.path.join(folder, "test/*"))

        for f in train_files:
            train_points.append(trimesh.load(f).sample(num_points))
            train_labels.append(i)

        for f in test_files:
            test_points.append(trimesh.load(f).sample(num_points))
            test_labels.append(i)

    return (
        np.array(train_points),
        np.array(test_points),
    )


NUM_POINTS = 10000
BATCH_SIZE = 20
EPOCHS = 250

train_points, test_points = parse_dataset(NUM_POINTS)

# jitter and shuffle points
def augment(points):
    points += tf.random.uniform(points.shape, -0.005, 0.005, dtype=tf.float64)
    points = tf.random.shuffle(points)
    return points

train_dataset = tf.data.Dataset.from_tensor_slices(train_points)
test_dataset = tf.data.Dataset.from_tensor_slices(test_points)

train_dataset = train_dataset.shuffle(len(train_points)).map(augment).batch(BATCH_SIZE)
test_dataset = test_dataset.shuffle(len(test_points)).batch(BATCH_SIZE)

#####################
def conv_bn(x, filters):
    x = layers.Conv1D(filters, kernel_size=1, padding="valid")(x)
    x = layers.BatchNormalization(momentum=0.0)(x)
    return layers.Activation("relu")(x)

def dense_bn(x, filters):
    x = layers.Dense(filters)(x)
    x = layers.BatchNormalization(momentum=0.0)(x)
    return layers.Activation("relu")(x)

class OrthogonalRegularizer(keras.regularizers.Regularizer):
    def __init__(self, num_features, l2reg=0.001):
        self.num_features = num_features
        self.l2reg = l2reg
        self.eye = tf.eye(num_features)

    def __call__(self, x):
        x = tf.reshape(x, (-1, self.num_features, self.num_features))
        xxt = tf.tensordot(x, x, axes=(2, 2))
        xxt = tf.reshape(xxt, (-1, self.num_features, self.num_features))
        return tf.reduce_sum(self.l2reg * tf.square(xxt - self.eye))

def tnet(inputs, num_features):
    # Initalise bias as the indentity matrix
    bias = keras.initializers.Constant(np.eye(num_features).flatten())
    reg = OrthogonalRegularizer(num_features)

    x = conv_bn(inputs, 32)
    x = conv_bn(x, 64)
    x = conv_bn(x, 512)
    x = layers.GlobalMaxPooling1D()(x)
    x = dense_bn(x, 256)
    x = dense_bn(x, 128)
    x = layers.Dense(
        num_features * num_features,
        kernel_initializer="zeros",
        bias_initializer=bias,
        activity_regularizer=reg,
        )(x)
    feat_T = layers.Reshape((num_features, num_features))(x)
    # Apply affine transformation to input features
    return layers.Dot(axes=(2, 1))([inputs, feat_T])


nfeature=3

inputs = keras.Input(shape=(NUM_POINTS, nfeature))

# pointNet
x = tnet(inputs, nfeature)
x = conv_bn(x, 32)
x = conv_bn(x, 32)
x = tnet(x, 32)
x = conv_bn(x, 32)
x = conv_bn(x, 64)
x = conv_bn(x, 512)
x = layers.GlobalMaxPooling1D()(x)

# encoder
encoder = dense_bn(x, 500)
encoder = layers.Dropout(0.3)(encoder)
encoder = dense_bn(encoder, 490)
encoder = layers.Dropout(0.3)(encoder)
encoder = dense_bn(encoder, 480)
encoder = layers.Dropout(0.3)(encoder)

# decoder
decoder = layers.Dense(490, activation='relu')(encoder)
decoder = layers.Dense(500, activation='relu')(encoder)
decoder = layers.Dense(512, activation='relu')(encoder)
decoder = layers.Dense(NUM_POINTS*nfeature, activation='sigmoid')(encoder)
decoder = layers.Reshape((NUM_POINTS, nfeature))(decoder)

autoencoder = keras.Model(inputs=inputs, outputs=decoder)

autoencoder.summary()

autoencoder.compile(
    loss='kl_divergence',
    optimizer=keras.optimizers.Adam(learning_rate=0.001),
    metrics=["sparse_categorical_accuracy"],
)

autoencoder.fit(
    train_dataset,
    epochs=EPOCHS,
    validation_data=test_dataset
)

decoded = autoencoder.predict(test_points) 

标签: tf.keraspoint-clouds

解决方案


推荐阅读