Tensorflowで次の畳み込みニューラルネットワーク(CNN)クラスを作成しました。[わかりやすくするためにいくつかのコード行を省略しようとしました。]Tensorflowで複数のモデルを読み込む
class CNN:
def __init__(self,
num_filters=16, # initial number of convolution filters
num_layers=5, # number of convolution layers
num_input=2, # number of channels in input
num_output=5, # number of channels in output
learning_rate=1e-4, # learning rate for the optimizer
display_step = 5000, # displays training results every display_step epochs
num_epoch = 10000, # number of epochs for training
batch_size= 64, # batch size for mini-batch processing
restore_file=None, # restore file (default: None)
):
# define placeholders
self.image = tf.placeholder(tf.float32, shape = (None, None, None,self.num_input))
self.groundtruth = tf.placeholder(tf.float32, shape = (None, None, None,self.num_output))
# builds CNN and compute prediction
self.pred = self._build()
# I have already created a tensorflow session and saver objects
self.sess = tf.Session()
self.saver = tf.train.Saver()
# also, I have defined the loss function and optimizer as
self.loss = self._loss_function()
self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.loss)
if restore_file is not None:
print("model exists...loading from the model")
self.saver.restore(self.sess,restore_file)
else:
print("model does not exist...initializing")
self.sess.run(tf.initialize_all_variables())
def _build(self):
#builds CNN
def _loss_function(self):
# computes loss
#
def train(self, train_x, train_y, val_x, val_y):
# uses mini batch to minimize the loss
self.sess.run(self.optimizer, feed_dict = {self.image:sample, self.groundtruth:gt})
# I save the session after n=10 epochs as:
if epoch%n==0:
self.saver.save(sess,'snapshot',global_step = epoch)
# finally my predict function is
def predict(self, X):
return self.sess.run(self.pred, feed_dict={self.image:X})
私は独立して2つの別々のタスクのための2つのCNNsを訓練してきました。それぞれ約1日かかりました。モデル1とモデル2はそれぞれ「snapshot-model1-10000
」と「snapshot-model2-10000
」(対応するメタファイル)として保存されています。私は各モデルをテストし、パフォーマンスを別々に計算できます。
ここでは、これらの2つのモデルを1つのスクリプトにロードします。私は自然に以下のように行うことをしようとするだろう:
cnn1 = CNN(..., restore_file='snapshot-model1-10000',..........)
cnn2 = CNN(..., restore_file='snapshot-model2-10000',..........)
私はエラーが発生した[エラーメッセージが長いです。私はそれのスニペットをコピー/ペーストしました。]
NotFoundError: Tensor name "Variable_26/Adam_1" not found in checkpoint files /home/amitkrkc/codes/A549_models/snapshot-hela-95000
[[Node: save_1/restore_slice_85 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/restore_slice_85/tensor_name, save_1/restore_slice_85/shape_and_slice)]]
は、これらの2つのファイル二つの別々のCNNsからロードする方法はありますか?どんな提案/コメント/フィードバックも歓迎します。
ありがとう、
ありがとうございます。私の場合、あなたの最初の提案はうまくいきます。 – Amit