Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Error when checking input: expected conv2d_1_input to have shape (216, 360, 3) but got array with shape (300, 500, 3) #266

Open
EdoQuasso opened this issue Dec 20, 2019 · 3 comments

Comments

@EdoQuasso
Copy link

EdoQuasso commented Dec 20, 2019

Hi, first of all thank you for your work.

I'm trying to use your framework to optimize hiperparameters in my Convolutional Neural Network in order to implement an image classifier.

I'm obtaining the following error:
ValueError: Error when checking input: expected conv2d_1_input to have shape (216, 360, 3) but got array with shape (300, 500, 3)

I have checked my data construction function and the training data returned have the right shape (216, 360, 3) but for some reason the model receive an input with shape (300, 50, 3).
I really don't know what to do, here is my code:

def dataAdv1():
    dfAll = pd.read_csv('filePath')
    labels = pd.read_csv('filePath')

    df = pd.read_csv('filePath')
    
    #this function returns a list of all the images
    imageList = createImageList(df, dfAll)
    imageTmp = imageList
    # this function is used to get the list of labels
    labels_list = clearAdvancingLabel(labels)
    labelTmp = labels_list
    #this function divide the data into train, validation and test set
    x_train, y_train, x_valid, y_valid, x_test, y_test = splitTrainValidationTest(imageTmp, labelTmp)
    
    y_train = y_train.ravel()
    y_valid = y_valid.ravel()
    y_test = y_test.ravel()
    
    return x_train, y_train, x_test, y_test, x_valid, y_valid


def create_modelAdvancing(x_train, y_train, x_test, y_test, x_valid, y_valid):
    model = Sequential()
    model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3), input_shape=(216, 360, 3)))
    model.add(Activation({{choice(['relu', 'tanh'])}}))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    
    num_layers = {{choice(['one', 'two', 'three', 'four'])}}
    
    if num_layers =='two':
        model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3)))
        model.add(Activation({{choice(['relu', 'tanh'])}}))
        model.add(MaxPooling2D(pool_size=(2, 2)))
    elif num_layers == 'three':
        model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3)))
        model.add(Activation({{choice(['relu', 'tanh'])}}))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3)))
        model.add(Activation({{choice(['relu', 'tanh'])}}))
        model.add(MaxPooling2D(pool_size=(2, 2)))
    elif num_layers == 'four':
        model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3)))
        model.add(Activation({{choice(['relu', 'tanh'])}}))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3)))
        model.add(Activation({{choice(['relu', 'tanh'])}}))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Conv2D({{choice([4, 16, 32, 64])}}, (3, 3)))
        model.add(Activation({{choice(['relu', 'tanh'])}}))
        model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())  
    model.add(Dense({{choice([4, 16, 32, 64])}}))
    model.add(Activation({{choice(['relu', 'tanh'])}}))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense(1))
    model.add(Activation({{choice(['softmax', 'sigmoid'])}}))
    
    chooseOptimizer = {{choice(['adam', 'sgd', 'rmsprop'])}}    
    model.compile(loss='binary_crossentropy', optimizer=chooseOptimizer, metrics=['accuracy'])

    model.fit(x_train, y_train,
              batch_size={{choice([4, 16, 32, 64])}},
              epochs={{choice([10, 30, 50])}},
              verbose=2,
              validation_data=(x_valid, y_valid))
    score, acc = model.evaluate(x_test, y_test, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model.to_yaml()}


# Create Spark context
conf = SparkConf().setAppName('Elephas_Hyperparameter_Optimization').setMaster('local[*]')
sc = SparkContext(conf=conf)

# Define hyper-parameter model and run optimization
hyperparam_model = HyperParamModel(sc)
hyperparam_model.minimize(model=create_modelAdvancing, data=dataAdv1, max_evals=5)

Thank you in advance for your help

@anasouzac
Copy link

Hey, guys! I'm having the same problem... I'm running my code on a Jupyter Notebook. Is that a problem?
Here's the error:

TypeError Traceback (most recent call last)
in
9 max_evals=5,
10 trials=Trials(),
---> 11 notebook_name='teste')
12 x_train, x_train, x_val, y_val = data()
13 print("Evalutation of best performing model:")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in minimize(model, data, algo, max_evals, trials, functions, rseed, notebook_name, verbose, eval_space, return_space, keep_temp)
67 notebook_name=notebook_name,
68 verbose=verbose,
---> 69 keep_temp=keep_temp)
70
71 best_model = None

~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in base_minimizer(model, data, functions, algo, max_evals, trials, rseed, full_model_string, notebook_name, verbose, stack, keep_temp)
96 model_str = full_model_string
97 else:
---> 98 model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack)
99 temp_file = './temp_model.py'
100 write_temp_files(model_str, temp_file)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack)
196
197 functions_string = retrieve_function_string(functions, verbose)
--> 198 data_string = retrieve_data_string(data, verbose)
199 model = hyperopt_keras_model(model_string, parts, aug_parts, verbose)
200

~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in retrieve_data_string(data, verbose)
217
218 def retrieve_data_string(data, verbose=True):
--> 219 data_string = inspect.getsource(data)
220 first_line = data_string.split("\n")[0]
221 indent_length = len(determine_indent(data_string))

~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getsource(object)
971 or code object. The source code is returned as a single string. An
972 OSError is raised if the source code cannot be retrieved."""
--> 973 lines, lnum = getsourcelines(object)
974 return ''.join(lines)
975

~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getsourcelines(object)
953 raised if the source code cannot be retrieved."""
954 object = unwrap(object)
--> 955 lines, lnum = findsource(object)
956
957 if istraceback(object):

~\AppData\Local\Continuum\anaconda3\lib\inspect.py in findsource(object)
766 is raised if the source code cannot be retrieved."""
767
--> 768 file = getsourcefile(object)
769 if file:
770 # Invalidate cache if needed.

~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getsourcefile(object)
682 Return None if no way can be identified to get the source.
683 """
--> 684 filename = getfile(object)
685 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
686 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]

~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getfile(object)
664 raise TypeError('module, class, method, function, traceback, frame, or '
665 'code object was expected, got {}'.format(
--> 666 type(object).name))
667
668 def getmodulename(path):

TypeError: module, class, method, function, traceback, frame, or code object was expected, got tuple

And my final code is:

from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform

if name == 'main':
best_run, best_model = optim.minimize(model=model,
data=data(),
algo=tpe.suggest,
max_evals=5,
trials=Trials(),
notebook_name='teste')
x_train, x_train, x_val, y_val = data()
print("Evalutation of best performing model:")
print(best_model.evaluate(X_test, Y_test))
print("Best performing model chosen hyper-parameters:")
print(best_run)

@JonnoFTW
Copy link
Collaborator

JonnoFTW commented Jul 14, 2020

@anasouza26 please make a new issue and use triple backticks at the start and end of your code (like this: ```) to format your code so we can actually read it. Avoid posting the same question in multiple issues as well.

@anasouzac
Copy link

@JonnoFTW Sure thing! Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants