二维Tensorflow离散余弦变换非常慢

时间:2019-05-08 19:35:18

标签: python tensorflow keras deep-learning fft

我正在构建一个样本神经网络,该网络以2D形式吸收余弦变换后的MNIST图像,并具有将余弦进行逆变换的第一层。但是,它非常慢,每个纪元需要4.5分钟的训练时间。 Tensorflow没有内置的2D余弦变换或逆变换;谁能建议替代方法或任何方法以加快速度?

Process: com.example.android.motivatinalapp, PID: 10115
    java.lang.RuntimeException: Failed to invoke clarifai2.dto.model.output_info.UnknownOutputInfo() with no args

编辑:添加了用于生成输入数据的功能:

import itertools
import numpy as np
import matplotlib.pyplot as plt 
import keras
from keras.datasets import mnist
from keras.models import load_model, Sequential
from keras.layers import Dense, Dropout, Flatten, Lambda
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.datasets import cifar10
import tensorflow as tf

def idct2d_tf(img_batch):
  img_new = tf.transpose(img_batch, perm=(0, 3, 1, 2))
  img_new = tf.spectral.idct(img_new)
  img_new = tf.transpose(img_new, perm=(0, 1, 3, 2))
  img_new = tf.spectral.idct(img_new)
  img_new = tf.transpose(img_new, perm=(0, 2, 3, 1))
  return img_new

# train model, has IDCT layer first, accepts DCT of MNIST images 
model = Sequential()
model.add(Lambda(idct2d_tf, output_shape=(img_rows, img_cols, 1)))
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train_dct, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test_dct, y_test))
score = model.evaluate(x_test_dct, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

0 个答案:

没有答案