The "dense3" layer is throwing an error due to incompatible input. It requires a minimum of 2 dimensions, but the input provided only has 1 dimension. The full shape of the input is (4096,)

Having recently delved into the realms of keras and machine learning, I am experiencing some issues with my code that I believe stem from my model. My objective is to train a model using a CSV file where each row represents a flattened image, essentially making each row of data an individual image. However, I keep encountering the following error message:

ValueError: Exception encountered when calling layer "sequential" (type Sequential).

Input 0 of layer "dense3" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (4096,)

Call arguments received by layer "sequential" (type Sequential):
  • inputs=tf.Tensor(shape=(4096,), dtype=uint8)
  • training=False
  • mask=None

I have explored various online resources and attempted multiple solutions, but being new to this field, there are still aspects that elude me. The dimensions of the image are 64x64, leading me to assume that the columns represent the input size. As a result, I set the input size to 64*64 and adjusted the predict batch size accordingly to ensure compatibility with the flatten image for real-time prediction. This approach resolved the issue for the initial layer (input layer), however, subsequent dense layers are proving problematic. Additionally, I have positioned the image label at the beginning of each row in the CSV file.

def modelTraining():
model = keras.models.Sequential()
model.add(keras.Input(shape=(4096,)))
model.add(keras.layers.Dense(4096, activation='relu', name = "dense2"), )
model.add(keras.layers.Dense(1, activation='softmax', name = "dense3", ))

model.compile(optimizer='rmsprop',
          loss='binary_crossentropy',
          metrics=['accuracy'])

#Retrieve training image data
trainingImageMatrix = []
trainingLabelsArray = []
temp = 0
with open(r'C:\Users\Jason\Desktop\signLanguage\testOpenCV\training.csv', 'r', newline='') as myFile:
    reader = csv.reader(myFile, delimiter=',')
    for row in reader:
        temp+=1
        imageDataToAdd = np.zeros(4096)
        for i in range (1, 4097):
            imageDataToAdd[i - 1] = row[i]
        trainingLabelsArray.append(int(row[0])) #Extracting the first index of each row
        trainingImageMatrix.append(imageDataToAdd)
        print("reading row: "+str(temp))
        #if temp >=10:
            #break
    trainingImageMatrix = np.array(trainingImageMatrix)
    trainingLabelsArray = np.array(trainingLabelsArray)
model.fit(trainingImageMatrix, trainingLabelsArray, epochs = 20, batch_size = 4096)
return model

model.predict(grayFrame.flatten(), batch_size = 4096) 

The above line appears in another segment of my code, where it currently serves as my prediction method.

I would greatly appreciate guidance on how to accurately specify the size and tailor the layers according to the requirements of my project.

Each row contains the label at index 0 followed by pixel brightness representing a 64x64 image ---- CSV FILE IMAGE

Answer №1

After working with this specific dataset, I encountered an issue when converting the npy file to csv. The problem stemmed from grayFrame.flatten() resulting in a one-dimensional output. To fix this error, it was necessary to add an extra dimension. Here is the modified line:

model.predict(tf.expand_dims(grayFrame.flatten(),axis=0), batch_size = 4096) 

You can access the sample code here. Appreciate your assistance!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The date provider in Factory Boy is producing a string value

