我正在创建一个以Tensorflow作为后端的Keras模型 . 以下是我正在创建的模型的摘要:Model
以下是加载培训和测试数据的代码:
X_train = np.load('data_train.npy')
Y_train = np.load('labels_train.npy')
X_test = np.load('data_test.npy')
Y_test = np.load('labels_test.npy')
X_train和X_test的形状为[?,608,608,3],而Y_train和Y_test的形状为[?,19,19,5] .
根据总结,这个模型的最终输出应该是维度(?,19,19,5),但是当我检查model.predict()的形状时,结果证明是(?,19,5, 5) .
这是我用于训练和预测输出的代码:
mmodel = model(X_train.shape[1:])
mmodel.compile('adam',cust_loss)
mmodel.fit(X_train, Y_train, epochs=40, batch_size=25)
o_p = nucleus_model.predict(X_test)
print(o_p.shape) # prints (134, 19, 5, 5)
为什么会发生这种情况?我尝试在CPU和GPU上运行 . 还尝试使用由较少数量的训练示例组成的输入 .
我正在为最后一层定义自定义丢失函数和自定义激活方法 . 为他们提供片段,以防他们在调试问题时有任何用处:
def cust_loss(y_true, y_pred):
class_error = tf.reduce_sum(tf.multiply((y_true[:,:,0]-y_pred[:,:,0]),(y_true[:,:,0]-y_pred[:,:,0])))
row_error = tf.reduce_sum(tf.multiply((y_true[:,:,1]-y_pred[:,:,1]),(y_true[:,:,1]-y_pred[:,:,1])))
col_error = tf.reduce_sum(tf.multiply((y_true[:,:,2]-y_pred[:,:,2]),(y_true[:,:,2]-y_pred[:,:,2])))
h_error = tf.reduce_sum(tf.abs(tf.sqrt(y_true[:,:,3])-tf.sqrt(y_pred[:,:,3])))
w_error = tf.reduce_sum(tf.abs(tf.sqrt(y_true[:,:,4])-tf.sqrt(y_pred[:,:,4])))
e1 = tf.add(class_error,row_error)
e2 = tf.add(e1,col_error)
e3 = tf.add(e2,h_error)
e4 = tf.add(e3,w_error)
return e4
def custom_activation(x):
return tf.concat([tf.sigmoid(x[:,:,0:1]) , tf.nn.relu(x[:,:,1:5])],axis = 2)
get_custom_objects().update({'custom_activation': Activation(custom_activation)})