问题
I am trying to implement jpeg compression as a noise layer in keras. during my implemention, I need to change the shape and I am puzzled how can I do this. so I try to explain what did I do and what do I want to do. first, in the following function, I produced all DCT coefficient of an 8x8 block that produces a filter with shape 64x8x8. each one of these 64 filters is the DCT coefficients for one pixel in final DCT transform output.
def gen_filters(size_x: int, size_y: int, dct_or_idct_fun: callable) -> np.ndarray:
tile_size_x = 8
filters = np.zeros((size_x * size_y, size_x, size_y))
for k_y in range(size_y):
for k_x in range(size_x):
for n_y in range(size_y):
for n_x in range(size_x):
filters[k_y * tile_size_x + k_x, n_y, n_x] = dct_or_idct_fun(n_y, k_y, size_y) * dct_or_idct_fun(n_x,
k_x,
size_x)
return filters
because we can not use assignment in keras, I have to implement the DCT transform using convolve layer using the following code.
image_conv = Kr.backend.conv2d(image_yuv_ch,filters,strides=(8,8),data_format='channels_first')
but I have some problem with the above code. if the input image_yuv_ch was 32x32x1 and the filters was 64x8x8, how would I change the shape in order to implement the convolve layer? because with these shape it produced an error. does keras consider the first number in filters 's shape as the number of filter and 8x8 as the size of the filter in the above code?
I also have another question. if the convolve layer produced the things I want, means output with shape 64x4x4, as we know each element of the 4x4 filter as a vector of length 64 is the DCT value of the 8x8 block of the input image and now I will need to reshape each 64 vector to 8x8 block and put them beside each other and make 32x32x1 from 64x4x4. but I really do not know how can I do this? do you have any suggestion for me? I look forward to hearing about your suggestion. Thank you.
来源:https://stackoverflow.com/questions/55913348/how-do-i-change-the-shape-of-a-tensor-with-shape-64x4x4x3-to-another-shape-as-th