I've been working on an app where I am using the factory_boy package to generate random data, but I'm having some trouble with the date provider :( class MyModel(models.Model): date = models.DateField() class MyModelFactory(factory.DjangoMo ...

What steps are needed to set up PyCharm for running py.test tests?

As I embark on the journey of writing unit tests for my Python code, I have decided to go with the py.test framework instead of Python's bundled unittest. After adding a "tests" directory to my project and including test_sample.py in it, my next step ...

Ways to determine the version of the Selenium RC server

I am currently utilizing selenium2 RC along with the python client (selenium.py) and I am in search of a way to retrieve the version of the selenium installed on the server. Can anyone advise if there is a command that can be sent to the server to fetch ...

Ways to execute a singular python function concurrently on multiple occasions

I am looking to run a Python function in parallel 100 times. Can anyone provide the code snippet to achieve this? from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium import webdriver from selenium.webdriver.chrome ...

Tips for creating an HTML report using Python

Currently, I am searching for a solution to convert my saved matplotlib graphs as png files along with some data frames into HTML format. This is similar to how I typically use R2HTML in R. Despite my efforts, I have not been able to locate clear informat ...

What is the best way to import a CSV file directly into a Pandas dataframe using a function attribute?

Recently, I developed a function to compute the log returns of a given dataset. The function accepts the file name in CSV format as an argument and is expected to output a dataframe containing the log returns from the dataset. The CSV file has already been ...

What is the best way to utilize the data in a file as a parameter?

I recently installed AutoKey on my computer with the intention of being able to run python scripts using keyboard shortcuts. Specifically, I wanted to create a script that would simulate pressing the "d" key followed by the "s" key in a loop, with the abil ...

Combining Starlette and pydantic for optimal performance

Could you provide guidance on using Pydantic to extract user input in a Starlette application? I would appreciate any assistance. class Post(BaseModel): title:str content:str @app.route("/createposts",methods=["POST"]) async def crea ...

Issue encountered: Module not found error persists even though the module is installed and appears in the list generated by pip

After installing biopython and related modules, I attempted to load them with the following code: from BCBio.GFF import GFFExaminer import pprint from BCBio import GFF However, I encountered the following error: ----------------------------------------- ...

Selenium ceases to function intermittently

I am attempting to extract data from a website that has a total of 9000 pages. However, the extraction process stops after retrieving approximately 1700 pages and then restarts from the beginning and continues for about 1000 more pages. Is there a way to s ...

What is the information stored inside the div element?

How can I extract the contents of a div using bs4? >>> Doc <div class="document"> <p>Text.</p> <p>More text</p> </div> >>> type(Doc) bs4.element.Tag I am looking to retrieve: <p>Text.</p> ...

Utilize Beautiful Soup, Selenium, and Pandas to extract price information by scraping the web for values stored within specified div class

My goal is to retrieve the price of a product based on its size, as prices tend to change daily. While I succeeded in extracting data from a website that uses "a class," I am facing difficulties with websites that use div and span classes. Link: Price: $ ...

Python: Enable F6 Key at specific time intervals

What is the best method to set up the active window to activate the F6 key every specified number of seconds, with 'x' being a user-entered value? The program should stop upon hitting a designated key combination like Ctrl+Z. Any recommendations ...

Navigating the complex world of Tkinter Text Widgets Indexing Mechanism

Is there a way to divide a decimal number into its whole and fractional parts? For example : 1.24345 should be separated into 1 and 24345 1455.24 should be divided into 1455 and 24 1455.0 should result in 1455 as the whole number and 0 as the fractiona ...

Enter into a namespace

Recently, I added a new folder to my project in order to utilize methods from files in another project. The process involved adding just the first 3 lines: import sys import os sys.path.insert(0, '/path/to/another/dir') from file_methods import ...

Utilizing Django fixtures: Importing HTML content as JSON into a Text Field

Currently in the process of transitioning my website to Django from another content management system, I am facing challenges with importing HTML into Django using fixtures. The specific issue revolves around "Events", which is a model within my Django app ...

Combining Python, Neo4j, and JSON to uniquely create a new NODE for each user

I am looking to create a Python script that can accomplish the following tasks: Load a JSON file containing user data Create a node for each user in the JSON file ## Sample User Data {'UserName': 'lolipop', 'UserId': '5 ...

group and apply operations to all remaining keys

If I have a pandas dataframe called df, I can find the average reading ability for each age by using the code df.groupby('Age').apply(lambda x: x['ReadingAbility'].mean()). But what if I want to find the average reading ability for all ...

Unable to access the Python gstreamer components

I've been referring to a tutorial at to develop a sample gstreamer element using Python. Despite my efforts, I'm unable to get GStreamer to recognize it. I've tried adjusting the GST_PLUGIN_PATH without success. While GStreamer can locate c ...

Dividing a string in Python without using square brackets

If I have a variable called nums with a value of "12345", is there a way to print it as individual characters, like just the number 1 or 2, without using bracket notation (i.e. print(num[0]))? ...