Giter Site home page Giter Site logo

ValueError: Error when checking input: expected conv2d_1_input to have shape (3, 496, 369) but got array with shape (496, 369, 3) about car-sound-classification-with-keras HOT 7 CLOSED

karodievas avatar karodievas commented on June 13, 2024
ValueError: Error when checking input: expected conv2d_1_input to have shape (3, 496, 369) but got array with shape (496, 369, 3)

from car-sound-classification-with-keras.

Comments (7)

xuhaoteoh avatar xuhaoteoh commented on June 13, 2024

Hi, is this a major issue?

from car-sound-classification-with-keras.

xuhaoteoh avatar xuhaoteoh commented on June 13, 2024

Hi, is this a major issue?

from car-sound-classification-with-keras.

KaroDievas avatar KaroDievas commented on June 13, 2024

Hello,

Sorry for late response I didn't have time to check this one.
if I remember correctly you shouldn't need to change any KerasModel

I can't open yours TrainModel it seems like file corrupted, but you should use correct loss and class_mode functions and define classes as well

from car-sound-classification-with-keras.

xuhaoteoh avatar xuhaoteoh commented on June 13, 2024

Hello, this is my KerasModel script, I just changed the activation function of the outer layer becasue I am doing multiclass classification.

`from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

class KerasModel:
def get_model(self, img_width, img_height):
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3,img_width, img_height), data_format='channels_first'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(32, (3, 3),data_format='channels_first'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.35))

    model.add(Conv2D(64, (3, 3),data_format='channels_first'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.35))

    model.add(Conv2D(128, (3, 3),data_format='channels_first'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.35))

    model.add(Conv2D(256, (3, 3),data_format='channels_first'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.35))

    model.add(Conv2D(512, (3, 3),data_format='channels_first'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.35))

    model.add(Flatten())
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('softmax'))
    return model

`

from car-sound-classification-with-keras.

xuhaoteoh avatar xuhaoteoh commented on June 13, 2024

This is my TrainModel code

from keras.preprocessing.image import ImageDataGenerator
from Models.KerasModel import KerasModel
from keras.callbacks import ModelCheckpoint
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import matplotlib.pyplot as plt
from keras import backend as K

K.set_image_data_format('channels_first')
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:

Restrict TensorFlow to only allocate 1GB of memory on the first GPU

try:
tf.config.experimental.set_virtual_device_configuration(
gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Virtual devices must be set before GPUs have been initialized
print(e)

img_width, img_height = 496, 369
train_data_dir = 'Data/train'
validation_data_dir = 'Data/validation'
nb_train_samples = 4667
nb_validation_samples = 4667
nb_epochs = 30
batch_size = 32

kerasModel = KerasModel()
model = kerasModel.get_model(img_width, img_height)

model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

this is the augmentation configuration we will use for training

train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
data_format='channels_first')

this is the augmentation configuration we will use for testing:

only rescaling

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
classes=['Honda', 'Perodua','Proton','Toyota'],
batch_size=batch_size,
class_mode='categorical')

class_dictionary = train_generator.class_indices

print('class dictionary', class_dictionary)

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
color_mode='rgb',
classes=['Honda', 'Perodua','Proton','Toyota'],
batch_size=batch_size,
class_mode='categorical')

check_pointer = ModelCheckpoint(filepath='weights.hdf5', verbose=1, save_best_only=True)
history = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples/batch_size,
epochs=nb_epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples/batch_size,
callbacks=[check_pointer])

list all data in history

print(history.history.keys())

summarize history for accuracy

plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

summarize history for loss

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

from car-sound-classification-with-keras.

xuhaoteoh avatar xuhaoteoh commented on June 13, 2024

now I have faced another problem, which is

ValueError: Error when checking target: expected activation_8 to have shape (1,) but got array with shape (4,)

Can you please guide me on which part of the code is wrong?

from car-sound-classification-with-keras.

KaroDievas avatar KaroDievas commented on June 13, 2024

Hello,

It's due

model.add(Dense(1))

This line means that you expect only one result at the end of model. You should put here number of class which you expect

from car-sound-classification-with-keras.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.