Keras - UpSampling2D layer appears to be returning a 2D tensor rather than 4D

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I am feeding 1080x1616 color photos into a convolutional neural network in Keras. The data is fed through the network just fine, until it gets to the last convolutional layer, throwing an error saying that it expects a 4D tensor, but instead gets a (32,1) tensor. I don't understand where this number is being generated.

This is my model architecture:

model = keras.Sequential([
        Conv2D(32, (3,3), padding='same', activation='relu', data_format="channels_last", input_shape=(self.x_res,self.y_res,self.n_channels), kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2,2)),
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),   
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(self.n_channels, (1, 1), padding='same', activation='sigmoid', kernel_initializer=keras
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

The input to your model should be of shape (batch_size, x_res, y_res, n_channels), please check it.

Following is a simple test:

import keras
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D
import numpy as np

x_res = 1080
y_res = 1616
n_channels = 3

model = keras.Sequential([
        Conv2D(32, (3,3), padding='same', activation='relu', data_format="channels_last", input_shape=(x_res,y_res,n_channels), kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2,2)),
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),   
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(n_channels, 
                                                    
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.