network = networks[str(config[parser_args.task]['NETWORK'])]
for idx, row in grid_df.iterrows(): `
if row['timestamp'] == 'None':
learning_rate = row['lr']
spatial_dropout_rate = row['s_d']
temporal_dropout_rate = row['t_d']
concat_dropout_rate = row['c_d']
curr_time_n = datetime.datetime.now()
curr_time = curr_time_n.strftime("%Y-%m-%d %H:%M:%S")
time_passed_since_start = curr_time_n - start_time_n
time_passed_since_start_in_min = time_passed_since_start/datetime.timedelta(minutes=1)
time_passed_since_start_in_hours = time_passed_since_start_in_min/60
if time_passed_since_start_in_hours >= 50.:
#print(f'Not enough time for lr = {}, dropout = {}')
grid_df.to_csv(grid_file, index=False)
sys.exit(0)
else:
grid_df.loc[idx, 'timestamp'] = curr_time
#print(f"Hostname: {socket.gethostname()}, Number of classes = {num_classes}\n Learning `rate = {learning_rate}\n` dropout rate = {dropout_rate}\n timestamp = {curr_time}")
train_generator = crop_generator(input_path=tr_path, batch_size=batch_size, mode="train", do_shuffle=True, epsilon=0)
val_generator = crop_generator(input_path=val_path, batch_size=batch_size, mode="val", do_shuffle=True, epsilon=0)
# spatial part starts here
cnn_base_model = network(include_top=False, weights = 'imagenet',classes=num_classes)
cnn_output = cnn_base_model.output
img_avgpool = GlobalAveragePooling2D()(cnn_output)
img_dense_1 = Dense(512, activation="relu")(img_avgpool)
img_dropout = Dropout(spatial_dropout_rate)(img_dense_1)
img_dense_2 = Dense(256, activation="relu")(img_dropout)
# spatial part ends here
# temporal part starts here
temporal_input_layer = Input(batch_shape = (None, 23, 1), name='time_input_layer')
# add an LSTM layer
lstm_1 = LSTM(100, input_shape=(23,1), dropout=temporal_dropout_rate)(`temporal_input_layer`)
# lstm_2 = LSTM(64)(lstm_1)
#x = LSTM(32)(x)
# add a dense layer
ts_output = Dense(32, activation="relu")(lstm_1)
# temporal part ends here
# add a concatenation layer to combine output of spatial and temporal
#ts_output.add(tf.keras.layers.Flatten())
final_merged = concatenate([img_dense_2, ts_output])
# add another dropout
concat_dropout = Dropout(concat_dropout_rate)(final_merged)
# add dense layer
final_dense = Dense(32, activation="relu")(concat_dropout)
# and a softmax layer -- num_classes
predictions = Dense(num_classes, activation='softmax')(final_dense)
optimizer = Adam(lr=0.0005)
# this is the model we will train
model = Model(inputs=[cnn_base_model.input, temporal_input_layer], outputs=predictions)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
我的代码附加在上面,我面临的错误如下:(这是附加的完整跟踪错误)
File "train-gridsearch.py", line 203, in <module>
history = model.fit(train_generator,
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/training.py", line 1187, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/tmp/__autograph_generated_file52u8rjgw.py", line 15, in tf__train_function
retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 232, in assert_input_compatibility
raise ValueError(
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 232, in assert_input_compatibility
raise ValueError(
ValueError: Input 0 of layer "zero_padding2d" is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: (None, 1)
我看到错误提到了“zero_padding2d”,但我还没有看到研究人员使用过它。有人可以帮忙吗?