我正在尝试解决超参数调整情况下的错误:
我正在尝试处理的图像具有(64, 64, 3)
(宽度、高度、通道)的形状。(64, 64, 1)
由于我正在执行二进制分类任务,因此相应的标签具有形状,但对于每个像素。
我使用的模型是 UNET 模型:
def make_model(dropout_c1, dropout_c2, dropout_c3, dropout_c4,
dropout_c5, final_activation, optimizer='Adam'):
inputs = Input((64, 64, 3))
s = Lambda(lambda x: x / 255) (inputs)
c1 = Conv2D(64, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (s)
c1 = Dropout(dropout_c1) (c1)
c1 = Conv2D(16, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c1)
p1 = MaxPooling2D((2, 2)) (c1)
c2 = Conv2D(32, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (p1)
c2 = Dropout(dropout_c2) (c2)
c2 = Conv2D(32, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c2)
p2 = MaxPooling2D((2, 2)) (c2)
c3 = Conv2D(64, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (p2)
c3 = Dropout(dropout_c3) (c3)
c3 = Conv2D(64, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c3)
p3 = MaxPooling2D((2, 2)) (c3)
c4 = Conv2D(128, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (p3)
c4 = Dropout(dropout_c4) (c4)
c4 = Conv2D(128, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c4)
p4 = MaxPooling2D(pool_size=(2, 2)) (c4)
c5 = Conv2D(256, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (p4)
c5 = Dropout(dropout_c5) (c5)
c5 = Conv2D(256, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c5)
u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (c5)
u6 = concatenate([u6, c4])
c6 = Conv2D(128, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (u6)
c6 = Dropout(dropout_c4) (c6)
c6 = Conv2D(128, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c6)
u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (c6)
u7 = concatenate([u7, c3])
c7 = Conv2D(64, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (u7)
c7 = Dropout(dropout_c3) (c7)
c7 = Conv2D(64, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c7)
u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same') (c7)
u8 = concatenate([u8, c2])
c8 = Conv2D(32, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (u8)
c8 = Dropout(dropout_c2) (c8)
c8 = Conv2D(32, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c8)
u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (c8)
u9 = concatenate([u9, c1], axis=3)
c9 = Conv2D(16, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (u9)
c9 = Dropout(dropout_c1) (c9)
c9 = Conv2D(16, (3, 3), activation='relu',
kernel_initializer='he_normal', padding='same') (c9)
outputs = Conv2D(1, (1, 1), activation=final_activation) (c9)
model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer=optimizer, loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()
return model
输出:
print(np.shape(X_train))
print(np.shape(y_train))
是:
(65792, 64, 64, 3)
(65792, 64, 64, 1)
-
cnn_model = KerasClassifier(build_fn=make_model)
batch_size_ = [64, 128, 256]
dropout_c1_ = [0.1, 0.3]
dropout_c2_ = [0.1, 0.3]
dropout_c3_ = [0.1, 0.3]
dropout_c4_ = [0.1, 0.3]
dropout_c5_ = [0.1, 0.3]
optimizer_ = ['Adam', 'RMSprop', 'Adagrad', 'SGD']
final_activation_ = ['sigmoid', 'softmax']
param_grid={'optimizer' : optimizer_,
'dropout_c1': dropout_c1_,
'dropout_c2': dropout_c2_,
'dropout_c3': dropout_c3_,
'dropout_c4': dropout_c4_,
'dropout_c5': dropout_c5_,
'final_activation': final_activation_,
'epochs': [30],
'batch_size': batch_size_
}
filepath = "best_cnn.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='acc', verbose=1,
save_best_only=True, mode='max')
grid = GridSearchCV(cnn_model, param_grid, cv=2,
scoring='average_precision', n_jobs=1, verbose=1)
来电:
grid_result = grid.fit(X_train, y_train, callbacks=[checkpoint])
将导致以下错误:
Fitting 2 folds for each of 768 candidates, totalling 1536 fits
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-49-b1fdcdb4a9af> in <module>
----> 1 grid_result = grid.fit(X_train, y_train, callbacks=[checkpoint])
~/venv/lib/python3.6/site-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)
720 return results_container[0]
721
--> 722 self._run_search(evaluate_candidates)
723
724 results = results_container[0]
~/venv/lib/python3.6/site-packages/sklearn/model_selection/_search.py in _run_search(self, evaluate_candidates)
1189 def _run_search(self, evaluate_candidates):
1190 """Search all candidates in param_grid"""
-> 1191 evaluate_candidates(ParameterGrid(self.param_grid))
1192
1193
~/venv/lib/python3.6/site-packages/sklearn/model_selection/_search.py in evaluate_candidates(candidate_params)
709 for parameters, (train, test)
710 in product(candidate_params,
--> 711 cv.split(X, y, groups)))
712
713 all_candidate_params.extend(candidate_params)
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __call__(self, iterable)
915 # remaining jobs.
916 self._iterating = False
--> 917 if self.dispatch_one_batch(iterator):
918 self._iterating = self._original_iterator is not None
919
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in dispatch_one_batch(self, iterator)
757 return False
758 else:
--> 759 self._dispatch(tasks)
760 return True
761
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in _dispatch(self, batch)
714 with self._lock:
715 job_idx = len(self._jobs)
--> 716 job = self._backend.apply_async(batch, callback=cb)
717 # A job can complete so quickly than its callback is
718 # called before we get here, causing self._jobs to
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py in apply_async(self, func, callback)
180 def apply_async(self, func, callback=None):
181 """Schedule a func to be run"""
--> 182 result = ImmediateResult(func)
183 if callback:
184 callback(result)
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py in __init__(self, batch)
547 # Don't delay the application, to avoid keeping the input
548 # arguments in memory
--> 549 self.results = batch()
550
551 def get(self):
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __call__(self)
223 with parallel_backend(self._backend, n_jobs=self._n_jobs):
224 return [func(*args, **kwargs)
--> 225 for func, args, kwargs in self.items]
226
227 def __len__(self):
~/venv/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in <listcomp>(.0)
223 with parallel_backend(self._backend, n_jobs=self._n_jobs):
224 return [func(*args, **kwargs)
--> 225 for func, args, kwargs in self.items]
226
227 def __len__(self):
~/venv/lib/python3.6/site-packages/sklearn/model_selection/_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, error_score)
526 estimator.fit(X_train, **fit_params)
527 else:
--> 528 estimator.fit(X_train, y_train, **fit_params)
529
530 except Exception as e:
~/venv/lib/python3.6/site-packages/keras/wrappers/scikit_learn.py in fit(self, x, y, sample_weight, **kwargs)
204 y = np.searchsorted(self.classes_, y)
205 else:
--> 206 raise ValueError('Invalid shape for y: ' + str(y.shape))
207 self.n_classes_ = len(self.classes_)
208 if sample_weight is not None:
ValueError: Invalid shape for y: (32896, 64, 64, 1)