maxpumperla / hyperas

Keras + Hyperopt: A very simple wrapper for convenient hyperparameter optimization
http://maxpumperla.com/hyperas/
MIT License
2.18k stars 318 forks source link

SyntaxError: invalid syntax File "<unknown>", line 418 #225

Closed naikshubham closed 5 years ago

naikshubham commented 5 years ago

Getting below error on executing the code.

Traceback (most recent call last):

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 7, in notebook_name='handwritten_vs_printed_text_classifier')

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\hyperas\optim.py", line 69, in minimize keep_temp=keep_temp)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\hyperas\optim.py", line 98, in base_minimizer model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\hyperas\optim.py", line 189, in get_hyperopt_model_string imports = extract_imports(cleaned_source, verbose)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\hyperas\utils.py", line 40, in extract_imports tree = ast.parse(source)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\ast.py", line 35, in parse return compile(source, filename, mode, PyCF_ONLY_AST)

File "", line 418 img = image.load_img(,grayscale=True, target_size=(img_width, img_height)) ^ SyntaxError: invalid syntax

Below is the code used

def one_hotlabel(img): label = img.split('')[0][0] if label == 'h': ohl = np.array([1,0]) elif label == 'l' or 'p': ohl = np.array([0,1]) return ohl

def train_data_with_label(): train_images = [] train_data_imgs = [file for file in os.listdir(train_data) if file.endswith(".jpg") or file.endswith(".JPG")] for i in train_data_imgs: path = os.path.join(train_data, i) img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (100, 32)) img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1] train_images.append([np.array(img), one_hot_label(i)]) shuffle(train_images) return train_images

def test_data_with_label(): test_images = [] test_data_imgs = [file for file in os.listdir(test_data) if file.endswith(".jpg") or file.endswith(".JPG")] for i in test_data_imgs: path = os.path.join(test_data, i) img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (100, 32)) img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1] test_images.append([np.array(img), one_hot_label(i)]) shuffle(test_images) return test_images`

training_images = train_data_with_label() testing_images = test_data_with_label()

tr_img_data = np.array([i[0] for i in training_images]).reshape(-1,100,32,1) tr_lbl_data = np.array([i[1] for i in training_images])

tst_img_data = np.array([i[0] for i in testing_images]).reshape(-1,100,32,1) tst_lbl_data = np.array([i[1] for i in testing_images])

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

def data(): X_train, X_val, y_train, y_val = train_test_split(tr_img_data, tr_lbl_data, test_size=0.2, random_state=12345) X_train = X_train.reshape(-1, 100, 32, 1) X_val = X_val.reshape(-1,100,32,1) X_train = X_train.astype('float32') X_val = X_val.astype('float32') X_train /= 255 X_val /= 255 return X_train, y_train, X_val, y_val

`def create_model(X_train, Y_train, X_val, Y_val): model = Sequential() model.add(Dense({{choice([128, 256, 512, 1024])}}, input_shape=[100, 32, 1])) model.add(Activation({{choice(['relu', 'sigmoid'])}})) model.add(Dropout({{uniform(0, 1)}})) model.add(Dense({{choice([128, 256, 512, 1024])}})) model.add(Activation({{choice(['relu', 'sigmoid'])}})) model.add(Dropout({{uniform(0, 1)}}))

if conditional({{choice(['two', 'three'])}}) == 'three':
    model.add(Dense({{choice([128, 256, 512, 1024])}}))
    model.add(Activation({{choice(['relu', 'sigmoid'])}}))
    model.add(Dropout({{uniform(0, 1)}}))

model.add(Dense(10))
model.add(Activation('softmax'))

adam = keras.optimizers.Adam(lr={{choice([10**-3, 10**-2, 10**-1])}})
rmsprop = keras.optimizers.RMSprop(lr={{choice([10**-3, 10**-2, 10**-1])}})
sgd = keras.optimizers.SGD(lr={{choice([10**-3, 10**-2, 10**-1])}})

choiceval = {{choice(['adam', 'sgd', 'rmsprop'])}}
if choiceval == 'adam':
    optim = adam
elif choiceval == 'rmsprop':
    optim = rmsprop
else:
    optim = sgd

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

model.fit(X_train, Y_train,
          batch_size={{choice([100,128,256,512])}},
          nb_epoch=20,
          verbose=2,
          validation_data=(X_val, Y_val))
score, acc = model.evaluate(X_val, Y_val, verbose=0)
print('Test accuracy:', acc)
return {'loss': -acc, 'status': STATUS_OK, 'model': model}`

if __name__ == '__main__': best_run, best_model = optim.minimize(model=create_model, data=data, algo=tpe.suggest, max_evals=30, trials=Trials(), notebook_name='handwritten_vs_printed_text_classifier')